]> www.fi.muni.cz Git - heater.git/blobdiff - firmware/main.c
Low battery bugfix:
[heater.git] / firmware / main.c
index d238fa8703dbe799425071e98e07f26185666c43..65801e81b05fd74192238c0d9892c2d87c9c0b24 100644 (file)
  * Buttons:
  * There are two buttons (+ and -). Any button can wake the system up from
  * the power-down state.
  * Buttons:
  * There are two buttons (+ and -). Any button can wake the system up from
  * the power-down state.
- * TODO: When the system is woken up by the "-" button,
- * it starts with the minimum output power, when it is woken up by the "+"
- * button, it start with the maximum output power.
  * When running, the "-" button is used for decreasing the output power,
  * the "+" button is for increasing it.
  * When running, the "-" button is used for decreasing the output power,
  * the "+" button is for increasing it.
- * When on the lowest power state, the "-" button switches the system off.
- * Long "-" button press switches the system off, long "+" button
- * press sets the output power to maximum.
+ * Any long button press switches the system off.
  *
  * Status LED:
  * When powering up by a button press, the LED goes on to provide a visual
  *
  * Status LED:
  * When powering up by a button press, the LED goes on to provide a visual
@@ -84,9 +79,10 @@ static volatile uint16_t batt_on, batt_off; // measured voltage
                                * (1024UL * (mV)) \
                                / (6UL * ADC_1100MV_VALUE)) >> 8))
 static unsigned char batt_levels[] = {
                                * (1024UL * (mV)) \
                                / (6UL * ADC_1100MV_VALUE)) >> 8))
 static unsigned char batt_levels[] = {
-       MV_TO_ADC8(3350),
-       MV_TO_ADC8(3700),
-       MV_TO_ADC8(3900),
+       MV_TO_ADC8(3000), // below this, do not enable load, and switch off
+       MV_TO_ADC8(3150), // below this, switch off after some time
+       MV_TO_ADC8(3450), // battery low
+       MV_TO_ADC8(3800), // battery ok, above that almost full
 };
 #define BATT_N_LEVELS  (sizeof(batt_levels) / sizeof(batt_levels[0]))
 
 };
 #define BATT_N_LEVELS  (sizeof(batt_levels) / sizeof(batt_levels[0]))
 
@@ -400,7 +396,7 @@ static void status_led_next_pattern()
                }
        } else {
                unsigned char b_level = battery_level();
                }
        } else {
                unsigned char b_level = battery_level();
-               if (b_level) {
+               if (b_level > 1) {
                        battery_exhausted = 0;
                } else if (battery_exhausted) {
                        if (!--battery_exhausted)
                        battery_exhausted = 0;
                } else if (battery_exhausted) {
                        if (!--battery_exhausted)
@@ -409,8 +405,8 @@ static void status_led_next_pattern()
                        battery_exhausted = LED_BATTEMPTY_COUNT;
                }
 
                        battery_exhausted = LED_BATTEMPTY_COUNT;
                }
 
-               n_blinks = b_level + 1;
-               blink_on_time = 4;
+               n_blinks = b_level ? b_level : 1;
+               blink_on_time = b_level ? 4 : 2;
                blink_off_time = 0;
        }
 
                blink_off_time = 0;
        }
 
@@ -439,19 +435,12 @@ static void button_pressed(unsigned char button, unsigned char long_press)
 {
        // ignore simlultaneous button 1 and 2 press
        if (long_press) {
 {
        // ignore simlultaneous button 1 and 2 press
        if (long_press) {
-               if (button == 1) {
-                       power_down();
-                       return;
-               } else if (button == 2) {
-                       power_level = N_POWER_LEVELS-1;
-               }
+               power_down();
+               return;
        } else { // short press
                if (button == 1) {
                        if (power_level > 0) {
                                --power_level;
        } else { // short press
                if (button == 1) {
                        if (power_level > 0) {
                                --power_level;
-                       } else {
-                               power_down();
-                               return;
                        }
                } else if (button == 2) {
                        if (power_level < N_POWER_LEVELS-1) {
                        }
                } else if (button == 2) {
                        if (power_level < N_POWER_LEVELS-1) {
@@ -575,6 +564,10 @@ int main()
                                log_byte(batt_on >> 8);
 #endif
                        }
                                log_byte(batt_on >> 8);
 #endif
                        }
+                       if (jiffies == 0) {
+                               log_byte(batt_on >> 8);
+                               log_byte(batt_off >> 8);
+                       }
                        log_flush();
                }
        }
                        log_flush();
                }
        }