forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.cpp
More file actions
1010 lines (863 loc) · 33.8 KB
/
Copy pathapp.cpp
File metadata and controls
1010 lines (863 loc) · 33.8 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
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
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "app.h"
#include <BLE2902.h>
#include <BLEAdvertisedDevice.h>
#include <BLEDevice.h>
#include <BLEScan.h>
#include <BLEUtils.h>
#include "config.h" // Use config.h for all configurations
#include "esp_camera.h"
#include "esp_sleep.h"
#include "mic.h"
#include "opus_encoder.h"
#include "ota.h"
// Battery state
float batteryVoltage = 0.0f;
int batteryPercentage = 0;
unsigned long lastBatteryCheck = 0;
// Device power state
bool deviceActive = true;
device_state_t deviceState = DEVICE_BOOTING;
// Button and LED state
volatile bool buttonPressed = false;
unsigned long buttonPressTime = 0;
led_status_t ledMode = LED_BOOT_SEQUENCE;
// Gentle power optimization
unsigned long lastActivity = 0;
bool powerSaveMode = false;
// Light sleep optimization - saves ~15mA = adds 3-4 hours battery life
bool lightSleepEnabled = true;
// ---------------------------------------------------------------------------------
// BLE - Using config.h definitions
// ---------------------------------------------------------------------------------
// Device Information Service UUIDs
#define DEVICE_INFORMATION_SERVICE_UUID (uint16_t) 0x180A
#define MANUFACTURER_NAME_STRING_CHAR_UUID (uint16_t) 0x2A29
#define MODEL_NUMBER_STRING_CHAR_UUID (uint16_t) 0x2A24
#define FIRMWARE_REVISION_STRING_CHAR_UUID (uint16_t) 0x2A26
#define HARDWARE_REVISION_STRING_CHAR_UUID (uint16_t) 0x2A27
#define SERIAL_NUMBER_STRING_CHAR_UUID (uint16_t) 0x2A25
// Main Friend Service - using config.h UUIDs
static BLEUUID serviceUUID(OMI_SERVICE_UUID);
static BLEUUID photoDataUUID(PHOTO_DATA_UUID);
static BLEUUID photoControlUUID(PHOTO_CONTROL_UUID);
static BLEUUID audioDataUUID(AUDIO_DATA_UUID);
static BLEUUID audioCodecUUID(AUDIO_CODEC_UUID);
// OTA Service UUIDs
static BLEUUID otaServiceUUID(OTA_SERVICE_UUID);
static BLEUUID otaControlUUID(OTA_CONTROL_UUID);
static BLEUUID otaDataUUID(OTA_DATA_UUID);
// Characteristics
BLECharacteristic *photoDataCharacteristic;
BLECharacteristic *photoControlCharacteristic;
BLECharacteristic *batteryLevelCharacteristic;
BLECharacteristic *audioDataCharacteristic;
BLECharacteristic *audioCodecCharacteristic;
BLECharacteristic *otaControlCharacteristic;
BLECharacteristic *otaDataCharacteristic;
// Audio state
bool audioEnabled = true;
volatile bool audioSubscribed = false;
uint16_t audioPacketIndex = 0;
// State
bool connected = false;
bool isCapturingPhotos = false;
int captureInterval = 0; // Interval in ms
unsigned long lastCaptureTime = 0;
// Audio ring buffer for encoded packets
#define AUDIO_TX_BUFFER_SIZE (AUDIO_TX_RING_BUFFER_SIZE * (OPUS_OUTPUT_MAX_BYTES + 2))
static uint8_t audio_tx_buffer[AUDIO_TX_BUFFER_SIZE];
static volatile size_t audio_tx_write_pos = 0;
static volatile size_t audio_tx_read_pos = 0;
static uint8_t audio_packet_buffer[OPUS_OUTPUT_MAX_BYTES + AUDIO_PACKET_HEADER_SIZE];
size_t sent_photo_bytes = 0;
size_t sent_photo_frames = 0;
bool photoDataUploading = false;
// -------------------------------------------------------------------------
// Camera Frame
// -------------------------------------------------------------------------
camera_fb_t *fb = nullptr;
image_orientation_t current_photo_orientation = ORIENTATION_0_DEGREES;
// Forward declarations
void handlePhotoControl(int8_t controlValue);
void readBatteryLevel();
void updateBatteryService();
void IRAM_ATTR buttonISR();
void handleButton();
void updateLED();
void blinkLED(int count, int delayMs);
void enterPowerSave();
void exitPowerSave();
void shutdownDevice();
void enableLightSleep();
// Audio forward declarations
void onMicData(int16_t *data, size_t samples);
void onOpusEncoded(uint8_t *data, size_t len);
void processAudioTx();
void broadcastAudioPacket(uint8_t *data, size_t len);
// -------------------------------------------------------------------------
// Button ISR
// -------------------------------------------------------------------------
void IRAM_ATTR buttonISR()
{
buttonPressed = true;
}
// -------------------------------------------------------------------------
// LED Functions
// -------------------------------------------------------------------------
void updateLED()
{
unsigned long now = millis();
static unsigned long bootStartTime = 0;
static unsigned long powerOffStartTime = 0;
switch (ledMode) {
case LED_BOOT_SEQUENCE:
if (bootStartTime == 0)
bootStartTime = now;
// 5 quick blinks over 1.5 seconds total (inverted logic: HIGH=OFF, LOW=ON)
if (now - bootStartTime < 1500) {
int blinkPhase = ((now - bootStartTime) / 150) % 2;
digitalWrite(STATUS_LED_PIN, !blinkPhase);
} else {
digitalWrite(STATUS_LED_PIN, HIGH); // OFF
ledMode = LED_NORMAL_OPERATION;
bootStartTime = 0;
}
break;
case LED_POWER_OFF_SEQUENCE:
if (powerOffStartTime == 0)
powerOffStartTime = now;
// 2 quick blinks over 800ms total (inverted logic: HIGH=OFF, LOW=ON)
if (now - powerOffStartTime < 800) {
int blinkPhase = ((now - powerOffStartTime) / 200) % 2;
digitalWrite(STATUS_LED_PIN, !blinkPhase);
} else {
digitalWrite(STATUS_LED_PIN, HIGH); // OFF
delay(100);
shutdownDevice();
}
break;
case LED_NORMAL_OPERATION:
default:
if (connected) {
// Connected - LED solid ON
digitalWrite(STATUS_LED_PIN, LOW);
} else {
// Disconnected - LED slow blink (1 sec on, 1 sec off)
int blinkPhase = (now / 1000) % 2;
digitalWrite(STATUS_LED_PIN, blinkPhase ? HIGH : LOW);
}
break;
}
}
void blinkLED(int count, int delayMs)
{
for (int i = 0; i < count; i++) {
digitalWrite(STATUS_LED_PIN, HIGH);
delay(delayMs);
digitalWrite(STATUS_LED_PIN, LOW);
delay(delayMs);
}
}
// -------------------------------------------------------------------------
// Button Handling
// -------------------------------------------------------------------------
void handleButton()
{
unsigned long now = millis();
static unsigned long lastDebounceTime = 0;
static bool buttonDown = false;
static bool longPressTriggered = false;
bool currentButtonState = !digitalRead(POWER_BUTTON_PIN); // Active low (pressed = true)
if (currentButtonState && !buttonDown) {
// Button just pressed - debounce
if (now - lastDebounceTime < 50) {
return;
}
buttonPressTime = now;
buttonDown = true;
longPressTriggered = false;
lastDebounceTime = now;
} else if (currentButtonState && buttonDown && !longPressTriggered) {
// Button still held - check for long press
unsigned long pressDuration = now - buttonPressTime;
if (pressDuration >= 2000) {
// Long press threshold reached - trigger power off immediately
longPressTriggered = true;
ledMode = LED_POWER_OFF_SEQUENCE;
}
} else if (!currentButtonState && buttonDown) {
// Button just released - debounce
if (now - lastDebounceTime < 50) {
return;
}
buttonDown = false;
unsigned long pressDuration = now - buttonPressTime;
lastDebounceTime = now;
// Only handle short press if long press wasn't already triggered
if (!longPressTriggered && pressDuration >= 50) {
// Short press - register activity
lastActivity = now;
if (powerSaveMode) {
exitPowerSave();
}
}
longPressTriggered = false;
}
buttonPressed = false;
}
// -------------------------------------------------------------------------
// Power Management
// -------------------------------------------------------------------------
void enterPowerSave()
{
if (!powerSaveMode) {
setCpuFrequencyMhz(MIN_CPU_FREQ_MHZ); // 40MHz for idle
powerSaveMode = true;
}
}
void exitPowerSave()
{
if (powerSaveMode) {
setCpuFrequencyMhz(NORMAL_CPU_FREQ_MHZ); // Back to 80MHz
powerSaveMode = false;
}
}
void enableLightSleep()
{
if (!lightSleepEnabled || !connected || photoDataUploading) {
return; // Don't sleep if disabled, not connected, or uploading
}
unsigned long now = millis();
// Don't sleep if there was recent activity (within 5 seconds)
if (now - lastActivity < 5000) {
return;
}
unsigned long timeUntilNextPhoto = 0;
if (isCapturingPhotos && captureInterval > 0) {
unsigned long timeSinceLastPhoto = now - lastCaptureTime;
if (timeSinceLastPhoto < captureInterval) {
timeUntilNextPhoto = captureInterval - timeSinceLastPhoto;
}
}
// Only sleep if we have at least 10 seconds until next photo
if (timeUntilNextPhoto > 10000) {
// Configure light sleep to wake on BLE events and timer
unsigned long sleepTime = timeUntilNextPhoto - 5000;
if (sleepTime > 15000)
sleepTime = 15000; // Max 15 seconds
esp_sleep_enable_timer_wakeup(sleepTime * 1000); // Wake 5s before photo or max 15s
esp_light_sleep_start();
lastActivity = millis(); // Update activity time after wake
}
}
void shutdownDevice()
{
Serial.println("Shutting down device...");
// Stop audio
mic_stop();
// Stop photo capture
isCapturingPhotos = false;
// Disconnect BLE gracefully
if (connected) {
Serial.println("Disconnecting BLE...");
}
// Turn off LED (inverted logic)
digitalWrite(STATUS_LED_PIN, HIGH);
// Enter deep sleep
esp_sleep_enable_ext0_wakeup(GPIO_NUM_1, 0); // Wake on button press
Serial.println("Entering deep sleep...");
delay(100);
esp_deep_sleep_start();
}
// -------------------------------------------------------------------------
// Audio Functions
// -------------------------------------------------------------------------
void onMicData(int16_t *data, size_t samples)
{
// Feed PCM data to Opus encoder
opus_receive_pcm(data, samples);
}
void onOpusEncoded(uint8_t *data, size_t len)
{
// Store encoded data in TX ring buffer
if (len > OPUS_OUTPUT_MAX_BYTES) {
return;
}
// Write length (2 bytes) + data
size_t packet_size = len + 2;
size_t next_write = (audio_tx_write_pos + packet_size) % AUDIO_TX_BUFFER_SIZE;
// Check for buffer overflow
if ((audio_tx_write_pos < audio_tx_read_pos && next_write >= audio_tx_read_pos) ||
(audio_tx_write_pos >= audio_tx_read_pos && next_write < audio_tx_write_pos &&
next_write >= audio_tx_read_pos)) {
// Buffer full, skip this packet
return;
}
// Write length
audio_tx_buffer[audio_tx_write_pos] = len & 0xFF;
audio_tx_buffer[(audio_tx_write_pos + 1) % AUDIO_TX_BUFFER_SIZE] = (len >> 8) & 0xFF;
// Write data
for (size_t i = 0; i < len; i++) {
audio_tx_buffer[(audio_tx_write_pos + 2 + i) % AUDIO_TX_BUFFER_SIZE] = data[i];
}
audio_tx_write_pos = next_write;
}
void broadcastAudioPacket(uint8_t *data, size_t len)
{
if (!connected || !audioSubscribed || audioDataCharacteristic == nullptr) {
return;
}
// Build packet: 2 bytes index + 1 byte sub-index + data
audio_packet_buffer[0] = audioPacketIndex & 0xFF;
audio_packet_buffer[1] = (audioPacketIndex >> 8) & 0xFF;
audio_packet_buffer[2] = 0; // Sub-index (for fragmentation if needed)
memcpy(audio_packet_buffer + AUDIO_PACKET_HEADER_SIZE, data, len);
audioDataCharacteristic->setValue(audio_packet_buffer, len + AUDIO_PACKET_HEADER_SIZE);
audioDataCharacteristic->notify();
audioPacketIndex++;
}
void processAudioTx()
{
if (!connected || !audioSubscribed) {
return;
}
if (audioDataCharacteristic == nullptr) {
return;
}
// Check if we have data in the ring buffer
while (audio_tx_read_pos != audio_tx_write_pos) {
// Read length
uint16_t len =
audio_tx_buffer[audio_tx_read_pos] | (audio_tx_buffer[(audio_tx_read_pos + 1) % AUDIO_TX_BUFFER_SIZE] << 8);
if (len == 0 || len > OPUS_OUTPUT_MAX_BYTES) {
// Invalid packet, skip
audio_tx_read_pos = (audio_tx_read_pos + 2) % AUDIO_TX_BUFFER_SIZE;
continue;
}
// Read data
static uint8_t temp_data[OPUS_OUTPUT_MAX_BYTES];
for (size_t i = 0; i < len; i++) {
temp_data[i] = audio_tx_buffer[(audio_tx_read_pos + 2 + i) % AUDIO_TX_BUFFER_SIZE];
}
// Update read position
audio_tx_read_pos = (audio_tx_read_pos + 2 + len) % AUDIO_TX_BUFFER_SIZE;
// Send packet
broadcastAudioPacket(temp_data, len);
// Small delay to prevent BLE congestion
delay(1);
}
}
// -------------------------------------------------------------------------
// BLE Callbacks
// -------------------------------------------------------------------------
class ServerHandler : public BLEServerCallbacks
{
void onConnect(BLEServer *server) override
{
connected = true;
audioSubscribed = false;
lastActivity = millis(); // Register activity - prevents sleep
Serial.println(">>> BLE Client connected.");
// Send current battery level on connect
updateBatteryService();
}
void onDisconnect(BLEServer *server) override
{
connected = false;
audioSubscribed = false;
Serial.println("<<< BLE Client disconnected. Restarting advertising.");
BLEDevice::startAdvertising();
}
};
// Callback for Audio Data CCCD (Client Characteristic Configuration Descriptor)
class AudioCCCDCallback : public BLEDescriptorCallbacks
{
void onWrite(BLEDescriptor *pDescriptor)
{
uint8_t *value = pDescriptor->getValue();
if (value && pDescriptor->getLength() >= 2) {
// Check notification bit (bit 0)
if (value[0] & 0x01) {
audioSubscribed = true;
Serial.println("Audio notifications enabled");
} else {
audioSubscribed = false;
Serial.println("Audio notifications disabled");
}
}
}
};
class AudioDataCallback : public BLECharacteristicCallbacks
{
void onStatus(BLECharacteristic *pCharacteristic, Status s, uint32_t code)
{
if (s == Status::SUCCESS_NOTIFY || s == Status::SUCCESS_INDICATE) {
// Notification sent successfully
}
}
void onRead(BLECharacteristic *pCharacteristic)
{
// Client read the characteristic
}
};
class PhotoControlCallback : public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *characteristic) override
{
if (characteristic->getLength() == 1) {
int8_t received = characteristic->getData()[0];
Serial.print("PhotoControl received: ");
Serial.println(received);
lastActivity = millis(); // Register activity - prevents sleep
handlePhotoControl(received);
}
}
};
class OTAControlCallback : public BLECharacteristicCallbacks
{
void onWrite(BLECharacteristic *pChar) override
{
std::string value = pChar->getValue();
if (value.length() > 0) {
ota_handle_command((uint8_t *) value.data(), value.length());
}
}
void onRead(BLECharacteristic *pChar) override
{
uint8_t status[2] = {ota_get_status(), 0};
pChar->setValue(status, 2);
}
};
// -------------------------------------------------------------------------
// Battery Functions
// -------------------------------------------------------------------------
void readBatteryLevel()
{
// Take multiple ADC readings for stability
int adcSum = 0;
for (int i = 0; i < 10; i++) {
int value = analogRead(BATTERY_ADC_PIN);
adcSum += value;
delay(10);
}
int adcValue = adcSum / 10;
// ESP32-S3 ADC: 12-bit (0-4095), reference voltage ~3.3V
float adcVoltage = (adcValue / 4095.0f) * 3.3f;
// Apply voltage divider ratio to get actual battery voltage
batteryVoltage = adcVoltage * VOLTAGE_DIVIDER_RATIO;
// Clamp voltage to reasonable range
if (batteryVoltage > 5.0f)
batteryVoltage = 5.0f;
if (batteryVoltage < 2.5f)
batteryVoltage = 2.5f;
// Load-compensated battery calculation (accounts for voltage sag under load)
float loadCompensatedMax = BATTERY_MAX_VOLTAGE;
float loadCompensatedMin = BATTERY_MIN_VOLTAGE;
// More accurate percentage calculation for load conditions
if (batteryVoltage >= loadCompensatedMax) {
batteryPercentage = 100;
} else if (batteryVoltage <= loadCompensatedMin) {
batteryPercentage = 0;
} else {
float range = loadCompensatedMax - loadCompensatedMin;
batteryPercentage = (int) (((batteryVoltage - loadCompensatedMin) / range) * 100.0f);
}
// Smooth percentage changes to avoid jumpy readings
static int lastBatteryPercentage = batteryPercentage;
if (abs(batteryPercentage - lastBatteryPercentage) > 5) {
batteryPercentage = lastBatteryPercentage + (batteryPercentage > lastBatteryPercentage ? 2 : -2);
}
lastBatteryPercentage = batteryPercentage;
// Clamp percentage
if (batteryPercentage > 100)
batteryPercentage = 100;
if (batteryPercentage < 0)
batteryPercentage = 0;
// Battery status with load info
Serial.print("Battery: ");
Serial.print(batteryVoltage);
Serial.print("V (");
Serial.print(batteryPercentage);
Serial.print("%) [Load-compensated: ");
Serial.print(loadCompensatedMin);
Serial.print("V-");
Serial.print(loadCompensatedMax);
Serial.println("V]");
}
void updateBatteryService()
{
if (batteryLevelCharacteristic) {
uint8_t batteryLevel = (uint8_t) batteryPercentage;
batteryLevelCharacteristic->setValue(&batteryLevel, 1);
if (connected) {
batteryLevelCharacteristic->notify();
}
}
}
// -------------------------------------------------------------------------
// configure_ble()
// -------------------------------------------------------------------------
void configure_ble()
{
Serial.println("Initializing BLE...");
BLEDevice::init(BLE_DEVICE_NAME);
BLEServer *server = BLEDevice::createServer();
server->setCallbacks(new ServerHandler());
// Main service
BLEService *service = server->createService(serviceUUID);
// Audio Data characteristic (for streaming audio to app)
audioDataCharacteristic = service->createCharacteristic(
audioDataUUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLE2902 *audioCcc = new BLE2902();
audioCcc->setNotifications(true);
audioCcc->setCallbacks(new AudioCCCDCallback());
audioDataCharacteristic->addDescriptor(audioCcc);
audioDataCharacteristic->setCallbacks(new AudioDataCallback());
// Audio Codec characteristic (tells app which codec we're using)
audioCodecCharacteristic = service->createCharacteristic(audioCodecUUID, BLECharacteristic::PROPERTY_READ);
uint8_t codecId = opus_get_codec_id();
audioCodecCharacteristic->setValue(&codecId, 1);
// Photo Data characteristic
photoDataCharacteristic = service->createCharacteristic(
photoDataUUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLE2902 *ccc = new BLE2902();
ccc->setNotifications(true);
photoDataCharacteristic->addDescriptor(ccc);
// Photo Control characteristic
photoControlCharacteristic = service->createCharacteristic(photoControlUUID, BLECharacteristic::PROPERTY_WRITE);
photoControlCharacteristic->setCallbacks(new PhotoControlCallback());
uint8_t controlValue = 0;
photoControlCharacteristic->setValue(&controlValue, 1);
// Battery Service
BLEService *batteryService = server->createService(BATTERY_SERVICE_UUID);
batteryLevelCharacteristic = batteryService->createCharacteristic(
BATTERY_LEVEL_UUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLE2902 *batteryCcc = new BLE2902();
batteryCcc->setNotifications(true);
batteryLevelCharacteristic->addDescriptor(batteryCcc);
// Set initial battery level
readBatteryLevel();
uint8_t initialBatteryLevel = (uint8_t) batteryPercentage;
batteryLevelCharacteristic->setValue(&initialBatteryLevel, 1);
// Device Information Service
BLEService *deviceInfoService = server->createService(DEVICE_INFORMATION_SERVICE_UUID);
BLECharacteristic *manufacturerNameCharacteristic =
deviceInfoService->createCharacteristic(MANUFACTURER_NAME_STRING_CHAR_UUID, BLECharacteristic::PROPERTY_READ);
BLECharacteristic *modelNumberCharacteristic =
deviceInfoService->createCharacteristic(MODEL_NUMBER_STRING_CHAR_UUID, BLECharacteristic::PROPERTY_READ);
BLECharacteristic *firmwareRevisionCharacteristic =
deviceInfoService->createCharacteristic(FIRMWARE_REVISION_STRING_CHAR_UUID, BLECharacteristic::PROPERTY_READ);
BLECharacteristic *hardwareRevisionCharacteristic =
deviceInfoService->createCharacteristic(HARDWARE_REVISION_STRING_CHAR_UUID, BLECharacteristic::PROPERTY_READ);
BLECharacteristic *serialNumberCharacteristic =
deviceInfoService->createCharacteristic(SERIAL_NUMBER_STRING_CHAR_UUID, BLECharacteristic::PROPERTY_READ);
manufacturerNameCharacteristic->setValue(MANUFACTURER_NAME);
modelNumberCharacteristic->setValue(BLE_DEVICE_NAME);
firmwareRevisionCharacteristic->setValue(FIRMWARE_VERSION_STRING);
hardwareRevisionCharacteristic->setValue(HARDWARE_REVISION);
// Generate serial number from ESP32 chip ID
uint64_t chipId = ESP.getEfuseMac();
char serialNumber[17];
snprintf(serialNumber, sizeof(serialNumber), "%04X%08X", (uint16_t) (chipId >> 32), (uint32_t) chipId);
serialNumberCharacteristic->setValue(serialNumber);
// OTA Service
BLEService *otaService = server->createService(otaServiceUUID);
// OTA Control characteristic (for receiving commands and reading status)
otaControlCharacteristic = otaService->createCharacteristic(
otaControlUUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_WRITE);
otaControlCharacteristic->setCallbacks(new OTAControlCallback());
// OTA Data characteristic (for progress notifications)
otaDataCharacteristic = otaService->createCharacteristic(
otaDataUUID, BLECharacteristic::PROPERTY_READ | BLECharacteristic::PROPERTY_NOTIFY);
BLE2902 *otaCcc = new BLE2902();
otaCcc->setNotifications(true);
otaDataCharacteristic->addDescriptor(otaCcc);
// Set OTA characteristics for the OTA module
ota_set_characteristics(otaControlCharacteristic, otaDataCharacteristic);
// Start services
service->start();
batteryService->start();
deviceInfoService->start();
otaService->start();
// Start advertising
BLEAdvertising *advertising = BLEDevice::getAdvertising();
advertising->addServiceUUID(service->getUUID()); // Main service (fits in 31 bytes)
advertising->setScanResponse(true);
advertising->setMinPreferred(BLE_ADV_MIN_INTERVAL);
advertising->setMaxPreferred(BLE_ADV_MAX_INTERVAL);
BLEDevice::startAdvertising();
Serial.println("BLE initialized and advertising started.");
}
// -------------------------------------------------------------------------
// Camera
// -------------------------------------------------------------------------
bool take_photo()
{
// Release previous buffer
if (fb) {
Serial.println("Releasing previous camera buffer...");
esp_camera_fb_return(fb);
fb = nullptr;
}
Serial.println("Capturing photo...");
fb = esp_camera_fb_get();
if (!fb) {
Serial.println("Failed to get camera frame buffer!");
return false;
}
Serial.print("Photo captured: ");
Serial.print(fb->len);
Serial.println(" bytes.");
// Set fixed orientation for the captured photo
current_photo_orientation = FIXED_IMAGE_ORIENTATION;
Serial.println("Photo orientation set to 180 degrees (fixed).");
lastActivity = millis(); // Register activity
return true;
}
void handlePhotoControl(int8_t controlValue)
{
if (controlValue == -1) {
Serial.println("Received command: Single photo.");
isCapturingPhotos = true;
captureInterval = 0;
} else if (controlValue == 0) {
Serial.println("Received command: Stop photo capture.");
isCapturingPhotos = false;
captureInterval = 0;
} else if (controlValue >= 5 && controlValue <= 300) {
Serial.print("Received command: Start interval capture with parameter ");
Serial.println(controlValue);
// Use fixed interval from config for optimal battery life
captureInterval = PHOTO_CAPTURE_INTERVAL_MS;
Serial.print("Using configured interval: ");
Serial.print(captureInterval / 1000);
Serial.println(" seconds");
isCapturingPhotos = true;
lastCaptureTime = millis() - captureInterval;
}
}
// -------------------------------------------------------------------------
// configure_camera()
// -------------------------------------------------------------------------
void configure_camera()
{
Serial.println("Initializing camera...");
camera_config_t config;
config.ledc_channel = LEDC_CHANNEL_0;
config.ledc_timer = LEDC_TIMER_0;
config.pin_d0 = Y2_GPIO_NUM;
config.pin_d1 = Y3_GPIO_NUM;
config.pin_d2 = Y4_GPIO_NUM;
config.pin_d3 = Y5_GPIO_NUM;
config.pin_d4 = Y6_GPIO_NUM;
config.pin_d5 = Y7_GPIO_NUM;
config.pin_d6 = Y8_GPIO_NUM;
config.pin_d7 = Y9_GPIO_NUM;
config.pin_xclk = XCLK_GPIO_NUM;
config.pin_pclk = PCLK_GPIO_NUM;
config.pin_vsync = VSYNC_GPIO_NUM;
config.pin_href = HREF_GPIO_NUM;
config.pin_sscb_sda = SIOD_GPIO_NUM;
config.pin_sscb_scl = SIOC_GPIO_NUM;
config.pin_pwdn = PWDN_GPIO_NUM;
config.pin_reset = RESET_GPIO_NUM;
config.xclk_freq_hz = CAMERA_XCLK_FREQ;
// Use config.h camera settings optimized for battery life
config.frame_size = CAMERA_FRAME_SIZE;
config.pixel_format = PIXFORMAT_JPEG;
config.fb_count = 1;
config.jpeg_quality = CAMERA_JPEG_QUALITY;
config.fb_location = CAMERA_FB_IN_PSRAM;
config.grab_mode = CAMERA_GRAB_LATEST;
esp_err_t err = esp_camera_init(&config);
if (err != ESP_OK) {
Serial.printf("Camera init failed with error 0x%x\n", err);
} else {
Serial.println("Camera initialized successfully.");
}
}
// -------------------------------------------------------------------------
// Setup & Loop
// -------------------------------------------------------------------------
// A small buffer for sending photo chunks over BLE
static uint8_t *s_compressed_frame_2 = nullptr;
void setup_app()
{
Serial.begin(921600);
Serial.println("Setup started...");
// Initialize GPIO
pinMode(POWER_BUTTON_PIN, INPUT_PULLUP);
pinMode(STATUS_LED_PIN, OUTPUT);
// LED uses inverted logic: HIGH = OFF, LOW = ON
digitalWrite(STATUS_LED_PIN, HIGH);
// Setup button interrupt
attachInterrupt(digitalPinToInterrupt(POWER_BUTTON_PIN), buttonISR, CHANGE);
// Start LED boot sequence
ledMode = LED_BOOT_SEQUENCE;
// Power optimization from config.h
setCpuFrequencyMhz(NORMAL_CPU_FREQ_MHZ);
lastActivity = millis();
configure_ble();
configure_camera();
// Allocate buffer for photo chunks (200 bytes + 2 for frame index)
s_compressed_frame_2 = (uint8_t *) ps_calloc(202, sizeof(uint8_t));
if (!s_compressed_frame_2) {
Serial.println("Failed to allocate chunk buffer!");
} else {
Serial.println("Chunk buffer allocated successfully.");
}
// Set default capture interval from config
isCapturingPhotos = true;
captureInterval = PHOTO_CAPTURE_INTERVAL_MS;
lastCaptureTime = millis() - captureInterval;
Serial.print("Default capture interval set to ");
Serial.print(PHOTO_CAPTURE_INTERVAL_MS / 1000);
Serial.println(" seconds.");
// Initial battery reading
// Battery voltage divider
analogReadResolution(12); // optional: set 12-bit resolution
analogSetPinAttenuation(BATTERY_ADC_PIN, ADC_11db); // set attenuation for full 3.3V range
readBatteryLevel();
deviceState = DEVICE_ACTIVE;
// Initialize audio subsystem
Serial.println("Initializing audio subsystem...");
if (opus_encoder_init()) {
opus_set_callback(onOpusEncoded);
if (mic_start()) {
mic_set_callback(onMicData);
Serial.println("Audio subsystem initialized successfully.");
} else {
Serial.println("Failed to start microphone!");
}
} else {
Serial.println("Failed to initialize Opus encoder!");
}
Serial.println("Setup complete.");
Serial.println("Light sleep optimization enabled for extended battery life.");
}
void loop_app()
{
unsigned long now = millis();
// Handle button presses
handleButton();
// Update LED
updateLED();
// Process OTA updates
ota_loop();
// Process microphone data - always run to keep audio realtime
if (audioEnabled && mic_is_running()) {
mic_process();
opus_process();
}
// Send audio packets over BLE - PRIORITY over photo
if (connected && audioSubscribed) {
processAudioTx();
}
// Check for power save mode (gentle optimization)
if (!connected && !photoDataUploading && (now - lastActivity > IDLE_THRESHOLD_MS)) {
enterPowerSave();
} else if (connected || photoDataUploading) {
if (powerSaveMode)
exitPowerSave();
lastActivity = now;
}
// Check battery level periodically
if (now - lastBatteryCheck >= BATTERY_TASK_INTERVAL_MS) {
readBatteryLevel();
updateBatteryService();
lastBatteryCheck = now;
}
// Force battery update on first connection
static bool firstBatteryUpdate = true;
if (connected && firstBatteryUpdate) {
readBatteryLevel();
updateBatteryService();
firstBatteryUpdate = false;
}
// Check if it's time to capture a photo
if (isCapturingPhotos && !photoDataUploading && connected) {
if ((captureInterval == 0) || (now - lastCaptureTime >= (unsigned long) captureInterval)) {
if (captureInterval == 0) {
// Single shot if interval=0
isCapturingPhotos = false;
}
Serial.println("Interval reached. Capturing photo...");
if (take_photo()) {
Serial.println("Photo capture successful. Starting upload...");
photoDataUploading = true;
sent_photo_bytes = 0;
sent_photo_frames = 0;
lastCaptureTime = now;
}
}
}
// If uploading, send chunks over BLE (interleave with audio - max 2 chunks per loop)
static int photo_chunks_this_loop = 0;
if (photoDataUploading && fb && photo_chunks_this_loop < 2) {
// Yield to audio if audio buffer has data
if (audioSubscribed && audio_tx_read_pos != audio_tx_write_pos) {
photo_chunks_this_loop = 0; // Reset for next loop
} else {
photo_chunks_this_loop++;
}
size_t remaining = fb->len - sent_photo_bytes;
if (remaining > 0) {
size_t bytes_to_copy;
if (sent_photo_frames == 0) {
// First chunk: includes orientation metadata
s_compressed_frame_2[0] = 0; // Frame index low byte
s_compressed_frame_2[1] = 0; // Frame index high byte
s_compressed_frame_2[2] = (uint8_t) current_photo_orientation;
bytes_to_copy = (remaining > 199) ? 199 : remaining;
memcpy(&s_compressed_frame_2[3], &fb->buf[sent_photo_bytes], bytes_to_copy);
photoDataCharacteristic->setValue(s_compressed_frame_2, bytes_to_copy + 3);
} else {
// Subsequent chunks
s_compressed_frame_2[0] = (uint8_t) (sent_photo_frames & 0xFF);
s_compressed_frame_2[1] = (uint8_t) ((sent_photo_frames >> 8) & 0xFF);
bytes_to_copy = (remaining > 200) ? 200 : remaining;
memcpy(&s_compressed_frame_2[2], &fb->buf[sent_photo_bytes], bytes_to_copy);
photoDataCharacteristic->setValue(s_compressed_frame_2, bytes_to_copy + 2);
}
photoDataCharacteristic->notify();
sent_photo_bytes += bytes_to_copy;
sent_photo_frames++;
Serial.print("Uploading chunk ");
Serial.print(sent_photo_frames);
Serial.print(" (");
Serial.print(bytes_to_copy);
Serial.print(" bytes), ");
Serial.print(remaining - bytes_to_copy);
Serial.println(" bytes remaining.");
lastActivity = now; // Register activity
} else {
// End of photo marker
s_compressed_frame_2[0] = 0xFF;
s_compressed_frame_2[1] = 0xFF;
photoDataCharacteristic->setValue(s_compressed_frame_2, 2);
photoDataCharacteristic->notify();
Serial.println("Photo upload complete.");
photoDataUploading = false;
// Free camera buffer
esp_camera_fb_return(fb);
fb = nullptr;
Serial.println("Camera frame buffer freed.");
photo_chunks_this_loop = 0; // Reset counter
}
} else {
photo_chunks_this_loop = 0; // Reset when not uploading
}
// Light sleep optimization - major power savings while maintaining BLE
// Disable light sleep when audio is active
if (!photoDataUploading && !audioSubscribed) {
enableLightSleep();
}