Skip to content

Instantly share code, notes, and snippets.

@fvdbosch
Last active December 13, 2016 17:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save fvdbosch/33ef9965c0efda940b54 to your computer and use it in GitHub Desktop.
Save fvdbosch/33ef9965c0efda940b54 to your computer and use it in GitHub Desktop.
PiDesk Main Script
#!/usr/bin/python3
#
# PiDesk Main script
#
# Make the necessary imports
import gertbot as gb
import RPi.GPIO as GPIO
import time
from os import system
# Map capacitive touch buttons to GPIO pins
BTN1 = 16
BTN2 = 19
BTN3 = 20
BTN4 = 21
BTN5 = 26
# Map relay channels to GPIO pins
RELAY1 = 5
RELAY2 = 6
# GPIO configuration parameters
GPIO.setmode(GPIO.BCM) # set up GPIO using BCM numbering
GPIO.setup(BTN1, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure GPIO 16 as input with internal pull-up
GPIO.setup(BTN2, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure GPIO 19 as input with internal pull-up
GPIO.setup(BTN3, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure GPIO 20 as input with internal pull-up
GPIO.setup(BTN4, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure GPIO 21 as input with internal pull-up
GPIO.setup(BTN5, GPIO.IN, pull_up_down=GPIO.PUD_UP) # configure GPIO 26 as input with internal pull-up
GPIO.setup(RELAY1, GPIO.OUT) # configure GPIO 5 as output
GPIO.setup(RELAY2, GPIO.OUT) # configure GPIO 6 as output
# Gertbot configuration parameters
BOARD = 0 # which board we talk to
STEPPER_A = 0 # channel for first stepper motor
STEPPER_B = 2 # channel for second stepper motor
MODE = 24 # stepper control, gray code
FREQ = 900.0 # frequency
STEPS = 5000000 # steps to raise/lower screen until endstop is triggered
ENDSTOP_MODE = 1 # trigger endstop when pin is low
# Open serial port to talk to Gertbot
gb.open_uart(0)
# Setup the channels and endstops for stepper motors
gb.set_mode(BOARD, STEPPER_A, MODE)
gb.set_mode(BOARD, STEPPER_B, MODE)
gb.freq_stepper(BOARD, STEPPER_A, FREQ)
gb.freq_stepper(BOARD, STEPPER_B, FREQ)
gb.set_endstop(BOARD, STEPPER_A, ENDSTOP_MODE, ENDSTOP_MODE)
gb.set_endstop(BOARD, STEPPER_B, ENDSTOP_MODE, ENDSTOP_MODE)
# Main loop
prev_millis = 0
while True :
millis = int(round(time.time() * 1000))
if((millis - prev_millis) > 500):
if (not GPIO.input(BTN1)): # Screen Up
system("sudo pkill -f pidesk-led.py") # stop any ongoing led animations
system("sudo killall aplay") # stop any ongoing sound effects
gb.emergency_stop() # stop all motors
system("aplay -D hw:1,0 /home/pi/beep.wav &") # play audio beep to acknowledge button press
gb.move_stepper(BOARD, STEPPER_A, STEPS) # rotate stepper motors clockwise
gb.move_stepper(BOARD, STEPPER_B, STEPS) # rotate stepper motors clockwise
system("sudo python /home/pi/rpi_ws281x/python/examples/pidesk-led.py sweep 0 255 0 &") # green sweep animation
GPIO.output(RELAY1, 1) # power on desktop computer and screen by enabling relay
time.sleep(0.05)
prev_millis = int(round(time.time() * 1000))
elif (not GPIO.input(BTN2)): # Screen Down
system("sudo pkill -f pidesk-led.py") # stop any ongoing led animations
system("sudo killall aplay") # stop any ongoing sound effects
gb.emergency_stop() # stop all motors
system("aplay -D hw:1,0 /home/pi/beep.wav &") # play audio beep to acknowledge button press
gb.move_stepper(BOARD, STEPPER_A, -STEPS) # rotate stepper motors counter-clockwise
gb.move_stepper(BOARD, STEPPER_B, -STEPS) # rotate stepper motors counter-clockwise
system("sudo python /home/pi/rpi_ws281x/python/examples/pidesk-led.py sweep 255 0 0 &") # red sweep animation
GPIO.output(RELAY1, 0) # power off desktop computer and screen by disabling relay
time.sleep(0.05)
prev_millis = int(round(time.time() * 1000))
elif (not GPIO.input(BTN3)): # Stop everything
system("sudo pkill -f pidesk-led.py") # stop any ongoing led animations
system("sudo killall aplay") # stop any ongoing sound effects
gb.emergency_stop() # stop all motors
system("aplay -D hw:1,0 /home/pi/beep.wav &") # play audio beep to acknowledge button press
system("sudo python /home/pi/rpi_ws281x/python/examples/pidesk-led.py off &") # turn off all leds
# trigger additional actions
time.sleep(0.05)
prev_millis = int(round(time.time() * 1000))
elif (not GPIO.input(BTN4)): # Spare
system("aplay -D hw:1,0 /home/pi/beep.wav &") # play audio beep to acknowledge button press
# trigger additional actions
time.sleep(0.05)
prev_millis = int(round(time.time() * 1000))
elif (not GPIO.input(BTN5)): # Spare
system("aplay -D hw:1,0 /home/pi/beep.wav &") # play audio beep to acknowledge button press
# trigger additional actions
time.sleep(0.05)
prev_millis = int(round(time.time() * 1000))
else: # No buttons pressed, reset states
time.sleep(0.05)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment