302 lines
9.2 KiB
Python
302 lines
9.2 KiB
Python
#!/usr/bin/python
|
|
# -*- coding:utf-8 -*-
|
|
|
|
# This is the main program of the EBB: E-Paper Bulletin Board. More info at https://git.laboratoryb.org/trav/ebb
|
|
#
|
|
# carousel iterates through all posts in posts.json
|
|
|
|
import epd4in2
|
|
import traceback
|
|
import time
|
|
import signal
|
|
import uuid
|
|
import subprocess
|
|
import buttonshim
|
|
import addtoDB
|
|
import refreshdb
|
|
import ConfigParser
|
|
from tinydb import TinyDB, Query
|
|
from PIL import Image,ImageDraw,ImageFont
|
|
|
|
## INITIALIZE ##
|
|
|
|
#Let the user know we're booting: with a PURPLE led
|
|
buttonshim.set_pixel(0xFF, 0x00, 0xFF)
|
|
|
|
#intervalTime is how many seconds before moving to next image (300 = 5 minutes)
|
|
#sync time is how many seconds before refreshing the DB from SSB
|
|
#this should be moved into the config file...
|
|
intervalTime = 1800
|
|
syncTime = 3600
|
|
#keep track of where we are moving through intervalTime, syncTime and where we are in the db, and what flier we're currently displaying
|
|
timeIndex = intervalTime
|
|
syncIndex = 0
|
|
dbIndex = 0
|
|
flierPath = "noneyet"
|
|
button_flag = "null"
|
|
|
|
#pull some vals from the config
|
|
configParser = ConfigParser.RawConfigParser()
|
|
configFilePath = r'config.txt'
|
|
configParser.read(configFilePath)
|
|
imagesPath = configParser.get('ebb-config', 'imagesPath')
|
|
dbPath = configParser.get('ebb-config', 'dbPath')
|
|
|
|
#initialize db and our place going through the db. We want dbCount instead of len(db) because len(db) doesn't account for the vacant holes in the db that I can't figure out how to get rid of...
|
|
db = TinyDB(dbPath)
|
|
howmany = Query()
|
|
dbCount = 0
|
|
for item in db:
|
|
dbCount = item.doc_id
|
|
print dbCount
|
|
|
|
#initialize display
|
|
#PIL.Image.new(mode, size, color=0)
|
|
Limage = Image.new('1', (epd4in2.EPD_HEIGHT, epd4in2.EPD_WIDTH), 255) # 255: clear the frame
|
|
try:
|
|
epd = epd4in2.EPD()
|
|
epd.init()
|
|
except:
|
|
print 'traceback.format_exc():\n%s' % traceback.format_exc()
|
|
exit()
|
|
|
|
|
|
## button press contingencies ##
|
|
@buttonshim.on_press(buttonshim.BUTTON_A)
|
|
def button_a(button, pressed):
|
|
global button_flag
|
|
buttonshim.set_pixel(0xFF, 0x00, 0x00)
|
|
button_flag = "button_1"
|
|
|
|
|
|
@buttonshim.on_press(buttonshim.BUTTON_B)
|
|
def button_b(button, pressed):
|
|
global button_flag
|
|
buttonshim.set_pixel(0x00, 0x00, 0xFF)
|
|
button_flag = "button_2"
|
|
|
|
|
|
@buttonshim.on_press(buttonshim.BUTTON_C)
|
|
def button_c(button, pressed):
|
|
global button_flag
|
|
buttonshim.set_pixel(0x00, 0x00, 0xFF)
|
|
button_flag = "button_3"
|
|
|
|
@buttonshim.on_press(buttonshim.BUTTON_D)
|
|
def button_d(button, pressed):
|
|
global button_flag
|
|
buttonshim.set_pixel(0xFF, 0x00, 0x00)
|
|
button_flag = "button_4"
|
|
|
|
@buttonshim.on_press(buttonshim.BUTTON_E)
|
|
def button_e(button, pressed):
|
|
global button_flag
|
|
buttonshim.set_pixel(0x00, 0x00, 0xFF)
|
|
button_flag = "button_5"
|
|
|
|
|
|
## MAIN LOOP ##
|
|
while True:
|
|
# chill for a bit, keep track of how long we're chilling
|
|
time.sleep(2)
|
|
print "time:", timeIndex, "/", intervalTime, "index", dbIndex, "/", dbCount, "sync time:", syncIndex, "/", syncTime
|
|
|
|
#iterate through syncTime and image time
|
|
syncIndex+=2
|
|
timeIndex+=2
|
|
|
|
#time to sync db with ssb
|
|
if syncIndex >= syncTime:
|
|
#light up red while syncing db
|
|
buttonshim.set_pixel(0xFF, 0x00, 0x00)
|
|
refreshdb.fresh()
|
|
#db count may have changed
|
|
for item in db:
|
|
dbCount = item.doc_id
|
|
syncIndex = 0
|
|
buttonshim.set_pixel(0x00, 0x00, 0x00)
|
|
|
|
#time to iterate through images
|
|
if timeIndex >= intervalTime:
|
|
#reset the index
|
|
timeIndex = 0
|
|
|
|
# iterate where we are in the db
|
|
dbIndex+=1
|
|
|
|
# if dbIndex is now out of range, reset to beginning (skipping 1 because that's the boot image)
|
|
if dbIndex > dbCount:
|
|
dbIndex=2
|
|
|
|
#grab the entry at dbIndex, newFlier is a dict
|
|
print ("getting db at")
|
|
print (dbIndex)
|
|
newFlier = db.get(doc_id=dbIndex)
|
|
|
|
#if that's an empty spot in the db we gotta keep lookin
|
|
while newFlier == None:
|
|
dbIndex+=1
|
|
newFlier = db.get(doc_id=dbIndex)
|
|
|
|
flierPath = newFlier["path"]
|
|
|
|
#display the images
|
|
try:
|
|
epd.init()
|
|
#Limage = Image.new('1', (epd4in2.EPD_HEIGHT, epd4in2.EPD_WIDTH), 255) # 255: clear the frame
|
|
bmp = Image.open(flierPath)
|
|
Limage.paste(bmp)
|
|
epd.display(epd.getbuffer(Limage))
|
|
time.sleep(2)
|
|
timeIndex+=2
|
|
epd.sleep()
|
|
|
|
except:
|
|
print 'traceback.format_exc():\n%s' % traceback.format_exc()
|
|
exit()
|
|
#in case the LED was on because we're buttoning through images
|
|
buttonshim.set_pixel(0x00, 0x00, 0x00)
|
|
|
|
#take pic
|
|
if button_flag == "button_1":
|
|
#generate unique file_name
|
|
unique = uuid.uuid4()
|
|
unique = str(unique)
|
|
jpgpath = imagesPath + unique + '.jpg'
|
|
|
|
#take photo
|
|
try:
|
|
result = subprocess.call(['raspistill', '-o', jpgpath, '-vf', '-w', '300', '-h', '400', '-t', '1000'])
|
|
except:
|
|
print 'traceback.format_exc():\n%s' % traceback.format_exc()
|
|
exit()
|
|
|
|
#generate unique bmp name
|
|
bmpath = imagesPath + str(unique) + '.bmp'
|
|
|
|
#convert to bmp
|
|
im = Image.open(jpgpath)
|
|
im = im.convert("1")
|
|
im.save(bmpath)
|
|
|
|
#add that file to the db. 0 is because this isn't coming from SSB, it's local
|
|
addtoDB.addFile(bmpath,dbPath,0)
|
|
#update dbCount
|
|
for item in db:
|
|
dbCount = item.doc_id
|
|
|
|
#display the image
|
|
epd.init()
|
|
Limage2 = Image.new('1', (epd4in2.EPD_HEIGHT, epd4in2.EPD_WIDTH), 255) # 255: clear the frame
|
|
bmp = Image.open(bmpath)
|
|
Limage.paste(bmp)
|
|
epd.display(epd.getbuffer(Limage))
|
|
#ok green done
|
|
buttonshim.set_pixel(0x00, 0x00, 0x00)
|
|
time.sleep(3)
|
|
timeIndex+=2
|
|
epd.sleep()
|
|
button_flag = "null"
|
|
|
|
|
|
|
|
#move to the next image
|
|
elif button_flag == "button_2":
|
|
print ("next image")
|
|
timeIndex = intervalTime
|
|
button_flag = "null"
|
|
|
|
|
|
#go back to the previous image
|
|
elif button_flag == "button_3":
|
|
print ("previous image")
|
|
#we go back 2 because we have to account for calling timeIndex = intervalTime iterates forward by 1
|
|
dbIndex-=2
|
|
if dbIndex < 2:
|
|
dbIndex = dbCount-1
|
|
timeIndex = intervalTime
|
|
button_flag = "null"
|
|
|
|
|
|
#delete current image
|
|
elif button_flag == "button_4":
|
|
button_flag = "null"
|
|
|
|
#can't delete the default image
|
|
if dbIndex != 1:
|
|
|
|
#CONFIRM WITH USER THEY WANT TO DELETE
|
|
epd.init()
|
|
#Limage = Image.new('1', (epd4in2.EPD_HEIGHT, epd4in2.EPD_WIDTH), 255) # 255: clear the frame
|
|
draw = ImageDraw.Draw(Limage)
|
|
font18 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 24)
|
|
draw.text((2, 0), 'Are you sure', font = font18, fill = 255)
|
|
draw.text((2, 40), 'you want to delete', font = font18, fill = 255)
|
|
draw.text((2, 80), 'the current image?', font = font18, fill = 255)
|
|
draw.text((2, 120), 'Press button 4', font = font18, fill = 255)
|
|
draw.text((2, 160), 'again to confirm.', font = font18, fill = 255)
|
|
draw.text((2, 200), 'Press any other', font = font18, fill = 255)
|
|
draw.text((2, 240), 'button for no.', font = font18, fill = 255)
|
|
epd.display(epd.getbuffer(Limage))
|
|
epd.sleep()
|
|
|
|
|
|
while button_flag == "null":
|
|
buttonshim.set_pixel(0xFF, 0x00, 0x00)
|
|
time.sleep(.1)
|
|
buttonshim.set_pixel(0x00, 0x00, 0x00)
|
|
time.sleep(.05)
|
|
|
|
#sounds like delete time
|
|
if button_flag == "button_4":
|
|
Fruit = Query()
|
|
#results = db.search(Fruit.path == flierPath)
|
|
db.remove(Fruit.path == flierPath)
|
|
#update dbCount
|
|
for item in db:
|
|
dbCount = item.doc_id
|
|
#ok so it's deleted, let's force display of next image
|
|
timeIndex = intervalTime
|
|
else:
|
|
#stay on same image and refresh to get rid of delete text
|
|
dbIndex-=1
|
|
timeIndex = intervalTime
|
|
|
|
button_flag = "null"
|
|
buttonshim.set_pixel(0x00, 0x00, 0xFF)
|
|
|
|
#weather button!
|
|
elif button_flag == "button_5":
|
|
|
|
#ACTUALLY PARSE THE JSON DUH, THIS IS BROKEN
|
|
|
|
#get the weather
|
|
#proc = subprocess.Popen(["wget", "http://wttr.in/btv_FnQT.png"], stdout=subprocess.PIPE)
|
|
proc = subprocess.Popen(["wget", "http://wttr.in/btv_FQT.png"], stdout=subprocess.PIPE)
|
|
(out, err) = proc.communicate()
|
|
|
|
#convert
|
|
size = 400, 300
|
|
#too pixely
|
|
#im = Image.open("btv_FnQT.png")
|
|
#im = im.rotate(90, Image.NEAREST, "expand=1")
|
|
|
|
#sideways, no rotate
|
|
im = Image.open("btv_FQT.png")
|
|
|
|
im.thumbnail(size, Image.BICUBIC)
|
|
im = im.convert("1")
|
|
im.save("btv.bmp")
|
|
|
|
#display the weather
|
|
epd.init()
|
|
Limage2 = Image.new('1', (epd4in2.EPD_HEIGHT, epd4in2.EPD_WIDTH), 255) # 255: clear the frame
|
|
bmp = Image.open("btv.bmp")
|
|
Limage.paste(bmp)
|
|
epd.display(epd.getbuffer(Limage))
|
|
time.sleep(3)
|
|
timeIndex+=2
|
|
epd.sleep()
|
|
button_flag = "null"
|
|
buttonshim.set_pixel(0x00, 0x00, 0x00)
|