RTOS

  1// Libraries
  2#if defined(ARDUINO_ARCH_AVR)
  3#include <Arduino_FreeRTOS.h>
  4#endif
  5#include <ItemSubMenu.h>
  6#include <ItemToggle.h>
  7#include <ItemValue.h>
  8#include <LcdMenu.h>
  9#include <MenuScreen.h>
 10#include <display/LiquidCrystal_I2CAdapter.h>
 11#include <input/KeyboardAdapter.h>
 12#include <renderer/CharacterDisplayRenderer.h>
 13#include <utils/printf.h>
 14
 15// 2x20 LCD Display
 16#define LCD_ROWS 2
 17#define LCD_COLS 16
 18
 19uint8_t temperature1, temperature2, temperature3;
 20bool relay1State, relay2State, relay3State;
 21
 22// Relay 1 Toggle
 23void toggleRelay1(bool isOn) {
 24    relay1State = isOn;
 25}
 26
 27// Relay 2 Toggle
 28void toggleRelay2(bool isOn) {
 29    relay2State = isOn;
 30}
 31
 32// Relay 3 Toggle
 33void toggleRelay3(bool isOn) {
 34    relay3State = isOn;
 35}
 36
 37LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
 38LiquidCrystal_I2CAdapter lcdAdapter(&lcd);
 39CharacterDisplayRenderer renderer(&lcdAdapter, LCD_COLS, LCD_ROWS);
 40LcdMenu menu(renderer);
 41KeyboardAdapter keyboard(&menu, &Serial);
 42
 43extern MenuScreen* relayScreen;
 44extern MenuScreen* tempScreen;
 45
 46// clang-format off
 47MENU_SCREEN(mainScreen, mainItems,
 48    ITEM_SUBMENU("Relay Test", relayScreen),
 49    ITEM_SUBMENU("Temp. Observing", tempScreen),
 50    ITEM_BASIC("Hello World"),
 51    ITEM_BASIC("I'm World"));
 52// clang-format on
 53
 54// Sub Menu 1: Relays
 55// clang-format off
 56MENU_SCREEN(relayScreen, relayItems, 
 57    ITEM_TOGGLE("Relay 1", toggleRelay1), 
 58    ITEM_TOGGLE("Relay 2", toggleRelay2), 
 59    ITEM_TOGGLE("Relay 3", toggleRelay3));
 60// clang-format on
 61
 62// Sub Menu 2: Temperature Values
 63// clang-format off
 64MENU_SCREEN(tempScreen, tempItems, 
 65    ITEM_VALUE("Temp1", temperature1, "%d C"), 
 66    ITEM_VALUE("Temp2", temperature2, "%d C"),
 67    ITEM_VALUE("Temp3", temperature3, "%d C"));
 68// clang-format on
 69
 70// RTOS func. to measure temperature value
 71static void tempMeas(void* pvParameters) {
 72    (void)pvParameters;
 73    for (;;) {
 74        //-------------------BEGIN: TEST WITH RANDOM------------------//
 75        // Update temperature1 only if relay1 is on
 76        if (relay1State) temperature1 = random(1, 101);
 77        // Update temperature2 only if relay2 is on
 78        if (relay2State) temperature2 = random(1, 101);
 79        // Update temperature3 only if relay3 is on
 80        if (relay3State) temperature3 = random(1, 101);
 81        //-------------------END: TEST WITH RANDOM--------------------//
 82        vTaskDelay(2000 / portTICK_PERIOD_MS);  // wait for two seconds
 83    }
 84}
 85
 86void setup() {
 87    Serial.begin(9600);
 88    // LCD activation
 89    renderer.begin();
 90    menu.setScreen(mainScreen);
 91    // Run RTOS func.
 92    xTaskCreate(tempMeas, "tempMeas", 128, NULL, tskIDLE_PRIORITY + 1, NULL);
 93    // Setup random seed
 94    pinMode(A0, INPUT);
 95    randomSeed(analogRead(A0));
 96}
 97
 98void loop() {
 99    renderer.updateTimer();
100    keyboard.observe();
101    menu.poll();
102}