Browse Source

modify brightness in menus. nicer battery display.

Thomas Buck 1 year ago
parent
commit
8c9fcb659d
4 changed files with 90 additions and 20 deletions
  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 View File

36
     def once(self, k):
36
     def once(self, k):
37
         return self.new[k] and not self.old[k]
37
         return self.new[k] and not self.old[k]
38
 
38
 
39
+    def held(self, k):
40
+        return self.new[k]
41
+
39
 class LCD(framebuf.FrameBuffer):
42
 class LCD(framebuf.FrameBuffer):
40
     def __init__(self):
43
     def __init__(self):
41
         self.pwm = PWM(Pin(13))
44
         self.pwm = PWM(Pin(13))
50
 
53
 
51
         self.cs(1)
54
         self.cs(1)
52
 
55
 
53
-        #self.spi = SPI(1)
54
-        #self.spi = SPI(1,   1_000_000)
55
         self.spi = SPI(1, 100_000_000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=None)
56
         self.spi = SPI(1, 100_000_000, polarity=0, phase=0, sck=Pin(10), mosi=Pin(11), miso=None)
56
 
57
 
57
         self.dc = Pin(8, Pin.OUT)
58
         self.dc = Pin(8, Pin.OUT)
95
             "enter": False,
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
     def buttons(self):
108
     def buttons(self):
99
         keys = {
109
         keys = {
100
             "a": self.keyA.value() == 0,
110
             "a": self.keyA.value() == 0,
115
     def color(self, R, G, B):
125
     def color(self, R, G, B):
116
         return (((G & 0b00011100) << 3) + ((B & 0b11111000) >> 3) << 8) + (R & 0b11111000) + ((G & 0b11100000) >> 5)
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
     def brightness(self, v):
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
         self.pwm.duty_u16(int(v * 65535))
147
         self.pwm.duty_u16(int(v * 65535))
120
 
148
 
121
     def write_cmd(self, cmd):
149
     def write_cmd(self, cmd):

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

39
         if self.lock.locked():
39
         if self.lock.locked():
40
             self.lock.release()
40
             self.lock.release()
41
 
41
 
42
+        self.lcd.store_brightness()
43
+
42
         return self.results[self.current][4]
44
         return self.results[self.current][4]
43
 
45
 
44
     async def scan(self):
46
     async def scan(self):
99
             if self.current == i:
101
             if self.current == i:
100
                 c2 = self.lcd.red
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
             self.lcd.text(s1, 0, off + 2, c1)
105
             self.lcd.text(s1, 0, off + 2, c1)
104
             self.lcd.text(s2, 0, off + 12, c2)
106
             self.lcd.text(s2, 0, off + 12, c2)
105
 
107
 
122
                     self.current = 0
124
                     self.current = 0
123
                 else:
125
                 else:
124
                     self.current += 1
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
             elif keys.once("y"):
134
             elif keys.once("y"):
126
                 return 0
135
                 return 0
127
 
136
 
147
 
156
 
148
             self.draw_list()
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
         return -1
162
         return -1

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

29
         self.menuOff = 0
29
         self.menuOff = 0
30
 
30
 
31
     def exit(self):
31
     def exit(self):
32
+        self.lcd.store_brightness()
32
         return self.client, workflows[self.current]
33
         return self.client, workflows[self.current]
33
 
34
 
34
     def draw_list(self):
35
     def draw_list(self):
47
             if self.current == i:
48
             if self.current == i:
48
                 c = self.lcd.red
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
             self.lcd.text(s1, 0, off + 2, c)
52
             self.lcd.text(s1, 0, off + 2, c)
52
             self.lcd.text(s2, 0, off + 12, c)
53
             self.lcd.text(s2, 0, off + 12, c)
53
 
54
 
64
             self.current += 1
65
             self.current += 1
65
         elif keys.once("enter") or keys.once("a"):
66
         elif keys.once("enter") or keys.once("a"):
66
             return 1
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
         while self.current < 0:
76
         while self.current < 0:
69
             self.current += len(workflows)
77
             self.current += len(workflows)
76
 
84
 
77
         self.draw_list()
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
         return -1
90
         return -1

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

16
 # See <http://www.gnu.org/licenses/>.
16
 # See <http://www.gnu.org/licenses/>.
17
 # ----------------------------------------------------------------------------
17
 # ----------------------------------------------------------------------------
18
 
18
 
19
+from lcd import LCD
20
+lcd = LCD()
21
+
19
 import uasyncio as asyncio
22
 import uasyncio as asyncio
20
 import io
23
 import io
21
 import sys
24
 import sys
23
 import os
26
 import os
24
 import gc
27
 import gc
25
 import time
28
 import time
29
+from state_wait_temp import from_hsv, translate
26
 
30
 
27
 # https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/pico_lipo_shim/battery_pico.py
31
 # https://github.com/pimoroni/pimoroni-pico/blob/main/micropython/examples/pico_lipo_shim/battery_pico.py
28
 # https://github.com/pimoroni/enviro/pull/146
32
 # https://github.com/pimoroni/enviro/pull/146
29
 # TODO https://github.com/micropython/micropython/issues/11185
33
 # TODO https://github.com/micropython/micropython/issues/11185
30
 
34
 
31
-full_battery = 4.2
35
+full_battery = 4.1
32
 empty_battery = 3.2
36
 empty_battery = 3.2
37
+batt_warn_limit = 15
38
+batt_reread_limit = 2.7
33
 
39
 
34
 charging = machine.Pin("WL_GPIO2", machine.Pin.IN)
40
 charging = machine.Pin("WL_GPIO2", machine.Pin.IN)
35
 conversion_factor = 3 * 3.3 / 65535
41
 conversion_factor = 3 * 3.3 / 65535
51
     old_pad = get_pad(29)
57
     old_pad = get_pad(29)
52
     set_pad(29, 128)  # no pulls, no output, no input
58
     set_pad(29, 128)  # no pulls, no output, no input
53
 
59
 
54
-    sample_count = 10
60
+    sample_count = 3
55
     voltage = 0
61
     voltage = 0
56
     for i in range(0, sample_count):
62
     for i in range(0, sample_count):
57
         voltage += batteryVoltageRead()
63
         voltage += batteryVoltageRead()
63
 def batteryVoltage():
69
 def batteryVoltage():
64
     global cachedVoltage, lastCaching
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
         lastCaching = time.time()
73
         lastCaching = time.time()
68
         cachedVoltage = batteryVoltageAverage()
74
         cachedVoltage = batteryVoltageAverage()
75
+        if cachedVoltage <= batt_reread_limit:
76
+            cachedVoltage = batteryVoltageAverage()
69
 
77
 
70
     percentage = 100.0 * ((cachedVoltage - empty_battery) / (full_battery - empty_battery))
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
     return cachedVoltage, percentage
84
     return cachedVoltage, percentage
75
 
85
 
86
         self.lcd.fill(self.lcd.black)
96
         self.lcd.fill(self.lcd.black)
87
         self.lcd.text("Volcano Remote Control App", 0, 0, self.lcd.green)
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
         voltage, percentage = batteryVoltage()
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
         if charging.value() != 1:
104
         if charging.value() != 1:
95
             s = "{:.0f}% ({:.2f}V)".format(percentage, voltage)
105
             s = "{:.0f}% ({:.2f}V)".format(percentage, voltage)
96
-            c = self.lcd.white
97
-            if percentage <= 20:
106
+            if percentage <= batt_warn_limit:
98
                 c = self.lcd.red
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
         self.lcd.show()
124
         self.lcd.show()
102
-        return r
125
+        return ret
103
 
126
 
104
     def run(self):
127
     def run(self):
105
         if self.current == None:
128
         if self.current == None:
166
     while True:
189
     while True:
167
         states.run()
190
         states.run()
168
 
191
 
169
-from lcd import LCD
170
-lcd = LCD()
171
-
172
 def main():
192
 def main():
173
     # splash screen
193
     # splash screen
174
     from state_wait_temp import from_hsv
194
     from state_wait_temp import from_hsv
194
     lcd.textC(os.uname()[4][30 : 60], int(lcd.width / 2), lcd.height - 10, lcd.white, lcd.black)
214
     lcd.textC(os.uname()[4][30 : 60], int(lcd.width / 2), lcd.height - 10, lcd.white, lcd.black)
195
 
215
 
196
     lcd.show()
216
     lcd.show()
197
-    lcd.brightness(1.0)
198
 
217
 
199
     # bootloader access with face buttons
218
     # bootloader access with face buttons
200
     keys = lcd.buttons()
219
     keys = lcd.buttons()

Loading…
Cancel
Save