From 53ba4faa70847d5c7c836302d42e85f47c12d914 Mon Sep 17 00:00:00 2001 From: Robin Dietzel Date: Thu, 26 Jan 2023 15:44:43 +0100 Subject: [PATCH] FEATURE: history tracking --- Core/Inc/button_input.h | 10 ------ Core/Inc/git_rev.h | 2 +- Core/Inc/stm32f4xx_it.h | 1 + Core/Src/button_input.c | 30 ------------------ Core/Src/custom_callbacks.c | 41 +++++++++++++++++++++++++ Core/Src/main.cpp | 59 ++++++++++++++++++++++++++++++++++-- Core/Src/stm32f4xx_hal_msp.c | 10 ++++++ Core/Src/stm32f4xx_it.c | 6 ++++ 8 files changed, 116 insertions(+), 43 deletions(-) delete mode 100644 Core/Inc/button_input.h delete mode 100644 Core/Src/button_input.c create mode 100644 Core/Src/custom_callbacks.c diff --git a/Core/Inc/button_input.h b/Core/Inc/button_input.h deleted file mode 100644 index f61fd01..0000000 --- a/Core/Inc/button_input.h +++ /dev/null @@ -1,10 +0,0 @@ -// -// Created by robtor on 16.12.22. -// - -#ifndef FLOATPUMP_BUTTON_INPUT_H -#define FLOATPUMP_BUTTON_INPUT_H - -#include - -#endif //FLOATPUMP_BUTTON_INPUT_H diff --git a/Core/Inc/git_rev.h b/Core/Inc/git_rev.h index 9885801..432616a 100644 --- a/Core/Inc/git_rev.h +++ b/Core/Inc/git_rev.h @@ -7,6 +7,6 @@ // Auto generated header file containing the last git revision -#define GIT_HASH "af8a022" +#define GIT_HASH "8f1aed9" #endif //FLOATPUMP_GIT_REVISION_TEMPLATE_H \ No newline at end of file diff --git a/Core/Inc/stm32f4xx_it.h b/Core/Inc/stm32f4xx_it.h index be5a830..6a9ff70 100644 --- a/Core/Inc/stm32f4xx_it.h +++ b/Core/Inc/stm32f4xx_it.h @@ -56,6 +56,7 @@ void DebugMon_Handler(void); void PendSV_Handler(void); void SysTick_Handler(void); void TIM2_IRQHandler(void); +void TIM3_IRQHandler(void); void OTG_FS_IRQHandler(void); /* USER CODE BEGIN EFP */ diff --git a/Core/Src/button_input.c b/Core/Src/button_input.c deleted file mode 100644 index e345edd..0000000 --- a/Core/Src/button_input.c +++ /dev/null @@ -1,30 +0,0 @@ -// -// Created by robtor on 16.12.22. -// -#include "main.h" -#include "button_input.h" - -bool rot_button = false; -uint16_t rot_counter = 0; - -void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { - static uint16_t rot_state = 0; - static uint16_t btn_state = 0; - - rot_state = (rot_state << 1) | (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000; - - btn_state = (btn_state << 1) | (HAL_GPIO_ReadPin(RRSW_GPIO_Port, RRSW_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000; - - if (rot_state == 0xf000) { - rot_state = 0x0000; - if (HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_SET) { - rot_counter++; - } else { - rot_counter--; - } - } - - if (btn_state == 0xf000) { - rot_button = true; - } -} \ No newline at end of file diff --git a/Core/Src/custom_callbacks.c b/Core/Src/custom_callbacks.c new file mode 100644 index 0000000..307d2a0 --- /dev/null +++ b/Core/Src/custom_callbacks.c @@ -0,0 +1,41 @@ +// +// Created by robtor on 16.12.22. +// +#include "main.h" + +extern TIM_HandleTypeDef htim2; +extern TIM_HandleTypeDef htim3; + +bool rot_button = false; +uint16_t rot_counter = 0; +uint16_t minute_counter = 0; + +void HAL_TIM_PeriodElapsedCallback(TIM_HandleTypeDef *htim) { + static uint16_t rot_state = 0; + static uint16_t btn_state = 0; + + if(htim == &htim2) { + + rot_state = + (rot_state << 1) | (HAL_GPIO_ReadPin(RRCLK_GPIO_Port, RRCLK_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000; + + btn_state = + (btn_state << 1) | (HAL_GPIO_ReadPin(RRSW_GPIO_Port, RRSW_Pin) == GPIO_PIN_SET ? 0x1 : 0x0) | 0xe000; + + if (rot_state == 0xf000) { + rot_state = 0x0000; + if (HAL_GPIO_ReadPin(RRDT_GPIO_Port, RRDT_Pin) == GPIO_PIN_SET) { + rot_counter++; + } else { + rot_counter--; + } + } + + if (btn_state == 0xf000) { + rot_button = true; + } + + } else if (htim == &htim3) { + minute_counter++; + } +} \ No newline at end of file diff --git a/Core/Src/main.cpp b/Core/Src/main.cpp index 45db930..4d0e409 100644 --- a/Core/Src/main.cpp +++ b/Core/Src/main.cpp @@ -4,7 +4,6 @@ #include "LCD_I2C_Driver.h" #include "InitSequence.h" -#include "button_input.h" #include "Config_Store.h" #include "Menu_Controller.h" #include "PressureChannel.h" @@ -18,6 +17,7 @@ ADC_HandleTypeDef hadc1; I2C_HandleTypeDef hi2c1; RTC_HandleTypeDef hrtc; TIM_HandleTypeDef htim2; +TIM_HandleTypeDef htim3; UART_HandleTypeDef huart1; @@ -33,10 +33,13 @@ static void MX_I2C1_Init(void); static void MX_TIM2_Init(void); +static void MX_TIM3_Init(void); + static void MX_USART1_UART_Init(void); -extern int16_t rot_counter; +extern uint16_t rot_counter; extern bool rot_button; +extern uint16_t minute_counter; using namespace floatpump; @@ -124,6 +127,7 @@ int main(void) { MX_GPIO_Init(); //MX_RTC_Init(); MX_TIM2_Init(); + MX_TIM3_Init(); MX_USB_DEVICE_Init(); MX_ADC1_Init(); MX_I2C1_Init(); @@ -195,6 +199,12 @@ int main(void) { Menu_Controller controller(&mainmenu, display); static int old_pos = 0; + static int8_t history_h[59]; + static int8_t history_d[23]; + + static uint16_t old_minute_counter = minute_counter; + + while (1) { display.LCDSetBacklight(S_backlight); @@ -323,6 +333,13 @@ int main(void) { CheckTankConditions(Config_Store::getInstance(), tankLevel0, tankPump); CheckRefillConditions(Config_Store::getInstance(), tankLevel0, refillBlocker0, refillPump); + //Create history + if(old_minute_counter < minute_counter) { + old_minute_counter = minute_counter; + history_h[minute_counter%60] = tankLevel0.getPercent(); + history_d[minute_counter/60] = tankLevel0.getPercent(); + } + HAL_GPIO_WritePin(LED2_GPIO_Port, LED2_Pin, GPIO_PIN_SET); HAL_Delay(1000); } @@ -534,6 +551,44 @@ static void MX_TIM2_Init(void) { } +static void MX_TIM3_Init(void) { + //Timer has input of 24MHZ + //Scale down with prescaler to 10kHz + //Auto reload each ms + + /* USER CODE BEGIN TIM2_Init 0 */ + + /* USER CODE END TIM2_Init 0 */ + + TIM_ClockConfigTypeDef sClockSourceConfig = {0}; + + /* USER CODE BEGIN TIM2_Init 1 */ + + /* USER CODE END TIM2_Init 1 */ + htim3.Instance = TIM3; + htim3.Init.Prescaler = 24000; + htim3.Init.CounterMode = TIM_COUNTERMODE_UP; + htim3.Init.Period = 59000; + htim3.Init.ClockDivision = TIM_CLOCKDIVISION_DIV1; + htim3.Init.AutoReloadPreload = TIM_AUTORELOAD_PRELOAD_ENABLE; + if (HAL_TIM_Base_Init(&htim3) != HAL_OK) { + Error_Handler(); + + } + sClockSourceConfig.ClockSource = TIM_CLOCKSOURCE_INTERNAL; + if (HAL_TIM_ConfigClockSource(&htim3, &sClockSourceConfig) != HAL_OK) { + Error_Handler(); + } + + //HAL_TIM_Base_Start(&htim2); + //Directly start timer base generation in interrupt mode + HAL_TIM_Base_Start_IT(&htim3); + /* USER CODE BEGIN TIM2_Init 2 */ + + /* USER CODE END TIM2_Init 2 */ + +} + /** * @brief USART1 Initialization Function * @param None diff --git a/Core/Src/stm32f4xx_hal_msp.c b/Core/Src/stm32f4xx_hal_msp.c index 7b3d324..359d26a 100644 --- a/Core/Src/stm32f4xx_hal_msp.c +++ b/Core/Src/stm32f4xx_hal_msp.c @@ -276,9 +276,13 @@ void HAL_TIM_Base_MspInit(TIM_HandleTypeDef* htim_base) /* USER CODE END TIM2_MspInit 0 */ /* Peripheral clock enable */ __HAL_RCC_TIM2_CLK_ENABLE(); + __HAL_RCC_TIM3_CLK_ENABLE(); /* TIM2 interrupt Init */ HAL_NVIC_SetPriority(TIM2_IRQn, 0, 0); HAL_NVIC_EnableIRQ(TIM2_IRQn); + + HAL_NVIC_SetPriority(TIM3_IRQn, 0, 0); + HAL_NVIC_EnableIRQ(TIM3_IRQn); /* USER CODE BEGIN TIM2_MspInit 1 */ /* USER CODE END TIM2_MspInit 1 */ @@ -309,6 +313,12 @@ void HAL_TIM_Base_MspDeInit(TIM_HandleTypeDef* htim_base) /* USER CODE END TIM2_MspDeInit 1 */ } + if(htim_base->Instance==TIM3) + { + __HAL_RCC_TIM3_CLK_DISABLE(); + HAL_NVIC_DisableIRQ(TIM3_IRQn); + } + } /** diff --git a/Core/Src/stm32f4xx_it.c b/Core/Src/stm32f4xx_it.c index cf96daa..e41d721 100644 --- a/Core/Src/stm32f4xx_it.c +++ b/Core/Src/stm32f4xx_it.c @@ -57,6 +57,7 @@ /* External variables --------------------------------------------------------*/ extern PCD_HandleTypeDef hpcd_USB_OTG_FS; extern TIM_HandleTypeDef htim2; +extern TIM_HandleTypeDef htim3; /* USER CODE BEGIN EV */ /* USER CODE END EV */ @@ -214,6 +215,11 @@ void TIM2_IRQHandler(void) /* USER CODE END TIM2_IRQn 1 */ } +void TIM3_IRQHandler(void) +{ + HAL_TIM_IRQHandler(&htim3); +} + /** * @brief This function handles USB On The Go FS global interrupt. */