-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathEmbedis_Uno.ino
134 lines (118 loc) · 4.86 KB
/
Embedis_Uno.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
/* Embedis - Embedded Dictionary Server
Copyright (C) 2015 PatternAgents, LLC
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program 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 General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <Embedis.h>
/* Test for platforms that have no native or emulated EEPROM - need special examples for those */
#if defined(ARDUINO_ARCH_SAM) || defined(ARDUINO_ARCH_SAMD) || defined(ARDUINO_ARCH_ESP8266) || defined(ARDUINO_ARCH_ARC32) || defined(__ARDUINO_X86__)
#error "Please use the specific example for your board type as it has no native EEPROM - this generic example won't work for it."
#else
#include <EEPROM.h>
#endif
// Embedis will run on the Serial port. Use the Arduino
// serial monitor and send "COMMANDS" to get started.
// Make sure "No line ending" is -not- selected. All others work.
Embedis embedis(Serial);
/* If E2END isn't defined you can manually set this.
* Set to 1024 bytes by default if undefined
*/
#ifndef E2END
#define E2END 1023
#warning "EEPROM size set to 1024 by default!"
#endif
const size_t EEPROM_SIZE = E2END + 1;
void setup()
{
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB (Leo, Teensy, etc)
}
/* We use "LOG" instead of "serial.println", to create a LOG channel */
/* Use SUBSCRIBE LOG to get these messages */
LOG( String() + F(" ") );
LOG( String() + F("[ Embedis : Arduino Uno (AVR Arch) Sketch ]") );
LOG( String() + F("[ Embedis : select 115200 Baud and 'Both NL & CR' as your line ending ]") );
// Create a key-value Dictionary in EEPROM
Embedis::dictionary( "EEPROM",
EEPROM_SIZE,
[](size_t pos) -> char { return EEPROM.read(pos); },
[](size_t pos, char value) { EEPROM.write(pos, value); }
);
LOG( String() + F("[ Embedis : EEPROM dictionary installed ]") );
LOG( String() + F("[ Embedis : EEPROM dictionary selected ]") );
// Let's add some useful commands as an example...
// Add pinMode command to mirror Arduino's
Embedis::command( F("pinMode"), [](Embedis* e) {
if (e->argc != 3) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
String argv3(e->argv[2]);
argv3.toUpperCase();
int mode;
if (argv3 == "INPUT") mode = INPUT;
else if (argv3 == "OUTPUT") mode = OUTPUT;
else if (argv3 == "INPUT_PULLUP") mode = INPUT_PULLUP;
else return e->response(Embedis::ARGS_ERROR);
pinMode(pin, mode);
e->response(Embedis::OK);
});
// Add digitalWrite command to mirror Arduino's
Embedis::command( F("digitalWrite"), [](Embedis* e) {
if (e->argc != 3) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
String argv3(e->argv[2]);
argv3.toUpperCase();
int mode;
if (argv3 == "HIGH") mode = HIGH;
else if (argv3 == "LOW") mode = LOW;
else mode = argv3.toInt();
digitalWrite(pin, mode);
e->response(Embedis::OK);
});
// Add digitalRead command to mirror Arduino's
Embedis::command( F("digitalRead"), [](Embedis* e) {
if (e->argc != 2) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
if (digitalRead(pin)) {
e->response(F("HIGH"));
} else {
e->response(F("LOW"));
}
});
// Add analogRead command to mirror Arduino's
Embedis::command( F("analogRead"), [](Embedis* e) {
if (e->argc != 2) return e->response(Embedis::ARGS_ERROR);
int pin = String(e->argv[1]).toInt();
e->response(':', analogRead(pin));
});
/* okay, done setting up new dictionary and commands... */
LOG( String() + F("[ Embedis : Type 'commands' to get a listing of commands ]") );
}
void loop()
{
embedis.process();
/* give delay - for any internal RTOS to switch context */
delay(20);
}
// This will log to an embedis channel called "log".
// Use SUBSCRIBE LOG to get these messages.
// Logs are also printed to Serial until an empty message is received.
void LOG(const String& message) {
static bool inSetup = true;
if (inSetup) {
if (!message.length()) {
inSetup = false;
return;
}
SERIAL_PORT_MONITOR.println(message);
}
Embedis::publish("log", message);
}