Improve implementation. Add blinking red led to overtemperature mode

This commit is contained in:
Mario Hüttel 2022-07-15 19:48:55 +02:00
parent 8dc30f15fb
commit be3efc07d3

View File

@ -26,6 +26,7 @@ volatile int32_t temperature;
extern void sk6812_send_led(uint32_t rgbw); extern void sk6812_send_led(uint32_t rgbw);
volatile uint32_t wait_tick = 0; volatile uint32_t wait_tick = 0;
volatile bool blink_tick = false;
static void wait_for_ticks(uint32_t ticks) static void wait_for_ticks(uint32_t ticks)
{ {
@ -43,8 +44,13 @@ int main(void)
bool button_pressed = false; bool button_pressed = false;
bool force_led_update; bool force_led_update;
bool overtemp_flag = false;
enum ring_modes mode; enum ring_modes mode;
/* Led value / mode before going to DMX */
uint32_t led_val_before_dmx = 0u;
enum ring_modes mode_before_dmx = RING_MODE_RED; /* Init to save value */
RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN; RCC->AHBENR |= RCC_AHBENR_GPIOAEN | RCC_AHBENR_GPIOBEN;
RCC->APB1ENR |= RCC_APB1ENR_TIM3EN | RCC_APB1ENR_TIM14EN; RCC->APB1ENR |= RCC_APB1ENR_TIM3EN | RCC_APB1ENR_TIM14EN;
@ -101,12 +107,26 @@ int main(void)
} }
/*! -# Gradually dim down the LED brightness in case the temperature is too high */ /*! -# Gradually dim down the LED brightness in case the temperature is too high */
if (temperature > ((MAX_TEMP_CELSIUS) * 10)) { if (overtemp_flag) {
if (led_val > 20) if (temperature < (MAX_TEMP_CELSIUS-15) * 10) {
overtemp_flag = false;
}
} else {
overtemp_flag = temperature > ((MAX_TEMP_CELSIUS) * 10) ? true : false;
}
if (overtemp_flag) {
if (led_val > 2 && mode < RING_MODE_MAX)
led_val--; led_val--;
} }
if (dmx_poll_break_received()) { if (dmx_poll_break_received()) {
/* DMX received. Go to DMX mode.
* Save old state
*/
if (mode < RING_MODE_MAX) {
led_val_before_dmx = led_val;
mode_before_dmx = mode;
}
mode = RING_MODE_WAIT_DMX; mode = RING_MODE_WAIT_DMX;
} }
@ -181,7 +201,7 @@ int main(void)
break; break;
case RING_MODE_WAIT_DMX: case RING_MODE_WAIT_DMX:
force_led_update = false; force_led_update = false;
if (dmx_enough_data_received()) { if (dmx_enough_data_received() && !overtemp_flag) {
dmx_data = dmx_get_data(); dmx_data = dmx_get_data();
mode = RING_MODE_WAIT_DMX_BREAK; mode = RING_MODE_WAIT_DMX_BREAK;
if (dmx_data[0] != 0) if (dmx_data[0] != 0)
@ -194,6 +214,12 @@ int main(void)
} }
led_pwm_val = dmx_data[129]; led_pwm_val = dmx_data[129];
force_led_update = true; force_led_update = true;
} else if (overtemp_flag) {
force_led_update = true;
for (int i = 0; i < RING_MAX_LED; i++) {
led_calc_val[i] = 0ul;
}
led_pwm_val = 0;
} }
break; break;
case RING_MODE_WAIT_DMX_BREAK: case RING_MODE_WAIT_DMX_BREAK:
@ -206,6 +232,11 @@ int main(void)
break; break;
} }
if (overtemp_flag) {
force_led_update = true;
led_calc_val[0] = blink_tick ? 0x00FF0000UL : 0UL;
}
if (force_led_update) { if (force_led_update) {
TIM14->CCR1 = led_pwm_val; TIM14->CCR1 = led_pwm_val;
@ -221,7 +252,7 @@ int main(void)
} }
/* Only wait in case of non-DMX mode */ /* Only wait in case of non-DMX mode */
if (!(mode == RING_MODE_WAIT_DMX_BREAK || mode == RING_MODE_WAIT_DMX)) if (!(mode == RING_MODE_WAIT_DMX_BREAK || mode == RING_MODE_WAIT_DMX) || overtemp_flag)
wait_for_ticks(5); wait_for_ticks(5);
if((int16_t)TIM3->CNT > (int16_t)led_val) { if((int16_t)TIM3->CNT > (int16_t)led_val) {
@ -239,14 +270,30 @@ int main(void)
if(GPIOA->IDR & GPIO_IDR_0) { if(GPIOA->IDR & GPIO_IDR_0) {
button_pressed = false; button_pressed = false;
} }
} } else if(!(GPIOA->IDR & GPIO_IDR_0)) {
else if(!(GPIOA->IDR & GPIO_IDR_0)) {
mode = (mode + 1) % RING_MODE_MAX;
button_pressed = true; button_pressed = true;
/* Button pressed */
if (mode > RING_MODE_MAX) {
/* In DMX mode. Abort DMX mode */
mode = mode_before_dmx;
led_val = led_val_before_dmx;
} else {
/* Normal mode switching */
mode = (mode + 1) % RING_MODE_MAX;
}
} }
} }
} }
void SysTick_Handler(void) { void SysTick_Handler(void)
{
static uint32_t tick = 10;
if (!--tick) {
tick = 10;
blink_tick = !blink_tick;
}
wait_tick++; wait_tick++;
} }