Skip to content

Instantly share code, notes, and snippets.

@WillemJan
Created October 6, 2021 07:38
Show Gist options
  • Save WillemJan/1fb88f96bdcfa779284a10109c7a00e8 to your computer and use it in GitHub Desktop.
Save WillemJan/1fb88f96bdcfa779284a10109c7a00e8 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python3
import contextlib # stackoverflow.com/q/51464455
with contextlib.redirect_stdout(None):
import pygame
import json
import time
import serial
import requests
import io
import RPi.GPIO as GPIO
import Adafruit_WS2801
import Adafruit_GPIO.SPI as SPI
# Configure the count of pixels:
PIXEL_COUNT = 23
# Alternatively specify a hardware SPI connection on /dev/spidev0.0:
SPI_PORT = 0
SPI_DEVICE = 0
# Setup ledstrip
pixels = Adafruit_WS2801.WS2801Pixels(PIXEL_COUNT, spi=SPI.SpiDev(SPI_PORT, SPI_DEVICE), gpio=GPIO)
pixels.clear()
pixels.show()
# pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color( r, g, b ))
'''
pi@shelf:~ $ xrandr | head -1
Screen 0: minimum 320 x 200, current 1680 x 1050, maximum 2048 x 2048
'''
SCREEN_WIDTH = 1680
SCREEN_HEIGHT = 1050
SENSOR_LEFT = "/dev/ttyUSB0"
SENSOR_RIGHT = "/dev/ttyUSB1"
pygame.init()
screen = pygame.display.set_mode((1680, 1050), pygame.FULLSCREEN)
with open('booklist.json') as booklist_file:
bookshelf_contents = json.loads(booklist_file.read())
font = pygame.font.SysFont('Verdana', 50)
book_align = []
old_left = 66
old_right = 0
for book in bookshelf_contents:
screen.fill((0, 0, 0))
img = font.render("Calibration time..", True, (200, 200, 200))
screen.blit(img, (40, 100))
img = font.render("Please fetch: ", True, (200, 200, 200))
screen.blit(img, (40, 100 + img.get_height() + 4))
if not book.get('Title'):
continue
img = font.render(book['Title'].split(' - ')[0], True, (100,200,100))
screen.blit(img, ((SCREEN_WIDTH /3), 120))
img = font.render(book['Year'].split(' - ')[0], True, (100,200,100))
screen.blit(img, ((SCREEN_WIDTH /3), 170))
img = font.render(book['Publisher'].split(' - ')[0], True, (100,200,100))
screen.blit(img, ((SCREEN_WIDTH /3), 220))
pygame.display.flip()
pixels.clear()
pixels.show()
time.sleep(3)
sensor_left = serial.Serial(SENSOR_LEFT, 115200)
sensor_right = serial.Serial(SENSOR_RIGHT, 115200)
for i in range(4):
pixels.set_pixel(22 - i, Adafruit_WS2801.RGB_to_color(i, 200 - (i * 40), i))
for i in range(4):
pixels.set_pixel(i, Adafruit_WS2801.RGB_to_color(i, 40 + i * 40, i))
pixels.show()
done = False
while not done:
left_all = []
right_all = []
for i in range(8):
try:
left = int(sensor_left.readline().decode("utf-8").strip().split()[1])
right = int(sensor_right.readline().decode("utf-8").strip().split()[1])
if left <= old_left:
left_all.append(left)
if right >= old_right:
right_all.append(right)
print(right, old_right)
except:
pass
if len(left_all) + len(right_all) == 16:
left_all = all(element == left_all[0] for element in left_all)
right_all = all(element == right_all[0] for element in right_all)
if left_all and right_all:
done = True
old_left = left
old_right = right
book['pos'] = (left, right)
book_align.append(book)
with open('booklist_alignd.json', 'w') as fh:
fh.write(json.dumps(book_align))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment