I have made a talking vending machine catalog for the blind, it runs inside a raspberry pi put inside the vending machine with a speaker.
The program will be running all the time, headless, and the csv where the vending machine snack information and numbers is kept, will be hosted online (its temporarily in the Pi for now though, as of this question). Its structured like so:
66, Mrs Freshleys Cupcakes
14, Flamin Hot Cheetos
etc.
It doesnt matter what order items are added to the csv, as I sort the list in my code.
The Vending Machine company guy said they will happily update the csv with new contents if they change things inside the vending machine; and so when they save that csv, I want my program, which will be running constantly, to pull that fresh data in and update my VL (VendyList) variable.
That way, if item "70, gummy bears" is added, or number 66 is changed, it will automatically update it. Is there a way to refresh the import csv? Or refresh the whole program whenever someone presses the '00' for 'help' function?
Here is the code
from gtts import gTTS
import pygame
from io import BytesIO
import os
import sys
import time
pygame.init()
if sys.version_info[0] == 3:
# for Python3
from tkinter import * ## notice lowercase 't' in tkinter here
else:
# for Python2
from Tkinter import *
def say(text):
tts = gTTS(text=text, slow=False, lang='en-us', lang_check=False)
fp = BytesIO()
tts.write_to_fp(fp)
fp.seek(0)
pygame.mixer.init()
pygame.mixer.music.load(fp)
pygame.mixer.music.play()
from csv import reader
infile = open(r'/home/pi/VendyLogProject/vendylist.csv',mode='r')
vl = sorted(list(reader(infile)))
vl2 = [item[0] for item in vl]
baseposition = vl[0] # 0 is the first entry index in the vl list
def current(event=None):
global baseposition # baseposition was defined outside of the function, therefore we call global
say(baseposition[1]+baseposition[0])
def back(event=None):
global baseposition
currentposition = vl.index(baseposition)
if currentposition == 0:
say(baseposition[1]+baseposition[0])
else:
previousposition = int(currentposition) - 1 # previousposition is previous position
baseposition = vl[previousposition]
say(baseposition[1]+baseposition[0])
def forward(event=None):
global baseposition
currentposition = vl.index(baseposition)
if currentposition == (len(vl) - 1):
say(baseposition[1]+baseposition[0])
else:
nextposition = int(currentposition) + 1 # nextposition is next position
baseposition = vl[nextposition]
say(baseposition[1]+baseposition[0])
def readnumber(int):
for item in vl:
global baseposition
currentposition = vl.index(baseposition)
if int == item[0]:
baseposition = vl[vl.index(item)]
say(baseposition[1]+baseposition[0])
def help():
say("Welcome to Vendy log! Use the plus and minus keys to go through the list of snacks or push a number to hear its contents!")
root = Tk()
prompt = ' VendyLog '
label1 = Label(root, text=prompt, width=len(prompt))
label1.pack()
#keys buffer
keybuf = []
def test_after():
if keybuf:
num = ''.join(keybuf)
keybuf.clear()
def get_key(event):
keybuf.append(event.char)
event.char = ''.join(keybuf)
root.after(500,test_after)
if event.char == '-':
back()
elif event.char == '+':
forward()
elif event.char == '.':
current()
elif event.char in vl2:
readnumber(event.char)
elif event.char == '00':
help()
root.bind_all('<Key>', get_key)
root.mainloop()