Browse Source

draw battery status on display

Thomas Buck 7 months ago
parent
commit
b7bae8f489
6 changed files with 74 additions and 7 deletions
  1. 2
    0
      include/image.h
  2. 3
    0
      src/console.c
  3. 48
    1
      src/image.c
  4. 19
    4
      src/lipo.c
  5. 2
    0
      src/main.c
  6. 0
    2
      src/text.c

+ 2
- 0
include/image.h View File

@@ -24,5 +24,7 @@
24 24
 void image_draw(char *data, uint width, uint height);
25 25
 
26 26
 void draw_splash(void);
27
+void draw_battery_indicator(void);
28
+void battery_run(void);
27 29
 
28 30
 #endif // __IMAGE_H__

+ 3
- 0
src/console.c View File

@@ -82,6 +82,7 @@ static void cnsl_interpret(const char *line) {
82 82
         println(" splash - draw image on screen");
83 83
         println("  fonts - show font list");
84 84
         println("   text - draw text on screen");
85
+        println("    bat - draw battery indicator");
85 86
         println("Press Enter with no input to repeat last command.");
86 87
         println("Use repeat to continuously execute last command.");
87 88
         println("Stop this by calling repeat again.");
@@ -153,6 +154,8 @@ static void cnsl_interpret(const char *line) {
153 154
             y_off = text.y;
154 155
             f = f->next;
155 156
         }
157
+    } else if (strcmp(line, "bat") == 0) {
158
+        draw_battery_indicator();
156 159
     } else {
157 160
         println("unknown command \"%s\"", line);
158 161
     }

+ 48
- 1
src/image.c View File

@@ -16,9 +16,12 @@
16 16
  * See <http://www.gnu.org/licenses/>.
17 17
  */
18 18
 
19
+#include <stdio.h>
20
+
19 21
 #include "config.h"
20 22
 #include "lcd.h"
21 23
 #include "text.h"
24
+#include "lipo.h"
22 25
 #include "image.h"
23 26
 
24 27
 #pragma GCC diagnostic push
@@ -26,6 +29,8 @@
26 29
 #include "logo.h"
27 30
 #pragma GCC diagnostic pop
28 31
 
32
+#define BATT_INTERVAL_MS 777
33
+
29 34
 void image_draw(char *data, uint width, uint height) {
30 35
     for (uint x = 0; x < width; x++) {
31 36
         for (uint y = 0; y < height; y++) {
@@ -66,7 +71,7 @@ void draw_splash(void) {
66 71
     text_draw(&text1);
67 72
 
68 73
     struct text_conf text2 = {
69
-        .text = __DATE__ " " __TIME__,
74
+        .text = __DATE__ " @ " __TIME__,
70 75
         .x = 0,
71 76
         .y = 195,
72 77
         .justify = false,
@@ -79,3 +84,45 @@ void draw_splash(void) {
79 84
     };
80 85
     text_draw(&text2);
81 86
 }
87
+
88
+void draw_battery_indicator(void) {
89
+    float v = lipo_voltage();
90
+    static char s[30];
91
+    if (lipo_charging()) {
92
+        //                     "Batt:   99.9%   (4.20V)"
93
+        snprintf(s, sizeof(s), "Batt: Charging! (%.2fV)", v);
94
+    } else {
95
+        snprintf(s, sizeof(s), "Batt:   %02.1f%%   (%.2fV)", lipo_percentage(v), v);
96
+    }
97
+
98
+    static struct text_font font = {
99
+        .fontname = "fixed_10x20",
100
+        .font = NULL,
101
+    };
102
+    if (font.font == NULL) {
103
+        text_prepare_font(&font);
104
+    }
105
+
106
+    struct text_conf text = {
107
+        .text = s,
108
+        .x = 0,
109
+        .y = 219,
110
+        .justify = false,
111
+        .alignment = MF_ALIGN_CENTER,
112
+        .width = 240,
113
+        .height = 240,
114
+        .margin = 2,
115
+        .bg = RGB_565(0x00, 0x00, 0x00),
116
+        .font = &font,
117
+    };
118
+    text_draw(&text);
119
+}
120
+
121
+void battery_run(void) {
122
+    static uint32_t last_run = 0;
123
+    uint32_t now = to_ms_since_boot(get_absolute_time());
124
+    if (now >= (last_run + BATT_INTERVAL_MS)) {
125
+        last_run = now;
126
+        draw_battery_indicator();
127
+    }
128
+}

+ 19
- 4
src/lipo.c View File

@@ -18,6 +18,8 @@
18 18
  * See <http://www.gnu.org/licenses/>.
19 19
  */
20 20
 
21
+#include <math.h>
22
+
21 23
 #include "stdbool.h"
22 24
 #include "hardware/adc.h"
23 25
 
@@ -37,6 +39,7 @@
37 39
 
38 40
 static const float full_battery = 4.1f;
39 41
 static const float empty_battery = 3.2f;
42
+static const float low_pass_factor = 0.9f;
40 43
 
41 44
 bool lipo_charging(void) {
42 45
 #if defined CYW43_WL_GPIO_VBUS_PIN
@@ -57,7 +60,7 @@ float lipo_voltage(void) {
57 60
 #if CYW43_USES_VSYS_PIN
58 61
     cyw43_thread_enter();
59 62
     // Make sure cyw43 is awake
60
-    cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
63
+    bool charging = cyw43_arch_gpio_get(CYW43_WL_GPIO_VBUS_PIN);
61 64
 #endif
62 65
 
63 66
     // setup adc
@@ -91,13 +94,25 @@ float lipo_voltage(void) {
91 94
 
92 95
     // Generate voltage
93 96
     const float conversion_factor = 3.3f / (1 << 12);
94
-    return vsys * 3 * conversion_factor;
97
+    float v_now = vsys * 3 * conversion_factor;
98
+
99
+    static float v_prev = NAN;
100
+    if (charging) {
101
+        v_prev = NAN;
102
+        return v_now;
103
+    } else {
104
+        if (isnan(v_prev)) {
105
+            v_prev = v_now;
106
+        }
107
+        v_prev = (v_prev * low_pass_factor) + (v_now * (1.0f - low_pass_factor));
108
+        return v_prev;
109
+    }
95 110
 }
96 111
 
97 112
 float lipo_percentage(float voltage) {
98 113
     float percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
99
-    if (percentage >= 100.0f) {
100
-        percentage = 100.0f;
114
+    if (percentage >= 99.9f) {
115
+        percentage = 99.9f;
101 116
     } else if (percentage < 0.0f) {
102 117
         percentage = 0.0f;
103 118
     }

+ 2
- 0
src/main.c View File

@@ -82,6 +82,8 @@ int main(void) {
82 82
         buttons_run();
83 83
         usb_run();
84 84
         cnsl_run();
85
+
86
+        battery_run();
85 87
     }
86 88
 
87 89
     return 0;

+ 0
- 2
src/text.c View File

@@ -117,8 +117,6 @@ void text_draw(struct text_conf *tc) {
117 117
         return;
118 118
     }
119 119
 
120
-    debug("'%s' %d", tc->text, tc->y);
121
-
122 120
     state_t state;
123 121
     state.options = tc;
124 122
 

Loading…
Cancel
Save