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 <LcdMenu.h>
  8#include <MenuScreen.h>
  9#include <display/LiquidCrystal_I2CAdapter.h>
 10#include <input/KeyboardAdapter.h>
 11#include <renderer/CharacterDisplayRenderer.h>
 12#include <utils/printf.h>
 13
 14// 2x20 LCD Display
 15#define LCD_ROWS 2
 16#define LCD_COLS 16
 17
 18float temperature1;
 19float temperature2;
 20float temperature3;
 21
 22bool relay1State, relay2State, relay3State;
 23
 24// Relay 1 Toggle
 25void toggleRelay1(bool isOn) {
 26    printLog(F("relay1State"));
 27}
 28
 29// Relay 2 Toggle
 30void toggleRelay2(bool isOn) {
 31    printLog(F("relay2State"));
 32}
 33
 34// Relay 3 Toggle
 35void toggleRelay3(bool isOn) {
 36    printLog(F("relay3State"));
 37}
 38
 39LiquidCrystal_I2C lcd(0x27, LCD_COLS, LCD_ROWS);
 40LiquidCrystal_I2CAdapter lcdAdapter(&lcd);
 41CharacterDisplayRenderer renderer(&lcdAdapter, LCD_COLS, LCD_ROWS);
 42LcdMenu menu(renderer);
 43KeyboardAdapter keyboard(&menu, &Serial);
 44
 45extern MenuScreen* relayScreen;
 46extern MenuScreen* tempScreen;
 47
 48// clang-format off
 49MENU_SCREEN(mainScreen, mainItems,
 50    ITEM_BASIC("Relay Test"),
 51    ITEM_SUBMENU("Temp. Observing", tempScreen),
 52    ITEM_BASIC("Hello World"),
 53    ITEM_BASIC("I'm World"));
 54// clang-format on
 55
 56// Sub Menu 1: Relays
 57// clang-format off
 58MENU_SCREEN(relayScreen, relayItems, 
 59    ITEM_TOGGLE("Relay 1", toggleRelay1), 
 60    ITEM_TOGGLE("Relay 2", toggleRelay2), 
 61    ITEM_TOGGLE("Relay 3", toggleRelay3));
 62// clang-format on
 63
 64// Sub Menu 2: Temperature Values
 65// clang-format off
 66MENU_SCREEN(tempScreen, tempItems, 
 67    ITEM_BASIC(""), 
 68    ITEM_BASIC(""));
 69// clang-format on
 70
 71// RTOS func. to measure temperature value
 72static void tempMeas(void* pvParameters) {
 73    (void)pvParameters;
 74    for (;;) {
 75        //-------------------BEGIN: TEST WITH RANDOM------------------//
 76        temperature1 = random(1, 1000) / 100.0;  // Generate random float
 77        temperature2 = random(1, 2000) / 100.0;  // Generate random float
 78        //-------------------END: TEST WITH RANDOM--------------------//
 79
 80        char buffer1[8];
 81        snprintf(buffer1, sizeof(buffer1), "%.02f mA", temperature1);
 82        tempItems[0]->setText(buffer1);
 83
 84        vTaskDelay(500 / portTICK_PERIOD_MS);  // wait for 200ms
 85
 86        char buffer2[8];
 87        snprintf(buffer2, sizeof(buffer2), "%.02f V", temperature2);
 88        tempItems[1]->setText(buffer2);
 89
 90        if (menu.getScreen() == tempScreen) {
 91            menu.refresh();
 92        }
 93        vTaskDelay(3000 / portTICK_PERIOD_MS);  // wait for three seconds
 94    }
 95}
 96
 97void setup() {
 98    Serial.begin(9600);
 99    // LCD activation
100    renderer.begin();
101    menu.setScreen(mainScreen);
102    // Run RTOS func.
103    xTaskCreate(tempMeas, "tempMeas", 128, NULL, tskIDLE_PRIORITY + 1, NULL);
104    // Setup random seed
105    pinMode(A0, INPUT);
106    randomSeed(analogRead(A0));
107}
108
109void loop() {
110    renderer.updateTimer();
111    keyboard.observe();
112}