DrRandom

Multiple Software TImers

Jun 21st, 2020
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.95 KB | None | 0 0
  1. /** TIMER ARRAYS **/
  2. #define MAX_TIMERS 255
  3.  
  4. int Timer_Counter = 0;
  5. typedef struct{
  6.     long Duration;
  7.     long Start;
  8.     int PIN;
  9.     uint16_t BUFFER_PIN[1] = {0x0000};
  10.     boolean State;
  11.     String ID;
  12.     uint16_t Address;
  13.     boolean Is_On = false;
  14. }Timer_Struct;
  15.  
  16. Timer_Struct Timers[MAX_TIMERS];
  17.  
  18. /** TIMER ARRAYS **/
  19.  
  20. static const inline void Timer_Stop(int Nth){
  21.     Timers[Nth].Is_On = false;  // I hope thats enough to turn off the timer of that pin
  22. }
  23.  
  24. static const inline void Timer_Checker_Loop(){
  25.     if(Timers[Timer_Counter].Is_On == true){    // Let's loop trought the array where the timer properties are ( check if on )
  26.         if(millis() - Timers[Timer_Counter].Start >= Timers[Timer_Counter].Duration){
  27.             // timer is fired at this point, the duration is timed out
  28.             struct QueueSmart PinDec;                           // add to the queue a struct to be able to toggle the pin
  29.             PinDec.Pin      = Timers[Timer_Counter].PIN;        // wich pin to toggle
  30.             PinDec.Address  = Timers[Timer_Counter].Address;    // Wich address the pin is on
  31.             PinDec.ID       = Timers[Timer_Counter].ID;         // The id for the web to toggle the button
  32.             PinDec.Timer    = Timers[Timer_Counter].Duration;   // the duration? idk why
  33.             xQueueSend(xQueue, &PinDec, portMAX_DELAY);         // Actually send to the queue
  34.             // Fired!
  35.         }
  36.     }
  37.     if(Timer_Counter == MAX_TIMERS){Timer_Counter = 0;}     // Reset the counter if we are at the end
  38.     Timer_Counter++;
  39. }
  40.  
  41. static const inline void Timer_Start(int Nth,int Duration,int PIN,String ID,uint16_t Address){
  42.     // Start a Timer, we need some settings to set!
  43.     Timers[Nth].Duration    = Duration * 1000;  // Timer duration ( user set it in seconds so have to convert to millis)
  44.     Timers[Nth].Start       = millis();         // The actual timer start time
  45.     Timers[Nth].PIN         = PIN;              // The PIN to switch on or off
  46.     Timers[Nth].ID          = ID;               // The ID of the PIN to be able to toggle it on the web after we set it
  47.     Timers[Nth].Address     = Address;          // The address of the expander where the pin is
  48.     Timers[Nth].Is_On       = true;             // Timer toggle on/off bit
  49. }
  50.  
  51.  
  52. static const inline void Write_To_Expand(uint16_t Address,int PIN,String ID,int Timer){
  53.     //vTaskSuspend(ModBus_Task_Handle);
  54.     xSemaphoreTake( xMutex, portMAX_DELAY );
  55.     vTaskDelay(10);
  56.     if (!mb.slave()) {
  57.         boolean Is_Set = false;
  58.         if(bitRead(Timers[PIN].BUFFER_PIN[0],PIN) == 0){
  59.             bitSet(Timers[PIN].BUFFER_PIN[0],PIN);
  60.             Is_Set = true;
  61.         }else{
  62.             bitClear(Timers[PIN].BUFFER_PIN[0],PIN);
  63.             Is_Set = false;
  64.         }
  65.         if(mb.writeHreg(Address,0x4032,Timers[PIN].BUFFER_PIN,1,cbWriteSmart)){
  66.             if(Is_Set){
  67.                 Timer_Start(PIN,Timer,PIN,ID,Address);
  68.                 Send_Async(ID,";true;Gomb");
  69.             }else{
  70.                 Timer_Stop(PIN);
  71.                 Send_Async(ID,";false;Gomb");
  72.             }
  73.         }
  74.         while (mb.slave()){mb.task();}
  75.     }
  76.     vTaskDelay(10);
  77.     //vTaskResume(ModBus_Task_Handle);
  78.     xSemaphoreGive( xMutex );
  79. }
  80.  
  81. void Smart_Switch_Run( void * parameter ){
  82.     for ever{
  83.         if(xQueue != NULL){
  84.             struct QueueSmart PINProp;
  85.             if(xQueueReceive(xQueue, &PINProp, portMAX_DELAY) == pdPASS){
  86.                 Write_To_Expand(PINProp.Address,PINProp.Pin,PINProp.ID,PINProp.Timer);
  87.             }
  88.         }
  89.         Timer_Checker_Loop();
  90.         vTaskDelay(1);
  91.     }
  92. }
Add Comment
Please, Sign In to add comment