-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathArduinoSim.cpp
191 lines (166 loc) · 5.29 KB
/
ArduinoSim.cpp
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include "ui.h"
#include "UNO.h"
#include "Frase.h"
using namespace prog3;
int main()
{
UI ui; //Objeto de Interface - Linha de Comando
Frase command; //Objeto de manipulação de Frases - Comandos
UNO arduino; //Objeto Arduino Uno
arduino.setUI(&ui); //Especifica a classe responsável por imprimir as informações para o usuário
ui.show(); //Mostra a interface
ui.print("Bem-Vindo ao Simulador de Arduino! Digite 'ajuda' para ver a lista de comandos.\n\n");
do
{
ui.print("-> ");
command = ui.readCommand();
ui.print(command.cppString() + "\n");
if(command.has("ajuda") || command.has("Ajuda") || command.has("help"))
{
ui.help();
}
if(command.has("millis"))
{
ui.print("Milliseconds = " + std::to_string(arduino.millis()) + "\n");
}
if(command.has("seconds"))
{
ui.print("Seconds = " + std::to_string(arduino.seconds()) + "\n");
}
if (command.has("getRTC"))
{
ui.print(arduino.getRTC());
}
if(command.has("EEPROM") || command.has("eeprom"))
{
if(command.has("begin"))
{
arduino.EEPROMbegin(command.findNumber());
}
if(command.has("write"))
{
arduino.EEPROMwrite(command.findNumber(), command.betweenQuotes());
}
if(command.has("read"))
{
int pos = command.findNumber();
char data = arduino.EEPROMread(pos);
if(data != '\0')
{
ui.print("EEPROM[" + std::to_string(pos) + "] = " + data + "\n");
}
}
}
if(command.has("pinMode"))
{
int pin = command.findNumber();
if(command.has("INPUT_PULLUP"))
{
arduino.pinMode(pin, INPUT_PULLUP);
}
else if(command.has("INPUT"))
{
arduino.pinMode(pin, INPUT);
}
else if(command.has("OUTPUT"))
{
arduino.pinMode(pin, OUTPUT);
}
}
if(command.has("digitalWrite"))
{
int pin = command.findNumber();
if(command.has("HIGH"))
{
arduino.digitalWrite(pin, HIGH);
}
else if(command.has("LOW"))
{
arduino.digitalWrite(pin, LOW);
}
else
{
ui.print("[digitalWrite]: Can only write HIGH or LOW\n");
}
}
if(command.has("analogWrite"))
{
Frase pinStr(command.subFrase(0, command.indexOf(",")));
Frase valueStr(command.subFrase(command.indexOf(",")));
int pin = pinStr.findNumber();
int value = valueStr.findNumber();
arduino.pinMode(pin, PWM);
arduino.analogWrite(pin, value);
}
if(command.has("analogRead"))
{
int pin = command.findNumber();
ui.print("Pino[A" + std::to_string(pin) + "] = " + std::to_string(arduino.analogRead(pin)) + "\n");
}
if(command.has("digitalRead"))
{
int pin = command.findNumber();
if(arduino.digitalRead(pin) == 1023)
{
ui.print("Pino[D" + std::to_string(pin) + "] = " + "HIGH" + "\n");
}
else if (arduino.digitalRead(pin) == 0)
{
ui.print("Pino[D" + std::to_string(pin) + "] = " + "LOW" + "\n");
}
else
{
ui.print("Pino[D" + std::to_string(pin) + "] = " + std::to_string(arduino.digitalRead(pin)) + "\n");
}
}
if(command.has("disconnectSensor") || command.has("dettachSensor"))
{
int pin = command.findNumber();
arduino.disconnectSensor(pin);
}
else if(command.has("connectSensor") || command.has("attachSensor"))
{
int pin = command.findNumber();
int protocol = SPI;
if(command.has("SPI"))
{
protocol = SPI;
}
else if(command.has("I2C"))
{
protocol = I2C;
}
else if(command.has("UART"))
{
protocol = UART;
}
int type = 0;
if(command.has("TEMP"))
{
type = TEMPERATURA;
}
else if(command.has("HUMIDITY"))
{
type = UMIDADE;
}
else if(command.has("PRESSURE"))
{
type = PRESSAO;
}
if(type == 0)
{
ui.print("[connectSensor]: Invalid Sensor Type\n");
}
else
{
arduino.connectSensor(pin, type, protocol);
}
}
if(command.has("clear") || command.has("limpa"))
{
ui.clearConsole();
}
}
while(!command.has("exit") && !command.has("sair"));
return 0;
}