Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated NinjaBlockEthernet Library #2

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 45 additions & 27 deletions NinjaBlockEthernet/NinjaBlockEthernet.cpp
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,15 @@ const byte kHeaderLength = sizeof(kStrHeaderEnd);
// will keep reading bytes until it has matched the header, or it has read all available bytes
inline void skipHeader(const int bytesAvailable, int &bytesRead) {
//skip past header
//Serial.print("_Head=");
for (uint8_t matching=0
; (matching < kHeaderLength) && (bytesRead < bytesAvailable)
; bytesRead++) {
matching = ((recvclient.read() == kStrHeaderEnd[matching]) ? matching+1 : 0);
char curByte = recvclient.read();
//Serial.print(curByte);
matching = ((curByte == kStrHeaderEnd[matching]) ? matching+1 : 0);
}
//Serial.println("");
}

const char kCharInvertedCommas = '\"';
Expand Down Expand Up @@ -179,11 +183,17 @@ char * valueString(const char *name, char *data, int &index, const int length) {
}
if (index < length) {
//if searching for a string seek end of string ("), otherwise (,) ends an int
char endChar = (data[index-1]==kCharInvertedCommas) ? kCharInvertedCommas : ',';
char endChar = ',';
if (data[index-1]==kCharInvertedCommas) endChar = kCharInvertedCommas;
if (data[index]=='{') endChar = '}';

int start = index;
while ((index < length) && (data[index] != endChar)) {
while ((index < length) && (data[index] != endChar) && (data[index] != '}')) {
index++;
}

if((index < length) && (endChar == '}')) index++;

if (index < length) {
data[index] = '\0'; // insert string terminator after value (string or int)
result = &data[start];
Expand All @@ -194,7 +204,8 @@ char * valueString(const char *name, char *data, int &index, const int length) {

bool NinjaBlockClass::receiveConnected(void) {
bool gotHeader = false;
bool gotData = false;
bool gotData = false;
IsTick = false;
int bytesAvailable = recvclient.available();
if (bytesAvailable > 0)
{
Expand All @@ -213,10 +224,13 @@ bool NinjaBlockClass::receiveConnected(void) {

char data[DATA_SIZE];
//read data into array eg. {"DEVICE":[{"G":"0","V":0,"D":1000,"DA":"FFFFFF"}]}
Serial.print("_Recv=");
for (bytesRead=0; bytesRead<bytesAvailable; bytesRead++) {
data[bytesRead] = recvclient.read();
//Serial.print(data[bytesRead]);
Serial.print(data[bytesRead]);
}
Serial.println("");
data[bytesRead] = '\0'; //terminate data as string
bytesRead = 0;
char *strVal;
Expand All @@ -238,39 +252,43 @@ bool NinjaBlockClass::receiveConnected(void) {
// Serial.println(intDID);

int start = bytesRead;
strVal = valueString("DA\":\"", data, bytesRead, bytesAvailable);
if (strVal != NULL) {
strcpy(strDATA, strVal);
IsDATAString = true;
gotData = true;
// Serial.print("strDATA=");
// Serial.println(strDATA);
}
else { // may be an int value
bytesRead = start; // reset to where we were before attempting (data is unmodified if NULL was returned)
strVal = valueString("DA\":", data, bytesRead, bytesAvailable);
if (strVal) {
intDATA = atoi(strVal);
IsDATAString = false;
gotData = true;
// Serial.print("intDATA=");
// Serial.println(intDATA);
}
}
strVal = valueString("DA\":\"", data, bytesRead, bytesAvailable);
if (strVal != NULL) {
strcpy(strDATA, strVal);
IsDATAString = true;
gotData = true;
//Serial.print("strDATA=");
//Serial.println(strDATA);
}
else { // may be an int value
bytesRead = start; // reset to where we were before attempting (data is unmodified if NULL was returned)
strVal = valueString("DA\":", data, bytesRead, bytesAvailable);
if (strVal) {
intDATA = atoi(strVal);
IsDATAString = false;
gotData = true;
// Serial.print("intDATA=");
// Serial.println(intDATA);
}
}


}
}
}

}
else { //no data - NB cloud sent a keep alive tick
IsTick = true;
}
}
if (gotHeader) {
//if a header was received, there was some data after (either json, or some html etc)
//purge and close the stream
recvclient.flush();
delay(100);
recvclient.stop();
delay(100);
}
return gotData;

return gotData || IsTick;
}

NinjaBlockClass NinjaBlock;
5 changes: 3 additions & 2 deletions NinjaBlockEthernet/NinjaBlockEthernet.h
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,9 @@
#include <SPI.h>
#include <Ethernet.h>

#define DATA_SIZE 128
#define DATA_SIZE 150
#define GUID_LEN 36
#define DATA_LEN 96
#define DATA_LEN 128

class EthernetClient;

Expand All @@ -36,6 +36,7 @@ class NinjaBlockClass {
int intDATA;
char strDATA[DATA_LEN];
bool IsDATAString;
bool IsTick;

int begin();
void send(int data);
Expand Down
94 changes: 60 additions & 34 deletions NinjaBlockEthernet/examples/Basic_Comms/Basic_Comms.ino
Original file line number Diff line number Diff line change
Expand Up @@ -114,40 +114,66 @@ void setup(){
void loop() {

if(NinjaBlock.receive()) {
// If this function returns true, there are commands (data) from the server
// Return values are:
// NinjaBlock.strGUID
// NinjaBlock.intVID
// NinjaBlock.intDID
// NinjaBlock.intDATA - if data is integer
// NinjaBlock.strDATA - if data is string (note char[64])

if (NinjaBlock.IsDATAString) {

// Serial.print("strDATA=");
// Serial.println(NinjaBlock.strDATA);

if (NinjaBlock.intDID == 1000) {

// FFFFFF is "white" in the RGB widget we identified as
if (strcmp(NinjaBlock.strDATA,"FFFFFF") == 0) {
#if ENABLE_SERIAL
Serial.println("LED ON");
#endif
digitalWrite(led, HIGH);
} else if (strcmp(NinjaBlock.strDATA,"000000") == 0) {
#if ENABLE_SERIAL
Serial.println("LED OFF");
#endif
digitalWrite(led, LOW);
}

}
} else {
// Do something with int data
// Serial.print("intDATA=");
// Serial.println(NinjaBlock.intDATA);
}
// If this function returns true, there are either commands (data) from the server
// or a keep alive Tick has been sent
//First, you MUST check if it is a Tick
if(NinjaBlock.IsTick) { //There is no input, just a keep alive update

//Make sure any devices tha don't regularly send data to the cloud
// are updated here or they will time out

} else { //There is data to respond to

// Return values are:
// NinjaBlock.strGUID
// NinjaBlock.intVID
// NinjaBlock.intDID
// NinjaBlock.intDATA - if data is integer
// NinjaBlock.strDATA - if data is string (note char[64])

if (NinjaBlock.IsDATAString) {

// Serial.print("strDATA=");
// Serial.println(NinjaBlock.strDATA);

// NOTE: This library can now read serialised JSON
// It will appear as a DATA String, but will contain multiple values
// If your DeviceID sends serialised JSON adapt the following code:
// (responds to a Light protocol sending "on":true and "bri":0-254)
//
// int bytesRead = 0;
// int bytesAvailable = strlen(NinjaBlock.strDATA);
// if (jsonString("on\\\":true", NinjaBlock.strDATA, bytesRead, bytesAvailable) != NULL) {
// char * strValue = jsonString("bri\\\":", NinjaBlock.strDATA, bytesRead, bytesAvailable);
// if(strValue != NULL) {
// int brightness = atoi(strValue);
// char buf[8]; //Using a buffer for String->char[]
// itoa(brightness,buf,10);
// analogWrite(led, brightness);
// }
// }

if (NinjaBlock.intDID == 1000) {

// FFFFFF is "white" in the RGB widget we identified as
if (strcmp(NinjaBlock.strDATA,"FFFFFF") == 0) {
#if ENABLE_SERIAL
Serial.println("LED ON");
#endif
digitalWrite(led, HIGH);
} else if (strcmp(NinjaBlock.strDATA,"000000") == 0) {
#if ENABLE_SERIAL
Serial.println("LED OFF");
#endif
digitalWrite(led, LOW);
}

}
} else {
// Do something with int data
// Serial.print("intDATA=");
// Serial.println(NinjaBlock.intDATA);
}

}

Expand Down
110 changes: 71 additions & 39 deletions NinjaBlockEthernet/examples/Ninja_Matrix/Ninja_Matrix.ino
Original file line number Diff line number Diff line change
Expand Up @@ -124,45 +124,77 @@ void loop() {

if(NinjaBlock.receive())
{
// If this function returns true, there are commands (data) from the server
// Return values are:
// NinjaBlock.strGUID
// NinjaBlock.intVID
// NinjaBlock.intDID
// NinjaBlock.intDATA - if data is integer
// NinjaBlock.strDATA - if data is string

digitalWrite(statusled, HIGH); // flash the status led on data received

Serial.print("DA:");
if (NinjaBlock.IsDATAString) {
Serial.print(NinjaBlock.strDATA);
if (NinjaBlock.intDID == 7000) {
dmd.drawMarquee(NinjaBlock.strDATA,strlen(NinjaBlock.strDATA),(32*DISPLAYS_ACROSS)-1,0, WHITE, BLACK);
marquee = true;
// TODO: Draw strings that will fit rather than scrolling them, change font, draw pixels, etc.
}
} else {
// Do something with int data
Serial.print("INT=");
Serial.print(NinjaBlock.intDATA);
}

Serial.print(" DID:");
Serial.println(NinjaBlock.intDID);
}

if ( marquee ) {
long start=millis();
long timer=start;
boolean ret=false;
while(!ret){
if ((timer+30) < millis()) {
ret=dmd.stepMarquee(-1,0);
timer=millis();
}
}
}
// If this function returns true, there are either commands (data) from the server
// or a keep alive Tick has been sent
//First, you MUST check if it is a Tick
if(NinjaBlock.IsTick) { //There is no input, just a keep alive update

//Make sure any devices tha don't regularly send data to the cloud
// are updated here or they will time out

} else { //There is data to respond to

// Return values are:
// NinjaBlock.strGUID
// NinjaBlock.intVID
// NinjaBlock.intDID
// NinjaBlock.intDATA - if data is integer
// NinjaBlock.strDATA - if data is string (note char[64])

if (NinjaBlock.IsDATAString) {

// Serial.print("strDATA=");
// Serial.println(NinjaBlock.strDATA);

// NOTE: This library can now read serialised JSON
// It will appear as a DATA String, but will contain multiple values
// If your DeviceID sends serialised JSON adapt the following code:
// (responds to a Light protocol sending "on":true and "bri":0-254)
//
// int bytesRead = 0;
// int bytesAvailable = strlen(NinjaBlock.strDATA);
// if (jsonString("on\\\":true", NinjaBlock.strDATA, bytesRead, bytesAvailable) != NULL) {
// char * strValue = jsonString("bri\\\":", NinjaBlock.strDATA, bytesRead, bytesAvailable);
// if(strValue != NULL) {
// int brightness = atoi(strValue);
// char buf[8]; //Using a buffer for String->char[]
// itoa(brightness,buf,10);
// analogWrite(led, brightness);
// }
// }

digitalWrite(statusled, HIGH); // flash the status led on data received

Serial.print("DA:");
if (NinjaBlock.IsDATAString) {
Serial.print(NinjaBlock.strDATA);
if (NinjaBlock.intDID == 7000) {
dmd.drawMarquee(NinjaBlock.strDATA,strlen(NinjaBlock.strDATA),(32*DISPLAYS_ACROSS)-1,0, WHITE, BLACK);
marquee = true;
// TODO: Draw strings that will fit rather than scrolling them, change font, draw pixels, etc.
}
} else {
// Do something with int data
Serial.print("INT=");
Serial.print(NinjaBlock.intDATA);
}

Serial.print(" DID:");
Serial.println(NinjaBlock.intDID);
}
}

if ( marquee ) {
long start=millis();
long timer=start;
boolean ret=false;
while(!ret){
if ((timer+30) < millis()) {
ret=dmd.stepMarquee(-1,0);
timer=millis();
}
}
}

}