#include "types.h" // class iActiveObject { public: virtual void run(void) = 0; void *taskHandle; };
// - //PE.10, PE.11 - stat3 stat4 //PA.4, PA.5 - stat1 stat 2 // PE.10, PE.11, PA.4, PA.5 , c.174 CD00240194.pdf GPIOE->MODER |= GPIO_MODER_MODER10_0; GPIOE->MODER |= GPIO_MODER_MODER11_0; GPIOA->MODER |= GPIO_MODER_MODER4_0; GPIOA->MODER |= GPIO_MODER_MODER5_0;
#include "types.h" // tU8 #define LEDS_NUMBER 4 class cLedsDriver { public: explicit cLedsDriver(void); void ledOn(const tU8 led); void ledOff(const tU8 led); void ledToggle(const tU8 led); private: static tPort ledsPort[LEDS_NUMBER]; static const tU16 ledsPin[LEDS_NUMBER]; };
#include "ledsdriver.h" // tLeds #include <stm32l1xx.h> // STM32 #include "susuassert.h" // for ASSERT #include "types.h" // tPort, tU16, tU8 #include "bitutil.h" // #define LED1_PIN GPIO_OTYPER_ODR_4 #define LED1_PORT GPIOA #define LED2_PIN GPIO_OTYPER_ODR_5 #define LED2_PORT GPIOA #define LED3_PIN GPIO_OTYPER_ODR_10 #define LED3_PORT GPIOE #define LED4_PIN GPIO_OTYPER_ODR_11 #define LED4_PORT GPIOE tPort cLedsDriver::ledsPort[LEDS_NUMBER] = {LED1_PORT, LED2_PORT, LED3_PORT, LED4_PORT}; const tU16 cLedsDriver::ledsPin[LEDS_NUMBER] = {LED1_PIN, LED2_PIN, LED3_PIN, LED4_PIN}; /******************************************************************************* * Function: constructor * Description: ******************************************************************************/ cLedsDriver::cLedsDriver(void) { } /******************************************************************************* * Function: ledOn * Description: ******************************************************************************/ void cLedsDriver::ledOn(const tU8 led) { ASSERT(led < LEDS_NUMBER); SETBIT(this->ledsPort[led]->ODR, this->ledsPin[led]); } /******************************************************************************* * Function: ledOff * Description: ******************************************************************************/ void cLedsDriver::ledOff(const tU8 led) { ASSERT(led < LEDS_NUMBER); CLRBIT(this->ledsPort[led]->ODR, this->ledsPin[led]); } /******************************************************************************* * Function: ledToggle * Description: ******************************************************************************/ void cLedsDriver::ledToggle(const tU8 led) { ASSERT(led < LEDS_NUMBER); TOGGLEBIT(this->ledsPort[led]->ODR, this->ledsPin[led]); }
#include "ledsdriver.h" // cLedsDriver #include "iactiveobject.h" // iActiveObject typedef enum { LD_led1 = 0, LD_led2 = 1, LD_led3 = 2, LD_led4 = 3, LD_none = 4 } tLeds; class cLedsDirector: public iActiveObject { public: explicit cLedsDirector(void); void run(void); private: cLedsDriver* pLedsDriver; };
#include "ledsdirector.h" // #include "frtoswrapper.h" // oRTOS #include "types.h" // l #define LED_DELAY (tU32)500/portTICK_PERIOD_MS /******************************************************************************* * Function: constructor * Description: cLedsDriver ******************************************************************************/ cLedsDirector::cLedsDirector(void) { this->pLedsDriver = new cLedsDriver(); } /******************************************************************************* * Function: runTask * Description: . led3 ******************************************************************************/ void cLedsDirector::run(void) { for(;;) { oRTOS.taskDelay(LED_DELAY); this->pLedsDriver->ledToggle(LD_led3); } }
#define LEDSDIRECTOR_STACK_SIZE configMINIMAL_STACK_SIZE #define LEDSDIRECTOR_PRIORITY (tU32)2 // , oRTOS // RTOS , - // , cRTOS oRTOS; .... void main(void) { cLedsDirector *pLedsDirector = new cLedsDirector(); oRTOS.taskCreate(pLedsDirector, LEDSDIRECTOR_STACK_SIZE, LEDSDIRECTOR_PRIORITY, "Leds"); oRTOS.startScheduler(); }
#include "types.h" // tU16 tU8 #define BUTTONS_NUMBER 2 typedef enum { BS_buttonNotPressed = 0, BS_buttonPressed = 1 } tButtonState; class cButtonsDriver { public: explicit cButtonsDriver(); tButtonState getButtonState(const tU8 button); private: static tPort buttonsPort[BUTTONS_NUMBER]; static const tU16 buttonsPin[BUTTONS_NUMBER]; static const tBoolean buttonsTrigger[BUTTONS_NUMBER]; };
#include "buttonsdriver.h" // tLeds #include <stm32l1xx.h> // STM32 #include "susuassert.h" //for ASSERT #include "types.h" // tPort, tU16, tU8 #define BUTTON1_PIN GPIO_OTYPER_IDR_13 #define BUTTON1_PORT GPIOC #define BUTTON2_PIN GPIO_OTYPER_IDR_0 #define BUTTON2_PORT GPIOA tPort cButtonsDriver::buttonsPort[BUTTONS_NUMBER] = {BUTTON1_PORT, BUTTON2_PORT}; const tU16 cButtonsDriver::buttonsPin[BUTTONS_NUMBER] = {BUTTON1_PIN, BUTTON2_PIN}; // , 0, 1 const tBoolean cButtonsDriver::buttonsTrigger[BUTTONS_NUMBER] = {FALSE, TRUE}; /******************************************************************************* * Function: constructor * Description: ******************************************************************************/ cButtonsDriver::cButtonsDriver() { } /******************************************************************************* * Function: getButtonState * Description: , ******************************************************************************/ tButtonState cButtonsDriver::getButtonState(const tU8 button) { tButtonState eState = BS_buttonNotPressed; ASSERT(button < BUTTONS_NUMBER); // , 1, 0, // , , // // . tBoolean isLogicalZero = !(this->buttonsPort[button]->IDR & this->buttonsPin[button]); if(isLogicalZero ^ this->buttonsTrigger[button]) { eState = BS_buttonPressed; } return eState; }
#include "types.h" // tU32 #include "iactiveobject.h" // iActiveObject #include "buttonsdriver.h" // cButtonsDriver #include "frtosWrapper.h" // tTaskHandle typedef enum { BT_button1 = 0, BT_button2 = 1, BT_none = 2 } tButtons; class cButtonsController: public iActiveObject { public: explicit cButtonsController(const tTaskHandle *pTaskToNotify, const tU32 countOfNotifiedTask); tButtons getPressedButton(void) const { return pressedButton; }; void run(void); private: cButtonsDriver* pButtonsDriver; tButtons getButton(void); tButtons pressedButton; const tTaskHandle *pTaskToNotify; tU32 countOfNotifiedTask; };
#include "buttonscontroller.h" // #include "susuassert.h" // ASSERT #include "types.h" // tPort, tU16, tU8 #include "bitutil.h" // #define BUTTON_TASK_DELAY (tU32) 50/portTICK_PERIOD_MS #define NEXT_PRESS_DELAY (tU32) 500/portTICK_PERIOD_MS /******************************************************************************* * Function: constructor * Description: * . . ******************************************************************************/ cButtonsController::cButtonsController(const tTaskHandle *pTaskToNotify, const tU32 countOfNotifiedTask) { ASSERT(pTaskToNotify != NULL); this->pButtonsDriver = new cButtonsDriver(); this->pTaskToNotify = pTaskToNotify; this->countOfNotifiedTask = countOfNotifiedTask; } /******************************************************************************* * Function: run * Description: * ******************************************************************************/ void cButtonsController::run(void) { tRtosStatus eStatus = RS_fail; tButtons eButtonPreviousState = BT_none; tButtons eButtonCurrentState = BT_none; const tTaskHandle *pTaskHandle; tU32 i = 0; for(;;) { eButtonPreviousState = this->getButton(); if (eButtonPreviousState != BT_none) { // oRTOS.taskDelay(BUTTON_TASK_DELAY); eButtonCurrentState = this->getButton(); if (eButtonPreviousState == eButtonCurrentState) { pTaskHandle = this->pTaskToNotify; i = 0; // , while ((pTaskHandle != NULL) && (i != countOfNotifiedTask)) { eStatus = oRTOS.taskNotify(*(pTaskHandle), (tU32)eButtonCurrentState, eSetValueWithOverwrite); if(eStatus == RS_fail) { ;// } pTaskHandle++; i++; } // 0.5 oRTOS.taskDelay(NEXT_PRESS_DELAY); } } oRTOS.taskDelay(BUTTON_TASK_DELAY); } } /******************************************************************************* * Function: getPressedButton * Description: ******************************************************************************/ tButtons cButtonsController::getButton(void) { tButtons eButton = BT_none; if (BS_buttonPressed == this->pButtonsDriver->getButtonState(BT_button1)) { eButton = BT_button1; } else if (BS_buttonPressed == this->pButtonsDriver->getButtonState(BT_button2)) { eButton = BT_button2; } this->pressedButton = eButton; return eButton; }
#include <stm32l1xx.h> // STM2 #include "ledsdirector.h" // cLedsDirector #include "buttonscontroller.h" // cButtonsController #include "types.h" // #include "frtoswrapper.h" // cRtos #define LEDS_TASK_HANDLE_INDEX 0 #define BUTTON_TASKS_NOTYFIED_NUM 1 #define LEDSDIRECTOR_STACK_SIZE configMINIMAL_STACK_SIZE #define LEDSDIRECTOR_PRIORITY (tU32)2 #define BUTTONSCONTROLLER_STACK_SIZE 256//configMINIMAL_STACK_SIZE #define BUTTONSCONTROLLER_PRIORITY (tU32)3 // , oRTOS // RTOS , - // , :) cRTOS oRTOS; .. void main( void ) { // ButtonControllera // , . //, static tTaskHandle tasksToNotifyFromButton[BUTTON_TASKS_NOTYFIED_NUM]; cLedsDirector *pLedsDirector = new cLedsDirector(); oRTOS.taskCreate(pLedsDirector, LEDSDIRECTOR_STACK_SIZE, LEDSDIRECTOR_PRIORITY, "Leds"); tasksToNotifyFromButton[LEDS_TASK_HANDLE_INDEX] = pLedsDirector->taskHandle; cButtonsController *pButtonsController = new cButtonsController(tasksToNotifyFromButton, BUTTON_TASKS_NOTYFIED_NUM); oRTOS.taskCreate(pButtonsController, BUTTONSCONTROLLER_STACK_SIZE, BUTTONSCONTROLLER_PRIORITY, "Buttons"); oRTOS.startScheduler(); }
Source: https://habr.com/ru/post/261807/
All Articles