Browse Source

draw battery status on display

Thomas Buck 1 year 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
 void image_draw(char *data, uint width, uint height);
24
 void image_draw(char *data, uint width, uint height);
25
 
25
 
26
 void draw_splash(void);
26
 void draw_splash(void);
27
+void draw_battery_indicator(void);
28
+void battery_run(void);
27
 
29
 
28
 #endif // __IMAGE_H__
30
 #endif // __IMAGE_H__

+ 3
- 0
src/console.c View File

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

+ 48
- 1
src/image.c View File

16
  * See <http://www.gnu.org/licenses/>.
16
  * See <http://www.gnu.org/licenses/>.
17
  */
17
  */
18
 
18
 
19
+#include <stdio.h>
20
+
19
 #include "config.h"
21
 #include "config.h"
20
 #include "lcd.h"
22
 #include "lcd.h"
21
 #include "text.h"
23
 #include "text.h"
24
+#include "lipo.h"
22
 #include "image.h"
25
 #include "image.h"
23
 
26
 
24
 #pragma GCC diagnostic push
27
 #pragma GCC diagnostic push
26
 #include "logo.h"
29
 #include "logo.h"
27
 #pragma GCC diagnostic pop
30
 #pragma GCC diagnostic pop
28
 
31
 
32
+#define BATT_INTERVAL_MS 777
33
+
29
 void image_draw(char *data, uint width, uint height) {
34
 void image_draw(char *data, uint width, uint height) {
30
     for (uint x = 0; x < width; x++) {
35
     for (uint x = 0; x < width; x++) {
31
         for (uint y = 0; y < height; y++) {
36
         for (uint y = 0; y < height; y++) {
66
     text_draw(&text1);
71
     text_draw(&text1);
67
 
72
 
68
     struct text_conf text2 = {
73
     struct text_conf text2 = {
69
-        .text = __DATE__ " " __TIME__,
74
+        .text = __DATE__ " @ " __TIME__,
70
         .x = 0,
75
         .x = 0,
71
         .y = 195,
76
         .y = 195,
72
         .justify = false,
77
         .justify = false,
79
     };
84
     };
80
     text_draw(&text2);
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
  * See <http://www.gnu.org/licenses/>.
18
  * See <http://www.gnu.org/licenses/>.
19
  */
19
  */
20
 
20
 
21
+#include <math.h>
22
+
21
 #include "stdbool.h"
23
 #include "stdbool.h"
22
 #include "hardware/adc.h"
24
 #include "hardware/adc.h"
23
 
25
 
37
 
39
 
38
 static const float full_battery = 4.1f;
40
 static const float full_battery = 4.1f;
39
 static const float empty_battery = 3.2f;
41
 static const float empty_battery = 3.2f;
42
+static const float low_pass_factor = 0.9f;
40
 
43
 
41
 bool lipo_charging(void) {
44
 bool lipo_charging(void) {
42
 #if defined CYW43_WL_GPIO_VBUS_PIN
45
 #if defined CYW43_WL_GPIO_VBUS_PIN
57
 #if CYW43_USES_VSYS_PIN
60
 #if CYW43_USES_VSYS_PIN
58
     cyw43_thread_enter();
61
     cyw43_thread_enter();
59
     // Make sure cyw43 is awake
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
 #endif
64
 #endif
62
 
65
 
63
     // setup adc
66
     // setup adc
91
 
94
 
92
     // Generate voltage
95
     // Generate voltage
93
     const float conversion_factor = 3.3f / (1 << 12);
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
 float lipo_percentage(float voltage) {
112
 float lipo_percentage(float voltage) {
98
     float percentage = 100.0f * ((voltage - empty_battery) / (full_battery - empty_battery));
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
     } else if (percentage < 0.0f) {
116
     } else if (percentage < 0.0f) {
102
         percentage = 0.0f;
117
         percentage = 0.0f;
103
     }
118
     }

+ 2
- 0
src/main.c View File

82
         buttons_run();
82
         buttons_run();
83
         usb_run();
83
         usb_run();
84
         cnsl_run();
84
         cnsl_run();
85
+
86
+        battery_run();
85
     }
87
     }
86
 
88
 
87
     return 0;
89
     return 0;

+ 0
- 2
src/text.c View File

117
         return;
117
         return;
118
     }
118
     }
119
 
119
 
120
-    debug("'%s' %d", tc->text, tc->y);
121
-
122
     state_t state;
120
     state_t state;
123
     state.options = tc;
121
     state.options = tc;
124
 
122
 

Loading…
Cancel
Save