Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flash optimizations #260

Merged
merged 10 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CustomDevices
Submodule CustomDevices added at dbf727
3 changes: 2 additions & 1 deletion platformio.ini
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ lib_deps =
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
;ricaun/ArduinoUniqueID @ ^1.3.0
custom_lib_deps_Atmel =
arduino-libraries/Servo @ 1.1.8
build_flags =
Expand Down Expand Up @@ -144,6 +144,7 @@ build_src_filter =
+<../_Boards/RaspberryPi>
lib_deps =
${env.lib_deps}
ricaun/ArduinoUniqueID @ ^1.3.0
monitor_speed = 115200
extra_scripts =
${env.extra_scripts}
56 changes: 43 additions & 13 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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';
elral marked this conversation as resolved.
Show resolved Hide resolved
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)
{
Expand All @@ -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)
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Analog/Analog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Analog/MFAnalog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Analog/MFAnalog.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

extern "C" {
// callback functions
typedef void (*analogEvent)(int, uint8_t, const char *);
typedef void (*analogEvent)(int, const char *);
};

/////////////////////////////////////////////////////////////////////
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Button/Button.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/MF_Button/MFButton.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
2 changes: 1 addition & 1 deletion src/MF_Button/MFButton.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)(byte, const char *);
elral marked this conversation as resolved.
Show resolved Hide resolved
};

enum {
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Encoder/Encoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
8 changes: 4 additions & 4 deletions src/MF_Encoder/MFEncoder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Encoder/MFEncoder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include <Arduino.h>

extern "C" {
typedef void (*encoderEvent)(uint8_t, uint8_t, const char *);
typedef void (*encoderEvent)(uint8_t, const char *);
};

// this prevents the internal position overflow.
Expand Down
4 changes: 2 additions & 2 deletions src/MF_Segment/MFSegments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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)
elral marked this conversation as resolved.
Show resolved Hide resolved
{
if (_moduleCount == 0)
return;
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Segment/MFSegments.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ class MFSegments
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;
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Servo/Servos.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/MF_Servo/Servos.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

namespace Servos
{
void Add(int pin);
void Add(uint8_t pin);
void Clear();
void OnSet();
void update();
Expand Down
1 change: 0 additions & 1 deletion src/mobiflight.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading