Sfoglia il codice sorgente

modify brightness in menus. nicer battery display.

Thomas Buck 1 anno fa
parent
commit
8c9fcb659d
4 ha cambiato i file con 90 aggiunte e 20 eliminazioni
  1. 30
    2
      python-test/lcd.py
  2. 13
    1
      python-test/state_scan.py
  3. 12
    1
      python-test/state_select.py
  4. 35
    16
      python-test/states.py

+ 30
- 2
python-test/lcd.py Vedi File

@@ -36,6 +36,9 @@ class KeyCheck:
36 36
     def once(self, k):
37 37
         return self.new[k] and not self.old[k]
38 38
 
39
+    def held(self, k):
40
+        return self.new[k]
41
+
39 42
 class LCD(framebuf.FrameBuffer):
40 43
     def __init__(self):
41 44
         self.pwm = PWM(Pin(13))
@@ -50,8 +53,6 @@ class LCD(framebuf.FrameBuffer):
50 53
 
51 54
         self.cs(1)
52 55
 
53
-        #self.spi = SPI(1)
54
-        #self.spi = SPI(1,   1_000_000)
55 56
         self.spi = SPI(1, 100_000_000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=None)
56 57
 
57 58
         self.dc = Pin(8, Pin.OUT)
@@ -95,6 +96,15 @@ class LCD(framebuf.FrameBuffer):
95 96
             "enter": False,
96 97
         }
97 98
 
99
+        self.curr_brightness = 0.0
100
+        try:
101
+            with open("_cur_bright.txt", "r") as f:
102
+                s = next(f).split()[0]
103
+                self.curr_brightness = float(s)
104
+        except:
105
+            pass
106
+        self.brightness(self.curr_brightness)
107
+
98 108
     def buttons(self):
99 109
         keys = {
100 110
             "a": self.keyA.value() == 0,
@@ -115,7 +125,25 @@ class LCD(framebuf.FrameBuffer):
115 125
     def color(self, R, G, B):
116 126
         return (((G & 0b00011100) << 3) + ((B & 0b11111000) >> 3) << 8) + (R & 0b11111000) + ((G & 0b11100000) >> 5)
117 127
 
128
+    def store_brightness(self):
129
+        old = -1.0
130
+        try:
131
+            with open("_cur_bright.txt", "r") as f:
132
+                s = next(f).split()[0]
133
+                old = float(s)
134
+        except:
135
+            pass
136
+        if self.curr_brightness != old:
137
+            with open("_cur_bright.txt", "w") as f:
138
+                s = "{}\n".format(self.curr_brightness)
139
+                f.write(s)
140
+
118 141
     def brightness(self, v):
142
+        if v < 0.0:
143
+            v = 0.0
144
+        if v > 1.0:
145
+            v = 1.0
146
+        self.curr_brightness = v
119 147
         self.pwm.duty_u16(int(v * 65535))
120 148
 
121 149
     def write_cmd(self, cmd):

+ 13
- 1
python-test/state_scan.py Vedi File

@@ -39,6 +39,8 @@ class StateScan:
39 39
         if self.lock.locked():
40 40
             self.lock.release()
41 41
 
42
+        self.lcd.store_brightness()
43
+
42 44
         return self.results[self.current][4]
43 45
 
44 46
     async def scan(self):
@@ -99,7 +101,7 @@ class StateScan:
99 101
             if self.current == i:
100 102
                 c2 = self.lcd.red
101 103
 
102
-            self.lcd.hline(0, off, self.lcd.width, self.lcd.blue)
104
+            self.lcd.hline(0, off - 3, self.lcd.width, self.lcd.blue)
103 105
             self.lcd.text(s1, 0, off + 2, c1)
104 106
             self.lcd.text(s2, 0, off + 12, c2)
105 107
 
@@ -122,6 +124,13 @@ class StateScan:
122 124
                     self.current = 0
123 125
                 else:
124 126
                     self.current += 1
127
+            elif keys.held("left"):
128
+                v = self.lcd.curr_brightness - 0.05
129
+                if v < 0.05:
130
+                    v = 0.05
131
+                self.lcd.brightness(v)
132
+            elif keys.held("right"):
133
+                self.lcd.brightness(self.lcd.curr_brightness + 0.05)
125 134
             elif keys.once("y"):
126 135
                 return 0
127 136
 
@@ -147,4 +156,7 @@ class StateScan:
147 156
 
148 157
             self.draw_list()
149 158
 
159
+        w = int(self.lcd.width * self.lcd.curr_brightness)
160
+        self.lcd.hline(0, 24, w, self.lcd.white)
161
+
150 162
         return -1

+ 12
- 1
python-test/state_select.py Vedi File

@@ -29,6 +29,7 @@ class StateSelect:
29 29
         self.menuOff = 0
30 30
 
31 31
     def exit(self):
32
+        self.lcd.store_brightness()
32 33
         return self.client, workflows[self.current]
33 34
 
34 35
     def draw_list(self):
@@ -47,7 +48,7 @@ class StateSelect:
47 48
             if self.current == i:
48 49
                 c = self.lcd.red
49 50
 
50
-            self.lcd.hline(0, off, self.lcd.width, self.lcd.blue)
51
+            self.lcd.hline(0, off - 3, self.lcd.width, self.lcd.blue)
51 52
             self.lcd.text(s1, 0, off + 2, c)
52 53
             self.lcd.text(s2, 0, off + 12, c)
53 54
 
@@ -64,6 +65,13 @@ class StateSelect:
64 65
             self.current += 1
65 66
         elif keys.once("enter") or keys.once("a"):
66 67
             return 1
68
+        elif keys.held("left"):
69
+            v = self.lcd.curr_brightness - 0.05
70
+            if v < 0.05:
71
+                v = 0.05
72
+            self.lcd.brightness(v)
73
+        elif keys.held("right"):
74
+            self.lcd.brightness(self.lcd.curr_brightness + 0.05)
67 75
 
68 76
         while self.current < 0:
69 77
             self.current += len(workflows)
@@ -76,4 +84,7 @@ class StateSelect:
76 84
 
77 85
         self.draw_list()
78 86
 
87
+        w = int(self.lcd.width * self.lcd.curr_brightness)
88
+        self.lcd.hline(0, 24, w, self.lcd.white)
89
+
79 90
         return -1

+ 35
- 16
python-test/states.py Vedi File

@@ -16,6 +16,9 @@
16 16
 # See <http://www.gnu.org/licenses/>.
17 17
 # ----------------------------------------------------------------------------
18 18
 
19
+from lcd import LCD
20
+lcd = LCD()
21
+
19 22
 import uasyncio as asyncio
20 23
 import io
21 24
 import sys
@@ -23,13 +26,16 @@ import machine
23 26
 import os
24 27
 import gc
25 28
 import time
29
+from state_wait_temp import from_hsv, translate
26 30
 
27 31
 # https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/pico_lipo_shim/battery_pico.py
28 32
 # https://github.com/pimoroni/enviro/pull/146
29 33
 # TODO https://github.com/micropython/micropython/issues/11185
30 34
 
31
-full_battery = 4.2
35
+full_battery = 4.1
32 36
 empty_battery = 3.2
37
+batt_warn_limit = 15
38
+batt_reread_limit = 2.7
33 39
 
34 40
 charging = machine.Pin("WL_GPIO2", machine.Pin.IN)
35 41
 conversion_factor = 3 * 3.3 / 65535
@@ -51,7 +57,7 @@ def batteryVoltageAverage():
51 57
     old_pad = get_pad(29)
52 58
     set_pad(29, 128)  # no pulls, no output, no input
53 59
 
54
-    sample_count = 10
60
+    sample_count = 3
55 61
     voltage = 0
56 62
     for i in range(0, sample_count):
57 63
         voltage += batteryVoltageRead()
@@ -63,13 +69,17 @@ def batteryVoltageAverage():
63 69
 def batteryVoltage():
64 70
     global cachedVoltage, lastCaching
65 71
 
66
-    if ((time.time() - lastCaching) >= 2) or (cachedVoltage == None):
72
+    if ((time.time() - lastCaching) > 0) or (cachedVoltage == None):
67 73
         lastCaching = time.time()
68 74
         cachedVoltage = batteryVoltageAverage()
75
+        if cachedVoltage <= batt_reread_limit:
76
+            cachedVoltage = batteryVoltageAverage()
69 77
 
70 78
     percentage = 100.0 * ((cachedVoltage - empty_battery) / (full_battery - empty_battery))
71
-    if percentage > 100.0:
72
-        percentage = 100.0
79
+    if percentage >= 100.0:
80
+        percentage = 99.0
81
+    elif percentage < 0.0:
82
+        percentage = 0.0
73 83
 
74 84
     return cachedVoltage, percentage
75 85
 
@@ -86,20 +96,33 @@ class States:
86 96
         self.lcd.fill(self.lcd.black)
87 97
         self.lcd.text("Volcano Remote Control App", 0, 0, self.lcd.green)
88 98
 
89
-        r = await self.states[self.current].draw()
99
+        ret = await self.states[self.current].draw()
90 100
 
91 101
         voltage, percentage = batteryVoltage()
92
-        s = "Charging ({:.2f}V)".format(voltage)
93
-        c = self.lcd.green
102
+        s = "Charging"
103
+        c = self.lcd.white
94 104
         if charging.value() != 1:
95 105
             s = "{:.0f}% ({:.2f}V)".format(percentage, voltage)
96
-            c = self.lcd.white
97
-            if percentage <= 20:
106
+            if percentage <= batt_warn_limit:
98 107
                 c = self.lcd.red
99
-        self.lcd.text("Battery: {}".format(s), 0, self.lcd.height - 10, c)
108
+            else:
109
+                hue = translate(percentage, batt_warn_limit, 100, 0.0, 0.333)
110
+                r, g, b = from_hsv(hue, 1.0, 1.0)
111
+                c = self.lcd.color(r, g, b)
112
+        whole = "Batt: {}".format(s)
113
+        self.lcd.text(whole, 0, self.lcd.height - 10, c)
114
+
115
+        off = (len(whole) + 1) * 8
116
+        if percentage <= batt_warn_limit:
117
+            self.lcd.text("CHARGE NOW!", off, self.lcd.height - 10, self.lcd.red)
118
+        elif charging.value() != 1:
119
+            self.lcd.rect(off, self.lcd.height - 10, self.lcd.width - off, 8, c, False)
120
+            max_w = self.lcd.width - off - 2
121
+            w = int(percentage / 100.0 * max_w)
122
+            self.lcd.rect(off + 1, self.lcd.height - 9, w, 6, c, True)
100 123
 
101 124
         self.lcd.show()
102
-        return r
125
+        return ret
103 126
 
104 127
     def run(self):
105 128
         if self.current == None:
@@ -166,9 +189,6 @@ def state_machine(lcd):
166 189
     while True:
167 190
         states.run()
168 191
 
169
-from lcd import LCD
170
-lcd = LCD()
171
-
172 192
 def main():
173 193
     # splash screen
174 194
     from state_wait_temp import from_hsv
@@ -194,7 +214,6 @@ def main():
194 214
     lcd.textC(os.uname()[4][30 : 60], int(lcd.width / 2), lcd.height - 10, lcd.white, lcd.black)
195 215
 
196 216
     lcd.show()
197
-    lcd.brightness(1.0)
198 217
 
199 218
     # bootloader access with face buttons
200 219
     keys = lcd.buttons()

Loading…
Annulla
Salva