From 01c5db39c1fc375920e5ba00939e5004a30dda00 Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Sun, 21 Aug 2022 12:09:47 +0200 Subject: [PATCH 01/10] Outsource display functions to display.cpp move functions for initializing and writing to display to new files display.cpp/hpp --- main/CMakeLists.txt | 1 + main/config.hpp | 2 - main/control.cpp | 61 +++++----------------------- main/control.hpp | 2 +- main/display.cpp | 97 +++++++++++++++++++++++++++++++++++++++++++++ main/display.hpp | 23 +++++++++++ 6 files changed, 133 insertions(+), 53 deletions(-) create mode 100644 main/display.cpp create mode 100644 main/display.hpp diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 2ba0eee..44f6fa5 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -5,6 +5,7 @@ idf_component_register( "control.cpp" "buzzer.cpp" "vfd.cpp" + "display.cpp" INCLUDE_DIRS "." ) diff --git a/main/config.hpp b/main/config.hpp index a4eb5d0..bf32c2a 100644 --- a/main/config.hpp +++ b/main/config.hpp @@ -1,7 +1,5 @@ #pragma once -extern "C" { #include "driver/adc.h" -} #include "gpio_evaluateSwitch.hpp" #include "buzzer.hpp" diff --git a/main/control.cpp b/main/control.cpp index 62cb483..cbec0c7 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -1,32 +1,5 @@ #include "control.hpp" -//======================== -//===== init display ===== -//======================== -max7219_t init_display(){ - // Configure SPI bus - spi_bus_config_t cfg; - cfg.mosi_io_num = DISPLAY_PIN_NUM_MOSI; - cfg.miso_io_num = -1; - cfg.sclk_io_num = DISPLAY_PIN_NUM_CLK; - cfg.quadwp_io_num = -1; - cfg.quadhd_io_num = -1; - cfg.max_transfer_sz = 0; - cfg.flags = 0; - ESP_ERROR_CHECK(spi_bus_initialize(HOST, &cfg, 1)); - - // Configure device - max7219_t dev; - dev.cascade_size = 2; - dev.digits = 0; - dev.mirrored = true; - ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, DISPLAY_PIN_NUM_CS)); - ESP_ERROR_CHECK(max7219_init(&dev)); - //0...15 - ESP_ERROR_CHECK(max7219_set_brightness(&dev, 12)); - return dev; -} - //======================== //===== init encoder ===== @@ -81,7 +54,6 @@ static const char *TAG = "control"; //tag for logging const char* systemStateStr[5] = {"COUNTING", "WINDING_START", "WINDING", "TARGET_REACHED", "MANUAL"}; systemState_t controlState = COUNTING; -max7219_t display; //display device char buf_disp[20]; //both displays char buf_disp1[10];// 8 digits + decimal point + \0 char buf_disp2[10];// 8 digits + decimal point + \0 @@ -169,28 +141,16 @@ void setDynSpeedLvl(uint8_t lvlMax = 3){ //======================== void task_control(void *pvParameter) { - //initialize display - display = init_display(); //initialize encoder encoder_queue = init_encoder(&encoder); - //----------------------------------- - //------- display welcome msg ------- - //----------------------------------- + //initialize display + display_init(); //outsourced in display.c + + //--- display welcome msg --- //display welcome message on two 7 segment displays - //show name and date - ESP_LOGI(TAG, "showing startup message..."); - max7219_clear(&display); - max7219_draw_text_7seg(&display, 0, "CUTTER 20.08.2022"); - // 1234567812 34 5678 - vTaskDelay(pdMS_TO_TICKS(700)); - //scroll "hello" over 2 displays - for (int offset = 0; offset < 23; offset++) { - max7219_clear(&display); - char hello[23] = " HELL0 "; - max7219_draw_text_7seg(&display, 0, hello + (22 - offset) ); - vTaskDelay(pdMS_TO_TICKS(50)); - } + //currently show name and date and scrolling 'hello' + display_ShowWelcomeMsg(); //================ @@ -364,16 +324,17 @@ void task_control(void *pvParameter) //--------- display --------- //--------------------------- //-- show current position on display1 --- - //sprintf(buf_tmp, "%06.1f cm", (float)lengthNow/10); //cm sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); //m // 123456789 //limit length to 8 digits + decimal point (drop decimal places when it does not fit) sprintf(buf_disp1, "%.9s", buf_tmp); + display1_showString(buf_tmp); //--- show target length on display2 --- //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm - sprintf(buf_disp2, "S0LL%5.3f", (float)lengthTarget/1000); //m + sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); //m // 1234 5678 + display2_showString(buf_tmp); //TODO: blink disp2 when set button pressed //TODO: blink disp2 when preset button pressed (exept manual mode) @@ -382,8 +343,8 @@ void task_control(void *pvParameter) //--- write to display --- //max7219_clear(&display); //results in flickering display if same value anyways - max7219_draw_text_7seg(&display, 0, buf_disp1); - max7219_draw_text_7seg(&display, 8, buf_disp2); + //max7219_draw_text_7seg(&display, 0, buf_disp1); + //max7219_draw_text_7seg(&display, 8, buf_disp2); // //switch between two display pages // if (esp_log_timestamp() - timestamp_pageSwitched > 1000){ diff --git a/main/control.hpp b/main/control.hpp index 3d2deb9..9e11f13 100644 --- a/main/control.hpp +++ b/main/control.hpp @@ -10,7 +10,6 @@ extern "C" #include "esp_log.h" #include "driver/adc.h" -#include #include "rotary_encoder.h" } #include @@ -19,6 +18,7 @@ extern "C" #include "gpio_evaluateSwitch.hpp" #include "buzzer.hpp" #include "vfd.hpp" +#include "display.hpp" diff --git a/main/display.cpp b/main/display.cpp new file mode 100644 index 0000000..847cbfa --- /dev/null +++ b/main/display.cpp @@ -0,0 +1,97 @@ +#include "display.hpp" + + +//=== variables === +static const char *TAG = "display"; //tag for logging +max7219_t display; + +//======================== +//===== init display ===== +//======================== +void display_init(){ + // Configure SPI bus + spi_bus_config_t cfg; + cfg.mosi_io_num = DISPLAY_PIN_NUM_MOSI; + cfg.miso_io_num = -1; + cfg.sclk_io_num = DISPLAY_PIN_NUM_CLK; + cfg.quadwp_io_num = -1; + cfg.quadhd_io_num = -1; + cfg.max_transfer_sz = 0; + cfg.flags = 0; + ESP_ERROR_CHECK(spi_bus_initialize(HOST, &cfg, 1)); + + // Configure device + max7219_t dev; + dev.cascade_size = 2; + dev.digits = 0; + dev.mirrored = true; + ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, DISPLAY_PIN_NUM_CS)); + ESP_ERROR_CHECK(max7219_init(&dev)); + //0...15 + ESP_ERROR_CHECK(max7219_set_brightness(&dev, 12)); + //return dev; + display = dev; +} + + + +void display_ShowWelcomeMsg(){ + //----------------------------------- + //------- display welcome msg ------- + //----------------------------------- + //display welcome message on two 7 segment displays + //show name and date + ESP_LOGI(TAG, "showing startup message..."); + max7219_clear(&display); + max7219_draw_text_7seg(&display, 0, "CUTTER 20.08.2022"); + // 1234567812 34 5678 + vTaskDelay(pdMS_TO_TICKS(700)); + //scroll "hello" over 2 displays + for (int offset = 0; offset < 23; offset++) { + max7219_clear(&display); + char hello[23] = " HELL0 "; + max7219_draw_text_7seg(&display, 0, hello + (22 - offset) ); + vTaskDelay(pdMS_TO_TICKS(50)); + } +} + + + +void display1_showString(const char * buf){ + max7219_draw_text_7seg(&display, 0, buf); +} +void display2_showString(const char * buf){ + max7219_draw_text_7seg(&display, 8, buf); +} + +void display_showString(uint8_t pos, const char * buf){ + max7219_draw_text_7seg(&display, pos, buf); +} + +// //--------------------------- +// //--------- display --------- +// //--------------------------- +// //-- show current position on display1 --- +// //sprintf(buf_tmp, "%06.1f cm", (float)lengthNow/10); //cm +// sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); //m +// // 123456789 +// //limit length to 8 digits + decimal point (drop decimal places when it does not fit) +// sprintf(buf_disp1, "%.9s", buf_tmp); +// +// //--- show target length on display2 --- +// //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm +// sprintf(buf_disp2, "S0LL%5.3f", (float)lengthTarget/1000); //m +// // 1234 5678 +// +// //TODO: blink disp2 when set button pressed +// //TODO: blink disp2 when preset button pressed (exept manual mode) +// //TODO: write "MAN CTL" to disp2 when in manual mode +// //TODO: display or blink "REACHED" when reached state and start pressed +// +// //--- write to display --- +// //max7219_clear(&display); //results in flickering display if same value anyways +// max7219_draw_text_7seg(&display, 0, buf_disp1); +// max7219_draw_text_7seg(&display, 8, buf_disp2); +// +// +// diff --git a/main/display.hpp b/main/display.hpp new file mode 100644 index 0000000..87a7e5b --- /dev/null +++ b/main/display.hpp @@ -0,0 +1,23 @@ +#pragma once +extern "C" +{ +#include +#include +#include +#include +#include "freertos/queue.h" +#include "esp_system.h" +#include "esp_log.h" +#include "driver/adc.h" + +#include +#include "rotary_encoder.h" +} + +#include "config.hpp" + +void display_init(); +void display_ShowWelcomeMsg(); +void display1_showString(const char * buf); +void display2_showString(const char * buf); +void display_showString(uint8_t pos, const char * buf); From c8ffd94fe947b97dbc93cb5a63b900e8d0e6b0ca Mon Sep 17 00:00:00 2001 From: jonny_l480 Date: Sun, 21 Aug 2022 14:08:59 +0200 Subject: [PATCH 02/10] Fix bug spi init; Fix random string + flickering Fix bug where sartup randomly crasehd several times because spi config was initialized with random data -> init with 0 Fix bug where length now was not truncated resulting in flickerin Fix bug in welcome message where hello string contained random data --- main/control.cpp | 2 +- main/display.cpp | 6 +++++- main/display.hpp | 2 ++ 3 files changed, 8 insertions(+), 2 deletions(-) diff --git a/main/control.cpp b/main/control.cpp index cbec0c7..cc38a8f 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -328,7 +328,7 @@ void task_control(void *pvParameter) // 123456789 //limit length to 8 digits + decimal point (drop decimal places when it does not fit) sprintf(buf_disp1, "%.9s", buf_tmp); - display1_showString(buf_tmp); + display1_showString(buf_disp1); //--- show target length on display2 --- //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm diff --git a/main/display.cpp b/main/display.cpp index 847cbfa..7a583c2 100644 --- a/main/display.cpp +++ b/main/display.cpp @@ -9,8 +9,11 @@ max7219_t display; //===== init display ===== //======================== void display_init(){ + + ESP_LOGI(TAG, "initializing display..."); // Configure SPI bus spi_bus_config_t cfg; + memset(&cfg, 0, sizeof(spi_bus_config_t)); //init bus config with 0 to prevent bugs with random flags cfg.mosi_io_num = DISPLAY_PIN_NUM_MOSI; cfg.miso_io_num = -1; cfg.sclk_io_num = DISPLAY_PIN_NUM_CLK; @@ -31,6 +34,7 @@ void display_init(){ ESP_ERROR_CHECK(max7219_set_brightness(&dev, 12)); //return dev; display = dev; + ESP_LOGI(TAG, "initializing display - done"); } @@ -49,7 +53,7 @@ void display_ShowWelcomeMsg(){ //scroll "hello" over 2 displays for (int offset = 0; offset < 23; offset++) { max7219_clear(&display); - char hello[23] = " HELL0 "; + char hello[40] = " HELL0 "; max7219_draw_text_7seg(&display, 0, hello + (22 - offset) ); vTaskDelay(pdMS_TO_TICKS(50)); } diff --git a/main/display.hpp b/main/display.hpp index 87a7e5b..1c9e250 100644 --- a/main/display.hpp +++ b/main/display.hpp @@ -13,6 +13,8 @@ extern "C" #include #include "rotary_encoder.h" } +#include + #include "config.hpp" From 9665560bbbb3ea4e535b7e3b17f1dee5eb11321b Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Sun, 21 Aug 2022 17:30:00 +0200 Subject: [PATCH 03/10] Add blink strings functionality add functions to display.cpp - void display2_handle(); - void display2_blinkStrings(const char * strOn, const char * strOff, uint32_t msOn, uint32_t msOff); control.cpp: - blink target length when set button is pressed - blink MANUAL when in manual mode --- main/config.hpp | 2 ++ main/control.cpp | 62 ++++++++++++++++++------------------------ main/display.cpp | 71 ++++++++++++++++++++++++++++++++++++++++++++++-- main/display.hpp | 5 ++++ 4 files changed, 102 insertions(+), 38 deletions(-) diff --git a/main/config.hpp b/main/config.hpp index bf32c2a..a4eb5d0 100644 --- a/main/config.hpp +++ b/main/config.hpp @@ -1,5 +1,7 @@ #pragma once +extern "C" { #include "driver/adc.h" +} #include "gpio_evaluateSwitch.hpp" #include "buzzer.hpp" diff --git a/main/control.cpp b/main/control.cpp index cc38a8f..54a54c1 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -320,51 +320,43 @@ void task_control(void *pvParameter) - //--------------------------- - //--------- display --------- - //--------------------------- - //-- show current position on display1 --- - sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); //m + //-------------------------- + //-------- display1 -------- + //-------------------------- + //show current position on display + sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); // 123456789 //limit length to 8 digits + decimal point (drop decimal places when it does not fit) sprintf(buf_disp1, "%.9s", buf_tmp); display1_showString(buf_disp1); - //--- show target length on display2 --- - //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm - sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); //m - // 1234 5678 - display2_showString(buf_tmp); - + + //-------------------------- + //-------- display2 -------- + //-------------------------- + //setting target length: blink target length + if (SW_SET.state == true){ + sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); + display2_blinkStrings(buf_tmp, " ", 400, 100); + } + //manual state: blink "manual" + else if (controlState == MANUAL) { + display2_blinkStrings(" MANUAL ", " ", 1000, 500); + } + //otherwise show target length + else { + //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm + sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); //m + // 1234 5678 + display2_showString(buf_tmp); + } + + //TODO: blink disp2 when set button pressed //TODO: blink disp2 when preset button pressed (exept manual mode) //TODO: write "MAN CTL" to disp2 when in manual mode //TODO: display or blink "REACHED" when reached state and start pressed - //--- write to display --- - //max7219_clear(&display); //results in flickering display if same value anyways - //max7219_draw_text_7seg(&display, 0, buf_disp1); - //max7219_draw_text_7seg(&display, 8, buf_disp2); - - // //switch between two display pages - // if (esp_log_timestamp() - timestamp_pageSwitched > 1000){ - // timestamp_pageSwitched = esp_log_timestamp(); - // page = !page; - // } - // max7219_clear(&display); - // if (page){ - // //display current position - // display_current_distance(&display, &encoder); - // } else { - // //display counter - // sprintf(display_buf, "lvl: %02d", count); - // max7219_draw_text_7seg(&display, 0, display_buf); - // //count++; - // } - - //sprintf(display_buf, "S0LL 12.3"); - //max7219_draw_text_7seg(&display, 8, display_buf); - } } diff --git a/main/display.cpp b/main/display.cpp index 7a583c2..152e410 100644 --- a/main/display.cpp +++ b/main/display.cpp @@ -5,6 +5,17 @@ static const char *TAG = "display"; //tag for logging max7219_t display; +bool disp1_blinkMode = false; + +char disp2_strOn[20]; +char disp2_strOff[20]; +bool disp2_state = false; +bool disp2_blinkMode = false; +uint32_t disp2_timestampOn; +uint32_t disp2_timestampOff; +uint32_t disp2_msOn; +uint32_t disp2_msOff; + //======================== //===== init display ===== //======================== @@ -62,16 +73,70 @@ void display_ShowWelcomeMsg(){ void display1_showString(const char * buf){ - max7219_draw_text_7seg(&display, 0, buf); + max7219_draw_text_7seg(&display, 0, buf); + disp1_blinkMode = false; } void display2_showString(const char * buf){ - max7219_draw_text_7seg(&display, 8, buf); + max7219_draw_text_7seg(&display, 8, buf); + disp2_blinkMode = false; } void display_showString(uint8_t pos, const char * buf){ - max7219_draw_text_7seg(&display, pos, buf); + max7219_draw_text_7seg(&display, pos, buf); } + + + +//function that handles blinking of display2 +void display2_handle(){ + if (disp2_blinkMode == false){ + return; + } + //--- define state on/off --- + if (disp2_state == true){ //display in ON state + if (esp_log_timestamp() - disp2_timestampOn > disp2_msOn){ + disp2_state = false; + disp2_timestampOff = esp_log_timestamp(); + } + } else { //display in OFF state + if (esp_log_timestamp() - disp2_timestampOff > disp2_msOff) { + disp2_state = true; + disp2_timestampOn = esp_log_timestamp(); + } + } + + //--- draw text of current state --- + if (disp2_state) { + max7219_draw_text_7seg(&display, 8, disp2_strOn); + } else { + max7219_draw_text_7seg(&display, 8, disp2_strOff); + } +} + + + +//function switches between two strings in a given interval +void display2_blinkStrings(const char * strOn, const char * strOff, uint32_t msOn, uint32_t msOff){ + //copy variables + strcpy(disp2_strOn, strOn); + strcpy(disp2_strOff, strOff); + disp2_msOn = msOn; + disp2_msOff = msOff; + //set to blink mode + disp2_blinkMode = true; + //run handle function for display update + display2_handle(); +} + + + + + + + + + // //--------------------------- // //--------- display --------- // //--------------------------- diff --git a/main/display.hpp b/main/display.hpp index 1c9e250..722ca9a 100644 --- a/main/display.hpp +++ b/main/display.hpp @@ -23,3 +23,8 @@ void display_ShowWelcomeMsg(); void display1_showString(const char * buf); void display2_showString(const char * buf); void display_showString(uint8_t pos, const char * buf); + +//function that handles blinking of display2 +void display2_handle(); +//function switches between two strings in a given interval +void display2_blinkStrings(const char * strOn, const char * strOff, uint32_t msOn, uint32_t msOff); From 586c335896e8dd196b0f4950680421e29186cf8e Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Mon, 22 Aug 2022 16:28:42 +0200 Subject: [PATCH 04/10] Create 'handledDisplay' class To abstract rather complex functions for 7 segment displays e.g. blink text, scroll text... a class was created. This makes it possible to have 2 instances for displayTop and displayBottom resulting in no duplicate code --- main/control.cpp | 15 ++-- main/control.hpp | 2 + main/display.cpp | 179 +++++++++++++++++++++-------------------------- main/display.hpp | 46 +++++++++--- 4 files changed, 129 insertions(+), 113 deletions(-) diff --git a/main/control.cpp b/main/control.cpp index 1de6edc..3441bb7 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -145,12 +145,15 @@ void task_control(void *pvParameter) encoder_queue = init_encoder(&encoder); //initialize display - display_init(); //outsourced in display.c + max7219_t two7SegDisplays = display_init(); + //create two separate handled display instances + handledDisplay displayTop(two7SegDisplays, 0); + handledDisplay displayBot(two7SegDisplays, 8); //--- display welcome msg --- //display welcome message on two 7 segment displays //currently show name and date and scrolling 'hello' - display_ShowWelcomeMsg(); + display_ShowWelcomeMsg(two7SegDisplays); //================ @@ -338,7 +341,7 @@ void task_control(void *pvParameter) // 123456789 //limit length to 8 digits + decimal point (drop decimal places when it does not fit) sprintf(buf_disp1, "%.9s", buf_tmp); - display1_showString(buf_disp1); + displayTop.showString(buf_disp1); //-------------------------- @@ -347,18 +350,18 @@ void task_control(void *pvParameter) //setting target length: blink target length if (SW_SET.state == true){ sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); - display2_blinkStrings(buf_tmp, " ", 400, 100); + displayBot.blinkStrings(buf_tmp, " ", 300, 100); } //manual state: blink "manual" else if (controlState == MANUAL) { - display2_blinkStrings(" MANUAL ", " ", 1000, 500); + displayBot.blinkStrings(" MANUAL ", " ", 1000, 800); } //otherwise show target length else { //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); //m // 1234 5678 - display2_showString(buf_tmp); + displayBot.showString(buf_tmp); } diff --git a/main/control.hpp b/main/control.hpp index 9e11f13..921a021 100644 --- a/main/control.hpp +++ b/main/control.hpp @@ -11,6 +11,8 @@ extern "C" #include "driver/adc.h" #include "rotary_encoder.h" +#include "max7219.h" + } #include diff --git a/main/display.cpp b/main/display.cpp index 152e410..ad7639f 100644 --- a/main/display.cpp +++ b/main/display.cpp @@ -3,23 +3,15 @@ //=== variables === static const char *TAG = "display"; //tag for logging -max7219_t display; -bool disp1_blinkMode = false; -char disp2_strOn[20]; -char disp2_strOff[20]; -bool disp2_state = false; -bool disp2_blinkMode = false; -uint32_t disp2_timestampOn; -uint32_t disp2_timestampOff; -uint32_t disp2_msOn; -uint32_t disp2_msOff; -//======================== -//===== init display ===== -//======================== -void display_init(){ +//============================== +//======== init display ======== +//============================== +//initialize display with parameters defined in config.hpp +//TODO: dont use global variables/macros here +max7219_t display_init(){ ESP_LOGI(TAG, "initializing display..."); // Configure SPI bus @@ -43,124 +35,115 @@ void display_init(){ ESP_ERROR_CHECK(max7219_init(&dev)); //0...15 ESP_ERROR_CHECK(max7219_set_brightness(&dev, 12)); - //return dev; - display = dev; + return dev; + //display = dev; ESP_LOGI(TAG, "initializing display - done"); } -void display_ShowWelcomeMsg(){ - //----------------------------------- - //------- display welcome msg ------- - //----------------------------------- +//=================================== +//======= display welcome msg ======= +//=================================== +void display_ShowWelcomeMsg(max7219_t dev){ //display welcome message on two 7 segment displays //show name and date ESP_LOGI(TAG, "showing startup message..."); - max7219_clear(&display); - max7219_draw_text_7seg(&display, 0, "CUTTER 20.08.2022"); + max7219_clear(&dev); + max7219_draw_text_7seg(&dev, 0, "CUTTER 20.08.2022"); // 1234567812 34 5678 vTaskDelay(pdMS_TO_TICKS(700)); //scroll "hello" over 2 displays for (int offset = 0; offset < 23; offset++) { - max7219_clear(&display); + max7219_clear(&dev); char hello[40] = " HELL0 "; - max7219_draw_text_7seg(&display, 0, hello + (22 - offset) ); + max7219_draw_text_7seg(&dev, 0, hello + (22 - offset) ); vTaskDelay(pdMS_TO_TICKS(50)); } } -void display1_showString(const char * buf){ - max7219_draw_text_7seg(&display, 0, buf); - disp1_blinkMode = false; -} -void display2_showString(const char * buf){ - max7219_draw_text_7seg(&display, 8, buf); - disp2_blinkMode = false; -} -void display_showString(uint8_t pos, const char * buf){ - max7219_draw_text_7seg(&display, pos, buf); + +//--------------------------------- +//---------- constructor ---------- +//--------------------------------- +handledDisplay::handledDisplay(max7219_t displayDevice, uint8_t posStart_f) { + ESP_LOGI(TAG, "Creating handledDisplay instance with startPos at %i", posStart); + //copy variables + dev = displayDevice; + posStart = posStart_f; } +//-------------------------------- +//---------- showString ---------- +//-------------------------------- +//function that displays a given string on the display +void handledDisplay::showString(const char * buf, uint8_t pos_f){ + //calculate actual absolute position + uint8_t pos = posStart + pos_f; + //draw string on display + max7219_draw_text_7seg(&dev, pos, buf); + //disable blinking mode + blinkMode = false; +} -//function that handles blinking of display2 -void display2_handle(){ - if (disp2_blinkMode == false){ - return; + + +//---------------------------------- +//---------- blinkStrings ---------- +//---------------------------------- +//function switches between two strings in a given interval +void handledDisplay::blinkStrings(const char * strOn_f, const char * strOff_f, uint32_t msOn_f, uint32_t msOff_f){ + //copy/update variables + strcpy(strOn, strOn_f); + strcpy(strOff, strOff_f); + msOn = msOn_f; + msOff = msOff_f; + //if changed to blink mode just now + if (blinkMode == false) { + ESP_LOGI(TAG, "pos:%i changing to blink mode", posStart); + blinkMode = true; + //start with on state + state = true; + timestampOn = esp_log_timestamp(); } + //run handle function for display update + handle(); +} + + + +//-------------------------------- +//------------ handle ------------ +//-------------------------------- +//function that handles blinking of display2 +void handledDisplay::handle() { + if (blinkMode == false){ + return; //not in blinking mode - nothing todo + } + //--- define state on/off --- - if (disp2_state == true){ //display in ON state - if (esp_log_timestamp() - disp2_timestampOn > disp2_msOn){ - disp2_state = false; - disp2_timestampOff = esp_log_timestamp(); + if (state == true){ //display in ON state + if (esp_log_timestamp() - timestampOn > msOn){ + state = false; + timestampOff = esp_log_timestamp(); } } else { //display in OFF state - if (esp_log_timestamp() - disp2_timestampOff > disp2_msOff) { - disp2_state = true; - disp2_timestampOn = esp_log_timestamp(); + if (esp_log_timestamp() - timestampOff > msOff) { + state = true; + timestampOn = esp_log_timestamp(); } } //--- draw text of current state --- - if (disp2_state) { - max7219_draw_text_7seg(&display, 8, disp2_strOn); + if (state) { + max7219_draw_text_7seg(&dev, posStart, strOn); } else { - max7219_draw_text_7seg(&display, 8, disp2_strOff); + max7219_draw_text_7seg(&dev, posStart, strOff); } } - - -//function switches between two strings in a given interval -void display2_blinkStrings(const char * strOn, const char * strOff, uint32_t msOn, uint32_t msOff){ - //copy variables - strcpy(disp2_strOn, strOn); - strcpy(disp2_strOff, strOff); - disp2_msOn = msOn; - disp2_msOff = msOff; - //set to blink mode - disp2_blinkMode = true; - //run handle function for display update - display2_handle(); -} - - - - - - - - - -// //--------------------------- -// //--------- display --------- -// //--------------------------- -// //-- show current position on display1 --- -// //sprintf(buf_tmp, "%06.1f cm", (float)lengthNow/10); //cm -// sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); //m -// // 123456789 -// //limit length to 8 digits + decimal point (drop decimal places when it does not fit) -// sprintf(buf_disp1, "%.9s", buf_tmp); -// -// //--- show target length on display2 --- -// //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm -// sprintf(buf_disp2, "S0LL%5.3f", (float)lengthTarget/1000); //m -// // 1234 5678 -// -// //TODO: blink disp2 when set button pressed -// //TODO: blink disp2 when preset button pressed (exept manual mode) -// //TODO: write "MAN CTL" to disp2 when in manual mode -// //TODO: display or blink "REACHED" when reached state and start pressed -// -// //--- write to display --- -// //max7219_clear(&display); //results in flickering display if same value anyways -// max7219_draw_text_7seg(&display, 0, buf_disp1); -// max7219_draw_text_7seg(&display, 8, buf_disp2); -// -// -// diff --git a/main/display.hpp b/main/display.hpp index 722ca9a..b3a916a 100644 --- a/main/display.hpp +++ b/main/display.hpp @@ -18,13 +18,41 @@ extern "C" #include "config.hpp" -void display_init(); -void display_ShowWelcomeMsg(); -void display1_showString(const char * buf); -void display2_showString(const char * buf); -void display_showString(uint8_t pos, const char * buf); +//function for initializing the display using configuration from macros in config.hpp +max7219_t display_init(); -//function that handles blinking of display2 -void display2_handle(); -//function switches between two strings in a given interval -void display2_blinkStrings(const char * strOn, const char * strOff, uint32_t msOn, uint32_t msOff); +//show welcome message on the entire display +void display_ShowWelcomeMsg(max7219_t displayDevice); + + +class handledDisplay { + public: + //--- constructor --- + //TODO add posMax to prevent writing in segments of other instance + handledDisplay(max7219_t displayDevice, uint8_t posStart); + + //--- methods --- + void showString(const char * buf, uint8_t pos = 0); + //function switches between two strings in a given interval + void blinkStrings(const char * strOn, const char * strOff, uint32_t msOn, uint32_t msOff); + //TODO: add 'scroll string' method + //function that handles blinking of display + void handle(); + + private: + + //--- variables --- + //config + max7219_t dev; + uint8_t posStart; //absolute position this display instance starts (e.g. multiple or very long 7 segment display) + + //blink mode + char strOn[20]; + char strOff[20]; + bool state = false; + bool blinkMode = false; + uint32_t msOn; + uint32_t msOff; + uint32_t timestampOn; + uint32_t timestampOff; +}; From 4e2cc070d1200164f652e07ca513fe0de1873ed3 Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Mon, 22 Aug 2022 18:27:28 +0200 Subject: [PATCH 05/10] Show manual speed lvl; Change buf - when in manual mode blink between 'manual' and motor 'lvl' on bottom display - optimize buffer variables and sizes --- main/control.cpp | 31 +++++++++++++++---------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/main/control.cpp b/main/control.cpp index 3441bb7..8627390 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -54,10 +54,8 @@ static const char *TAG = "control"; //tag for logging const char* systemStateStr[5] = {"COUNTING", "WINDING_START", "WINDING", "TARGET_REACHED", "MANUAL"}; systemState_t controlState = COUNTING; -char buf_disp[20]; //both displays -char buf_disp1[10];// 8 digits + decimal point + \0 -char buf_disp2[10];// 8 digits + decimal point + \0 -char buf_tmp[15]; +char buf10[10];// 8 digits + decimal point + \0 +char buf20[20]; rotary_encoder_info_t encoder; //encoder device/info QueueHandle_t encoder_queue = NULL; //encoder event queue @@ -71,7 +69,7 @@ int lengthTarget = 3000; //target length in mm int lengthRemaining = 0; //(target - now) length needed for reaching the target int potiRead = 0; //voltage read from adc uint32_t timestamp_motorStarted = 0; //timestamp winding started - +uint8_t manualSpeedLvl = 0; //current motor speed level in manual mode //===== change State ===== //function for changing the controlState with log output @@ -309,7 +307,7 @@ void task_control(void *pvParameter) //read poti value potiRead = readAdc(ADC_CHANNEL_POTI); //0-4095 //scale poti to speed levels 0-3 - uint8_t level = round( (float)potiRead / 4095 * 3 ); + manualSpeedLvl = round( (float)potiRead / 4095 * 3 ); //exit manual mode if preset2 released if ( SW_PRESET2.state == false ) { changeState(COUNTING); @@ -317,12 +315,12 @@ void task_control(void *pvParameter) } //P2 + P1 -> turn left else if ( SW_PRESET1.state && !SW_PRESET3.state ) { - vfd_setSpeedLevel(level); //TODO: use poti input for level + vfd_setSpeedLevel(manualSpeedLvl); //TODO: use poti input for level vfd_setState(true, REV); } //P2 + P3 -> turn right else if ( SW_PRESET3.state && !SW_PRESET1.state ) { - vfd_setSpeedLevel(level); //TODO: use poti input for level + vfd_setSpeedLevel(manualSpeedLvl); //TODO: use poti input for level vfd_setState(true, FWD); } //no valid switch combination -> turn off motor @@ -337,11 +335,11 @@ void task_control(void *pvParameter) //-------- display1 -------- //-------------------------- //show current position on display - sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); + sprintf(buf20, "1ST %5.4f", (float)lengthNow/1000); // 123456789 //limit length to 8 digits + decimal point (drop decimal places when it does not fit) - sprintf(buf_disp1, "%.9s", buf_tmp); - displayTop.showString(buf_disp1); + sprintf(buf10, "%.9s", buf20); + displayTop.showString(buf10); //-------------------------- @@ -349,19 +347,20 @@ void task_control(void *pvParameter) //-------------------------- //setting target length: blink target length if (SW_SET.state == true){ - sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); - displayBot.blinkStrings(buf_tmp, " ", 300, 100); + sprintf(buf10, "S0LL%5.3f", (float)lengthTarget/1000); + displayBot.blinkStrings(buf10, "S0LL ", 300, 100); } //manual state: blink "manual" else if (controlState == MANUAL) { - displayBot.blinkStrings(" MANUAL ", " ", 1000, 800); + sprintf(buf10, " lvl %02i ", manualSpeedLvl); + displayBot.blinkStrings(" MANUAL ", buf10, 1000, 800); } //otherwise show target length else { //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm - sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); //m + sprintf(buf10, "S0LL%5.3f", (float)lengthTarget/1000); //m // 1234 5678 - displayBot.showString(buf_tmp); + displayBot.showString(buf10); } From 9f329101a1767e63e0d9dbc1a758f4775c2f2fc9 Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Mon, 22 Aug 2022 22:22:45 +0200 Subject: [PATCH 06/10] Fix bug display lib: random characters flickering Fix bug in display library where random chars were written to display even though the array has ended. Resulting in random character appearing briefly when only first display got written. -> stop loop when 0-termination of char array is reached --- components/max7219/max7219.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/components/max7219/max7219.c b/components/max7219/max7219.c index fd8939c..5a59893 100644 --- a/components/max7219/max7219.c +++ b/components/max7219/max7219.c @@ -236,7 +236,7 @@ esp_err_t max7219_draw_text_7seg(max7219_t *dev, uint8_t pos, const char *s) { CHECK_ARG(dev && s); - while (s && pos < dev->digits) + while (*s != '\0' && pos < dev->digits) { uint8_t c = get_char(dev, *s); if (*(s + 1) == '.') From 2e331d841d5ea7b5c069089a014509af00bcb6f2 Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Mon, 22 Aug 2022 22:37:23 +0200 Subject: [PATCH 07/10] Revert "Show manual speed lvl; Change buf" This reverts commit 4e2cc070d1200164f652e07ca513fe0de1873ed3. Other approach for displaying manual speed lvl found --- main/control.cpp | 31 ++++++++++++++++--------------- 1 file changed, 16 insertions(+), 15 deletions(-) diff --git a/main/control.cpp b/main/control.cpp index 8627390..3441bb7 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -54,8 +54,10 @@ static const char *TAG = "control"; //tag for logging const char* systemStateStr[5] = {"COUNTING", "WINDING_START", "WINDING", "TARGET_REACHED", "MANUAL"}; systemState_t controlState = COUNTING; -char buf10[10];// 8 digits + decimal point + \0 -char buf20[20]; +char buf_disp[20]; //both displays +char buf_disp1[10];// 8 digits + decimal point + \0 +char buf_disp2[10];// 8 digits + decimal point + \0 +char buf_tmp[15]; rotary_encoder_info_t encoder; //encoder device/info QueueHandle_t encoder_queue = NULL; //encoder event queue @@ -69,7 +71,7 @@ int lengthTarget = 3000; //target length in mm int lengthRemaining = 0; //(target - now) length needed for reaching the target int potiRead = 0; //voltage read from adc uint32_t timestamp_motorStarted = 0; //timestamp winding started -uint8_t manualSpeedLvl = 0; //current motor speed level in manual mode + //===== change State ===== //function for changing the controlState with log output @@ -307,7 +309,7 @@ void task_control(void *pvParameter) //read poti value potiRead = readAdc(ADC_CHANNEL_POTI); //0-4095 //scale poti to speed levels 0-3 - manualSpeedLvl = round( (float)potiRead / 4095 * 3 ); + uint8_t level = round( (float)potiRead / 4095 * 3 ); //exit manual mode if preset2 released if ( SW_PRESET2.state == false ) { changeState(COUNTING); @@ -315,12 +317,12 @@ void task_control(void *pvParameter) } //P2 + P1 -> turn left else if ( SW_PRESET1.state && !SW_PRESET3.state ) { - vfd_setSpeedLevel(manualSpeedLvl); //TODO: use poti input for level + vfd_setSpeedLevel(level); //TODO: use poti input for level vfd_setState(true, REV); } //P2 + P3 -> turn right else if ( SW_PRESET3.state && !SW_PRESET1.state ) { - vfd_setSpeedLevel(manualSpeedLvl); //TODO: use poti input for level + vfd_setSpeedLevel(level); //TODO: use poti input for level vfd_setState(true, FWD); } //no valid switch combination -> turn off motor @@ -335,11 +337,11 @@ void task_control(void *pvParameter) //-------- display1 -------- //-------------------------- //show current position on display - sprintf(buf20, "1ST %5.4f", (float)lengthNow/1000); + sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); // 123456789 //limit length to 8 digits + decimal point (drop decimal places when it does not fit) - sprintf(buf10, "%.9s", buf20); - displayTop.showString(buf10); + sprintf(buf_disp1, "%.9s", buf_tmp); + displayTop.showString(buf_disp1); //-------------------------- @@ -347,20 +349,19 @@ void task_control(void *pvParameter) //-------------------------- //setting target length: blink target length if (SW_SET.state == true){ - sprintf(buf10, "S0LL%5.3f", (float)lengthTarget/1000); - displayBot.blinkStrings(buf10, "S0LL ", 300, 100); + sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); + displayBot.blinkStrings(buf_tmp, " ", 300, 100); } //manual state: blink "manual" else if (controlState == MANUAL) { - sprintf(buf10, " lvl %02i ", manualSpeedLvl); - displayBot.blinkStrings(" MANUAL ", buf10, 1000, 800); + displayBot.blinkStrings(" MANUAL ", " ", 1000, 800); } //otherwise show target length else { //sprintf(buf_disp2, "%06.1f cm", (float)lengthTarget/10); //cm - sprintf(buf10, "S0LL%5.3f", (float)lengthTarget/1000); //m + sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); //m // 1234 5678 - displayBot.showString(buf10); + displayBot.showString(buf_tmp); } From 92f729908077416e6b660e6d3c00a7cf0ff0eabd Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Mon, 22 Aug 2022 23:02:26 +0200 Subject: [PATCH 08/10] Disp: show manual lvl and dir, brightness, soll blink - in manual mode blink between 'manual' and lvl+direction indication on display 2 - when setting custom target length only blink value not "soll" - reduce brightness from 12 to 8 because of high display temperature --- main/control.cpp | 9 ++++++--- main/display.cpp | 2 +- 2 files changed, 7 insertions(+), 4 deletions(-) diff --git a/main/control.cpp b/main/control.cpp index 3441bb7..29e23ec 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -63,7 +63,6 @@ rotary_encoder_info_t encoder; //encoder device/info QueueHandle_t encoder_queue = NULL; //encoder event queue rotary_encoder_state_t encoderState; -uint8_t count = 0; //count for testing uint32_t timestamp_pageSwitched = 0; bool page = false; //store page number currently displayed int lengthNow = 0; //length measured in mm @@ -319,15 +318,19 @@ void task_control(void *pvParameter) else if ( SW_PRESET1.state && !SW_PRESET3.state ) { vfd_setSpeedLevel(level); //TODO: use poti input for level vfd_setState(true, REV); + sprintf(buf_disp2, "[--%02i ", level); + // 123 45 678 } //P2 + P3 -> turn right else if ( SW_PRESET3.state && !SW_PRESET1.state ) { vfd_setSpeedLevel(level); //TODO: use poti input for level vfd_setState(true, FWD); + sprintf(buf_disp2, " %02i--]", level); } //no valid switch combination -> turn off motor else { vfd_setState(false); + sprintf(buf_disp2, " %02i ", level); } } @@ -350,11 +353,11 @@ void task_control(void *pvParameter) //setting target length: blink target length if (SW_SET.state == true){ sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); - displayBot.blinkStrings(buf_tmp, " ", 300, 100); + displayBot.blinkStrings(buf_tmp, "S0LL ", 300, 100); } //manual state: blink "manual" else if (controlState == MANUAL) { - displayBot.blinkStrings(" MANUAL ", " ", 1000, 800); + displayBot.blinkStrings(" MANUAL ", buf_disp2, 1000, 1000); } //otherwise show target length else { diff --git a/main/display.cpp b/main/display.cpp index ad7639f..9e8d493 100644 --- a/main/display.cpp +++ b/main/display.cpp @@ -34,7 +34,7 @@ max7219_t display_init(){ ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, DISPLAY_PIN_NUM_CS)); ESP_ERROR_CHECK(max7219_init(&dev)); //0...15 - ESP_ERROR_CHECK(max7219_set_brightness(&dev, 12)); + ESP_ERROR_CHECK(max7219_set_brightness(&dev, 9)); return dev; //display = dev; ESP_LOGI(TAG, "initializing display - done"); From c1a12d93f0566426394fc477a9a4d165c840f0de Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Tue, 23 Aug 2022 11:55:02 +0200 Subject: [PATCH 09/10] Add blink method to display class - Add method to trigger blinking of the display for a certain count and durations also with a optional off-string - Add 3x blinking after applying a new target length with preset or set buttons --- main/control.cpp | 8 ++++ main/display.cpp | 107 +++++++++++++++++++++++++++++++++-------------- main/display.hpp | 15 +++++-- 3 files changed, 95 insertions(+), 35 deletions(-) diff --git a/main/control.cpp b/main/control.cpp index 29e23ec..2557cf3 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -237,6 +237,7 @@ void task_control(void *pvParameter) } if (SW_SET.fallingEdge) { buzzer.beep(2, 70, 50); + displayBot.blink(3, 100, 100, "S0LL "); } @@ -245,14 +246,17 @@ void task_control(void *pvParameter) if (SW_PRESET1.risingEdge){ lengthTarget = 1000; buzzer.beep(lengthTarget/1000, 25, 30); + displayBot.blink(3, 100, 100, "S0LL "); } else if (SW_PRESET2.risingEdge) { lengthTarget = 5000; buzzer.beep(lengthTarget/1000, 25, 30); + displayBot.blink(3, 100, 100, "S0LL "); } else if (SW_PRESET3.risingEdge) { lengthTarget = 10000; buzzer.beep(lengthTarget/1000, 25, 30); + displayBot.blink(3, 100, 100, "S0LL "); } } @@ -339,6 +343,8 @@ void task_control(void *pvParameter) //-------------------------- //-------- display1 -------- //-------------------------- + //run handle function + displayTop.handle(); //show current position on display sprintf(buf_tmp, "1ST %5.4f", (float)lengthNow/1000); // 123456789 @@ -350,6 +356,8 @@ void task_control(void *pvParameter) //-------------------------- //-------- display2 -------- //-------------------------- + //run handle function + displayBot.handle(); //setting target length: blink target length if (SW_SET.state == true){ sprintf(buf_tmp, "S0LL%5.3f", (float)lengthTarget/1000); diff --git a/main/display.cpp b/main/display.cpp index 9e8d493..d5b5fa0 100644 --- a/main/display.cpp +++ b/main/display.cpp @@ -34,7 +34,7 @@ max7219_t display_init(){ ESP_ERROR_CHECK(max7219_init_desc(&dev, HOST, MAX7219_MAX_CLOCK_SPEED_HZ, DISPLAY_PIN_NUM_CS)); ESP_ERROR_CHECK(max7219_init(&dev)); //0...15 - ESP_ERROR_CHECK(max7219_set_brightness(&dev, 9)); + ESP_ERROR_CHECK(max7219_set_brightness(&dev, 8)); return dev; //display = dev; ESP_LOGI(TAG, "initializing display - done"); @@ -84,15 +84,21 @@ handledDisplay::handledDisplay(max7219_t displayDevice, uint8_t posStart_f) { //function that displays a given string on the display void handledDisplay::showString(const char * buf, uint8_t pos_f){ //calculate actual absolute position - uint8_t pos = posStart + pos_f; - //draw string on display - max7219_draw_text_7seg(&dev, pos, buf); - //disable blinking mode - blinkMode = false; + posCurrent = posStart + pos_f; + //copy the desired string + strcpy(strOn, buf); + //exit blinking mode + if (mode == displayMode::BLINK_STRINGS){ + mode = displayMode::NORMAL; + } + handle(); //draws the text depending on mode } +//TODO: blinkStrings() and blink() are very similar - can be optimized? +//only difficulty currently is the reset behaivor of blinkStrings through showString (blink does not reset) + //---------------------------------- //---------- blinkStrings ---------- //---------------------------------- @@ -103,10 +109,11 @@ void handledDisplay::blinkStrings(const char * strOn_f, const char * strOff_f, u strcpy(strOff, strOff_f); msOn = msOn_f; msOff = msOff_f; - //if changed to blink mode just now - if (blinkMode == false) { + //if changed to blink mode just now: + if (mode != displayMode::BLINK_STRINGS) { + //switch mode ESP_LOGI(TAG, "pos:%i changing to blink mode", posStart); - blinkMode = true; + mode = displayMode::BLINK_STRINGS; //start with on state state = true; timestampOn = esp_log_timestamp(); @@ -117,33 +124,71 @@ void handledDisplay::blinkStrings(const char * strOn_f, const char * strOff_f, u +//------------------------------- +//------------ blink ------------ +//------------------------------- +//function triggers certain count and interval of off durations +void handledDisplay::blink(uint8_t count_f, uint32_t msOn_f, uint32_t msOff_f, const char * strOff_f) { + //set to blink mode + mode = displayMode::BLINK; + //copy parameters + count = count_f; + msOn = msOn_f; + msOff = msOff_f; + strcpy(strOff, strOff_f); + //FIXME this strings length must be dynamic depending on display size (posEnd - posStart) -> otherwise overwrites next segments if other display size or start pos + ESP_LOGI(TAG, "start blinking: count=%i on/off=%d/%d", count, msOn, msOff); + //start with off state + state = false; + timestampOff = esp_log_timestamp(); + //run handle function for display update + handle(); +} + + + //-------------------------------- //------------ handle ------------ //-------------------------------- -//function that handles blinking of display2 +//function that handles time based modes +//writes text to the 7 segment display depending on the current mode void handledDisplay::handle() { - if (blinkMode == false){ - return; //not in blinking mode - nothing todo - } + switch (mode){ + case displayMode::NORMAL: + //daw given string + max7219_draw_text_7seg(&dev, posCurrent, strOn); + break; - //--- define state on/off --- - if (state == true){ //display in ON state - if (esp_log_timestamp() - timestampOn > msOn){ - state = false; - timestampOff = esp_log_timestamp(); - } - } else { //display in OFF state - if (esp_log_timestamp() - timestampOff > msOff) { - state = true; - timestampOn = esp_log_timestamp(); - } - } + case displayMode::BLINK: + case displayMode::BLINK_STRINGS: + //--- define state on/off --- + if (state == true){ //display in ON state + if (esp_log_timestamp() - timestampOn > msOn){ + state = false; + timestampOff = esp_log_timestamp(); + //decrement remaining counts in BLINK mode each cycle + if (mode == displayMode::BLINK) count--; + } + } else { //display in OFF state + if (esp_log_timestamp() - timestampOff > msOff) { + state = true; + timestampOn = esp_log_timestamp(); + } + } + //--- draw text of current state --- + if (state) { + max7219_draw_text_7seg(&dev, posStart, strOn); + } else { + max7219_draw_text_7seg(&dev, posStart, strOff); + } - //--- draw text of current state --- - if (state) { - max7219_draw_text_7seg(&dev, posStart, strOn); - } else { - max7219_draw_text_7seg(&dev, posStart, strOff); + //--- check finished condition in BLINK mode --- + if (mode == displayMode::BLINK){ + if (count == 0) { + mode = displayMode::NORMAL; + ESP_LOGI(TAG, "finished blinking -> normal mode"); + } + } + break; } } - diff --git a/main/display.hpp b/main/display.hpp index b3a916a..d62e684 100644 --- a/main/display.hpp +++ b/main/display.hpp @@ -24,6 +24,7 @@ max7219_t display_init(); //show welcome message on the entire display void display_ShowWelcomeMsg(max7219_t displayDevice); +enum class displayMode {NORMAL, BLINK_STRINGS, BLINK}; class handledDisplay { public: @@ -35,9 +36,13 @@ class handledDisplay { void showString(const char * buf, uint8_t pos = 0); //function switches between two strings in a given interval void blinkStrings(const char * strOn, const char * strOff, uint32_t msOn, uint32_t msOff); + //triggers certain count of blinking between currently shown string and off or optional certain string + void blink(uint8_t count, uint32_t msOn, uint32_t msOff, const char * strOff = " "); + //function that handles time based modes and writes text to display + void handle(); //has to be run regularly when blink method is used + + //TODO: blinkStrings and blink are very similar - optimize? //TODO: add 'scroll string' method - //function that handles blinking of display - void handle(); private: @@ -45,12 +50,14 @@ class handledDisplay { //config max7219_t dev; uint8_t posStart; //absolute position this display instance starts (e.g. multiple or very long 7 segment display) + uint8_t posCurrent; - //blink mode + displayMode mode = displayMode::NORMAL; + //blink modes + uint8_t count = 0; char strOn[20]; char strOff[20]; bool state = false; - bool blinkMode = false; uint32_t msOn; uint32_t msOff; uint32_t timestampOn; From 070fd7069db87eca4f9767a52878411153511d0e Mon Sep 17 00:00:00 2001 From: jonny_ji7 Date: Fri, 26 Aug 2022 11:03:22 +0200 Subject: [PATCH 10/10] Add msg on stop, start-target, Optimize logging --- main/control.cpp | 25 +++++++++++++++++++------ main/display.cpp | 22 +++++++++++++--------- 2 files changed, 32 insertions(+), 15 deletions(-) diff --git a/main/control.cpp b/main/control.cpp index 2557cf3..473f1fd 100644 --- a/main/control.cpp +++ b/main/control.cpp @@ -89,19 +89,25 @@ void changeState (systemState_t stateNew) { //===== handle Stop Condition ===== //function that checks whether start button is released or target is reached (used in multiple states) //returns true when stopped, false when no action -bool handleStopCondition(){ +bool handleStopCondition(handledDisplay * displayTop, handledDisplay * displayBot){ //--- stop conditions --- //stop conditions that are checked in any mode //target reached if (lengthRemaining <= 0 ) { changeState(TARGET_REACHED); vfd_setState(false); + displayTop->blink(1, 0, 1500, " S0LL "); + displayBot->blink(1, 0, 1500, "ERREICHT"); + buzzer.beep(2, 100, 100); return true; } //start button released else if (SW_START.state == false) { changeState(COUNTING); vfd_setState(false); + displayTop->blink(2, 900, 1000, "- STOP -"); + displayBot->blink(2, 900, 1000, " TASTER "); + buzzer.beep(3, 200, 100); return true; } else { return false; @@ -237,7 +243,7 @@ void task_control(void *pvParameter) } if (SW_SET.fallingEdge) { buzzer.beep(2, 70, 50); - displayBot.blink(3, 100, 100, "S0LL "); + displayBot.blink(2, 100, 100, "S0LL "); } @@ -251,12 +257,12 @@ void task_control(void *pvParameter) else if (SW_PRESET2.risingEdge) { lengthTarget = 5000; buzzer.beep(lengthTarget/1000, 25, 30); - displayBot.blink(3, 100, 100, "S0LL "); + displayBot.blink(2, 100, 100, "S0LL "); } else if (SW_PRESET3.risingEdge) { lengthTarget = 10000; buzzer.beep(lengthTarget/1000, 25, 30); - displayBot.blink(3, 100, 100, "S0LL "); + displayBot.blink(2, 100, 100, "S0LL "); } } @@ -272,6 +278,7 @@ void task_control(void *pvParameter) switch (controlState) { case COUNTING: //no motor action vfd_setState(false); + //TODO check stop condition before starting - prevents motor from starting 2 cycles when //--- start winding to length --- if (SW_START.risingEdge) { changeState(WINDING_START); @@ -289,14 +296,14 @@ void task_control(void *pvParameter) if (esp_log_timestamp() - timestamp_motorStarted > 2000) { changeState(WINDING); } - handleStopCondition(); //stops if button released or target reached + handleStopCondition(&displayTop, &displayBot); //stops if button released or target reached //TODO: cancel when there was no cable movement during start time? break; case WINDING: //wind fast, slow down when close //set vfd speed depending on remaining distance setDynSpeedLvl(); //slow down when close to target - handleStopCondition(); //stops if button released or target reached + handleStopCondition(&displayTop, &displayBot); //stops if button released or target reached //TODO: cancel when there is no cable movement anymore e.g. empty / timeout? break; @@ -306,6 +313,12 @@ void task_control(void *pvParameter) if ( lengthRemaining > 0 ) { changeState(COUNTING); } + //show msg when trying to start, but target is reached + if (SW_START.risingEdge){ + buzzer.beep(3, 40, 30); + displayTop.blink(2, 600, 800, " S0LL "); + displayBot.blink(2, 600, 800, "ERREICHT"); + } break; case MANUAL: //manually control motor via preset buttons + poti diff --git a/main/display.cpp b/main/display.cpp index d5b5fa0..cf610eb 100644 --- a/main/display.cpp +++ b/main/display.cpp @@ -90,6 +90,7 @@ void handledDisplay::showString(const char * buf, uint8_t pos_f){ //exit blinking mode if (mode == displayMode::BLINK_STRINGS){ mode = displayMode::NORMAL; + ESP_LOGI(TAG, "pos:%i - disable blink strings mode -> normal mode str='%s'", posStart, strOn); } handle(); //draws the text depending on mode } @@ -112,7 +113,7 @@ void handledDisplay::blinkStrings(const char * strOn_f, const char * strOff_f, u //if changed to blink mode just now: if (mode != displayMode::BLINK_STRINGS) { //switch mode - ESP_LOGI(TAG, "pos:%i changing to blink mode", posStart); + ESP_LOGI(TAG, "pos:%i - toggle blink strings mode on/off=%d/%d stings='%s'/'%s'", posStart, msOn, msOff, strOn, strOff); mode = displayMode::BLINK_STRINGS; //start with on state state = true; @@ -129,18 +130,21 @@ void handledDisplay::blinkStrings(const char * strOn_f, const char * strOff_f, u //------------------------------- //function triggers certain count and interval of off durations void handledDisplay::blink(uint8_t count_f, uint32_t msOn_f, uint32_t msOff_f, const char * strOff_f) { - //set to blink mode - mode = displayMode::BLINK; - //copy parameters + //copy/update parameters count = count_f; msOn = msOn_f; msOff = msOff_f; strcpy(strOff, strOff_f); //FIXME this strings length must be dynamic depending on display size (posEnd - posStart) -> otherwise overwrites next segments if other display size or start pos - ESP_LOGI(TAG, "start blinking: count=%i on/off=%d/%d", count, msOn, msOff); - //start with off state - state = false; - timestampOff = esp_log_timestamp(); + //if changed to blink mode just now: + if (mode != displayMode::BLINK) { + //set to blink mode + mode = displayMode::BLINK; + ESP_LOGI(TAG, "pos:%i - start blinking: count=%i on/off=%d/%d sting='%s'",posStart, count, msOn, msOff, strOff); + //start with off state + state = false; + timestampOff = esp_log_timestamp(); + } //run handle function for display update handle(); } @@ -186,7 +190,7 @@ void handledDisplay::handle() { if (mode == displayMode::BLINK){ if (count == 0) { mode = displayMode::NORMAL; - ESP_LOGI(TAG, "finished blinking -> normal mode"); + ESP_LOGI(TAG, "pos:%i - finished blinking -> normal mode", posStart); } } break;