Created
October 19, 2016 18:21
-
-
Save ahmadx87/f59adac4269f739bba0df10a30256053 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
#include <ESP8266WiFi.h> | |
#include "FastLED.h" | |
#include "EEPROM.h" | |
#define DATA_PIN 4 | |
#define LED_TYPE WS2811 | |
#define COLOR_ORDER GRB | |
CRGB * leds; | |
uint16_t NUM_LEDS = 1; | |
CLEDController * pLed = NULL; | |
String inData; | |
uint8_t FPS=100; //FRAMES_PER_SECOND | |
uint8_t BRIGHTNESS=100; | |
int DURATION=10; | |
uint8_t animationList[51]; | |
uint8_t animationListLength; | |
unsigned long previousMillis=0; | |
uint8_t gCurrentPatternNumber = 0; | |
bool initSetup=true; | |
WiFiServer server(80); | |
inline void setupFastLED() | |
{ | |
leds = (CRGB*)malloc(3*NUM_LEDS); | |
memset(leds, 0, 3*NUM_LEDS); | |
pLed = new WS2811Controller800Khz<DATA_PIN, COLOR_ORDER>(); | |
FastLED.addLeds(pLed, leds, NUM_LEDS); | |
FastLED.clear(); | |
FastLED.show(); | |
} | |
void setup() { | |
delay(30); // 300 milisecond delay for recovery | |
EEPROM.begin(200); | |
Serial.begin(115200); | |
NUM_LEDS=EEPROM.read(0); | |
setupFastLED(); | |
initHardware(); | |
cWiFi(); | |
loadConfig(); | |
FastLED.setBrightness(BRIGHTNESS); | |
gCurrentPatternNumber = animationList[0]; // Index number of which pattern is current | |
server.begin(); | |
} | |
// List of patterns to cycle through. Each is defined as a separate function below. | |
typedef void (*SimplePatternList[])(); | |
SimplePatternList gPatterns = { solidColor, rainbow, rainbowWithGlitter, confetti, sinelon, juggle, bpm }; | |
uint8_t animListindex= 0; | |
uint8_t gHue = 0; // rotating "base color" used by many of the patterns | |
void loop() | |
{ | |
WiFiClient client = server.available(); | |
if (client) { | |
// Read the first line of the request | |
String req = client.readStringUntil('\r'); | |
Serial.println("req= " + req); | |
inData=req.substring(req.indexOf("+")+1,req.lastIndexOf("+")); | |
Serial.println("inData: "+ inData); | |
Serial.println("done--------"); | |
GetConfig(inData); | |
client.flush(); | |
client.print("HTTP/1.1 200 OK\r\n"); | |
} | |
// Call the current pattern function once, updating the 'leds' array | |
gPatterns[gCurrentPatternNumber](); | |
// send the 'leds' array out to the actual LED strip | |
FastLED.show(); | |
// insert a delay to keep the framerate modest | |
FastLED.delay(1000/FPS); | |
EVERY_N_MILLISECONDS( 20 ) { gHue++; } // slowly cycle the "base color" through the rainbow | |
if (EveryNSec( DURATION )) { nextPattern(); FastLED.setBrightness(BRIGHTNESS); } // change patterns periodically | |
} | |
#define ARRAY_SIZE(A) (sizeof(A) / sizeof((A)[0])) | |
void nextPattern() | |
{ | |
animListindex=(animListindex+1)% animationListLength; | |
if (animListindex==animationListLength) animListindex=0; | |
gCurrentPatternNumber = animationList[animListindex]; | |
} | |
void solidColor(){ | |
for (int i=0; i<NUM_LEDS; i++){ | |
leds[i]=CRGB(SolidColorR,SolidColorG,SolidColorB); | |
} | |
} | |
void rainbow() | |
{ | |
// FastLED's built-in rainbow generator | |
fill_rainbow( leds, NUM_LEDS, gHue, 7); | |
} | |
void rainbowWithGlitter() | |
{ | |
// built-in FastLED rainbow, plus some random sparkly glitter | |
rainbow(); | |
addGlitter(80); | |
} | |
void rainbowWithGlitterHalfBright(){ | |
//Similar to rainbowWithGlitter but brightness is reduced to show sparks (at full brightness) more brightly. | |
FastLED.setBrightness(255); | |
rainbow(); | |
for (int i=0; i<NUM_LEDS; i++){ | |
leds[i].fadeToBlackBy(210); | |
} | |
addGlitter(30); | |
} | |
void addGlitter( fract8 chanceOfGlitter) | |
{ | |
if( random8() < chanceOfGlitter) { | |
leds[ random16(NUM_LEDS) ] += CRGB::White; | |
} | |
} | |
void confetti() | |
{ | |
// random colored speckles that blink in and fade smoothly | |
fadeToBlackBy( leds, NUM_LEDS, 10); | |
int pos = random16(NUM_LEDS); | |
leds[pos] += CHSV( gHue + random8(64), 255, 255); | |
} | |
void sinelon() | |
{ | |
// a colored dot sweeping back and forth, with fading trails | |
fadeToBlackBy( leds, NUM_LEDS, 20); | |
int pos = beatsin16(13,0,NUM_LEDS); | |
leds[pos] += CHSV( gHue, 255, 192); | |
} | |
void bpm() | |
{ | |
// colored stripes pulsing at a defined Beats-Per-Minute (BPM) | |
uint8_t BeatsPerMinute = 62; | |
CRGBPalette16 palette = PartyColors_p; | |
uint8_t beat = beatsin8( BeatsPerMinute, 64, 255); | |
for( int i = 0; i < NUM_LEDS; i++) { //9948 | |
leds[i] = ColorFromPalette(palette, gHue+(i*2), beat-gHue+(i*10)); | |
} | |
} | |
void juggle() { | |
// eight colored dots, weaving in and out of sync with each other | |
fadeToBlackBy( leds, NUM_LEDS, 20); | |
byte dothue = 0; | |
for( int i = 0; i < 3; i++) { | |
leds[beatsin16(i+7,0,NUM_LEDS)] |= CHSV(dothue, 200, 255); | |
dothue += 32; | |
} | |
} | |
void solidColorCycle(){ | |
//cycle through solid colors. | |
fill_solid( leds, NUM_LEDS, CHSV(gHue,255,255)); | |
} | |
//get cofiguration and mode from received data. | |
void GetConfig(String inData) | |
{ | |
//look if brightness has been changed, the number between I's is the assigned brightness. | |
if (inData.indexOf("I")!=-1){ | |
BRIGHTNESS=str2int(inData.substring(inData.indexOf("I")+1,inData.lastIndexOf("I"))); | |
EEPROMupdate(0,BRIGHTNESS); | |
FastLED.setBrightness(BRIGHTNESS); | |
} | |
//Constant color mode. input format CR100RG200GB254B | |
if (inData.indexOf("C")!=-1){ //If a "C" is found in the incoming string it is constant color mode. | |
SolidColorR=str2int(inData.substring(inData.indexOf("R")+1,inData.lastIndexOf("R"))); | |
SolidColorG=str2int(inData.substring(inData.indexOf("G")+1,inData.lastIndexOf("G"))); | |
SolidColorB=str2int(inData.substring(inData.indexOf("B")+1,inData.lastIndexOf("B"))); | |
EEPROMupdate(10,SolidColorR); | |
EEPROMupdate(11,SolidColorG); | |
EEPROMupdate(12,SolidColorB); | |
animationList[0]=0; | |
animationList[1]=255; | |
EEPROMupdate(30,0); | |
EEPROMupdate(31,255); | |
animationListLength=1; | |
animListindex=0; | |
gCurrentPatternNumber=0; | |
} | |
//Multi animation mode entered this way M-3-25-3-0-12-M | |
if (inData.indexOf("M")!=-1){ | |
String sequence=inData.substring(inData.indexOf("M")+1,inData.lastIndexOf("M")); | |
Serial.println(sequence); | |
int firstminus=0; | |
int secondminus=0; | |
int i=0; | |
while (1){ | |
firstminus=sequence.indexOf("-",secondminus); | |
secondminus=sequence.indexOf("-",firstminus+1); | |
if (secondminus==-1){ | |
animationList[i]=255; | |
EEPROMupdate(30+i,255); | |
break; | |
} | |
animationList[i]=str2int(sequence.substring(firstminus+1,secondminus))+0; | |
if (animationList[i]<ARRAY_SIZE(gPatterns)) {EEPROMupdate(30+i,animationList[i]);} | |
else {animationList[i]=255; i--;} | |
i++; | |
} | |
animationListLength=i; | |
animListindex=0; | |
gCurrentPatternNumber=animationList[0]; | |
} | |
//Look if Duration has been changed. Duration is time of each animation | |
if (inData.indexOf("D")!=-1){ | |
DURATION=str2int(inData.substring(inData.indexOf("D")+1,inData.lastIndexOf("D"))); | |
DURATION=DURATION+0; | |
EEPROMupdate(2,DURATION); | |
} | |
//Look if FPS has been changed. | |
if (inData.indexOf("F")!=-1){ | |
FPS=str2int(inData.substring(inData.indexOf("F")+1,inData.lastIndexOf("F"))); | |
EEPROMupdate(1,FPS); | |
} | |
} | |
void loadConfig(){ | |
BRIGHTNESS=EEPROM.read(0); | |
Serial.println("BRIGHTNESS :" + String(BRIGHTNESS)); | |
FPS=EEPROM.read(1); | |
if (FPS==0) FPS=100; | |
Serial.println("FPS :" + String(FPS)); | |
DURATION=EEPROM.read(2); | |
Serial.println("Duration :" + String(DURATION)); | |
for (uint8_t i=0; i<30; i++){ | |
animationList[i]=EEPROM.read(30+i); | |
if (animationList[0]==255) {animationList[0]=1; animationList[1]=255; animationListLength=1; break;} | |
if (animationList[i]==0){ | |
SolidColorR=EEPROM.read(10); | |
SolidColorG=EEPROM.read(11); | |
SolidColorB=EEPROM.read(12); | |
} | |
if (EEPROM.read(30+i)==255) { | |
animationListLength=i; | |
break;} | |
} | |
} | |
int str2int(String InputStr){ | |
return InputStr.toInt(); | |
} | |
boolean EveryNSec(uint8_t period){ | |
unsigned long currentMillis = millis(); | |
if (currentMillis - previousMillis >= period*1000) { | |
// save the last time you blinked the LED | |
previousMillis = currentMillis; | |
return true; | |
} | |
else {return false;} | |
} | |
void EEPROMupdate(byte address, byte value){ | |
if (EEPROM.read(address)!=value) { | |
EEPROM.write(address,value); | |
EEPROM.commit(); | |
} | |
return; | |
} | |
void initHardware() | |
{ | |
Serial.begin(115200); | |
Serial.setTimeout(50); | |
} | |
void cWiFi() | |
{ | |
Serial.begin(115200); | |
Serial.print("\nConnecting to "); Serial.println("ViSTA"); | |
WiFi.begin("ViSTA", "---------"); | |
uint8_t i = 0; | |
while (WiFi.status() != WL_CONNECTED && i++ < 20) delay(500); | |
if(i == 21){ | |
Serial.print("Could not connect to"); Serial.println("WiFi"); | |
while(1) delay(500); | |
} | |
Serial.println(WiFi.localIP()); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment