diff options
author | jaseg <git@jaseg.net> | 2020-03-24 00:10:38 +0100 |
---|---|---|
committer | jaseg <git@jaseg.net> | 2020-03-24 00:10:38 +0100 |
commit | f8735232e26db82858e0801a6f8a82f28ee65d0c (patch) | |
tree | d39798aef21d1961e86374a4ced586a1d9bf68b4 /fw | |
parent | 6ea56ed6ceacd990e0255d592032af4bd84f7cd6 (diff) | |
download | lampomatic-f8735232e26db82858e0801a6f8a82f28ee65d0c.tar.gz lampomatic-f8735232e26db82858e0801a6f8a82f28ee65d0c.tar.bz2 lampomatic-f8735232e26db82858e0801a6f8a82f28ee65d0c.zip |
works
Diffstat (limited to 'fw')
-rw-r--r-- | fw/.gitignore | 13 | ||||
-rw-r--r-- | fw/main.c | 104 |
2 files changed, 112 insertions, 5 deletions
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 @@ -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<<GPIO_MODER_MODER10_Pos);/* PA10 - SDA */ GPIOA->AFR[0] = - (1<<GPIO_AFRH_AFSEL9_Pos); /* PA6 */ + (1<<GPIO_AFRL_AFSEL6_Pos); /* PA6 */ GPIOA->AFR[1] = (4<<GPIO_AFRH_AFSEL9_Pos) /* PA9 */ | (4<<GPIO_AFRH_AFSEL10_Pos);/* PA10 */ @@ -155,6 +156,8 @@ int main(void) { is to have this fall out of STMCubeMX. I love downloading 120MB of software to download another 100MB of software, only this time over unsecured HTTP, to generate 3.5 bytes of configuration values using a Java(TM) GUI. */ + for (int i=0; i<2000000; i++) + ; i2c_enable(I2C1); lcd1602_init(); @@ -166,13 +169,13 @@ int main(void) { TIM1->PSC = 48-1; /* 1us ticks */ TIM1->ARR = 1000-1; /* 1ms interval */ TIM1->EGR = TIM_EGR_UG; - TIM1->CCMR1 = (6<<TIM_CCMR1_OC1M_Pos); - TIM1->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<<TIM_CCMR1_OC1M_Pos) | TIM_CCMR1_OC1PE; + TIM3->CCER = TIM_CCER_CC1E; TIM3->EGR = TIM_EGR_UG; NVIC_EnableIRQ(TIM1_BRK_UP_TRG_COM_IRQn); @@ -191,14 +194,38 @@ 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<<INPUT_MENU)) { + if (menu_adjustment_done) { + menu_state = MENU_DEFAULT; + update_servo(); + } else { + menu_servo_lvl++; + if (menu_servo_lvl == NUM_SERVO_LEVELS) + menu_state = MENU_DEFAULT; + } + break; + } + + if (btn & (1<<INPUT_UP)) { + menu_adjustment_done = 1; + if (input_hold_total[INPUT_UP] < 3000) { + servo_lvl_set[menu_servo_lvl]++; + } else { + servo_lvl_set[menu_servo_lvl] += 10; + servo_lvl_set[menu_servo_lvl] -= servo_lvl_set[menu_servo_lvl]%10; + } + TIM3->CCR1 = servo_lvl_set[menu_servo_lvl]; + } + if (btn & (1<<INPUT_DOWN)) { + menu_adjustment_done = 1; + if (input_hold_total[INPUT_DOWN] < 3000) { + servo_lvl_set[menu_servo_lvl]--; + } else { + servo_lvl_set[menu_servo_lvl] -= 10; + servo_lvl_set[menu_servo_lvl] += (10 - servo_lvl_set[menu_servo_lvl]%10) % 10; + } + TIM3->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) { |