I'm trying to create a sketch that gets a request from a browser and displays an image that relates to it. I've mashed a couple of examples and almost got it working. There appears to be a problem with the way in which i loop [or don't] through the 'showSprite'/'showHeart' functions.
I'm a little lost with it. Here's the sketch:
#include <Spi.h>
#include <Ethernet.h>
#include <string.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 0, 10 };
byte subnet[] = {255, 255, 255, 0 };
int rowA[] = {9,8,7,6,5,4,3,2};
int colA[] = {17,16,15,14,13,12,11,10};
char cmd[15];
int speed = 5; //the delay time in milliseconds
int pauseDelay = 1; //the number of milliseconds to display each scanned line
Server server(80);
#define bufferMax 128
int bufferSize;
char buffer[bufferMax];
byte data[] = {0,0,0,0,0,0,0,0};
const int powers[] = {1,2,4,8,16,32,64,128};
void setup()
{
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
for(int i = 0; i <8; i++){ //Set the 16 pins used to control the array as OUTPUTs
pinMode(rowA[i], OUTPUT);
pinMode(colA[i], OUTPUT);
}
}
void loop()
{
Client client = server.available();
if (client)
{
WaitForRequest(client);
ParseReceivedRequest(client);
PerformRequestedCommands(client);
client.stop();
}
}
void WaitForRequest(Client client) // Sets buffer[] and bufferSize
{
bufferSize = 0;
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.print("i have received the request and i'm <a href='http://www.testingURL.com'>processing</a> it");
break;
}
if (c == '\n')
break;
else
if (bufferSize < bufferMax)
buffer[bufferSize++] = c;
else
break;
}
}
// PrintNumber("bufferSize", bufferSize);
}
void ParseReceivedRequest(Client client)
{
Serial.println("in ParseReceivedRequest");
Serial.println(buffer);
char* slash1;
char* slash2;
slash1 = strstr(buffer, "/") + 1; // Look for first slash
slash2 = strstr(slash1, "/") + 1; // second slash
//PrintString("slash1",slash1);
//PrintString("slash2",slash2);
cmd[0] = 0;//create array to fill with the main command
strncat(cmd, slash1, slash2-slash1-1);
client.print(" VAR = ");
client.print(cmd);
}
void PerformRequestedCommands(Client client)
{
if ( strcmp(cmd,"heart") == 0 ) showHeart(client);
}
void showHeart(Client client){
client.print(" HEART REQUESTED ");
data[0] = B01101100; //row 1s bit mask (1 LED is on 0 LED is off)
data[1] = B11111110; //row 1s bit mask (1 LED is on 0 LED is off)
data[2] = B11111110; //row 1s bit mask (1 LED is on 0 LED is off)
data[3] = B11111110; //row 1s bit mask (1 LED is on 0 LED is off)
data[4] = B01111100; //row 1s bit mask (1 LED is on 0 LED is off)
data[5] = B00111000; //row 1s bit mask (1 LED is on 0 LED is off)
data[6] = B00010000; //row 1s bit mask (1 LED is on 0 LED is off)
data[7] = B00000000; //row 1s bit mask (1 LED is on 0 LED is off)
showSprite(speed);
}
void showSprite(int speed2){
for(int iii = 0; iii < speed2; iii++){ //show the current frame speed2 times
for(int column = 0; column < 8; column++){ //iterate through each column
for(int i = 0; i < 8; i++){
digitalWrite(rowA[i], LOW); //turn off all row pins
}
for(int i = 0; i < 8; i++){ //Set only the one pin
if(i == column){ digitalWrite(colA[i], LOW);} //turns the current row on
else{ digitalWrite(colA[i], HIGH); }//turns the rest of the rows off
}
for(int row = 0; row < 8; row++){ //iterate through each pixel in the current column
int bit = (data[column] >> row) & 1;
if(bit == 1){
digitalWrite(rowA[row], HIGH); //if the bit in the data array is set turn the LED on
}
}
delay(pauseDelay); //leave the column on for pauseDelay microseconds (too high a delay causes flicker)
}
}
}
When this runs and gets a request for a heart the sketch output in the browser as expected however the matrix only briefly shows the first five lines of the heart. It's obviously not looping through the showHeart func right. Can anyone point out where i've gone wrong?
Any help is really appreciated!