Last active
September 24, 2015 08:07
-
-
Save ersatzavian/0e19d8dc9cc25ce3f0c1 to your computer and use it in GitHub Desktop.
Flash fun colors when someone tweets
This file contains 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
// AGENT CODE | |
#require "twitter.class.nut:1.1.0" | |
const KEY = "YOUR KEY"; | |
const API_SECRET = "YOUR API SECRET"; | |
const TOKEN = "YOUR TOKEN"; | |
const TOKEN_SECRET = "YOUR TOKEN SECRET"; | |
twitter <- Twitter(KEY, API_SECRET, TOKEN, TOKEN_SECRET); | |
// long-poll twitter's search API with this keyword | |
twitter.stream("electric", function(tweetData) { | |
// notify the device any time we see a tweet with the keyword | |
device.send("tweet", null); | |
server.log(tweetData.user.screen_name+": "+tweetData.text); | |
}); |
This file contains 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
// DEVICE CODE | |
#require "ws2812.class.nut:2.0.1" | |
hardware.spi257.configure(MSB_FIRST, 7500); | |
leds <- WS2812(hardware.spi257, 5); | |
function rnd(max) { return math.rand() % max; } | |
// store the color globally so we can fade it out | |
local color = [0, 0, 0]; | |
// light a random color when a new tweet arrives | |
agent.on("tweet", function(data) { | |
color = [ rnd(64), rnd(64), rnd(64) ]; | |
leds.fill(color); | |
leds.draw(); | |
// start decreasing the brightness in 50 milliseconds | |
imp.wakeup(0.05, fade); | |
}); | |
function fade() { | |
// lower the brightness smoothly | |
for (local i = 0; i < 3; i++) { | |
color[i] = (color[i].tofloat() / 1.05).tointeger(); | |
} | |
leds.fill(color); | |
leds.draw(); | |
// only do the next step in the fade if the LEDs are still lit | |
if (color[0] > 0 || color[1] > 0 || color[2] > 0) { | |
// decrease the brightness again in 50 milliseconds | |
imp.wakeup(0.05, fade); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment