Last active
August 29, 2015 14:07
-
-
Save circuitsenses/3bd3da687e140b3eac1b to your computer and use it in GitHub Desktop.
NeoPixel Shield RGB Light Wireless control over Bluetooth
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
/* | |
Adafruit NeoPixel Shield RGB Light Wireless control over Bluetooth | |
Author : Rishi F. | |
Adapted from the code written by - http://kuchenzeit.wordpress.com/ | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files (the "Software"), to deal | |
in the Software without restriction, including without limitation the rights | |
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
copies of the Software, and to permit persons to whom the Software is | |
furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in | |
all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | |
THE SOFTWARE. | |
*/ | |
#include <Adafruit_GFX.h> | |
#include <Adafruit_NeoMatrix.h> | |
#include <Adafruit_NeoPixel.h> | |
#include <SoftwareSerial.h> | |
const int TX_BT = 10; // Red Wire | |
const int RX_BT = 11; // Yellow Wire | |
#define PIN 6 | |
// #define SERIAL_DBG | |
unsigned int cmdOffset = 4; | |
unsigned int xpos = 0; | |
unsigned int ypos = 0; | |
unsigned int R = 0; | |
unsigned int B = 0; | |
unsigned int G = 0; | |
unsigned int I = 0; | |
#define NO_REDRAW 2 | |
#define REDRAW 1 | |
#define MAX_CMD_LEN 16 | |
// Enumerate new commands here | |
enum PhoneCommands | |
{ | |
RESET = 0, | |
// Add more commands Here | |
MAX_PH_CMDS | |
}; | |
SoftwareSerial EzSerial(TX_BT, RX_BT); | |
Adafruit_NeoMatrix matrix = Adafruit_NeoMatrix(5, 8, PIN, | |
NEO_MATRIX_TOP + NEO_MATRIX_RIGHT + | |
NEO_MATRIX_COLUMNS + NEO_MATRIX_PROGRESSIVE, | |
NEO_GRB + NEO_KHZ800); | |
//Process the incoming command from Windows Phone. | |
int processCommand(char* command) { | |
String i = " "; | |
int index = 0; | |
#ifdef SERIAL_DBG | |
Serial.println(command); | |
#endif | |
if(command[0] == 'I') { | |
cmdOffset = 2; | |
while(command[cmdOffset] != '\0') | |
{ | |
i.setCharAt(index, command[cmdOffset++]); | |
index++; | |
} | |
I = i.toInt(); | |
index = 0; | |
cmdOffset = 4; | |
matrix.setBrightness(I); | |
} | |
if(command[0] == 'O') { | |
matrix.fillScreen(0); | |
return NO_REDRAW; | |
} | |
else { | |
xpos = (int)command[0] - 97; | |
ypos = (int)command[2] - 97; | |
// Extract RGB values from the string rx'd | |
R = String(strtok((command + cmdOffset), "_")).toInt(); | |
G = String(strtok(NULL, "_")).toInt(); | |
B = String(strtok(NULL, "_")).toInt(); | |
} | |
return REDRAW; | |
} | |
void sendCmdToPhone(unsigned int cmd) | |
{ | |
if(cmd > MAX_PH_CMDS){ | |
// Event Horizon... | |
while(1); | |
} | |
switch(cmd) | |
{ | |
case RESET: | |
EzSerial.write(strlen("RESET")); | |
EzSerial.print("RESET"); | |
EzSerial.flush(); | |
break; | |
default: | |
#ifdef SERIAL_DBG | |
Serial.println("Command not implemented"); | |
#else | |
break; | |
#endif | |
} | |
} | |
void setup() { | |
#ifdef SERIAL_DBG | |
Serial.begin(115200); | |
Serial.println("USB Connected"); | |
#endif | |
EzSerial.begin(9600); | |
matrix.begin(); | |
matrix.setBrightness(40); | |
matrix.fillScreen(0); | |
matrix.show(); | |
sendCmdToPhone(RESET); | |
} | |
void loop() { | |
if(EzSerial.available()) { | |
int commandSize = (int)EzSerial.read(); | |
if(commandSize < MAX_CMD_LEN) { | |
char command[commandSize]; | |
int commandPos = 0; | |
while(commandPos < commandSize) { | |
if(EzSerial.available()) { | |
command[commandPos] = (char)EzSerial.read(); | |
commandPos++; | |
} | |
} | |
command[commandPos] = 0; | |
if(REDRAW == processCommand(command)) { | |
matrix.drawPixel(ypos, xpos, matrix.Color(R, G, B)); | |
} | |
matrix.show(); | |
EzSerial.flush(); | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment