Last active
December 10, 2022 08:34
-
-
Save nio101/2930119ff20826c1d80c13ee245ad097 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
#!/usr/bin/python3 | |
# -*- coding:utf-8 -*- | |
import sys | |
import os | |
picdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'pic') | |
libdir = os.path.join(os.path.dirname(os.path.dirname(os.path.realpath(__file__))), 'lib') | |
if os.path.exists(libdir): | |
sys.path.append(libdir) | |
import logging | |
import time | |
import traceback | |
from waveshare_OLED import OLED_0in91 | |
from PIL import Image,ImageDraw,ImageFont | |
logging.basicConfig(level=logging.DEBUG) | |
from subprocess import check_output | |
import re | |
import subprocess | |
try: | |
disp = OLED_0in91.OLED_0in91() | |
logging.info("\r 0.91inch OLED Module ") | |
# Initialize library. | |
disp.Init() | |
# Clear display. | |
logging.info("clear display") | |
disp.clear() | |
logging.info ("***draw image") | |
Himage2 = Image.new('1', (disp.width, disp.height), 255) # 255: clear the frame | |
bmp = Image.open(os.path.join(picdir, 'flexion.bmp')) | |
Himage2.paste(bmp, (1,0)) | |
Himage2=Himage2.rotate(0) | |
disp.ShowImage(disp.getbuffer(Himage2)) | |
time.sleep(3) | |
def show_lier(): | |
logging.info ("***draw image") | |
Himage2 = Image.new('1', (disp.width, disp.height), 255) # 255: clear the frame | |
bmp = Image.open(os.path.join(picdir, 'lier.bmp')) | |
Himage2.paste(bmp, (1,0)) | |
Himage2=Himage2.rotate(0) | |
disp.ShowImage(disp.getbuffer(Himage2)) | |
time.sleep(3) | |
return | |
def show_jeu(): | |
logging.info ("***draw image") | |
Himage2 = Image.new('1', (disp.width, disp.height), 255) # 255: clear the frame | |
bmp = Image.open(os.path.join(picdir, 'jeu.bmp')) | |
Himage2.paste(bmp, (1,0)) | |
Himage2=Himage2.rotate(0) | |
disp.ShowImage(disp.getbuffer(Himage2)) | |
time.sleep(3) | |
return | |
# go on only if wifi is up | |
wifi_ip = None | |
while wifi_ip is None : | |
wifi_ip = check_output(['hostname', '-I']) | |
if wifi_ip is None: | |
time.sleep(3) | |
show_lier() | |
# switch accordingly to printer connnection | |
state = False | |
while True: | |
device_re = re.compile(b"Bus\s+(?P<bus>\d+)\s+Device\s+(?P<device>\d+).+ID\s(?P<id>\w+:\w+)\s(?P<tag>.+)$", re.I) | |
df = subprocess.check_output("lsusb") | |
devices = [] | |
for i in df.split(b'\n'): | |
if i: | |
info = device_re.match(i) | |
if info: | |
dinfo = info.groupdict() | |
dinfo['device'] = '/dev/bus/usb/%s/%s' % (dinfo.pop('bus'), dinfo.pop('device')) | |
devices.append(dinfo) | |
#print(devices) | |
found = False | |
for device in devices: | |
if device['tag'] == b'HP, Inc LaserJet 1018': | |
found = True | |
if found: | |
#print("FOUND!!!") | |
if state is False: | |
state = True | |
show_jeu() | |
else: | |
#print("NOT found!") | |
if state is True: | |
state = False | |
show_lier() | |
time.sleep(5) | |
except IOError as e: | |
logging.info(e) | |
except KeyboardInterrupt: | |
logging.info("ctrl + c:") | |
OLED_0in91.config.module_exit() | |
exit() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment