Implement DMX reciever and add more advanced failure mode #1

Merged
mhu merged 7 commits from dmx_receiver into master 2022-07-15 19:49:55 +02:00
Showing only changes of commit be3efc07d3 - Show all commits

View File

@ -26,6 +26,7 @@ volatile int32_t temperature;
extern void sk6812_send_led(uint32_t rgbw);
volatile uint32_t wait_tick = 0;
volatile bool blink_tick = false;
static void wait_for_ticks(uint32_t ticks)
{
@ -43,8 +44,13 @@ int main(void)
bool button_pressed = false;
bool force_led_update;
bool overtemp_flag = false;
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->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 */
if (temperature > ((MAX_TEMP_CELSIUS) * 10)) {
if (led_val > 20)
if (overtemp_flag) {
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--;
}
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;
}
@ -181,7 +201,7 @@ int main(void)
break;
case RING_MODE_WAIT_DMX:
force_led_update = false;
if (dmx_enough_data_received()) {
if (dmx_enough_data_received() && !overtemp_flag) {
dmx_data = dmx_get_data();
mode = RING_MODE_WAIT_DMX_BREAK;
if (dmx_data[0] != 0)
@ -194,6 +214,12 @@ int main(void)
}
led_pwm_val = dmx_data[129];
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;
case RING_MODE_WAIT_DMX_BREAK:
@ -206,6 +232,11 @@ int main(void)
break;
}
if (overtemp_flag) {
force_led_update = true;
led_calc_val[0] = blink_tick ? 0x00FF0000UL : 0UL;
}
if (force_led_update) {
TIM14->CCR1 = led_pwm_val;
@ -221,7 +252,7 @@ int main(void)
}
/* 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);
if((int16_t)TIM3->CNT > (int16_t)led_val) {
@ -239,14 +270,30 @@ int main(void)
if(GPIOA->IDR & GPIO_IDR_0) {
button_pressed = false;
}
}
else if(!(GPIOA->IDR & GPIO_IDR_0)) {
mode = (mode + 1) % RING_MODE_MAX;
} else if(!(GPIOA->IDR & GPIO_IDR_0)) {
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++;
}