Skip to content

Commit

Permalink
Replace Serial.print with esp_log (#125)
Browse files Browse the repository at this point in the history
* Migrate from Serial.print to ESP_LOG.

Use the ESP32 logging framework for debug messages.

* Migrate more Serial.prints.

* Clean up migration to ESP logging.

Whilst here, noticed we're leaking client connections, so migrate to
unique_ptr to handle it.
  • Loading branch information
gkoh authored Sep 1, 2024
1 parent 8065699 commit bbcd140
Show file tree
Hide file tree
Showing 16 changed files with 121 additions and 99 deletions.
4 changes: 3 additions & 1 deletion lib/M5ez/src/M5ez.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
#include <algorithm>
#include <string>

const char *M5EZ_TAG = "M5ez";

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Expand Down Expand Up @@ -482,7 +484,7 @@ uint16_t ezCanvas::loop(void *private_data) {
clean_copy.push_back(_printed[n]);
}
_printed = clean_copy;
Serial.println(ESP.getFreeHeap());
ESP_LOGD(M5EZ_TAG, "%d", ESP.getFreeHeap());
}
return 10;
}
Expand Down
2 changes: 2 additions & 0 deletions lib/M5ez/src/M5ez.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ struct line_t {
std::string line;
};

extern const char *M5EZ_TAG;

//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
Expand Down
10 changes: 9 additions & 1 deletion lib/furble/Camera.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,21 @@

namespace Furble {

Camera::Camera() {
m_Client = NimBLEDevice::createClient();
}

Camera::~Camera() {
NimBLEDevice::deleteClient(m_Client);
}

bool Camera::connect(esp_power_level_t power, progressFunc pFunc, void *pCtx) {
// try extending range by adjusting connection parameters
m_Client->updateConnParams(m_MinInterval, m_MaxInterval, m_Latency, m_Timeout);
bool connected = this->connect(pFunc, pCtx);
if (connected) {
// Set BLE transmit power after connection is established.
NimBLEDevice::setPower(power);
m_Client->updateConnParams(m_MinInterval, m_MaxInterval, m_Latency, m_Timeout);
}

return connected;
Expand Down
7 changes: 5 additions & 2 deletions lib/furble/Camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ namespace Furble {
*/
class Camera {
public:
Camera();
~Camera();

/**
* GPS data type.
*/
Expand Down Expand Up @@ -99,8 +102,8 @@ class Camera {
*/
virtual bool connect(progressFunc pFunc = nullptr, void *pCtx = nullptr) = 0;

NimBLEAddress m_Address = NimBLEAddress("");
NimBLEClient *m_Client = NimBLEDevice::createClient();
NimBLEAddress m_Address = NimBLEAddress{};
NimBLEClient *m_Client;
std::string m_Name;

void updateProgress(progressFunc pFunc, void *ctx, float value);
Expand Down
54 changes: 27 additions & 27 deletions lib/furble/CameraList.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@

namespace Furble {

std::vector<Furble::Camera *> CameraList::m_ConnectList;
std::vector<std::unique_ptr<Furble::Camera>> CameraList::m_ConnectList;
static Preferences m_Prefs;

/**
Expand Down Expand Up @@ -41,12 +41,12 @@ static std::vector<index_entry_t> load_index(void) {
if (bytes > 0 && (bytes % sizeof(index_entry_t) == 0)) {
uint8_t buffer[bytes] = {0};
size_t count = bytes / sizeof(index_entry_t);
Serial.printf("Index entries: %d\r\n", count);
ESP_LOGI(LOG_TAG, "Index entries: %d\r\n", count);
m_Prefs.getBytes(FURBLE_PREF_INDEX, buffer, bytes);
index_entry_t *entry = (index_entry_t *)buffer;

for (int i = 0; i < count; i++) {
Serial.printf("Loading index entry: %s\r\n", entry[i].name);
ESP_LOGI(LOG_TAG, "Loading index entry: %s\r\n", entry[i].name);
index.push_back(entry[i]);
}
}
Expand All @@ -57,56 +57,56 @@ static std::vector<index_entry_t> load_index(void) {
static void add_index(std::vector<index_entry_t> &index, index_entry_t &entry) {
bool exists = false;
for (size_t i = 0; i < index.size(); i++) {
Serial.printf("[%d] %s : %s\r\n", i, index[i].name, entry.name);
ESP_LOGI(LOG_TAG, "[%d] %s : %s\r\n", i, index[i].name, entry.name);
if (strcmp(index[i].name, entry.name) == 0) {
Serial.println("Overwriting existing entry");
ESP_LOGI(LOG_TAG, "Overwriting existing entry");
index[i] = entry;
exists = true;
break;
}
}

if (!exists) {
Serial.println("Adding new entry");
ESP_LOGI(LOG_TAG, "Adding new entry");
index.push_back(entry);
}
}

void CameraList::save(Camera *pCamera) {
void CameraList::save(Furble::Camera *camera) {
m_Prefs.begin(FURBLE_STR, false);
std::vector<index_entry_t> index = load_index();

index_entry_t entry = {0};
pCamera->fillSaveName(entry.name);
entry.type = pCamera->getDeviceType();
camera->fillSaveName(entry.name);
entry.type = camera->getDeviceType();

add_index(index, entry);

size_t dbytes = pCamera->getSerialisedBytes();
size_t dbytes = camera->getSerialisedBytes();
uint8_t dbuffer[dbytes] = {0};
if (pCamera->serialise(dbuffer, dbytes)) {
if (camera->serialise(dbuffer, dbytes)) {
// Store the entry and the index if serialisation succeeds
m_Prefs.putBytes(entry.name, dbuffer, dbytes);
Serial.printf("Saved %s\r\n", entry.name);
ESP_LOGI(LOG_TAG, "Saved %s\r\n", entry.name);
save_index(index);
Serial.printf("Index entries: %d\r\n", index.size());
ESP_LOGI(LOG_TAG, "Index entries: %d\r\n", index.size());
}

m_Prefs.end();
}

void CameraList::remove(Camera *pCamera) {
void CameraList::remove(Furble::Camera *camera) {
m_Prefs.begin(FURBLE_STR, false);
std::vector<index_entry_t> index = load_index();

index_entry_t entry = {0};
pCamera->fillSaveName(entry.name);
camera->fillSaveName(entry.name);

size_t i = 0;
for (i = 0; i < index.size(); i++) {
if (strcmp(index[i].name, entry.name) == 0) {
Serial.print("Deleting: ");
Serial.println(entry.name);
ESP_LOGI(LOG_TAG, "Deleting: ");
ESP_LOGI(LOG_TAG, "%s", entry.name);
break;
}
}
Expand Down Expand Up @@ -140,16 +140,16 @@ void CameraList::load(void) {

switch (index[i].type) {
case FURBLE_FUJIFILM:
m_ConnectList.push_back(new Fujifilm(dbuffer, dbytes));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new Fujifilm(dbuffer, dbytes)));
break;
case FURBLE_CANON_EOS_M6:
m_ConnectList.push_back(new CanonEOSM6(dbuffer, dbytes));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new CanonEOSM6(dbuffer, dbytes)));
break;
case FURBLE_CANON_EOS_RP:
m_ConnectList.push_back(new CanonEOSRP(dbuffer, dbytes));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new CanonEOSRP(dbuffer, dbytes)));
break;
case FURBLE_MOBILE_DEVICE:
m_ConnectList.push_back(new MobileDevice(dbuffer, dbytes));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new MobileDevice(dbuffer, dbytes)));
break;
}
}
Expand All @@ -173,30 +173,30 @@ void CameraList::clear(void) {
}

Furble::Camera *CameraList::get(size_t n) {
return m_ConnectList[n];
return m_ConnectList[n].get();
}

Furble::Camera *CameraList::back(void) {
return m_ConnectList.back();
return m_ConnectList.back().get();
}

bool CameraList::match(NimBLEAdvertisedDevice *pDevice) {
if (Fujifilm::matches(pDevice)) {
m_ConnectList.push_back(new Furble::Fujifilm(pDevice));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new Furble::Fujifilm(pDevice)));
return true;
} else if (CanonEOSM6::matches(pDevice)) {
m_ConnectList.push_back(new Furble::CanonEOSM6(pDevice));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new Furble::CanonEOSM6(pDevice)));
return true;
} else if (CanonEOSRP::matches(pDevice)) {
m_ConnectList.push_back(new Furble::CanonEOSRP(pDevice));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new Furble::CanonEOSRP(pDevice)));
return true;
}

return false;
}

void CameraList::add(NimBLEAddress address) {
m_ConnectList.push_back(new Furble::MobileDevice(address));
m_ConnectList.push_back(std::unique_ptr<Furble::Camera>(new Furble::MobileDevice(address)));
}

} // namespace Furble
10 changes: 6 additions & 4 deletions lib/furble/CameraList.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#ifndef CAMERALIST_H
#define CAMERALIST_H

#include <memory>

#include "Camera.h"

namespace Furble {
Expand All @@ -12,12 +14,12 @@ class CameraList {
/**
* Save camera to connection list.
*/
static void save(Camera *pCamera);
static void save(Furble::Camera *camera);

/**
* Remove camera from connection list.
*/
static void remove(Camera *pCamera);
static void remove(Furble::Camera *camera);

/**
* Load previously connected devices.
Expand Down Expand Up @@ -59,13 +61,13 @@ class CameraList {
/**
* Retrieve last entry.
*/
static Camera *back(void);
static Furble::Camera *back(void);

private:
/**
* List of connectable devices.
*/
static std::vector<Furble::Camera *> m_ConnectList;
static std::vector<std::unique_ptr<Furble::Camera>> m_ConnectList;
};
} // namespace Furble

Expand Down
36 changes: 18 additions & 18 deletions lib/furble/CanonEOS.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ CanonEOS::CanonEOS(const void *data, size_t len) {
CanonEOS::CanonEOS(NimBLEAdvertisedDevice *pDevice) {
m_Name = pDevice->getName();
m_Address = pDevice->getAddress();
Serial.printf("Name = %s\r\n", m_Name.c_str());
Serial.printf("Address = %s\r\n", m_Address.toString().c_str());
ESP_LOGI(LOG_TAG, "Name = %s", m_Name.c_str());
ESP_LOGI(LOG_TAG, "Address = %s", m_Address.toString().c_str());
Device::getUUID128(&m_Uuid);
}

Expand All @@ -38,7 +38,7 @@ void CanonEOS::pairCallback(BLERemoteCharacteristic *pBLERemoteCharacteristic,
size_t length,
bool isNotify) {
if (!isNotify && (length > 0)) {
Serial.printf("Got pairing callback: 0x%02x\n", pData[0]);
ESP_LOGI(LOG_TAG, "Got pairing callback: 0x%02x", pData[0]);
m_PairResult = pData[0];
}
}
Expand Down Expand Up @@ -85,70 +85,70 @@ bool CanonEOS::connect(progressFunc pFunc, void *pCtx) {
m_PairResult = 0x00;
}

Serial.println("Connecting");
ESP_LOGI(LOG_TAG, "Connecting");
if (!m_Client->connect(m_Address)) {
Serial.println("Connection failed!!!");
ESP_LOGI(LOG_TAG, "Connection failed!!!");
return false;
}

Serial.println("Connected");
ESP_LOGI(LOG_TAG, "Connected");
updateProgress(pFunc, pCtx, 10.0f);

Serial.println("Securing");
ESP_LOGI(LOG_TAG, "Securing");
if (!m_Client->secureConnection()) {
return false;
}
Serial.println("Secured!");
ESP_LOGI(LOG_TAG, "Secured!");
updateProgress(pFunc, pCtx, 20.0f);

NimBLERemoteService *pSvc = m_Client->getService(CANON_EOS_SVC_IDEN_UUID);
if (pSvc) {
NimBLERemoteCharacteristic *pChr = pSvc->getCharacteristic(CANON_EOS_CHR_NAME_UUID);
if ((pChr != nullptr) && pChr->canIndicate()) {
Serial.println("Subscribed for pairing indication");
ESP_LOGI(LOG_TAG, "Subscribed for pairing indication");
pChr->subscribe(false, std::bind(&CanonEOS::pairCallback, this, _1, _2, _3, _4));
}
}

Serial.println("Identifying 1!");
ESP_LOGI(LOG_TAG, "Identifying 1!");
const char *name = Device::getStringID();
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_NAME_UUID, 0x01,
(uint8_t *)name, strlen(name)))
return false;

updateProgress(pFunc, pCtx, 30.0f);

Serial.println("Identifying 2!");
ESP_LOGI(LOG_TAG, "Identifying 2!");
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_IDEN_UUID, 0x03, m_Uuid.uint8,
UUID128_LEN))
return false;

updateProgress(pFunc, pCtx, 40.0f);

Serial.println("Identifying 3!");
ESP_LOGI(LOG_TAG, "Identifying 3!");
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_IDEN_UUID, 0x04,
(uint8_t *)name, strlen(name)))
return false;

updateProgress(pFunc, pCtx, 50.0f);

Serial.println("Identifying 4!");
ESP_LOGI(LOG_TAG, "Identifying 4!");

uint8_t x = 0x02;
if (!write_prefix(m_Client, CANON_EOS_SVC_IDEN_UUID, CANON_EOS_CHR_IDEN_UUID, 0x05, &x, 1))
return false;

updateProgress(pFunc, pCtx, 60.0f);

Serial.println("Identifying 5!");
ESP_LOGI(LOG_TAG, "Identifying 5!");

/* write to 0xf204 */
x = 0x0a;
if (!write_value(m_Client, CANON_EOS_SVC_UNK0_UUID, CANON_EOS_CHR_UNK0_UUID, &x, 1))
return false;

// Give the user 60s to confirm/deny pairing
Serial.println("Waiting for user to confirm/deny pairing.");
ESP_LOGI(LOG_TAG, "Waiting for user to confirm/deny pairing.");
for (unsigned int i = 0; i < 60; i++) {
float progress = 70.0f + (float(i) / 6.0f);
updateProgress(pFunc, pCtx, progress);
Expand All @@ -160,7 +160,7 @@ bool CanonEOS::connect(progressFunc pFunc, void *pCtx) {

if (m_PairResult != CANON_EOS_PAIR_ACCEPT) {
bool deleted = NimBLEDevice::deleteBond(m_Address);
Serial.printf("Rejected, delete pairing: %d\n", deleted);
ESP_LOGW(LOG_TAG, "Rejected, delete pairing: %d", deleted);
return false;
}

Expand All @@ -171,14 +171,14 @@ bool CanonEOS::connect(progressFunc pFunc, void *pCtx) {

updateProgress(pFunc, pCtx, 80.0f);

Serial.println("Identifying 6!");
ESP_LOGI(LOG_TAG, "Identifying 6!");

/* write to 0xf307 */
x = 0x03;
if (!write_value(m_Client, CANON_EOS_SVC_UNK1_UUID, CANON_EOS_CHR_UNK1_UUID, &x, 1))
return false;

Serial.println("Paired!");
ESP_LOGI(LOG_TAG, "Paired!");
updateProgress(pFunc, pCtx, 100.0f);

return true;
Expand Down
Loading

0 comments on commit bbcd140

Please sign in to comment.