|
| 1 | +#include "StatusLEDModule.h" |
| 2 | +#include "MeshService.h" |
| 3 | +#include "configuration.h" |
| 4 | +#include <Arduino.h> |
| 5 | + |
| 6 | +/* |
| 7 | +StatusLEDModule manages the device's status LEDs, updating their states based on power and Bluetooth status. |
| 8 | +It reflects charging, charged, discharging, and Bluetooth connection states using the appropriate LEDs. |
| 9 | +*/ |
| 10 | +StatusLEDModule *statusLEDModule; |
| 11 | + |
| 12 | +StatusLEDModule::StatusLEDModule() : concurrency::OSThread("StatusLEDModule") |
| 13 | +{ |
| 14 | + bluetoothStatusObserver.observe(&bluetoothStatus->onNewStatus); |
| 15 | + powerStatusObserver.observe(&powerStatus->onNewStatus); |
| 16 | +} |
| 17 | + |
| 18 | +int StatusLEDModule::handleStatusUpdate(const meshtastic::Status *arg) |
| 19 | +{ |
| 20 | + switch (arg->getStatusType()) { |
| 21 | + case STATUS_TYPE_POWER: { |
| 22 | + meshtastic::PowerStatus *powerStatus = (meshtastic::PowerStatus *)arg; |
| 23 | + if (powerStatus->getHasUSB()) { |
| 24 | + power_state = charging; |
| 25 | + if (powerStatus->getBatteryChargePercent() >= 100) { |
| 26 | + power_state = charged; |
| 27 | + } |
| 28 | + } else { |
| 29 | + power_state = discharging; |
| 30 | + } |
| 31 | + break; |
| 32 | + } |
| 33 | + case STATUS_TYPE_BLUETOOTH: { |
| 34 | + meshtastic::BluetoothStatus *bluetoothStatus = (meshtastic::BluetoothStatus *)arg; |
| 35 | + switch (bluetoothStatus->getConnectionState()) { |
| 36 | + case meshtastic::BluetoothStatus::ConnectionState::DISCONNECTED: { |
| 37 | + ble_state = unpaired; |
| 38 | + PAIRING_LED_starttime = millis(); |
| 39 | + break; |
| 40 | + } |
| 41 | + case meshtastic::BluetoothStatus::ConnectionState::PAIRING: { |
| 42 | + ble_state = pairing; |
| 43 | + PAIRING_LED_starttime = millis(); |
| 44 | + break; |
| 45 | + } |
| 46 | + case meshtastic::BluetoothStatus::ConnectionState::CONNECTED: { |
| 47 | + ble_state = connected; |
| 48 | + PAIRING_LED_starttime = millis(); |
| 49 | + break; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + break; |
| 54 | + } |
| 55 | + } |
| 56 | + return 0; |
| 57 | +}; |
| 58 | + |
| 59 | +int32_t StatusLEDModule::runOnce() |
| 60 | +{ |
| 61 | + |
| 62 | + if (power_state == charging) { |
| 63 | + CHARGE_LED_state = !CHARGE_LED_state; |
| 64 | + } else if (power_state == charged) { |
| 65 | + CHARGE_LED_state = LED_STATE_ON; |
| 66 | + } else { |
| 67 | + CHARGE_LED_state = LED_STATE_OFF; |
| 68 | + } |
| 69 | + |
| 70 | + if (!config.bluetooth.enabled || PAIRING_LED_starttime + 30 * 1000 < millis()) { |
| 71 | + PAIRING_LED_state = LED_STATE_OFF; |
| 72 | + } else if (ble_state == unpaired) { |
| 73 | + if (slowTrack) { |
| 74 | + PAIRING_LED_state = !PAIRING_LED_state; |
| 75 | + slowTrack = false; |
| 76 | + } else { |
| 77 | + slowTrack = true; |
| 78 | + } |
| 79 | + } else if (ble_state == pairing) { |
| 80 | + PAIRING_LED_state = !PAIRING_LED_state; |
| 81 | + } else { |
| 82 | + PAIRING_LED_state = LED_STATE_ON; |
| 83 | + } |
| 84 | + |
| 85 | +#ifdef LED_CHARGE |
| 86 | + digitalWrite(LED_CHARGE, CHARGE_LED_state); |
| 87 | +#endif |
| 88 | + // digitalWrite(green_LED_PIN, LED_STATE_OFF); |
| 89 | +#ifdef LED_PAIRING |
| 90 | + digitalWrite(LED_PAIRING, PAIRING_LED_state); |
| 91 | +#endif |
| 92 | + |
| 93 | + return (my_interval); |
| 94 | +} |
0 commit comments