From cef1f7d56396f5581e703073c709100eb336f212 Mon Sep 17 00:00:00 2001 From: DocMoebiuz Date: Fri, 20 Oct 2023 11:50:08 +0200 Subject: [PATCH 1/5] Refactored and improved memory --- src/Config.cpp | 100 ++++++++++++------------ src/MF_Segment/LedControl_dual.cpp | 117 +++++++++++++++-------------- src/MF_Segment/LedControl_dual.h | 93 ++++++++++++----------- src/MF_Segment/LedSegment.cpp | 4 +- src/MF_Segment/LedSegment.h | 9 ++- src/MF_Segment/MFSegments.cpp | 4 +- src/MF_Segment/MFSegments.h | 2 +- src/config.h | 33 ++++---- 8 files changed, 189 insertions(+), 173 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 0445483e..2824815d 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -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; } @@ -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; } @@ -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 @@ -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 @@ -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]); @@ -308,7 +308,7 @@ 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; @@ -333,9 +333,9 @@ void readConfig() #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; @@ -353,10 +353,10 @@ 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; @@ -364,10 +364,10 @@ void readConfig() #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 diff --git a/src/MF_Segment/LedControl_dual.cpp b/src/MF_Segment/LedControl_dual.cpp index e6926c1e..77c8af21 100644 --- a/src/MF_Segment/LedControl_dual.cpp +++ b/src/MF_Segment/LedControl_dual.cpp @@ -89,7 +89,7 @@ enum { }; #ifdef LEDCONTROL_NO_BUF - uint8_t LedControl::rawdata[16] = {0}; +uint8_t LedControl::rawdata[16] = {0}; #endif // ======================================================================= @@ -139,10 +139,10 @@ enum { // 1 0 0 0 0 _ _ _ - Display OFF // 1 0 0 0 1 _ _ _ - Display ON -#define TM1637_I2C_COMM1 0x40 // CmdSetData 0b01000000 -#define TM1637_I2C_COMM2 0xC0 // CmdSetAddress 0b11000000 -#define TM1637_I2C_COMM3 0x80 // CmdDisplay 0b10000000 -#define TM1637_I2C_COMM1F 0x44 // CmdSetData - fixedAddress 0b11000100 +#define TM1637_I2C_COMM1 0x40 // CmdSetData 0b01000000 +#define TM1637_I2C_COMM2 0xC0 // CmdSetAddress 0b11000000 +#define TM1637_I2C_COMM3 0x80 // CmdDisplay 0b10000000 +#define TM1637_I2C_COMM1F 0x44 // CmdSetData - fixedAddress 0b11000100 // ======================================================================= // Digit sequence map for 6 digit displays @@ -153,11 +153,12 @@ const uint8_t digitmap[] = {2, 1, 0, 5, 4, 3}; // csPin = 0xFD -> TM1637 4-digit // csPin = 0xFE -> TM1637 6-digit // csPin = 0xFF -> uninitialized -void LedControl::begin(uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices) +void LedControl::begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices) { - IO_DTA = dataPin; - IO_CLK = clkPin; - IO_CS = csPin; + this->type = type; + IO_DTA = dataPin; + IO_CLK = clkPin; + IO_CS = csPin; if (isMAX()) { if ((numDevices - 1) > 7) numDevices = 8; maxUnits = numDevices; @@ -170,7 +171,7 @@ void LedControl::begin(uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t n setScanLimit(i, 7); // scanlimit is set to max on startup spiTransfer(i, OP_DECODEMODE, 0); // decode is done in source clearDisplay(i); - shutdown(i, true); // we go into shutdown-mode on startup + shutdown(i, true); // we go into shutdown-mode on startup } } else { maxUnits = (IO_CS == 0xFD ? 4 : 6); @@ -180,7 +181,7 @@ void LedControl::begin(uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t n digitalWrite(IO_CLK, LOW); // Prepare '0' value as dominant digitalWrite(IO_DTA, LOW); // Prepare '0' value as dominant clearDisplay(0); - //setIntensity(0, MAX_BRIGHTNESS); + // setIntensity(0, MAX_BRIGHTNESS); brightness = MAX_BRIGHTNESS; shutdown(0, true); } @@ -193,7 +194,7 @@ void LedControl::shutdown(uint8_t addr, bool b) spiTransfer(addr, OP_SHUTDOWN, b ? 0 : 1); } else { uint8_t bri = brightness >> 1; - if(!b) bri |= 0x08; + if (!b) bri |= 0x08; // Write COMM3 + intensity start(); writeByte(TM1637_I2C_COMM3 + bri); @@ -209,8 +210,8 @@ void LedControl::setIntensity(uint8_t addr, uint8_t intensity) if (addr >= maxUnits) return; spiTransfer(addr, OP_INTENSITY, brightness); } else { - if(intensity > 0) { - if(intensity > 1) intensity >>= 1; + if (intensity > 0) { + if (intensity > 1) intensity >>= 1; intensity |= 0x08; } // Write COMM3 + intensity @@ -229,7 +230,7 @@ void LedControl::clearDisplay(uint8_t addr) } } else { #ifdef LEDCONTROL_NO_BUF - for(uint8_t i = 0; i < 8; i++) { + for (uint8_t i = 0; i < 8; i++) { writeOneDigit(i, 0); } #else @@ -242,7 +243,7 @@ void LedControl::clearDisplay(uint8_t addr) void LedControl::setDigit(uint8_t addr, uint8_t digit, uint8_t value, bool dp, bool sendNow) { if (addr >= maxUnits) return; - if ((value > 15) && (value != '-')) value = (uint8_t)' '; // Use space for invalid digit + if ((value > 15) && (value != '-')) value = (uint8_t)' '; // Use space for invalid digit if (dp) value |= 0x80; setPattern(addr, digit, value, sendNow); } @@ -259,12 +260,12 @@ void LedControl::setChar(uint8_t addr, uint8_t digit, char value, bool dp, bool void LedControl::setPattern(uint8_t addr, uint8_t digit, uint8_t value, bool sendNow) { - if(digit > getDigitCount()-1) return; + if (digit > getDigitCount() - 1) return; uint8_t v; v = pgm_read_byte_near(charTable + (value & 0x7F)); if (isMAX()) { if (value & 0x80) v |= 0x80; - spiTransfer(addr, digit + 1, v); // Always send immediately for MAX + spiTransfer(addr, digit + 1, v); // Always send immediately for MAX } else { // Original data for MAX has the bit sequence: dABCDEFG // Common TM1637 boards are connected so that they require: dGFEDCBA @@ -275,8 +276,8 @@ void LedControl::setPattern(uint8_t addr, uint8_t digit, uint8_t value, bool sen #ifdef LEDCONTROL_NO_BUF writeOneDigit(digit, v); #else - rawdata[(maxUnits-1) - digit] = v; // Change only the individual affected digit in static buffer - if(sendNow) { + rawdata[(maxUnits - 1) - digit] = v; // Change only the individual affected digit in static buffer + if (sendNow) { writeDigits(digit, 1); } #endif @@ -287,11 +288,12 @@ void LedControl::setPattern(uint8_t addr, uint8_t digit, uint8_t value, bool sen // MAX-specific driver methods // ------------------------------------------------ -void LedControl::setScanLimit(uint8_t addr, uint8_t limit) { - if(!isMAX()) return; - if(addr>=maxUnits) return; - if(limit > 7) return; - spiTransfer(addr, OP_SCANLIMIT,limit); +void LedControl::setScanLimit(uint8_t addr, uint8_t limit) +{ + if (!isMAX()) return; + if (addr >= maxUnits) return; + if (limit > 7) return; + spiTransfer(addr, OP_SCANLIMIT, limit); } void LedControl::spiTransfer(uint8_t addr, uint8_t opcode, uint8_t data) @@ -371,17 +373,17 @@ bool LedControl::writeByte(uint8_t data, bool rvs) #ifdef LEDCONTROL_NO_BUF -void LedControl::writeOneDigit(uint8_t ndigit, uint8_t pattern) +void LedControl::writeOneDigit(uint8_t ndigit, uint8_t pattern) { uint8_t b; // Write COMM1 start(); - writeByte(TM1637_I2C_COMM1F); // TM1637_I2C_COMM1 is also fine + writeByte(TM1637_I2C_COMM1F); // TM1637_I2C_COMM1 is also fine stop(); start(); - ndigit = (maxUnits-1)-ndigit; - b = ((maxUnits == 4) ? ndigit : digitmap[ndigit]); + ndigit = (maxUnits - 1) - ndigit; + b = ((maxUnits == 4) ? ndigit : digitmap[ndigit]); writeByte(TM1637_I2C_COMM2 + b); // Write only raw data bit-reversed (to use the existing data in MAX-format) writeByte(pattern, true); @@ -398,7 +400,7 @@ void LedControl::writeOneDigit(uint8_t ndigit, uint8_t pattern) void LedControl::writeDigits(uint8_t startd, uint8_t len) { - bool is4Digit = (maxUnits == 4); + bool is4Digit = (maxUnits == 4); uint8_t b; // Write COMM1 @@ -406,17 +408,17 @@ void LedControl::writeDigits(uint8_t startd, uint8_t len) writeByte(TM1637_I2C_COMM1); stop(); - uint8_t pos = (maxUnits-1) - startd; - b = (is4Digit ? pos : digitmap[pos+len-1]); + uint8_t pos = (maxUnits - 1) - startd; + b = (is4Digit ? pos : digitmap[pos + len - 1]); start(); writeByte(TM1637_I2C_COMM2 + b); // Write the data bytes - if(pos + len > maxUnits) len = maxUnits - pos; + if (pos + len > maxUnits) len = maxUnits - pos; uint8_t k; for (b = 0; b < len; b++) { k = (is4Digit ? b : len - b - 1); - writeByte(rawdata[pos+k], true); + writeByte(rawdata[pos + k], true); } stop(); } @@ -429,15 +431,16 @@ void LedControl::showNumber(uint8_t addr, int32_t num, bool isHex, uint8_t dots, { uint8_t digits[8]; uint8_t pos; - uint8_t maxlen = getDigitCount(); - bool minusRequired = (num < 0); - if(minusRequired) num = -num; + uint8_t maxlen = getDigitCount(); + bool minusRequired = (num < 0); + if (minusRequired) num = -num; - for (uint8_t i = 0; i < 8; i++) digits[i] = (uint8_t)' '; + for (uint8_t i = 0; i < 8; i++) + digits[i] = (uint8_t)' '; - pos = (maxlen-1)-roffset; // Reverse to use as counter + pos = (maxlen - 1) - roffset; // Reverse to use as counter if (num == 0) { - if(leading_zero) { + if (leading_zero) { for (uint8_t i = 0; i < pos; i++) { digits[i] = '0'; } @@ -447,7 +450,7 @@ void LedControl::showNumber(uint8_t addr, int32_t num, bool isHex, uint8_t dots, uint8_t dval; do { if (num == 0) { - if(leading_zero) { + if (leading_zero) { digits[pos] = '0'; if (minusRequired && pos == 0) digits[0] = '-'; } else { @@ -455,36 +458,36 @@ void LedControl::showNumber(uint8_t addr, int32_t num, bool isHex, uint8_t dots, pos = 0; } } else { - if(!isHex) { - dval = num % 10; - digits[pos] = '0'+dval; + if (!isHex) { + dval = num % 10; + digits[pos] = '0' + dval; } else { - dval = num & 0x0F; - digits[pos] = ((dval > 9) ? 'A'-10 : '0') + dval; + dval = num & 0x0F; + digits[pos] = ((dval > 9) ? 'A' - 10 : '0') + dval; } } - if(!isHex) { + if (!isHex) { num /= 10; } else { num >>= 4; } - } while((pos--) > 0); + } while ((pos--) > 0); } - showString(addr, (char*)digits, 0, dots); + showString(addr, (char *)digits, 0, dots); } -void LedControl::showString(uint8_t addr, char* s, uint8_t loffset, uint8_t dots) -{ +void LedControl::showString(uint8_t addr, char *s, uint8_t loffset, uint8_t dots) +{ uint8_t maxlen = getDigitCount(); - uint8_t msk = 0x80 >> loffset; - for(uint8_t d = loffset; d < maxlen && (*s != 0); d++) { - uint8_t pos = (maxlen-1)-d; - setChar(addr, pos, *s++, ((dots & msk)!=0), false); - msk>>=1; + uint8_t msk = 0x80 >> loffset; + for (uint8_t d = loffset; d < maxlen && (*s != 0); d++) { + uint8_t pos = (maxlen - 1) - d; + setChar(addr, pos, *s++, ((dots & msk) != 0), false); + msk >>= 1; } #ifndef LEDCONTROL_NO_BUF - if(!isMAX()) writeBuffer(); + if (!isMAX()) writeBuffer(); #endif } diff --git a/src/MF_Segment/LedControl_dual.h b/src/MF_Segment/LedControl_dual.h index 076ecfe6..1e95f62c 100644 --- a/src/MF_Segment/LedControl_dual.h +++ b/src/MF_Segment/LedControl_dual.h @@ -33,12 +33,12 @@ #define __LEDCONTROL_DUAL__H__ // This constant reduces buffer usage: a single (static) 16-byte buffer -// is used for all objects. However, for TM1637s, data is written -// byte-by-byte, making transmission of data blocks slower. -//#define LEDCONTROL_NO_BUF +// is used for all objects. However, for TM1637s, data is written +// byte-by-byte, making transmission of data blocks slower. +// #define LEDCONTROL_NO_BUF // This constant adds methods to print a decimal/hex number or a string -// (as opposite to writing individual chars). +// (as opposite to writing individual chars). #define LEDCONTROL_EXTENDED #include @@ -46,12 +46,12 @@ #include #ifdef __AVR__ - #include +#include #elif defined(ESP8266) || defined(ESP32) - #include +#include #else - #define pgm_read_byte(addr) \ - (*(const unsigned char *)(addr)) // workaround for non-AVR +#define pgm_read_byte(addr) \ + (*(const unsigned char *)(addr)) // workaround for non-AVR #endif // ======================================================================= @@ -65,58 +65,64 @@ class LedControl { public: - enum { TM_4D = 0xFD, TM_6D = 0xFE, UNINITIALIZED = 0xFF}; - enum { ZERO_BRIGHTNESS = 0, MIN_BRIGHTNESS = 1, MAX_BRIGHTNESS = 15 }; + enum { TYPE_MAX72XX = 0, + TYPE_TM1637_4DIGITS = 0xFD, + TYPE_TM1637_6DIGITS = 0xFE, + TYPE_UNDEFINED = 0xFF }; + enum { ZERO_BRIGHTNESS = 0, + MIN_BRIGHTNESS = 1, + MAX_BRIGHTNESS = 15 }; private: // Common - uint8_t IO_DTA = UNINITIALIZED; - uint8_t IO_CLK = UNINITIALIZED; - uint8_t IO_CS = UNINITIALIZED; + uint8_t type = TYPE_UNDEFINED; + uint8_t IO_DTA = TYPE_UNDEFINED; + uint8_t IO_CLK = TYPE_UNDEFINED; + uint8_t IO_CS = TYPE_UNDEFINED; // MAX: Number of chained units // TM: Number of digits (4 or 6) #ifdef LEDCONTROL_NO_BUF - // For TM, buffer can't be static (= shared): either we are building - // the extended version (which adds a per-unit buffer instead of the static one) + // For TM, buffer can't be static (= shared): either we are building + // the extended version (which adds a per-unit buffer instead of the static one) // or we are forced to resort to digit-by-digit output static #endif - uint8_t rawdata[16]; - uint8_t maxUnits = 0; // MAX: N. of chained units; TM: N. of digits - uint8_t brightness = MAX_BRIGHTNESS; - void setPattern(uint8_t addr, uint8_t digit, uint8_t value, bool sendNow = true); + uint8_t rawdata[16]; + uint8_t maxUnits = 0; // MAX: N. of chained units; TM: N. of digits + uint8_t brightness = MAX_BRIGHTNESS; + void setPattern(uint8_t addr, uint8_t digit, uint8_t value, bool sendNow = true); // MAX-specific - void setScanLimit(uint8_t addr, uint8_t limit); - void spiTransfer(uint8_t addr, uint8_t opcode, uint8_t data); + void setScanLimit(uint8_t addr, uint8_t limit); + void spiTransfer(uint8_t addr, uint8_t opcode, uint8_t data); // TM-specific // uint8_t dpSet = 0; - void bitDelay() { delayMicroseconds(DEFAULT_BIT_DELAY); }; - void start(void); - void stop(void); - bool writeByte(uint8_t data, bool rvs = false); + void bitDelay() { delayMicroseconds(DEFAULT_BIT_DELAY); }; + void start(void); + void stop(void); + bool writeByte(uint8_t data, bool rvs = false); #ifdef LEDCONTROL_NO_BUF - void writeOneDigit(uint8_t ndigit, uint8_t val); + void writeOneDigit(uint8_t ndigit, uint8_t val); #else // Has buffer available - void writeDigits(uint8_t ndigit, uint8_t len); - void writeBuffer(void) { writeDigits(maxUnits-1, maxUnits); }; + void writeDigits(uint8_t ndigit, uint8_t len); + void writeBuffer(void) { writeDigits(maxUnits - 1, maxUnits); }; #endif public: - LedControl() {}; + LedControl(){}; - void begin(uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices = 1); + void begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices = 1); - bool isMAX(void) { return (IO_CS + 1) < TM_6D; } + bool isMAX(void) { return type == TYPE_MAX72XX; } uint8_t getDeviceCount(void) { return (isMAX() ? maxUnits : 1); }; uint8_t getDigitCount(void) { return (isMAX() ? 8 : maxUnits); }; - void shutdown(uint8_t addr, bool status); - void setIntensity(uint8_t addr, uint8_t intensity); - void clearDisplay(uint8_t addr = 0); + void shutdown(uint8_t addr, bool status); + void setIntensity(uint8_t addr, uint8_t intensity); + void clearDisplay(uint8_t addr = 0); // Display a hexadecimal digit. // Params: @@ -124,10 +130,10 @@ class LedControl // digit the position of the digit on the display (0 is RIGHTMOST) // value the value to be displayed. (0x00..0x0F) // dp sets the decimal point. - // sendnow If false, buffers chars rather than sending them immediately (TM only; + // sendnow If false, buffers chars rather than sending them immediately (TM only; // requires a sendAll() afterwards). // Ignored for MAX, or if LEDCONTROL_NO_BUF is defined. - void setDigit(uint8_t addr, uint8_t digit, uint8_t value, bool dp = false, bool sendNow = true); + void setDigit(uint8_t addr, uint8_t digit, uint8_t value, bool dp = false, bool sendNow = true); // Display a character. // There are only a few characters that make sense here : @@ -139,14 +145,14 @@ class LedControl // digit the position of the character on the display (0 is RIGHTMOST) // value the character to be displayed. // dp sets the decimal point. - // sendnow If false, buffers chars rather than sending them immediately (TM only; + // sendnow If false, buffers chars rather than sending them immediately (TM only; // requires a sendAll() afterwards). // Ignored for MAX, or if LEDCONTROL_NO_BUF is defined. - void setChar(uint8_t addr, uint8_t digit, char value, bool dp = false, bool sendNow = true); + void setChar(uint8_t addr, uint8_t digit, char value, bool dp = false, bool sendNow = true); #ifndef LEDCONTROL_NO_BUF // Sends the whole (previously filled) buffer content. - void sendAll(void) { writeBuffer(); }; + void sendAll(void) { writeBuffer(); }; #endif #ifdef LEDCONTROL_EXTENDED @@ -163,8 +169,8 @@ class LedControl // @param leading_zero When true, leading zeros are displayed. Otherwise unnecessary digits are // blank. // @param rstart The position of the LEAST significant digit (N-1 = leftmost, 0 = rightmost) - void showNumber(uint8_t addr, int32_t num, bool isHex = false, uint8_t dots = 0, - bool leading_zero = false, uint8_t roffset = 0); + void showNumber(uint8_t addr, int32_t num, bool isHex = false, uint8_t dots = 0, + bool leading_zero = false, uint8_t roffset = 0); // Display a string // @@ -176,11 +182,10 @@ class LedControl // @param dots Dot/Colon enable. The argument is a bitmask, with each bit corresponding to a dot // between the digits (LEFT aligned) // See showString_P function for reading PROGMEM read-only flash memory space instead of RAM - void showString(uint8_t addr, char* s, uint8_t loffset = 0, uint8_t dots = 0); - //void showString_P(uint8_t addr, const char s[], uint8_t pos = 0, uint8_t dots = 0); + void showString(uint8_t addr, char *s, uint8_t loffset = 0, uint8_t dots = 0); + // void showString_P(uint8_t addr, const char s[], uint8_t pos = 0, uint8_t dots = 0); #endif - }; #endif //!__LEDCONTROL_DUAL__H__ diff --git a/src/MF_Segment/LedSegment.cpp b/src/MF_Segment/LedSegment.cpp index 4792adc7..c0c66f0b 100644 --- a/src/MF_Segment/LedSegment.cpp +++ b/src/MF_Segment/LedSegment.cpp @@ -13,7 +13,7 @@ namespace LedSegment MFSegments *ledSegments[MAX_LEDSEGMENTS]; uint8_t ledSegmentsRegistered = 0; - void Add(int dataPin, int csPin, int clkPin, int numDevices, int brightness) + void Add(uint8_t type, uint8_t dataPin, uint8_t csPin, uint8_t clkPin, uint8_t numDevices, uint8_t brightness) { if (ledSegmentsRegistered == MAX_LEDSEGMENTS) return; @@ -24,7 +24,7 @@ namespace LedSegment return; } ledSegments[ledSegmentsRegistered] = new (allocateMemory(sizeof(MFSegments))) MFSegments; - ledSegments[ledSegmentsRegistered]->attach(dataPin, csPin, clkPin, numDevices, brightness); // lc is our object + ledSegments[ledSegmentsRegistered]->attach(type, dataPin, csPin, clkPin, numDevices, brightness); // lc is our object ledSegmentsRegistered++; #ifdef DEBUG2CMDMESSENGER cmdMessenger.sendCmd(kDebug, F("Added Led Segment")); diff --git a/src/MF_Segment/LedSegment.h b/src/MF_Segment/LedSegment.h index df89c92b..68bcc4c6 100644 --- a/src/MF_Segment/LedSegment.h +++ b/src/MF_Segment/LedSegment.h @@ -8,7 +8,14 @@ namespace LedSegment { - void Add(int dataPin, int csPin, int clkPin, int numDevices, int brightness); + enum { + TYPE_MAX72XX = 0, + TYPE_TM1637_4DIGITS = 0xFD, + TYPE_TM1637_6DIGITS = 0xFE, + TYPE_UNDEFINED = 0xFF + }; + + void Add(uint8_t type, uint8_t dataPin, uint8_t csPin, uint8_t clkPin, uint8_t numDevices, uint8_t brightness); void Clear(); void PowerSave(bool state); void OnInitModule(); diff --git a/src/MF_Segment/MFSegments.cpp b/src/MF_Segment/MFSegments.cpp index 1d4777e5..8d837388 100644 --- a/src/MF_Segment/MFSegments.cpp +++ b/src/MF_Segment/MFSegments.cpp @@ -40,9 +40,9 @@ void MFSegments::setBrightness(byte module, byte value) } } -void MFSegments::attach(int dataPin, int csPin, int clkPin, byte moduleCount, byte brightness) +void MFSegments::attach(byte type, int dataPin, int csPin, int clkPin, byte moduleCount, byte brightness) { - _ledControl.begin(dataPin, clkPin, csPin, moduleCount); + _ledControl.begin(type, dataPin, clkPin, csPin, moduleCount); _moduleCount = moduleCount; for (uint8_t i = 0; i < _moduleCount; ++i) { setBrightness(i, brightness); diff --git a/src/MF_Segment/MFSegments.h b/src/MF_Segment/MFSegments.h index 4e6dd92e..a76233ed 100644 --- a/src/MF_Segment/MFSegments.h +++ b/src/MF_Segment/MFSegments.h @@ -14,7 +14,7 @@ class MFSegments public: MFSegments(); void display(byte module, char *string, byte points, byte mask, bool convertPoints = false); - void attach(int dataPin, int csPin, int clkPin, byte moduleCount, byte brightness); + void attach(byte type, int dataPin, int csPin, int clkPin, byte moduleCount, byte brightness); void detach(); void test(); void powerSavingMode(bool state); diff --git a/src/config.h b/src/config.h index a882abad..8eb2e976 100644 --- a/src/config.h +++ b/src/config.h @@ -7,22 +7,23 @@ #pragma once enum { - kTypeNotSet, // 0 - kTypeButton, // 1 - kTypeEncoderSingleDetent, // 2 (retained for backwards compatibility, use kTypeEncoder for new configs) - kTypeOutput, // 3 - kTypeLedSegment, // 4 - kTypeStepperDeprecated1, // 5 (keep for backwards compatibility, doesn't support autohome) - kTypeServo, // 6 - kTypeLcdDisplayI2C, // 7 - kTypeEncoder, // 8 - kTypeStepperDeprecated2, // 9 (keep for backwards compatibility, stepper type with auto zero support if btnPin is > 0) - kTypeOutputShifter, // 10 Shift register support (example: 74HC595, TLC592X) - kTypeAnalogInput, // 11 Analog Device with 1 pin - kTypeInputShifter, // 12 Input shift register support (example: 74HC165) - kTypeMuxDriver, // 13 Multiplexer selector support (generates select outputs) - kTypeDigInMux, // 14 Digital input multiplexer support (example: 74HCT4067, 74HCT4051) - kTypeStepper // new stepper type with settings for backlash and deactivate output + kTypeNotSet, // 0 + kTypeButton, // 1 + kTypeEncoderSingleDetent, // 2 (retained for backwards compatibility, use kTypeEncoder for new configs) + kTypeOutput, // 3 + kTypeLedSegmentDeprecated, // 4 (keep for backwards compatibility) + kTypeStepperDeprecated1, // 5 (keep for backwards compatibility, doesn't support autohome) + kTypeServo, // 6 + kTypeLcdDisplayI2C, // 7 + kTypeEncoder, // 8 + kTypeStepperDeprecated2, // 9 (keep for backwards compatibility, stepper type with auto zero support if btnPin is > 0) + kTypeOutputShifter, // 10 Shift register support (example: 74HC595, TLC592X) + kTypeAnalogInput, // 11 Analog Device with 1 pin + kTypeInputShifter, // 12 Input shift register support (example: 74HC165) + kTypeMuxDriver, // 13 Multiplexer selector support (generates select outputs) + kTypeDigInMux, // 14 Digital input multiplexer support (example: 74HCT4067, 74HCT4051) + kTypeStepper, // 15 new stepper type with settings for backlash and deactivate output + kTypeLedSegmentMulti // 16 new led segment with MAX7219 and TM1637 support }; void loadConfig(void); From f93aec865cb3f1307c62bdb36da254633793802e Mon Sep 17 00:00:00 2001 From: DocMoebiuz Date: Fri, 20 Oct 2023 13:04:29 +0200 Subject: [PATCH 2/5] memory improvement by converting enum to compiler define (28528) --- src/MF_Segment/LedControl_dual.cpp | 6 +----- src/MF_Segment/LedControl_dual.h | 26 +++++++++++--------------- 2 files changed, 12 insertions(+), 20 deletions(-) diff --git a/src/MF_Segment/LedControl_dual.cpp b/src/MF_Segment/LedControl_dual.cpp index 77c8af21..9bbb774b 100644 --- a/src/MF_Segment/LedControl_dual.cpp +++ b/src/MF_Segment/LedControl_dual.cpp @@ -148,17 +148,13 @@ uint8_t LedControl::rawdata[16] = {0}; // Digit sequence map for 6 digit displays const uint8_t digitmap[] = {2, 1, 0, 5, 4, 3}; -// Configuration: -// csPin < 0xFD -> MAX72xx -// csPin = 0xFD -> TM1637 4-digit -// csPin = 0xFE -> TM1637 6-digit -// csPin = 0xFF -> uninitialized void LedControl::begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices) { this->type = type; IO_DTA = dataPin; IO_CLK = clkPin; IO_CS = csPin; + if (isMAX()) { if ((numDevices - 1) > 7) numDevices = 8; maxUnits = numDevices; diff --git a/src/MF_Segment/LedControl_dual.h b/src/MF_Segment/LedControl_dual.h index 1e95f62c..c122473d 100644 --- a/src/MF_Segment/LedControl_dual.h +++ b/src/MF_Segment/LedControl_dual.h @@ -44,6 +44,7 @@ #include // #include #include +#include #ifdef __AVR__ #include @@ -59,20 +60,13 @@ // ======================================================================= #define DEFAULT_BIT_DELAY 100 +#define TYPE_UNDEFINED 0xFF +#define MAX_BRIGHTNESS 15 // ======================================================================= class LedControl { -public: - enum { TYPE_MAX72XX = 0, - TYPE_TM1637_4DIGITS = 0xFD, - TYPE_TM1637_6DIGITS = 0xFE, - TYPE_UNDEFINED = 0xFF }; - enum { ZERO_BRIGHTNESS = 0, - MIN_BRIGHTNESS = 1, - MAX_BRIGHTNESS = 15 }; - private: // Common uint8_t type = TYPE_UNDEFINED; @@ -85,12 +79,14 @@ class LedControl // For TM, buffer can't be static (= shared): either we are building // the extended version (which adds a per-unit buffer instead of the static one) // or we are forced to resort to digit-by-digit output - static + static uint8_t rawdata[16]; +#else + uint8_t rawdata[16]; #endif - uint8_t rawdata[16]; - uint8_t maxUnits = 0; // MAX: N. of chained units; TM: N. of digits - uint8_t brightness = MAX_BRIGHTNESS; - void setPattern(uint8_t addr, uint8_t digit, uint8_t value, bool sendNow = true); + + uint8_t maxUnits = 0; // MAX: N. of chained units; TM: N. of digits + uint8_t brightness = MAX_BRIGHTNESS; + void setPattern(uint8_t addr, uint8_t digit, uint8_t value, bool sendNow = true); // MAX-specific void setScanLimit(uint8_t addr, uint8_t limit); @@ -116,7 +112,7 @@ class LedControl void begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices = 1); - bool isMAX(void) { return type == TYPE_MAX72XX; } + bool isMAX(void) { return type == LedSegment::TYPE_MAX72XX; } uint8_t getDeviceCount(void) { return (isMAX() ? maxUnits : 1); }; uint8_t getDigitCount(void) { return (isMAX() ? 8 : maxUnits); }; From 248799f4716a3be6e539205e3ac9535b50281f33 Mon Sep 17 00:00:00 2001 From: DocMoebiuz Date: Fri, 20 Oct 2023 13:06:43 +0200 Subject: [PATCH 3/5] memory improvement by encoder config logic: (28422) --- src/Config.cpp | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/Config.cpp b/src/Config.cpp index 2824815d..8cf41ab5 100644 --- a/src/Config.cpp +++ b/src/Config.cpp @@ -315,20 +315,16 @@ void readConfig() #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 From 366544ac49a8428fdfe1099e25bcde4ce968c9c6 Mon Sep 17 00:00:00 2001 From: DocMoebiuz Date: Fri, 20 Oct 2023 13:16:22 +0200 Subject: [PATCH 4/5] Updated the head section of the files. --- src/MF_Segment/LedControl_dual.cpp | 22 +++------------------- src/MF_Segment/LedControl_dual.h | 21 +++++++-------------- 2 files changed, 10 insertions(+), 33 deletions(-) diff --git a/src/MF_Segment/LedControl_dual.cpp b/src/MF_Segment/LedControl_dual.cpp index 9bbb774b..a27604bd 100644 --- a/src/MF_Segment/LedControl_dual.cpp +++ b/src/MF_Segment/LedControl_dual.cpp @@ -1,27 +1,11 @@ -// ======================================================================= -// @file LedControl_dual.cpp -// -// @project MobiFlight custom Firmware // -// @author GiorgioCC (g.crocic@gmail.com) - 2023-06-29 -// @modifiedby GiorgioCC - 2023-07-09 20:13 +// LedControl_dual.cpp // -// A library for controlling LED 7-segment displays with either -// a MAX7219/MAX7221 or a TM1637 (4/6 digit) driver -// Portions of code derived from: -// - LedControl - A library for controlling Leds with a MAX7219/MAX7221 -// Copyright (c) 2007 Eberhard Fahle -// - TM1637TinyDisplay - TM1637 Tiny Display library by Jason A. Cox -// (https://github.com/jasonacox) +// (C) MobiFlight Project 2023 // -// ======================================================================= #include "LedControl_dual.h" -// ======================================================================= -// Common Definitions -// ======================================================================= - // Segments to be switched on for characters and digits on 7-Segment Displays // bit/segment sequence: dABCDEFG // A @@ -154,7 +138,7 @@ void LedControl::begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t cs IO_DTA = dataPin; IO_CLK = clkPin; IO_CS = csPin; - + if (isMAX()) { if ((numDevices - 1) > 7) numDevices = 8; maxUnits = numDevices; diff --git a/src/MF_Segment/LedControl_dual.h b/src/MF_Segment/LedControl_dual.h index c122473d..43b7d3ff 100644 --- a/src/MF_Segment/LedControl_dual.h +++ b/src/MF_Segment/LedControl_dual.h @@ -1,33 +1,26 @@ -// ======================================================================= -// @file LedControl_dual.h // -// @project MobiFlight custom Firmware +// LedControl_dual.cpp +// +// (C) MobiFlight Project 2023 +// +// @author GiorgioCC (g.crocic@gmail.com) - 2023-06-29 // -// @author GiorgioCC (g.crocic@gmail.com) - 2023-06-29 -// @modifiedby GiorgioCC - 2023-07-04 15:45 +// Remarks: // -// A library for controlling LED 7-segment displays with either -// a MAX7219/MAX7221 or a TM1637 (4/6 digit) driver // Portions of code derived from: // - LedControl - A library for controlling Leds with a MAX7219/MAX7221 // Copyright (c) 2007 Eberhard Fahle // - TM1637TinyDisplay - TM1637 Tiny Display library by Jason A. Cox // (https://github.com/jasonacox) -// // ======================================================================= // This is basically a mix of two drivers in the same library (with common // code parts factorized as much as possible): -// the type of driver to be used is determined with the value passed for -// the argument in the begin() call. -// Since the TM1637 does not use a CS line, special values (which are -// invalid for a MAX72xx) are used to configure the display as a TM1637. -// Specifically, csPin = 0xFD -> TM1637 4-digit, 0xFE -> TM1637 6-digit +// the type of driver is determined by the first argument in constructor // ======================================================================= // Method signatures (and purpose) have been kept exactly as in the original // LedControl library used in MF firmware, so the interface doesn't change; // non-relevant arguments (particularly: for TM's) are ignored. // A few methods (mostly for internal use) have been added. -// #ifndef __LEDCONTROL_DUAL__H__ #define __LEDCONTROL_DUAL__H__ From 73ed51c296e1220d9d37ed8f7475fb2b20072ac0 Mon Sep 17 00:00:00 2001 From: DocMoebiuz Date: Sat, 21 Oct 2023 09:59:48 +0200 Subject: [PATCH 5/5] minor renaming for consistency with other classes --- src/MF_Segment/LedControl_dual.cpp | 62 +++++++++++++++--------------- src/MF_Segment/LedControl_dual.h | 10 ++--- 2 files changed, 36 insertions(+), 36 deletions(-) diff --git a/src/MF_Segment/LedControl_dual.cpp b/src/MF_Segment/LedControl_dual.cpp index a27604bd..31ffb64e 100644 --- a/src/MF_Segment/LedControl_dual.cpp +++ b/src/MF_Segment/LedControl_dual.cpp @@ -134,18 +134,18 @@ const uint8_t digitmap[] = {2, 1, 0, 5, 4, 3}; void LedControl::begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices) { - this->type = type; - IO_DTA = dataPin; - IO_CLK = clkPin; - IO_CS = csPin; + _type = type; + _dataPin = dataPin; + _clkPin = clkPin; + _csPin = csPin; if (isMAX()) { if ((numDevices - 1) > 7) numDevices = 8; maxUnits = numDevices; - pinMode(IO_DTA, OUTPUT); - pinMode(IO_CLK, OUTPUT); - pinMode(IO_CS, OUTPUT); - digitalWrite(IO_CS, HIGH); + pinMode(_dataPin, OUTPUT); + pinMode(_clkPin, OUTPUT); + pinMode(_csPin, OUTPUT); + digitalWrite(_csPin, HIGH); for (uint8_t i = 0; i < maxUnits; i++) { spiTransfer(i, OP_DISPLAYTEST, 0); setScanLimit(i, 7); // scanlimit is set to max on startup @@ -154,12 +154,12 @@ void LedControl::begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t cs shutdown(i, true); // we go into shutdown-mode on startup } } else { - maxUnits = (IO_CS == 0xFD ? 4 : 6); + maxUnits = (this->_type == LedSegment::TYPE_TM1637_4DIGITS ? 4 : 6); // Both pins are set as inputs, allowing the pull-up resistors to pull them up - pinMode(IO_CLK, INPUT_PULLUP); - pinMode(IO_DTA, INPUT_PULLUP); - digitalWrite(IO_CLK, LOW); // Prepare '0' value as dominant - digitalWrite(IO_DTA, LOW); // Prepare '0' value as dominant + pinMode(_clkPin, INPUT_PULLUP); + pinMode(_dataPin, INPUT_PULLUP); + digitalWrite(_clkPin, LOW); // Prepare '0' value as dominant + digitalWrite(_dataPin, LOW); // Prepare '0' value as dominant clearDisplay(0); // setIntensity(0, MAX_BRIGHTNESS); brightness = MAX_BRIGHTNESS; @@ -286,18 +286,18 @@ void LedControl::spiTransfer(uint8_t addr, uint8_t opcode, uint8_t data) rawdata[offset + 1] = opcode; rawdata[offset] = data; - digitalWrite(IO_CS, LOW); + digitalWrite(_csPin, LOW); for (uint8_t i = maxBytes; i > 0; i--) { // shiftOut(IO_DTA, IO_CLK, MSBFIRST, rawdata[i - 1]); byte dta = rawdata[i - 1]; for (uint8_t m = 0x80; m != 0; m >>= 1) { // MSB first - digitalWrite(IO_DTA, (dta & m)); - digitalWrite(IO_CLK, HIGH); - digitalWrite(IO_CLK, LOW); + digitalWrite(_dataPin, (dta & m)); + digitalWrite(_clkPin, HIGH); + digitalWrite(_clkPin, LOW); } } - digitalWrite(IO_CS, HIGH); + digitalWrite(_csPin, HIGH); } // ------------------------------------------------ @@ -306,17 +306,17 @@ void LedControl::spiTransfer(uint8_t addr, uint8_t opcode, uint8_t data) void LedControl::start() { - pinMode(IO_DTA, OUTPUT); + pinMode(_dataPin, OUTPUT); bitDelay(); } void LedControl::stop() { - pinMode(IO_DTA, OUTPUT); + pinMode(_dataPin, OUTPUT); bitDelay(); - pinMode(IO_CLK, INPUT); + pinMode(_clkPin, INPUT); bitDelay(); - pinMode(IO_DTA, INPUT); + pinMode(_dataPin, INPUT); bitDelay(); } @@ -325,28 +325,28 @@ bool LedControl::writeByte(uint8_t data, bool rvs) uint8_t msk = (rvs ? 0x80 : 0x01); for (uint8_t i = 0; i < 8; i++) { // CLK low - pinMode(IO_CLK, OUTPUT); + pinMode(_clkPin, OUTPUT); bitDelay(); // Set data bit - pinMode(IO_DTA, (data & msk) ? INPUT : OUTPUT); + pinMode(_dataPin, (data & msk) ? INPUT : OUTPUT); bitDelay(); // CLK high - pinMode(IO_CLK, INPUT); + pinMode(_clkPin, INPUT); bitDelay(); data = (rvs ? data << 1 : data >> 1); } // Wait for acknowledge // CLK to zero - pinMode(IO_CLK, OUTPUT); - pinMode(IO_DTA, INPUT); + pinMode(_clkPin, OUTPUT); + pinMode(_dataPin, INPUT); bitDelay(); // CLK to high - pinMode(IO_CLK, INPUT); + pinMode(_clkPin, INPUT); bitDelay(); - uint8_t ack = digitalRead(IO_DTA); - if (ack == 0) pinMode(IO_DTA, OUTPUT); + uint8_t ack = digitalRead(_dataPin); + if (ack == 0) pinMode(_dataPin, OUTPUT); bitDelay(); - pinMode(IO_CLK, OUTPUT); + pinMode(_clkPin, OUTPUT); bitDelay(); return ack; } diff --git a/src/MF_Segment/LedControl_dual.h b/src/MF_Segment/LedControl_dual.h index 43b7d3ff..634dd1b4 100644 --- a/src/MF_Segment/LedControl_dual.h +++ b/src/MF_Segment/LedControl_dual.h @@ -62,10 +62,10 @@ class LedControl { private: // Common - uint8_t type = TYPE_UNDEFINED; - uint8_t IO_DTA = TYPE_UNDEFINED; - uint8_t IO_CLK = TYPE_UNDEFINED; - uint8_t IO_CS = TYPE_UNDEFINED; + uint8_t _type = TYPE_UNDEFINED; + uint8_t _dataPin = TYPE_UNDEFINED; + uint8_t _clkPin = TYPE_UNDEFINED; + uint8_t _csPin = TYPE_UNDEFINED; // MAX: Number of chained units // TM: Number of digits (4 or 6) #ifdef LEDCONTROL_NO_BUF @@ -105,7 +105,7 @@ class LedControl void begin(uint8_t type, uint8_t dataPin, uint8_t clkPin, uint8_t csPin, uint8_t numDevices = 1); - bool isMAX(void) { return type == LedSegment::TYPE_MAX72XX; } + bool isMAX(void) { return _type == LedSegment::TYPE_MAX72XX; } uint8_t getDeviceCount(void) { return (isMAX() ? maxUnits : 1); }; uint8_t getDigitCount(void) { return (isMAX() ? 8 : maxUnits); };