Added CO2 Level LED indication.

This commit is contained in:
David Žaitlík 2021-09-30 17:43:29 +02:00
parent 80a50d57cf
commit 5244fe768f
2 changed files with 47 additions and 22 deletions

View File

@ -49,8 +49,8 @@ config_read(&config);
* Device configuration data can be accessed using config_t struct. * Device configuration data can be accessed using config_t struct.
*/ */
#define CONFIG_DEFAULT_LED_ON 1 #define CONFIG_DEFAULT_LED_ON 1
#define CONFIG_DEFAULT_LED_ALERT1_LIMIT 1000 #define CONFIG_DEFAULT_LED_ALERT1_LIMIT 1500
#define CONFIG_DEFAULT_LED_ALERT2_LIMIT 2000 #define CONFIG_DEFAULT_LED_ALERT2_LIMIT 3000
#define MODBUS_ADDR_LENGTH 2 #define MODBUS_ADDR_LENGTH 2
#define CONFIG_LED_ON_LENGTH 2 #define CONFIG_LED_ON_LENGTH 2

View File

@ -267,22 +267,15 @@ int main(void)
MX_LPUART1_UART_Init(); MX_LPUART1_UART_Init();
MX_TIM21_Init(); MX_TIM21_Init();
/* USER CODE BEGIN 2 */ /* USER CODE BEGIN 2 */
/* Turn on MAGENTA LED to signal startup state */
LL_GPIO_ResetOutputPin(LED_R_GPIO_Port, LED_R_Pin);
LL_GPIO_SetOutputPin(LED_G_GPIO_Port, LED_G_Pin);
LL_GPIO_ResetOutputPin(LED_B_GPIO_Port, LED_B_Pin);
/* Enable I2C for sensors */ /* Enable I2C for sensors */
LL_I2C_Enable(I2C1); LL_I2C_Enable(I2C1);
/* Enable UART for RS485 */
LL_LPUART_Enable(LPUART1);
/* Start the timer for measurement triggering */
LL_TIM_EnableCounter(TIM21);
LL_TIM_EnableIT_UPDATE(TIM21);
/*LL_TIM_GenerateEvent_UPDATE(TIM2);*/
/* I2C context init (for SHT4x and SCD4x) */
i2c_context_t i2c_context;
i2c_context.i2c = I2C1;
i2c_init(&i2c_context);
/* Read config from EEPROM - if unsuccessful, set the default values*/ /* Read config from EEPROM - if unsuccessful, set the default values*/
int8_t config_read_status = config_read(&sensor_config); int8_t config_read_status = config_read(&sensor_config);
if (config_read_status != CONFIG_OK) if (config_read_status != CONFIG_OK)
@ -292,8 +285,22 @@ int main(void)
sensor_config.led_co2_alert_limit2 = CONFIG_DEFAULT_LED_ALERT2_LIMIT; sensor_config.led_co2_alert_limit2 = CONFIG_DEFAULT_LED_ALERT2_LIMIT;
sensor_config.led_on = CONFIG_DEFAULT_LED_ON; sensor_config.led_on = CONFIG_DEFAULT_LED_ON;
} }
/* Set the modbus address */
modbus_slave_set_address(sensor_config.modbus_addr); modbus_slave_set_address(sensor_config.modbus_addr);
/* Enable UART for RS485 */
LL_LPUART_Enable(LPUART1);
/* Start the timer for measurement triggering */
LL_TIM_EnableCounter(TIM21);
LL_TIM_EnableIT_UPDATE(TIM21);
/* I2C context init (for SHT4x and SCD4x) */
i2c_context_t i2c_context;
i2c_context.i2c = I2C1;
i2c_init(&i2c_context);
scd4x_start_periodic_measurement(); scd4x_start_periodic_measurement();
uint8_t scd4x_is_connected = 1; uint8_t scd4x_is_connected = 1;
uint8_t sps30_is_connected = 0; uint8_t sps30_is_connected = 0;
@ -321,8 +328,6 @@ int main(void)
/* SPS30 Init Time: max 30000 ms (datasheet pg. 2) */ /* SPS30 Init Time: max 30000 ms (datasheet pg. 2) */
LL_mDelay(1000); LL_mDelay(1000);
/* Turn on LED to signal ready state */
LL_GPIO_ResetOutputPin(LED_B_GPIO_Port, LED_B_Pin);
/* Enter the main loop */ /* Enter the main loop */
while (1) while (1)
@ -347,12 +352,32 @@ int main(void)
{ {
sps30_read_measured_values(sps30_measured_data, 10); sps30_read_measured_values(sps30_measured_data, 10);
} }
/* Toggle LED for now */
/* TODO: Remove LED Toggle */
LL_GPIO_TogglePin(LED_B_GPIO_Port, LED_B_Pin);
/* TODO: Process data and light a desired color of LED */ /* TODO: Process data and light a desired color of LED */
/* TODO: Add hystheresis */
if (CO2 <= sensor_config.led_co2_alert_limit1)
{
/* CO2 is OK -> BLUE */
/* TODO: Decide if blue or green */
LL_GPIO_SetOutputPin(LED_R_GPIO_Port, LED_R_Pin);
LL_GPIO_SetOutputPin(LED_G_GPIO_Port, LED_G_Pin);
LL_GPIO_ResetOutputPin(LED_B_GPIO_Port, LED_B_Pin);
} else if ((sensor_config.led_co2_alert_limit1 < CO2) && (CO2 <= sensor_config.led_co2_alert_limit2))
{
/* CO2 is NOT OK -> YELLOW */
LL_GPIO_ResetOutputPin(LED_R_GPIO_Port, LED_R_Pin);
LL_GPIO_ResetOutputPin(LED_G_GPIO_Port, LED_G_Pin);
LL_GPIO_SetOutputPin(LED_B_GPIO_Port, LED_B_Pin);
} else if (sensor_config.led_co2_alert_limit2 < CO2)
{
/* CO2 is CRITICAL -> RED */
LL_GPIO_ResetOutputPin(LED_R_GPIO_Port, LED_R_Pin);
LL_GPIO_SetOutputPin(LED_G_GPIO_Port, LED_G_Pin);
LL_GPIO_SetOutputPin(LED_B_GPIO_Port, LED_B_Pin);
}
/* Reset the TIM21 Elapsed Period Flag */ /* Reset the TIM21 Elapsed Period Flag */
tim21_elapsed_period = 0; tim21_elapsed_period = 0;