-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathardSim.cpp
More file actions
116 lines (99 loc) · 1.93 KB
/
Copy pathardSim.cpp
File metadata and controls
116 lines (99 loc) · 1.93 KB
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
// Do not remove the include below
#include "ardSim.h"
#include "Timer.h"
#define TIME_STEP 10
#define LED 13
#define BUT 8
int but;
Timer tim;
int tim_event;
long t;
int x, y, z;
void takeReading()
{
x = (x+3) % 512;
y = (y+5) % 512;
z = (z+7) % 512;
// envia uma mensagem "t ttt xxx yyy zzz\n"
// o ttt e' o tempo em milisegundos
Serial.print("t ");
Serial.print(t);
Serial.print(" ");
Serial.print(x);
Serial.print(" ");
Serial.print(y);
Serial.print(" ");
Serial.print(z);
Serial.print("\n"); // O println envia "\r\n" no fim!
t = t + TIME_STEP;
}
void setup() {
t = x = y = z = 0;
pinMode(BUT, INPUT);
pinMode(LED, OUTPUT);
digitalWrite(LED, LOW);
Serial.begin(115200);
}
#define CMD_SIZE 10
char *getMsg(void) {
int c;
int static index;
char static cmd[CMD_SIZE+1];
if ((c = Serial.read()) != -1) {
if (index && ((char) c == '\r')) {
cmd[index] = '\0';
index = 0;
return cmd;
}
else {
if (index < CMD_SIZE)
cmd[index++] = (char) c;
return 0;
}
}
return 0;
}
void procMsg(char *msg) {
switch(msg[0]) {
case 'l':
if (msg[1] == '1') {
digitalWrite(LED, HIGH);
Serial.println("LED ON");
}
else {
digitalWrite(LED, LOW);
Serial.println("LED OFF");
}
break;
case 'b':
if (!but)
Serial.println("BUT ON");
else
Serial.println("BUT OFF");
break;
case 'a':
if (msg[1] == '1') {
tim_event = tim.every(TIME_STEP, takeReading);
Serial.println("Timer ON");
}
else {
tim.stop(tim_event);
Serial.println("Timer OFF");
}
break;
}
}
// the loop routine runs over and over again forever:
void loop() {
char *msg;
if ((msg = getMsg()) != 0) {
Serial.println(msg);
procMsg(msg);
}
int b = digitalRead(BUT);
if (b != but) {
but = b;
Serial.println((!but) ? "BUT ON" : "BUT OFF");
}
tim.update();
}