#!/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 os 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 = 1200 syncIndex = 0 dbIndex = 1 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 and display boot image #grab the entry at dbIndex, newFlier is a dict newFlier = db.get(doc_id=dbIndex) flierPath = newFlier["path"] Limage = Image.new('1', (epd4in2.EPD_HEIGHT, epd4in2.EPD_WIDTH), 255) # 255: clear the frame try: font18 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 36) epd = epd4in2.EPD() epd.init() bmp = Image.open(flierPath) Limage.paste(bmp) draw = ImageDraw.Draw(Limage) draw.text((2, 280), 'EBB', font = font18, fill = 255) draw.text((2, 300), 'version .02', font = font18, fill = 255) epd.display(epd.getbuffer(Limage)) time.sleep(2) epd.sleep() 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" buttonshim.set_pixel(0x00, 0x00, 0x00) ## 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 image try: epd.init() 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' #generate unique bmp name bmpath = imagesPath + unique + '.bmp' #loop in case we wanna re-take it exitPhotoMode = False while exitPhotoMode == False: #take photo try: result = subprocess.call(['raspistill', '-o', jpgpath, '-vf', '-hf', '-w', '300', '-h', '400', '-t', '1000']) except: print 'traceback.format_exc():\n%s' % traceback.format_exc() exit() #convert to bmp im = Image.open(jpgpath) im = im.convert("1") im.save(bmpath) #display the image epd.init() bmp = Image.open(bmpath) Limage.paste(bmp) epd.display(epd.getbuffer(Limage)) #wait for user decision on photo waited = 0 button_flag = "none" #strobe light green yellow red for a while while (button_flag == "none") and (waited < 30): buttonshim.set_pixel(0xFF, 0x00, 0x00) time.sleep(.2) buttonshim.set_pixel(0xFF, 0xFF, 0x00) time.sleep(.2) buttonshim.set_pixel(0x00, 0xFF, 0x00) time.sleep(.2) buttonshim.set_pixel(0xFF, 0xFF, 0x00) time.sleep(.2) waited+=1 #if still not sure, give em the menu if button_flag == "none": #ok time to display menu draw = ImageDraw.Draw(Limage) font18 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 24) draw.text((2, 0), 'press button 1 to:', font = font18, fill = 255) draw.text((2, 40), 'post online', font = font18, fill = 255) draw.text((2, 80), 'button 2: post photo', font = font18, fill = 255) draw.text((2, 120), 'here only', font = font18, fill = 255) draw.text((2, 160), 'button 3: retake', font = font18, fill = 255) draw.text((2, 200), 'buttons 4 or 5:', font = font18, fill = 255) draw.text((2, 240), 'exit photo mode', font = font18, fill = 255) epd.display(epd.getbuffer(Limage)) #now just wait for them to press something while button_flag == "none": buttonshim.set_pixel(0xFF, 0x00, 0x00) time.sleep(.2) buttonshim.set_pixel(0xFF, 0xFF, 0x00) time.sleep(.2) buttonshim.set_pixel(0x00, 0xFF, 0x00) time.sleep(.2) buttonshim.set_pixel(0xFF, 0xFF, 0x00) time.sleep(.2) #led to thinking mode buttonshim.set_pixel(0x00, 0x00, 0xFF) #post to ssb if button_flag == "button_1": button_flag == "null" #add the photo to the db/ssb addtoDB.addFile(bmpath,dbPath,0) #update dbCount for item in db: dbCount = item.doc_id exitPhotoMode = True #display the image to clear the menu (also display text "posted") font18 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 24) bmp = Image.open(bmpath) Limage.paste(bmp) draw = ImageDraw.Draw(Limage) draw.text((2, 240), 'PHOTO POSTED', font = font18, fill = 255) draw.text((2, 260), 'online!', font = font18, fill = 255) epd.display(epd.getbuffer(Limage)) #post only locally if button_flag == "button_2": button_flag == "null" #add that file to the db. -1 tells addtoDB not to post to ssb. addtoDB.addFile(bmpath,dbPath,-1) #update dbCount for item in db: dbCount = item.doc_id exitPhotoMode = True #display the image to clear the menu (also display text "posted") font18 = ImageFont.truetype('/usr/share/fonts/truetype/wqy/wqy-microhei.ttc', 24) bmp = Image.open(bmpath) Limage.paste(bmp) draw = ImageDraw.Draw(Limage) draw.text((2, 240), 'PHOTO POSTED', font = font18, fill = 255) draw.text((2, 260), 'to this device only', font = font18, fill = 255) epd.display(epd.getbuffer(Limage)) #reshoot the picture if button_flag == "button_3": button_flag == "null" #nothin! loop around... exitPhotoMode still false #exit photomode, delete photo if (button_flag == "button_4") or (button_flag == "button_5"): button_flag == "null" exitPhotoMode = True os.remove(bmpath) os.remove(jpgpath) #led off 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() Limage = 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)