diff --git a/.gitignore b/.gitignore index c1c4127..a4c9f0c 100644 --- a/.gitignore +++ b/.gitignore @@ -41,4 +41,5 @@ node_modules # Ignore files related to API keys .env -build \ No newline at end of file +build +.project \ No newline at end of file diff --git a/5-Semaphore/TaskNotification/src/BlinkAgent.h b/5-Semaphore/TaskNotification/src/BlinkAgent.h index e62e570..7191dd5 100644 --- a/5-Semaphore/TaskNotification/src/BlinkAgent.h +++ b/5-Semaphore/TaskNotification/src/BlinkAgent.h @@ -45,7 +45,6 @@ class BlinkAgent: public Agent { */ virtual configSTACK_DEPTH_TYPE getMaxStackSize(); -private: //GPIO PAD for LED uint8_t xLedPad = 0; diff --git a/5-Semaphore/TaskNotification/src/BlinkWorker.cpp b/5-Semaphore/TaskNotification/src/BlinkWorker.cpp new file mode 100644 index 0000000..e926ca0 --- /dev/null +++ b/5-Semaphore/TaskNotification/src/BlinkWorker.cpp @@ -0,0 +1,80 @@ +/* + * BlinkWorker.cpp + * + * Blink worker, to blink LED on a GPIO pad but do a random amount of work + * between blinks + * + * Created on: 17 Aug 2022 + * Author: jondurrant + */ + +#include "BlinkWorker.h" +#include "math.h" +#include + +//Blink Delay +#define DELAY 500 + +/*** + * Constructor + * @param gp = GPIO pad number + */ +BlinkWorker::BlinkWorker(uint8_t gp):BlinkAgent(gp) { + //NOP + +} + +/*** + * Destructor + */ +BlinkWorker::~BlinkWorker() { + // NOP +} + +/*** +* Main Run Task for agent +*/ +void BlinkWorker::run(){ + + printf("Blink Started\n"); + + gpio_init(xLedPad); + + gpio_set_dir(xLedPad, GPIO_OUT); + + while (true) { // Loop forever + gpio_put(xLedPad, 1); + vTaskDelay(DELAY); + gpio_put(xLedPad, 0); + + int n = rand() % 5000; + for (int i=0; i < n; i++ ){ + double d = asin(sin(sqrt((double)n))); + d = acos(cos(d)); + } + vTaskDelay(DELAY); + if (pPeer != NULL){ + xTaskNotifyGive(pPeer->getTask()); + } + uint32_t r = ulTaskNotifyTake(pdTRUE, DELAY); + } + +} + +/*** + * Get the static depth required in words + * @return - words + */ +configSTACK_DEPTH_TYPE BlinkWorker::getMaxStackSize(){ + return 150; +} + + +/*** + * Set Peer Worker + * @param peer - peer BlinkWorker that we will sync blink with + */ +void BlinkWorker::setPeer(BlinkWorker *peer){ + pPeer = peer; +} + diff --git a/5-Semaphore/TaskNotification/src/BlinkWorker.h b/5-Semaphore/TaskNotification/src/BlinkWorker.h new file mode 100644 index 0000000..e739100 --- /dev/null +++ b/5-Semaphore/TaskNotification/src/BlinkWorker.h @@ -0,0 +1,62 @@ +/* + * BlinkWorker.h + * + * Blink worker, to blink LED on a GPIO pad but do a random amount of work + * between blinks + * + * Created on: 17 Aug 2022 + * Author: jondurrant + */ + +#ifndef BLINKWORKER_H_ +#define BLINKWORKER_H_ + +#include "pico/stdlib.h" +#include +#include "FreeRTOS.h" +#include "task.h" + +#include "BlinkAgent.h" + + +class BlinkWorker: public BlinkAgent { +public: + /*** + * Constructor + * @param gp = GPIO pad number + */ + BlinkWorker(uint8_t gp); + + /*** + * Destructor + */ + virtual ~BlinkWorker(); + + /*** + * Set Peer Worker + * @param peer - peer BlinkWorker that we will sync blink with + */ + virtual void setPeer(BlinkWorker *peer); + + +protected: + + /*** + * Run loop for the agent. + */ + virtual void run(); + + + /*** + * Get the static depth required in words + * @return - words + */ + virtual configSTACK_DEPTH_TYPE getMaxStackSize(); + + + BlinkWorker *pPeer = NULL; + +}; + + +#endif /* BLINKWORKER_H_ */ diff --git a/5-Semaphore/TaskNotification/src/CMakeLists.txt b/5-Semaphore/TaskNotification/src/CMakeLists.txt index cfcc15c..3226709 100644 --- a/5-Semaphore/TaskNotification/src/CMakeLists.txt +++ b/5-Semaphore/TaskNotification/src/CMakeLists.txt @@ -2,7 +2,7 @@ add_executable(${NAME} main.cpp BlinkAgent.cpp Agent.cpp - RainbowAgent.cpp + BlinkWorker.cpp ) # Pull in our pico_stdlib which pulls in commonly used features diff --git a/5-Semaphore/TaskNotification/src/RainbowAgent.cpp b/5-Semaphore/TaskNotification/src/RainbowAgent.cpp deleted file mode 100644 index c7cdc4d..0000000 --- a/5-Semaphore/TaskNotification/src/RainbowAgent.cpp +++ /dev/null @@ -1,120 +0,0 @@ -/* - * RainbowAgent.cpp - * - * A Rainbow display agent. - * Walks through colours of the rainbow - * Doing a random amount of work inbetween steps - * - * Created on: 16 Aug 2022 - * Author: jondurrant - */ - -#include "RainbowAgent.h" -#include "hardware/pwm.h" -#include -#include - -//Blink Delay -#define DELAY 1000 - -/**** - * Constructor - * @param gpRed = GPIO pin for Red - * @param gpGreen - GPIO pin for Green - * @param gpBlue - GPIO pin for Blue - */ -RainbowAgent::RainbowAgent(uint8_t gpRed, uint8_t gpGreen, uint8_t gpBlue) { - xRGBPads[0] = gpRed; - xRGBPads[1] = gpGreen; - xRGBPads[2] = gpBlue; -} - -/*** - * Destructor - */ -RainbowAgent::~RainbowAgent() { - // Nop -} - -/*** - * Initialise the GPIO Pins - */ -void RainbowAgent::init(){ - uint8_t gp; - - for (uint8_t i = 0; i < 3; i++){ - gp = xRGBPads[i]; - - gpio_init(gp); - gpio_set_function(gp, GPIO_FUNC_PWM); - uint slice_num = pwm_gpio_to_slice_num(gp); - pwm_set_enabled(slice_num, true); - pwm_set_gpio_level(gp, 0); - } -} - -/*** - * Set the RGB LED to a colour - * @param rgb - array of three bytes for Red, Green and Blue components - */ -void RainbowAgent::setRGB(const uint8_t* rgb){ - for (uint8_t i=0; i < 3; i++){ - int lvl = rgb[i]* rgb[i]; - pwm_set_gpio_level(xRGBPads[i], lvl); - } -} - -/*** - * Get the static depth required in words - * @return - words - */ -configSTACK_DEPTH_TYPE RainbowAgent::getMaxStackSize(){ - return 200; -} - -/*** - * Run loop for the agent. - */ -void RainbowAgent::run(){ - int n; - double d; - - - init(); - xStep = 0; - - - for(;;){ - setRGB(pRainbow[xStep]); - printf("%s: %d\n", pName, xStep); - n = rand(); - for (int i=1; i < n; i++){ - d = cbrt(i); - d= cbrt(n*n); - - } - - vTaskDelay(n%300); - - vTaskDelay(DELAY); - xStep++; - if (xStep > 7){ - xStep = 0; - - if (pPeer != NULL){ - xTaskNotifyGive(pPeer->getTask()); - uint32_t r = ulTaskNotifyTake(pdTRUE, DELAY); - printf("%s %d %d", pName, xStep, r); - } - } - } - -} - -/*** - * Set peer. Will notify each time return to begin of sequence - * @param peer - */ -void RainbowAgent::setPeer(RainbowAgent *peer){ - pPeer = peer; -} diff --git a/5-Semaphore/TaskNotification/src/RainbowAgent.h b/5-Semaphore/TaskNotification/src/RainbowAgent.h deleted file mode 100644 index 4fed70a..0000000 --- a/5-Semaphore/TaskNotification/src/RainbowAgent.h +++ /dev/null @@ -1,91 +0,0 @@ -/* - * RainbowAgent.h - * - * A Rainbow display agent. - * Walks through colours of the rainbow - * Doing a random amount of work inbetween steps - * - * Created on: 16 Aug 2022 - * Author: jondurrant - */ - -#ifndef SRC_RAINBOWAGENT_H_ -#define SRC_RAINBOWAGENT_H_ - -#include "pico/stdlib.h" -#include -#include "FreeRTOS.h" -#include "task.h" - -#include "Agent.h" - -class RainbowAgent : public Agent { -public: - /**** - * Constructor - * @param gpRed = GPIO pin for Red - * @param gpGreen - GPIO pin for Green - * @param gpBlue - GPIO pin for Blue - */ - RainbowAgent(uint8_t gpRed, uint8_t gpGreen, uint8_t gpBlue); - - /*** - * Destructor - */ - virtual ~RainbowAgent(); - - /*** - * Set peer. Will notify each time return to begin of sequence - * @param peer - */ - void setPeer(RainbowAgent *peer); -protected: - - /*** - * Run loop for the agent. - */ - virtual void run(); - - - /*** - * Get the static depth required in words - * @return - words - */ - virtual configSTACK_DEPTH_TYPE getMaxStackSize(); - -private: - - /*** - * Initialise the GPIO Pins - */ - void init(); - - /*** - * Set the RGB LED to a colour - * @param rgb - array of three bytes for Red, Green and Blue components - */ - void setRGB(const uint8_t* rgb); - - //GPIO PAD for LED - uint8_t xRGBPads[3]; - - uint8_t xStep = 0; - - //Peer for sync notification - RainbowAgent *pPeer = NULL; - - //Rainbow sequence as Red, Green, Blue tuples - const uint8_t pRainbow[7][3]= { - {0x94, 0x00, 0xD3}, - {0x4B, 0x00, 0x82}, - {0x00, 0x00, 0xFF}, - {0x00, 0xFF, 0x00}, - {0xFF, 0xFF, 0x00}, - {0xFF, 0x7F, 0x00}, - {0xFF, 0x00, 0x00} - }; - -}; - - -#endif /* SRC_RAINBOWAGENT_H_ */ diff --git a/5-Semaphore/TaskNotification/src/main.cpp b/5-Semaphore/TaskNotification/src/main.cpp index 46649bd..583ec2d 100644 --- a/5-Semaphore/TaskNotification/src/main.cpp +++ b/5-Semaphore/TaskNotification/src/main.cpp @@ -13,7 +13,7 @@ #include #include "BlinkAgent.h" -#include "RainbowAgent.h" +#include "BlinkWorker.h" //Standard Task priority @@ -21,6 +21,9 @@ //LED PAD to use #define LED_PAD 0 +#define LED1_PAD 2 +#define LED2_PAD 3 + void runTimeStats( ){ @@ -79,18 +82,18 @@ void runTimeStats( ){ */ void mainTask(void *params){ BlinkAgent blink(LED_PAD); - - RainbowAgent rainbowA(2,4,5); - RainbowAgent rainbowB(6,8,9); - rainbowA.setPeer(&rainbowB); - rainbowB.setPeer(&rainbowA); - - printf("Main task started\n"); - - blink.start("Blink", TASK_PRIORITY); - rainbowA.start("Rainbow A", TASK_PRIORITY); - rainbowB.start("Rainbow B", TASK_PRIORITY); - + BlinkWorker worker1(LED1_PAD); + BlinkWorker worker2(LED2_PAD); + + worker1.setPeer(&worker2); + worker2.setPeer(&worker1); + + blink.start("Blink", + TASK_PRIORITY); + worker1.start("Worker 1", + TASK_PRIORITY); + worker2.start("Worker 2", + TASK_PRIORITY); diff --git a/7-MessageBuf/SerialCmds/src/CMakeLists.txt b/7-MessageBuf/SerialCmds/src/CMakeLists.txt index 7523de8..76cbfaa 100644 --- a/7-MessageBuf/SerialCmds/src/CMakeLists.txt +++ b/7-MessageBuf/SerialCmds/src/CMakeLists.txt @@ -12,6 +12,7 @@ target_link_libraries(${NAME} pico_stdlib FreeRTOS-Kernel-Heap4 tiny_json + hardware_adc ) target_include_directories(${NAME} PRIVATE diff --git a/7-MessageBuf/SerialCmds/src/main.cpp b/7-MessageBuf/SerialCmds/src/main.cpp index b0e595b..be57eda 100644 --- a/7-MessageBuf/SerialCmds/src/main.cpp +++ b/7-MessageBuf/SerialCmds/src/main.cpp @@ -12,6 +12,7 @@ #include "task.h" #include #include +#include "hardware/adc.h" #include "BlinkAgent.h" #include "CounterAgent.h" @@ -80,6 +81,21 @@ void runTimeStats( ){ ); } +/*** + * Salt Random number generator using current temperature + * Otherwise both Pico will go through same random sequence + */ +void saltRand(){ + adc_init(); + adc_set_temp_sensor_enabled(true); + adc_select_input(4); + + int seed = 0; + + seed = adc_read(); + srand(seed); +} + /*** * Main task to blink external LED @@ -93,6 +109,7 @@ void mainTask(void *params){ IOAgent ioAgent(&decoder, LED5_PAD); printf("Main task started\n"); + saltRand(); blink.start("Blink", TASK_PRIORITY); counter.start("Counter", TASK_PRIORITY);