Skip to content

Commit

Permalink
Merge pull request #39 from thankthemaker/feature/version_2_2_1
Browse files Browse the repository at this point in the history
Version 2.2.1
  • Loading branch information
thankthemaker authored May 22, 2022
2 parents 051145a + a4f7a26 commit c422ae6
Show file tree
Hide file tree
Showing 17 changed files with 218 additions and 103 deletions.
7 changes: 6 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
.pio
.idea
.vscode/.browse.c_cpp.db*
.vscode/c_cpp_properties.json
.vscode/launch.json
.vscode/ipch
.DS_Store
extensions.json
extensions.json
/cmake-build-debug/**
/cmake-build-**
/CMakeLists.txt
/CMakeListsPrivate.txt
3 changes: 1 addition & 2 deletions lib/XT_DAC_Audio/XT_DAC_Audio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,7 @@
#include "XT_DAC_Audio.h"
#include "HardwareSerial.h"
#include "soc/sens_reg.h" // For dacWrite() patch, TEB Sep-16-2019


#include "soc/rtc_io_reg.h"

/* Every variable that is used in the mainline code and in the onTImer interrupt code
* must be declared volatile.
Expand Down
4 changes: 2 additions & 2 deletions platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ default_envs = wemos_d1_mini32

[common_env_data]
lib_deps_external =
adafruit/Adafruit NeoPixel @ ^1.7.0
adafruit/Adafruit NeoPixel @ ^1.10.4
bakercp/Logger @ 1.0.3
h2zero/NimBLE-Arduino@1.2.0
h2zero/NimBLE-Arduino@1.3.6
fabianoriccardi/Melody Player @ ^2.2.2
bblanchon/ArduinoJson @ ^6.17.3
paulo-raca/Buffered Streams @ ^1.0.6
Expand Down
2 changes: 2 additions & 0 deletions src/AppConfiguration.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ void AppConfiguration::readPreferences() {
config.ledFrequency = doc["ledFrequency"] | "KHZ800";
config.idleLightTimeout = doc["idleLightTimeout"] | 60000;
config.logLevel = doc["logLevel"] | Logger::WARNING;
config.mtuSize = doc["mtuSize"] | 512;
config.saveConfig = false;
config.sendConfig = false;
preferences.end();
Expand Down Expand Up @@ -95,6 +96,7 @@ void AppConfiguration::savePreferences() {
doc["ledFrequency"] = config.ledFrequency;
doc["idleLightTimeout"] = config.idleLightTimeout;
doc["logLevel"] = config.logLevel;
doc["mtuSize"] = config.mtuSize;
String json = "";
serializeJson(doc, json);
log_n("savePreferences: %s", json.c_str());
Expand Down
1 change: 1 addition & 0 deletions src/AppConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ struct Config {
VISITABLE(String , ledType);
VISITABLE(String , ledFrequency);
VISITABLE(int , idleLightTimeout);
VISITABLE(int , mtuSize);
END_VISITABLES;
};

Expand Down
188 changes: 104 additions & 84 deletions src/BleServer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ void startUpdate() {
Serial.println("Update started");
}

void handleUpdate(const std::string& data) {
void handleUpdate(const std::string &data) {
/*
Serial.printf("\nhandleUpdate incoming data (size %d):\n", data.length());
for(int i=0; i<data.length();i++) {
Expand Down Expand Up @@ -79,22 +79,22 @@ void activateWiFiAp(const char *password) {
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/ping", HTTP_GET, [](AsyncWebServerRequest *request){
server.on("/ping", HTTP_GET, [](AsyncWebServerRequest *request) {
AsyncWebServerResponse *response = request->beginResponse(200, "text/plain", "alive");
response->addHeader("Access-Control-Allow-Origin", "*");
request->send(response);
});
server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request){
server.on("/update", HTTP_POST, [](AsyncWebServerRequest *request) {
int params = request->params();
for(int i=0;i<params;i++){
AsyncWebParameter* p = request->getParam(i);
if(p->isPost() && p->name().compareTo("data") != -1) {
const char *value =p->value().c_str();
for (int i = 0; i < params; i++) {
AsyncWebParameter *p = request->getParam(i);
if (p->isPost() && p->name().compareTo("data") != -1) {
const char *value = p->value().c_str();
//Serial.printf("\nPOST[%s]: bytes %d\n", p->name().c_str(), p->value().length());
if(!updateFlag) {
if (!updateFlag) {
startUpdate();
}
if(p->value().length() > 0) {
if (p->value().length() > 0) {
handleUpdate(base64_decode(value, false));
}
}
Expand All @@ -111,17 +111,9 @@ void activateWiFiAp(const char *password) {
inline
void BleServer::onConnect(NimBLEServer *pServer, ble_gap_conn_desc *desc) {
char buf[128];
snprintf(buf, 128, "Client connected: %s", NimBLEAddress(desc->peer_ota_addr).toString().c_str());
snprintf(buf, 128, "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");
uint16_t mtu = pServer->getPeerMTU(NimBLEAddress(desc->peer_ota_addr));
if(mtu != 0 && mtu < MTU_SIZE) {
MTU_SIZE = mtu;
BLE_PACKET_SIZE = MTU_SIZE - 3;
NimBLEDevice::setMTU(mtu);
snprintf(buf, 128, "New MTU-size: %d", mtu);
Logger::warning(LOG_TAG_BLESERVER, buf);
}
deviceConnected = true;
NimBLEDevice::startAdvertising();
};
Expand All @@ -134,13 +126,26 @@ void BleServer::onDisconnect(NimBLEServer *pServer) {
NimBLEDevice::startAdvertising();
}

// NimBLEServerCallbacks::onMTUChange
inline
void BleServer::onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc) {
char buf[128];
snprintf(buf, 128, "MTU changed - new size %d, peer %s", MTU, NimBLEAddress(desc->peer_ota_addr).toString().c_str());
Logger::notice(LOG_TAG_BLESERVER, buf);
MTU_SIZE = MTU;
BLE_PACKET_SIZE = MTU_SIZE - 3;
}

void BleServer::init(Stream *vesc, CanBus *canbus) {
vescSerial = vesc;

// Create the BLE Device
NimBLEDevice::init(AppConfiguration::getInstance()->config.deviceName.c_str());
NimBLEDevice::setMTU(MTU_SIZE);

int mtu_size = AppConfiguration::getInstance()->config.mtuSize;
NimBLEDevice::setMTU(mtu_size);
char buf[128];
snprintf(buf, 128, "Initial MTU size %d", mtu_size);
Logger::notice(LOG_TAG_BLESERVER, buf);
this->canbus = canbus;

// Create the BLE Server
Expand Down Expand Up @@ -229,17 +234,15 @@ void BleServer::loop(CanBus::VescData *vescData, long loopTime, long maxLoopTime
oneByte = vescSerial->read();
bufferString.push_back(oneByte);
}
//if (Logger::getLogLevel() == Logger::VERBOSE) {
Logger::notice(LOG_TAG_BLESERVER, "BLE from VESC: ");
Logger::notice(LOG_TAG_BLESERVER, bufferString.c_str());
// }

if (deviceConnected) {
while (bufferString.length() > 0) {
if (bufferString.length() > BLE_PACKET_SIZE) {
dumpBuffer("VESC => BLE/UART", bufferString.substr(0, BLE_PACKET_SIZE));
pCharacteristicVescTx->setValue(bufferString.substr(0, BLE_PACKET_SIZE));
bufferString = bufferString.substr(BLE_PACKET_SIZE);
} else {
dumpBuffer("VESC => BLE/UART", bufferString);
pCharacteristicVescTx->setValue(bufferString);
bufferString.clear();
}
Expand Down Expand Up @@ -270,23 +273,28 @@ void BleServer::loop(CanBus::VescData *vescData, long loopTime, long maxLoopTime
#endif //CANBUS_ENABLED
}

void BleServer::dumpBuffer(std::string header, std::string buffer) {
if(!Logger::getLogLevel() == Logger::VERBOSE) {
return;
}
char tmpbuf[1024];
int length = snprintf(tmpbuf, 50, "%s : len = %d / ", header.c_str(), buffer.length());
for (int i = 0; i < buffer.size(); i++) {
length += snprintf(tmpbuf+length, 1024-length, "%02x ", buffer.at(i));
}
Logger::verbose(LOG_TAG_BLESERVER, tmpbuf);
}

//NimBLECharacteristicCallbacks::onWrite
void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
char buffer[128];
snprintf(buffer, 128, "onWrite to characteristics: %s", pCharacteristic->getUUID().toString().c_str());
Logger::notice(LOG_TAG_BLESERVER, buffer);
std::string rxValue = pCharacteristic->getValue();
snprintf(buffer, 128, "onWrite - value: %s\n", rxValue.data());
Logger::notice(LOG_TAG_BLESERVER, buffer);
if (rxValue.length() > 0) {
if (pCharacteristic->getUUID().equals(pCharacteristicVescRx->getUUID())) {
/*
for (int i = 0; i < rxValue.length(); i++) {
Serial.print(rxValue[i], DEC);
Serial.print(" ");
}
Serial.println();
*/
dumpBuffer("BLE/UART => VESC: ", rxValue);

#ifdef CANBUS_ONLY
canbus->proxy->proxyIn(rxValue);
Expand Down Expand Up @@ -314,7 +322,7 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
AppConfiguration::getInstance()->config.otaUpdateActive = false;
AppConfiguration::getInstance()->config.saveConfig = true;
delay(100);
} else if (key == "deviceName") {
} else if (key == "deviceName") {
AppConfiguration::getInstance()->config.deviceName = value.c_str();
} else if (key == "isNotificationEnabled") {
AppConfiguration::getInstance()->config.isNotificationEnabled = value.c_str();
Expand Down Expand Up @@ -379,11 +387,19 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
AppConfiguration::getInstance()->config.ledFrequency = value.c_str();
} else if (key == "logLevel") {
AppConfiguration::getInstance()->config.logLevel = static_cast<Logger::Level>(atoi(value.c_str()));
} else if(key == "wifiActive") {
if(value.compare("true") != -1) {
} else if (key == "mtuSize") {
uint16_t mtu = atoi(value.c_str());
AppConfiguration::getInstance()->config.mtuSize = mtu;
if (mtu != 0 && mtu < MTU_SIZE) {
NimBLEDevice::setMTU(mtu);
snprintf(buf, 128, "New MTU-size: %d", mtu);
Logger::warning(LOG_TAG_BLESERVER, buf);
}
} else if (key == "wifiActive") {
if (value.compare("true") != -1) {
activateWiFiAp(wifiPassword);
}
} else if(key == "wifiPassword") {
} else if (key == "wifiPassword") {
wifiPassword = value.c_str();
}
Logger::notice(LOG_TAG_BLESERVER, buf);
Expand All @@ -405,27 +421,30 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
}
}
}

//NimBLECharacteristicCallbacks::onSubscribe
void BleServer::onSubscribe(NimBLECharacteristic *pCharacteristic, ble_gap_conn_desc *desc, uint16_t subValue) {
char buf[256];
snprintf(buf, 256, "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);
}
void BleServer::onSubscribe(NimBLECharacteristic *pCharacteristic, ble_gap_conn_desc *desc, uint16_t subValue) {
char buf[256];
snprintf(buf, 256, "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::onSubscribe
void BleServer::onStatus(NimBLECharacteristic *pCharacteristic, Status status, int code) {
void BleServer::onStatus(NimBLECharacteristic *pCharacteristic, Status status, int code) {
if(Logger::getLogLevel() == Logger::VERBOSE) {
char buf[256];
snprintf(buf, 256, "Notification/Indication status code: %d, return code: %d", status, code);
Logger::verbose(LOG_TAG_BLESERVER, buf);
}
}

#ifdef CANBUS_ENABLED

void BleServer::updateRescueApp(long loopTime, long maxLoopTime) {
this->sendValue("loopTime", loopTime);
this->sendValue("maxLoopTime", maxLoopTime);
void BleServer::updateRescueApp(long loopTime, long maxLoopTime) {
this->sendValue("loopTime", loopTime);
this->sendValue("maxLoopTime", maxLoopTime);

/*
Blynk.setProperty(VPIN_VESC_DUTY_CYCLE, "color",
Expand All @@ -443,52 +462,53 @@ void BleServer::onWrite(BLECharacteristic *pCharacteristic) {
}
}
*/
}
}

template<typename TYPE>
void BleServer::sendValue(std::string key, TYPE value) {
std::stringstream ss;
ss << key << "=" << value;
pCharacteristicConf->setValue(ss.str());
pCharacteristicConf->notify();
ss.str("");
delay(5);
}
template<typename TYPE>
void BleServer::sendValue(std::string key, TYPE value) {
std::stringstream ss;
ss << key << "=" << value;
pCharacteristicConf->setValue(ss.str());
pCharacteristicConf->notify();
ss.str("");
delay(5);
}

boolean isStringType(String a) { return true; }
boolean isStringType(String a) { return true; }

boolean isStringType(std::string a) { return true; }
boolean isStringType(std::string a) { return true; }

boolean isStringType(int a) { return false; }
boolean isStringType(int a) { return false; }

boolean isStringType(double a) { return false; }
boolean isStringType(double a) { return false; }

boolean isStringType(boolean a) { return false; }
boolean isStringType(boolean a) { return false; }

struct BleServer::sendConfigValue {
NimBLECharacteristic *pCharacteristic;
std::stringstream ss;
struct BleServer::sendConfigValue {
NimBLECharacteristic *pCharacteristic;
std::stringstream ss;

sendConfigValue(NimBLECharacteristic *pCharacteristic) {
this->pCharacteristic = pCharacteristic;
}
sendConfigValue(NimBLECharacteristic *pCharacteristic) {
this->pCharacteristic = pCharacteristic;
}

template<typename T>
void operator()(const char *name, const T &value) {
if (isStringType(value)) {
ss << name << "=" << static_cast<String>(value).c_str();
} else {
ss << name << "=" << value;
}
Serial.println("Sending: " + String(ss.str().c_str()));
pCharacteristic->setValue(ss.str());
pCharacteristic->indicate();
ss.str("");
delay(5);
template<typename T>
void operator()(const char *name, const T &value) {
if (isStringType(value)) {
ss << name << "=" << static_cast<String>(value).c_str();
} else {
ss << name << "=" << value;
}
};

void BleServer::sendConfig() {
visit_struct::for_each(AppConfiguration::getInstance()->config, sendConfigValue(pCharacteristicConf));
Serial.println("Sending: " + String(ss.str().c_str()));
pCharacteristic->setValue(ss.str());
pCharacteristic->indicate();
ss.str("");
delay(5);
}
};

void BleServer::sendConfig() {
visit_struct::for_each(AppConfiguration::getInstance()->config, sendConfigValue(pCharacteristicConf));
}

#endif //CANBUS_ENABLED
2 changes: 2 additions & 0 deletions src/BleServer.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ class BleServer :
// NimBLEServerCallbacks
void onConnect(NimBLEServer* pServer, ble_gap_conn_desc* desc);
void onDisconnect(NimBLEServer* pServer);
void onMTUChange(uint16_t MTU, ble_gap_conn_desc* desc);

// NimBLECharacteristicCallbacks
void onWrite(NimBLECharacteristic* pCharacteristic);
Expand All @@ -52,6 +53,7 @@ class BleServer :
private:
CanBus *canbus{};
struct sendConfigValue;
void dumpBuffer(std::string header, std::string buffer);
};

#endif
1 change: 1 addition & 0 deletions src/CanBus.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "VescCanConstants.h"
#include "BleCanProxy.h"
#include "CanDevice.h"
#include <vector>

#define B10000001 129
#define B11000011 195
Expand Down
Loading

0 comments on commit c422ae6

Please sign in to comment.