-
Notifications
You must be signed in to change notification settings - Fork 0
/
Controller Firmware.ino
243 lines (207 loc) · 6.22 KB
/
Controller Firmware.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
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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
#include <Wire.h>
#ifndef STASSID
#define STASSID "Test"
#define STAPSK "testpass"
#endif
unsigned int localPort = 8888; // local port to listen on
uint8_t __up = 1;
uint8_t __down = 1;
uint8_t __left = 1;
uint8_t __right = 1;
uint8_t __a = 1;
uint8_t __b = 1;
uint8_t __strt = 1;
uint8_t __slct = 1;
boolean tick = false;
// buffers for receiving and sending data
char ReplyBuffer[] = "nth"; // a string to send back
WiFiUDP Udp;
IPAddress ip(192, 168, 43, 137);
void configurePort(uint8_t port, uint8_t custom)
{
if (custom == INPUT)
{
writeBlockData(port, 0xFF);
}
else if (custom == OUTPUT)
{
writeBlockData(port, 0x00);
}
else
{
writeBlockData(port, custom);
}
}
uint8_t readGP()
{
uint8_t statusGP = 0;
Wire.beginTransmission(0x20);
Wire.write(0x09);
Wire.endTransmission();
Wire.requestFrom(0x20, 1); // ler do chip 1 byte
statusGP = Wire.read();
return statusGP;
Serial.print("Value of GPIO is: ");
Serial.println(255 - statusGP);
}
void writeBlockData(uint8_t cmd, uint8_t data)
{
Wire.beginTransmission(0x20);
Wire.write(cmd);
Wire.write(data);
Wire.endTransmission();
delay(10);
}
uint8_t valueFromPin(uint8_t pin, uint8_t statusGP)
{
return (statusGP & ( 0x0001 << pin)) == 0 ? 0 : 1;
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(STASSID, STAPSK);
while (WiFi.status() != WL_CONNECTED) {
Serial.print('.');
delay(500);
}
Serial.print("Connected! IP address: ");
Serial.println(WiFi.localIP());
Serial.printf("UDP server on port %d\n", localPort);
Udp.begin(localPort);
Serial.println();
Wire.begin(0, 2); //ESP-01
Wire.setClock(200000); //frequencia
//configura o GPIO0 como OUTPUT (todos os pinos)
configurePort(0x00, INPUT);
//configura o GPIO1 como INPUT o GP1.0 e como OUTPUT os outros GP1
//writeBlockData(0x09, B00000000);
}
void loop() {
// if there's data available, read a packet
uint8_t gpStat = readGP();
// Serial.println(gpStat);
uint8_t up = valueFromPin(7, gpStat);
uint8_t down = valueFromPin(6, gpStat);
uint8_t left = valueFromPin(5, gpStat);
uint8_t right = valueFromPin(4, gpStat);
uint8_t a = valueFromPin(2, gpStat);
uint8_t b = valueFromPin(3, gpStat);
uint8_t strt = valueFromPin(1, gpStat);
uint8_t slct = valueFromPin(0, gpStat);
//Serial.print("connecting to ");
//Serial.print(host);
//Serial.print(':');
//Serial.println(port);
// Use WiFiClient class to create TCP connections
if (__up + up == 1 || __down + down == 1 || __left + left == 1 || __right + right == 1 || __a + a == 1 || __b + b == 1 || __strt + strt == 1 || __slct + slct == 1)tick = true;
//Serial.print("Tick: ");
//Serial.println(tick);wwww
if (tick) {
// This will send a string to the server
Serial.println("sending data to server");
if (__up == 0 && up == 1) {
Serial.println("UP released");
Udp.beginPacket(ip, 8888);
Udp.write("UP OFF");
Udp.endPacket();
} else if (__up == 1 && up == 0) {
Serial.println("UP pressed: ");
Udp.beginPacket(ip, 8888);
Udp.write("UP ON");
Udp.endPacket();
}
if (__down == 0 && down == 1) {
Serial.println("Down released");
Udp.beginPacket(ip, 8888);
Udp.write("DOWN OFF");
Udp.endPacket();
} else if (__down == 1 && down == 0) {
Serial.println("Down pressed");
Udp.beginPacket(ip, 8888);
Udp.write("DOWN ON");
Udp.endPacket();
}
if (__left == 0 && left == 1) {
Serial.println("Left released");
Udp.beginPacket(ip, 8888);
Udp.write("LEFT OFF");
Udp.endPacket();
} else if (__left == 1 && left == 0) {
Serial.println("Left pressed");
Udp.beginPacket(ip, 8888);
Udp.write("LEFT ON");
Udp.endPacket();
}
if (__right == 0 && right == 1) {
Serial.println("Right released");
Udp.beginPacket(ip, 8888);
Udp.write("RIGHT OFF");
Udp.endPacket();
} else if (__right == 1 && right == 0) {
Serial.println("Right pressed");
Udp.beginPacket(ip, 8888);
Udp.write("RIGHT ON");
Udp.endPacket();
}
if (__a == 0 && a == 1) {
Serial.println("A pressed");
Udp.beginPacket(ip, 8888);
Udp.write("A OFF");
Udp.endPacket();
} else if (__a == 1 && a == 0) {
Serial.println("A pressed");
Udp.beginPacket(ip, 8888);
Udp.write("A ON");
Udp.endPacket();
}
if (__b == 0 && b == 1) {
Serial.println("B released");
Udp.beginPacket(ip, 8888);
Udp.write("B OFF");
Udp.endPacket();
} else if (__b == 1 && b == 0) {
Serial.println("B pressed");
Udp.beginPacket(ip, 8888);
Udp.write("B ON");
Udp.endPacket();
}
if (__strt == 0 && strt == 1) {
Serial.println("Start released");
Udp.beginPacket(ip, 8888);
Udp.write("START OFF");
Udp.endPacket();
} else if (__strt == 1 && strt == 0) {
Serial.println("Start pressed");
Udp.beginPacket(ip, 8888);
Udp.write("START ON");
Udp.endPacket();
}
if (__slct == 0 && slct == 1) {
Serial.println("Select released");
Udp.beginPacket(ip, 8888);
Udp.write("SELECT OFF");
Udp.endPacket();
} else if (__slct == 1 && slct == 0) {
Serial.println("Select pressed");
Udp.beginPacket(ip, 8888);
Udp.write("SELECT ON");
Udp.endPacket();
}
// Close the connection
__up = up;
__down = down;
__left = left;
__right = right;
__a = a;
__b = b;
__strt = strt;
__slct = slct;
Serial.println();
Serial.println("closing connection");
tick = false;
//wdelay(50); // execute once every 5 minutes, don't flood remote service
// send a reply, to the IP address and port that sent us the packet we received
}
}