-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserialMonitor.py
83 lines (68 loc) · 1.83 KB
/
serialMonitor.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
import io
import time
import psutil
import signal
import sys
import serial
import Adafruit_Nokia_LCD as LCD
import Adafruit_GPIO.SPI as SPI
from PIL import Image
from PIL import ImageDraw
from PIL import ImageFont
# Raspberry Pi hardware SPI config:
DC = 23
RST = 24
SPI_PORT = 0
SPI_DEVICE = 0
# Hardware SPI usage:
disp = LCD.PCD8544(DC, RST, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE, max_speed_hz=4000000))
# Initialize library.
disp.begin(contrast=85)
disp.set_contrast(55)
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
image = Image.new('1', (LCD.LCDWIDTH, LCD.LCDHEIGHT))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
font = ImageFont.truetype('fonts/atomicsc.TTF', 6)
ser = serial.Serial('/dev/ttyAMA0', 9600, timeout=None)
class lcdprinter():
def __init__(self):
self.currline = 0
def println(self,printtext):
draw.text((1,6*self.currline), printtext, font=font);
self.currline = (self.currline+1) % 7;
textlcd = lcdprinter()
def signal_term_handler(signum = None, frame = None):
sys.stderr.write("Terminated.\n")
ser.close()
disp.clear()
disp.display()
sys.exit(0)
for sig in [signal.SIGTERM, signal.SIGINT, signal.SIGHUP, signal.SIGQUIT]:
signal.signal(sig, signal_term_handler)
#sio = io.TextIOWrapper(io.BufferedRWPair(ser, ser))
try:
draw.rectangle((0,0,83,47), outline=255, fill=255)
textlcd.println("waiting for serial...")
disp.image(image)
disp.display()
while 1:
# Clear image buffer.
if textlcd.currline == 0:
draw.rectangle((0,0,83,47), outline=255, fill=255)
serial_str = ser.readline()
if serial_str:
textlcd.println(serial_str)
# Display image.
disp.image(image)
disp.display()
time.sleep(1)
except KeyboardInterrupt:
ser.close()
# clear display.
disp.clear()
disp.display()