Skip to content

Commit

Permalink
Migrate framework to espidf (#148)
Browse files Browse the repository at this point in the history
* Initial migration to espidf.

Some libraries depend on Arduino types, thus we build as an espidf
project with Arduino as a component.

Enable ESP32 dynamic frequency control.
Enable FreeRTOS tickless idle.
Enable ESP32 power management.

Minimally tested on M5Core2 and M5StickC Plus2.

* Various tweaks.

Use array length for Fujifilm token write.

Adjust battery smoothing, reduce weight on newer values.

Convert UI loop delay to proper 5ms.

Format clean-up.

* Fix mis-formatted CMakeLists.txt.

* Restore missing NimBLE configuration.

* Refactor Furble.cpp into Scan.cpp.

* Update battery life time from test results.

* Remove app0 binary.
  • Loading branch information
gkoh authored Jan 2, 2025
1 parent bb4b632 commit db551aa
Show file tree
Hide file tree
Showing 21 changed files with 6,609 additions and 168 deletions.
3 changes: 0 additions & 3 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ jobs:
FIRMWARE_NAME: furble-${{ matrix.platform }}-${{ github.ref_name }}+${{ github.run_attempt }}.bin
BOOTLOADER_NAME: furble-bootloader-${{ matrix.platform }}-${{ github.ref_name }}+${{ github.run_attempt }}.bin
PARTITIONS_NAME: furble-partitions-${{ matrix.platform }}-${{ github.ref_name }}+${{ github.run_attempt }}.bin
BOOT_APP0_NAME: furble-boot-app0-${{ matrix.platform }}-${{ github.ref_name }}+${{ github.run_attempt }}.bin

steps:
- uses: actions/checkout@v4
Expand All @@ -49,7 +48,6 @@ jobs:
mv .pio/build/${{ matrix.platform }}/firmware.bin $FIRMWARE_NAME
mv .pio/build/${{ matrix.platform }}/bootloader.bin $BOOTLOADER_NAME
mv .pio/build/${{ matrix.platform }}/partitions.bin $PARTITIONS_NAME
mv ~/.platformio/packages/framework-arduinoespressif32/tools/partitions/boot_app0.bin $BOOT_APP0_NAME
- name: Generate manifest (${{ matrix.platform }})
working-directory: ./web-installer
Expand All @@ -67,7 +65,6 @@ jobs:
${{ env.FIRMWARE_NAME }}
${{ env.BOOTLOADER_NAME }}
${{ env.PARTITIONS_NAME }}
${{ env.BOOT_APP0_NAME }}
manifest_${{ matrix.platform }}.json
if-no-files-found: error

Expand Down
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
.pio
.vscode
src/furble.ino.cpp
sdkconfig.*.old
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
cmake_minimum_required(VERSION 3.16.0)
include($ENV{IDF_PATH}/tools/cmake/project.cmake)
project(furble)
14 changes: 7 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,13 +352,13 @@ the following libraries:

# Known Issues

- depending on your perspective, battery life is anywhere from reasonable to abysmal
- with an active BLE connection, the ESP32 consumes around 50mA
- an M5StickC Plus2 would last around 4 hours
- an M5StickC Plus would last around 2.5 hours
- an old M5StickC would last less than 2 hours
- if battery life is crucial, and form factor is not, consider an M5Stack Core2 with the 1500mAh module
- this might last 30 hours
- depending on your perspective, battery life is anywhere from reasonable to bad
- with an active BLE connection and power management, the ESP32 consumes around 30mA
- an M5StickC Plus2 would last around 6 hours
- an M5StickC Plus would last around 4 hours
- an old M5StickC would last around 3 hours
- if battery life is crucial, and form factor is not, consider an M5Stack Core with the 1500mAh module
- this might last 50 hours

# Things To Do

Expand Down
8 changes: 7 additions & 1 deletion lib/furble/Device.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <Esp.h>
#include <NimBLEDevice.h>

#include "Device.h"
#include "FurbleTypes.h"
Expand All @@ -20,7 +21,7 @@ static uint32_t xorshift(uint32_t x) {
return x;
}

void Device::init(void) {
void Device::init(esp_power_level_t power) {
uint32_t chip_id = (uint32_t)ESP.getEfuseMac();
for (size_t i = 0; i < UUID128_AS_32_LEN; i++) {
chip_id = xorshift(chip_id);
Expand All @@ -31,6 +32,11 @@ void Device::init(void) {
snprintf(m_StringID, DEVICE_ID_STR_MAX, "%s-%05x", FURBLE_STR, m_Uuid.uint32[0] & 0xFFFFF);

m_ID = std::string(m_StringID);

NimBLEDevice::init(m_ID);
NimBLEDevice::setPower(power);
NimBLEDevice::setSecurityAuth(true, true, true);
// NimBLEDevice::setOwnAddrType(BLE_OWN_ADDR_PUBLIC);
}

void Device::getUUID128(uuid128_t *uuid) {
Expand Down
4 changes: 3 additions & 1 deletion lib/furble/Device.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <cstdint>

#include <NimBLEDevice.h>

namespace Furble {

class Device {
Expand All @@ -22,7 +24,7 @@ class Device {
/**
* Initialise the device.
*/
static void init(void);
static void init(esp_power_level_t power);

/**
* Return a device consistent 128-bit UUID.
Expand Down
2 changes: 1 addition & 1 deletion lib/furble/Fujifilm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ bool Fujifilm::_connect(void) {
if (!pChr->canWrite())
return false;
print_token(m_Token);
if (!pChr->writeValue(m_Token.data(), sizeof(m_Token), true))
if (!pChr->writeValue(m_Token.data(), m_Token.size(), true))
return false;
ESP_LOGI(LOG_TAG, "Paired!");
m_Progress = 30;
Expand Down
79 changes: 0 additions & 79 deletions lib/furble/Furble.cpp

This file was deleted.

58 changes: 0 additions & 58 deletions lib/furble/Furble.h

This file was deleted.

70 changes: 70 additions & 0 deletions lib/furble/Scan.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
#include <NimBLEAdvertisedDevice.h>
#include <NimBLEScan.h>

#include "Device.h"
#include "Scan.h"

// log tag
const char *LOG_TAG = FURBLE_STR;

namespace Furble {

Scan &Scan::getInstance(void) {
static Scan instance;

if (instance.m_Scan == nullptr) {
// NimBLE requires configuring server before scan
instance.m_HIDServer = HIDServer::getInstance();

instance.m_Scan = NimBLEDevice::getScan();
instance.m_Scan->setScanCallbacks(&instance);
instance.m_Scan->setActiveScan(true);
instance.m_Scan->setInterval(6553);
instance.m_Scan->setWindow(6553);
}

return instance;
}

/**
* BLE Advertisement callback.
*/
void Scan::onResult(const NimBLEAdvertisedDevice *pDevice) {
if (CameraList::match(pDevice)) {
ESP_LOGI(LOG_TAG, "RSSI(%s) = %d", pDevice->getName().c_str(), pDevice->getRSSI());
if (m_ScanResultCallback != nullptr) {
(m_ScanResultCallback)(m_ScanResultPrivateData);
}
}
};

/**
* HID server callback.
*/
void Scan::onComplete(const NimBLEAddress &address, const std::string &name) {
CameraList::add(address, name);
if (m_ScanResultCallback != nullptr) {
(m_ScanResultCallback)(m_ScanResultPrivateData);
}
};

void Scan::start(std::function<void(void *)> scanCallback, void *scanPrivateData) {
m_HIDServer->start(nullptr, this);

m_ScanResultCallback = scanCallback;
m_ScanResultPrivateData = scanPrivateData;
m_Scan->start(0, false);
}

void Scan::stop(void) {
m_HIDServer->stop();

m_Scan->stop();
m_ScanResultPrivateData = nullptr;
m_ScanResultCallback = nullptr;
}

void Scan::clear(void) {
m_Scan->clearResults();
}
} // namespace Furble
62 changes: 62 additions & 0 deletions lib/furble/Scan.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#ifndef SCAN_H
#define SCAN_H

#include <vector>

#include <NimBLEScan.h>

#include "CameraList.h"
#include "FurbleTypes.h"
#include "HIDServer.h"

#ifndef FURBLE_VERSION
#define FURBLE_VERSION "unknown"
#endif

namespace Furble {
/**
* BLE advertisement scanning class.
*
* Works in conjunction with Furble::Device class.
*/
class Scan: public HIDServerCallbacks, public NimBLEScanCallbacks {
public:
static Scan &getInstance(void);

Scan(Scan const &) = delete;
Scan(Scan &&) = delete;
Scan &operator=(Scan const &) = delete;
Scan &operator=(Scan &&) = delete;

/**
* Start the scan for BLE advertisements with a callback function when a
* matching reseult is encountered.
*/
void start(std::function<void(void *)> scanCallback, void *scanResultPrivateData);

/**
* Stop the scan.
*/
void stop(void);

/**
* Clear the scan list.
*/
void clear(void);

void onResult(const NimBLEAdvertisedDevice *pDevice) override;

void onComplete(const NimBLEAddress &address, const std::string &name) override;

private:
Scan() {};

NimBLEScan *m_Scan = nullptr;
std::function<void(void *)> m_ScanResultCallback;
void *m_ScanResultPrivateData = nullptr;
HIDServer *m_HIDServer = nullptr;
};

} // namespace Furble

#endif
2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ build_flags = -Wall -Wextra
platform = espressif32
board_build.f_cpu = 80000000L
upload_protocol = esptool
framework = arduino
framework = arduino, espidf
lib_deps =
[email protected]
[email protected]
Expand Down
Loading

0 comments on commit db551aa

Please sign in to comment.