|
35 | 35 | 2319 is 3.916 |
36 | 36 | 2269 is 3.831 |
37 | 37 |
|
| 38 | +I want application that will show big time (hour, minutes), with smaller seconds, date, and current battery parameters on the left side, on the right side, i want big battery icon, green when over 30 percent, red otherwise, and in bottom left I want graph of history values for voltage and percentage. |
| 39 | +
|
38 | 40 | """ |
39 | 41 |
|
40 | 42 | import lvgl as lv |
41 | 43 | import time |
42 | 44 |
|
43 | 45 | from mpos import Activity, BatteryManager |
44 | 46 |
|
| 47 | +HISTORY_LEN = 60 |
| 48 | + |
| 49 | +DARKPINK = lv.color_hex(0xEC048C) |
| 50 | +BLACK = lv.color_hex(0x000000) |
| 51 | + |
45 | 52 | class ShowBattery(Activity): |
46 | 53 |
|
47 | 54 | refresh_timer = None |
48 | | - |
49 | | - # Widgets: |
50 | | - raw_label = None |
| 55 | + |
| 56 | + # Widgets |
| 57 | + lbl_time = None |
| 58 | + lbl_sec = None |
| 59 | + lbl_date = None |
| 60 | + |
| 61 | + bat_outline = None |
| 62 | + bat_fill = None |
| 63 | + |
| 64 | + history_v = [] |
| 65 | + history_p = [] |
51 | 66 |
|
52 | 67 | def onCreate(self): |
53 | | - s = lv.obj() |
54 | | - self.raw_label = lv.label(s) |
55 | | - self.raw_label.set_text("starting...") |
56 | | - self.raw_label.center() |
57 | | - self.setContentView(s) |
| 68 | + scr = lv.obj() |
| 69 | + |
| 70 | + # --- TIME --- |
| 71 | + self.lbl_time = lv.label(scr) |
| 72 | + self.lbl_time.set_style_text_font(lv.font_montserrat_48, 0) |
| 73 | + self.lbl_time.align(lv.ALIGN.TOP_LEFT, 5, 5) |
| 74 | + |
| 75 | + self.lbl_sec = lv.label(scr) |
| 76 | + self.lbl_sec.set_style_text_font(lv.font_montserrat_24, 0) |
| 77 | + self.lbl_sec.align_to(self.lbl_time, lv.ALIGN.OUT_RIGHT_BOTTOM, 24, -4) |
| 78 | + |
| 79 | + self.lbl_date = lv.label(scr) |
| 80 | + self.lbl_date.set_style_text_font(lv.font_montserrat_24, 0) |
| 81 | + self.lbl_date.align(lv.ALIGN.TOP_LEFT, 5, 60) |
| 82 | + |
| 83 | + # --- BATTERY ICON --- |
| 84 | + self.bat_outline = lv.obj(scr) |
| 85 | + self.bat_size = 225 |
| 86 | + self.bat_outline.set_size(80, self.bat_size) |
| 87 | + self.bat_outline.align(lv.ALIGN.TOP_RIGHT, -10, 10) |
| 88 | + self.bat_outline.set_style_border_width(2, 0) |
| 89 | + self.bat_outline.set_style_radius(4, 0) |
| 90 | + |
| 91 | + self.bat_fill = lv.obj(self.bat_outline) |
| 92 | + self.bat_fill.align(lv.ALIGN.BOTTOM_MID, 0, -2) |
| 93 | + self.bat_fill.set_width(52) |
| 94 | + self.bat_fill.set_style_radius(2, 0) |
| 95 | + |
| 96 | + # --- CANVAS --- |
| 97 | + self.canvas = lv.canvas(scr) |
| 98 | + self.canvas.set_size(220, 100) |
| 99 | + self.canvas.align(lv.ALIGN.BOTTOM_LEFT, 5, -5) |
| 100 | + self.canvas.set_style_border_width(1, 0) |
| 101 | + self.canvas.set_style_bg_color(lv.color_white(), lv.PART.MAIN) |
| 102 | + buffer = bytearray(220 * 100 * 4) |
| 103 | + self.canvas.set_buffer(buffer, 220, 100, lv.COLOR_FORMAT.NATIVE) |
| 104 | + self.layer = lv.layer_t() |
| 105 | + self.canvas.init_layer(self.layer) |
| 106 | + |
| 107 | + self.setContentView(scr) |
| 108 | + |
| 109 | + def draw_line(self, color, x1, y1, x2, y2): |
| 110 | + dsc = lv.draw_line_dsc_t() |
| 111 | + lv.draw_line_dsc_t.init(dsc) |
| 112 | + dsc.color = color |
| 113 | + dsc.width = 4 |
| 114 | + dsc.round_end = 1 |
| 115 | + dsc.round_start = 1 |
| 116 | + dsc.p1 = lv.point_precise_t() |
| 117 | + dsc.p1.x = x1 |
| 118 | + dsc.p1.y = y1 |
| 119 | + dsc.p2 = lv.point_precise_t() |
| 120 | + dsc.p2.x = x2 |
| 121 | + dsc.p2.y = y2 |
| 122 | + lv.draw_line(self.layer,dsc) |
| 123 | + self.canvas.finish_layer(self.layer) |
58 | 124 |
|
59 | 125 | def onResume(self, screen): |
60 | 126 | super().onResume(screen) |
61 | 127 |
|
62 | | - def update_bat(timer): |
63 | | - r = BatteryManager.read_raw_adc() |
64 | | - v = BatteryManager.read_battery_voltage() |
| 128 | + def update(timer): |
| 129 | + now = time.localtime() |
| 130 | + |
| 131 | + hour, minute, second = now[3], now[4], now[5] |
| 132 | + date = f"{now[0]}-{now[1]:02}-{now[2]:02}" |
| 133 | + |
| 134 | + voltage = BatteryManager.read_battery_voltage() |
65 | 135 | percent = BatteryManager.get_battery_percentage() |
66 | | - text = f"{time.localtime()}\n{r}\n{v}V\n{percent}%" |
67 | | - print(text) |
68 | | - self.raw_label.set_text(text) |
69 | 136 |
|
70 | | - self.refresh_timer = lv.timer_create(update_bat,1000,None) #.set_repeat_count(10) |
| 137 | + # --- TIME --- |
| 138 | + self.lbl_time.set_text(f"{hour:02}:{minute:02}") |
| 139 | + self.lbl_sec.set_text(f":{second:02}") |
| 140 | + date += f"\n{voltage:.2f}V {percent:.0f}%" |
| 141 | + self.lbl_date.set_text(date) |
| 142 | + |
| 143 | + # --- BATTERY ICON --- |
| 144 | + fill_h = int((percent / 100) * (self.bat_size * 0.9)) |
| 145 | + self.bat_fill.set_height(fill_h) |
| 146 | + |
| 147 | + if percent >= 30: |
| 148 | + self.bat_fill.set_style_bg_color(lv.palette_main(lv.PALETTE.GREEN), 0) |
| 149 | + else: |
| 150 | + self.bat_fill.set_style_bg_color(lv.palette_main(lv.PALETTE.RED), 0) |
| 151 | + |
| 152 | + # --- HISTORY --- |
| 153 | + self.history_v.append(voltage) |
| 154 | + self.history_p.append(percent) |
| 155 | + |
| 156 | + if len(self.history_v) > HISTORY_LEN: |
| 157 | + self.history_v.pop(0) |
| 158 | + self.history_p.pop(0) |
| 159 | + |
| 160 | + self.draw_graph() |
| 161 | + |
| 162 | + self.refresh_timer = lv.timer_create(update, 1000, None) |
| 163 | + |
| 164 | + def draw_graph(self): |
| 165 | + self.canvas.fill_bg(lv.color_white(), lv.OPA.COVER) |
| 166 | + self.canvas.clean() |
| 167 | + |
| 168 | + w = self.canvas.get_width() |
| 169 | + h = self.canvas.get_height() |
| 170 | + |
| 171 | + if len(self.history_v) < 2: |
| 172 | + return |
| 173 | + |
| 174 | + v_min = 3.3 |
| 175 | + v_max = 4.2 |
| 176 | + v_range = max(v_max - v_min, 0.01) |
| 177 | + |
| 178 | + for i in range(1, len(self.history_v)): |
| 179 | + x1 = int((i - 1) * w / HISTORY_LEN) |
| 180 | + x2 = int(i * w / HISTORY_LEN) |
| 181 | + |
| 182 | + yv1 = h - int((self.history_v[i - 1] - v_min) / v_range * h) |
| 183 | + yv2 = h - int((self.history_v[i] - v_min) / v_range * h) |
| 184 | + |
| 185 | + yp1 = h - int(self.history_p[i - 1] / 100 * h) |
| 186 | + yp2 = h - int(self.history_p[i] / 100 * h) |
| 187 | + |
| 188 | + self.draw_line(DARKPINK, x1, yv1, x2, yv2) |
| 189 | + self.draw_line(BLACK, x1, yp1, x2, yp2) |
71 | 190 |
|
72 | 191 | def onPause(self, screen): |
73 | 192 | super().onPause(screen) |
74 | 193 | if self.refresh_timer: |
75 | 194 | self.refresh_timer.delete() |
| 195 | + self.refresh_timer = None |
0 commit comments