]> mj.ucw.cz Git - home-hw.git/blobdiff - aircon/firmware/main.c
Auto: Meditation mode turned off
[home-hw.git] / aircon / firmware / main.c
index b4650bf59810ca0842e85b1dcc91450ee59fcabd..da9df476d2e6403e1fc47cf0d726e2e5f47285d1 100644 (file)
@@ -19,6 +19,7 @@
 #include <string.h>
 
 static void rc_init(void);
+static bool rc_send(char key);
 
 static void clock_init(void)
 {
@@ -106,12 +107,14 @@ static void tick_init(void)
        systick_interrupt_enable();
 }
 
+#if 0
 static void delay_ms(uint ms)
 {
        u32 start_ticks = ms_ticks;
        while (ms_ticks - start_ticks < ms)
                ;
 }
+#endif
 
 static void usart_init(void)
 {
@@ -131,7 +134,6 @@ static void usart_init(void)
 static byte bypass_active;
 static byte fan_pwm;
 
-// FIXME
 static void show_temperature(void)
 {
        debug_putc('#');
@@ -157,7 +159,6 @@ static void pwm_init(void)
 
        // 50% PWM for the IR LED
        timer_set_oc_mode(TIM4, TIM_OC2, TIM_OCM_FORCE_HIGH);   // will be TIM_OCM_PWM1 when transmitting
-       // timer_set_oc_mode(TIM4, TIM_OC2, TIM_OCM_PWM1);      // FIXME
        timer_set_oc_value(TIM4, TIM_OC2, T4_CYCLE / 2);
        timer_set_oc_polarity_high(TIM4, TIM_OC2);
        timer_enable_oc_output(TIM4, TIM_OC2);
@@ -180,17 +181,24 @@ int main(void)
        pwm_init();
        rc_init();
 
-       debug_puts("Hello, world!\n");
+       debug_puts("Hello, world! This is Aircon Controller speaking.\n");
 
        ds_init();
        modbus_init();
 
-       byte cycles = 0;
+       u32 last_show_temp = 0;
+       u32 last_ds_step = 0;
+
        for (;;) {
-               debug_led_toggle();
-               delay_ms(100);
-               ds_step();
+               if (ms_ticks - last_ds_step >= 100) {
+                       debug_led_toggle();
+                       ds_step();
+                       last_ds_step = ms_ticks;
+               }
+
                modbus_loop();
+
+#if 0
                if (usart_get_flag(USART1, USART_SR_RXNE)) {
                        uint ch = usart_recv(USART1);
                        if (ch == 'B') {
@@ -219,12 +227,18 @@ int main(void)
                                 *      % = pwm*4.389 - 12.723
                                 */
                                timer_set_oc_value(TIM4, TIM_OC1, T4_CYCLE * fan_pwm / 256);
+                       } else {
+                               rc_send(ch);
                        }
                }
-               if (cycles++ >= 50) {
-                       cycles = 0;
+#endif
+
+               if (ms_ticks - last_show_temp >= 5000) {
                        show_temperature();
+                       last_show_temp = ms_ticks;
                }
+
+               wait_for_interrupt();
        }
 
        return 0;
@@ -335,12 +349,15 @@ static bool rc_send(char key)
 {
        if (rc_pending)
                return false;
+       if (!key)
+               return false;
 
        const char *s = strchr(rc_keys, key);
        if (!s)
                return false;
        rc_pending = key;
        rc_pattern_pos = rc_patterns[s - rc_keys];
+       debug_printf("RC sending: %c\n", key);
 
        gpio_clear(GPIOC, GPIO13);