-
Notifications
You must be signed in to change notification settings - Fork 18
/
lcdprint.py
92 lines (73 loc) · 2.53 KB
/
lcdprint.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
import sys
from pydos_hw import Pydos_hw
try:
from pydos_ui import input
except ImportError:
pass
if sys.implementation.name.upper() == "CIRCUITPYTHON":
from circuitpython_i2c_lcd import I2cLcd
else:
import lcd2004
def lcdPrint(passedIn):
# The PCF8574 has a jumper selectable address: 0x20 - 0x27
DEFAULT_I2C_ADDR = 0x27
foundLCD = False
mess = passedIn
if mess == "":
mess = input("Say what?: ")
i2c = None
for i2cNo in range(3):
try:
i2c = Pydos_hw.I2C(i2cNo)
except:
pass
if i2c:
if sys.implementation.name.upper() == "CIRCUITPYTHON":
# circuitpython seems to require locking the i2c bus
while not i2c.try_lock():
pass
if DEFAULT_I2C_ADDR in i2c.scan():
foundLCD = True
break
if sys.implementation.name.upper() == 'CIRCUITPYTHON':
i2c.unlock()
if not foundLCD:
print("LCD not found at address: ",hex(DEFAULT_I2C_ADDR))
return
if sys.implementation.name.upper() == "CIRCUITPYTHON":
# 2 lines, 16 characters per line
lcd = I2cLcd(i2c, DEFAULT_I2C_ADDR, 2, 16)
# smiley faces as custom characters
happy = bytearray([0x00,0x0A,0x00,0x04,0x00,0x11,0x0E,0x00])
heart = bytearray([0x00,0x00,0x0A,0X15,0X11,0X0A,0X04,0X00])
grin = bytearray([0x00,0x00,0x0A,0x00,0x1F,0x11,0x0E,0x00])
lcd.custom_char(0, happy)
lcd.custom_char(1, heart)
lcd.custom_char(2, grin)
else:
lcd=lcd2004.lcd(DEFAULT_I2C_ADDR,i2c)
lcd.lcd_backlight(True)
lcd.lcd_clear()
if mess == "":
if sys.implementation.name.upper() == "CIRCUITPYTHON":
lcd.backlight_off()
else:
lcd.lcd_backlight(False)
else:
if sys.implementation.name.upper() == "CIRCUITPYTHON":
mess = mess.replace("<3",chr(1))
mess = mess.replace(":)",chr(0))
mess = mess.replace("(:",chr(0))
mess = mess.replace(":-)",chr(0))
mess = mess.replace("(-:",chr(0))
mess = mess.replace(":D",chr(2))
lcd.move_to(0,0)
lcd.putstr(mess)
else:
lcd.lcd_print(mess,1,0)
if sys.implementation.name.upper() == "CIRCUITPYTHON":
i2c.unlock()
#i2c.deinit()
if __name__ != "PyDOS":
passedIn = ""
lcdPrint(passedIn)