fix compile issue and refine display logic

This commit is contained in:
2019-06-02 20:05:39 +10:00
parent bc749b4147
commit 06e77f7ebf
+20 -12
View File
@@ -1,6 +1,7 @@
import usb.core
import usb.util
import time
import datetime
import RPi.GPIO as GPIO
import Adafruit_GPIO.SPI as SPI
import Adafruit_SSD1306
@@ -118,14 +119,15 @@ def draw_stats(disp, draw, image):
# Write two lines of text.
draw.text((x, top), "IP: " + str(IP), font=font, fill=255)
draw.text((x, top + 8), str(CPU), font=font, fill=255)
draw.text((x, top + 16), str(MemUsage), font=font, fill=255)
draw.text((x, top + 25), str(Disk), font=font, fill=255)
# draw.text((x, top + 8), str(CPU), font=font, fill=255)
draw.text((x, top + 19), str(MemUsage), font=font, fill=255)
draw.text((x, top + 38), str(Disk), font=font, fill=255)
draw.text((x, top + 47), str(datetime.datetime.now().time()), font=font, fill=255)
# Display image.
disp.image(image)
disp.display()
time.sleep(0.1)
time.sleep(0.5)
GPIO.setmode(GPIO.BCM)
@@ -135,6 +137,8 @@ GPIO.setup(ForwardLeft, GPIO.OUT)
GPIO.setup(BackwardLeft, GPIO.OUT)
disp, draw, image = init_display()
width = disp.width
height = disp.height
# First define some constants to allow easy resizing of shapes.
padding = -2
top = padding
@@ -154,43 +158,47 @@ if dev.is_kernel_driver_active(USB_IF) is True:
usb.util.claim_interface(dev, USB_IF)
draw_stats(disp, draw, image)
while True:
draw_stats(disp, draw, image)
control = None
try:
control = dev.read(
endpoint.bEndpointAddress, endpoint.wMaxPacketSize, USB_TIMEOUT
)
# print(control)
if control[0] == 0:
# print('clear')
clear()
except:
pass
if control is not None:
if BTN_DOWN in control:
if control[2] == BTN_DOWN:
print("-")
backward()
if BTN_UP in control:
if control[2] == BTN_UP:
print("+")
forward()
if BTN_LEFT in control:
if control[2] == BTN_LEFT:
print("<-")
turn_left()
if BTN_RIGHT in control:
if control[2] == BTN_RIGHT:
print("->")
turn_right()
if BTN_STOP in control:
if control[2] == BTN_STOP:
print("stop")
if BTN_EXIT in control:
if control[2] == BTN_EXIT:
clear()
exit()
if control[3] == 2:
print("fresh stats")
draw_stats(disp, draw, image)
continue
else:
# Let CTRL+C actually exit
clear()