-
Notifications
You must be signed in to change notification settings - Fork 0
/
Bitemonster.ino
460 lines (419 loc) · 11.1 KB
/
Bitemonster.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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
/*
Bite Monster development board
VL53L0X ToF sensor
*/
//Includes
#include <fishing.h>
#include <Preferences.h>
#include <U8g2lib.h>
#include <VL53L0X.h>
#include <WiFi.h>
//Enum
enum DISPLAY_SM {
INIT = 0,
SET_VOL = 1,
SET_DIST = 2,
PREPARE = 3,
BITEWATCH = 4,
ALARM = 5,
FINISH = 6,
RESERVED = 9
};
//Defines
#define HOLD_PIN 6
#define BUZPIN 9
#define LED_PIN 13
#define I2C_SDA 39
#define I2C_SCL 38
#define BUT_UP 7
#define BUT_PUSH 15
#define BUT_DOWN 16
#define BATV 8
#define PWM_FREQ 2700 //2.7 kHz
#define PWM_CHANNEL 0
#define PWM_RESOLUTION 8 //8 bit
#define FREQ_CHANGE 2000 //3 sec
#define TOF_INSENS_MM 6 //[mm] no screen refresh in this range
#define CPU_LOW_FREQ 80 //80 MHz
#define ARRAY_SIZE 21 //values of lookup tables
#define BATTERY_CAP 6700 //6700 mAh
#define CURRCONS 68.0 //68mA in state BITEWATCH
#define MAXVALIDDIST 8189 //Maximal valid value for ToF sensor
#define IIRCOEFF 0.986
#define MENUVALUES 4 //4 menu variables
//Global variables
float voltages[ARRAY_SIZE] = { 3.27, 3.61, 3.69, 3.71, 3.73, 3.75, 3.77, 3.79, 3.8, 3.82, 3.84, 3.85, 3.87, 3.91, 3.95, 3.98, 4.02, 4.08, 4.11, 4.15, 4.2 };
float percentages[ARRAY_SIZE] = { 0.0, 5.0, 10.0, 15.0, 20.0, 25.0, 30.0, 35.0, 40.0, 45.0, 50.0, 55.0, 60.0, 65.0, 70.0, 75.0, 80.0, 85.0, 90.0, 95.0, 100.0 };
float remaintime[ARRAY_SIZE] = { 0, 4.92, 9.85, 14.77, 19.68, 24.6, 29.52, 34.44, 39.36, 44.28, 49.2, 54.12, 59.04, 63.96, 68.88, 73.8, 78.72, 83.64, 88.56, 93.48, 98.4 };
bool BUT_UP_Pressed = false;
bool BUT_DOWN_Pressed = false;
bool BUT_PUSH_Pressed = false;
//Init services
VL53L0X sensor;
U8G2_SH1106_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, U8X8_PIN_NONE, I2C_SCL, I2C_SDA); // ESP32 Thing, HW I2C with pin remapping
Preferences preferences;
void PWM_config() {
ledcAttachPin(BUZPIN, PWM_CHANNEL);
ledcSetup(PWM_CHANNEL, PWM_FREQ, PWM_RESOLUTION);
ledcWrite(PWM_CHANNEL, 0);
}
void BuzzerSiren(int volume, bool init) {
static unsigned long startFreq;
static unsigned int step;
unsigned int pause[] = { 935, 435, 185, 60, 32 };
if (init) {
startFreq = millis();
step = 0;
}
if (millis() - startFreq > FREQ_CHANGE) {
if (step < 4) {
step++;
startFreq = millis();
}
}
ledcWrite(PWM_CHANNEL, volume);
digitalWrite(LED_PIN, HIGH);
delay(64);
ledcWrite(PWM_CHANNEL, 0);
digitalWrite(LED_PIN, LOW);
delay(pause[step]);
}
int BatteryPercent(float actualVoltage) {
int i = 0;
float actualPercent;
if (actualVoltage > voltages[ARRAY_SIZE - 1]) {
return 100;
}
if (actualVoltage < voltages[0]) {
return 0;
}
while (actualVoltage > voltages[i]) {
i++;
}
actualPercent = floorf(percentages[i - 1] + 5.0 * (voltages[i] - actualVoltage) / (voltages[i] - voltages[i - 1]));
return (int)actualPercent;
}
int BatteryRemain(float actualVoltage) {
int i = 0;
float actualRemain;
if (actualVoltage > voltages[ARRAY_SIZE - 1]) {
return remaintime[ARRAY_SIZE - 1];
}
if (actualVoltage < voltages[0]) {
return 0;
}
while (actualVoltage > voltages[i]) {
i++;
}
actualRemain = floorf(remaintime[i - 1] + 4.92 * (voltages[i] - actualVoltage) / (voltages[i] - voltages[i - 1]));
return (int)actualRemain;
}
bool CheckRange(int inputDistance, int inputSetup) {
bool retValue;
if (((inputDistance - inputSetup) > 20) && ((inputDistance + inputSetup) < 200)) {
return true;
} else {
return false;
}
}
int EEPROMHandler(int mode, byte dataToStore) {
short storedValue;
bool successWrite;
switch (mode) {
case 1:
storedValue = preferences.getChar("volume", -1);
return storedValue;
break;
case 2:
storedValue = preferences.getChar("displacement", -1);
return storedValue;
break;
case 3:
storedValue = preferences.putChar("volume", dataToStore);
return storedValue;
break;
case 4:
storedValue = preferences.putChar("displacement", dataToStore);
return storedValue;
break;
default:
break;
}
}
int DrawSettings(int val1, int val2, int val3, int val4, DISPLAY_SM mode) {
static int setValue;
static int readoutValue;
static bool newPageNeeded = true;
static bool readoutNeeded = true;
int values[MENUVALUES] = { val1, val2, val3, val4 };
int coord_x[MENUVALUES] = { 0, 0, 90, 90 };
int coord_y[MENUVALUES] = { 25, 62, 25, 62 };
static char plotString[3];
static int actualPos;
if (readoutNeeded) {
if (SET_VOL == mode) {
readoutValue = EEPROMHandler(1, 0);
}
if (SET_DIST == mode) {
readoutValue = EEPROMHandler(2, 0);
}
if (readoutValue > -1) {
setValue = readoutValue;
}
readoutNeeded = false;
}
if (newPageNeeded) {
BUT_PUSH_Pressed = false;
BUT_UP_Pressed = false;
BUT_DOWN_Pressed = false;
u8g2.clearBuffer();
for (int i = 0; i < MENUVALUES; i++) {
if (setValue == values[i]) {
u8g2.setDrawColor(0);
actualPos = i;
}
sprintf(plotString, "%02i", values[i]);
u8g2.drawStr(coord_x[i], coord_y[i], plotString);
u8g2.setDrawColor(1);
}
u8g2.sendBuffer();
newPageNeeded = false;
}
if (BUT_UP_Pressed) {
setValue = values[++actualPos];
if (actualPos > 3) {
actualPos = 0;
setValue = values[actualPos];
}
newPageNeeded = true;
delay(200);
}
if (BUT_DOWN_Pressed) {
setValue = values[--actualPos];
if (actualPos < 0) {
actualPos = 3;
setValue = values[actualPos];
}
newPageNeeded = true;
delay(200);
}
if (BUT_PUSH_Pressed) {
if (setValue != readoutValue) {
if (SET_VOL == mode) {
EEPROMHandler(3, setValue);
}
if (SET_DIST == mode) {
EEPROMHandler(4, setValue);
}
}
newPageNeeded = true;
readoutNeeded = true;
BUT_PUSH_Pressed = false;
delay(400);
return (setValue);
}
return (-1);
}
void DrawMainPage(int actualDistance, int actualPercent, bool distanceColor, int actRemTime) {
static int lastDrawnDistance;
char distanceStr[4];
char remainStr[6];
char batteryStr[5];
if (actualDistance < MAXVALIDDIST) {
dtostrf(actualDistance, 3, 0, distanceStr);
} else {
strcpy(distanceStr, "XXX");
}
dtostrf(actRemTime, 3, 1, remainStr);
dtostrf(actualPercent, 3, 0, batteryStr);
strcat(remainStr, "h");
strcat(batteryStr, "%");
if ((abs(lastDrawnDistance - actualDistance)) < TOF_INSENS_MM) {
return;
} else {
u8g2.clearBuffer();
u8g2.setDrawColor(1);
u8g2.setFont(u8g2_font_6x13B_tf); // choose a suitable font
u8g2.drawStr(10, 10, remainStr);
u8g2.drawUTF8(100, 10, batteryStr);
if (distanceColor) {
u8g2.setDrawColor(1);
} else {
u8g2.setDrawColor(0);
}
u8g2.setFont(u8g2_font_logisoso38_tf); // choose a suitable font
u8g2.drawStr(25, 60, distanceStr);
u8g2.sendBuffer();
lastDrawnDistance = actualDistance;
}
}
float BatteryVoltage() {
int batADC;
float batteryVolt;
static bool firstRun = true;
static float lastVal;
for (int i = 0; i < 63; i++) {
batADC = batADC + analogRead(BATV);
}
batADC = batADC >> 6;
batteryVolt = 1.04 * batADC / 1000;
if (!firstRun) {
batteryVolt = lastVal * IIRCOEFF + batteryVolt * (1 - IIRCOEFF); //IIR filter
lastVal = batteryVolt;
} else {
lastVal = batteryVolt;
firstRun = false;
}
return (batteryVolt);
}
void IRAM_ATTR IsrUp() {
BUT_UP_Pressed = true;
}
void IRAM_ATTR IsrDown() {
BUT_DOWN_Pressed = true;
}
void IRAM_ATTR IsrPush() {
BUT_PUSH_Pressed = true;
}
void setup() {
pinMode(HOLD_PIN, OUTPUT);
digitalWrite(HOLD_PIN, HIGH);
Serial.begin(115200);
Serial.println();
pinMode(LED_PIN, OUTPUT);
pinMode(BUT_PUSH, INPUT_PULLDOWN);
pinMode(BUT_DOWN, INPUT_PULLDOWN);
pinMode(BUT_UP, INPUT_PULLDOWN);
attachInterrupt(BUT_UP, IsrUp, RISING);
attachInterrupt(BUT_DOWN, IsrDown, RISING);
attachInterrupt(BUT_PUSH, IsrPush, RISING);
while (digitalRead(BUT_PUSH)) {
digitalWrite(LED_PIN, HIGH);
}
digitalWrite(LED_PIN, LOW);
pinMode(BATV, INPUT);
preferences.begin("bitemonster", false);
setCpuFrequencyMhz(CPU_LOW_FREQ);
delay(100);
btStop();
delay(100);
WiFi.mode(WIFI_OFF);
delay(100);
PWM_config();
delay(100);
u8g2.setI2CAddress(0x78);
u8g2.begin();
delay(100);
//u8g2.setContrast(255);
u8g2.setFont(u8g2_font_logisoso24_tf); // choose a suitable font
delay(100);
sensor.init();
delay(100);
}
void loop() {
static char distString[5];
int batAdc;
float batVolt;
static DISPLAY_SM displayBox = INIT;
int distance;
static int setupDistance;
static int setupVolume;
int batteryP;
int remainingTime;
bool inRange;
static bool firstRun = true;
static int settledDistance;
static bool isBiteIndicated = false;
static bool firstBite = true;
switch (displayBox) {
case INIT:
{
u8g2.clearBuffer();
u8g2.drawXBM(0, 10, logo_width, logo_height, logo_bits);
u8g2.sendBuffer();
delay(3000);
displayBox = SET_VOL;
break;
}
case SET_VOL:
{
setupVolume = DrawSettings(0, 33, 66, 99, SET_VOL);
if (setupVolume > -1) {
setupVolume = setupVolume / 11 * 14;
displayBox = SET_DIST;
BUT_PUSH_Pressed = false;
delay(400);
}
break;
}
case SET_DIST:
{
setupDistance = DrawSettings(20, 30, 40, 50, SET_DIST);
if (setupDistance > -1) {
displayBox = PREPARE;
BUT_PUSH_Pressed = false;
delay(400);
}
break;
}
case PREPARE:
{
distance = sensor.readRangeSingleMillimeters();
inRange = CheckRange(distance, setupDistance);
batVolt = BatteryVoltage();
batteryP = BatteryPercent(batVolt);
remainingTime = BatteryRemain(batVolt);
DrawMainPage(distance, batteryP, inRange, remainingTime);
if (BUT_PUSH_Pressed && inRange) {
settledDistance = distance;
displayBox = BITEWATCH;
BUT_PUSH_Pressed = false;
} else {
delay(1000);
}
break;
}
case BITEWATCH:
{
if (firstRun) {
u8g2.clearDisplay();
firstRun = false;
}
distance = sensor.readRangeSingleMillimeters();
if (abs(distance - settledDistance) > setupDistance) {
displayBox = ALARM;
}
break;
}
case ALARM:
{
BuzzerSiren(setupVolume, firstBite);
if (firstBite) {
u8g2.clearBuffer();
u8g2.drawXBM(8, 0, fish_width, fish_height, fish_bits);
u8g2.sendBuffer();
firstBite = false;
}
distance = sensor.readRangeSingleMillimeters();
if (abs(distance - settledDistance) < setupDistance) {
ledcWrite(PWM_CHANNEL, 0);
digitalWrite(LED_PIN, LOW);
u8g2.clearDisplay();
firstBite = true;
displayBox = BITEWATCH;
}
if (distance > MAXVALIDDIST) {
displayBox = FINISH;
}
break;
}
case FINISH:
{
u8g2.clearDisplay();
ledcWrite(PWM_CHANNEL, 0);
digitalWrite(LED_PIN, LOW);
digitalWrite(HOLD_PIN, LOW);
break;
}
}
}