diff --git a/lib/ble_ota_dfu/src/ble_ota_dfu.cpp b/lib/ble_ota_dfu/src/ble_ota_dfu.cpp index df2a099..8676bf8 100644 --- a/lib/ble_ota_dfu/src/ble_ota_dfu.cpp +++ b/lib/ble_ota_dfu/src/ble_ota_dfu.cpp @@ -164,7 +164,7 @@ void BLEOverTheAirDeviceFirmwareUpdate::onNotify( uint8_t *pData = (uint8_t *)value.data(); if (pData != NULL) { - ESP_LOGD(TAG, "Notify callback for characteristic %s of data length %d", + ESP_LOGD(LOG_TAG_BLE_OTA, "Notify callback for characteristic %s of data length %d", pCharacteristic->getUUID().toString().c_str(), len); // Print transferred packets @@ -189,7 +189,7 @@ void BLEOverTheAirDeviceFirmwareUpdate::onWrite( if (pData != NULL) { // #define DEBUG_BLE_OTA_DFU_RX #ifdef DEBUG_BLE_OTA_DFU_RX - ESP_LOGD(TAG, "Write callback for characteristic %s of data length %d", + ESP_LOGD(LOG_TAG_BLE_OTA, "Write callback for characteristic %s of data length %d", pCharacteristic->getUUID().toString().c_str(), len); Serial.print("RX "); for (uint16_t index = 0; index < len; index++) { @@ -261,7 +261,7 @@ void BLEOverTheAirDeviceFirmwareUpdate::onWrite( write_len[selected_updater]); if (received_file_size > expected_file_size) { - ESP_LOGW(TAG, "Unexpected size:\n Expected: %d\nReceived: %d", + ESP_LOGW(LOG_TAG_BLE_OTA, "Unexpected size:\n Expected: %d\nReceived: %d", expected_file_size, received_file_size); } @@ -308,7 +308,7 @@ void BLEOverTheAirDeviceFirmwareUpdate::onWrite( break; default: - ESP_LOGW(TAG, "Unknown command: %02X", pData[0]); + ESP_LOGW(LOG_TAG_BLE_OTA, "Unknown command: %02X", pData[0]); break; } // Serial.printf("Not stuck in loop"); diff --git a/lib/ble_ota_dfu/src/ble_ota_dfu.hpp b/lib/ble_ota_dfu/src/ble_ota_dfu.hpp index c90d270..593eb16 100644 --- a/lib/ble_ota_dfu/src/ble_ota_dfu.hpp +++ b/lib/ble_ota_dfu/src/ble_ota_dfu.hpp @@ -27,6 +27,8 @@ const bool FASTMODE = true; #endif // #endif +static const char* LOG_TAG_BLE_OTA = "BleOtaDfu"; + const char SERVICE_OTA_BLE_UUID[] = "fe590001-54ae-4a28-9f74-dfccb248601d"; const char CHARACTERISTIC_OTA_BL_UUID_RX[] = "fe590002-54ae-4a28-9f74-dfccb248601d"; diff --git a/platformio.ini b/platformio.ini index 5c1b9ef..6a7b036 100644 --- a/platformio.ini +++ b/platformio.ini @@ -17,18 +17,20 @@ default_envs = wemos_d1_mini32 [common_env_data] lib_deps_external = adafruit/Adafruit NeoPixel @ ^1.11.0 - bakercp/Logger @ 1.0.3 h2zero/NimBLE-Arduino@1.4.1 fabianoriccardi/Melody Player @ ^2.4.0 bblanchon/ArduinoJson @ ^6.21.3 paulo-raca/Buffered Streams @ ^1.0.8 - -[ESP32] -build_flags= +build_flags = -D CONFIG_BT_NIMBLE_PINNED_TO_CORE=0 -D CONFIG_NIMBLE_STACK_USE_MEM_POOLS=1 -D CONFIG_BT_NIMBLE_ROLE_CENTRAL_DISABLED ; Disable NimBLE Client code, will save ~7kB -D CONFIG_BT_NIMBLE_ROLE_OBSERVER_DISABLED ; Disable NimBLE Client code, will save ~26kB + -D CORE_DEBUG_LEVEL=0 + +[ESP32] +build_flags= + ${common_env_data.build_flags} -D PIN_NEOPIXEL=4 ; DIN of WS28xx stripe for front- / backlight, only needed if WS28xx is used -D MOSFET_PIN_1=22 ; PWM signal for MOSFET 1 (front white, back red), only needed if COB is used -D MOSFET_PIN_2=23 ; PWM signal for MOSFET 2 (front red, back white), only needed if COB is used @@ -45,10 +47,7 @@ build_flags= [ESP32-S3] build_flags= - -D CONFIG_BT_NIMBLE_PINNED_TO_CORE=0 - -D CONFIG_NIMBLE_STACK_USE_MEM_POOLS=1 - -D CONFIG_BT_NIMBLE_ROLE_CENTRAL_DISABLED ; Disable NimBLE Client code, will save ~7kB - -D CONFIG_BT_NIMBLE_ROLE_OBSERVER_DISABLED ; Disable NimBLE Client code, will save ~26kB + ${common_env_data.build_flags} -D PIN_NEOPIXEL=3 ; DIN of WS28xx stripe for front- / backlight, only needed if WS28xx is used ; -D MOSFET_PIN_1= ; PWM signal for MOSFET 1 (front white, back red), only needed if COB is used ; -D MOSFET_PIN_2= ; PWM signal for MOSFET 2 (front red, back white), only needed if COB is used @@ -66,10 +65,7 @@ build_flags= [AVASPARK-RGB] build_flags= - -D CONFIG_BT_NIMBLE_PINNED_TO_CORE=0 - -D CONFIG_NIMBLE_STACK_USE_MEM_POOLS=1 - -D CONFIG_BT_NIMBLE_ROLE_CENTRAL_DISABLED ; Disable NimBLE Client code, will save ~7kB - -D CONFIG_BT_NIMBLE_ROLE_OBSERVER_DISABLED ; Disable NimBLE Client code, will save ~26kB + ${common_env_data.build_flags} -D PIN_NEOPIXEL_FRONT=18 ; DIN of WS28xx stripe for front light, only needed if WS28xx is used -D PIN_NEOPIXEL_BACK=17 ; DIN of WS28xx stripe for back light, only needed if WS28xx is used ;-D MOSFET_PIN_1=22 ; PWM signal for MOSFET 1 (front white, back red), only needed if COB is used diff --git a/src/AppConfiguration.cpp b/src/AppConfiguration.cpp index 624cba4..7d101ec 100644 --- a/src/AppConfiguration.cpp +++ b/src/AppConfiguration.cpp @@ -1,6 +1,8 @@ #include "AppConfiguration.h" #include "config.h" +static const char* TAG = "AppConfiguration"; + AppConfiguration* AppConfiguration::instance = nullptr; AppConfiguration* AppConfiguration::getInstance() { @@ -13,15 +15,15 @@ AppConfiguration* AppConfiguration::getInstance() { boolean AppConfiguration::readPreferences() { String json = ""; if(!preferences.begin("rESCue", true)) { - log_e("no config file found"); + ESP_LOGE(TAG, "no config file found"); } else { - log_n("found config file"); + ESP_LOGI(TAG, "found config file"); json = preferences.getString("config", ""); } StaticJsonDocument<1024> doc; deserializeJson(doc, json); preferences.end(); - log_n("readPreferences: %s", json.c_str()); + ESP_LOGI(TAG, "readPreferences: %s", json.c_str()); config.deviceName = doc["deviceName"] | "rESCue"; config.otaUpdateActive = false; config.isNotificationEnabled = doc["isNotificationEnabled"] | false; @@ -65,7 +67,6 @@ boolean AppConfiguration::readPreferences() { config.isLightBarLedTypeDifferent = doc["isLightBarLedTypeDifferent"] | false; config.idleLightTimeout = doc["idleLightTimeout"] | 60000; config.mallGrab = doc["mallGrab"] | false; - config.logLevel = doc["logLevel"] | Logger::SILENT; config.mtuSize = doc["mtuSize"] | 512; config.oddevenActive = doc["oddevenActive"] | true; config.lightsSwitch = true; @@ -110,11 +111,10 @@ boolean AppConfiguration::savePreferences() { doc["isLightBarLedTypeDifferent"] = config.isLightBarLedTypeDifferent; doc["idleLightTimeout"] = config.idleLightTimeout; doc["mallGrab"] = config.mallGrab; - doc["logLevel"] = config.logLevel; doc["mtuSize"] = config.mtuSize; String json = ""; serializeJson(doc, json); - log_n("savePreferences: %s", json.c_str()); + ESP_LOGI(TAG, "savePreferences: %s", json.c_str()); if(doc.overflowed()) { return false; diff --git a/src/AppConfiguration.h b/src/AppConfiguration.h index 89873dc..d8df526 100644 --- a/src/AppConfiguration.h +++ b/src/AppConfiguration.h @@ -4,7 +4,6 @@ #include #include #include -#include "Logger.h" #include "visit_struct.hh" #include "visit_struct_intrusive.hh" @@ -45,7 +44,6 @@ struct Config { VISITABLE(int, numberPixelLight); VISITABLE(int, numberPixelBatMon); VISITABLE(int, vescId); - VISITABLE(Logger::Level, logLevel); VISITABLE(boolean, saveConfig); VISITABLE(boolean, sendConfig); VISITABLE(String , ledType); diff --git a/src/BMSController.h b/src/BMSController.h index 968e259..621b2ea 100644 --- a/src/BMSController.h +++ b/src/BMSController.h @@ -1,5 +1,4 @@ #include -#include #include "bms_relay.h" #include "packet.h" diff --git a/src/BatteryMonitor.cpp b/src/BatteryMonitor.cpp index 6f43a0b..0f0c94c 100644 --- a/src/BatteryMonitor.cpp +++ b/src/BatteryMonitor.cpp @@ -1,7 +1,5 @@ #include "BatteryMonitor.h" -#define LOG_TAG_BATMON "BatteryMonitor" - const int numCurReadings = 50; unsigned long lastCheck = 0; @@ -15,7 +13,7 @@ BatteryMonitor::BatteryMonitor(VescData *vescData) { } void BatteryMonitor::init() { - Logger::notice(LOG_TAG_BATMON, "initializing..."); + ESP_LOGI(LOG_TAG_BATMON, "initializing..."); this->min_voltage = (int) AppConfiguration::getInstance()->config.minBatteryVoltage * 100; this->max_voltage = (int) AppConfiguration::getInstance()->config.maxBatteryVoltage * 100; this->warn_voltage = (int) AppConfiguration::getInstance()->config.lowBatteryVoltage * 100; @@ -28,9 +26,7 @@ double BatteryMonitor::readValues() { auto current = (double) abs(vescData->current); updateCurrentArray(current); - if (Logger::getLogLevel() == Logger::VERBOSE) { - Logger::verbose(LOG_TAG_BATMON, String("Voltage: " + String(voltage) + "V").c_str()); - } + ESP_LOGV(LOG_TAG_BATMON, "Voltage: %fV", voltage); return voltage; } @@ -52,8 +48,8 @@ void BatteryMonitor::checkValues() { // check if voltage is below absolute minimum or above absolute maximum (regen) if (voltage < min_voltage || voltage > max_voltage) { - Serial.printf("Voltages: %d %d, %d", min_voltage, voltage, max_voltage); - Logger::warning(LOG_TAG_BATMON, "ALARM: Battery voltage out of range"); +// Serial.printf("Voltages: %d %d, %d", min_voltage, voltage, max_voltage); + ESP_LOGW(LOG_TAG_BATMON, "ALARM: Battery voltage out of range"); Buzzer::alarm(); // play an anoying alarm tone return; } @@ -61,7 +57,7 @@ void BatteryMonitor::checkValues() { // check if the voltage is close to the minimum if (voltage < warn_voltage) { if (millis() - lastBatWarn > 5000) { - Logger::warning(LOG_TAG_BATMON, "WARN: Battery voltage out of range"); + ESP_LOGW(LOG_TAG_BATMON, "WARN: Battery voltage out of range"); Buzzer::warning(); // play a warn tonen every 5 seconds lastBatWarn = millis(); } @@ -70,7 +66,7 @@ void BatteryMonitor::checkValues() { // check if the average current is higher max if (getAverageCurrent() > max_current) { if (millis() - lastCurWarn > 5000) { - Logger::warning(LOG_TAG_BATMON, "WARN: Average current too high"); + ESP_LOGW(LOG_TAG_BATMON, "WARN: Average current too high"); Buzzer::warning(); // play a warn tonen every 5 seconds lastCurWarn = millis(); } diff --git a/src/BatteryMonitor.h b/src/BatteryMonitor.h index 5837bdc..d0cd1be 100644 --- a/src/BatteryMonitor.h +++ b/src/BatteryMonitor.h @@ -1,11 +1,12 @@ #ifndef __BATTERY_CONTROLLER_H__ #define __BATTERY_CONTROLLER_H__ -#include #include "config.h" #include "Buzzer.h" #include "CanBus.h" +static const char* LOG_TAG_BATMON = "BatteryMonitor"; + class BatteryMonitor { public: explicit BatteryMonitor(VescData *vescData); diff --git a/src/BleCanProxy.cpp b/src/BleCanProxy.cpp index a3a77a5..04819ab 100644 --- a/src/BleCanProxy.cpp +++ b/src/BleCanProxy.cpp @@ -30,10 +30,7 @@ void BleCanProxy::proxyIn(std::string in) { default: return; } - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "Proxy in, command %d, length %d\n", command, length); - Logger::verbose(LOG_TAG_BLE_CAN_PROXY, buf); - } + ESP_LOGD(LOG_TAG_BLE_CAN_PROXY, "Proxy in, command %d, length %d\n", command, length); } if (length <= 6) { @@ -126,13 +123,10 @@ void BleCanProxy::proxyIn(std::string in) { void BleCanProxy::proxyOut(uint8_t *data, unsigned int size, uint8_t crc1, uint8_t crc2) { if (size > BUFFER_SIZE) { - Logger::error(LOG_TAG_BLE_CAN_PROXY, "proxyOut - Buffer size exceeded, abort (message not sent via proxy)"); + ESP_LOGE(LOG_TAG_BLE_CAN_PROXY, "proxyOut - Buffer size exceeded, abort (message not sent via proxy)"); return; } - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "Proxy out, sending %d bytes\n", size); - Logger::verbose(LOG_TAG_BLE_CAN_PROXY, buf); - } + ESP_LOGD(LOG_TAG_BLE_CAN_PROXY, "Proxy out, sending %d bytes\n", size); //Start bit, package size if (size <= 255) { //Serial.print(0x02); diff --git a/src/BleCanProxy.h b/src/BleCanProxy.h index 2f3feb8..adec8d1 100644 --- a/src/BleCanProxy.h +++ b/src/BleCanProxy.h @@ -3,12 +3,11 @@ #include "Arduino.h" #include "config.h" -#include #include "VescCanConstants.h" #include "LoopbackStream.h" #include "CanDevice.h" -#define LOG_TAG_BLE_CAN_PROXY "BleCanProxy" +static const char* LOG_TAG_BLE_CAN_PROXY = "BleCanProxy"; class BleCanProxy { public: diff --git a/src/BleServer.cpp b/src/BleServer.cpp index dc3fa8b..b41fc23 100644 --- a/src/BleServer.cpp +++ b/src/BleServer.cpp @@ -1,5 +1,4 @@ #include "BleServer.h" -#include #include int MTU_SIZE = 128; @@ -36,9 +35,8 @@ uint32_t frameNumber = 0; // NimBLEServerCallbacks::onConnect inline void BleServer::onConnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) { - snprintf(buf, bufSize, "Client connected: %s", NimBLEAddress(desc->peer_ota_addr).toString().c_str()); - Logger::notice(LOG_TAG_BLESERVER, buf); - Logger::notice(LOG_TAG_BLESERVER, "Multi-connect support: start advertising"); + ESP_LOGI(LOG_TAG_BLESERVER, "Client connected: %s", NimBLEAddress(desc->peer_ota_addr).toString().c_str()); + ESP_LOGI(LOG_TAG_BLESERVER, "Multi-connect support: start advertising"); deviceConnected = true; NimBLEDevice::startAdvertising(); } @@ -46,7 +44,7 @@ void BleServer::onConnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) { // NimBLEServerCallbacks::onDisconnect inline void BleServer::onDisconnect(NimBLEServer *pServer) { - Logger::notice(LOG_TAG_BLESERVER, "Client disconnected - start advertising"); + ESP_LOGI(LOG_TAG_BLESERVER, "Client disconnected - start advertising"); deviceConnected = false; NimBLEDevice::startAdvertising(); } @@ -54,8 +52,7 @@ void BleServer::onDisconnect(NimBLEServer *pServer) { // NimBLEServerCallbacks::onMTUChange inline void BleServer::onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc) { - snprintf(buf, bufSize, "MTU changed - new size %d, peer %s", MTU, NimBLEAddress(desc->peer_ota_addr).toString().c_str()); - Logger::notice(LOG_TAG_BLESERVER, buf); + ESP_LOGI(LOG_TAG_BLESERVER, "MTU changed - new size %d, peer %s", MTU, NimBLEAddress(desc->peer_ota_addr).toString().c_str()); MTU_SIZE = MTU; PACKET_SIZE = MTU_SIZE - 3; } @@ -73,8 +70,7 @@ void BleServer::init(Stream *vesc) { NimBLEDevice::setMTU(mtu_size); NimBLEDevice::setPower(ESP_PWR_LVL_P9); - snprintf(buf, bufSize, "Initial MTU size %d", mtu_size); - Logger::notice(LOG_TAG_BLESERVER, buf); + ESP_LOGI(LOG_TAG_BLESERVER, "Initial MTU size %d", mtu_size); #if defined(CANBUS_ENABLED) this->canbus = canbus; #endif @@ -161,7 +157,7 @@ void BleServer::init(Stream *vesc) { // pAdvertising->setMinPreferred(0x0); // set value to 0x00 to not advertise this parameter pAdvertising->start(); - Logger::notice(LOG_TAG_BLESERVER, "waiting a client connection to notify..."); + ESP_LOGI(LOG_TAG_BLESERVER, "waiting a client connection to notify..."); } void BleServer::loop(VescData *vescData, unsigned long loopTime, unsigned long maxLoopTime) { @@ -196,7 +192,7 @@ void BleServer::loop(VescData *vescData, unsigned long loopTime, unsigned long m if (!deviceConnected && oldDeviceConnected) { delay(500); // give the bluetooth stack the chance to get things ready pServer->startAdvertising(); // restart advertising - Logger::notice(LOG_TAG_BLESERVER, "start advertising"); + ESP_LOGI(LOG_TAG_BLESERVER, "start advertising"); oldDeviceConnected = deviceConnected; } // connecting @@ -218,20 +214,19 @@ void BleServer::stop() { } void BleServer::dumpBuffer(std::string header, std::string buffer) { - if(Logger::getLogLevel() != Logger::VERBOSE) { + if(esp_log_level_get(LOG_TAG_BLESERVER) < ESP_LOG_DEBUG) { return; } int length = snprintf(tmpbuf, 50, "%s : len = %d / ", header.c_str(), buffer.length()); for (char i : buffer) { length += snprintf(tmpbuf+length, 1024-length, "%02x ", i); } - Logger::verbose(LOG_TAG_BLESERVER, tmpbuf); + ESP_LOGD(LOG_TAG_BLESERVER, "%s", tmpbuf); } //NimBLECharacteristicCallbacks::onWrite void BleServer::onWrite(BLECharacteristic *pCharacteristic) { - snprintf(buf, bufSize, "onWrite to characteristics: %s", pCharacteristic->getUUID().toString().c_str()); - Logger::verbose(LOG_TAG_BLESERVER, buf); + ESP_LOGD(LOG_TAG_BLESERVER, "onWrite to characteristics: %s", pCharacteristic->getUUID().toString().c_str()); std::string rxValue = pCharacteristic->getValue(); if (rxValue.length() > 0) { if (pCharacteristic->getUUID().equals(pCharacteristicVescRx->getUUID())) { @@ -337,15 +332,12 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) { AppConfiguration::getInstance()->config.isLightBarLedTypeDifferent = value.c_str(); } else if (key == "mallGrab") { AppConfiguration::getInstance()->config.mallGrab = value.c_str(); - } else if (key == "logLevel") { - AppConfiguration::getInstance()->config.logLevel = static_cast(strtol(value.c_str(), nullptr, 10)); } else if (key == "mtuSize") { uint16_t mtu = strtol(value.c_str(), nullptr, 10); AppConfiguration::getInstance()->config.mtuSize = mtu; if (mtu != 0 && mtu < MTU_SIZE) { NimBLEDevice::setMTU(mtu); - snprintf(buf, bufSize, "New MTU-size: %d", mtu); - Logger::warning(LOG_TAG_BLESERVER, buf); + ESP_LOGW(LOG_TAG_BLESERVER, "New MTU-size: %d", mtu); } } else if(key == "lightsSwitch") { AppConfiguration::getInstance()->config.lightsSwitch = ("true" == value); @@ -355,7 +347,7 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) { snprintf(buf, bufSize, "startUpdate"); } } - Logger::notice(LOG_TAG_BLESERVER, buf); + ESP_LOGI(LOG_TAG_BLESERVER, "%s", buf); } } delay(bleWait); // needed to give BLE stack some time @@ -363,19 +355,15 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) { //NimBLECharacteristicCallbacks::onSubscribe void BleServer::onSubscribe(NimBLECharacteristic *pCharacteristic, ble_gap_conn_desc *desc, uint16_t subValue) { - snprintf(buf, bufSize, "Client ID: %d, Address: %s, Subvalue %d, Characteristics %s ", + ESP_LOGI(LOG_TAG_BLESERVER, "Client ID: %d, Address: %s, Subvalue %d, Characteristics %s ", desc->conn_handle, NimBLEAddress(desc->peer_ota_addr).toString().c_str(), subValue, pCharacteristic->getUUID().toString().c_str()); - Logger::notice(LOG_TAG_BLESERVER, buf); } //NimBLECharacteristicCallbacks::onStatus void BleServer::onStatus(NimBLECharacteristic *pCharacteristic, Status status, int code) { - if(Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "Notification/Indication characteristics: %s, status code: %d, return code: %d", + ESP_LOGD(LOG_TAG_BLESERVER, "Notification/Indication characteristics: %s, status code: %d, return code: %d", pCharacteristic->getUUID().toString().c_str(), status, code); - Logger::verbose(LOG_TAG_BLESERVER, buf); - } } void BleServer::updateRescueApp(long count, long loopTime, long maxLoopTime) { @@ -414,7 +402,6 @@ double BleServer::parseDouble(const std::string& strValue) { struct BleServer::sendConfigValue { NimBLECharacteristic *pCharacteristic; std::stringstream ss; - char localbuf[128]; explicit sendConfigValue(NimBLECharacteristic *pCharacteristic) { this->pCharacteristic = pCharacteristic; @@ -427,8 +414,7 @@ struct BleServer::sendConfigValue { } else { ss << name << "=" << value; } - snprintf(localbuf, 128, "Sending %s", ss.str().c_str()); - Logger::notice(LOG_TAG_BLESERVER, localbuf); + ESP_LOGI(LOG_TAG_BLESERVER, "Sending %s", ss.str().c_str()); pCharacteristic->setValue(ss.str()); pCharacteristic->indicate(); diff --git a/src/BleServer.h b/src/BleServer.h index bf757eb..d7cbe3b 100644 --- a/src/BleServer.h +++ b/src/BleServer.h @@ -9,7 +9,7 @@ #include #include "base64.h" -#define LOG_TAG_BLESERVER "BleServer" +static const char* LOG_TAG_BLESERVER = "BleServer"; #define VESC_SERVICE_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E" #define VESC_CHARACTERISTIC_UUID_RX "6E400002-B5A3-F393-E0A9-E50E24DCCA9E" diff --git a/src/Buzzer.cpp b/src/Buzzer.cpp index de37efa..3cafe59 100644 --- a/src/Buzzer.cpp +++ b/src/Buzzer.cpp @@ -19,12 +19,12 @@ void Buzzer::playSound(RTTTL_MELODIES selection){ return; } if(player.isPlaying()) { - Logger::notice(LOG_TAG_BUZZER, "Still playing melody, abort!"); + ESP_LOGI(LOG_TAG_BUZZER, "Still playing melody, abort!"); return; } auto it = RTTTL_MELODIES_VALUES.find(selection); if (it == RTTTL_MELODIES_VALUES.end()) { - Logger::error(LOG_TAG_BUZZER, "Melody not found"); + ESP_LOGE(LOG_TAG_BUZZER, "Melody not found"); return; } Melody sound = MelodyFactory.loadRtttlString(it->second); diff --git a/src/Buzzer.h b/src/Buzzer.h index 022efba..ae7346a 100644 --- a/src/Buzzer.h +++ b/src/Buzzer.h @@ -2,7 +2,6 @@ #define __BUZZER_H__ #include -#include #include #include #include "RTTTL.h" diff --git a/src/CanBus.cpp b/src/CanBus.cpp index 178a2aa..3b6f2a4 100644 --- a/src/CanBus.cpp +++ b/src/CanBus.cpp @@ -58,7 +58,7 @@ void CanBus::loop() { initRetryCounter--; lastRetry = millis(); if(initRetryCounter == 0) { - Logger::error("CANBUS initialization failed"); + ESP_LOGE(LOG_TAG_CANBUS, "CANBUS initialization failed"); initRetryCounter = 1; lastRetry = (millis() + 5000); } @@ -70,13 +70,13 @@ void CanBus::loop() { this->vescData->connected = true; } if (!initialized) { - Logger::notice(LOG_TAG_CANBUS, "CANBUS is now initialized"); + ESP_LOGI(LOG_TAG_CANBUS, "CANBUS is now initialized"); initialized = true; } frameCount++; //VESC only uses ext packages, so skip std packages if (rx_frame.extd) { - if (Logger::getLogLevel() == Logger::VERBOSE) { + if (esp_log_level_get(LOG_TAG_CANBUS) > ESP_LOG_DEBUG) { printFrame(rx_frame, frameCount); } processFrame(rx_frame, frameCount); @@ -84,12 +84,12 @@ void CanBus::loop() { clearFrame(rx_frame); if (frameCount > 1000) { // WORKAROUND if messages arrive too fast - Logger::error(LOG_TAG_CANBUS, "reached 1000 frames in one loop, abort"); + ESP_LOGE(LOG_TAG_CANBUS, "reached 1000 frames in one loop, abort"); buffer.clear(); return; } } - if (Logger::getLogLevel() == Logger::VERBOSE) { + if (esp_log_level_get(LOG_TAG_CANBUS) > ESP_LOG_DEBUG) { dumpVescValues(); } } @@ -335,7 +335,7 @@ boolean CanBus::bmsState(bms_op_state op_state, bms_fault_state fault_state) { } boolean CanBus::requestFirmwareVersion() { - Logger::notice(LOG_TAG_CANBUS, "requestFirmwareVersion"); + ESP_LOGI(LOG_TAG_CANBUS, "requestFirmwareVersion"); twai_message_t tx_frame = {}; tx_frame.extd = 1; @@ -348,7 +348,7 @@ boolean CanBus::requestFirmwareVersion() { } boolean CanBus::requestRealtimeData() { - Logger::notice(LOG_TAG_CANBUS, "requestRealtimeData"); + ESP_LOGI(LOG_TAG_CANBUS, "requestRealtimeData"); twai_message_t tx_frame = {}; tx_frame.extd = 1; @@ -366,7 +366,7 @@ boolean CanBus::requestRealtimeData() { } boolean CanBus::requestBalanceData() { - Logger::notice(LOG_TAG_CANBUS, "requestBalanceData"); + ESP_LOGI(LOG_TAG_CANBUS, "requestBalanceData"); twai_message_t tx_frame = {}; tx_frame.extd = 1; @@ -379,7 +379,7 @@ boolean CanBus::requestBalanceData() { } boolean CanBus::requestFloatPackageData() { - Logger::notice(LOG_TAG_CANBUS, "requestFloatPackageData"); + ESP_LOGI(LOG_TAG_CANBUS, "requestFloatPackageData"); twai_message_t tx_frame = {}; tx_frame.extd = 1; @@ -692,14 +692,13 @@ void CanBus::processFrame(twai_message_t rx_frame, int frameCount) { } } - if (Logger::getLogLevel() <= Logger::NOTICE) { - snprintf(buf, bufSize, "processed frame #%d, type %s", frameCount, frametype.c_str()); - Logger::verbose(LOG_TAG_CANBUS, buf); + if (esp_log_level_get(LOG_TAG_CANBUS) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_CANBUS, "processed frame #%d, type %s", frameCount, frametype.c_str()); } } void CanBus::dumpVescValues() { - if (Logger::getLogLevel() != Logger::VERBOSE || millis() - lastDump < 1000) { + if (esp_log_level_get(LOG_TAG_CANBUS) < ESP_LOG_DEBUG || millis() - lastDump < 1000) { return; } std::string bufferString; @@ -772,7 +771,7 @@ void CanBus::dumpVescValues() { bufferString += ", fault="; snprintf(buf, bufSize, "%d", vescData->fault); bufferString += buf; - Logger::verbose(LOG_TAG_CANBUS, bufferString.c_str()); + ESP_LOGD(LOG_TAG_CANBUS, "%s", bufferString.c_str()); lastDump = millis(); } diff --git a/src/CanBus.h b/src/CanBus.h index ccf8483..1ca6270 100644 --- a/src/CanBus.h +++ b/src/CanBus.h @@ -6,7 +6,6 @@ #include "AppConfiguration.h" #include -#include #include "VescCanConstants.h" #include "BleCanProxy.h" #include "CanDevice.h" @@ -16,7 +15,7 @@ #define B10000001 129 #define B11000011 195 -#define LOG_TAG_CANBUS "CanBus" +static const char* LOG_TAG_CANBUS = "CanBus"; class CanBus { public: diff --git a/src/CanDevice.cpp b/src/CanDevice.cpp index 99b4be9..d1fc73d 100644 --- a/src/CanDevice.cpp +++ b/src/CanDevice.cpp @@ -9,25 +9,25 @@ boolean CanDevice::init() { // Install TWAI driver if (twai_driver_install(&g_config, &t_config, &f_config) == ESP_OK) { - printf("Driver installed\n"); + ESP_LOGI(LOG_TAG_CANDEVICE, "Driver installed\n"); } else { - printf("Failed to install driver\n"); + ESP_LOGE(LOG_TAG_CANDEVICE, "Failed to install driver\n"); return false; } // Start TWAI driver if (twai_start() == ESP_OK) { - printf("Driver started\n"); + ESP_LOGI(LOG_TAG_CANDEVICE, "Driver started\n"); } else { - printf("Failed to start driver\n"); + ESP_LOGE(LOG_TAG_CANDEVICE, "Failed to start driver\n"); return false; } return true; } boolean CanDevice::sendCanFrame(const twai_message_t *p_frame) { - if (Logger::getLogLevel() == Logger::VERBOSE) { + if (esp_log_level_get(LOG_TAG_CANDEVICE) >= ESP_LOG_DEBUG) { char buf[128]; - snprintf(buf, 128, "Sending CAN frame %" PRIu32 " DLC %d, [%d, %d, %d, %d, %d, %d, %d, %d]", + ESP_LOGD(LOG_TAG_CANDEVICE, "Sending CAN frame %" PRIu32 " DLC %d, [%d, %d, %d, %d, %d, %d, %d, %d]", p_frame->identifier, p_frame->data_length_code, p_frame->data[0], @@ -38,12 +38,11 @@ boolean CanDevice::sendCanFrame(const twai_message_t *p_frame) { p_frame->data[5], p_frame->data[6], p_frame->data[7]); - Logger::verbose(LOG_TAG_CANDEVICE, buf); } xSemaphoreTake(mutex_v, portMAX_DELAY); //Queue message for transmission if (twai_transmit(p_frame, pdMS_TO_TICKS(10)) != ESP_OK) { - printf("Failed to queue message for transmission\n"); + ESP_LOGE(LOG_TAG_CANDEVICE, "Failed to queue message for transmission\n"); xSemaphoreGive(mutex_v); return false; } diff --git a/src/CanDevice.h b/src/CanDevice.h index 512d15d..ace07e4 100644 --- a/src/CanDevice.h +++ b/src/CanDevice.h @@ -3,10 +3,9 @@ #include "Arduino.h" #include "config.h" -#include #include "driver/twai.h" -#define LOG_TAG_CANDEVICE "CanDevice" +static const char* LOG_TAG_CANDEVICE = "CanDevice"; #ifndef CAN_TX_PIN #define CAN_TX_PIN 26 diff --git a/src/CobController.cpp b/src/CobController.cpp index c58aba5..d390e0d 100644 --- a/src/CobController.cpp +++ b/src/CobController.cpp @@ -1,11 +1,10 @@ #include -#include #include "CobController.h" CobController::CobController() = default; void CobController::init() { - Logger::notice(LOG_TAG_COB, "initializing ..."); + ESP_LOGI(LOG_TAG_COB, "initializing ..."); ledcAttachPin(MOSFET_PIN_1, 0); // assign a led pins to a channel #ifdef DUAL_MOSFET ledcAttachPin(MOSFET_PIN_2, 1); // assign a led pins to a channel @@ -20,11 +19,7 @@ void CobController::init() { } void CobController::changePattern(Pattern pattern, boolean isForward, boolean repeatPattern) { - //if (Logger::getLogLevel() == Logger::VERBOSE) { - char buf[128]; - snprintf(buf, 128, "changePattern new pattern %d, forward %d, repeat %d", pattern, isForward, repeatPattern); - Logger::error(LOG_TAG_COB, buf); - ///} + ESP_LOGD(LOG_TAG_COB, "changePattern new pattern %d, forward %d, repeat %d", pattern, isForward, repeatPattern); if (activePattern == pattern && isForward == (direction == Direction::FORWARD)) { return; @@ -151,7 +146,7 @@ void CobController::stop() { } void CobController::startSequence() { - Logger::notice(LOG_TAG_COB, "run startSequence"); + ESP_LOGI(LOG_TAG_COB, "run startSequence"); //changePattern(RESCUE_FLASH_LIGHT, true, true); } diff --git a/src/CobController.h b/src/CobController.h index 3b5a833..98ff414 100644 --- a/src/CobController.h +++ b/src/CobController.h @@ -12,7 +12,7 @@ #define MOSFET_PIN_2 23 #endif //MOSFET_PIN_2 -#define LOG_TAG_COB "CobController" +static const char* LOG_TAG_COB = "CobController"; class CobController : public ILedController { public: diff --git a/src/ILedController.cpp b/src/ILedController.cpp index 06abdd5..54639b2 100644 --- a/src/ILedController.cpp +++ b/src/ILedController.cpp @@ -1,6 +1,5 @@ #include "ILedController.h" #include "AppConfiguration.h" -#include unsigned long idleTimer = 0; @@ -17,10 +16,9 @@ void ILedController::loop(const int *new_forward, const int *new_backward, const // is there a change detected if (old_forward != *(new_forward) || old_backward != *(new_backward)) { - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "change detected: forward is %d was %d, backward is %d was %d", + if (esp_log_level_get(LOG_TAG_LED) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_LED, "change detected: forward is %d was %d, backward is %d was %d", *(new_forward), old_forward, *(new_backward), old_backward); - Logger::verbose(LOG_TAG_LED, buf); } this->changePattern(Pattern::FADE, (*new_forward) == HIGH, false); @@ -50,7 +48,7 @@ void ILedController::loop(const int *new_forward, const int *new_backward, const if (idleTimer != 0 && AppConfiguration::getInstance()->config.idleLightTimeout > 0 && millis() - idleTimer > AppConfiguration::getInstance()->config.idleLightTimeout) { - Logger::notice(LOG_TAG_LED, "Turn off idle light"); + ESP_LOGI(LOG_TAG_LED, "Turn off idle light"); this->changePattern(NONE, true, false); idleTimer = 0; } diff --git a/src/ILedController.h b/src/ILedController.h index c8e1eea..6175640 100644 --- a/src/ILedController.h +++ b/src/ILedController.h @@ -5,7 +5,7 @@ #include "CanBus.h" #include "VescData.h" -#define LOG_TAG_LED "ILedController" +static const char* LOG_TAG_LED = "ILedController"; // Pattern types supported: enum Pattern { NONE, RAINBOW_CYCLE, THEATER_CHASE, COLOR_WIPE, CYLON, FADE, RESCUE_FLASH_LIGHT, PULSE, SLIDE, BATTERY_INDICATOR, TRANS_PRIDE}; diff --git a/src/LightBarController.cpp b/src/LightBarController.cpp index 9cda2df..3155d50 100644 --- a/src/LightBarController.cpp +++ b/src/LightBarController.cpp @@ -59,12 +59,10 @@ void LightBarController::updateLightBar(double voltage, uint16_t switchstate, do int whole = count; // number of "full" green pixels int remainder = (count - whole) * 100; // percentage of usage of current pixel - if (Logger::getLogLevel() == Logger::VERBOSE) { - Logger::verbose(LOG_TAG_LIGHTBAR, String("used=" + String(used) + ", value=" + String(value)).c_str()); - Logger::verbose(LOG_TAG_LIGHTBAR, - String("count=" + String(count) + ", diffPerPixel=" + String(diffPerPixel)).c_str()); - Logger::verbose(LOG_TAG_LIGHTBAR, - String("whole=" + String(whole) + ", remainder=" + String(remainder)).c_str()); + if (esp_log_level_get(LOG_TAG_LIGHTBAR) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_LIGHTBAR, "used=%d, value=%d", used, value); + ESP_LOGD(LOG_TAG_LIGHTBAR, "count=%f, diffPerPixel=%f", count, diffPerPixel); + ESP_LOGD(LOG_TAG_LIGHTBAR, "whole=%d, remainder=%d", whole, remainder); } if (adcState != lastAdcState) { @@ -143,7 +141,7 @@ AdcState LightBarController::mapSwitchState(uint16_t intState, boolean isAdc1Ena case 2: return AdcState::ADC_FULL; default: - Logger::error(LOG_TAG_LIGHTBAR, "Unknown switch state"); + ESP_LOGE(LOG_TAG_LIGHTBAR, "Unknown switch state"); } return AdcState::ADC_NONE; } \ No newline at end of file diff --git a/src/LightBarController.h b/src/LightBarController.h index 19ecb5b..371d445 100644 --- a/src/LightBarController.h +++ b/src/LightBarController.h @@ -2,12 +2,11 @@ #define __LIGHTBAR_CONTROLLER_H__ #include -#include #include "config.h" #include "AppConfiguration.h" #include "ILedController.h" -#define LOG_TAG_LIGHTBAR "LightBar" +static const char* LOG_TAG_LIGHTBAR = "LightBar"; #include diff --git a/src/Ws28xxController.cpp b/src/Ws28xxController.cpp index bb71518..abe5407 100644 --- a/src/Ws28xxController.cpp +++ b/src/Ws28xxController.cpp @@ -1,5 +1,4 @@ #include "Ws28xxController.h" -#include //stuff for using seperate front and back pins. Ws28xxController::Ws28xxController(uint16_t pixels, uint8_t frontPin, uint8_t backPin, uint8_t type, VescData *vescData) @@ -104,9 +103,8 @@ void Ws28xxController::increment() { // Reverse pattern direction void Ws28xxController::reverse() { - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "reversing pattern %d, direction %d", activePattern, direction); - Logger::warning(LOG_TAG_WS28XX, buf); + if (esp_log_level_get(LOG_TAG_WS28XX) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_WS28XX, "reversing pattern %d, direction %d", activePattern, direction); } if (direction == FORWARD) { direction = REVERSE; @@ -118,10 +116,9 @@ void Ws28xxController::reverse() { } void Ws28xxController::onComplete() { - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "onComplete pattern %d, startSequence %d, reverseonComplete %d, repeat %d", + if (esp_log_level_get(LOG_TAG_WS28XX) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_WS28XX, "onComplete pattern %d, startSequence %d, reverseonComplete %d, repeat %d", activePattern, isStartSequence, reverseOnComplete, repeat); - Logger::verbose(LOG_TAG_WS28XX, buf); } stopPattern = true; blockChange = false; @@ -150,9 +147,8 @@ void Ws28xxController::changePattern(Pattern pattern, boolean isForward, boolean return; } - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "changePattern new pattern %d, forward %d, repeat %d", pattern, isForward, repeatPattern); - Logger::verbose(LOG_TAG_WS28XX, buf); + if (esp_log_level_get(LOG_TAG_WS28XX) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_WS28XX, "changePattern new pattern %d, forward %d, repeat %d", pattern, isForward, repeatPattern); } maxBrightness = config.lightMaxBrightness; @@ -265,9 +261,8 @@ void Ws28xxController::flashLight(uint8_t timeinterval, Direction dir) { totalSteps = 10; index = 0; direction = dir; - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "flash %s", direction == FORWARD ? "forward" : "backward"); - Logger::verbose(LOG_TAG_WS28XX, buf); + if (esp_log_level_get(LOG_TAG_WS28XX) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_WS28XX, "flash %s", direction == FORWARD ? "forward" : "backward"); } } @@ -325,9 +320,8 @@ void Ws28xxController::fadeLight(uint8_t timeinterval, Direction dir) { totalSteps = maxBrightness; direction = dir; index = dir == Direction::FORWARD ? 0 : totalSteps - 1; - if (Logger::getLogLevel() == Logger::VERBOSE) { - snprintf(buf, bufSize, "fade %s", direction == FORWARD ? "forward" : "backward"); - Logger::verbose(LOG_TAG_WS28XX, buf); + if (esp_log_level_get(LOG_TAG_WS28XX) >= ESP_LOG_DEBUG) { + ESP_LOGD(LOG_TAG_WS28XX, "fade %s", direction == FORWARD ? "forward" : "backward"); } } @@ -479,14 +473,14 @@ uint32_t Ws28xxController::wheel(byte wheelPos) { } void Ws28xxController::init() { - Logger::notice(LOG_TAG_WS28XX, "initializing ..."); + ESP_LOGI(LOG_TAG_WS28XX, "initializing ..."); begin(); // This initializes the NeoPixel library. maxBrightness = config.lightMaxBrightness; show(); } void Ws28xxController::stop() { - Logger::verbose("stop"); + ESP_LOGD(LOG_TAG_WS28XX, "stop"); activePattern = NONE; for (int i = 0; i < numPixels(); i++) { setPixelColor(i, Color(0, 0, 0, 0)); @@ -561,7 +555,7 @@ void Ws28xxController::idleSequence() { } void Ws28xxController::startSequence() { - Logger::notice(LOG_TAG_WS28XX, "run startSequence"); + ESP_LOGI(LOG_TAG_WS28XX, "run startSequence"); blockChange = true; isStartSequence = true; int timeinterval = 0; diff --git a/src/Ws28xxController.h b/src/Ws28xxController.h index ff4e4c0..c60f056 100644 --- a/src/Ws28xxController.h +++ b/src/Ws28xxController.h @@ -11,7 +11,7 @@ #define PIN_NEOPIXEL 5 #endif //PIN_NEOPIXEL -#define LOG_TAG_WS28XX "Ws28xxController" +static const char* LOG_TAG_WS28XX = "Ws28xxController"; class Ws28xxController : public ILedController, Adafruit_NeoPixel { public: diff --git a/src/main.cpp b/src/main.cpp index 009d366..a5195ea 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,5 @@ #include -#include +#include "esp_log.h" #include "config.h" #include "BatteryMonitor.h" #include "Buzzer.h" @@ -47,8 +47,6 @@ BatteryMonitor *batMonitor = new BatteryMonitor(&vescData); BleServer *bleServer = new BleServer(); LightBarController *lightbar = new LightBarController(); -// Declare the local logger function before it is called. -void localLogger(Logger::Level level, const char *module, const char *message); #if defined(CANBUS_ENABLED) && defined(BMS_TX_PIN) && defined(BMS_ON_PIN) BMSController *bmsController = new BMSController(&vescData); @@ -62,14 +60,12 @@ void setup() { digitalWrite(PIN_BOARD_LED,LOW); #endif - Logger::setOutputFunction(localLogger); - AppConfiguration::getInstance()->readPreferences(); // AppConfiguration::getInstance()->readMelodies(); delay(10); AppConfiguration::getInstance()->config.sendConfig = false; - Logger::setLogLevel(AppConfiguration::getInstance()->config.logLevel); - if (Logger::getLogLevel() != Logger::SILENT) { + + if (esp_log_level_get("*") != ESP_LOG_NONE) { #ifdef ESP32S3 Serial.setRxBufferSize(2048); #endif @@ -116,7 +112,7 @@ void setup() { snprintf(mainBuf, mainBufSize, " sw-version %d.%d.%d is happily running on hw-version %d.%d", SOFTWARE_VERSION_MAJOR, SOFTWARE_VERSION_MINOR, SOFTWARE_VERSION_PATCH, HARDWARE_VERSION_MAJOR, HARDWARE_VERSION_MINOR); - Logger::notice("rESCue", mainBuf); + ESP_LOGI("rESCue", "%s", mainBuf); #ifdef PIN_BOARD_LED digitalWrite(PIN_BOARD_LED,HIGH); @@ -182,17 +178,4 @@ void loop() { // call the VESC UART-to-Bluetooth bridge bleServer->loop(&vescData, loopTime, maxLoopTime); -} - -void localLogger(Logger::Level level, const char *module, const char *message) { - log_printf(F("[")); - log_printf(Logger::asString(level)); - log_printf(F("] ")); - if (strlen(module) > 0) { - log_printf(F(": ")); - log_printf(module); - log_printf(" "); - } - log_printf(message); - log_printf("\n"); } \ No newline at end of file