From 353c58bfcb91c0efcb6e4bd0f3b582b25c9941e3 Mon Sep 17 00:00:00 2001 From: Anton Mukhin Date: Sat, 21 Jan 2023 19:35:12 +0300 Subject: [PATCH] Tidy up the code --- firmware/.settings/language.settings.xml | 4 +- firmware/Core/Src/main.c | 66 ++++++++++-------------- 2 files changed, 29 insertions(+), 41 deletions(-) diff --git a/firmware/.settings/language.settings.xml b/firmware/.settings/language.settings.xml index ba20e13..8a92f6c 100644 --- a/firmware/.settings/language.settings.xml +++ b/firmware/.settings/language.settings.xml @@ -5,7 +5,7 @@ - + @@ -16,7 +16,7 @@ - + diff --git a/firmware/Core/Src/main.c b/firmware/Core/Src/main.c index 6709d87..2f79dd4 100644 --- a/firmware/Core/Src/main.c +++ b/firmware/Core/Src/main.c @@ -95,13 +95,10 @@ uint8_t TOF_Ready = 0; // Flag to ignore TOF IRQ before it is initialized uint8_t curLightLevel = 0; // Current light level (0-99) uint16_t curDist = 0; // Current measured distance in mm uint16_t btn_ticks; // How many ticks the button is pressed (1 tick = 100ms) -//uint8_t needConfig = 1; // Need to configure the device. Timers won't "react" to events -//uint8_t startConfig = 0; // Flag to start configuration sequence int8_t dLevel = 1; // Direction of fade: -1 for fade out; 1 for fade in; -//uint8_t manualOn = 0; // Flag to indicate a manual on DLMode_t DLmode = DL_normal; // Global mode: DL_normal, DL_manual_on, DL_manual_off, DL_need_config, DL_config_waiting, DL_config_accepted, DL_error -DLMode_t prev_mode = DL_normal; -uint8_t modeSwitch = 0; // Flag to switch the mode after the button tap +DLMode_t prev_mode = DL_normal; // To hold the mode during temporary mode switches +uint8_t modeSwitch = 0; // Flag to switch the mode after the button tap /* USER CODE END PV */ @@ -143,7 +140,6 @@ volatile void loadConfig(void) { if (HAL_CRC_Calculate(&hcrc, configuration.data32, 2) != configuration.sector.checksum || configuration.config.token != CONF_TOKEN) { //First start or configuration is corrupted - saveConfig(); //Save dafault config } // else successfully read the configuration @@ -157,9 +153,9 @@ volatile void saveConfig(void) { uint32_t l_Error = 0; //We need it to erase a page - EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; - EraseInitStruct.Page = CONF_FLASH_PAGE; - EraseInitStruct.NbPages = 1; + EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES; + EraseInitStruct.Page = CONF_FLASH_PAGE; + EraseInitStruct.NbPages = 1; if (configuration.config.token != CONF_TOKEN) { //first start @@ -213,15 +209,12 @@ void fastBlink(uint8_t count) { // Range configuration procedure (set on and off ranges) void configure(void) { - //startConfig = 0; DLmode = DL_config_waiting; //Fast blink 5 times - //needConfig = 1; fastBlink(5); //Configure ON distance - //needConfig = 1; // in case the button was touched during fast blinks while (DLmode == DL_config_waiting) { HAL_Delay(250); if (curDist > MAX_DIST - MIN_DIST_GAP) setLightLevel(0); @@ -234,12 +227,10 @@ void configure(void) { configuration.config.dist_on = curDist; // this will be checked during config save //Fast blink 2 times - //needConfig = 1; DLmode = DL_config_waiting; fastBlink(2); //Configure OFF distance - //needConfig = 1; // in case the button was touched during fast blinks while (DLmode == DL_config_waiting) { HAL_Delay(250); if (curDist > MAX_DIST) setLightLevel(0); @@ -250,10 +241,7 @@ void configure(void) { saveConfig(); //Fast blink 5 times - //needConfig = 1; fastBlink(5); - - //needConfig = 0; } /* USER CODE END 0 */ @@ -333,29 +321,30 @@ int main(void) DLmode = prev_mode; } + // Check if the button was tapped and we should switch the mode if (modeSwitch) { modeSwitch = 0; switch (DLmode) { - case DL_normal: - DLmode = DL_manual_on; - fastBlink(1); - dLevel = 2; - HAL_TIM_Base_Start_IT(&htim17); - break; - case DL_manual_on: - DLmode = DL_manual_off; - fastBlink(1); - dLevel = -1; - HAL_TIM_Base_Start_IT(&htim17); - break; - case DL_manual_off: - DLmode = DL_normal; - fastBlink(2); - break; - default: - break; + case DL_normal: + DLmode = DL_manual_on; + fastBlink(1); + dLevel = 2; + HAL_TIM_Base_Start_IT(&htim17); + break; + case DL_manual_on: + DLmode = DL_manual_off; + fastBlink(1); + dLevel = -1; + HAL_TIM_Base_Start_IT(&htim17); + break; + case DL_manual_off: + DLmode = DL_normal; + fastBlink(2); + break; + default: + break; + } } - } HAL_Delay(50); /* USER CODE END WHILE */ @@ -704,7 +693,6 @@ void HAL_GPIO_EXTI_Falling_Callback(uint16_t GPIO_Pin) { if (btn_ticks < 60) { // Button was not held for more than 6 seconds if (DLmode == DL_config_waiting) { - //needConfig = 0; DLmode = DL_config_accepted; } else { modeSwitch = 1; @@ -717,9 +705,9 @@ void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { if(htim->Instance == TIM16) { //check if the interrupt comes from TIM16 // TIM16 - ticks timer (10Hz - 100ms) if (btn_ticks < 60) { btn_ticks++; - } else { // the button is held for more than 6 seconds + } else { + // the button is held for more than 6 seconds HAL_TIM_Base_Stop_IT(&htim16); - //startConfig = 1; prev_mode = DLmode; DLmode = DL_need_config; }