From 4b31b6c61806ea7c1b22d5d39b95830f5be96fa7 Mon Sep 17 00:00:00 2001 From: Matthias Mitscherlich Date: Thu, 29 Feb 2024 16:53:16 +0100 Subject: [PATCH] Started reorganizing the code. Put everything existing to OLD and began with a new main.cppy --- code/main/CMakeLists.txt | 29 +- code/main/main.cpp | 361 +-------------- code/main/{ => old}/inc/bmp280.h | 0 code/main/{ => old}/inc/clock.h | 0 code/main/{ => old}/inc/clockwordmap.h | 0 code/main/{ => old}/inc/daywordmap.h | 0 code/main/{ => old}/inc/gpio.h | 0 code/main/{ => old}/inc/i2c.h | 0 code/main/{ => old}/inc/led_strip_encoder.h | 0 code/main/{ => old}/inc/ledmatrix.h | 0 code/main/{ => old}/inc/logger.h | 0 code/main/{ => old}/inc/temperature.h | 0 code/main/{ => old}/inc/temperaturewordmap.h | 0 code/main/{ => old}/inc/wifi.h | 0 code/main/{ => old}/inc/wordmap.h | 0 code/main/old/main.cpp | 415 ++++++++++++++++++ code/main/{ => old}/src/bmp280.cpp | 0 code/main/{ => old}/src/clock.cpp | 0 code/main/{ => old}/src/clockwordmap.cpp | 0 code/main/{ => old}/src/daywordmap.cpp | 0 code/main/{ => old}/src/gpio.cpp | 0 code/main/{ => old}/src/i2c.cpp | 0 code/main/{ => old}/src/led_strip_encoder.c | 0 code/main/{ => old}/src/ledmatrix.cpp | 0 code/main/{ => old}/src/logger.cpp | 0 code/main/{ => old}/src/temperature.cpp | 0 .../main/{ => old}/src/temperaturewordmap.cpp | 0 code/main/{ => old}/src/wifi.cpp | 0 code/main/{ => old}/src/wordmap.cpp | 0 29 files changed, 446 insertions(+), 359 deletions(-) rename code/main/{ => old}/inc/bmp280.h (100%) rename code/main/{ => old}/inc/clock.h (100%) rename code/main/{ => old}/inc/clockwordmap.h (100%) rename code/main/{ => old}/inc/daywordmap.h (100%) rename code/main/{ => old}/inc/gpio.h (100%) rename code/main/{ => old}/inc/i2c.h (100%) rename code/main/{ => old}/inc/led_strip_encoder.h (100%) rename code/main/{ => old}/inc/ledmatrix.h (100%) rename code/main/{ => old}/inc/logger.h (100%) rename code/main/{ => old}/inc/temperature.h (100%) rename code/main/{ => old}/inc/temperaturewordmap.h (100%) rename code/main/{ => old}/inc/wifi.h (100%) rename code/main/{ => old}/inc/wordmap.h (100%) create mode 100644 code/main/old/main.cpp rename code/main/{ => old}/src/bmp280.cpp (100%) rename code/main/{ => old}/src/clock.cpp (100%) rename code/main/{ => old}/src/clockwordmap.cpp (100%) rename code/main/{ => old}/src/daywordmap.cpp (100%) rename code/main/{ => old}/src/gpio.cpp (100%) rename code/main/{ => old}/src/i2c.cpp (100%) rename code/main/{ => old}/src/led_strip_encoder.c (100%) rename code/main/{ => old}/src/ledmatrix.cpp (100%) rename code/main/{ => old}/src/logger.cpp (100%) rename code/main/{ => old}/src/temperature.cpp (100%) rename code/main/{ => old}/src/temperaturewordmap.cpp (100%) rename code/main/{ => old}/src/wifi.cpp (100%) rename code/main/{ => old}/src/wordmap.cpp (100%) diff --git a/code/main/CMakeLists.txt b/code/main/CMakeLists.txt index e2dc1de..c335d36 100644 --- a/code/main/CMakeLists.txt +++ b/code/main/CMakeLists.txt @@ -4,21 +4,22 @@ idf_component_register( SRCS # list the source files of this component "main.cpp" - "src/bmp280.cpp" - "src/gpio.cpp" - "src/i2c.cpp" - "src/wifi.cpp" - "src/logger.cpp" - "src/led_strip_encoder.c" - "src/ledmatrix.cpp" - "src/clock.cpp" - "src/wordmap.cpp" - "src/clockwordmap.cpp" - "src/daywordmap.cpp" - "src/temperaturewordmap.cpp" - "src/temperature.cpp" +# "old/src/bmp280.cpp" +# "old/src/gpio.cpp" +# "old/src/i2c.cpp" +# "old/src/wifi.cpp" +# "old/src/logger.cpp" +# "old/src/led_strip_encoder.c" +# "old/src/ledmatrix.cpp" +# "old/src/clock.cpp" +# "old/src/wordmap.cpp" +# "old/src/clockwordmap.cpp" +# "old/src/daywordmap.cpp" +# "old/src/temperaturewordmap.cpp" +# "old/src/temperature.cpp" INCLUDE_DIRS # optional, add here public include directories - "inc" + "platform/inc" + "application/inc" PRIV_INCLUDE_DIRS # optional, add here private include directories REQUIRES # optional, list the public requirements (component names) PRIV_REQUIRES # optional, list the private requirements diff --git a/code/main/main.cpp b/code/main/main.cpp index c04eb9b..08e5d60 100644 --- a/code/main/main.cpp +++ b/code/main/main.cpp @@ -1,8 +1,8 @@ // -------------------------------------------------------------------------------------------------------------------- -/// \file main +/// \file main.cpp /// \brief Description // -------------------------------------------------------------------------------------------------------------------- -// +// // vbchaos software design // // -------------------------------------------------------------------------------------------------------------------- @@ -17,104 +17,30 @@ // Include files // -------------------------------------------------------------------------------------------------------------------- -#include -#include - #include "freertos/FreeRTOS.h" #include "freertos/task.h" -#include "esp_event.h" -#include "esp_log.h" -#include "esp_wifi.h" - -#include "nvs_flash.h" - -#include "driver/gpio.h" -#include "driver/rmt_tx.h" -#include "driver/uart_select.h" -#include "driver/gptimer.h" - -#include "inc/bmp280.h" -#include "inc/gpio.h" -#include "inc/i2c.h" -#include "inc/led_strip_encoder.h" -#include "inc/ledmatrix.h" -#include "inc/logger.h" -#include "inc/wifi.h" - -#include "clock.h" -#include "clockwordmap.h" -#include "daywordmap.h" -#include "temperaturewordmap.h" -#include "temperature.h" - - // -------------------------------------------------------------------------------------------------------------------- // Constant and macro definitions // -------------------------------------------------------------------------------------------------------------------- -#define RMT_LED_STRIP_RESOLUTION_HZ 10000000 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution) -#define RMT_LED_STRIP_GPIO_NUM 0 // -------------------------------------------------------------------------------------------------------------------- // Type definitions // -------------------------------------------------------------------------------------------------------------------- - // -------------------------------------------------------------------------------------------------------------------- // File-scope variables // -------------------------------------------------------------------------------------------------------------------- -static const uart_port_t uartPort = UART_NUM_0; -static TaskHandle_t devTaskHandle = NULL; -static TaskHandle_t colourMapTaskHandle = NULL; -// GPIOs -static GPIO led_rgb_red(3, GPIO_DIRECTION_OUTPUT); -static GPIO led_rgb_green(4, GPIO_DIRECTION_OUTPUT); -static GPIO led_rgb_blue(5, GPIO_DIRECTION_OUTPUT); -static GPIO led_orange(18, GPIO_DIRECTION_OUTPUT); - -// ------------------------------------------------------- -// LED Matrix components -static rmt_channel_handle_t led_chan = NULL; -static rmt_transmit_config_t tx_config; -static rmt_encoder_handle_t led_encoder = NULL; - -static LEDMatrix_Parameters_t ledmatrix_parameters = -{ - LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT, - LEDMATRIX_ORIENTATION_COLUM_UP_DOWN, - LEDMATRIX_ORIENTATION_ROW, - 20, - 13, - &led_chan, - &led_encoder, - &tx_config -}; - -static LEDMatrix matrix(&ledmatrix_parameters); - -static ClockWordmap clockWordmap(&matrix); -static DayWordmap dayWordmap(&matrix); -static TemperatureWordmap tempWordmap(&matrix); - -static gptimer_handle_t matrixRefreshTimer = NULL; // -------------------------------------------------------------------------------------------------------------------- // Function declarations // -------------------------------------------------------------------------------------------------------------------- -// Simple countdown on display -static void countdown(int delay); -// Timer Callback for the LEDMatrix refresh -static bool timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data); - -static void devTask(void* parameters); - -static void colourMapTask(void* parameters); // -------------------------------------------------------------------------------------------------------------------- // Function definitions @@ -122,15 +48,15 @@ static void colourMapTask(void* parameters); extern "C" void app_main(void) { - esp_log_level_set("*", ESP_LOG_WARN); + esp_log_level_set("*", ESP_LOG_WARN); - esp_err_t ret = nvs_flash_init(); + esp_err_t ret = nvs_flash_init(); - if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) - { - ESP_ERROR_CHECK(nvs_flash_erase()); - ret = nvs_flash_init(); - } + if (ret == ESP_ERR_NVS_NO_FREE_PAGES + || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } //-------------------------------------------- // UART @@ -145,271 +71,16 @@ extern "C" void app_main(void) .rx_flow_ctrl_thresh = 0, .source_clk = UART_SCLK_DEFAULT }; - ESP_ERROR_CHECK(uart_param_config(uartPort, &uartConfig)); - ESP_ERROR_CHECK(uart_set_pin(uartPort, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); - ESP_ERROR_CHECK(uart_driver_install(uartPort, 1024, 1024, 0, NULL, 0)); - //-------------------------------------------- - // LOGGER - // - Logger logger(10, uartPort); - - LOGGER_PRINT("\n\r-----------------------------------------------------------------------\n\r"); - LOGGER_PRINT("System Start\n\r"); - LOGGER_PRINT("\n\r"); - LOGGER_PRINT("WordClock\n\r"); - LOGGER_PRINT("Release: %f\n\r", RELEASE); - LOGGER_PRINT("Compiled on %d %d\n\r\n\r\n\r", __TIME__, __DATE__); - - - //-------------------------------------------- - // RMT Channel - // - LOGGER_INFO("Create RMT TX channel"); - rmt_tx_channel_config_t tx_chan_config; - memset(&tx_chan_config, 0, sizeof(tx_chan_config)); - - tx_chan_config.clk_src = RMT_CLK_SRC_DEFAULT; // select source clock - tx_chan_config.gpio_num = (gpio_num_t)RMT_LED_STRIP_GPIO_NUM; - tx_chan_config.mem_block_symbols = 64; // increase the block size can make the LED less flickering - tx_chan_config.resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ; - tx_chan_config.trans_queue_depth = 4; - - ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan)); - - LOGGER_INFO("Install led strip encoder"); - led_strip_encoder_config_t encoder_config; - memset(&encoder_config, 0, sizeof(encoder_config)); - encoder_config.resolution = RMT_LED_STRIP_RESOLUTION_HZ; - - ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder)); - - LOGGER_INFO("Enable RMT TX channel"); - ESP_ERROR_CHECK(rmt_enable(led_chan)); - - memset(&tx_config, 0, sizeof(tx_config)); - tx_config.loop_count = 0; - - - //-------------------------------------------- - // I2C - // - // SourceClock: GPIO 8 - // SourceData: GPIO 9 - I2C i2c0(8, 9); - - //-------------------------------------------- - // BMP280 - // - // Communicates on I2C i2c0 - // Has slave address 0x76 - BMP280 bmp280(&i2c0, 0x76); - // Reset the sensor - bmp280.resetSensor(); - // Make sure to apply a wait cycle between reset and continuous use - 2ms is advised as minimum - vTaskDelay(10); - // Initialize the BMP280 - bmp280.initialize(); - // Set the temperature Oversampling - bmp280.setSensorTemperatureOversampling(BMP280::BMP280_Oversampling_t::X1); - // Set the sensor to NORMAL mode - bmp280.setSensorMode(BMP280::BMP280_Mode_t::NORMAL); - - Temperature temperature; - - //-------------------------------------------- - // LED Matrix - // - matrix.setGlobalColour(0x10, 0, 0x04); + ESP_ERROR_CHECK(uart_param_config(uartPort, &uartConfig)); + ESP_ERROR_CHECK(uart_set_pin(uartPort, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); + ESP_ERROR_CHECK(uart_driver_install(uartPort, 1024, 1024, 0, NULL, 0)); - //-------------------------------------------- - // GP Timer for automatic matrix re-draw trigger - // - gptimer_config_t timer_config = { }; - gptimer_event_callbacks_t cbs = { }; - gptimer_alarm_config_t alarm_config = { }; - - timer_config.clk_src = GPTIMER_CLK_SRC_DEFAULT; - timer_config.direction = GPTIMER_COUNT_UP; - timer_config.resolution_hz = 1000000; // 1 MHz - cbs.on_alarm = timerCallback; - alarm_config.reload_count = 0; - alarm_config.alarm_count = timer_config.resolution_hz / 60; - alarm_config.flags.auto_reload_on_alarm = true; - - ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &matrixRefreshTimer)); - ESP_ERROR_CHECK(gptimer_register_event_callbacks(matrixRefreshTimer, &cbs, NULL)); - ESP_ERROR_CHECK(gptimer_enable(matrixRefreshTimer)); - ESP_ERROR_CHECK(gptimer_set_alarm_action(matrixRefreshTimer, &alarm_config)); - ESP_ERROR_CHECK(gptimer_start(matrixRefreshTimer)); - - - // Create the development task - if(xTaskCreate(devTask, "DevTask", 2048, NULL, 3, &devTaskHandle) != pdPASS) - { - LOGGER_ERROR("Task not created"); - } - - // Create the colour Map task - if(xTaskCreate(colourMapTask, "ColourTask", 2048, NULL, 3, &colourMapTaskHandle) != pdPASS) - { - LOGGER_ERROR("Task not created"); - } - - Wifi wifi; - wifi.start_client(); - - Clock clock(Clock::mode::TEN_BEFORE_HALF); - - clockWordmap.setColour(0x00, 0xFF, 0xFF); - dayWordmap.setColour(0x20, 0xCC, 0x80); - - -// countdown(1000); - - list clockWordlist; - list tempWordList; - - - while (true) - { - - clock.generateWordlist(&clockWordlist); - - - matrix.clear(); - std::list::iterator it; - for(it = clockWordlist.begin(); it != clockWordlist.end(); it++) - { - clockWordmap.setWord(Wordmap::Language_t::NL, *it, true); - dayWordmap.setWord(Wordmap::Language_t::NL, *it, true); - } - - // Get the temperature from sensor - int currentTemperature = bmp280.getTemperature() / 100; - - LOGGER_INFO("The current temperature is: %i (%s)", currentTemperature, to_string(21)); - // Generate temperature wordlist - temperature.generateWordlist(currentTemperature, &tempWordList); - for(it = tempWordList.begin(); it != tempWordList.end(); it++) - { - tempWordmap.setWord(Wordmap::Language_t::NL, *it, true); - } - uint8_t tRed, tGreen, tBlue; - temperature.calculateRGB(currentTemperature, &tRed, &tGreen, &tBlue); - tempWordmap.setColour(tRed, tGreen, tBlue); - - // Add a seconds indicator - matrix.setPixelValue(11, 11, clock.getTime() % 2); - - // Update the matrix - matrix.tick(); - - // Update the clock every second (1000 ms) - vTaskDelay(1000); - - - } + while(1) + { + vTaskDelay(100); + } } - -static void devTask(void* parameters) -{ - uint32_t counter = 0; - printf("DevTask created"); - while (true) - { - (void)led_orange.SetOutput((GPIO_Value_t)(counter % 2)); - counter++; - vTaskDelay(500); - } -} - - -static void colourMapTask(void* parameters) -{ - uint8_t red = 0xF0; - uint8_t green = 0x20; - uint8_t blue = 0xF0; - -// uint8_t red = 0x00; -// uint8_t green = 0x00; -// uint8_t blue = 0x00; - - uint32_t counter = 0; - - - while (true) - { -// red = 0; -// green = 0; -// blue = 0; -// if ((counter % 2) == 0) -// { -// red = 0xFF; -// } -// if ((counter % 5) == 0) -// { -// green = 0xFF; -// } -// if ((counter % 9) == 0) -// { -// blue = 0xFF; -// } - - -// matrix.setGlobalColour(red, green, blue); - -// red = counter & 0xFF; -// green = (counter >> 8) & 0xFF; -// blue = (counter >> 16) & 0xFF; - counter++; - vTaskDelay(200); - } - -} - -static void countdown(int delay) -{ - clockWordmap.setWord(Wordmap::Language_t::NL, "ten", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "ten", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "nine", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "nine", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "eight", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "eight", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "seven", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "seven", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "six", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "six", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "five", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "five", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "four", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "four", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "three", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "three", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "two", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "two", false); - clockWordmap.setWord(Wordmap::Language_t::NL, "one", true); - vTaskDelay(delay); - clockWordmap.setWord(Wordmap::Language_t::NL, "one", false); -} - -static bool IRAM_ATTR timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data) -{ - BaseType_t xHigherPriorityTaskWoken = pdFALSE; - -// xHigherPriorityTaskWoken = matrix.tick(); - - return xHigherPriorityTaskWoken == pdTRUE; -} diff --git a/code/main/inc/bmp280.h b/code/main/old/inc/bmp280.h similarity index 100% rename from code/main/inc/bmp280.h rename to code/main/old/inc/bmp280.h diff --git a/code/main/inc/clock.h b/code/main/old/inc/clock.h similarity index 100% rename from code/main/inc/clock.h rename to code/main/old/inc/clock.h diff --git a/code/main/inc/clockwordmap.h b/code/main/old/inc/clockwordmap.h similarity index 100% rename from code/main/inc/clockwordmap.h rename to code/main/old/inc/clockwordmap.h diff --git a/code/main/inc/daywordmap.h b/code/main/old/inc/daywordmap.h similarity index 100% rename from code/main/inc/daywordmap.h rename to code/main/old/inc/daywordmap.h diff --git a/code/main/inc/gpio.h b/code/main/old/inc/gpio.h similarity index 100% rename from code/main/inc/gpio.h rename to code/main/old/inc/gpio.h diff --git a/code/main/inc/i2c.h b/code/main/old/inc/i2c.h similarity index 100% rename from code/main/inc/i2c.h rename to code/main/old/inc/i2c.h diff --git a/code/main/inc/led_strip_encoder.h b/code/main/old/inc/led_strip_encoder.h similarity index 100% rename from code/main/inc/led_strip_encoder.h rename to code/main/old/inc/led_strip_encoder.h diff --git a/code/main/inc/ledmatrix.h b/code/main/old/inc/ledmatrix.h similarity index 100% rename from code/main/inc/ledmatrix.h rename to code/main/old/inc/ledmatrix.h diff --git a/code/main/inc/logger.h b/code/main/old/inc/logger.h similarity index 100% rename from code/main/inc/logger.h rename to code/main/old/inc/logger.h diff --git a/code/main/inc/temperature.h b/code/main/old/inc/temperature.h similarity index 100% rename from code/main/inc/temperature.h rename to code/main/old/inc/temperature.h diff --git a/code/main/inc/temperaturewordmap.h b/code/main/old/inc/temperaturewordmap.h similarity index 100% rename from code/main/inc/temperaturewordmap.h rename to code/main/old/inc/temperaturewordmap.h diff --git a/code/main/inc/wifi.h b/code/main/old/inc/wifi.h similarity index 100% rename from code/main/inc/wifi.h rename to code/main/old/inc/wifi.h diff --git a/code/main/inc/wordmap.h b/code/main/old/inc/wordmap.h similarity index 100% rename from code/main/inc/wordmap.h rename to code/main/old/inc/wordmap.h diff --git a/code/main/old/main.cpp b/code/main/old/main.cpp new file mode 100644 index 0000000..92431ac --- /dev/null +++ b/code/main/old/main.cpp @@ -0,0 +1,415 @@ +// -------------------------------------------------------------------------------------------------------------------- +/// \file main +/// \brief Description +// -------------------------------------------------------------------------------------------------------------------- +// +// vbchaos software design +// +// -------------------------------------------------------------------------------------------------------------------- +/// $Revision: $ +/// $Author: $ +/// $Date: $ +// (c) 2023 vbchaos +// -------------------------------------------------------------------------------------------------------------------- + + +// -------------------------------------------------------------------------------------------------------------------- +// Include files +// -------------------------------------------------------------------------------------------------------------------- + +#include +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" + +#include "esp_event.h" +#include "esp_log.h" +#include "esp_wifi.h" + +#include "nvs_flash.h" + +#include "driver/gpio.h" +#include "driver/rmt_tx.h" +#include "driver/uart_select.h" +#include "driver/gptimer.h" + +#include "inc/bmp280.h" +#include "inc/gpio.h" +#include "inc/i2c.h" +#include "inc/led_strip_encoder.h" +#include "inc/ledmatrix.h" +#include "inc/logger.h" +#include "inc/wifi.h" + +#include "clock.h" +#include "clockwordmap.h" +#include "daywordmap.h" +#include "temperaturewordmap.h" +#include "temperature.h" + + +// -------------------------------------------------------------------------------------------------------------------- +// Constant and macro definitions +// -------------------------------------------------------------------------------------------------------------------- + +#define RMT_LED_STRIP_RESOLUTION_HZ 10000000 // 10MHz resolution, 1 tick = 0.1us (led strip needs a high resolution) +#define RMT_LED_STRIP_GPIO_NUM 0 + +// -------------------------------------------------------------------------------------------------------------------- +// Type definitions +// -------------------------------------------------------------------------------------------------------------------- + + + +// -------------------------------------------------------------------------------------------------------------------- +// File-scope variables +// -------------------------------------------------------------------------------------------------------------------- + +static const uart_port_t uartPort = UART_NUM_0; +static TaskHandle_t devTaskHandle = NULL; +static TaskHandle_t colourMapTaskHandle = NULL; + +// GPIOs +static GPIO led_rgb_red(3, GPIO_DIRECTION_OUTPUT); +static GPIO led_rgb_green(4, GPIO_DIRECTION_OUTPUT); +static GPIO led_rgb_blue(5, GPIO_DIRECTION_OUTPUT); +static GPIO led_orange(18, GPIO_DIRECTION_OUTPUT); + +// ------------------------------------------------------- +// LED Matrix components +static rmt_channel_handle_t led_chan = NULL; +static rmt_transmit_config_t tx_config; +static rmt_encoder_handle_t led_encoder = NULL; + +static LEDMatrix_Parameters_t ledmatrix_parameters = +{ + LEDMATRIX_ORIENTATION_ROW_LEFT_RIGHT, + LEDMATRIX_ORIENTATION_COLUM_UP_DOWN, + LEDMATRIX_ORIENTATION_ROW, + 20, + 13, + &led_chan, + &led_encoder, + &tx_config +}; + +static LEDMatrix matrix(&ledmatrix_parameters); + +static ClockWordmap clockWordmap(&matrix); +static DayWordmap dayWordmap(&matrix); +static TemperatureWordmap tempWordmap(&matrix); + +static gptimer_handle_t matrixRefreshTimer = NULL; + +// -------------------------------------------------------------------------------------------------------------------- +// Function declarations +// -------------------------------------------------------------------------------------------------------------------- + +// Simple countdown on display +static void countdown(int delay); + +// Timer Callback for the LEDMatrix refresh +static bool timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data); + +static void devTask(void* parameters); + +static void colourMapTask(void* parameters); + +// -------------------------------------------------------------------------------------------------------------------- +// Function definitions +// -------------------------------------------------------------------------------------------------------------------- + +extern "C" void app_main(void) +{ + esp_log_level_set("*", ESP_LOG_WARN); + + esp_err_t ret = nvs_flash_init(); + + if(ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) + { + ESP_ERROR_CHECK(nvs_flash_erase()); + ret = nvs_flash_init(); + } + + //-------------------------------------------- + // UART + // + const uart_config_t uartConfig = + { + .baud_rate = 115200, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .rx_flow_ctrl_thresh = 0, + .source_clk = UART_SCLK_DEFAULT + }; + ESP_ERROR_CHECK(uart_param_config(uartPort, &uartConfig)); + ESP_ERROR_CHECK(uart_set_pin(uartPort, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); + ESP_ERROR_CHECK(uart_driver_install(uartPort, 1024, 1024, 0, NULL, 0)); + + //-------------------------------------------- + // LOGGER + // + Logger logger(10, uartPort); + + LOGGER_PRINT("\n\r-----------------------------------------------------------------------\n\r"); + LOGGER_PRINT("System Start\n\r"); + LOGGER_PRINT("\n\r"); + LOGGER_PRINT("WordClock\n\r"); + LOGGER_PRINT("Release: %f\n\r", RELEASE); + LOGGER_PRINT("Compiled on %d %d\n\r\n\r\n\r", __TIME__, __DATE__); + + + //-------------------------------------------- + // RMT Channel + // + LOGGER_INFO("Create RMT TX channel"); + rmt_tx_channel_config_t tx_chan_config; + memset(&tx_chan_config, 0, sizeof(tx_chan_config)); + + tx_chan_config.clk_src = RMT_CLK_SRC_DEFAULT; // select source clock + tx_chan_config.gpio_num = (gpio_num_t)RMT_LED_STRIP_GPIO_NUM; + tx_chan_config.mem_block_symbols = 64; // increase the block size can make the LED less flickering + tx_chan_config.resolution_hz = RMT_LED_STRIP_RESOLUTION_HZ; + tx_chan_config.trans_queue_depth = 4; + + ESP_ERROR_CHECK(rmt_new_tx_channel(&tx_chan_config, &led_chan)); + + LOGGER_INFO("Install led strip encoder"); + led_strip_encoder_config_t encoder_config; + memset(&encoder_config, 0, sizeof(encoder_config)); + encoder_config.resolution = RMT_LED_STRIP_RESOLUTION_HZ; + + ESP_ERROR_CHECK(rmt_new_led_strip_encoder(&encoder_config, &led_encoder)); + + LOGGER_INFO("Enable RMT TX channel"); + ESP_ERROR_CHECK(rmt_enable(led_chan)); + + memset(&tx_config, 0, sizeof(tx_config)); + tx_config.loop_count = 0; + + + //-------------------------------------------- + // I2C + // + // SourceClock: GPIO 8 + // SourceData: GPIO 9 + I2C i2c0(8, 9); + + //-------------------------------------------- + // BMP280 + // + // Communicates on I2C i2c0 + // Has slave address 0x76 + BMP280 bmp280(&i2c0, 0x76); + // Reset the sensor + bmp280.resetSensor(); + // Make sure to apply a wait cycle between reset and continuous use - 2ms is advised as minimum + vTaskDelay(10); + // Initialize the BMP280 + bmp280.initialize(); + // Set the temperature Oversampling + bmp280.setSensorTemperatureOversampling(BMP280::BMP280_Oversampling_t::X1); + // Set the sensor to NORMAL mode + bmp280.setSensorMode(BMP280::BMP280_Mode_t::NORMAL); + + Temperature temperature; + + //-------------------------------------------- + // LED Matrix + // + matrix.setGlobalColour(0x10, 0, 0x04); + + + + //-------------------------------------------- + // GP Timer for automatic matrix re-draw trigger + // + gptimer_config_t timer_config = { }; + gptimer_event_callbacks_t cbs = { }; + gptimer_alarm_config_t alarm_config = { }; + + timer_config.clk_src = GPTIMER_CLK_SRC_DEFAULT; + timer_config.direction = GPTIMER_COUNT_UP; + timer_config.resolution_hz = 1000000; // 1 MHz + cbs.on_alarm = timerCallback; + alarm_config.reload_count = 0; + alarm_config.alarm_count = timer_config.resolution_hz / 60; + alarm_config.flags.auto_reload_on_alarm = true; + + ESP_ERROR_CHECK(gptimer_new_timer(&timer_config, &matrixRefreshTimer)); + ESP_ERROR_CHECK(gptimer_register_event_callbacks(matrixRefreshTimer, &cbs, NULL)); + ESP_ERROR_CHECK(gptimer_enable(matrixRefreshTimer)); + ESP_ERROR_CHECK(gptimer_set_alarm_action(matrixRefreshTimer, &alarm_config)); + ESP_ERROR_CHECK(gptimer_start(matrixRefreshTimer)); + + + // Create the development task + if(xTaskCreate(devTask, "DevTask", 2048, NULL, 3, &devTaskHandle) != pdPASS) + { + LOGGER_ERROR("Task not created"); + } + + // Create the colour Map task + if(xTaskCreate(colourMapTask, "ColourTask", 2048, NULL, 3, &colourMapTaskHandle) != pdPASS) + { + LOGGER_ERROR("Task not created"); + } + + Wifi wifi; + wifi.start_client(); + + Clock clock(Clock::mode::TEN_BEFORE_HALF); + + clockWordmap.setColour(0xFF, 0xFF, 0x00); + dayWordmap.setColour(0x20, 0xCC, 0x80); + + +// countdown(1000); + + list clockWordlist; + list tempWordList; + + + while (true) + { + + clock.generateWordlist(&clockWordlist); + + + matrix.clear(); + std::list::iterator it; + for(it = clockWordlist.begin(); it != clockWordlist.end(); it++) + { + clockWordmap.setWord(Wordmap::Language_t::NL, *it, true); + dayWordmap.setWord(Wordmap::Language_t::NL, *it, true); + } + + // Get the temperature from sensor + int currentTemperature = bmp280.getTemperature() / 100; + + LOGGER_INFO("The current temperature is: %i (%s)", currentTemperature, to_string(21)); + // Generate temperature wordlist + temperature.generateWordlist(currentTemperature, &tempWordList); + for(it = tempWordList.begin(); it != tempWordList.end(); it++) + { + tempWordmap.setWord(Wordmap::Language_t::NL, *it, true); + } + uint8_t tRed, tGreen, tBlue; + temperature.calculateRGB(currentTemperature, &tRed, &tGreen, &tBlue); + tempWordmap.setColour(tRed, tGreen, tBlue); + + // Add a seconds indicator + matrix.setPixelValue(11, 11, clock.getTime() % 2); + + // Update the matrix + matrix.tick(); + + // Update the clock every second (1000 ms) + vTaskDelay(1000); + + + } +} + + +static void devTask(void* parameters) +{ + uint32_t counter = 0; + printf("DevTask created"); + while (true) + { + (void)led_orange.SetOutput((GPIO_Value_t)(counter % 2)); + counter++; + vTaskDelay(500); + } +} + + +static void colourMapTask(void* parameters) +{ + uint8_t red = 0xF0; + uint8_t green = 0x20; + uint8_t blue = 0xF0; + +// uint8_t red = 0x00; +// uint8_t green = 0x00; +// uint8_t blue = 0x00; + + uint32_t counter = 0; + + + while (true) + { +// red = 0; +// green = 0; +// blue = 0; +// if ((counter % 2) == 0) +// { +// red = 0xFF; +// } +// if ((counter % 5) == 0) +// { +// green = 0xFF; +// } +// if ((counter % 9) == 0) +// { +// blue = 0xFF; +// } + + +// matrix.setGlobalColour(red, green, blue); + +// red = counter & 0xFF; +// green = (counter >> 8) & 0xFF; +// blue = (counter >> 16) & 0xFF; + counter++; + vTaskDelay(200); + } + +} + +static void countdown(int delay) +{ + clockWordmap.setWord(Wordmap::Language_t::NL, "ten", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "ten", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "nine", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "nine", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "eight", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "eight", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "seven", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "seven", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "six", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "six", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "five", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "five", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "four", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "four", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "three", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "three", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "two", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "two", false); + clockWordmap.setWord(Wordmap::Language_t::NL, "one", true); + vTaskDelay(delay); + clockWordmap.setWord(Wordmap::Language_t::NL, "one", false); +} + +static bool IRAM_ATTR timerCallback(gptimer_handle_t timer, const gptimer_alarm_event_data_t *edata, void *user_data) +{ + BaseType_t xHigherPriorityTaskWoken = pdFALSE; + +// xHigherPriorityTaskWoken = matrix.tick(); + + return xHigherPriorityTaskWoken == pdTRUE; +} diff --git a/code/main/src/bmp280.cpp b/code/main/old/src/bmp280.cpp similarity index 100% rename from code/main/src/bmp280.cpp rename to code/main/old/src/bmp280.cpp diff --git a/code/main/src/clock.cpp b/code/main/old/src/clock.cpp similarity index 100% rename from code/main/src/clock.cpp rename to code/main/old/src/clock.cpp diff --git a/code/main/src/clockwordmap.cpp b/code/main/old/src/clockwordmap.cpp similarity index 100% rename from code/main/src/clockwordmap.cpp rename to code/main/old/src/clockwordmap.cpp diff --git a/code/main/src/daywordmap.cpp b/code/main/old/src/daywordmap.cpp similarity index 100% rename from code/main/src/daywordmap.cpp rename to code/main/old/src/daywordmap.cpp diff --git a/code/main/src/gpio.cpp b/code/main/old/src/gpio.cpp similarity index 100% rename from code/main/src/gpio.cpp rename to code/main/old/src/gpio.cpp diff --git a/code/main/src/i2c.cpp b/code/main/old/src/i2c.cpp similarity index 100% rename from code/main/src/i2c.cpp rename to code/main/old/src/i2c.cpp diff --git a/code/main/src/led_strip_encoder.c b/code/main/old/src/led_strip_encoder.c similarity index 100% rename from code/main/src/led_strip_encoder.c rename to code/main/old/src/led_strip_encoder.c diff --git a/code/main/src/ledmatrix.cpp b/code/main/old/src/ledmatrix.cpp similarity index 100% rename from code/main/src/ledmatrix.cpp rename to code/main/old/src/ledmatrix.cpp diff --git a/code/main/src/logger.cpp b/code/main/old/src/logger.cpp similarity index 100% rename from code/main/src/logger.cpp rename to code/main/old/src/logger.cpp diff --git a/code/main/src/temperature.cpp b/code/main/old/src/temperature.cpp similarity index 100% rename from code/main/src/temperature.cpp rename to code/main/old/src/temperature.cpp diff --git a/code/main/src/temperaturewordmap.cpp b/code/main/old/src/temperaturewordmap.cpp similarity index 100% rename from code/main/src/temperaturewordmap.cpp rename to code/main/old/src/temperaturewordmap.cpp diff --git a/code/main/src/wifi.cpp b/code/main/old/src/wifi.cpp similarity index 100% rename from code/main/src/wifi.cpp rename to code/main/old/src/wifi.cpp diff --git a/code/main/src/wordmap.cpp b/code/main/old/src/wordmap.cpp similarity index 100% rename from code/main/src/wordmap.cpp rename to code/main/old/src/wordmap.cpp