mirror of
https://github.com/arduino/Arduino.git
synced 2025-02-18 12:54:25 +01:00
Moved Bridge.message* method on their own class.
This commit is contained in:
parent
fdae2dbb6e
commit
8830751859
@ -58,32 +58,6 @@ void BridgeClass::begin() {
|
|||||||
while (true);
|
while (true);
|
||||||
}
|
}
|
||||||
|
|
||||||
unsigned int BridgeClass::readMessage(uint8_t *buff, unsigned int size) {
|
|
||||||
uint8_t tmp[] = { 'm' };
|
|
||||||
return transfer(tmp, 1, buff, size);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::writeMessage(const uint8_t *buff, unsigned int size) {
|
|
||||||
uint8_t cmd[] = {'M'};
|
|
||||||
transfer(cmd, 1, buff, size, NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::writeMessage(const String& str) {
|
|
||||||
writeMessage((uint8_t*) str.c_str(), str.length());
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::writeJSON(const String& str) {
|
|
||||||
uint8_t cmd[] = {'J'};
|
|
||||||
transfer(cmd, 1, (uint8_t*) str.c_str(), str.length(), NULL, 0);
|
|
||||||
}
|
|
||||||
|
|
||||||
unsigned int BridgeClass::messageAvailable() {
|
|
||||||
uint8_t tmp[] = {'n'};
|
|
||||||
uint8_t res[2];
|
|
||||||
transfer(tmp, 1, res, 2);
|
|
||||||
return (res[0] << 8) + res[1];
|
|
||||||
}
|
|
||||||
|
|
||||||
void BridgeClass::put(const char *key, const char *value) {
|
void BridgeClass::put(const char *key, const char *value) {
|
||||||
// TODO: do it in a more efficient way
|
// TODO: do it in a more efficient way
|
||||||
String cmd = "D";
|
String cmd = "D";
|
||||||
|
@ -27,13 +27,6 @@ public:
|
|||||||
BridgeClass(Stream &_stream);
|
BridgeClass(Stream &_stream);
|
||||||
void begin();
|
void begin();
|
||||||
|
|
||||||
// Methods to handle mailbox messages
|
|
||||||
unsigned int readMessage(uint8_t *buffer, unsigned int size);
|
|
||||||
void writeMessage(const uint8_t *buffer, unsigned int size);
|
|
||||||
void writeMessage(const String& str);
|
|
||||||
void writeJSON(const String& str);
|
|
||||||
unsigned int messageAvailable();
|
|
||||||
|
|
||||||
// Methods to handle key/value datastore
|
// Methods to handle key/value datastore
|
||||||
void put(const char *key, const char *value);
|
void put(const char *key, const char *value);
|
||||||
unsigned int get(const char *key, uint8_t *buff, unsigned int size);
|
unsigned int get(const char *key, uint8_t *buff, unsigned int size);
|
||||||
|
56
hardware/arduino/avr/libraries/Bridge/Mailbox.cpp
Normal file
56
hardware/arduino/avr/libraries/Bridge/Mailbox.cpp
Normal file
@ -0,0 +1,56 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2013 Arduino LLC. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#include <Mailbox.h>
|
||||||
|
|
||||||
|
unsigned int MailboxClass::readMessage(uint8_t *buff, unsigned int size) {
|
||||||
|
uint8_t tmp[] = { 'm' };
|
||||||
|
return bridge.transfer(tmp, 1, buff, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MailboxClass::readMessage(String &str, unsigned int maxLength) {
|
||||||
|
uint8_t tmp[] = { 'm' };
|
||||||
|
// XXX: Is there a better way to create the string?
|
||||||
|
uint8_t buff[maxLength+1];
|
||||||
|
int l = bridge.transfer(tmp, 1, buff, maxLength);
|
||||||
|
buff[l] = 0;
|
||||||
|
str = (const char *)buff;
|
||||||
|
}
|
||||||
|
|
||||||
|
void MailboxClass::writeMessage(const uint8_t *buff, unsigned int size) {
|
||||||
|
uint8_t cmd[] = {'M'};
|
||||||
|
bridge.transfer(cmd, 1, buff, size, NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void MailboxClass::writeMessage(const String& str) {
|
||||||
|
writeMessage((uint8_t*) str.c_str(), str.length());
|
||||||
|
}
|
||||||
|
|
||||||
|
void MailboxClass::writeJSON(const String& str) {
|
||||||
|
uint8_t cmd[] = {'J'};
|
||||||
|
bridge.transfer(cmd, 1, (uint8_t*) str.c_str(), str.length(), NULL, 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
unsigned int MailboxClass::messageAvailable() {
|
||||||
|
uint8_t tmp[] = {'n'};
|
||||||
|
uint8_t res[2];
|
||||||
|
bridge.transfer(tmp, 1, res, 2);
|
||||||
|
return (res[0] << 8) + res[1];
|
||||||
|
}
|
||||||
|
|
||||||
|
MailboxClass Mailbox(Bridge);
|
53
hardware/arduino/avr/libraries/Bridge/Mailbox.h
Normal file
53
hardware/arduino/avr/libraries/Bridge/Mailbox.h
Normal file
@ -0,0 +1,53 @@
|
|||||||
|
/*
|
||||||
|
Copyright (c) 2013 Arduino LLC. All right reserved.
|
||||||
|
|
||||||
|
This library is free software; you can redistribute it and/or
|
||||||
|
modify it under the terms of the GNU Lesser General Public
|
||||||
|
License as published by the Free Software Foundation; either
|
||||||
|
version 2.1 of the License, or (at your option) any later version.
|
||||||
|
|
||||||
|
This library is distributed in the hope that it will be useful,
|
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
||||||
|
Lesser General Public License for more details.
|
||||||
|
|
||||||
|
You should have received a copy of the GNU Lesser General Public
|
||||||
|
License along with this library; if not, write to the Free Software
|
||||||
|
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
|
||||||
|
*/
|
||||||
|
|
||||||
|
#ifndef _MAILBOX_CLASS_H_INCLUDED_
|
||||||
|
#define _MAILBOX_CLASS_H_INCLUDED_
|
||||||
|
|
||||||
|
#include <Bridge.h>
|
||||||
|
|
||||||
|
class MailboxClass {
|
||||||
|
public:
|
||||||
|
MailboxClass(BridgeClass &b = Bridge) : bridge(b) { }
|
||||||
|
|
||||||
|
void begin() { }
|
||||||
|
void end() { }
|
||||||
|
|
||||||
|
// Receive a message and store it inside a buffer
|
||||||
|
unsigned int readMessage(uint8_t *buffer, unsigned int size);
|
||||||
|
// Receive a message and store it inside a String
|
||||||
|
void readMessage(String &str, unsigned int maxLength=128);
|
||||||
|
|
||||||
|
// Send a message
|
||||||
|
void writeMessage(const uint8_t *buffer, unsigned int size);
|
||||||
|
// Send a message
|
||||||
|
void writeMessage(const String& str);
|
||||||
|
// Send a JSON message
|
||||||
|
void writeJSON(const String& str);
|
||||||
|
|
||||||
|
// Return the size of the next available message, 0 if there are
|
||||||
|
// no messages in queue.
|
||||||
|
unsigned int messageAvailable();
|
||||||
|
|
||||||
|
private:
|
||||||
|
BridgeClass &bridge;
|
||||||
|
};
|
||||||
|
|
||||||
|
extern MailboxClass Mailbox;
|
||||||
|
|
||||||
|
#endif // _MAILBOX_CLASS_H_INCLUDED_
|
@ -1,51 +1,50 @@
|
|||||||
|
|
||||||
#include <Bridge.h>
|
//#include <Bridge.h>
|
||||||
|
#include <Mailbox.h>
|
||||||
|
|
||||||
void setup() {
|
void setup() {
|
||||||
pinMode(13,OUTPUT);
|
pinMode(13,OUTPUT);
|
||||||
digitalWrite(13, LOW);
|
digitalWrite(13, LOW);
|
||||||
Bridge.begin();
|
Bridge.begin();
|
||||||
digitalWrite(13, HIGH);
|
digitalWrite(13, HIGH);
|
||||||
|
Serial.begin(9600);
|
||||||
}
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
while (Bridge.messageAvailable()) {
|
while (Mailbox.messageAvailable()) {
|
||||||
uint8_t buff[64];
|
String msg;
|
||||||
int l = Bridge.readMessage(buff, 64);
|
Mailbox.readMessage(msg);
|
||||||
process(buff, l);
|
process(msg);
|
||||||
}
|
}
|
||||||
delay(100); // Poll every 0.100s
|
delay(100); // Poll every 0.100s
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(uint8_t buff[], int length) {
|
void process(String command) {
|
||||||
// "digital/13/1" -> digitalWrite(13, HIGH)
|
Serial.println(command);
|
||||||
// "digital/13" -> digitalRead(13)
|
// "digital/13" -> digitalRead(13)
|
||||||
|
// "digital/13/1" -> digitalWrite(13, HIGH)
|
||||||
// "analog/2/123" -> analogWrite(2, 123)
|
// "analog/2/123" -> analogWrite(2, 123)
|
||||||
// "analog/2" -> analogRead(2)
|
// "analog/2" -> analogRead(2)
|
||||||
// "mode/13/input" -> pinMode(13, INPUT)
|
// "mode/13/input" -> pinMode(13, INPUT)
|
||||||
// "mode/13/output" -> pinMode(13, OUTPUT)
|
// "mode/13/output" -> pinMode(13, OUTPUT)
|
||||||
|
|
||||||
// Sanity check
|
// is digital command?
|
||||||
if (length < 9 || length > 14)
|
if (command.startsWith("digital/")) {
|
||||||
return;
|
// extract subcommand (after the "/")
|
||||||
|
|
||||||
// string terminator
|
|
||||||
buff[length] = '\0';
|
|
||||||
|
|
||||||
String command = String((char*)buff);
|
|
||||||
|
|
||||||
// digital command
|
|
||||||
if (command.indexOf("digital/") == 0) {
|
|
||||||
command = command.substring(8);
|
command = command.substring(8);
|
||||||
digitalCommand(command);
|
digitalCommand(command);
|
||||||
|
|
||||||
// analog command
|
}
|
||||||
} else if (command.indexOf("analog/") == 0) {
|
// is analog command?
|
||||||
|
else if (command.startsWith("analog/")) {
|
||||||
|
// extract subcommand (after the "/")
|
||||||
command = command.substring(7);
|
command = command.substring(7);
|
||||||
analogCommand(command);
|
analogCommand(command);
|
||||||
|
|
||||||
// mode command
|
}
|
||||||
} else if (command.indexOf("mode/") == 0) {
|
// is mode command?
|
||||||
|
else if (command.startsWith("mode/")) {
|
||||||
|
// extract subcommand (after the "/")
|
||||||
command = command.substring(5);
|
command = command.substring(5);
|
||||||
modeCommand(command);
|
modeCommand(command);
|
||||||
}
|
}
|
||||||
@ -53,12 +52,27 @@ void process(uint8_t buff[], int length) {
|
|||||||
|
|
||||||
void digitalCommand(String command) {
|
void digitalCommand(String command) {
|
||||||
int pin, value;
|
int pin, value;
|
||||||
if (command.indexOf("/") != -1) {
|
|
||||||
pin = command.substring(0, command.indexOf("/")).toInt();
|
// Find the position of the "/" inside the command
|
||||||
value = command.substring(command.indexOf("/") + 1, command.length()).toInt();
|
int slashIndex = command.indexOf("/");
|
||||||
digitalWrite(pin, value);
|
|
||||||
} else {
|
// If there are no slashes
|
||||||
|
if (slashIndex == -1) {
|
||||||
|
// then we are in the following case:
|
||||||
|
// "digital/13" -> digitalRead(13)
|
||||||
|
|
||||||
|
// so we can extract the pin number from the remainder of the command string
|
||||||
pin = command.toInt();
|
pin = command.toInt();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
// else, we found a slash, so we are in the following case:
|
||||||
|
// "digital/13/1" -> digitalWrite(13, HIGH)
|
||||||
|
|
||||||
|
// we must estract pin number before the "/"
|
||||||
|
pin = command.substring(0, slashIndex).toInt();
|
||||||
|
// and value after the "/"
|
||||||
|
value = command.substring(slashIndex+1).toInt();
|
||||||
|
digitalWrite(pin, value);
|
||||||
}
|
}
|
||||||
reportDigitalRead(pin, true);
|
reportDigitalRead(pin, true);
|
||||||
}
|
}
|
||||||
@ -69,7 +83,8 @@ void analogCommand(String command) {
|
|||||||
pin = command.substring(0, command.indexOf("/")).toInt();
|
pin = command.substring(0, command.indexOf("/")).toInt();
|
||||||
value = command.substring(command.indexOf("/") + 1, command.length()).toInt();
|
value = command.substring(command.indexOf("/") + 1, command.length()).toInt();
|
||||||
analogWrite(pin, value);
|
analogWrite(pin, value);
|
||||||
} else {
|
}
|
||||||
|
else {
|
||||||
pin = command.toInt();
|
pin = command.toInt();
|
||||||
}
|
}
|
||||||
reportAnalogRead(pin, true);
|
reportAnalogRead(pin, true);
|
||||||
@ -83,7 +98,8 @@ void modeCommand(String command) {
|
|||||||
if (strValue == "output") {
|
if (strValue == "output") {
|
||||||
pinMode(pin, OUTPUT);
|
pinMode(pin, OUTPUT);
|
||||||
reportPinMode(pin, strValue);
|
reportPinMode(pin, strValue);
|
||||||
} else if (strValue == "input") {
|
}
|
||||||
|
else if (strValue == "input") {
|
||||||
pinMode(pin, INPUT);
|
pinMode(pin, INPUT);
|
||||||
reportPinMode(pin, strValue);
|
reportPinMode(pin, strValue);
|
||||||
}
|
}
|
||||||
@ -95,18 +111,18 @@ void reportPinMode(int pin, String mode) {
|
|||||||
json += ", \"mode\": \"";
|
json += ", \"mode\": \"";
|
||||||
json += mode;
|
json += mode;
|
||||||
json += "\"}";
|
json += "\"}";
|
||||||
Bridge.writeJSON(json);
|
Mailbox.writeJSON(json);
|
||||||
}
|
}
|
||||||
|
|
||||||
void reportDigitalRead(int pin, boolean dataset) {
|
void reportDigitalRead(int pin, boolean dataset) {
|
||||||
int value = digitalRead(pin);
|
int value = digitalRead(pin);
|
||||||
|
|
||||||
String json = "{\"pin\":";
|
String json = "{\"pin\":";
|
||||||
json += pin;
|
json += pin;
|
||||||
json += ", \"value\": ";
|
json += ", \"value\": ";
|
||||||
json += value;
|
json += value;
|
||||||
json += "}";
|
json += "}";
|
||||||
Bridge.writeJSON(json);
|
Mailbox.writeJSON(json);
|
||||||
|
|
||||||
if (dataset) {
|
if (dataset) {
|
||||||
String key = "D";
|
String key = "D";
|
||||||
@ -117,13 +133,13 @@ void reportDigitalRead(int pin, boolean dataset) {
|
|||||||
|
|
||||||
void reportAnalogRead(int pin, boolean dataset) {
|
void reportAnalogRead(int pin, boolean dataset) {
|
||||||
int value = analogRead(pin);
|
int value = analogRead(pin);
|
||||||
|
|
||||||
String json = "{\"pin\":";
|
String json = "{\"pin\":";
|
||||||
json += pin;
|
json += pin;
|
||||||
json += ", \"value\": ";
|
json += ", \"value\": ";
|
||||||
json += value;
|
json += value;
|
||||||
json += "}";
|
json += "}";
|
||||||
Bridge.writeJSON(json);
|
Mailbox.writeJSON(json);
|
||||||
|
|
||||||
if (dataset) {
|
if (dataset) {
|
||||||
String key = "A";
|
String key = "A";
|
||||||
@ -131,3 +147,4 @@ void reportAnalogRead(int pin, boolean dataset) {
|
|||||||
Bridge.put(key.c_str(), String(value).c_str());
|
Bridge.put(key.c_str(), String(value).c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user