Created
November 19, 2016 00:26
-
-
Save kordless/e44306568d90bf1baf046dcf8d2c0500 to your computer and use it in GitHub Desktop.
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
// This #include statement was automatically added by the Particle IDE. | |
#include "HttpClient/HttpClient.h" | |
#include "InternetButton/InternetButton.h" | |
#include "WebServer.h" | |
// network stuff | |
TCPClient webClient; | |
WebServer webserver("/", 80); | |
unsigned char httpProto[100]; | |
String httpString = ""; | |
char myIpAddress[24]; | |
HttpClient http; | |
http_header_t headers[] = { | |
{ "Accept" , "*/*"}, | |
{ NULL, NULL } // NOTE: Always terminate headers will NULL | |
}; | |
http_request_t request; | |
http_response_t response; | |
// button | |
InternetButton b = InternetButton(); | |
// vars | |
int speakerPin = D1; | |
int fastPeriod = 10; | |
int lastContact = 0; | |
int lastTimerAdjust = 0; | |
int currentTime = 0; | |
int adjustCount = 0; | |
// current color | |
int color[] = { 0, 50, 0, 0 }; // light green | |
// light circle arrays | |
int lightRed[12]; | |
int lightGreen[12]; | |
int lightBlue[12]; | |
int darkBlue[12]; | |
int darkGreen[12]; | |
int darkRed[12]; | |
int offset = 0; | |
int lastoffset = 0; | |
// draw the current lights | |
void drawLights() { | |
for (int k=11; k >= 0; k--) { | |
bool drawLight = true; | |
int l = ((k + offset) % 12); | |
if (offset != lastoffset) { | |
lastoffset = offset; | |
drawLight = true; | |
} | |
if (lightRed[k] != darkRed[k]) { | |
drawLight = true; | |
} | |
if (lightGreen[k] != darkGreen[k]) { | |
drawLight = true; | |
} | |
if (lightBlue[k] != darkBlue[k]) { | |
drawLight = true; | |
} | |
if (drawLight == true) { | |
darkRed[k] = lightRed[k]; | |
darkGreen[k] = lightGreen[k]; | |
darkBlue[k] = lightBlue[k]; | |
b.ledOn(l,lightRed[k],lightGreen[k],lightBlue[k]); | |
} | |
} | |
} | |
Timer drawTimer(50, drawLights); | |
// rotate the current lights | |
int rotateClockwisePeriod = fastPeriod; | |
int rotateClockwiseTime = 10; // in seconds | |
bool slowClockwiseFlag = false; | |
int adjustClockwiseCount = 0; | |
void rotateLightsClockwise() { | |
offset = offset + 1; | |
} | |
Timer rotateClockwiseTimer(fastPeriod, rotateLightsClockwise); | |
// rotate the current lights | |
int rotateCounterClockwisePeriod = fastPeriod; | |
int rotateCounterClockwiseTime = 10; // in seconds | |
bool slowCounterClockwiseFlag = false; | |
int adjustCounterClockwiseCount = 0; | |
void rotateLightsCounterClockwise() { | |
offset = offset - 1; | |
} | |
Timer rotateCounterClockwiseTimer(fastPeriod, rotateLightsCounterClockwise); | |
// turn on X many lights if X many aren't already on. | |
void turnOn(int number, int fhred, int fhgreen, int fhblue, int shred, int shgreen, int shblue) { | |
// check to see if they are already set to what we want somewhere along in the array | |
// set new ones at origin | |
for (int l=0; l<=11; l++) { | |
if (l < number) { | |
lightRed[l] = fhred; | |
lightGreen[l] = fhgreen; | |
lightBlue[l] = fhblue; | |
} else { | |
lightRed[l] = shred; | |
lightGreen[l] = shgreen; | |
lightBlue[l] = shblue; | |
} | |
} | |
} | |
// turn off all lights | |
void allOff() { | |
offset = 0; | |
for (int l=0; l <= 11; l++) { | |
lightRed[l] = 0; | |
lightGreen[l] = 0; | |
lightBlue[l] = 0; | |
} | |
} | |
// stop timers | |
void stopTimers() { | |
rotateCounterClockwiseTimer.stop(); | |
rotateClockwiseTimer.stop(); | |
drawTimer.stop(); | |
} | |
// reset timers | |
void resetTimers(int period) { | |
rotateClockwisePeriod = period; | |
rotateCounterClockwisePeriod = period; | |
slowClockwiseFlag = false; | |
slowCounterClockwiseFlag = false; | |
rotateCounterClockwiseTimer.changePeriod(rotateClockwisePeriod); | |
rotateClockwiseTimer.changePeriod(rotateCounterClockwisePeriod); | |
rotateCounterClockwiseTimer.stop(); | |
rotateClockwiseTimer.stop(); | |
lastTimerAdjust = millis(); | |
} | |
// slow counter clockwise rotation | |
void slowClockwiseRotation(int rotateTime) { | |
int delta = millis() - lastTimerAdjust; // millis since we last adjusted timer | |
// if longer than a half a second, then increase the timer a little | |
if (delta > 250) { | |
adjustClockwiseCount++; | |
rotateClockwisePeriod = rotateClockwisePeriod + 1; // slow a little each time | |
if (adjustClockwiseCount > (rotateTime*5/3)*3) { // pop on more slowing on the last 4th | |
rotateClockwisePeriod = rotateClockwisePeriod + (100/adjustClockwiseCount-rotateTime*4); | |
} | |
rotateClockwiseTimer.changePeriod(rotateClockwisePeriod); | |
drawTimer.start(); | |
lastTimerAdjust = millis(); | |
// stop timer if we've adjusted long enough (250ms each cycle - multiply by 4 - 1000ms/250ms = 4) | |
if (adjustClockwiseCount > rotateTime*4 ) { | |
rotateClockwiseTimer.stop(); | |
adjustClockwiseCount = 0; | |
rotateClockwisePeriod = fastPeriod; | |
slowClockwiseFlag = false; | |
} | |
} | |
} | |
// slow counter clockwise rotation | |
void slowCounterClockwiseRotation(int rotateTime) { | |
int delta = millis() - lastTimerAdjust; // millis since we last adjusted timer | |
// if longer than a half a second, then increase the timer a little | |
if (delta > 250) { | |
adjustCounterClockwiseCount++; | |
rotateCounterClockwisePeriod = rotateCounterClockwisePeriod + 1; | |
if (adjustCounterClockwiseCount > (rotateTime*5/3)*3) { // pop on more slowing on the last 4th | |
rotateCounterClockwisePeriod = rotateCounterClockwisePeriod + adjustCounterClockwiseCount; | |
} | |
rotateCounterClockwiseTimer.changePeriod(rotateCounterClockwisePeriod); | |
drawTimer.start(); | |
lastTimerAdjust = millis(); | |
// stop timer if we've adjusted long enough | |
if (adjustCounterClockwiseCount > rotateTime*4 ) { | |
rotateCounterClockwiseTimer.stop(); | |
adjustCounterClockwiseCount = 0; | |
slowCounterClockwiseFlag = false; | |
} | |
} | |
} | |
// web page stuff | |
void serveWebpage(WebServer &server, WebServer::ConnectionType type, char *, bool) { | |
// indicate we're active | |
lastContact = millis(); | |
// send headers | |
server.httpSuccess(); | |
// process a GET request | |
if (type == WebServer::POST) { | |
bool repeat; | |
char name[16], value[16]; | |
int fr=0, sr=0, fg=0, sg=0, fb=0, sb=0, num=0, tmr=0, dir=0, slw=0, ins=0; | |
do { | |
repeat = server.readPOSTparam(name, 16, value, 16); | |
if (strcmp(name, "fr") == 0) { | |
fr = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "sr") == 0) { | |
sr = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "fg") == 0) { | |
fg = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "sg") == 0) { | |
sg = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "fb") == 0) { | |
fb = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "sb") == 0) { | |
sb = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "num") == 0) { | |
num = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "tmr") == 0) { | |
tmr = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "dir") == 0) { | |
dir = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "slw") == 0) { | |
slw = strtoul(value, NULL, 10); | |
} | |
if (strcmp(name, "ins") == 0) { | |
ins = strtoul(value, NULL, 0); | |
} | |
} while (repeat); | |
// start it up | |
turnOn(num, fr, fg, fb, sr, sg, sb); | |
if (dir == 1) { | |
// rotate clockwise | |
if (ins > 0) { | |
rotateClockwisePeriod = ins; | |
} | |
resetTimers(rotateClockwisePeriod); | |
rotateClockwiseTimer.start(); | |
if (tmr > 0) { | |
rotateClockwiseTime = tmr; | |
} | |
if (slw == 1) { | |
slowClockwiseFlag = true; | |
} else { | |
slowClockwiseFlag = false; | |
} | |
} | |
if (dir == 2) { | |
// rotate counter clockwise | |
if (ins > 0) { | |
rotateCounterClockwisePeriod = ins; | |
} | |
resetTimers(rotateCounterClockwisePeriod); | |
rotateCounterClockwiseTimer.start(); | |
if (tmr > 0) { | |
rotateCounterClockwiseTime = tmr; | |
} | |
if (slw == 1) { | |
slowCounterClockwiseFlag = true; | |
} else { | |
slowCounterClockwiseFlag = false; | |
} | |
} | |
if (dir == 0) { | |
// stop the rotation timers | |
resetTimers(fastPeriod); | |
} | |
// spit out some html | |
P(message) = "<!DOCTYPE html><html><body>OK</body></html>"; | |
server.printP(message); | |
return; | |
} | |
// if not a POST, just dump stuff | |
P(message) = "<!DOCTYPE html><html><body>OK</body></html>"; | |
server.printP(message); | |
} | |
// setup runs once on start | |
void setup() { | |
// start the business | |
b.begin(); | |
// set network info | |
Particle.variable("ipaddress", myIpAddress, STRING); | |
IPAddress myIp = WiFi.localIP(); | |
sprintf(myIpAddress, "%d.%d.%d.%d", myIp[0], myIp[1], myIp[2], myIp[3]); | |
// start the webserver | |
webserver.begin(); | |
webserver.setDefaultCommand(&serveWebpage); | |
// start time | |
lastContact = millis(); | |
lastTimerAdjust = millis(); | |
// reset the lights and start rotation | |
turnOn(6, 0, 50, 0, 50, 0, 50); | |
resetTimers(fastPeriod); | |
rotateClockwiseTime = 10; | |
rotateClockwiseTimer.start(); | |
slowClockwiseFlag = true; | |
// holler at the server we're started | |
request.hostname = "192.168.1.100"; | |
request.port = 80; | |
request.path = "/python/postPhoton.py?photon=1"; | |
http.get(request, response, headers); | |
} | |
void loop() { | |
// webserver stuff | |
webserver.processConnection(); | |
// check for button | |
if (b.buttonOn(1) || b.buttonOn(2) || b.buttonOn(3) || b.buttonOn(4)) { | |
// indicate we're active | |
lastContact = millis(); | |
// reset the lights and start rotation | |
turnOn(6, 255, 0, 0, 0, 0, 255); | |
resetTimers(50); | |
rotateClockwiseTime = 20; | |
slowClockwiseFlag = true; | |
rotateClockwiseTimer.start(); | |
} | |
// time functions | |
currentTime = millis(); | |
if ( (lastContact + 2400000) < currentTime) { | |
// warn we're going to sleep in 5 minutes | |
allOff(); | |
delay(100); | |
lightRed[6] = 60; | |
delay(100); | |
stopTimers(); | |
} | |
if ( (lastContact + 3000000) < currentTime) { | |
// sleep at 30 minutes idle | |
allOff(); | |
delay(100); | |
lightRed[6] = 20; | |
delay(100); | |
stopTimers(); | |
System.sleep(D6,FALLING); | |
} | |
// slow any animations we're running | |
if ( slowClockwiseFlag == true) { | |
slowClockwiseRotation(rotateClockwiseTime); | |
} | |
if ( slowCounterClockwiseFlag == true) { | |
slowCounterClockwiseRotation(rotateCounterClockwiseTime); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment