Status LED visual feedback for buttons and battery
[heater.git] / firmware / main.c
index 5c9a566..76bed47 100644 (file)
  * Status LED:
  * When powering up by a button press, the LED goes on to provide a visual
  * feedback, and is switched off after the button is released.
- * TODO: After a button press, the # of blinks of the LED reflects the
+ * After a button press, the # of blinks of the LED reflects the
  * chosen output power level for some time. Afterwards, it displays
  * the battery level.
- * TODO: When the battery is completely exhausted, the output power is switched
+ * When the battery is completely exhausted, the output power is switched
  * off, the LED keeps blinking for some time, and then the whole system is
  * switched off to avoid deep discharge of the battery.
  *
 #define WAKEUP_POLL 50 // msec
 #define WAKEUP_LIMIT 5 // times WAKEUP_POLL
 
-/* output power levels */
-#define N_POWER_LEVELS 5
-static unsigned char power_levels[N_POWER_LEVELS] = {
-
-};
-
-static unsigned char power_level = 0; // selected power level
-
 /* which state (output on or output off) are we measuring now */
 static volatile unsigned char adc_type, adc_drop;
 #define ADC_RUNAVG_SHIFT 5     // running average shift on batt_on, batt_off
@@ -82,12 +74,42 @@ static volatile uint16_t batt_on, batt_off; // measured voltage
 #define MV_TO_ADC8(mV) ((unsigned char)(((uint32_t)(1UL << ADC_RUNAVG_SHIFT) \
                                * (1024UL * (mV)) \
                                / (6UL * ADC_1100MV_VALUE)) >> 8))
-#define BATT_N_LEVELS  3
-static unsigned char batt_levels[BATT_N_LEVELS] = {
-       MV_TO_ADC8(3500),
+static unsigned char batt_levels[] = {
+       MV_TO_ADC8(3350),
        MV_TO_ADC8(3700),
        MV_TO_ADC8(3900),
 };
+#define BATT_N_LEVELS  (sizeof(batt_levels) / sizeof(batt_levels[0]))
+
+/* output power and PWM calculation */
+#define PWM_TOP        255
+#define PWM_MAX        (PWM_TOP - 8)   // to allow for ADC "batt_off" measurements
+
+/*
+ * The values in power_levels[] array are voltages at which the load
+ * would give the expected power (we don't have sqrt() function,
+ * so we cannot use mW values directly. They can be calculated as
+ * voltage[V] = sqrt(load_resistance[Ohm] * expected_power[W])
+ * or
+ * voltage[mV] = sqrt(load_resistance[mOhm] * expected_power[mW])
+ *
+ * I use 1.25 W as minimum power, each step is sqrt(2)*previous_step,
+ * so the 5th step is 5 W.
+ */
+static unsigned char power_levels[] = {
+       MV_TO_ADC8(1581),       // 1250 mW for 2 Ohm load
+       MV_TO_ADC8(1880),       // 1768 mW for 2 Ohm load
+       MV_TO_ADC8(2236),       // 2500 mW for 2 Ohm load
+       MV_TO_ADC8(2659),       // 3536 mW for 2 Ohm load
+       MV_TO_ADC8(3162),       // 5000 mW for 2 Ohm load
+};
+#define N_POWER_LEVELS (sizeof(power_levels) / sizeof(power_levels[0]))
+
+static unsigned char power_level = 0; // selected power level
+static unsigned char power_level_changed; // for visual feedback
+
+#define LED_PWRCHANGE_COUNT    3
+#define LED_BATTEMPTY_COUNT    60
 
 /* timing by WDT */
 static volatile unsigned char jiffies, next_clock_tick;
@@ -96,6 +118,7 @@ static volatile unsigned char jiffies, next_clock_tick;
 #define BUTTON_SHORT_MIN       1
 #define BUTTON_LONG_MIN                10
 
+
 /* ========= Analog to Digital Converter (battery voltage) ========== */
 static void adc_init()
 {
@@ -160,7 +183,7 @@ static void pwm_init()
        // TCCR1 = _BV(CS10); // clk/1 = 1 MHz
        TCCR1 = _BV(CS11) | _BV(CS13); // clk/512 = 2 kHz
        GTCCR = _BV(COM1B1) | _BV(PWM1B);
-       OCR1C = 255;
+       OCR1C = PWM_TOP;
        // OCR1B = steps[0];
        OCR1B = 0;
        TIMSK = _BV(OCIE1B) | _BV(TOIE1);
@@ -323,13 +346,78 @@ static void power_down()
        hw_setup();
 }
 
+/* ============ Status LED blinking =================================== */
+static unsigned char blink_on_time, blink_off_time, n_blinks;
+static unsigned char blink_counter;
+
+static unsigned char battery_level()
+{
+       unsigned char i, adc8;
+
+       // NOTE: we use 8-bit value only, so we don't need lock to protect
+       // us against concurrently running ADC IRQ handler:
+       adc8 = batt_off >> 8;
+
+       for (i = 0; i < BATT_N_LEVELS; i++)
+               if (batt_levels[i] > adc8)
+                       break;
+
+       return i;
+}
+
+static void status_led_next_pattern()
+{
+       static unsigned char battery_exhausted;
+
+       if (power_level_changed) {
+               power_level_changed--;
+               n_blinks = power_level + 1;
+       } else {
+               unsigned char b_level = battery_level();
+               if (b_level) {
+                       battery_exhausted = 0;
+               } else if (battery_exhausted) {
+                       if (!--battery_exhausted)
+                               power_down();
+               } else {
+                       battery_exhausted = LED_BATTEMPTY_COUNT;
+               }
+
+               n_blinks = b_level + 1;
+       }
+
+       blink_on_time = 2;
+       blink_off_time = 1;
+       blink_counter = 10;
+}
+
+static void timer_blink()
+{
+       if (blink_counter) {
+               blink_counter--;
+       } else if (!status_led_is_on()) {
+               status_led_on();
+               blink_counter = blink_on_time;
+       } else if (n_blinks) {
+               --n_blinks;
+               status_led_off();
+               blink_counter = blink_off_time;
+       } else {
+               status_led_next_pattern();
+       }
+}
+
 /* ======== Button press detection and  handling ===================== */
 static void button_pressed(unsigned char button, unsigned char long_press)
 {
        // ignore simlultaneous button 1 and 2 press
+       // Note: we set power_level_changed after each button press,
+       // even when the power is at maximum, to provide visual feedback
+       // with status LED.
        if (long_press) {
                if (button == 1) {
                        power_down();
+                       return;
                } else if (button == 2) {
                        power_level = N_POWER_LEVELS-1;
                }
@@ -339,6 +427,7 @@ static void button_pressed(unsigned char button, unsigned char long_press)
                                --power_level;
                        } else {
                                power_down();
+                               return;
                        }
                } else if (button == 2) {
                        if (power_level < N_POWER_LEVELS-1) {
@@ -346,6 +435,8 @@ static void button_pressed(unsigned char button, unsigned char long_press)
                        }
                }
        }
+       power_level_changed = LED_PWRCHANGE_COUNT;
+       status_led_next_pattern();
 }
 
 static unsigned char button_state, button_state_time;
@@ -379,50 +470,32 @@ static void timer_check_buttons()
        button_state_time = 0;
 }
 
-/* ============ Status LED blinking =================================== */
-static unsigned char blink_on_time, blink_off_time, n_blinks;
-static unsigned char blink_counter;
-
-static unsigned char battery_level()
+/* ===================== Output power control ======================== */
+static void calculate_power_level()
 {
-       unsigned char i, adc8;
+       uint32_t pwm;
+       unsigned char batt_on8;
 
-       // NOTE: we use 8-bit value only, so we don't need lock to protect
-       // us against concurrently running ADC IRQ handler:
-       adc8 = batt_off >> 8;
+       if (battery_level() == 0 || batt_on == 0) {
+               pwm_set(0);
+               // TODO power_down() after some time
+               return;
+       }
 
-       for (i = 0; i < BATT_N_LEVELS; i++)
-               if (batt_levels[i] > adc8)
-                       break;
+       batt_on8 = batt_on >> 8;
 
-       return i;
-}
+       pwm = (uint32_t)PWM_TOP * power_levels[power_level]
+               * power_levels[power_level];
+       pwm /= (uint32_t)batt_on8 * batt_on8;
 
-static void status_led_next_pattern()
-{
+       if (pwm > PWM_MAX)
+               pwm = PWM_MAX;
 
-       // for now, display the selected intensity
-       n_blinks = power_level + 1;
-       // n_blinks = battery_level() + 1;
-       blink_on_time = 0;
-       blink_off_time = 2;
-       blink_counter = 10;
-}
+       log_byte(0x10 + power_level);
+       log_byte(batt_on8);
+       log_byte(pwm & 0xFF);
 
-static void timer_blink()
-{
-       if (blink_counter) {
-               blink_counter--;
-       } else if (status_led_is_on()) {
-               status_led_off();
-               blink_counter = blink_off_time;
-       } else if (n_blinks) {
-               --n_blinks;
-               status_led_on();
-               blink_counter = blink_on_time;
-       } else {
-               status_led_next_pattern();
-       }
+       pwm_set(pwm);
 }
 
 int main()
@@ -435,6 +508,9 @@ int main()
        log_word(batt_levels[2]);
        log_flush();
 #endif
+       log_byte(power_levels[0]);
+       log_byte(power_levels[4]);
+       log_flush();
 
        power_down();
 
@@ -459,12 +535,7 @@ int main()
                        timer_check_buttons();
 
                        if ((jiffies & 0x0F) == 0) {
-                               unsigned char i;
-
-                               for (i = 0; i < BATT_N_LEVELS; i++)
-                                       if (batt_levels[i] > batt_off)
-                                               break;
-
+                               calculate_power_level();
 #if 0
                                log_byte(0xcc);
                                log_byte(i);