Skip to content

Commit

Permalink
Add naming to config
Browse files Browse the repository at this point in the history
  • Loading branch information
adnanademovic committed Dec 15, 2016
1 parent 3c78eb6 commit d3bf30e
Show file tree
Hide file tree
Showing 8 changed files with 109 additions and 40 deletions.
1 change: 1 addition & 0 deletions config.cpp
Expand Up @@ -59,6 +59,7 @@ MAP_SYSTEM(CHANNEL, R415X::ChannelProperties, receiver.channel)
MAP_SYSTEM(PID_PARAMETERS, Control::PIDParameters, control.pid_parameters)
MAP_SYSTEM(STATE_PARAMETERS, State::Parameters, state.parameters)
MAP_SYSTEM(LED_STATES, LED::States, led.states)
MAP_SYSTEM(DEVICE_NAME, DeviceName, name)

#undef MAP_SYSTEM

Expand Down
8 changes: 5 additions & 3 deletions config.h
Expand Up @@ -26,6 +26,7 @@
#include "led.h"
#include "state.h"
#include "version.h"
#include "devicename.h"

#include <tuple>

Expand Down Expand Up @@ -66,9 +67,10 @@ struct Config {
PID_PARAMETERS,
STATE_PARAMETERS,
LED_STATES,
DEVICE_NAME,
};

using Data = std::tuple<Version, ConfigID, PcbTransform, Airframe::MixTable, AK8963::MagBias, R415X::ChannelProperties, Control::PIDParameters, State::Parameters, LED::States>;
using Data = std::tuple<Version, ConfigID, PcbTransform, Airframe::MixTable, AK8963::MagBias, R415X::ChannelProperties, Control::PIDParameters, State::Parameters, LED::States, DeviceName>;

Config();
explicit Config(Systems& sys);
Expand Down Expand Up @@ -97,10 +99,10 @@ struct Config {

static_assert(sizeof(Config) ==
sizeof(Version) + sizeof(ConfigID) + sizeof(PcbTransform) + sizeof(Airframe::MixTable) + sizeof(AK8963::MagBias) + sizeof(R415X::ChannelProperties) + sizeof(State::Parameters) +
sizeof(Control::PIDParameters) + sizeof(LED::States),
sizeof(Control::PIDParameters) + sizeof(LED::States) + sizeof(DeviceName),
"Data is not packed");

static_assert(sizeof(Config) == 619, "Data does not have expected size");
static_assert(sizeof(Config) == 628, "Data does not have expected size");

Config readEEPROM();
bool isEmptyEEPROM();
Expand Down
64 changes: 64 additions & 0 deletions devicename.cpp
@@ -0,0 +1,64 @@
#include "devicename.h"

#include "Arduino.h"

#include "debug.h"

DeviceName::DeviceName() : DeviceName("FLYBRIX") {
}

DeviceName::DeviceName(const String& name) {
std::size_t l{name.length()};
if (l > 8) {
l = 8;
}
for (std::size_t i{0}; i < l; ++i) {
value[i] = name.charAt(i);
}
for (std::size_t i{l}; i < MAX_NAME_LENGTH + 1; ++i) {
value[i] = 0;
}
}

bool badChar(char c);

bool DeviceName::verify() const {
if (!value[0]) {
DebugPrint("Name cannot be empty");
}
for (char c : value) {
if (badChar(c)) {
DebugPrint(
"Illegal character in name!"
" "
"Names are limited to 0-9, a-z, A-Z, '_', '-'!");
return false;
}
if (!c) {
return true;
}
}
DebugPrint("Given device name is too long (max 8 characters)!");
return false;
}

bool badChar(char c) {
if (!c) {
return false;
}
if (c >= 'a' && c <= 'z') {
return false;
}
if (c >= 'A' && c <= 'Z') {
return false;
}
if (c >= '0' && c <= '9') {
return false;
}
for (char c_legal : "_-") {
if (c == c_legal) {
return false;
}
}
return true;
}
21 changes: 21 additions & 0 deletions devicename.h
@@ -0,0 +1,21 @@
#ifndef DEVICENAME_H
#define DEVICENAME_H

#include <cstdint>

class String;

constexpr uint8_t MAX_NAME_LENGTH = 8;

struct __attribute__((packed)) DeviceName {
DeviceName();
DeviceName(const String& name);

bool verify() const;

char value[MAX_NAME_LENGTH + 1];
};

static_assert(sizeof(DeviceName) == sizeof(char) * 9, "Data is not packed");

#endif /* DEVICENAME_H */
4 changes: 2 additions & 2 deletions flybrix-firmware.ino
Expand Up @@ -57,12 +57,12 @@ void setup() {

bool go_to_test_mode{isEmptyEEPROM()};

setBluetoothUart();

// load stored settings (this will reinitialize if there is no data in the EEPROM!
readEEPROM().applyTo(sys);
sys.state.resetState();

setBluetoothUart(sys.name);

sys.state.set(STATUS_BMP_FAIL);
sys.led.update();
sys.bmp.restart();
Expand Down
44 changes: 10 additions & 34 deletions serialFork.cpp
Expand Up @@ -11,6 +11,7 @@
#include "serialFork.h"
#include <Arduino.h>
#include "board.h"
#include "devicename.h"

namespace {
struct USBComm {
Expand Down Expand Up @@ -49,7 +50,7 @@ struct Bluetooth {
Serial1.begin(57600);
}

void setBluetoothUart();
void setBluetoothUart(const DeviceName& name);

bool read() {
while (Serial1.available()) {
Expand Down Expand Up @@ -139,7 +140,7 @@ void flushATmodeResponse() {
}
}

void Bluetooth::setBluetoothUart() {
void Bluetooth::setBluetoothUart(const DeviceName& name) {
// PIN 12 of teensy is BMD (P0.13)
// PIN 30 of teensy is BMD (PO.14) AT Mode
// PIN 28 of teensy is BMD RST
Expand All @@ -153,39 +154,14 @@ void Bluetooth::setBluetoothUart() {
digitalWriteFast(board::bluetooth::RESET, LOW); // reset BMD
delay(100);
digitalWriteFast(board::bluetooth::RESET, HIGH); // reset BMD complete, now in AT mode
delay(2500); // time needed initialization of AT mode
uint8_t data[18];

data[0] = 'a';
data[1] = 't';
data[2] = '$';

data[3] = 'u';
data[4] = 'e';
data[5] = 'n';
data[6] = ' ';
data[7] = '0';
data[8] = '1';
data[9] = '\n';
Serial1.write(data, 10);
delay(2500); // time needed initialization of AT mode

Serial1.print("at$uen 01\n");
flushATmodeResponse();

data[3] = 'n';
data[4] = 'a';
data[5] = 'm';
data[6] = 'e';
data[7] = ' ';
data[8] = 'F';
data[9] = 'L';
data[10] = 'Y';
data[11] = 'B';
data[12] = 'R';
data[13] = 'I';
data[14] = 'X';
data[15] = '\n';
Serial1.write(data, 16);

Serial1.print("at$name ");
Serial1.print(name.value);
Serial1.print("\n");
flushATmodeResponse();

digitalWriteFast(board::bluetooth::MODE, HIGH);
Expand All @@ -197,9 +173,9 @@ void Bluetooth::setBluetoothUart() {
#endif
}

void setBluetoothUart() {
void setBluetoothUart(const DeviceName& name) {
#ifndef ALPHA
bluetooth.setBluetoothUart();
bluetooth.setBluetoothUart(name);
#endif
}

Expand Down
4 changes: 3 additions & 1 deletion serialFork.h
Expand Up @@ -14,8 +14,10 @@
#include <cstdint>
#include "cobs.h"

class DeviceName;

CobsReaderBuffer* readSerial();
void writeSerial(uint8_t* data, size_t length);
void flushSerial();
void setBluetoothUart();
void setBluetoothUart(const DeviceName& name);
#endif
3 changes: 3 additions & 0 deletions systems.h
Expand Up @@ -25,6 +25,7 @@
#include "serial.h"
#include "state.h"
#include "version.h"
#include "devicename.h"

struct Systems {
Systems();
Expand All @@ -48,6 +49,8 @@ struct Systems {

ConfigID id;

DeviceName name;

void parseConfig();
};

Expand Down

0 comments on commit d3bf30e

Please sign in to comment.