which pins are being used for the two buttons and which for the pumps?
import time
import Adafruit_GPIO.SPI as SPI
import adafruit_ssd1306
import RPi.GPIO as GPIO
import json
import threading
import traceback
from PIL import Image
from PIL import ImageDraw, ImageFont
import board
import neopixel
from rpi_ws281x import *
import digitalio
#from dotstar import Adafruit_DotStar
from menu import MenuItem, Menu, Back, MenuContext, MenuDelegate
from drinks import drink_list, drink_options
i2c = board.I2C()
oled_reset = digitalio.DigitalInOut(board.D4)
oled = adafruit_ssd1306.SSD1306_I2C(SCREEN_WIDTH, SCREEN_HEIGHT, i2c, addr=0x3C, reset=oled_reset)
font = ImageFont.truetype('PixelOperator.ttf', 16)
#oled = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
LED_COUNT = 45 # Number of LED pixels.
LED_PIN = 18 # GPIO pin connected to the pixels (18 uses PWM!).
#LED_PIN = 10 # GPIO pin connected to the pixels (10 uses SPI /dev/spidev0.0).
LED_FREQ_HZ = 800000 # LED signal frequency in hertz (usually 800khz)
LED_DMA = 10 # DMA channel to use for generating signal (try 10)
LED_BRIGHTNESS = 65 # Set to 0 for darkest and 255 for brightest
LED_INVERT = False # True to invert the signal (when using NPN transistor level shift)
LED_CHANNEL = 0
LEFT_BTN_PIN = 13
LEFT_PIN_BOUNCE = 1000
self.pump_configuration = Bartender.readPumpConfiguration()
for pump in self.pump_configuration.keys():
GPIO.setup(self.pump_configuration[pump]["pin"], GPIO.OUT, initial=GPIO.LOW)
setup pixels:
self.numpixels = NUMBER_NEOPIXELS # Number of LEDs in strip
Assistant
The left button is connected to pin 13 (LEFT_BTN_PIN), and the right button is connected to pin 5 (RIGHT_BTN_PIN). The pumps are connected to the pins specified in the pump_config.json file, which is loaded and stored in the self.pump_configuration attribute of the Bartender class. The pump pins are initialized as outputs with an initial value of GPIO.LOW during initialization of the class instance. The pin used for the neopixel LED strip is specified as NEOPIXEL_DATA_PIN, which is set to pin 18.
User
and which for the pumps
Assistant
The specific pins used for the pumps are defined in the pump_config.json file, which is loaded and stored in the self.pump_configuration attribute of the Bartender class. The pins are defined in the "pin" field for each pump configuration object in the JSON file. For example, if the configuration for pump 1 in the JSON file looks like this:
vbnet
{
"1": {
"name": "Gin",
"value": "gin",
"pin": 17
},
...
}
then pump 1 is connected to pin 17.
It looks like a JSON object that represents a configuration for a drink dispensing machine with six pumps. Each pump is assigned a name, pin number, and a value that corresponds to the liquid it dispenses.
The configuration specifies that:
Pump 1 dispenses "gin" and is connected to pin 17.
Pump 2 dispenses "vodka" and is connected to pin 27.
Pump 3 dispenses "rum" and is connected to pin 22.
Pump 4, Pump 5, and Pump 6 are not currently assigned a value, indicating that they are not being used. Their pin numbers are 23, 24, and 25, respectively.
Here's how to control the strip from any two GPIO pins:
def filterDrinks(self, menu):
"""
Removes any drinks that can't be handled by the pump configuration
"""
for i in menu.options:
if (i.type == "drink"):
i.visible = False
ingredients = i.attributes["ingredients"]
presentIng = 0
for ing in ingredients.keys():
for p in self.pump_configuration.keys():
if (ing == self.pump_configuration[p]["value"]):
presentIng += 1
if (presentIng == len(ingredients.keys())):
i.visible = True
elif (i.type == "menu"):
self.filterDrinks(i)
def selectConfigurations(self, menu):
"""
Adds a selection star to the pump configuration option
"""
for i in menu.options:
if (i.type == "pump_selection"):
key = i.attributes["key"]
if (self.pump_configuration[key]["value"] == i.attributes["value"]):
i.name = "%s %s" % (i.attributes["name"], "*")
else:
i.name = i.attributes["name"]
elif (i.type == "menu"):
self.selectConfigurations(i)
cancel any button presses while the drink is being made
self.stopInterrupts()
self.running = True
for pump in self.pump_configuration.keys():
pump_t = threading.Thread(target=self.pour, args=(self.pump_configuration[pump]["pin"], waitTime))
pumpThreads.append(pump_t)
start the pump threads
for thread in pumpThreads:
thread.start()
start the progress bar
self.progressBar(waitTime)
wait for threads to finish
for thread in pumpThreads:
thread.join()
show the main menu
self.menuContext.showMenu()
sleep for a couple seconds to make sure the interrupts don't get triggered
def cycleLights(self):
t = threading.currentThread()
head = 0 # Index of first 'on' pixel
tail = -10 # Index of last 'off' pixel
color = 0xff0000 # 'On' color (starts red)
while getattr(t, "do_run", True):
self.strip[head] = color # Turn on 'head' pixel
self.strip[tail] = color # Turn off 'tail'
self.strip.show() # Refresh strip
time.sleep(1.0 / 50)
head += 1 # Advance head position
if(head >= self.numpixels): # Off end of strip?
head = 0 # Reset to start
color >>= 8 # Red->green->blue->black
if(color == 0): color = 0xff0000# If black, reset to red
tail += 1 # Advance tail position
if(tail >= self.numpixels): tail = 0 # Off end? Reset
def lightsEndingSequence(self):
make lights green
for i in range(0, self.numpixels):
self.strip[i] = (0, 250, 0)
#self.strip.show()
time.sleep(5)
for i in range(0, self.numpixels):
self.strip[i] =(0, 0, 0)
#self.strip.show()
Parse the drink ingredients and spawn threads for pumps
maxTime = 0
pumpThreads = []
for ing in ingredients.keys():
for pump in self.pump_configuration.keys():
if ing == self.pump_configuration[pump]["value"]:
waitTime = ingredients[ing] * FLOW_RATE
if (waitTime > maxTime):
maxTime = waitTime
pump_t = threading.Thread(target=self.pour, args=(self.pump_configuration[pump]["pin"], waitTime))
pumpThreads.append(pump_t)
start the pump threads
for thread in pumpThreads:
thread.start()
start the progress bar
self.progressBar(maxTime)
wait for threads to finish
for thread in pumpThreads:
thread.join()
show the main menu
self.menuContext.showMenu()
stop the light thread
lightsThread.do_run = False
lightsThread.join()
show the ending sequence lights
self.lightsEndingSequence()
sleep for a couple seconds to make sure the interrupts don't get triggered
time.sleep(2);
reenable interrupts
self.startInterrupts()
self.running = False
def left_btn(self, ctx):
if not self.running:
self.menuContext.advance()
def right_btn(self, ctx):
if not self.running:
self.menuContext.select()