Created
May 3, 2020 06:06
-
-
Save gudongfeng/f08c4c9907f95b99f0f5a80791609d1b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import sys # import sys | |
import json | |
import time | |
import os | |
picdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'pic') | |
libdir = os.path.join(os.path.dirname(os.path.realpath(__file__)), 'lib') | |
if os.path.exists(libdir): | |
sys.path.append(libdir) | |
from watchdog.observers import Observer | |
from watchdog.events import PatternMatchingEventHandler | |
from signal import pause | |
from waveshare_epd import epd2in7 # import the display drivers | |
from PIL import Image,ImageDraw,ImageFont # import the image libraries | |
import time | |
from gpiozero import Button # import the Button control from gpiozero | |
btn1 = Button(5) # assign each button to a variable | |
btn2 = Button(6) # by passing in the pin number | |
btn3 = Button(13) # associated with the button | |
btn4 = Button(19) # | |
epd = epd2in7.EPD() # get the display object and assing to epd | |
epd.init() # initialize the display | |
print("Clear...") # print message to console (not display) for debugging | |
epd.Clear(0xFF) # clear the display | |
deviceInfoFilePath = '/tmp/devicelist.json' | |
deviceInfo = {} | |
selectionNum = 0 | |
class DeviceFileChange(PatternMatchingEventHandler): | |
patterns=["*.json"] | |
def on_modified(self, event): | |
for attempt in range(3): | |
try: | |
with open(deviceInfoFilePath) as f: | |
newDeviceInfo = json.load(f) | |
current = json.dumps(newDeviceInfo, sort_keys=True, indent=2) | |
new = json.dumps(deviceInfo, sort_keys=True, indent=2) | |
if (current != new): | |
deviceInfo = newDeviceInfo | |
printToDisplay() | |
except: | |
time.sleep(0.5) | |
else: | |
break | |
# Listen to the file change, refresh the display accordingly. | |
observer = Observer() | |
observer.schedule(DeviceFileChange(), '/tmp/', recursive=False) | |
observer.start() | |
# Print device object to the screen | |
def printToDisplay(): | |
LBlackImage = Image.new('1', (epd2in7.EPD_WIDTH, epd2in7.EPD_HEIGHT), 255) | |
draw = ImageDraw.Draw(LBlackImage) | |
font = ImageFont.truetype(os.path.join(picdir, 'Font.ttc'), 18) | |
# Print the selection box | |
draw.rectangle((1, 1 + selectionNum*20, 19, 19 + selectionNum*20), outline = 0) | |
n = 0 | |
for device in deviceInfo.values(): | |
if (device['status']): | |
# This device has been turned on | |
draw.chord((5, 5 + n*20, 15, 15 + n*20), 0, 360, fill = 0) | |
draw.text((20, n*20), device['name'], font = font, fill = 0) | |
n += 1 | |
epd.display(epd.getbuffer(LBlackImage)) | |
def writeToFile(): | |
with open(deviceInfoFilePath, 'w') as f: | |
json.dump(deviceInfo, f) | |
def moveup(btn): | |
global selectionNum | |
if selectionNum < 12: | |
selectionNum += 1 | |
printToDisplay() | |
def movedown(btn): | |
global selectionNum | |
if selectionNum > 0: | |
selectionNum -= 1 | |
printToDisplay() | |
def turnOn(btn): | |
global deviceInfo | |
key = deviceInfo.keys()[selectionNum] | |
if(not deviceInfo[key]['status']): | |
deviceInfo[key]['status'] = True | |
printToDisplay() | |
writeToFile() | |
def turnOff(btn): | |
global deviceInfo | |
key = deviceInfo.keys()[selectionNum] | |
if(deviceInfo[key]['status']): | |
deviceInfo[key]['status'] = False | |
printToDisplay() | |
writeToFile() | |
# Initialize the screen | |
for attempt in range(3): | |
try: | |
with open(deviceInfoFilePath) as f: | |
deviceInfo = json.load(f) | |
printToDisplay() | |
except: | |
time.sleep(2) | |
else: | |
break; | |
# tell the button what to do when pressed | |
btn1.when_pressed = movedown | |
btn2.when_pressed = moveup | |
btn3.when_pressed = turnOn | |
btn4.when_pressed = turnOff | |
pause() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment