diff --git a/platformio.ini b/platformio.ini index de33b48c..1d59d9f6 100644 --- a/platformio.ini +++ b/platformio.ini @@ -19,11 +19,9 @@ ; Common build settings across all devices [env] lib_deps = - ;https://github.com/MobiFlight/LedControl#1.1.0 waspinator/AccelStepper @ 1.61 https://github.com/MobiFlight/LiquidCrystal_I2C#v1.1.4 https://github.com/MobiFlight/Arduino-CmdMessenger#4.2.1 - ricaun/ArduinoUniqueID @ ^1.3.0 custom_lib_deps_Atmel = arduino-libraries/Servo @ 1.1.8 build_flags = @@ -144,6 +142,7 @@ build_src_filter = +<../_Boards/RaspberryPi> lib_deps = ${env.lib_deps} + ricaun/ArduinoUniqueID @ ^1.3.0 monitor_speed = 115200 extra_scripts = ${env.extra_scripts} \ No newline at end of file diff --git a/src/Config.cpp b/src/Config.cpp index 8cf41ab5..617cb330 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -9,7 +9,9 @@ #include "Button.h" #include "Encoder.h" #include "Output.h" +#if defined(ARDUINO_ARCH_RP2040) #include "ArduinoUniqueID.h" +#endif #if MF_ANALOG_SUPPORT == 1 #include "Analog.h" @@ -55,12 +57,16 @@ const uint8_t MEM_OFFSET_SERIAL = MEM_OFFSET_NAME + MEM_LEN_NAME; const uint8_t MEM_LEN_SERIAL = 11; const uint8_t MEM_OFFSET_CONFIG = MEM_OFFSET_NAME + MEM_LEN_NAME + MEM_LEN_SERIAL; -char serial[3 + UniqueIDsize * 2 + 1] = MOBIFLIGHT_SERIAL; // 3 characters for "SN-", UniqueID as HEX String, terminating NULL -char name[MEM_LEN_NAME] = MOBIFLIGHT_NAME; -const int MEM_LEN_CONFIG = MEMLEN_CONFIG; -char nameBuffer[MEM_LEN_CONFIG] = ""; -uint16_t configLength = 0; -boolean configActivated = false; +#if defined(ARDUINO_ARCH_AVR) +char serial[11] = MOBIFLIGHT_SERIAL; // 3 characters for "SN-",7 characters for "xyz-zyx" plus terminating NULL +#elif defined(ARDUINO_ARCH_RP2040) +char serial[3 + UniqueIDsize * 2 + 1] = MOBIFLIGHT_SERIAL; // 3 characters for "SN-", UniqueID as HEX String, terminating NULL +#endif +char name[MEM_LEN_NAME] = MOBIFLIGHT_NAME; +const int MEM_LEN_CONFIG = MEMLEN_CONFIG; +char nameBuffer[MEM_LEN_CONFIG] = ""; +uint16_t configLength = 0; +boolean configActivated = false; void resetConfig(); void readConfig(); @@ -451,17 +457,40 @@ bool getStatusConfig() void generateRandomSerial() { randomSeed(millis()); - sprintf(serial, "SN-%03x-%03x", (unsigned int)random(4095), (unsigned int)random(4095)); + serial[0] = 'S'; + serial[1] = 'N'; + serial[2] = '-'; + serial[6] = '-'; + serial[10] = 0x00; + uint16_t randomSerial = random(4095); + for (uint8_t i = 3; i < 6; i++) { + serial[i] = (randomSerial & 0x000F) + 48; // convert from 4bit to HEX string + if (serial[i] >= 58) serial[i] += 8; // if HeX value is A - F add 8 to get the letters + randomSerial >>= 4; + } + randomSerial = random(4095); + for (uint8_t i = 7; i < 10; i++) { + serial[i] = (randomSerial & 0x000F) + 48; // convert from 4bit to HEX string + if (serial[i] >= 58) serial[i] += 7; // if HeX value is A - F add 7 to get the letters + randomSerial >>= 4; + } MFeeprom.write_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL); } -void generateUniqueSerial() +#if defined(ARDUINO_ARCH_RP2040) +void readUniqueSerial() { - sprintf(serial, "SN-"); + serial[0] = 'S'; + serial[1] = 'N'; + serial[2] = '-'; for (size_t i = 0; i < UniqueIDsize; i++) { - sprintf(&serial[3 + i * 2], "%02X", UniqueID[i]); + serial[3 + i * 2] = (UniqueID[i] >> 4) + 48; + if (serial[3 + i * 2] >= 58) serial[3 + i * 2] += 7; + serial[3 + i * 2 + 1] = (UniqueID[i] & 0x0F) + 48; + if (serial[3 + i * 2 + 1] >= 58) serial[3 + i * 2 + 1] += 7; } } +#endif void generateSerial(bool force) { @@ -477,12 +506,13 @@ void generateSerial(bool force) MFeeprom.read_block(MEM_OFFSET_SERIAL, serial, MEM_LEN_SERIAL); return; } - +#if defined(ARDUINO_ARCH_RP2040) // A uniqueID is already generated and saved to the eeprom if (MFeeprom.read_byte(MEM_OFFSET_SERIAL) == 'I' && MFeeprom.read_byte(MEM_OFFSET_SERIAL + 1) == 'D') { - generateUniqueSerial(); + readUniqueSerial(); return; } +#endif // Coming here no UniqueID and no serial number is available, so it's the first start up of a board #if defined(ARDUINO_ARCH_AVR) @@ -493,7 +523,7 @@ void generateSerial(bool force) generateRandomSerial(); #elif defined(ARDUINO_ARCH_RP2040) // Read the uniqueID for Pico's and use it as serial number - generateUniqueSerial(); + readUniqueSerial(); // mark this in the eeprom that a UniqueID is used on first start up for Pico's MFeeprom.write_block(MEM_OFFSET_SERIAL, "ID", 2); #endif diff --git a/src/MF_Analog/Analog.cpp b/src/MF_Analog/Analog.cpp index 70a186cb..f4bd2efa 100644 --- a/src/MF_Analog/Analog.cpp +++ b/src/MF_Analog/Analog.cpp @@ -14,7 +14,7 @@ namespace Analog MFAnalog *analog[MAX_ANALOG_INPUTS]; uint8_t analogRegistered = 0; - void handlerOnAnalogChange(int value, uint8_t pin, const char *name) + void handlerOnAnalogChange(int value, const char *name) { cmdMessenger.sendCmdStart(kAnalogChange); cmdMessenger.sendCmdArg(name); diff --git a/src/MF_Analog/MFAnalog.cpp b/src/MF_Analog/MFAnalog.cpp index 473c6700..b86eed36 100644 --- a/src/MF_Analog/MFAnalog.cpp +++ b/src/MF_Analog/MFAnalog.cpp @@ -33,7 +33,7 @@ void MFAnalog::readChannel(uint8_t alwaysTrigger) if (alwaysTrigger || valueHasChanged(newValue)) { _lastValue = newValue; if (_handler != NULL) { - (*_handler)(_lastValue, _pin, _name); + (*_handler)(_lastValue, _name); } } } diff --git a/src/MF_Analog/MFAnalog.h b/src/MF_Analog/MFAnalog.h index aa29ed42..e97a51bd 100644 --- a/src/MF_Analog/MFAnalog.h +++ b/src/MF_Analog/MFAnalog.h @@ -16,7 +16,7 @@ extern "C" { // callback functions -typedef void (*analogEvent)(int, uint8_t, const char *); +typedef void (*analogEvent)(int, const char *); }; ///////////////////////////////////////////////////////////////////// diff --git a/src/MF_Button/Button.cpp b/src/MF_Button/Button.cpp index 8c767882..6a4b6cfd 100644 --- a/src/MF_Button/Button.cpp +++ b/src/MF_Button/Button.cpp @@ -13,7 +13,7 @@ namespace Button MFButton *buttons[MAX_BUTTONS]; uint8_t buttonsRegistered = 0; - void handlerOnButton(uint8_t eventId, uint8_t pin, const char *name) + void handlerOnButton(uint8_t eventId, const char *name) { cmdMessenger.sendCmdStart(kButtonChange); cmdMessenger.sendCmdArg(name); diff --git a/src/MF_Button/MFButton.cpp b/src/MF_Button/MFButton.cpp index 1a80e17e..dea8b61a 100644 --- a/src/MF_Button/MFButton.cpp +++ b/src/MF_Button/MFButton.cpp @@ -33,14 +33,14 @@ void MFButton::trigger(uint8_t state) void MFButton::triggerOnPress() { if (_handler && _state == LOW) { - (*_handler)(btnOnPress, _pin, _name); + (*_handler)(btnOnPress, _name); } } void MFButton::triggerOnRelease() { if (_handler && _state == HIGH) { - (*_handler)(btnOnRelease, _pin, _name); + (*_handler)(btnOnRelease, _name); } } diff --git a/src/MF_Button/MFButton.h b/src/MF_Button/MFButton.h index 87386e20..9711ef31 100644 --- a/src/MF_Button/MFButton.h +++ b/src/MF_Button/MFButton.h @@ -10,7 +10,7 @@ extern "C" { // callback functions always follow the signature: void cmd(void); -typedef void (*buttonEvent)(byte, uint8_t, const char *); +typedef void (*buttonEvent)(uint8_t, const char *); }; enum { diff --git a/src/MF_Encoder/Encoder.cpp b/src/MF_Encoder/Encoder.cpp index 46edd649..5a34e25a 100644 --- a/src/MF_Encoder/Encoder.cpp +++ b/src/MF_Encoder/Encoder.cpp @@ -13,7 +13,7 @@ namespace Encoder MFEncoder *encoders[MAX_ENCODERS]; uint8_t encodersRegistered = 0; - void handlerOnEncoder(uint8_t eventId, uint8_t pin, const char *name) + void handlerOnEncoder(uint8_t eventId, const char *name) { cmdMessenger.sendCmdStart(kEncoderChange); cmdMessenger.sendCmdArg(name); diff --git a/src/MF_Encoder/MFEncoder.cpp b/src/MF_Encoder/MFEncoder.cpp index 590d220f..e5cf2b05 100644 --- a/src/MF_Encoder/MFEncoder.cpp +++ b/src/MF_Encoder/MFEncoder.cpp @@ -103,16 +103,16 @@ void MFEncoder::update() if (abs(delta) < (MF_ENC_FAST_LIMIT)) { // slow turn detected if (dir) { - (*_handler)(encLeft, _pin1, _name); + (*_handler)(encLeft, _name); } else { - (*_handler)(encRight, _pin2, _name); + (*_handler)(encRight, _name); } } else { // fast turn detected if (dir) { - (*_handler)(encLeftFast, _pin1, _name); + (*_handler)(encLeftFast, _name); } else { - (*_handler)(encRightFast, _pin2, _name); + (*_handler)(encRightFast, _name); } } } diff --git a/src/MF_Encoder/MFEncoder.h b/src/MF_Encoder/MFEncoder.h index 0cb0db39..6ad8a80b 100644 --- a/src/MF_Encoder/MFEncoder.h +++ b/src/MF_Encoder/MFEncoder.h @@ -20,7 +20,7 @@ #include extern "C" { -typedef void (*encoderEvent)(uint8_t, uint8_t, const char *); +typedef void (*encoderEvent)(uint8_t, const char *); }; // this prevents the internal position overflow. diff --git a/src/MF_Segment/MFSegments.cpp b/src/MF_Segment/MFSegments.cpp index 8d837388..c9575e17 100644 --- a/src/MF_Segment/MFSegments.cpp +++ b/src/MF_Segment/MFSegments.cpp @@ -11,12 +11,12 @@ MFSegments::MFSegments() _moduleCount = 0; } -void MFSegments::display(byte module, char *string, byte points, byte mask, bool convertPoints) +void MFSegments::display(uint8_t module, char *string, uint8_t points, uint8_t mask, bool convertPoints) { if (_moduleCount == 0) return; - byte digit = 8; - byte pos = 0; + uint8_t digit = 8; + uint8_t pos = 0; for (uint8_t i = 0; i < 8; i++) { digit--; if (((1 << digit) & mask) == 0) @@ -26,7 +26,7 @@ void MFSegments::display(byte module, char *string, byte points, byte mask, bool } } -void MFSegments::setBrightness(byte module, byte value) +void MFSegments::setBrightness(uint8_t module, uint8_t value) { if (_moduleCount == 0) return; @@ -40,7 +40,7 @@ void MFSegments::setBrightness(byte module, byte value) } } -void MFSegments::attach(byte type, int dataPin, int csPin, int clkPin, byte moduleCount, byte brightness) +void MFSegments::attach(uint8_t type, uint8_t dataPin, uint8_t csPin, uint8_t clkPin, uint8_t moduleCount, uint8_t brightness) { _ledControl.begin(type, dataPin, clkPin, csPin, moduleCount); _moduleCount = moduleCount; @@ -58,7 +58,7 @@ void MFSegments::detach() void MFSegments::powerSavingMode(bool state) { - for (byte i = 0; i != _moduleCount; ++i) { + for (uint8_t i = 0; i != _moduleCount; ++i) { _ledControl.shutdown(i, state); } } @@ -67,9 +67,9 @@ void MFSegments::test() { if (_moduleCount == 0) return; - byte _delay = 10; - byte module = 0; - byte digit = 0; + uint8_t _delay = 10; + uint8_t module = 0; + uint8_t digit = 0; for (digit = 0; digit < 8; digit++) { for (module = 0; module != _moduleCount; ++module) { diff --git a/src/MF_Segment/MFSegments.h b/src/MF_Segment/MFSegments.h index a76233ed..81f4ca14 100644 --- a/src/MF_Segment/MFSegments.h +++ b/src/MF_Segment/MFSegments.h @@ -13,16 +13,16 @@ class MFSegments { public: MFSegments(); - void display(byte module, char *string, byte points, byte mask, bool convertPoints = false); - void attach(byte type, int dataPin, int csPin, int clkPin, byte moduleCount, byte brightness); + void display(uint8_t module, char *string, uint8_t points, uint8_t mask, bool convertPoints = false); + void attach(uint8_t type, uint8_t dataPin, uint8_t csPin, uint8_t clkPin, uint8_t moduleCount, uint8_t brightness); void detach(); void test(); void powerSavingMode(bool state); - void setBrightness(byte module, byte value); + void setBrightness(uint8_t module, uint8_t value); private: LedControl _ledControl; - byte _moduleCount; + uint8_t _moduleCount; }; // MFSegments.h diff --git a/src/MF_Servo/Servos.cpp b/src/MF_Servo/Servos.cpp index b3530d9a..dc05ea49 100644 --- a/src/MF_Servo/Servos.cpp +++ b/src/MF_Servo/Servos.cpp @@ -13,7 +13,7 @@ namespace Servos MFServo *servos[MAX_MFSERVOS]; uint8_t servosRegistered = 0; - void Add(int pin) + void Add(uint8_t pin) { if (servosRegistered == MAX_MFSERVOS) return; diff --git a/src/MF_Servo/Servos.h b/src/MF_Servo/Servos.h index 9b6bbd5b..58dc6ccb 100644 --- a/src/MF_Servo/Servos.h +++ b/src/MF_Servo/Servos.h @@ -8,7 +8,7 @@ namespace Servos { - void Add(int pin); + void Add(uint8_t pin); void Clear(); void OnSet(); void update(); diff --git a/src/mobiflight.cpp b/src/mobiflight.cpp index 737df09a..0b2feeff 100644 --- a/src/mobiflight.cpp +++ b/src/mobiflight.cpp @@ -9,7 +9,6 @@ #include "Button.h" #include "Encoder.h" #include "MFEEPROM.h" -#include "ArduinoUniqueID.h" #if MF_ANALOG_SUPPORT == 1 #include "Analog.h" #endif