Skip to content

Commit

Permalink
SimpleCLI broke my code grrr
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-ffm committed Apr 13, 2019
1 parent ce7282c commit f2f30b8
Show file tree
Hide file tree
Showing 3 changed files with 118 additions and 31 deletions.
50 changes: 50 additions & 0 deletions src/lib/CRC8.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@


#include "lib/CRC8.h"


CRC8::CRC8(void) {


}

void CRC8::begin(void) {
crc remainder;

for (int dividend = 0; dividend < 256; ++dividend)
{
remainder = dividend << (WIDTH - 8);


for (uint8_t bit = 8; bit > 0; --bit)
{
if (remainder & TOPBIT)
{
remainder = (remainder << 1) ^ POLYNOMIAL;
}
else
{
remainder = (remainder << 1);
}
}

crcTable[dividend] = remainder;
}
}


crc CRC8::get_crc8(uint8_t const message[], int nBytes) {
uint8_t data;
crc remainder = 0;


for (int byte = 0; byte < nBytes; ++byte)
{
data = message[byte] ^ (remainder >> (WIDTH - 8));
remainder = crcTable[data] ^ (remainder << 8);
}


return (remainder);

}
30 changes: 30 additions & 0 deletions src/lib/CRC8.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* CRC8 library
*/
#ifndef CRC8_H
#define CRC8_H

#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif

typedef uint8_t crc;
#define POLYNOMIAL 0xd5 /* CRC8_CCITT -- this polynomial needs to match choice on javascript end */
#define WIDTH (8 * sizeof(crc))
#define TOPBIT (1 << (WIDTH - 1))

class CRC8 {
public:
CRC8();
void begin();
crc get_crc8(uint8_t const message[], int nBytes);

private:
uint8_t crcTable[256];


};

#endif
69 changes: 38 additions & 31 deletions src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
#include <esp_system.h>
#include <lib/MSP.h>
#include <lib/LoRa.h>
#include <lib/CRC8.h>
#include <SSD1306.h>
#include <EEPROM.h>
#include <SimpleCLI.h>
using namespace simplecli;
//using namespace simplecli;
#include <main.h>

#define SCK 5 // GPIO5 - SX1278's SCK
Expand All @@ -22,6 +23,8 @@ config cfg;
MSP msp;
bool booted = 0;
Stream *serialConsole[1];
CRC8 crc8;
uint8_t FLchecksum;
int cNum = 0;
int displayPage = 0;
SSD1306 display (0x3c, 4, 15);
Expand Down Expand Up @@ -73,7 +76,7 @@ void initConfig () {
cfg.configVersion = CFGVER;
String("IRP2").toCharArray(cfg.loraHeader,5); // protocol identifier
//cfg.loraAddress = 2; // local lora address
cfg.loraFrequency = 915E6; // 433E6, 868E6, 915E6
cfg.loraFrequency = 433E6; // 433E6, 868E6, 915E6
cfg.loraBandwidth = 250000;// 250000 khz
cfg.loraCodingRate4 = 6; // Error correction rate 4/6
cfg.loraSpreadingFactor = 8; // 7 is shortest time on air - 12 is longest
Expand Down Expand Up @@ -153,14 +156,14 @@ String getValue(String data, char separator, int index) {


// ----------------------------------------------------------------------------- CLI
SimpleCLI* cli;

SimpleCLI cli;
Command Cmd;
int serIn; // var that will hold the bytes-in read from the serialBuffer
char serInString[100]; // array that will hold the different bytes 100=100characters;
// -> you must state how long the array will be else it won't work.
int serInIndx = 0; // index of serInString[] in which to insert the next incoming byte
int serOutIndx = 0; // index of the outgoing serInString[] array;

/*
void readCli () {
int sb;
if(serialConsole[0]->available()) {
Expand All @@ -171,14 +174,15 @@ void readCli () {
serialConsole[0]->write(sb);
if (sb == '\n') {
cNum = 0;
cli->parse(serInString);
cli.parse(serInString);
serInIndx = 0;
memset(serInString, 0, sizeof(serInString));
serialConsole[0]->print("> ");
}
}
}
}
*/
void cliLog (String log) {
//logger.append(log);
if (cfg.debugOutput) {
Expand All @@ -191,6 +195,7 @@ void cliLog (String log) {
}
}
}
/*
void cliStatus(int n) {
serialConsole[n]->println("================== Status ==================");
serialConsole[n]->print("FC: ");
Expand Down Expand Up @@ -317,26 +322,26 @@ void cliFCpass(int n) {
}
void initCli () {
cli = new SimpleCLI();
cli->onNotFound = [](String str) {
Serial.println("\"" + str + "\" not found");
};
cli->addCmd(new Command("status", [](Cmd* cmd) { cliStatus(cNum); } ));
cli->addCmd(new Command("help", [](Cmd* cmd) { cliHelp(cNum); } ));
cli->addCmd(new Command("debug", [](Cmd* cmd) { cliDebug(cNum); } ));
cli->addCmd(new Command("log", [](Cmd* cmd) { cliShowLog(cNum); } ));
cli->addCmd(new Command("localfakeplanes", [](Cmd* cmd) { cliLocalFake(cNum); } ));
cli->addCmd(new Command("radiofakeplanes", [](Cmd* cmd) { cliRadioFake(cNum); } ));
cli->addCmd(new Command("movefakeplanes", [](Cmd* cmd) { cliMoveFake(cNum); } ));
cli->addCmd(new Command("lfp", [](Cmd* cmd) { cliLocalFake(cNum); } ));
cli->addCmd(new Command("rfp", [](Cmd* cmd) { cliRadioFake(cNum); } ));
cli->addCmd(new Command("mfp", [](Cmd* cmd) { cliMoveFake(cNum); } ));
cli->addCmd(new Command("reboot", [](Cmd* cmd) { cliReboot(cNum); } ));
cli->addCmd(new Command("gpspos", [](Cmd* cmd) { cliGPSpos(cNum); } ));
//cli = new SimpleCLI();
// cli.onNotFound = [](String str) {
// Serial.println("\"" + str + "\" not found");
// };
cli.addCmd(new Command("status", [](Cmd* cmd) { cliStatus(cNum); } ));
cli.addCmd(new Command("help", [](Cmd* cmd) { cliHelp(cNum); } ));
cli.addCmd(new Command("debug", [](Cmd* cmd) { cliDebug(cNum); } ));
cli.addCmd(new Command("log", [](Cmd* cmd) { cliShowLog(cNum); } ));
cli.addCmd(new Command("localfakeplanes", [](Cmd* cmd) { cliLocalFake(cNum); } ));
cli.addCmd(new Command("radiofakeplanes", [](Cmd* cmd) { cliRadioFake(cNum); } ));
cli.addCmd(new Command("movefakeplanes", [](Cmd* cmd) { cliMoveFake(cNum); } ));
cli.addCmd(new Command("lfp", [](Cmd* cmd) { cliLocalFake(cNum); } ));
cli.addCmd(new Command("rfp", [](Cmd* cmd) { cliRadioFake(cNum); } ));
cli.addCmd(new Command("mfp", [](Cmd* cmd) { cliMoveFake(cNum); } ));
cli.addCmd(new Command("reboot", [](Cmd* cmd) { cliReboot(cNum); } ));
cli.addCmd(new Command("gpspos", [](Cmd* cmd) { cliGPSpos(cNum); } ));
Command* config = new Command("config", [](Cmd* cmd) {
String arg1 = cmd->getValue(0);
String arg2 = cmd->getValue(1);
String arg1 = cmd.getValue(0);
String arg2 = cmd.getValue(1);
if (arg1 == "") cliConfig(cNum);
if (arg1 == "loraFreq") {
if ( arg2.toInt() == 433E6 || arg2.toInt() == 868E6 || arg2.toInt() == 915E6) {
Expand All @@ -357,7 +362,7 @@ void initCli () {
}
}
if (arg1 == "loraSpread") {
if (arg2.toInt() >= 7 && arg2.toInt() <= 12) {
if (arg2.toInt() >= 7 && arg2.toInt() <= 12) {
cfg.loraSpreadingFactor = arg2.toInt();
saveConfig();
serialConsole[cNum]->println("Lora spreading factor changed!");
Expand All @@ -366,7 +371,7 @@ void initCli () {
}
}
if (arg1 == "loraPower") {
if (arg2.toInt() >= 0 && arg2.toInt() <= 20) {
if (arg2.toInt() >= 0 && arg2.toInt() <= 20) {
cfg.loraPower = arg2.toInt();
saveConfig();
serialConsole[cNum]->println("Lora power factor changed!");
Expand Down Expand Up @@ -426,10 +431,11 @@ void initCli () {
config->addArg(new AnonymOptArg(""));
config->addArg(new AnonymOptArg(""));
cli->addCmd(config);
//cli->parse("ping");
//cli->parse("hello");
cli.addCmd(config);
//cli.parse("ping");
//cli.parse("hello");
}
*/
// ----------------------------------------------------------------------------- Logger
void initLogger () {

Expand Down Expand Up @@ -775,11 +781,12 @@ void setup() {
Serial.begin(115200);
serialConsole[0] = &Serial;
initLogger();
initCli();
//initCli();
initConfig();
initDisplay();
initLora();
delay(1500);
crc8.begin();
initMSP();
delay(1000);
pinMode(interruptPin, INPUT);
Expand Down Expand Up @@ -835,7 +842,7 @@ void loop() {

if (displayon && millis() - displayLastTime > cfg.intervalDisplay) {
drawDisplay();
readCli();
//readCli();
loraTX = 0;
loraRX = 0;
displayLastTime = millis();
Expand Down

0 comments on commit f2f30b8

Please sign in to comment.