Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix main.ino Welcome Message, BLE client, OLED client and Console. #56

Merged
merged 5 commits into from
Mar 14, 2020
Merged
Changes from 1 commit
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
Next Next commit
Fix main.ino Welcome Message, BLE client, OLED client and Console.
  • Loading branch information
beegee-tokyo committed Feb 24, 2020
commit 50e14e603817ae18f217653631107d135f717446
118 changes: 67 additions & 51 deletions firmware/esp32/client/BleUartClient.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#ifdef USE_BLE
#include "BleUartClient.h"

/** Comment out to stop debug output */
// #define DEBUG_OUT

/** Service UUID for Uart */
#define UART_UUID "6E400001-B5A3-F393-E0A9-E50E24DCCA9E"
/** Characteristic UUID for receiver */
@@ -44,28 +47,31 @@ void BleUartClient::receive(struct Datagram datagram, size_t len)
{
if (deviceConnected)
{
// /// \todo for debug only
// Serial.printf("BLE: sending %s with len %d\n", message.c_str(), message.length());
// /// \todo end of for debug only

// TODO: msg id? defaulting to 0 for now
uint16_t msg_id = 0x2020;

unsigned char buf[2 + len-DATAGRAM_HEADER + 2] = {'\0'};
memcpy(buf, &msg_id, 2);
//message.getBytes(buf + 2, message.length() + 1);
memcpy(buf+2, &datagram.message, len-DATAGRAM_HEADER);

// /// \todo for debug only
// Serial.println("BLE: Sending raw data");
// for (int idx = 0; idx < sizeof(buf); idx++)
// {
// Serial.printf("%02X ", buf[idx]);
// }
// Serial.println("");
// /// \todo end of for debug only

pCharacteristicUartTX->setValue(buf, 2 + len-DATAGRAM_HEADER + 2);
#ifdef DEBUG_OUT
Serial.printf("BLE:receive %s length %d\n", (char *)datagram.message, len - DATAGRAM_HEADER);
Serial.println("BLE::receive raw data");
for (int idx = 0; idx < len - DATAGRAM_HEADER; idx++)
{
Serial.printf("%02X ", datagram.message[idx]);
}
Serial.println("");
#endif

unsigned char buf[len - DATAGRAM_HEADER] = {'\0'};
memcpy(buf, &datagram.message, len-DATAGRAM_HEADER);

#ifdef DEBUG_OUT
Serial.printf("BLE: Sending raw data with len %d\n", sizeof(buf));
for (int idx = 0; idx < sizeof(buf); idx++)
{
Serial.printf("%02X ", buf[idx]);
}
Serial.println("");

Serial.printf("BLE::receive %s len %d type %c\n", buf, sizeof(buf), datagram.type);
#endif

pCharacteristicUartTX->setValue(buf, sizeof(buf));
pCharacteristicUartTX->notify();

// Give BLE time to get the data out
@@ -75,34 +81,27 @@ void BleUartClient::receive(struct Datagram datagram, size_t len)

void BleUartClient::handleData(void *data, size_t len)
{
//uint16_t msg_id;
//char msg[len - 2 + 1] = {'\0'};

// /// \todo for debug only
// char debug[len] = {'\0'};
// memcpy(debug, data, len);
// Serial.println("BLE: Received raw data");
// for (int idx = 0; idx < len; idx++)
// {
// Serial.printf("%02X ", debug[idx]);
// }
// Serial.println("");
// /// \todo end of for debug only

// parse out message and message id
//memcpy(&msg_id, data, 2);
//memcpy(msg, data + 2, rxLen - 2);
#ifdef DEBUG_OUT
char debug[len] = {'\0'};
memcpy(debug, data, len);
Serial.println("BLE: Received raw data");
for (int idx = 0; idx < len; idx++)
{
Serial.printf("%02X ", debug[idx]);
}
Serial.println("");
#endif

char *msg = (char *)data;
struct Datagram datagram = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
datagram.type = 'c';
datagram.type = msg[2];
memcpy(&datagram.message, data, len);
len = len+DATAGRAM_HEADER;

// /// \todo for debug only
// Serial.printf("BLE: received %s len %d\n", msg, len - 2 + 1);
// /// \todo end of for debug only
#ifdef DEBUG_OUT
Serial.printf("BLE::handleData %s len %d type %c\n", msg, len, datagram.type);
#endif

server->transmit(this, datagram, len);
server->transmit(this, datagram, len + DATAGRAM_HEADER);
}

/**
@@ -112,14 +111,18 @@ class MyServerCallbacks : public BLEServerCallbacks
{
void onConnect(BLEServer *pServer)
{
#ifdef DEBUG_OUT
Serial.println("BLE client connected");
#endif
pServer->updatePeerMTU(pServer->getConnId(), 260);
deviceConnected = true;
};

void onDisconnect(BLEServer *pServer)
{
#ifdef DEBUG_OUT
Serial.println("BLE client disconnected");
#endif
deviceConnected = false;
pAdvertising->start();
}
@@ -140,7 +143,15 @@ class UartTxCbHandler : public BLECharacteristicCallbacks
{
strncpy((char *)rxData, rxValue.c_str(), 512);

Serial.printf("UART write callback received %s\n", (char *)rxData);
#ifdef DEBUG_OUT
Serial.printf("BLE:onWrite write callback received %s length %d\n", (char *)rxData, rxLen);
Serial.println("BLE:onWrite raw data");
for (int idx = 0; idx < rxLen; idx++)
{
Serial.printf("%02X ", rxData[idx]);
}
Serial.println("");
#endif
dataRcvd = true;
}
};
@@ -154,7 +165,9 @@ class DescriptorCallbacks : public BLEDescriptorCallbacks
descrValue = pDescriptor->getValue();
if (descrValue[0] & (1 << 0))
{
#ifdef DEBUG_OUT
Serial.println("Notifications enabled");
#endif
if (connectCallback)
{
if (!connectedToServer)
@@ -164,10 +177,10 @@ class DescriptorCallbacks : public BLEDescriptorCallbacks
}
else
{
if (history)
{
history->replay(&ble_client);
}
// if (history)
// {
// history->replay(&ble_client);
// }
}
}
}
@@ -200,7 +213,9 @@ void BleUartClient::init()
(uint8_t)(uniqueId), (uint8_t)(uniqueId >> 8),
(uint8_t)(uniqueId >> 16), (uint8_t)(uniqueId >> 24),
(uint8_t)(uniqueId >> 32), (uint8_t)(uniqueId >> 40));
#ifdef DEBUG_OUT
Serial.printf("Device name: %s\n", apName);
#endif

// Initialize BLE and set output power
BLEDevice::init(apName);
@@ -209,7 +224,9 @@ void BleUartClient::init()

BLEAddress thisAddress = BLEDevice::getAddress();

#ifdef DEBUG_OUT
Serial.printf("BLE address: %s\n", thisAddress.toString().c_str());
#endif

// Create BLE Server
pServer = BLEDevice::createServer();
@@ -240,7 +257,6 @@ void BleUartClient::init()
txDescriptor = pCharacteristicUartTX->getDescriptorByUUID("2902");
if (txDescriptor != NULL)
{
Serial.println("Got descriptor for TX as 2902");
txDescriptor->setCallbacks(new DescriptorCallbacks());
}

2 changes: 0 additions & 2 deletions firmware/esp32/client/BleUartClient.h
Original file line number Diff line number Diff line change
@@ -27,8 +27,6 @@ class BleUartClient : public DisasterClient
BleUartClient *client;

public:
String buffer = "";

void receive(struct Datagram datagram, size_t len);

void handleData(void *data, size_t len);
10 changes: 3 additions & 7 deletions firmware/esp32/client/OLEDClient.cpp
Original file line number Diff line number Diff line change
@@ -27,15 +27,11 @@ void OLEDClient::loop()

void OLEDClient::receive(struct Datagram datagram, size_t len)
{

String message;
for(size_t i=0; i < len-DATAGRAM_HEADER; i++) {
message += (char) datagram.message[i];
}
if (message.substring(0, 2) == "c|")
String message = String((char *) datagram.message);
if (message.substring(2, 4) == "c|")
{
uint16_t maxLines = (height - top) / LINE_HEIGHT;
buffer.push_back(message.substring(2));
buffer.push_back(message.substring(4));
while (buffer.size() > maxLines)
{
buffer.pop_front();
8 changes: 2 additions & 6 deletions firmware/esp32/client/StreamClient.cpp
Original file line number Diff line number Diff line change
@@ -14,7 +14,7 @@ void StreamClient::loop()
{
String message = stream->readString();
size_t len = message.length();
uint8_t* data;
uint8_t data[len];
message.getBytes(data, len);
struct Datagram datagram = {0xff, 0xff, 0xff, 0xff, 0xff, 0xff};
datagram.type = 'c';
@@ -27,9 +27,5 @@ void StreamClient::loop()

void StreamClient::receive(struct Datagram datagram, size_t len)
{
String message;
for(size_t i=0; i < len-DATAGRAM_HEADER; i++) {
message += (char) datagram.message[i];
}
stream->print(message);
stream->write(datagram.message, len - DATAGRAM_HEADER);
};
27 changes: 12 additions & 15 deletions firmware/esp32/main.ino
Original file line number Diff line number Diff line change
@@ -29,7 +29,7 @@
#include "client/BleUartClient.h"
#endif
// middleware
//#include "middleware/Console.h"
#include "middleware/Console.h"
//#include "middleware/HistoryReplay.h"
// history
//#include "history/HistorySD.h"
@@ -243,23 +243,20 @@ class WelcomeMessage : public DisasterMiddleware
public:
void setup()
{
size_t len = 30;
uint8_t data[len] = "00c|Welcome to DISASTER RADIO";
struct Datagram datagram1 = buildDatagram(LL2.broadcastAddr(), 'c', data, len);
client->receive(datagram1, len+7);
uint8_t data[] = "00c|Welcome to DISASTER RADIO";
struct Datagram datagram1 = buildDatagram(LL2.broadcastAddr(), 'c', data, sizeof(data));
client->receive(datagram1, sizeof(data) + 7);
if (!sdInitialized)
{
size_t len = 64;
uint8_t data[len] = "00c|WARNING: SD card not found, functionality may be limited";
struct Datagram datagram2 = buildDatagram(LL2.broadcastAddr(), 'c', data, len);
client->receive(datagram2, len+7);
uint8_t data[] = "00c|WARNING: SD card not found, functionality may be limited";
struct Datagram datagram2 = buildDatagram(LL2.broadcastAddr(), 'c', data, sizeof(data));
client->receive(datagram2, sizeof(data) + 7);
}
if (!loraInitialized)
{
size_t len = 67;
uint8_t data[len] = "00c|WARNING: LoRa radio not found, functionality may be limited";
struct Datagram datagram3 = buildDatagram(LL2.broadcastAddr(), 'c', data, len);
client->receive(datagram3, len+7);
uint8_t data[] = "00c|WARNING: LoRa radio not found, functionality may be limited";
struct Datagram datagram3 = buildDatagram(LL2.broadcastAddr(), 'c', data, sizeof(data));
client->receive(datagram3, sizeof(data) + 7);
}
client->setup();
}
@@ -269,8 +266,8 @@ void setupSerial()
{
Serial.println("* Initializing serial...");

//radio->connect(new Console())
//radio->connect(new StreamClient(&Serial));
radio->connect(new Console())
->connect(new StreamClient(&Serial));
//->connect(new WelcomeMessage())
//->connect(new HistoryReplay(history))
}
Loading