Created
December 4, 2010 01:07
-
-
Save laclefyoshi/727799 to your computer and use it in GitHub Desktop.
Arduino code using aJson, Matrix and Ethernet
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
/** | |
Copyright: (c) SAEKI Yoshiyasu | |
License : MIT-style license | |
<http://www.opensource.org/licenses/mit-license.php> | |
last updated: 2010/12/03 | |
**/ | |
#include <SPI.h> | |
#include <Ethernet.h> | |
#include <aJSON.h> | |
#include <Sprite.h> | |
#include <Matrix.h> | |
byte mac[] = {0x10, 0x20, 0x30, 0x40, 0x50, 0x60}; // Ethernet Shield MAC-address | |
byte ip[] = {192, 168, 254, 100}; // Ethernet Shield IP-address | |
Server server(9999); // Listen port | |
#define BUFFER_SIZE 40 | |
char buffer[BUFFER_SIZE]; | |
aJsonObject* root = aJson.createObject(); | |
const int DIN = 5; // DotMatrix LED & MAX7219 | |
const int LOAD = 6; | |
const int CLK = 7; | |
Matrix leds = Matrix(DIN, CLK, LOAD, 2); | |
byte mx, my; | |
void setup() { | |
memset(buffer, 0, BUFFER_SIZE); | |
Ethernet.begin(mac, ip); | |
server.begin(); | |
leds.clear(); | |
Serial.begin(9600); | |
} | |
void loop() { | |
Client client = server.available(); | |
if (client) { | |
int bufIndex = 0; | |
while(client.connected()) { | |
if(client.available()) { | |
char c = client.read(); | |
if(bufIndex < BUFFER_SIZE) { | |
buffer[bufIndex] = c; | |
bufIndex++; | |
} else { | |
Serial.println("buffer overflow"); | |
break; | |
} | |
if(c == '\n') { | |
buffer[bufIndex] = '\0'; | |
// Serial.println(buffer); | |
root = aJson.parse(buffer); | |
if(root != NULL) { | |
int i; | |
for(i = 0; i < 8; i++) { | |
leds.write(adjust(mx), i, LOW); // if dp=seg7: not need adjust() | |
leds.write(i, my, LOW); | |
leds.write(i + 8, my, LOW); | |
} | |
aJsonObject* ball = aJson.getObjectItem(root, "ball"); | |
aJsonObject* bx = aJson.getObjectItem(root, "x"); | |
mx = (byte)map(bx->valueint, 0, 300, 7, 0); | |
if(strcmp(ball->valuestring, "red") == 0) { | |
mx += 8; | |
} | |
aJsonObject* by = aJson.getObjectItem(root, "y"); | |
my = (byte)map(by->valueint, 0, 336, 0, 7); | |
// Serial.println(ball->valuestring); | |
for(i = 0; i < 8; i++) { | |
leds.write(adjust(mx), i, HIGH); | |
if(strcmp(ball->valuestring, "red") == 0) { | |
leds.write(i + 8, my, HIGH); | |
} else { | |
leds.write(i, my, HIGH); | |
} | |
} | |
} | |
// client.println(); | |
break; | |
} | |
} | |
} | |
client.stop(); | |
} | |
} | |
byte adjust(byte i) { | |
byte t; | |
t = i + 1; | |
if(t == 8) { | |
t = 0; | |
} else if(t == 16) { | |
t = 8; | |
} | |
return t; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment