Created
October 7, 2021 13:09
-
-
Save WillemJan/24c9a2d1d43c997ea43babff8b81aa87 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/env python3 | |
import contextlib # stackoverflow.com/q/51464455 | |
with contextlib.redirect_stdout(None): | |
import pygame | |
import os | |
import json | |
import time | |
import serial | |
import board | |
import adafruit_ws2801 | |
""" | |
pi@shelf:~ $ xrandr | head -1 | |
Screen 0: minimum 320 x 200, current 1680 x 1050, maximum 2048 x 2048 | |
# apt install -y mbrola-nl3 espeak python3-pygame | |
# pip3 install adafruit-circuitpython-ws2801 | |
""" | |
SCREEN_WIDTH = 1680 | |
SCREEN_HEIGHT = 1050 | |
SENSOR_LEFT = "/dev/ttyUSB0" | |
SENSOR_RIGHT = "/dev/ttyUSB1" | |
SENSOR_READS = 4 | |
DEBUG = True | |
pygame.init() | |
screen = pygame.display.set_mode((1680, 1050), pygame.FULLSCREEN) | |
font = pygame.font.SysFont("Verdana", 50) | |
gimp = pygame.image.load("gimp.jpg") | |
gimp.convert() | |
with open("bl.json") as booklist_file: | |
bookshelf_contents = json.loads(booklist_file.read()) | |
left_c = right_c = -1 | |
book_align = [] | |
pygame.display.flip() | |
def draw_metadata(book): | |
screen.fill((0, 0, 0)) | |
img = font.render("Title:", True, (200, 200, 200)) | |
screen.blit(img, ((SCREEN_WIDTH / 9), 120)) | |
img = font.render(book["Title"].split(" - ")[0], True, (100, 200, 100)) | |
screen.blit(img, ((SCREEN_WIDTH / 4), 120)) | |
offset = 50 | |
if book["Title"].find(" - ") > -1: | |
title = book["Title"] | |
if len(title.split(" - ")[1]) > 0: | |
img = font.render(" - ".join(title.split(" - ")[1:]), True, (100, 200, 100)) | |
screen.blit(img, ((SCREEN_WIDTH / 4), 120 + offset)) | |
offset += 50 | |
img = font.render("Year:", True, (200, 200, 200)) | |
screen.blit(img, ((SCREEN_WIDTH / 9), 120 + offset)) | |
img = font.render(book["Year"].split(" - ")[0], True, (100, 200, 100)) | |
screen.blit(img, ((SCREEN_WIDTH / 4), 120 + offset)) | |
offset += 50 | |
img = font.render("Author(s):", True, (200, 200, 200)) | |
screen.blit(img, ((SCREEN_WIDTH / 9), 120 + offset)) | |
for author in book["Authors"]: | |
img = font.render(author, True, (100, 200, 100)) | |
screen.blit(img, ((SCREEN_WIDTH / 4), 120 + offset)) | |
offset += 50 | |
if book["Publisher"]: | |
img = font.render("Publisher:", True, (200, 200, 200)) | |
screen.blit(img, ((SCREEN_WIDTH / 9), 120 + offset)) | |
img = font.render(book["Publisher"].split(" - ")[0], True, (100, 200, 100)) | |
screen.blit(img, ((SCREEN_WIDTH / 4), 120 + offset)) | |
offset += 50 | |
pygame.display.flip() | |
if book["Language"] == "nl": | |
ESPEAK = 'espeak -a 400 -v nl+f1 "%s" --stdout | aplay' | |
os.system(ESPEAK % book["Title"]) | |
else: | |
ESPEAK = 'espeak -a 400 -v en+f1 "%s" --stdout | aplay' | |
os.system(ESPEAK % book["Title"]) | |
book_detected = False | |
old_book = {} | |
unstable = 0 | |
while True: | |
left_all = [] | |
right_all = [] | |
ser_left = serial.Serial(SENSOR_LEFT, 115200) | |
ser_right = serial.Serial(SENSOR_RIGHT, 115200) | |
for i in range(SENSOR_READS): | |
try: | |
left = int(ser_left.readline().decode("utf-8").strip().split()[1]) | |
left_all.append(left) | |
except: | |
pass | |
try: | |
right = int(ser_right.readline().decode("utf-8").strip().split()[1]) | |
right_all.append(right) | |
except: | |
pass | |
ser_left.close() | |
ser_right.close() | |
left_stable = all(element == left_all[0] for element in left_all) | |
right_stable = all(element == right_all[0] for element in right_all) | |
if DEBUG: | |
print("Left reading:", left_all, "Stable:", left_stable, | |
"\nRight reading:", right_all, "Stable:", right_stable, | |
"\nUnstable", unstable) | |
if right_stable or left_stable and len(left_all) + len(right_all) == SENSOR_READS * 2: | |
for book in bookshelf_contents: | |
if book.get("pos") == [left, right]: | |
if old_book == book: | |
draw_metadata(book) | |
old_book = book | |
break | |
if left_stable: | |
if book.get('pos')[0] == left: | |
if old_book == book: | |
draw_metadata(book) | |
old_book = book | |
break | |
if right_stable: | |
if book.get('pos')[1] == right: | |
if old_book == book: | |
draw_metadata(book) | |
old_book = book | |
break | |
else: | |
if not left_stable and not right_stable: | |
unstable += 1 | |
if unstable > 10: | |
screen.fill((0, 0, 0)) | |
pygame.display.flip() | |
unstable = 0 | |
old_book = {} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment