Skip to content

Commit

Permalink
TM1637 memory and code structure improvements (#261)
Browse files Browse the repository at this point in the history
* Refactored and improved memory
  • Loading branch information
DocMoebiuz authored Oct 21, 2023
1 parent 426f428 commit 2a4a43c
Show file tree
Hide file tree
Showing 8 changed files with 230 additions and 249 deletions.
120 changes: 58 additions & 62 deletions src/Config.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ bool readConfigLength()

while (MFeeprom.read_byte(addreeprom++) != 0x00) {
configLength++;
if (addreeprom > length)
{
if (addreeprom > length) {
cmdMessenger.sendCmd(kStatus, F("Loading config failed")); // text or "-1" like config upload?
return false;
}
Expand Down Expand Up @@ -209,8 +208,8 @@ bool readNameFromEEPROM(uint16_t *addreeprom, char *buffer, uint16_t *addrbuffer
if (*addrbuffer >= MEMLEN_NAMES_BUFFER) { // nameBuffer will be exceeded
return false; // abort copying from EEPROM to nameBuffer
}
} while (temp != ':'); // reads until limiter ':' and locates the next free buffer position
buffer[(*addrbuffer) - 1] = 0x00; // replace ':' by NULL, terminates the string
} while (temp != ':'); // reads until limiter ':' and locates the next free buffer position
buffer[(*addrbuffer) - 1] = 0x00; // replace ':' by NULL, terminates the string
return true;
}

Expand All @@ -223,13 +222,13 @@ bool readEndCommandFromEEPROM(uint16_t *addreeprom)
temp = MFeeprom.read_byte((*addreeprom)++);
if (*addreeprom > length) // abort if EEPROM size will be exceeded
return false;
} while (temp != ':'); // reads until limiter ':'
} while (temp != ':'); // reads until limiter ':'
return true;
}

void readConfig()
{
if (configLength == 0) // do nothing if no config is available
if (configLength == 0) // do nothing if no config is available
return;
uint16_t addreeprom = MEM_OFFSET_CONFIG; // define first memory location where config is saved in EEPROM
uint16_t addrbuffer = 0; // and start with first memory location from nameBuffer
Expand All @@ -238,7 +237,7 @@ void readConfig()
bool copy_success = true; // will be set to false if copying input names to nameBuffer exceeds array dimensions
// not required anymore when pins instead of names are transferred to the UI

if (command == 0) // just to be sure, configLength should also be 0
if (command == 0) // just to be sure, configLength should also be 0
return;

do // go through the EEPROM until it is NULL terminated
Expand All @@ -251,54 +250,55 @@ void readConfig()
break;

case kTypeOutput:
params[0] = readUintFromEEPROM(&addreeprom); // Pin number
params[0] = readUintFromEEPROM(&addreeprom); // Pin number
Output::Add(params[0]);
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;

#if MF_SEGMENT_SUPPORT == 1
case kTypeLedSegment:
params[0] = readUintFromEEPROM(&addreeprom); // Pin Data number
params[1] = readUintFromEEPROM(&addreeprom); // Pin CS number
params[2] = readUintFromEEPROM(&addreeprom); // Pin CLK number
params[3] = readUintFromEEPROM(&addreeprom); // brightness
params[4] = readUintFromEEPROM(&addreeprom); // number of modules
LedSegment::Add(params[0], params[1], params[2], params[4], params[3]);
// this is for backwards compatibility
case kTypeLedSegmentDeprecated:
// this is the new type
case kTypeLedSegmentMulti:
params[0] = LedSegment::TYPE_MAX72XX;
if (command == kTypeLedSegmentMulti)
params[0] = readUintFromEEPROM(&addreeprom); // Type of LedSegment

params[1] = readUintFromEEPROM(&addreeprom); // Pin Data number
params[2] = readUintFromEEPROM(&addreeprom); // Pin CS number
params[3] = readUintFromEEPROM(&addreeprom); // Pin CLK number
params[4] = readUintFromEEPROM(&addreeprom); // brightness
params[5] = readUintFromEEPROM(&addreeprom); // number of modules
LedSegment::Add(params[0], params[1], params[2], params[3], params[5], params[4]);
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;
#endif

#if MF_STEPPER_SUPPORT == 1
case kTypeStepperDeprecated1:
// this is for backwards compatibility
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
params[2] = readUintFromEEPROM(&addreeprom); // Pin3 number
params[3] = readUintFromEEPROM(&addreeprom); // Pin4 number
params[4] = readUintFromEEPROM(&addreeprom); // Button number
Stepper::Add(params[0], params[1], params[2], params[3], 0);
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;

case kTypeStepperDeprecated2:
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
params[2] = readUintFromEEPROM(&addreeprom); // Pin3 number
params[3] = readUintFromEEPROM(&addreeprom); // Pin4 number
params[4] = readUintFromEEPROM(&addreeprom); // Button number
Stepper::Add(params[0], params[1], params[2], params[3], params[4]);
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;

case kTypeStepper:
// Values for all stepper types
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
params[2] = readUintFromEEPROM(&addreeprom); // Pin3 number
params[3] = readUintFromEEPROM(&addreeprom); // Pin4 number
params[4] = readUintFromEEPROM(&addreeprom); // Button number
params[5] = readUintFromEEPROM(&addreeprom); // Stepper Mode
params[6] = readUintFromEEPROM(&addreeprom); // backlash
params[7] = readUintFromEEPROM(&addreeprom); // deactivate output

// Default values for older types
params[4] = (uint8_t)0; // Button number
params[5] = (uint8_t)0; // Stepper Mode
params[6] = (uint8_t)0; // backlash
params[7] = false; // deactivate output

if (command == kTypeStepperDeprecated2) {
params[4] = readUintFromEEPROM(&addreeprom); // Button number
}

if (command == kTypeStepper) {
params[5] = readUintFromEEPROM(&addreeprom); // Stepper Mode
params[6] = readUintFromEEPROM(&addreeprom); // backlash
params[7] = readUintFromEEPROM(&addreeprom); // deactivate output
}
// there is an additional 9th parameter stored in the config (profileID) which is not needed in the firmware
// and therefor not read in, it is just skipped like the name with reading until end of command
Stepper::Add(params[0], params[1], params[2], params[3], params[4], params[5], params[6], params[7]);
Expand All @@ -308,34 +308,30 @@ void readConfig()

#if MF_SERVO_SUPPORT == 1
case kTypeServo:
params[0] = readUintFromEEPROM(&addreeprom); // Pin number
params[0] = readUintFromEEPROM(&addreeprom); // Pin number
Servos::Add(params[0]);
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;
#endif

case kTypeEncoderSingleDetent:
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
Encoder::Add(params[0], params[1], 0, &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name
copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to nameBuffer and set to next free memory location
// copy_success = readEndCommandFromEEPROM(&addreeprom); // once the nameBuffer is not required anymore uncomment this line and delete the line before
break;

case kTypeEncoder:
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
params[2] = readUintFromEEPROM(&addreeprom); // type
params[0] = readUintFromEEPROM(&addreeprom); // Pin1 number
params[1] = readUintFromEEPROM(&addreeprom); // Pin2 number
params[2] = 0; // type

if (command == kTypeEncoder)
params[2] = readUintFromEEPROM(&addreeprom); // type

Encoder::Add(params[0], params[1], params[2], &nameBuffer[addrbuffer]); // MUST be before readNameFromEEPROM because readNameFromEEPROM returns the pointer for the NEXT Name
copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location
// copy_success = readEndCommandFromEEPROM(&addreeprom); // once the nameBuffer is not required anymore uncomment this line and delete the line before
copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to nameBuffer and set to next free memory location
break;

#if MF_LCD_SUPPORT == 1
case kTypeLcdDisplayI2C:
params[0] = readUintFromEEPROM(&addreeprom); // address
params[1] = readUintFromEEPROM(&addreeprom); // columns
params[2] = readUintFromEEPROM(&addreeprom); // lines
params[0] = readUintFromEEPROM(&addreeprom); // address
params[1] = readUintFromEEPROM(&addreeprom); // columns
params[2] = readUintFromEEPROM(&addreeprom); // lines
LCDDisplay::Add(params[0], params[1], params[2]);
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;
Expand All @@ -353,21 +349,21 @@ void readConfig()

#if MF_OUTPUT_SHIFTER_SUPPORT == 1
case kTypeOutputShifter:
params[0] = readUintFromEEPROM(&addreeprom); // latch Pin
params[1] = readUintFromEEPROM(&addreeprom); // clock Pin
params[2] = readUintFromEEPROM(&addreeprom); // data Pin
params[3] = readUintFromEEPROM(&addreeprom); // number of daisy chained modules
params[0] = readUintFromEEPROM(&addreeprom); // latch Pin
params[1] = readUintFromEEPROM(&addreeprom); // clock Pin
params[2] = readUintFromEEPROM(&addreeprom); // data Pin
params[3] = readUintFromEEPROM(&addreeprom); // number of daisy chained modules
OutputShifter::Add(params[0], params[1], params[2], params[3]);
copy_success = readEndCommandFromEEPROM(&addreeprom); // check EEPROM until end of name
break;
#endif

#if MF_INPUT_SHIFTER_SUPPORT == 1
case kTypeInputShifter:
params[0] = readUintFromEEPROM(&addreeprom); // latch Pin
params[1] = readUintFromEEPROM(&addreeprom); // clock Pin
params[2] = readUintFromEEPROM(&addreeprom); // data Pin
params[3] = readUintFromEEPROM(&addreeprom); // number of daisy chained modules
params[0] = readUintFromEEPROM(&addreeprom); // latch Pin
params[1] = readUintFromEEPROM(&addreeprom); // clock Pin
params[2] = readUintFromEEPROM(&addreeprom); // data Pin
params[3] = readUintFromEEPROM(&addreeprom); // number of daisy chained modules
InputShifter::Add(params[0], params[1], params[2], params[3], &nameBuffer[addrbuffer]);
copy_success = readNameFromEEPROM(&addreeprom, nameBuffer, &addrbuffer); // copy the NULL terminated name to to nameBuffer and set to next free memory location
// copy_success = readEndCommandFromEEPROM(&addreeprom); // once the nameBuffer is not required anymore uncomment this line and delete the line before
Expand Down
Loading

0 comments on commit 2a4a43c

Please sign in to comment.