This repository has been archived by the owner on Jan 18, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6
/
talalib.py
532 lines (444 loc) · 18.3 KB
/
talalib.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
import time
import serial
import Adafruit_SSD1306
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
import textwrap
import RPi.GPIO as GPIO
_FONT_LECO_PATH = "/opt/tala/leco1976.ttf"
_FONT_FREEPIXEL_PATH = "/opt/tala/FreePixel.ttf"
class Tala():
interrupt = False
def __init__(self):
# Setup Display
self.display = Adafruit_SSD1306.SSD1306_128_64(rst=24)
self.display.begin()
self.display.clear()
self.display.display()
self.width = self.display.width
self.height = self.display.height
# Setup Radio
self.radio = serial.Serial("/dev/serial0", baudrate=9600, timeout=1)
# Setup Keypad
GPIO.setmode(GPIO.BCM)
# Tell the Raspberry Pi that each row on the keypad is an output
GPIO.setup(26, GPIO.OUT) # row 1
GPIO.setup(19, GPIO.OUT) # row 2
GPIO.setup(13, GPIO.OUT) # row 3
GPIO.setup(6, GPIO.OUT) # row 4
# Tell the Raspberry Pi that each column on the keypad is an input and
# should have an (internal) pull down resistor.
GPIO.setup(17, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # col 1
GPIO.setup(27, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # col 2
GPIO.setup(22, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # col 3
# Turn keypad rows off.
GPIO.output(26, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
def keypad_row(self, row):
if row == 1:
GPIO.output(26, GPIO.HIGH)
GPIO.output(19, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
if row == 2:
GPIO.output(26, GPIO.LOW)
GPIO.output(19, GPIO.HIGH)
GPIO.output(13, GPIO.LOW)
GPIO.output(6, GPIO.LOW)
if row == 3:
GPIO.output(26, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(13, GPIO.HIGH)
GPIO.output(6, GPIO.LOW)
if row == 4:
GPIO.output(26, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
def singlebutton(self, wait=0.05, interrupt_bypass=False):
key = ""
# Loop while we haven't got a key
while key == "":
# Turn on row 1 but turn off every other row
self.keypad_row(1)
# If the first column is turned on we know that the first
# button has been pressed and so on.
if GPIO.input(17):
key = "1"
if GPIO.input(27):
key = "2"
if GPIO.input(22):
key = "3"
self.keypad_row(2)
if GPIO.input(17):
key = "4"
if GPIO.input(27):
key = "5"
if GPIO.input(22):
key = "6"
self.keypad_row(3)
if GPIO.input(17):
key = "7"
if GPIO.input(27):
key = "8"
if GPIO.input(22):
key = "9"
self.keypad_row(4)
if GPIO.input(17):
key = "*"
if GPIO.input(27):
key = "0"
if GPIO.input(22):
key = "#"
if self.interrupt and not interrupt_bypass:
print("Interrupting singlebutton(). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
time.sleep(wait)
return key
def type_numbers(self, wait=0.05, interrupt_bypass=False):
doloop = True
message = ""
key = ""
while doloop:
self.keypad_row(1)
if GPIO.input(17):
key = "1"
if GPIO.input(27):
key = "2"
if GPIO.input(22):
key = "3"
self.keypad_row(2)
if GPIO.input(17):
key = "4"
if GPIO.input(27):
key = "5"
if GPIO.input(22):
key = "6"
self.keypad_row(3)
if GPIO.input(17):
key = "7"
if GPIO.input(27):
key = "8"
if GPIO.input(22):
key = "9"
self.keypad_row(4)
if GPIO.input(17):
key = "*"
if GPIO.input(27):
key = "0"
if GPIO.input(22):
doloop = False
key = ""
message = message + key
if self.interrupt and not interrupt_bypass:
print("Interrupting type_numbers(). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
time.sleep(wait)
return message
def type(self, wait=0.5, interrupt_bypass=False):
# Make a new image/canvas with the width and height of the screen.
image = Image.new("1", (self.width, self.height))
# Make a new draw variable which draws shapes/text to the screen.
draw = ImageDraw.Draw(image)
# Define the fonts
bigfont = ImageFont.truetype(_FONT_LECO_PATH, 45)
font = ImageFont.truetype(_FONT_FREEPIXEL_PATH, 14)
# Define the characters to cycle for each key on the keypad
# Special:
# _ = Space
# <- = Backspace
keypadkeys = {
"1": ["1"],
"2": ["a", "b", "c", "2"],
"3": ["d", "e", "f", "3"],
"4": ["g", "h", "i", "4"],
"5": ["j", "k", "l", "5"],
"6": ["m", "n", "o", "6"],
"7": ["p", "q", "r", "s", "7"],
"8": ["t", "u", "v", "8"],
"9": ["w", "x", "y", "z", "9"],
"0": ["_", "0"],
"*": ["<-", ".", ",", "!", "?"]
}
doloop = True
message = ""
keybefore = ""
key = ""
currentletter = ""
while doloop:
key = ""
self.keypad_row(1)
if GPIO.input(17):
key = "1"
if GPIO.input(27):
key = "2"
if GPIO.input(22):
key = "3"
self.keypad_row(2)
if GPIO.input(17):
key = "4"
if GPIO.input(27):
key = "5"
if GPIO.input(22):
key = "6"
self.keypad_row(3)
if GPIO.input(17):
key = "7"
if GPIO.input(27):
key = "8"
if GPIO.input(22):
key = "9"
GPIO.output(26, GPIO.LOW)
GPIO.output(19, GPIO.LOW)
GPIO.output(13, GPIO.LOW)
GPIO.output(6, GPIO.HIGH)
if GPIO.input(17):
key = "*"
if GPIO.input(27):
key = "0"
if GPIO.input(22):
self.clear()
message = message + currentletter
doloop = False
if self.interrupt and not interrupt_bypass:
print("Interrupting type(). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
if key != "":
# If the key is the same as the key before (aka the button is
# being held down.)
if key == keybefore:
# Get the current index of the letter
index = keypadkeys[key].index(currentletter)
# Check if adding 1 to the key will make it go off the list
if (index+1) < len(keypadkeys[key]):
# Nope, add 1 to the key
currentletter = keypadkeys[key][index+1]
else:
# Yep, set the list to 0
currentletter = keypadkeys[key][0]
else:
# Key has not being held, so set the current letter to the
# first on the list of the button.
currentletter = keypadkeys[key][0]
# Clear the canvas by drawing a blank rectangle.
draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
# Get the width and height of the letter
w, h = draw.textsize(currentletter, font=bigfont)
# Draw the letter in the middle of the screen
draw.text(((self.width-w)/2, (self.height-h)/2), currentletter, font=bigfont, fill=255)
# Display the canvas on the screen.
if self.interrupt and not interrupt_bypass:
print("Interrupting type() (SCREEN DRAW). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
else:
self.display.image(image)
self.display.display()
keybefore = key
time.sleep(wait)
else:
if currentletter != "":
# Check for special keys
if currentletter != "<-":
if currentletter == "_":
currentletter = " "
message = message + currentletter
else:
message = message[:-1]
currentletter = ""
# if a key is not pressed show the text being typed
# clear the canvas by drawing a blank rectangle
draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
# make the message wrap (aka if the message goes off the screen
# make it start on a new line)
messagewrap = textwrap.wrap(message, width=17)
# get the height of a line
linewidth, lineheight = draw.textsize("test", font=font)
lines = 0
# draw each line
for i in range(0, len(messagewrap)):
draw.text((5, 5+((lineheight+2)*lines)), messagewrap[i], font=font, fill=255)
lines += 1
# display the canvas to the screen
if self.interrupt and not interrupt_bypass:
print("Interrupting type() (SCREEN DRAW). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
else:
self.display.image(image)
self.display.display()
keybefore = ""
time.sleep(0.1)
return message
def cleanup(self):
# clean up the raspberry pi's gpio
# ie turn off pins, etc..
GPIO.cleanup()
def yn(self, question, interrupt_bypass=False):
self.popup(body=question + "?")
while True:
# check for button presses
btn = self.singlebutton(interrupt_bypass=interrupt_bypass)
# if button * is pressed return yes
if btn == "#":
return True
# if # button is pressed return no
elif btn == "*":
return False
elif btn is None:
return None
def popup(self, title="", body="", interrupt_bypass=False):
# make the message wrap (aka if it goes off the screen make it start on
# a new line)
wrapbody = textwrap.wrap(body, width=17)
# make a new image
image = Image.new("1", (self.width, self.height))
draw = ImageDraw.Draw(image)
# define the fonts
titlefont = ImageFont.truetype(_FONT_LECO_PATH, 15)
font = ImageFont.truetype(_FONT_FREEPIXEL_PATH, 11)
# clear the canvas
draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
padding = 5
if title != "":
# get width and height of title
titlewidth, titleheight = draw.textsize(title, font=titlefont)
# draw a box to put the title in
draw.rectangle((0, 0, self.width, titleheight+10), outline=255, fill=255)
# draw the title text
draw.text((0+padding, 0+padding), title, font=titlefont, fill=0)
else:
titlewidth = 0
titleheight = 0
# get line width and height
linewidth, lineheight = draw.textsize("test", font=font)
lines = 0
# draw each line on the screen
for i in range(0, len(wrapbody)):
draw.text((0+padding, 0+((padding+padding) if title != "" else 0)+titleheight+padding+((lineheight+2)*lines)), wrapbody[i], font=font, fill=255)
lines += 1
if self.interrupt and not interrupt_bypass:
print("Interrupting popup(). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
return None
else:
# draw the canvas to the screen
self.display.image(image)
self.display.display()
def message(self, title, body, interrupt_bypass=False):
# make the message wrap (aka if it goes off the screen make it start on
# a new line)
wrapbody = textwrap.wrap(body, width=17)
# make a new image
image = Image.new("1", (self.width, self.height))
draw = ImageDraw.Draw(image)
# define the fonts
titlefont = ImageFont.truetype(_FONT_LECO_PATH, 15)
font = ImageFont.truetype(_FONT_FREEPIXEL_PATH, 11)
startline = 0
while True:
# clear the canvas
draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
# get width and height of title
titlewidth, titleheight = draw.textsize(title, font=titlefont)
# draw a box to put the title in
draw.rectangle((0, 0, self.width, titleheight+10), outline=255, fill=255)
padding = 5
# draw the title text
draw.text((0+padding, 0+padding), title, font=titlefont, fill=0)
# get line width and height
linewidth, lineheight = draw.textsize("test", font=font)
lines = 0
# draw each line on the screen
for i in range(startline, len(wrapbody)):
draw.text((0+padding, 0+padding+titleheight+padding+padding+((lineheight+2)*lines)), wrapbody[i], font=font, fill=255)
lines += 1
if self.interrupt and not interrupt_bypass:
print("Interrupting message() (SCREEN DRAW). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
else:
# draw the canvas to the screen
self.display.image(image)
self.display.display()
# check for button presses
btn = self.singlebutton(interrupt_bypass=interrupt_bypass)
# if button 2 is pressed scroll up
if btn == "2":
if startline > 0:
startline = startline - 1
# if button 8 is pressed scroll down
elif btn == "8":
if startline < (len(wrapbody) - 1):
startline = startline + 1
# if # button is pressed dismiss the message
elif btn == "#":
return
elif btn is None:
return None
def menu(self, items, interrupt_bypass=False):
if len(items) < 2:
raise ValueError("Items list given must contain 2 or more items!")
# make a canvas
image = Image.new("1", (self.width, self.height))
draw = ImageDraw.Draw(image)
# clear the canvas
draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
# define font
font = ImageFont.truetype(_FONT_LECO_PATH, 15)
selected = 0
while True:
# clear the screen
draw.rectangle((0, 0, self.width, self.height), outline=0, fill=0)
# if the first item is selected
if selected == 0:
self.draw_rectangle(draw, 0, 0, 32, 255, 255, 0, selected, items, 0, font)
self.draw_rectangle(draw, 0, 32, 64, 0, 0, 255, selected, items, 1, font)
elif selected == (len(items)-1):
self.draw_rectangle(draw, 0, 0, 32, 0, 0, 255, selected, items, -1, font)
self.draw_rectangle(draw, 0, 32, 64, 255, 255, 0, selected, items, 0, font)
else:
self.draw_rectangle(draw, 0, -16, 16, 0, 0, 255, selected, items, -1, font)
self.draw_rectangle(draw, 0, 16, 48, 255, 255, 0, selected, items, 0, font)
self.draw_rectangle(draw, 0, 48, 80, 0, 0, 255, selected, items, 1, font)
if self.interrupt and not interrupt_bypass:
print("Interrupting menu() (SCREEN DRAW). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
else:
self.display.image(image)
self.display.display()
btn = self.singlebutton(interrupt_bypass=interrupt_bypass)
if btn == "2":
if selected > 0:
selected = selected - 1
elif btn == "8":
if selected < (len(items)-1):
selected = selected + 1
elif btn == "#":
return items[selected]
elif btn is None:
return None
def draw_rectangle(self, draw, x, y, h, o, f1, f2, selected, items, s, font):
draw.rectangle((x, y, self.width, h), outline=o, fill=f1)
tw, th = draw.textsize(items[selected+s], font=font)
padding = (32-th)/2
draw.text((x+padding, y+padding), items[selected+s], font=font, fill=f2)
def clear(self, interrupt_bypass=False):
if self.interrupt and not interrupt_bypass:
print("Interrupting clear() (SCREEN DRAW). Interrupt is " + str(self.interrupt) + ", Bypass is " + str(interrupt_bypass))
return None
else:
self.display.clear()
self.display.display()
def send(self, message):
message = message.encode()
self.radio.write(message)
def waitForReceive(self):
while True:
response = self.radio.readline().decode("utf-8")
if not response == "":
return response
def receive(self):
return self.radio.readline().decode("utf-8")
def closeRadio(self):
self.radio.close()