Parcourir la source

cardpal

having recursion issues (try going in and out of a menu repeatedly you will see what i mean)
cube il y a 1 mois
Parent
révision
f46ab997dd
1 fichiers modifiés avec 283 ajouts et 0 suppressions
  1. 283
    0
      CARDPUTER CircuitPython/projects/CardPal/code.py

+ 283
- 0
CARDPUTER CircuitPython/projects/CardPal/code.py Voir le fichier

@@ -0,0 +1,283 @@
1
+from keyb import Keyboard # type: ignore
2
+import time, os, rtc, json, wifi, supervisor # type: ignore
3
+import board, busio, sdcardio, storage, terminalio, displayio # type: ignore
4
+from adafruit_display_text import label # type: ignore
5
+from adafruit_display_shapes.rect import Rect # type: ignore
6
+#import pytumblr # type: ignore
7
+import tumblrapi # type: ignore
8
+
9
+### DEFAULT HOME PAGE # # # # # # # # # #
10
+def home(pos = 0):
11
+    title_label.text = "Home"
12
+    title_label.x = 10
13
+    title_label.y = 14
14
+
15
+    ip_label.text = my_ip
16
+    ip_label.x = 160
17
+    ip_label.y = 20
18
+
19
+    menu_options = ["SD Card", "Tumblr", "Web Server"]
20
+    count = 0
21
+    menu_labs = []
22
+    for option in menu_options:
23
+        if count == pos:
24
+            menu_label = label.Label(terminalio.FONT, text = "> " + option, color=0xffffff, scale=1)
25
+        else:
26
+            menu_label = label.Label(terminalio.FONT, text = option, color=0xffffff, scale=1)
27
+        menu_label.x = 10
28
+        menu_label.y = 45 + count * 10
29
+        display.root_group.append(menu_label)
30
+        menu_labs.append(menu_label)
31
+        count+=1
32
+
33
+    while True:
34
+        key = keyb.scan()
35
+        if key == "UP":
36
+            if pos == 0:
37
+                pass
38
+            else:
39
+                menu_labs[pos].text = menu_options[pos]
40
+                pos -= 1
41
+            menu_labs[pos].text = "> " + menu_options[pos]
42
+
43
+        elif key == "DOWN":
44
+            if pos == len(menu_options)-1:
45
+                pass
46
+            else:
47
+                menu_labs[pos].text = menu_options[pos]
48
+                pos += 1
49
+            menu_labs[pos].text = "> " + menu_options[pos]
50
+
51
+        elif key == "\n":
52
+            option = menu_options[pos]
53
+            if option == "SD Card":
54
+                for lab in menu_labs:
55
+                    display.root_group.remove(lab)
56
+                sd_card()
57
+                return
58
+            elif option == "Tumblr":
59
+                for lab in menu_labs:
60
+                    display.root_group.remove(lab)
61
+                tumblr()
62
+                return
63
+            elif option == "Web Server":
64
+                for lab in menu_labs:
65
+                    display.root_group.remove(lab)
66
+                web_server()
67
+                return
68
+
69
+# # # # # # # # # # # # # # # # # # 
70
+
71
+def sd_card():
72
+    title_label.text = "SD Card"
73
+
74
+    fpos = 0
75
+
76
+    filename_labels = []
77
+    files = os.listdir("/sd")
78
+    count = 0
79
+    for filename in files:
80
+        if count == pos:
81
+            filename_label = label.Label(terminalio.FONT, text = "> " + filename, color=0xffffff, scale=1)
82
+        else:
83
+            filename_label = label.Label(terminalio.FONT, text = filename, color=0xffffff, scale=1)
84
+        filename_label.x = 10
85
+        filename_label.y = 45 + count * 10
86
+        display.root_group.append(filename_label)
87
+        filename_labels.append(filename_label)
88
+        count+=1
89
+
90
+    while True:
91
+        key = keyb.scan()
92
+        if key == "ESC":
93
+            for lab in filename_labels:
94
+                display.root_group.remove(lab)
95
+            home(pos = 0)
96
+            return
97
+
98
+
99
+        elif key == "UP":
100
+            if fpos == 0:
101
+                pass
102
+            else:
103
+                filename_labels[fpos].text = files[fpos]
104
+                fpos -= 1
105
+            filename_labels[fpos].text = "> " + files[fpos]
106
+
107
+        elif key == "DOWN":
108
+            if fpos == len(files)-1:
109
+                pass
110
+            else:
111
+                filename_labels[fpos].text = files[fpos]
112
+                fpos += 1
113
+            filename_labels[fpos].text = "> " + files[fpos]
114
+
115
+        elif key == "\n":
116
+            file = "/sd/" + files[fpos]
117
+            for lab in filename_labels:
118
+                display.root_group.remove(lab)
119
+            file_label = label.Label(terminalio.FONT, text = "", color=0xffffff, scale=1)
120
+            file_label.x = 10
121
+            file_label.y = 45
122
+            display.root_group.append(file_label)
123
+
124
+            with open(file, "r") as f:
125
+                file_label.text = f.read()
126
+
127
+            while True:
128
+                key = keyb.scan()
129
+                if key == "ESC":
130
+                    display.root_group.remove(file_label)
131
+                    sd_card()
132
+                    return
133
+
134
+
135
+def tumblr():
136
+    title_label.text = "Tumblr"
137
+
138
+    while True:
139
+        key = keyb.scan()
140
+        if key == "ESC":
141
+            home(pos = 1)
142
+            return
143
+
144
+
145
+def web_server():
146
+    title_label.text = "Web Server"
147
+
148
+    while True:
149
+        key = keyb.scan()
150
+        if key == "ESC":
151
+            home(pos = 2)
152
+            return
153
+
154
+
155
+
156
+
157
+
158
+
159
+
160
+
161
+
162
+
163
+keyb = Keyboard()
164
+
165
+
166
+# SET UP DISPLAY & WIFI
167
+display = board.DISPLAY
168
+display_group = displayio.Group()
169
+display.root_group = display_group
170
+
171
+wifi_options = [[os.getenv("WIFI1"),os.getenv("WIFIPW1")],
172
+                [os.getenv("WIFI2"),os.getenv("WIFIPW2")],
173
+                [os.getenv("WIFI3"),os.getenv("WIFIPW3")]]
174
+
175
+
176
+h1 = label.Label(terminalio.FONT, text="Hello!", color=0xffffff, scale=2)
177
+h1.x = 10
178
+h1.y = 15
179
+
180
+count = 0
181
+menu_labs = []
182
+for option in wifi_options:
183
+    if count == 0:
184
+        menu_lab = label.Label(terminalio.FONT, text = "> " + option[0], color=0xffffff, scale=1)
185
+    else:
186
+        menu_lab = label.Label(terminalio.FONT, text = option[0], color=0xffffff, scale=1)
187
+    menu_lab.x = 10
188
+    menu_lab.y = 35 + count * 10
189
+    display.root_group.append(menu_lab)
190
+    menu_labs.append(menu_lab)
191
+    count+=1
192
+
193
+display.root_group.append(h1)
194
+
195
+pos = 0
196
+while True:
197
+    key = keyb.scan()
198
+    if key == "UP":
199
+        if pos == 0:
200
+            pass
201
+        else:
202
+            menu_labs[pos].text = wifi_options[pos][0]
203
+            pos -= 1
204
+        menu_labs[pos].text = "> " + wifi_options[pos][0]
205
+    elif key == "DOWN":
206
+        if pos == len(wifi_options)-1:
207
+            pass
208
+        else:
209
+            menu_labs[pos].text = wifi_options[pos][0]
210
+            pos += 1
211
+        menu_labs[pos].text = "> " + wifi_options[pos][0]
212
+    elif key == "\n":
213
+        WIFI_SSID = wifi_options[pos][0]
214
+        WIFI_PASS = wifi_options[pos][1]
215
+        break
216
+
217
+try:
218
+    for lab in menu_labs:
219
+        display.root_group.remove(lab)
220
+
221
+    txt = label.Label(terminalio.FONT, text = "", color=0x00ff00, scale=1)
222
+    txt.text = "Connecting to " + WIFI_SSID + "..."
223
+    txt.x = 10
224
+    txt.y = 35
225
+    display.root_group.append(txt)
226
+
227
+    wifi.radio.connect(ssid=WIFI_SSID,
228
+                    password=WIFI_PASS)
229
+    my_ip = str(wifi.radio.ipv4_address).strip()
230
+
231
+    display.root_group.remove(h1)
232
+    display.root_group.remove(txt)
233
+except ConnectionError as e:
234
+    h1.text = "Aw..."
235
+    
236
+    txt.text = "Could not connect to " + WIFI_SSID
237
+    txt.color = 0xff0000
238
+    txt.x = 10
239
+    txt.y = 35
240
+
241
+    txt2 = label.Label(terminalio.FONT, text = "", color=0x89a0a8, scale=1)
242
+    txt2.text = "Press CTRL + C followed by CTRL + D\nto soft-reboot"
243
+    txt2.x = 10
244
+    txt2.y = 100
245
+    
246
+    display.root_group.append(txt2)
247
+    
248
+    while True:
249
+        pass
250
+
251
+
252
+
253
+
254
+# SET UP SD CARD
255
+spi = busio.SPI(board.SD_SCK, MOSI=board.SD_MOSI, MISO=board.SD_MISO)
256
+cs = board.SD_CS
257
+
258
+try:
259
+    sdcard = sdcardio.SDCard(spi, cs)
260
+    vfs = storage.VfsFat(sdcard)
261
+
262
+    storage.mount(vfs, "/sd") # access files on sd card here
263
+except OSError:
264
+    pass # SD card not inserted/found
265
+
266
+
267
+
268
+
269
+# Header Bar
270
+header_bar = Rect(0, 0, 238, 30, fill=0x1f4476)
271
+display.root_group.append(header_bar)
272
+
273
+# Page Title
274
+title_label = label.Label(terminalio.FONT, text="Home", color=0xffffff, scale=2)
275
+display.root_group.append(title_label)
276
+
277
+# IP Label
278
+ip_label = label.Label(terminalio.FONT, text="0.0.0.0", color=0x89a0a8, scale=1)
279
+display.root_group.append(ip_label)
280
+
281
+home()
282
+
283
+