|
| 1 | +#!/usr/bin/python |
| 2 | + |
| 3 | +# Main script for Adafruit Internet of Things Printer 2. Monitors button |
| 4 | +# for taps and holds, performs periodic actions (Twitter polling by default) |
| 5 | +# and daily actions (Sudoku and weather by default). |
| 6 | +# Written by Adafruit Industries. MIT license. |
| 7 | +# |
| 8 | +# MUST BE RUN AS ROOT (due to GPIO access) |
| 9 | +# |
| 10 | +# Required software includes Adafruit_Thermal, Python Imaging and PySerial |
| 11 | +# libraries. Other libraries used are part of stock Python install. |
| 12 | +# |
| 13 | +# Resources: |
| 14 | +# http://www.adafruit.com/products/597 Mini Thermal Receipt Printer |
| 15 | +# http://www.adafruit.com/products/600 Printer starter pack |
| 16 | + |
| 17 | +import RPi.GPIO as GPIO |
| 18 | +import subprocess, time, Image |
| 19 | +from Adafruit_Thermal import * |
| 20 | + |
| 21 | +ledPin = 18 |
| 22 | +buttonPin = 23 |
| 23 | +holdTime = 2 # Duration for button hold (shutdown) |
| 24 | +tapTime = 0.01 # Debounce time for button taps |
| 25 | +nextInterval = 0.0 # Time of next recurring operation |
| 26 | +dailyFlag = False # Set after daily trigger occurs |
| 27 | +lastId = '1' # State information passed to/from interval script |
| 28 | +printer = Adafruit_Thermal("/dev/ttyAMA0", 19200, timeout=5) |
| 29 | + |
| 30 | + |
| 31 | +# Called when button is briefly tapped. Invokes time/temperature script. |
| 32 | +def tap(): |
| 33 | + GPIO.output(ledPin, GPIO.HIGH) # LED on while working |
| 34 | + subprocess.call(["python", "timetemp.py"]) |
| 35 | + GPIO.output(ledPin, GPIO.LOW) |
| 36 | + |
| 37 | + |
| 38 | +# Called when button is held down. Prints image, invokes shutdown process. |
| 39 | +def hold(): |
| 40 | + GPIO.output(ledPin, GPIO.HIGH) |
| 41 | + printer.printImage(Image.open('gfx/goodbye.png'), True) |
| 42 | + printer.feed(3) |
| 43 | +# subprocess.call("sync") |
| 44 | +# subprocess.call(["shutdown", "-h", "now"]) |
| 45 | + GPIO.output(ledPin, GPIO.LOW) |
| 46 | + |
| 47 | + |
| 48 | +# Called at periodic intervals (30 seconds by default). |
| 49 | +# Invokes twitter script. |
| 50 | +def interval(): |
| 51 | + GPIO.output(ledPin, GPIO.HIGH) |
| 52 | + p = subprocess.Popen(["python", "twitter.py", str(lastId)], |
| 53 | + stdout=subprocess.PIPE) |
| 54 | + GPIO.output(ledPin, GPIO.LOW) |
| 55 | + return p.communicate()[0] # Script pipes back lastId, returned to main |
| 56 | + |
| 57 | + |
| 58 | +# Called once per day (6:30am by default). |
| 59 | +# Invokes weather forecast and sudoku-gfx scripts. |
| 60 | +def daily(): |
| 61 | + GPIO.output(ledPin, GPIO.HIGH) |
| 62 | + subprocess.call(["python", "forecast.py"]) |
| 63 | + subprocess.call(["python", "sudoku-gfx.py"]) |
| 64 | + GPIO.output(ledPin, GPIO.LOW) |
| 65 | + |
| 66 | + |
| 67 | +# Initialization |
| 68 | + |
| 69 | +# Use Broadcom pin numbers (not Raspberry Pi pin numbers) for GPIO |
| 70 | +GPIO.setmode(GPIO.BCM) |
| 71 | + |
| 72 | +# Enable LED and button (w/pull-up on latter) |
| 73 | +GPIO.setup(ledPin, GPIO.OUT) |
| 74 | +GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP) |
| 75 | + |
| 76 | +# Print greeting image (LED on while working) |
| 77 | +GPIO.output(ledPin, GPIO.HIGH) |
| 78 | +printer.printImage(Image.open('gfx/hello.png'), True) |
| 79 | +printer.feed(3) |
| 80 | +GPIO.output(ledPin, GPIO.LOW) |
| 81 | + |
| 82 | +# Poll initial button state and time |
| 83 | +prevButtonState = GPIO.input(buttonPin) |
| 84 | +prevTime = time.clock() |
| 85 | +tapEnable = False |
| 86 | +holdEnable = False |
| 87 | + |
| 88 | +# Main loop |
| 89 | +while(True): |
| 90 | + |
| 91 | + # Poll current button state and time |
| 92 | + buttonState = GPIO.input(buttonPin) |
| 93 | + t = time.clock() |
| 94 | + |
| 95 | + # Has button state changed? |
| 96 | + if buttonState != prevButtonState: |
| 97 | + prevButtonState = buttonState # Yes, save new state/time |
| 98 | + prevTime = t |
| 99 | + else: # Button state unchanged |
| 100 | + if (t - prevTime) >= holdTime: # Button held more than 'holdTime'? |
| 101 | + # Yes it has. Is the hold action as-yet untriggered? |
| 102 | + if holdEnable == True: # Yep! |
| 103 | + hold() # Perform hold action (usu. shutdown) |
| 104 | + holdEnable = False # 1 shot...don't repeat hold action |
| 105 | + tapEnable = False # Don't do tap action on release |
| 106 | + elif (t - prevTime) >= tapTime: # Not holdTime. tapTime elapsed? |
| 107 | + # Yes. Debounced press or release... |
| 108 | + if buttonState == True: # Button released? |
| 109 | + if tapEnable == True: # Ignore if prior hold() |
| 110 | + tap() # Tap triggered (button released) |
| 111 | + tapEnable = False # Disable tap and hold |
| 112 | + holdEnable = False |
| 113 | + else: # Button pressed |
| 114 | + tapEnable = True # Enable tap and hold actions |
| 115 | + holdEnable = True |
| 116 | + |
| 117 | + # LED blinks while idle, for a brief interval every 2 seconds. |
| 118 | + # Pin 18 is PWM-capable and a "sleep throb" would be nice, but |
| 119 | + # the PWM-related library is a hassle for average users to install |
| 120 | + # right now. Might return to this later when it's more accessible. |
| 121 | + if ((int(t) & 1) == 0) and ((t - int(t)) < 0.15): |
| 122 | + GPIO.output(ledPin, GPIO.HIGH) |
| 123 | + else: |
| 124 | + GPIO.output(ledPin, GPIO.LOW) |
| 125 | + |
| 126 | + # Once per day (currently set for 6:30am local time, or when script |
| 127 | + # is first run, if after 6:30am), run forecast and sudoku scripts. |
| 128 | + l = time.localtime() |
| 129 | + if (60 * l.tm_hour + l.tm_min) > (60 * 6 + 30): |
| 130 | + if dailyFlag == False: |
| 131 | + daily() |
| 132 | + dailyFlag = True |
| 133 | + else: |
| 134 | + dailyFlag = False # Reset daily trigger |
| 135 | + |
| 136 | + # Every 30 seconds, run Twitter scripts. 'lastId' is passed around |
| 137 | + # to preserve state between invocations. Probably simpler to do an |
| 138 | + # import thing. |
| 139 | + if t > nextInterval: |
| 140 | + nextInterval = t + 30.0 |
| 141 | + lastId = interval() |
0 commit comments