-
Notifications
You must be signed in to change notification settings - Fork 0
/
colourTweet.py
146 lines (127 loc) · 4.73 KB
/
colourTweet.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
##################################################
#
# Program based on the excellent example by
# http://computers.tutsplus.com/tutorials/how-to-build-a-tweet-controlled-rgb-lcd--cms-20515
# By by Jeremy Blythe
#
# Modified by Mark Routledge to work with Hacked Dioder LEDs using Co-Op-Pi
#
# Created 1st June 2014
#
##################################################
import sys
from tweepy.streaming import StreamListener
from tweepy import OAuthHandler
from tweepy import Stream
import json
import threading
import time
import textwrap
import subprocess
import struct
from PyMata.pymata import PyMata
# Give the pins names:
REDPIN = 5
GREENPIN = 3
BLUEPIN = 6
# Create an instance of PyMata.
SERIAL_PORT = "/dev/ttyS0"
firmata = PyMata( SERIAL_PORT, max_wait_time=5 )
# initialize the digital pin as an output.
firmata.set_pin_mode( REDPIN, firmata.PWM, firmata.DIGITAL)
firmata.set_pin_mode( GREENPIN, firmata.PWM, firmata.DIGITAL)
firmata.set_pin_mode( BLUEPIN, firmata.PWM, firmata.DIGITAL)
api_key=''
api_secret=''
access_token_key=''
access_token_secret=''
class DisplayLoop(StreamListener):
"""
This class is a listener for tweet stream data.
It's also callable so it can run the main display
thread loop to update the display.
"""
def __init__(self):
self.colour_map = {'red':'REDDDDD',
'yellow':'YELLOWWWWWW',
'green':'GREEEEN',
'blue':'BLUUUUUUE',
'cyan':'CYANNNNNN',
'magenta':'MAGENTAAAAAA',
'rgb':'RGB',
'white':'WHITE'}
self.msglist = []
self.pos = 0
self.tweet = 'Nothing yet'
def set_colour(self):
words = self.tweet.lower().split(' ')
use_default = True
for w in words[:]:
if w in self.colour_map:
print(self.colour_map[w])
print(words.index(w))
if(words[words.index(w)]=='rgb'):
print(words[words.index(w)+1])
#uptohere
rgb=struct.unpack('BBB',words[words.index(w)+1].decode('hex'))
print ('RGB VALUES:', rgb)
firmata.analog_write( REDPIN, rgb[0])
firmata.analog_write( GREENPIN, rgb[1])
firmata.analog_write( BLUEPIN, rgb[2])
elif (words[words.index(w)]=='red'):
firmata.analog_write( REDPIN, 255)
firmata.analog_write( GREENPIN, 0)
firmata.analog_write( BLUEPIN, 0)
elif (words[words.index(w)]=='green'):
firmata.analog_write( REDPIN, 0)
firmata.analog_write( GREENPIN, 255)
firmata.analog_write( BLUEPIN, 0)
elif (words[words.index(w)]=='blue'):
firmata.analog_write( REDPIN, 0)
firmata.analog_write( GREENPIN, 0)
firmata.analog_write( BLUEPIN, 255)
elif (words[words.index(w)]=='yellow'):
firmata.analog_write( REDPIN, 255)
firmata.analog_write( GREENPIN, 255)
firmata.analog_write( BLUEPIN, 0)
elif (words[words.index(w)]=='magenta'):
firmata.analog_write( REDPIN, 255)
firmata.analog_write( GREENPIN, 0)
firmata.analog_write( BLUEPIN, 255)
elif (words[words.index(w)]=='cyan'):
firmata.analog_write( REDPIN, 0)
firmata.analog_write( GREENPIN, 255)
firmata.analog_write( BLUEPIN, 255)
elif (words[words.index(w)]=='white'):
firmata.analog_write( REDPIN, 255)
firmata.analog_write( GREENPIN, 255)
firmata.analog_write( BLUEPIN, 255)
use_default = False
break
if use_default:
print('LEDs set to white!!!')
#SET THE LED COLOUR TO WHITE
def on_data(self, data):
tweet_data = json.loads(data)
self.tweet = tweet_data['text'].encode('ascii',
errors='backslashreplace')
self.msglist = [x.ljust(16) for x in
textwrap.wrap(str(self.tweet),16)]
self.pos = 0
self.set_colour()
return True
def on_error(self, status):
print status
def write_message(self,msg):
print (msg)
def __call__(self):
while True:
time.sleep(1)
display_loop_instance = DisplayLoop()
# Start the thread running the callable
threading.Thread(target=display_loop_instance).start()
# Log in to twitter and start the tracking stream
auth = OAuthHandler(api_key, api_secret)
auth.set_access_token(access_token_key, access_token_secret)
stream = Stream(auth, display_loop_instance)
stream.filter(track=['AMC_PI'])