Created
July 28, 2014 11:29
-
-
Save andrewn/c410e10390287523acec to your computer and use it in GitHub Desktop.
CPA UI flow with a PiFace
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pifacecad | |
import string | |
# Create object | |
cad = pifacecad.PiFaceCAD() | |
cad.lcd.backlight_on() | |
# Config | |
code_length = 8 | |
characters = list(string.digits + string.ascii_lowercase) | |
# State | |
state = "RADIO" | |
current_character = 0 | |
current_cursor_pos = 0 | |
code = [] | |
# Functions | |
def cursor_off(): | |
cad.lcd.cursor_off() | |
cad.lcd.blink_off() | |
def cursor_on(): | |
cad.lcd.cursor_on() | |
def write(line_1="", line_2=""): | |
cad.lcd.clear() | |
cad.lcd.write(line_1) | |
cad.lcd.set_cursor(0, 1) | |
cad.lcd.write(line_2) | |
def button_label(label): | |
cad.lcd.set_cursor(9, 2) | |
cad.lcd.write(label) | |
def next_character(): | |
global current_character | |
current_character = current_character + 1 | |
display_current_character() | |
def previous_character(): | |
global current_character | |
current_character = current_character - 1 | |
display_current_character() | |
def cpa_confirm(): | |
global state | |
state = "RADIO" | |
cursor_off() | |
write("Paired!") | |
def confirm_character(): | |
print("confirm") | |
global current_cursor_pos | |
global current_character | |
global code | |
# Store confirmed characters | |
code.append(get_current_character()) | |
if len(code) == 8: | |
cpa_confirm() | |
else: | |
# Reset | |
current_character = 0 | |
current_cursor_pos = current_cursor_pos + 1 | |
display_current_character() | |
def get_current_character(): | |
return characters[current_character] | |
def display_current_character(): | |
cad.lcd.set_cursor(current_cursor_pos, 2) | |
cad.lcd.write(characters[current_character]) | |
cad.lcd.set_cursor(current_cursor_pos, 2) | |
def start_input(): | |
global current_cursor_pos | |
global current_character | |
current_cursor_pos = 0 | |
current_character = 0 | |
cursor_on() | |
display_current_character() | |
def link_ok(): | |
global state | |
state = "CPA" | |
cursor_off() | |
write("Code:") | |
start_input() | |
def button_event(event): | |
pin = event.pin_num | |
if pin == 0 and state == "RADIO": | |
begin_cpa() | |
elif pin == 4 and state == "START_CPA": | |
link_ok() | |
elif pin == 5 and state == "CPA": | |
confirm_character() | |
elif pin == 6 and state == "CPA": | |
previous_character() | |
elif pin == 7 and state == "CPA": | |
next_character() | |
else: | |
now_playing("Radio 1") | |
print(state, pin, "".join(code)) | |
def init_listeners(): | |
listener = pifacecad.SwitchEventListener(chip=cad) | |
for i in range(8): | |
listener.register(i, pifacecad.IODIR_FALLING_EDGE, button_event) | |
listener.activate() | |
def now_playing(station): | |
cursor_off() | |
write("Now playing: ", station) | |
def begin_cpa(): | |
global state | |
state = "START_CPA" | |
write("Go to bit.ly/1riff3h") | |
button_label("OK") | |
def init(): | |
init_listeners() | |
now_playing("Radio 1") | |
init() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment