From d3bf30e5eb09245d727a847da2dcd57b292ee411 Mon Sep 17 00:00:00 2001 From: Adnan Ademovic Date: Thu, 15 Dec 2016 21:58:20 +0100 Subject: [PATCH] Add naming to config --- config.cpp | 1 + config.h | 8 +++--- devicename.cpp | 64 ++++++++++++++++++++++++++++++++++++++++++++ devicename.h | 21 +++++++++++++++ flybrix-firmware.ino | 4 +-- serialFork.cpp | 44 +++++++----------------------- serialFork.h | 4 ++- systems.h | 3 +++ 8 files changed, 109 insertions(+), 40 deletions(-) create mode 100644 devicename.cpp create mode 100644 devicename.h diff --git a/config.cpp b/config.cpp index 3b423be..b8e9994 100644 --- a/config.cpp +++ b/config.cpp @@ -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 diff --git a/config.h b/config.h index 1b0b14b..7c8cb65 100644 --- a/config.h +++ b/config.h @@ -26,6 +26,7 @@ #include "led.h" #include "state.h" #include "version.h" +#include "devicename.h" #include @@ -66,9 +67,10 @@ struct Config { PID_PARAMETERS, STATE_PARAMETERS, LED_STATES, + DEVICE_NAME, }; - using Data = std::tuple; + using Data = std::tuple; Config(); explicit Config(Systems& sys); @@ -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(); diff --git a/devicename.cpp b/devicename.cpp new file mode 100644 index 0000000..6ea810e --- /dev/null +++ b/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; +} diff --git a/devicename.h b/devicename.h new file mode 100644 index 0000000..ab892f5 --- /dev/null +++ b/devicename.h @@ -0,0 +1,21 @@ +#ifndef DEVICENAME_H +#define DEVICENAME_H + +#include + +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 */ diff --git a/flybrix-firmware.ino b/flybrix-firmware.ino index a4e65d2..a185fb0 100644 --- a/flybrix-firmware.ino +++ b/flybrix-firmware.ino @@ -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(); diff --git a/serialFork.cpp b/serialFork.cpp index e6ce77f..a1268e3 100644 --- a/serialFork.cpp +++ b/serialFork.cpp @@ -11,6 +11,7 @@ #include "serialFork.h" #include #include "board.h" +#include "devicename.h" namespace { struct USBComm { @@ -49,7 +50,7 @@ struct Bluetooth { Serial1.begin(57600); } - void setBluetoothUart(); + void setBluetoothUart(const DeviceName& name); bool read() { while (Serial1.available()) { @@ -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 @@ -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); @@ -197,9 +173,9 @@ void Bluetooth::setBluetoothUart() { #endif } -void setBluetoothUart() { +void setBluetoothUart(const DeviceName& name) { #ifndef ALPHA - bluetooth.setBluetoothUart(); + bluetooth.setBluetoothUart(name); #endif } diff --git a/serialFork.h b/serialFork.h index 80f05f4..505a3b4 100644 --- a/serialFork.h +++ b/serialFork.h @@ -14,8 +14,10 @@ #include #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 diff --git a/systems.h b/systems.h index 62f69e8..e8e03b9 100644 --- a/systems.h +++ b/systems.h @@ -25,6 +25,7 @@ #include "serial.h" #include "state.h" #include "version.h" +#include "devicename.h" struct Systems { Systems(); @@ -48,6 +49,8 @@ struct Systems { ConfigID id; + DeviceName name; + void parseConfig(); };