forked from adafruit/Kegomatic
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kegomatic.py
204 lines (168 loc) · 6.28 KB
/
kegomatic.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
#!/usr/bin/python
import os
import time
import math
import logging
import pygame, sys
from pygame.locals import *
import RPi.GPIO as GPIO
from twitter import *
from flowmeter import *
from adabot import *
from seekrits import *
t = Twitter( auth=OAuth(OAUTH_TOKEN, OAUTH_SECRET, CONSUMER_KEY, CONSUMER_SECRET) )
#boardRevision = GPIO.RPI_REVISION
GPIO.setmode(GPIO.BCM) # use real GPIO numbering
GPIO.setup(23,GPIO.IN, pull_up_down=GPIO.PUD_UP)
GPIO.setup(24,GPIO.IN, pull_up_down=GPIO.PUD_UP)
# set up pygame
pygame.init()
# set up the window
VIEW_WIDTH = 1248
VIEW_HEIGHT = 688
pygame.display.set_caption('KEGBOT')
lastTweet = 0
view_mode = 'normal'
# hide the mouse
pygame.mouse.set_visible(False)
# set up the flow meters
fm = FlowMeter('metric', 'beer')
fm2 = FlowMeter('metric', 'root beer')
tweet = ''
# set up the colors
BLACK = (0,0,0)
WHITE = (255,255,255)
# set up the window surface
windowSurface = pygame.display.set_mode((VIEW_WIDTH,VIEW_HEIGHT), FULLSCREEN, 32)
windowInfo = pygame.display.Info()
FONTSIZE = 48
LINEHEIGHT = 28
basicFont = pygame.font.SysFont(None, FONTSIZE)
# set up the backgrounds
bg = pygame.image.load('beer-bg.png')
tweet_bg = pygame.image.load('tweet-bg.png')
# set up the adabots
back_bot = adabot(361, 151, 361, 725)
middle_bot = adabot(310, 339, 310, 825)
front_bot = adabot(220, 527, 220, 888)
# draw some text into an area of a surface
# automatically wraps words
# returns any text that didn't get blitted
def drawText(surface, text, color, rect, font, aa=False, bkg=None):
rect = Rect(rect)
y = rect.top
lineSpacing = -2
# get the height of the font
fontHeight = font.size("Tg")[1]
while text:
i = 1
# determine if the row of text will be outside our area
if y + fontHeight > rect.bottom:
break
# determine maximum width of line
while font.size(text[:i])[0] < rect.width and i < len(text):
i += 1
# if we've wrapped the text, then adjust the wrap to the last word
if i < len(text):
i = text.rfind(" ", 0, i) + 1
# render the line and blit it to the surface
if bkg:
image = font.render(text[:i], 1, color, bkg)
image.set_colorkey(bkg)
else:
image = font.render(text[:i], aa, color)
surface.blit(image, (rect.left, y))
y += fontHeight + lineSpacing
# remove the text we just blitted
text = text[i:]
return text
def renderThings(flowMeter, flowMeter2, tweet, windowSurface, basicFont):
# Clear the screen
windowSurface.blit(bg,(0,0))
# draw the adabots
back_bot.update()
windowSurface.blit(back_bot.image,(back_bot.x, back_bot.y))
middle_bot.update()
windowSurface.blit(middle_bot.image,(middle_bot.x, middle_bot.y))
front_bot.update()
windowSurface.blit(front_bot.image,(front_bot.x, front_bot.y))
# Draw Ammt Poured
text = basicFont.render("CURRENT", True, WHITE, BLACK)
textRect = text.get_rect()
windowSurface.blit(text, (40,20))
if fm.enabled:
text = basicFont.render(fm.getFormattedThisPour(), True, WHITE, BLACK)
textRect = text.get_rect()
windowSurface.blit(text, (40,30+LINEHEIGHT))
if fm2.enabled:
text = basicFont.render(fm2.getFormattedThisPour(), True, WHITE, BLACK)
textRect = text.get_rect()
windowSurface.blit(text, (40, 30+(2*(LINEHEIGHT+5))))
# Draw Ammt Poured Total
text = basicFont.render("TOTAL", True, WHITE, BLACK)
textRect = text.get_rect()
windowSurface.blit(text, (windowInfo.current_w - textRect.width - 40, 20))
if fm.enabled:
text = basicFont.render(fm.getFormattedTotalPour(), True, WHITE, BLACK)
textRect = text.get_rect()
windowSurface.blit(text, (windowInfo.current_w - textRect.width - 40, 30 + LINEHEIGHT))
if fm2.enabled:
text = basicFont.render(fm2.getFormattedTotalPour(), True, WHITE, BLACK)
textRect = text.get_rect()
windowSurface.blit(text, (windowInfo.current_w - textRect.width - 40, 30 + (2 * (LINEHEIGHT+5))))
if view_mode == 'tweet':
windowSurface.blit(tweet_bg,(0,0))
textRect = Rect(545,265,500,225)
drawText(windowSurface, tweet, BLACK, textRect, basicFont, True, None)
# Display everything
pygame.display.flip()
# Beer, on Pin 23.
def doAClick(channel):
currentTime = int(time.time() * FlowMeter.MS_IN_A_SECOND)
if fm.enabled == True:
fm.update(currentTime)
# Root Beer, on Pin 24.
def doAClick2(channel):
currentTime = int(time.time() * FlowMeter.MS_IN_A_SECOND)
if fm2.enabled == True:
fm2.update(currentTime)
def tweetPour(theTweet):
try:
t.statuses.update(status=theTweet)
except:
logging.warning('Error tweeting: ' + theTweet + "\n")
GPIO.add_event_detect(23, GPIO.RISING, callback=doAClick, bouncetime=20) # Beer, on Pin 23
GPIO.add_event_detect(24, GPIO.RISING, callback=doAClick2, bouncetime=20) # Root Beer, on Pin 24
# main loop
while True:
# Handle keyboard events
for event in pygame.event.get():
if event.type == QUIT or (event.type == KEYUP and event.key == K_ESCAPE):
GPIO.cleanup()
pygame.quit()
sys.exit()
elif event.type == KEYUP and event.key == K_1:
fm.enabled = not(fm.enabled)
elif event.type == KEYUP and event.key == K_2:
fm2.enabled = not(fm2.enabled)
elif event.type == KEYUP and event.key == K_9:
fm.clear()
elif event.type == KEYUP and event.key == K_0:
fm2.clear()
currentTime = int(time.time() * FlowMeter.MS_IN_A_SECOND)
if currentTime - lastTweet < 5000: # Pause for 5 seconds after tweeting to show the tweet
view_mode = 'tweet'
else:
view_mode = 'normal'
if (fm.thisPour > 0.23 and currentTime - fm.lastClick > 10000): # 10 seconds of inactivity causes a tweet
tweet = "Someone just poured " + fm.getFormattedThisPour() + " of " + fm.getBeverage() + " from the Adafruit kegomatic!"
lastTweet = int(time.time() * FlowMeter.MS_IN_A_SECOND)
fm.thisPour = 0.0
tweetPour(tweet)
if (fm2.thisPour > 0.23 and currentTime - fm2.lastClick > 10000): # 10 seconds of inactivity causes a tweet
tweet = "Someone just poured " + fm2.getFormattedThisPour() + " of " + fm2.getBeverage() + " from the Adafruit kegomatic!"
lastTweet = int(time.time() * FlowMeter.MS_IN_A_SECOND)
fm2.thisPour = 0.0
tweetPour(tweet)
# Update the screen
renderThings(fm, fm2, tweet, windowSurface, basicFont)