DrRandom

Struktúrált array

Jun 21st, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.55 KB | None | 0 0
  1. /** TIMER ARRAYS **/
  2. #define MAX_TIMERS 255
  3.  
  4. int Timer_Counter = 10;
  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_Struct Timers[MAX_TIMERS];
  19.  
  20. /** TIMER ARRAYS **/
  21.  
  22. static const inline void Timer_Stop(int Nth){
  23.     Timers[Nth]->Is_On = false;  // I hope thats enough to turn off the timer of that pin
  24.     Send_Async("Timer Stopped"," gf");
  25.     delete Timers[Nth];
  26. }
  27.  
  28. static const inline void Timer_Checker_Loop(){
  29.     if(Timers[Timer_Counter] != 0){
  30.     if(Timers[Timer_Counter]->Is_On == true){    // Let's loop trought the array where the timer properties are ( check if on )
  31.         if(millis() - Timers[Timer_Counter]->Start >= Timers[Timer_Counter]->Duration){
  32.             // timer is fired at this point, the duration is timed out
  33.             Timers[Timer_Counter]->Is_On = false;
  34.             struct QueueSmart PinDec;                           // add to the queue a struct to be able to toggle the pin
  35.             PinDec.Pin      = Timers[Timer_Counter]->PIN;        // wich pin to toggle
  36.             PinDec.Address  = Timers[Timer_Counter]->Address;    // Wich address the pin is on
  37.             PinDec.ID       = Timers[Timer_Counter]->ID;         // The id for the web to toggle the button
  38.             PinDec.Timer    = Timers[Timer_Counter]->Duration;   // the duration? idk why
  39.             xQueueSend(xQueue, &PinDec, portMAX_DELAY);         // Actually send to the queue
  40.             // Fired!
  41.         }
  42.     }
  43.     }
  44.     Timer_Counter++;
  45.     if(Timer_Counter == MAX_TIMERS){Timer_Counter = 0;}     // Reset the counter if we are at the end
  46. }
  47.  
  48. static const inline void Timer_Start(int Nth,int Duration,int PIN,String ID,uint16_t Address){
  49.     // Start a Timer, we need some settings to set!
  50.     if(Timers[Nth] != 0){
  51.         Timers[Nth] = new Timer_Struct;
  52.     }
  53.     Timers[Nth]->Duration    = Duration * 1000;  // Timer duration ( user set it in seconds so have to convert to millis)
  54.     Timers[Nth]->Start       = millis();         // The actual timer start time
  55.     Timers[Nth]->PIN         = PIN;              // The PIN to switch on or off
  56.     Timers[Nth]->ID          = ID;               // The ID of the PIN to be able to toggle it on the web after we set it
  57.     Timers[Nth]->Address     = Address;          // The address of the expander where the pin is
  58.     Timers[Nth]->Is_On       = true;             // Timer toggle on/off bit
  59.  
  60.     String lk = "Timer Started with: ";
  61.     lk += String(Timers[Nth]->Duration);
  62.     lk += " duration.";
  63.     Send_Async(lk," TimStart");
  64. }
  65.  
  66.  
  67. bool cbWriteSmart(Modbus::ResultCode event, uint16_t transactionId, void* data) {
  68.     if(event == mb.EX_SUCCESS){
  69.         return true;
  70.     }else{
  71.         return false;
  72.     }
  73. }
  74.  
  75. static const inline void Check_All_Struct(uint16_t Address,uint16_t SETTED_PIN){
  76.     for(int i = 0; i < MAX_TIMERS;i++){
  77.         if(Timers[i] != 0){
  78.         if(Timers[i]->Address == Address){
  79.             Timers[i]->BUFFER_PIN[0] = SETTED_PIN;
  80.         }
  81.         }
  82.     }
  83. }
  84.  
  85. static const inline void Write_To_Expand(uint16_t Address,int PIN,String ID,int Timer){
  86.     //vTaskSuspend(ModBus_Task_Handle);
  87.     xSemaphoreTake( xMutex, portMAX_DELAY );
  88.     vTaskDelay(10);
  89.     if (!mb.slave()) {
  90.         boolean Is_Set = false;
  91.         if(bitRead(Timers[PIN]->BUFFER_PIN[0],PIN) == 0){
  92.             bitSet(Timers[PIN]->BUFFER_PIN[0],PIN);
  93.             Is_Set = true;
  94.         }else{
  95.             bitClear(Timers[PIN]->BUFFER_PIN[0],PIN);
  96.             Is_Set = false;
  97.         }
  98.         Check_All_Struct(Address,Timers[PIN]->BUFFER_PIN[0]);
  99.         if(mb.writeHreg(Address,0x4032,Timers[PIN]->BUFFER_PIN,1,cbWriteSmart)){
  100.             if(Is_Set){
  101.                 Timer_Start(PIN,Timer,PIN,ID,Address);
  102.                 Send_Async(ID,";true;Gomb");
  103.             }else{
  104.                 Timer_Stop(PIN);
  105.                 Send_Async(ID,";false;Gomb");
  106.             }
  107.         }
  108.         while (mb.slave()){mb.task();}
  109.     }
  110.     vTaskDelay(10);
  111.     //vTaskResume(ModBus_Task_Handle);
  112.     xSemaphoreGive( xMutex );
  113. }
  114.  
  115. void Smart_Switch_Run( void * parameter ){
  116.     memset(Timers, 0, sizeof(Timers));
  117.     for ever{
  118.         if(xQueue != NULL){
  119.             struct QueueSmart PINProp;
  120.             if(xQueueReceive(xQueue, &PINProp, portMAX_DELAY) == pdPASS){
  121.                 Write_To_Expand(PINProp.Address,PINProp.Pin,PINProp.ID,PINProp.Timer);
  122.             }
  123.         }
  124.         vTaskDelay(1);
  125.     }
  126. }
Add Comment
Please, Sign In to add comment