From f8735232e26db82858e0801a6f8a82f28ee65d0c Mon Sep 17 00:00:00 2001 From: jaseg Date: Tue, 24 Mar 2020 00:10:38 +0100 Subject: works --- fw/.gitignore | 13 ++++++++ fw/main.c | 104 +++++++++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 112 insertions(+), 5 deletions(-) create mode 100644 fw/.gitignore diff --git a/fw/.gitignore b/fw/.gitignore new file mode 100644 index 0000000..d2d5078 --- /dev/null +++ b/fw/.gitignore @@ -0,0 +1,13 @@ +*.elf +*.o +*.expand +*.hex +*.lst +*.map +*.bin +*.pp +sources.c +sources.tar.xz +sources.tar.xz.zip +8b10b_test_decode +8b10b_test_encode diff --git a/fw/main.c b/fw/main.c index c60dff0..0d08576 100644 --- a/fw/main.c +++ b/fw/main.c @@ -39,6 +39,7 @@ volatile bool backchannel_trigger_flag = 0; void run_menu(void); void update_display(void); void calc_next_alarm(void); +void update_servo(void); uint8_t random() { static uint8_t x, a, b, c; @@ -141,7 +142,7 @@ int main(void) { | (2<AFR[0] = - (1<AFR[1] = (4<PSC = 48-1; /* 1us ticks */ TIM1->ARR = 1000-1; /* 1ms interval */ TIM1->EGR = TIM_EGR_UG; - TIM1->CCMR1 = (6<CCER = TIM_CCER_CC1E; TIM1->DIER = TIM_DIER_UIE; TIM3->CR1 = TIM_CR1_CEN; TIM3->PSC = 48-1; /* 1us ticks */ TIM3->ARR = 20000-1; /* 20ms interval */ + TIM3->CCMR1 = (6<CCER = TIM_CCER_CC1E; TIM3->EGR = TIM_EGR_UG; NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn); @@ -190,15 +193,39 @@ int main(void) { } } +enum { + LVL_SERVO_OFF, + LVL_SERVO_IDLE, + LVL_SERVO_ON, + NUM_SERVO_LEVELS +}; + +int servo_lvl_set[NUM_SERVO_LEVELS] = { + [LVL_SERVO_ON] = 1050, + [LVL_SERVO_IDLE] = 1220, + [LVL_SERVO_OFF] = 1390, +}; + +const char *servo_lvl_string[NUM_SERVO_LEVELS] = { + [LVL_SERVO_OFF] = "OFF", + [LVL_SERVO_IDLE] = "IDLE", + [LVL_SERVO_ON] = "ON", +}; + +const int servo_lvl_set_ms = 1000; +int servo_lvl_ms = 0; + enum { MENU_DEFAULT, MENU_SET_TIME, MENU_SET_ALARM, + MENU_SET_SERVO_LVL, } menu_state; int menu_adjustment_done = 0; int sel_field_idx = 0; int menu_alarm = 0; +int menu_servo_lvl = 0; enum { INPUT_LEFT, @@ -228,6 +255,15 @@ void RTC_IRQHandler() { /* TODO FIXME handle alarm */ } +void update_servo() { + if (next_alarm == ALR_TON) { + TIM3->CCR1 = servo_lvl_set[LVL_SERVO_ON]; + } else { + TIM3->CCR1 = servo_lvl_set[LVL_SERVO_OFF]; + } + servo_lvl_ms = servo_lvl_set_ms; +} + void adj_alarm(int alarm, int sel, int adj) { int fields[3] = { alarms[alarm]>>16, (alarms[alarm]>>8) & 0xff, alarms[alarm] & 0xff }; int max[3] = { 24, 60, 60 }; @@ -278,6 +314,8 @@ void calc_next_alarm() { | (s/10 << RTC_ALRMAR_ST_Pos) | (s%10 << RTC_ALRMAR_SU_Pos); RTC->ISR &= ~RTC_ISR_ALRAF; RTC->CR |= RTC_CR_ALRAE; + + update_servo(); } void update_display() { @@ -331,6 +369,13 @@ void update_display() { mini_snprintf(buf, sizeof(buf), "SET %s" LCD_FILL, alarm_names[menu_alarm]); lcd_write_str(0, 1, buf); break; + + case MENU_SET_SERVO_LVL: + mini_snprintf(buf, sizeof(buf), "%04d" LCD_FILL, servo_lvl_set[menu_servo_lvl]); + lcd_write_str(0, 0, buf); + mini_snprintf(buf, sizeof(buf), "SET %s POS" LCD_FILL, servo_lvl_string[menu_servo_lvl]); + lcd_write_str(0, 1, buf); + break; } } @@ -394,8 +439,10 @@ void run_menu() { } else { sel_field_idx = 2; menu_alarm++; - if (menu_alarm == NUM_ALARMS) - menu_state = MENU_DEFAULT; + if (menu_alarm == NUM_ALARMS) { + menu_state = MENU_SET_SERVO_LVL; + menu_servo_lvl = 0; + } } break; } @@ -422,6 +469,45 @@ void run_menu() { adj_alarm(menu_alarm, sel_field_idx, -1); } break; + + case MENU_SET_SERVO_LVL: + if (btn & (1<CCR1 = servo_lvl_set[menu_servo_lvl]; + } + if (btn & (1<CCR1 = servo_lvl_set[menu_servo_lvl]; + } + if (servo_lvl_set[menu_servo_lvl] < 500) + servo_lvl_set[menu_servo_lvl] = 500; + if (servo_lvl_set[menu_servo_lvl] > 2500) + servo_lvl_set[menu_servo_lvl] = 2500; + break; } } @@ -491,6 +577,14 @@ void SysTick_Handler(void) { sys_time_s++; sys_flag_1Hz = 1; } + + if (servo_lvl_ms > 0) { + servo_lvl_ms -= TICK_MS; + if (servo_lvl_ms <= 0) { + servo_lvl_ms = 0; + TIM3->CCR1 = servo_lvl_set[LVL_SERVO_IDLE]; + } + } } void _init(void) { -- cgit