Skip to content

Commit 958cafa

Browse files
committed
Added new functions
Signed-off-by: cibear <holo-deck@gmx.de>
1 parent b5d7625 commit 958cafa

File tree

3 files changed

+195
-0
lines changed

3 files changed

+195
-0
lines changed

collected_before.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python
2+
3+
from Adafruit_Thermal import *
4+
5+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
6+
7+
printer.setSize('L') # Set type size, accepts 'S', 'M', 'L'
8+
printer.println("Collected before!")
9+
10+
printer.sleep() # Tell printer to sleep
11+
printer.wake() # Call wake() before printing again, even if reset
12+
printer.setDefault() # Restore printer to defaults

daily-main.py

Lines changed: 171 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,171 @@
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+
from __future__ import print_function
18+
import RPi.GPIO as GPIO
19+
import subprocess, time, Image, socket
20+
from Adafruit_Thermal import *
21+
22+
print("Starting up....")
23+
ledPin = 18
24+
buttonPin = 23
25+
holdTime = 2 # Duration for button hold (shutdown)
26+
tapTime = 0.01 # Debounce time for button taps
27+
nextInterval = 0.0 # Time of next recurring operation
28+
dailyFlag = False # Set after daily trigger occurs
29+
lastId = '1' # State information passed to/from interval script
30+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
31+
32+
33+
# Called when button is briefly tapped. Invokes time/temperature script.
34+
def tap():
35+
print("Button press detected.")
36+
GPIO.output(ledPin, GPIO.HIGH) # LED on while working
37+
subprocess.call(["python", "collected_before.py"])
38+
GPIO.output(ledPin, GPIO.LOW)
39+
40+
41+
# Called when button is held down. Prints image, invokes shutdown process.
42+
def hold():
43+
print("Button hold detected.")
44+
GPIO.output(ledPin, GPIO.HIGH)
45+
printer.printImage(Image.open('gfx/goodbye.png'), True)
46+
printer.feed(3)
47+
subprocess.call("sync")
48+
subprocess.call(["shutdown", "-h", "now"])
49+
GPIO.output(ledPin, GPIO.LOW)
50+
51+
52+
# Called at periodic intervals (30 seconds by default).
53+
# Invokes twitter script.
54+
def interval():
55+
print("Twitter polled.")
56+
GPIO.output(ledPin, GPIO.HIGH)
57+
p = subprocess.Popen(["python", "twitter.py", str(lastId)],
58+
stdout=subprocess.PIPE)
59+
GPIO.output(ledPin, GPIO.LOW)
60+
return p.communicate()[0] # Script pipes back lastId, returned to main
61+
62+
63+
# Called once per day (5:30am by default).
64+
def daily():
65+
print("First button press of the day...")
66+
GPIO.output(ledPin, GPIO.HIGH)
67+
subprocess.call(["python", "daily_message.py"])
68+
GPIO.output(ledPin, GPIO.LOW)
69+
70+
71+
# Initialization
72+
73+
# Use Broadcom pin numbers (not Raspberry Pi pin numbers) for GPIO
74+
GPIO.setmode(GPIO.BCM)
75+
76+
# Enable LED and button (w/pull-up on latter)
77+
GPIO.setup(ledPin, GPIO.OUT)
78+
GPIO.setup(buttonPin, GPIO.IN, pull_up_down=GPIO.PUD_UP)
79+
80+
# LED on while working
81+
GPIO.output(ledPin, GPIO.HIGH)
82+
83+
# Processor load is heavy at startup; wait a moment to avoid
84+
# stalling during greeting.
85+
#time.sleep(30)
86+
87+
# Show IP address (if network is available)
88+
try:
89+
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
90+
s.connect(('8.8.8.8', 0))
91+
printer.print('My IP address is ' + s.getsockname()[0])
92+
printer.feed(3)
93+
except:
94+
printer.boldOn()
95+
printer.println('Network is unreachable.')
96+
printer.boldOff()
97+
printer.print('Connect display and keyboard\n'
98+
'for network troubleshooting.')
99+
printer.feed(3)
100+
exit(0)
101+
102+
# Print greeting image
103+
printer.printImage(Image.open('gfx/hello.png'), True)
104+
printer.feed(3)
105+
GPIO.output(ledPin, GPIO.LOW)
106+
107+
# Poll initial button state and time
108+
prevButtonState = GPIO.input(buttonPin)
109+
prevTime = time.time()
110+
tapEnable = False
111+
holdEnable = False
112+
113+
print("Waiting for event....")
114+
115+
# Main loop
116+
while(True):
117+
118+
# Poll current button state and time
119+
buttonState = GPIO.input(buttonPin)
120+
t = time.time()
121+
122+
# Has button state changed?
123+
if buttonState != prevButtonState:
124+
prevButtonState = buttonState # Yes, save new state/time
125+
prevTime = t
126+
else: # Button state unchanged
127+
if (t - prevTime) >= holdTime: # Button held more than 'holdTime'?
128+
# Yes it has. Is the hold action as-yet untriggered?
129+
if holdEnable == True: # Yep!
130+
hold() # Perform hold action (usu. shutdown)
131+
holdEnable = False # 1 shot...don't repeat hold action
132+
tapEnable = False # Don't do tap action on release
133+
elif (t - prevTime) >= tapTime: # Not holdTime. tapTime elapsed?
134+
# Yes. Debounced press or release...
135+
if buttonState == True: # Button released?
136+
if tapEnable == True: # Ignore if prior hold()
137+
tap() # Tap triggered (button released)
138+
tapEnable = False # Disable tap and hold
139+
holdEnable = False
140+
else: # Button pressed
141+
tapEnable = True # Enable tap and hold actions
142+
holdEnable = True
143+
144+
# LED blinks while idle, for a brief interval every 2 seconds.
145+
# Pin 18 is PWM-capable and a "sleep throb" would be nice, but
146+
# the PWM-related library is a hassle for average users to install
147+
# right now. Might return to this later when it's more accessible.
148+
if ((int(t) & 1) == 0) and ((t - int(t)) < 0.15):
149+
GPIO.output(ledPin, GPIO.HIGH)
150+
else:
151+
GPIO.output(ledPin, GPIO.LOW)
152+
153+
# Once per day (currently set for 6:30am local time, or when script
154+
# is first run, if after 5:30am), run forecast and sudoku scripts.
155+
l = time.localtime()
156+
if (60 * l.tm_hour + l.tm_min) > (60 * 5 + 30):
157+
if dailyFlag == False:
158+
daily()
159+
dailyFlag = True
160+
else:
161+
dailyFlag = False # Reset daily trigger
162+
163+
# Every 30 seconds, run Twitter scripts. 'lastId' is passed around
164+
# to preserve state between invocations. Probably simpler to do an
165+
# import thing.
166+
if t > nextInterval:
167+
nextInterval = t + 30.0
168+
result = interval()
169+
if result is not None:
170+
lastId = result.rstrip('\r\n')
171+

daily_message.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#!/usr/bin/python
2+
3+
from Adafruit_Thermal import *
4+
5+
printer = Adafruit_Thermal("/dev/serial0", 19200, timeout=5)
6+
7+
printer.setSize('L') # Set type size, accepts 'S', 'M', 'L'
8+
printer.println("Daily message")
9+
10+
printer.sleep() # Tell printer to sleep
11+
printer.wake() # Call wake() before printing again, even if reset
12+
printer.setDefault() # Restore printer to defaults

0 commit comments

Comments
 (0)