reviewed bridge example

added BridgeClass::writeMessage(const String& str)
This commit is contained in:
Federico Fissore 2013-06-20 16:32:12 +02:00
parent ed672deb74
commit 4ec08ef6cc
3 changed files with 103 additions and 69 deletions

View File

@ -68,6 +68,10 @@ void BridgeClass::writeMessage(const uint8_t *buff, unsigned int size) {
transfer(cmd, 1, buff, size, NULL, 0); transfer(cmd, 1, buff, size, NULL, 0);
} }
void BridgeClass::writeMessage(const String& str) {
writeMessage((uint8_t*) str.c_str(), str.length());
}
unsigned int BridgeClass::messageAvailable() { unsigned int BridgeClass::messageAvailable() {
uint8_t tmp[] = {'n'}; uint8_t tmp[] = {'n'};
uint8_t res[2]; uint8_t res[2];

View File

@ -30,6 +30,7 @@ public:
// Methods to handle mailbox messages // Methods to handle mailbox messages
unsigned int readMessage(uint8_t *buffer, unsigned int size); unsigned int readMessage(uint8_t *buffer, unsigned int size);
void writeMessage(const uint8_t *buffer, unsigned int size); void writeMessage(const uint8_t *buffer, unsigned int size);
void writeMessage(const String& str);
unsigned int messageAvailable(); unsigned int messageAvailable();
// Methods to handle key/value datastore // Methods to handle key/value datastore

View File

@ -17,88 +17,117 @@ void loop() {
delay(100); // Poll every 0.100s delay(100); // Poll every 0.100s
} }
void process(uint8_t buff[], int l) { void process(uint8_t buff[], int length) {
// "DWppv" -> digitalWrite(pp, v) // "digital/13/1" -> digitalWrite(13, HIGH)
// "DRpp" -> digitalRead(pp) -> "Dpp0" / "Dpp1" // "digital/13" -> digitalRead(13)
// "AWppvvv" -> analogWrite(pp, vvv) // "analog/2/123" -> analogWrite(2, 123)
// "ARpp" -> analogRead(pp) -> "App0000" - "App1023" // "analog/2" -> analogRead(2)
// "PIpp" -> pinMode(pp, INPUT) // "mode/13/input" -> pinMode(13, INPUT)
// "POpp" -> pinMode(pp, OUTPUT) // "mode/13/output" -> pinMode(13, OUTPUT)
// Sanity check // Sanity check
if (l<4 || l>7) if (length < 9 || length > 14)
return;
if (buff[2]<'0' || buff[2]>'9')
return;
if (buff[3]<'0' || buff[3]>'9')
return;
char cmd0 = buff[0];
char cmd1 = buff[1];
int pin = (buff[2]-'0')*10 + (buff[3]-'0');
if (pin<0 || pin>13)
return; return;
// Command selection // string terminator
if (l==5 && cmd0=='D' && cmd1=='W') { buff[length] = '\0';
char c = buff[4];
if (c=='0' || c=='1') { String command = String((char*)buff);
digitalWrite(pin, c-'0');
reportDigitalRead(pin, true, true); // digital command
} if (command.indexOf("digital/") == 0) {
} else if (l==4 && cmd0=='D' && cmd1=='R') { command = command.substring(8);
reportDigitalRead(pin, true, true); digitalCommand(command);
} else if (l==7 && cmd0=='A' && cmd1=='W') {
analogWrite(pin, buff[4]); // analog command
reportAnalogRead(pin); } else if (command.indexOf("analog/") == 0) {
} else if (l==4 && cmd0=='A' && cmd1=='R') { command = command.substring(7);
reportAnalogRead(pin); analogCommand(command);
} else if (l==4 && cmd0=='P' && cmd1=='I') {
pinMode(pin, INPUT); // mode command
reportPinMode(pin, INPUT); } else if (command.indexOf("mode/") == 0) {
} else if (l==4 && cmd0=='P' && cmd1=='O') { command = command.substring(5);
modeCommand(command);
}
}
void digitalCommand(String command) {
int pin, value;
if (command.indexOf("/") != -1) {
pin = command.substring(0, command.indexOf("/")).toInt();
value = command.substring(command.indexOf("/") + 1, command.length()).toInt();
digitalWrite(pin, value);
} else {
pin = command.toInt();
}
reportDigitalRead(pin, true);
}
void analogCommand(String command) {
int pin, value;
if (command.indexOf("/") != -1) {
pin = command.substring(0, command.indexOf("/")).toInt();
value = command.substring(command.indexOf("/") + 1, command.length()).toInt();
analogWrite(pin, value);
} else {
pin = command.toInt();
}
reportAnalogRead(pin, true);
}
void modeCommand(String command) {
int pin;
String strValue;
pin = command.substring(0, command.indexOf("/")).toInt();
strValue = command.substring(command.indexOf("/") + 1, command.length());
if (strValue == "output") {
pinMode(pin, OUTPUT); pinMode(pin, OUTPUT);
reportPinMode(pin, OUTPUT); reportPinMode(pin, strValue);
} else if (strValue == "input") {
pinMode(pin, INPUT);
reportPinMode(pin, strValue);
} }
} }
void reportPinMode(int pin, uint8_t dir) { void reportPinMode(int pin, String mode) {
uint8_t buff[] = { 'P', 'I', '0', '0' }; String message = "{\"pin\":";
buff[1] = dir == INPUT ? 'I' : 'O'; message += pin;
buff[2] += pin/10; message += ", \"mode\": \"";
buff[3] += pin%10; message += mode;
Bridge.writeMessage(buff, 4); message += "\"}";
Bridge.writeMessage(message);
} }
void reportDigitalRead(int pin, boolean raw, boolean dataset) { void reportDigitalRead(int pin, boolean dataset) {
// "Dpp0" - "Dpp1" int value = digitalRead(pin);
// 0 1 2 3
uint8_t buff[] = { 'D', '0', '0', '0' }; String message = "{\"pin\":";
buff[1] += pin/10; message += pin;
buff[2] += pin%10; message += ", \"value\": ";
if (digitalRead(pin) == HIGH) message += value;
buff[3] = '1'; message += "}";
if (raw) Bridge.writeMessage(message);
Bridge.writeMessage(buff, 4);
if (dataset) { if (dataset) {
char *val = "0"; String key = "D";
val[0] = buff[3]; key += pin;
buff[3] = 0; Bridge.put(key.c_str(), String(value).c_str());
Bridge.put((const char *)buff, val);
} }
} }
void reportAnalogRead(int pin) { void reportAnalogRead(int pin, boolean dataset) {
// "App0000" - "App1023" int value = analogRead(pin);
// 0 1 2 3 4 5 6
uint8_t buff[] = { 'A', '0', '0', '0', '0', '0', '0' }; String message = "{\"pin\":";
buff[1] += pin/10; message += pin;
buff[2] += pin%10; message += ", \"value\": ";
message += value;
message += "}";
Bridge.writeMessage(message);
int v = analogRead(pin); if (dataset) {
buff[6] += v%10; v /= 10; String key = "A";
buff[5] += v%10; v /= 10; key += pin;
buff[4] += v%10; v /= 10; Bridge.put(key.c_str(), String(value).c_str());
buff[3] += v; }
Bridge.writeMessage(buff, 7);
} }