-
Notifications
You must be signed in to change notification settings - Fork 40
/
homeduino.ino
159 lines (141 loc) · 3.14 KB
/
homeduino.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#include <SerialCommand.h>
#include <RFControl.h>
#include <DHTlib.h>
void argument_error();
SerialCommand sCmd;
#include "rfcontrol_command.h"
#ifdef KEYPAD_ENABLED
#include "keypad_command.h"
#endif
#include "dht_command.h"
void digital_read_command();
void digital_write_command();
void analog_read_command();
void analog_write_command();
void reset_command();
void pin_mode_command();
void ping_command();
void unrecognized(const char *command);
void setup() {
Serial.begin(115200);
// Setup callbacks for SerialCommand commands
sCmd.addCommand("DR", digital_read_command);
sCmd.addCommand("DW", digital_write_command);
sCmd.addCommand("AR", analog_read_command);
sCmd.addCommand("AW", analog_write_command);
sCmd.addCommand("PM", pin_mode_command);
sCmd.addCommand("RF", rfcontrol_command);
sCmd.addCommand("PING", ping_command);
sCmd.addCommand("DHT", dht_command);
sCmd.addCommand("RESET", reset_command);
#ifdef KEYPAD_ENABLED
sCmd.addCommand("K", keypad_command);
#endif
sCmd.setDefaultHandler(unrecognized);
Serial.print("ready\r\n");
}
void loop() {
// handle serial command
sCmd.readSerial();
// handle rf control receiving
rfcontrol_loop();
#ifdef KEYPAD_ENABLED
// handle keypad keypress
keypad_loop();
#endif
}
void digital_read_command() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int pin = atoi(arg);
int val = digitalRead(pin);
Serial.print("ACK ");
Serial.write('0' + val);
Serial.print("\r\n");
}
void analog_read_command() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int pin = atoi(arg);
int val = analogRead(pin);
Serial.print("ACK ");
Serial.print(val);
Serial.print("\r\n");
}
void digital_write_command() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int pin = atoi(arg);
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int val = atoi(arg);
digitalWrite(pin, val);
Serial.print("ACK\r\n");
}
void analog_write_command() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int pin = atoi(arg);
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int val = atoi(arg);
analogWrite(pin, val);
Serial.print("ACK\r\n");
}
void pin_mode_command() {
char* arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
int pin = atoi(arg);
arg = sCmd.next();
if(arg == NULL) {
argument_error();
return;
}
// INPUT 0x0
// OUTPUT 0x1
int mode = atoi(arg);
pinMode(pin, mode);
Serial.print("ACK\r\n");
}
void ping_command() {
char *arg;
Serial.print("PING");
arg = sCmd.next();
if (arg != NULL) {
Serial.write(' ');
Serial.print(arg);
}
Serial.print("\r\n");
}
void reset_command() {
RFControl::stopReceiving();
Serial.print("ready\r\n");
}
void argument_error() {
Serial.print("ERR argument_error\r\n");
}
// This gets set as the default handler, and gets called when no other command matches.
void unrecognized(const char *command) {
Serial.print("ERR unknown_command\r\n");
}