-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSmart700.h
More file actions
228 lines (180 loc) · 5.1 KB
/
Copy pathSmart700.h
File metadata and controls
228 lines (180 loc) · 5.1 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
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
#ifndef _SMART700_H_
#define _SMART700_H_
#include "Setup.h"
#include <Arduino.h>
class Smart700 {
private:
enum ChargeState {
UNKNOWN,
EXPECT_HIGH,
EXPECT_LOW
};
ChargeState chargeState = UNKNOWN;
bool _wifi = false;
bool _shutdown = false;
bool _searchingForCharger = false;
bool _charging = false;
const uint8_t analogPins[4] = { 1, 2, 3, 4 };
const uint8_t digitalOutPins[2] = { 5, 6 };
bool between(uint16_t v, uint16_t min, uint16_t max) {
return (v >= min && v <= max);
}
void detectCharging(int a1, int a0) {
static uint8_t oscillationCount = 0;
bool a1High = a1 > 2000;
bool a1Low = a1 < 100;
bool a0High = a0 > 2000;
bool a0Low = a0 < 100;
if (!a0High) {
_charging = false;
oscillationCount = 0;
chargeState = UNKNOWN;
return;
}
switch (chargeState) {
case UNKNOWN:
if (a1High) chargeState = EXPECT_LOW;
else if (a1Low) chargeState = EXPECT_HIGH;
break;
case EXPECT_HIGH:
if (a1High) {
oscillationCount++;
chargeState = EXPECT_LOW;
}
break;
case EXPECT_LOW:
if (a1Low) {
oscillationCount++;
chargeState = EXPECT_HIGH;
}
break;
}
if (oscillationCount >= 4)
_charging = true;
else
_charging = false;
}
void WiFiCheck(uint16_t a3) {
static int8_t stableCount = 0; // Contador de estabilidade
static int8_t lastZone = -1;
int8_t zone;
if (a3 < 300) {
zone = 0; // zona baixa → A3 ativo
} else if (a3 > 2000) {
zone = 1; // zona alta → A3 inativo
} else {
zone = -1; // zona indefinida → ignora, mas não troca estado
}
// Se ficou em uma zona válida
if (zone == lastZone && zone != -1) {
stableCount++;
} else {
stableCount = 1; // reset para novo estado
lastZone = zone;
}
// Se está estável por tempo suficiente, confirma mudança
if (stableCount >= 2) {
if (zone == 0) {
_wifi = true;
} else if (zone == 1) {
_wifi = false;
}
}
}
void detectBoardShutdown(uint16_t a3, uint16_t a2, uint16_t a1, uint16_t a0) {
static uint8_t shutdownStableCount = 0;
bool match =
(a2 > 2000) && between(a3, 0, 900) && between(a1, 0, 900) && between(a0, 0, 900);
if (match) {
shutdownStableCount++;
} else {
shutdownStableCount = 0;
}
if (shutdownStableCount >= 3)
_shutdown = true;
else
_shutdown = false;
}
void detectSearchingForCharger(uint16_t a1, uint16_t a2) {
static uint8_t searchStableCount = 0;
bool match = (a1 < 500) && (a2 < 500);
if (match) {
searchStableCount++;
} else {
searchStableCount = 0;
}
if (searchStableCount >= 3)
_searchingForCharger = true;
else
_searchingForCharger = false;
}
public:
bool WiFi() {
return _wifi;
}
bool shutdown() {
return _shutdown;
}
bool searchingForCharger() {
return _searchingForCharger;
}
bool charging() {
return _charging;
}
void goToCharger() {
digitalWrite(digitalOutPins[1], 0);
delay(250);
digitalWrite(digitalOutPins[1], 1);
}
void restart() {
digitalWrite(digitalOutPins[0], 1);
delay(4000);
digitalWrite(digitalOutPins[0], 0);
delay(5000);
digitalWrite(digitalOutPins[0], 1);
delay(2000);
digitalWrite(digitalOutPins[0], 0);
delay(15000);
goToCharger();
}
void begin() {
for (uint8_t i = 0; i < sizeof(analogPins) / sizeof(analogPins[0]); i++)
pinMode(analogPins[i], INPUT);
pinMode(digitalOutPins[0], OUTPUT);
digitalWrite(digitalOutPins[0], 0);
pinMode(digitalOutPins[1], OUTPUT);
digitalWrite(digitalOutPins[1], 1);
analogReadResolution(12);
analogSetAttenuation(ADC_11db);
}
void tick() {
uint16_t a3_raw = (uint16_t)analogRead(analogPins[0]);
uint16_t a2_raw = (uint16_t)analogRead(analogPins[1]);
uint16_t a1_raw = (uint16_t)analogRead(analogPins[2]);
uint16_t a0_raw = (uint16_t)analogRead(analogPins[3]);
detectCharging(a1_raw, a0_raw);
WiFiCheck(a3_raw);
detectBoardShutdown(a3_raw, a2_raw, a1_raw, a0_raw);
detectSearchingForCharger(a1_raw, a2_raw);
#ifdef DEBUG_MODE
Serial.println("------- SMART700 DEBUG -------");
Serial.printf("Board State: %s\n", _shutdown ? "OFF" : "ON");
Serial.printf("WiFi: %s\n", _wifi ? "ACTIVE" : "INACTIVE");
Serial.printf("Charging: %s\n", _charging ? "YES" : "NO");
Serial.printf("SearchingCharge: %s\n", _searchingForCharger ? "YES" : "NO");
Serial.printf("A3 Raw: %u\n", a3_raw);
Serial.printf("A2 Raw: %u\n", a2_raw);
Serial.printf("A1 Raw: %u\n", a1_raw);
Serial.printf("A0 Raw: %u\n", a0_raw);
Serial.println("Voltages:");
for (int i = 0; i < 4; i++) {
int raw = analogRead(analogPins[i]);
float volts = raw * 3.3 / 4095.0;
Serial.printf("A%d (GPIO%d): %4d (%.2f V)\n",
3 - i, analogPins[i], raw, volts);
}
Serial.println("--------------------------------\n");
#endif
}
};
#endif