#include <Arduino.h>
#include <ShiftRegGPIOXpander.h>

//==============================================>> General use definitions BEGIN
#define LoopDlyTtlTm 1500 // Time between task unblocking, time taken from the start of the task execution to the next execution 
#define MainCtrlTskPrrtyLvl 5 // Task priority level

static BaseType_t xReturned; /*!<Static variable to keep returning result value from Tasks and Timers executions*/
static BaseType_t errorFlag {pdFALSE};

BaseType_t srgxExecTskCore = xPortGetCoreID();
BaseType_t srgxExecTskPrrtyCnfg = MainCtrlTskPrrtyLvl;
//================================================>> General use definitions END
 
//======================================>> General use function prototypes BEGIN
void Error_Handler();
//========================================>> General use function prototypes END
 
//====================================>> Task Callback function prototypes BEGIN
void mainCtrlTsk(void *pvParameters);
//======================================>> Task Callback function prototypes END
 
//===========================================>> Tasks Handles declarations BEGIN
TaskHandle_t mainCtrlTskHndl {NULL};
//=============================================>> Tasks Handles declarations END

void setup() { 

   // Create the Main control task for setup and execution of the main code
    xReturned = xTaskCreatePinnedToCore(
       mainCtrlTsk,  // Callback function/task to be called
       "MainControlTask",  // Name of the task
       2048,   // Stack size (in bytes in ESP32, words in FreeRTOS), the minimum value is in the config file, for this is 768 bytes
       NULL,  // Pointer to the parameters for the function to work with
       srgxExecTskPrrtyCnfg, // Priority level given to the task
       &mainCtrlTskHndl, // Task handle
       srgxExecTskCore // Run in the App Core if it's a dual core mcu (ESP-FreeRTOS specific)
    );
    if(xReturned != pdPASS)
       Error_Handler();
 }

 void loop() {
   vTaskDelete(NULL); // Delete this task -the ESP-Arduino LoopTask()- and remove it from the execution list
}  

 //===============================>> User Tasks Implementations BEGIN
 void mainCtrlTsk(void *pvParameters){
   delay(10);  //FTPO Part of the WOKWI simulator additions, for simulation startup needs

   TickType_t loopTmrStrtTm{0};
   TickType_t* loopTmrStrtTmPtr{&loopTmrStrtTm};
   TickType_t totalDelay {LoopDlyTtlTm};

   uint8_t ds{33};
   uint8_t sh_cp{26};
   uint8_t st_cp{25};
   uint8_t srQty{4};
   
   uint8_t strtngVals [4] {0xFF, 0xFF, 0xFF, 0xFF};

   ShiftRegGPIOXpander srgx(ds, sh_cp, st_cp, srQty, strtngVals);
   

   uint8_t pinUpdtd{0};
   uint8_t setVal{LOW};

   for(;;){
      srgx.digitalWriteSr(pinUpdtd, setVal);
      vTaskDelay(500);
      pinUpdtd++;
      if(pinUpdtd > srgx.getMaxPin()){
         pinUpdtd = 0;
         if(setVal == HIGH)
            setVal = LOW;
         else
            setVal = HIGH;
      }
   }
}

//================================================>> General use functions BEGIN
//==================================================>> General use functions END

//=======================================>> User Functions Implementations BEGIN
 /**
  * @brief Error Handling function
  * 
  * Placeholder for a Error Handling function, in case of an error the execution
  * will be trapped in this endless loop
  */
 void Error_Handler(){
   for(;;)
   {    
   }
   
   return;
 }
//=========================================>> User Functions Implementations END
74HC595
sr4:Q1
sr4:Q2
sr4:Q3
sr4:Q4
sr4:Q5
sr4:Q6
sr4:Q7
sr4:GND
sr4:Q7S
sr4:MR
sr4:SHCP
sr4:STCP
sr4:OE
sr4:DS
sr4:Q0
sr4:VCC
DS/DIO
STCP/RCLK
SHCP/SCLK
VCC
esp1:0
esp1:2
esp1:4
esp1:5
esp1:12
esp1:13
esp1:14
esp1:15
esp1:16
esp1:17
esp1:18
esp1:19
esp1:21
esp1:22
esp1:23
esp1:25
esp1:26
esp1:27
esp1:32
esp1:33
esp1:34
esp1:35
esp1:3V3
esp1:EN
esp1:VP
esp1:VN
esp1:GND.1
esp1:D2
esp1:D3
esp1:CMD
esp1:5V
esp1:GND.2
esp1:TX
esp1:RX
esp1:GND.3
esp1:D1
esp1:D0
esp1:CLK
DIG2 R01 - b08~15
sevseg6:COM.1
sevseg6:COM.2
sevseg6:A
sevseg6:B
sevseg6:C
sevseg6:D
sevseg6:E
sevseg6:F
sevseg6:G
sevseg6:DP
r1:1
r1:2
r2:1
r2:2
r3:1
r3:2
r4:1
r4:2
r5:1
r5:2
r6:1
r6:2
r7:1
r7:2
r8:1
r8:2
GND
vcc1:VCC
74HC595
sr1:Q1
sr1:Q2
sr1:Q3
sr1:Q4
sr1:Q5
sr1:Q6
sr1:Q7
sr1:GND
sr1:Q7S
sr1:MR
sr1:SHCP
sr1:STCP
sr1:OE
sr1:DS
sr1:Q0
sr1:VCC
DS/DIO
STCP/RCLK
SHCP/SCLK
VCC
sevseg1:COM.1
sevseg1:COM.2
sevseg1:A
sevseg1:B
sevseg1:C
sevseg1:D
sevseg1:E
sevseg1:F
sevseg1:G
sevseg1:DP
r9:1
r9:2
r10:1
r10:2
r11:1
r11:2
r12:1
r12:2
r13:1
r13:2
r14:1
r14:2
r15:1
r15:2
r16:1
r16:2
GND
74HC595
sr2:Q1
sr2:Q2
sr2:Q3
sr2:Q4
sr2:Q5
sr2:Q6
sr2:Q7
sr2:GND
sr2:Q7S
sr2:MR
sr2:SHCP
sr2:STCP
sr2:OE
sr2:DS
sr2:Q0
sr2:VCC
DS/DIO
STCP/RCLK
SHCP/SCLK
VCC
sevseg2:COM.1
sevseg2:COM.2
sevseg2:A
sevseg2:B
sevseg2:C
sevseg2:D
sevseg2:E
sevseg2:F
sevseg2:G
sevseg2:DP
r17:1
r17:2
r18:1
r18:2
r19:1
r19:2
r20:1
r20:2
r21:1
r21:2
r22:1
r22:2
r23:1
r23:2
r24:1
r24:2
GND
DIG3 R02 - b16~23
DIG4 R02 - b24~31
DIG1 R00 - b00~07
74HC595
sr3:Q1
sr3:Q2
sr3:Q3
sr3:Q4
sr3:Q5
sr3:Q6
sr3:Q7
sr3:GND
sr3:Q7S
sr3:MR
sr3:SHCP
sr3:STCP
sr3:OE
sr3:DS
sr3:Q0
sr3:VCC
DS/DIO
STCP/RCLK
SHCP/SCLK
VCC
SevSegSttDspHC595x4
sevseg3:COM.1
sevseg3:COM.2
sevseg3:A
sevseg3:B
sevseg3:C
sevseg3:D
sevseg3:E
sevseg3:F
sevseg3:G
sevseg3:DP
r25:1
r25:2
r26:1
r26:2
r27:1
r27:2
r28:1
r28:2
r29:1
r29:2
r30:1
r30:2
r31:1
r31:2
r32:1
r32:2
GND