Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added characters/memes-src/attention.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes-src/busy.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes-src/celebrate.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes-src/dizzy.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes-src/heart.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes-src/idle.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions characters/memes-src/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "memes",
"colors": {
"bg": "#000000",
"text": "#FFFFFF",
"textDim": "#808080"
},
"states": {
"sleep": "sleep.gif",
"idle": "idle.gif",
"busy": "busy.gif",
"attention": "attention.gif",
"celebrate": "celebrate.gif",
"dizzy": "dizzy.gif",
"heart": "heart.gif"
}
}
Binary file added characters/memes-src/sleep.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes/attention.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes/busy.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes/celebrate.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes/dizzy.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes/heart.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added characters/memes/idle.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
17 changes: 17 additions & 0 deletions characters/memes/manifest.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"name": "memes",
"colors": {
"bg": "#000000",
"text": "#FFFFFF",
"textDim": "#808080"
},
"states": {
"sleep": "sleep.gif",
"idle": "idle.gif",
"busy": "busy.gif",
"attention": "attention.gif",
"celebrate": "celebrate.gif",
"dizzy": "dizzy.gif",
"heart": "heart.gif"
}
}
Binary file added characters/memes/sleep.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
27 changes: 25 additions & 2 deletions src/ble_bridge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ static volatile bool secure = false;
static volatile uint32_t passkey = 0;
static volatile uint16_t mtu = 23;

// BD address of the currently connected peer. Set on connect, cleared on
// disconnect. Used by bleRemoveCurrentBond() so "unpair" only removes the
// host that sent the command rather than wiping all stored bonds.
static esp_bd_addr_t peerAddr;
static volatile bool hasPeer = false;

static void rxPush(const uint8_t* p, size_t n) {
for (size_t i = 0; i < n; i++) {
size_t next = (rxHead + 1) % RX_CAP;
Expand All @@ -47,15 +53,21 @@ class RxCallbacks : public BLECharacteristicCallbacks {
};

class ServerCallbacks : public BLEServerCallbacks {
void onConnect(BLEServer* s) override {
// Use the extended overload to capture the peer BD address.
void onConnect(BLEServer* s, esp_ble_gatts_cb_param_t* param) override {
connected = true;
Serial.println("[ble] connected");
memcpy((void*)peerAddr, param->connect.remote_bda, sizeof(esp_bd_addr_t));
hasPeer = true;
Serial.printf("[ble] connected %02x:%02x:%02x:%02x:%02x:%02x\n",
peerAddr[0], peerAddr[1], peerAddr[2],
peerAddr[3], peerAddr[4], peerAddr[5]);
}
void onDisconnect(BLEServer* s) override {
connected = false;
secure = false;
passkey = 0;
mtu = 23;
hasPeer = false;
Serial.println("[ble] disconnected");
// Restart advertising so the next client can find us.
BLEDevice::startAdvertising();
Expand Down Expand Up @@ -137,6 +149,17 @@ bool bleConnected() { return connected; }
bool bleSecure() { return secure; }
uint32_t blePasskey() { return passkey; }

void bleRemoveCurrentBond() {
if (!hasPeer) {
Serial.println("[ble] unpair: no peer to remove");
return;
}
esp_ble_remove_bond_device(peerAddr);
Serial.printf("[ble] removed bond %02x:%02x:%02x:%02x:%02x:%02x\n",
peerAddr[0], peerAddr[1], peerAddr[2],
peerAddr[3], peerAddr[4], peerAddr[5]);
}

void bleClearBonds() {
int n = esp_ble_get_bond_device_num();
if (n <= 0) return;
Expand Down
7 changes: 5 additions & 2 deletions src/ble_bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,11 @@ bool bleSecure();
// Non-zero while a 6-digit pairing passkey should be on screen. main.cpp
// renders it; cleared automatically on auth complete or disconnect.
uint32_t blePasskey();
// Erase all stored bonds (LTKs) from NVS. Called from the "unpair" cmd
// and from factory reset.
// Remove only the bond for the currently connected peer. Called from the
// "unpair" JSON command so a desktop can unpair itself without affecting
// bonds held by other hosts.
void bleRemoveCurrentBond();
// Erase ALL stored bonds from NVS. Called only from factory reset.
void bleClearBonds();
size_t bleAvailable();
int bleRead();
Expand Down
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -803,7 +803,7 @@ static void drawPetStats(const Palette& p) {

y += 20;
spr.setCursor(6, y - 2); spr.print("energy");
uint8_t en = statsEnergyTier();
uint8_t en = statsEnergyTier(_onUsb);
uint16_t enCol = (en >= 4) ? 0x07FF : (en >= 2) ? 0xFFE0 : HOT;
for (int i = 0; i < 5; i++) {
int px = 54 + i * 13;
Expand Down
7 changes: 5 additions & 2 deletions src/stats.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,15 +158,18 @@ inline uint8_t statsMoodTier() {
}

// Energy: starts at 3/5 on boot, tops up to full on nap end, drains 1 tier per 2h.
// When on USB power the floor is 1 so the pet never shows fully depleted while charging.
static uint32_t _lastNapEndMs = 0;
static uint8_t _energyAtNap = 3;

inline void statsOnWake() { _lastNapEndMs = millis(); _energyAtNap = 5; }

inline uint8_t statsEnergyTier() {
inline uint8_t statsEnergyTier(bool onUsb = false) {
uint32_t hoursSince = (millis() - _lastNapEndMs) / 3600000;
int8_t e = (int8_t)_energyAtNap - (int8_t)(hoursSince / 2);
if (e < 0) e = 0; if (e > 5) e = 5;
int8_t minEnergy = onUsb ? 1 : 0;
if (e < minEnergy) e = minEnergy;
if (e > 5) e = 5;
return (uint8_t)e;
}

Expand Down
2 changes: 1 addition & 1 deletion src/xfer.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ inline bool xferCommand(JsonDocument& doc) {
}

if (strcmp(cmd, "unpair") == 0) {
bleClearBonds();
bleRemoveCurrentBond();
_xAck("unpair", true);
return true;
}
Expand Down