Skip to content

Commit

Permalink
remove remaining Serial prints from Console, add utils library, relat…
Browse files Browse the repository at this point in the history
…ed to #65
  • Loading branch information
paidforby committed Apr 8, 2020
1 parent c665f12 commit 440bde1
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 36 deletions.
4 changes: 2 additions & 2 deletions firmware/esp32/client/StreamClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,15 +26,15 @@ void StreamClient::loop()
if(p == '\b') // backspace
{
if(inputLength > 0){
input[inputLength] = NULL;
input[inputLength] = '\0';
inputLength--;
stream->write(" \b");
}
}
else if(p == 0x7f ) // delete
{
if(inputLength > 0){
input[inputLength] = NULL;
input[inputLength] = '\0';
inputLength--;
stream->write("\b \b");
}
Expand Down
89 changes: 56 additions & 33 deletions firmware/esp32/middleware/Console.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <Layer1.h>
#include <LoRaLayer2.h>
#include "settings/settings.h"
#include "utils/utils.h"

#include <vector>

Expand Down Expand Up @@ -34,9 +35,9 @@ void Console::processLine(char *message, size_t len)
memset(response.message, 0, DATAGRAM_MESSAGE);
int msgLen;

#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("Console::processLine help result %s\r\n", message);
#endif
#endif
// message might not be NULL ended
char msgBuff[len + 2] = {0};
memcpy(msgBuff, message, len);
Expand All @@ -58,31 +59,41 @@ void Console::processLine(char *message, size_t len)

if (strncmp(&args[0][1], "help", 4) == 0)
{
print("Commands: /help /join /nick /raw /lora /switch /restart\r\n");
#ifdef DEBUG_OUT
print("Commands: /help /join /nick /raw /lora /set /restart\r\n");
#ifdef DEBUG_OUT
Serial.printf("Console::processLine help result %s\r\n", (char *)response.message);
#endif
#endif
}
else if (strncmp(&args[0][1], "raw", 3) == 0)
{
#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("Console::processLine switching to RAW\r\n");
#endif
#endif
disconnect(client);
server->disconnect(this);
server->connect(client);
}
else if (strncmp(&args[0][1], "switch", 6) == 0)
else if ((strncmp(&args[0][1], "set", 3) == 0) && (args.size() > 1))
{
#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("Switching UI to %s\n", useBLE ? "WiFi" : "BLE");
#endif
saveUI(!useBLE);
delay(500);
ESP.restart();
#endif
if (strncmp(&args[1][0], "ui", 2) == 0){
saveUI(!useBLE);
delay(500);
ESP.restart();
}
}
else if ((strncmp(&args[0][1], "set", 3) == 0) && (args.size() == 1)){
print("No setting provided, type '/set SETTING'\r\n");
print("SETTINGs include,\r\n");
print("'ui' - toggles between WiFi and BLE user interface\r\n");
}

else if (((strncmp(&args[0][1], "join", 4) == 0) || (strncmp(&args[0][1], "nick", 4) == 0)) && (args.size() > 1))
{
strtok(args[1], "\r"); // remove CR-LF from username

if (username.length() > 0)
{
msgLen = sprintf((char *)response.message, "00c|~ %s is now known as %s\r\n", username.c_str(), args[1]);
Expand All @@ -91,34 +102,44 @@ void Console::processLine(char *message, size_t len)
{
msgLen = sprintf((char *)response.message, "00c|~ %s joined the channel\r\n", args[1]);
}

memcpy(response.destination, LL2.broadcastAddr(), ADDR_LENGTH);
response.type = 'c';
server->transmit(this, response, msgLen + DATAGRAM_HEADER);

memcpy(response.message, &response.message[4], msgLen - 4);
response.message[msgLen - 4] = '\n';
client->receive(response, msgLen - 4 + DATAGRAM_HEADER);

username = String(args[1]);
saveUsername(username);

#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("Console::processLine join/nick result %s\r\n", (char *)response.message);
Serial.printf("Console::processLine new username is %s\r\n", username.c_str());
#endif
#endif
}
else if (((strncmp(&args[0][1], "join", 4) == 0) || (strncmp(&args[0][1], "nick", 4) == 0)) && (args.size() == 1)){
print("No NICKNAME provided, type '/join NICKNAME' to join the chat\r\n");
}
else if ((strncmp(&args[0][1], "restart", 7) == 0))
{
#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("Console::processLine restarting\r\n");
delay(500);
#endif
#endif
ESP.restart();
}
else if ((strncmp(&args[0][1], "lora", 4) == 0))
{
// TODO: print to client instead of serial
Serial.printf("Address: ");
LL2.printAddress(LL2.localAddress());
LL2.printRoutingTable();
print("Local address: ");
char str[ADDR_LENGTH*2 + 1] = {'\0'};
hexcpy(str, LL2.localAddress(), ADDR_LENGTH);
print(str);
print("\r\n");
char str2[256] = {'\0'}; //TODO: need to check size of routing table to allocate correct amount of memory
LL2.getRoutingTable(str2);
print(str2);
}
else
{
Expand All @@ -134,9 +155,9 @@ void Console::processLine(char *message, size_t len)
server->transmit(this, response, msgLen + DATAGRAM_HEADER);
memcpy(response.message, &response.message[4], msgLen - 4);
response.message[msgLen - 4] = '\n';
#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("Console message =>%s<\r\n", &response.message[4]);
#endif
#endif
}
else
{
Expand All @@ -146,9 +167,9 @@ void Console::processLine(char *message, size_t len)
server->transmit(this, response, msgLen + DATAGRAM_HEADER);
memcpy(response.message, &response.message[4], msgLen - 4);
response.message[msgLen - 4] = '\n';
#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("Console message =>%s<\r\n", &response.message[4]);
#endif
#endif
}
}

Expand All @@ -166,7 +187,9 @@ void Console::printBanner()
print("WARNING: LoRa transceiver not found!\r\n");
}
print("Local address of your node is ");
LL2.printAddress(LL2.localAddress()); //TODO: convert byte array to string to print using Console not Serial
char str[ADDR_LENGTH*2 + 1] = {'\0'};
hexcpy(str, LL2.localAddress(), ADDR_LENGTH);
print(str);
print("\r\n");
print("Type '/join NICKNAME' to join the chat, or '/help' for more commands.\r\n");
}
Expand All @@ -175,9 +198,9 @@ void Console::printPrompt()
{
if (username.length() > 0)
{
char message[DATAGRAM_MESSAGE];
sprintf(message, "<%s> ", username.c_str());
print(message);
print("<");
print(username.c_str());
print("> ");
}
else
{
Expand All @@ -187,14 +210,14 @@ void Console::printPrompt()

void Console::transmit(DisasterClient *client, struct Datagram datagram, size_t len)
{
#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("CONSOLE::transmit raw data with len %d type %c\n", len, datagram.type);
for (int idx = 0; idx < len - DATAGRAM_HEADER; idx++)
{
Serial.printf("%02X ", datagram.message[idx]);
}
Serial.println("\n" + String((char *)datagram.message));
#endif
#endif

// TODO: set sessionConnected back to zero on disconnection?
if(sessionConnected == 0){
Expand All @@ -215,14 +238,14 @@ void Console::receive(struct Datagram datagram, size_t len)

if ((datagram.message[2] == 'c') && (datagram.message[3] == '|'))
{
#ifdef DEBUG_OUT
#ifdef DEBUG_OUT
Serial.printf("CONSOLE::receive raw data with len %d type %c\n", msgSize, datagram.type);
for (int idx = 0; idx < msgSize; idx++)
{
Serial.printf("%02X ", datagram.message[idx]);
}
Serial.println("");
#endif
#endif

memcpy(datagram.message, &datagram.message[4], msgSize - 4);
msgSize -= 4;
Expand Down
14 changes: 14 additions & 0 deletions firmware/esp32/settings/settings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,17 @@ void saveUI(bool useBLE)
preferences.putBool("ble", useBLE);
preferences.end();
}

/*
void saveLRFQ(int lrfq)
{
if (!preferences.begin("dr", false))
{
Serial.println("Error opening preferences");
return;
}
preferences.putInt("lrfq", lrfq);
preferences.end();
}
*/
12 changes: 12 additions & 0 deletions firmware/esp32/utils/utils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#include "utils.h"

void hexcpy(char * out, unsigned char * in, size_t insz)
{
int i;
char* buf = out;
for (i = 0; i < insz; i++)
{
buf += sprintf(buf, "%02x", in[i]);
}
buf += sprintf(buf, "\0");
}
4 changes: 4 additions & 0 deletions firmware/esp32/utils/utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#include <Arduino.h>

void hexcpy(char * out, unsigned char * in, size_t insz);

2 changes: 1 addition & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ lib_deps =
AsyncTCP
ESP Async [email protected]
[email protected]
https://github.com/sudomesh/LoRaLayer2#d533eea6c9e97ab2b72347bc673cc2d0d4d1ea3e
https://github.com/sudomesh/LoRaLayer2#ae513036dc0aa1c7aa002726139052a80e07e617
https://github.com/paidforby/AsyncSDServer#13375c6be978cb34180378ecf4042a3a4a1f5eab
ESP8266 and ESP32 OLED driver for SSD1306 displays
[email protected]
Expand Down

0 comments on commit 440bde1

Please sign in to comment.