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

Add support for rearranging output digits #2

Merged
merged 4 commits into from
Dec 10, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
90 changes: 70 additions & 20 deletions src/TM1637_7SegmentLEDController.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,13 @@ class TM1637_7SegmentLEDCustomSegmentAddressingController : public TM1637Control
TM1637_7SegmentLEDCustomSegmentAddressingController(
uint8_t pinDIO,
uint8_t pinCLK
)
: TM1637Controller(pinDIO, pinCLK) {}
);

TM1637_7SegmentLEDCustomSegmentAddressingController(
uint8_t pinDIO,
uint8_t pinCLK,
const unsigned int (&gridsOrder)[NUM_OF_GRIDS]
);

static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfGridsForUse() { return NUM_OF_GRIDS_FOR_USE; }
static constexpr inline _ATTR_ALWAYS_INLINE_ size_t numberOfDigits() { return NUM_OF_DIGITS; }
Expand Down Expand Up @@ -109,7 +114,14 @@ class TM1637_7SegmentLEDCustomSegmentAddressingController : public TM1637Control
inline _ATTR_ALWAYS_INLINE_ void flushAllGrids();

private:
uint8_t m_segmentBits[NUM_OF_GRIDS_FOR_USE] = {0};
using index_t = unsigned int;

inline _ATTR_ALWAYS_INLINE_ const index_t& getGrid(const index_t& grid) { return m_gridsOrder[grid]; }
inline _ATTR_ALWAYS_INLINE_ void writeSegmentBitsUnchecked(const index_t& grid, const uint8_t& segmentBits) { m_segmentBits[getGrid(grid)] = segmentBits; }
inline _ATTR_ALWAYS_INLINE_ const uint8_t& readSegmentBitsUnchecked(const index_t& grid) { return m_segmentBits[getGrid(grid)]; }

/*const*/ index_t m_gridsOrder[NUM_OF_GRIDS];
uint8_t m_segmentBits[NUM_OF_GRIDS] = {0};
};

template <size_t NUM_OF_DIGITS>
Expand All @@ -122,11 +134,36 @@ using TM1637_4Digit7SegmentLEDController = TM1637_7SegmentLEDController<4 /*DIGI
using TM1637_5Digit7SegmentLEDController = TM1637_7SegmentLEDController<5 /*DIGITS*/>;
using TM1637_6Digit7SegmentLEDController = TM1637_7SegmentLEDController<6 /*DIGITS*/>;

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_GRIDS_FOR_USE, TSegmentAddressing>::TM1637_7SegmentLEDCustomSegmentAddressingController(
uint8_t pinDIO,
uint8_t pinCLK
) : TM1637Controller(pinDIO, pinCLK)
{
for (auto grid = 0u; grid < NUM_OF_GRIDS; grid++)
m_gridsOrder[grid] = grid; // set default grids order
}

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_GRIDS_FOR_USE, TSegmentAddressing>::TM1637_7SegmentLEDCustomSegmentAddressingController(
uint8_t pinDIO,
uint8_t pinCLK,
const unsigned int (&gridsOrder)[NUM_OF_GRIDS]
) : TM1637Controller(pinDIO, pinCLK)
{
for (auto grid = 0u; grid < NUM_OF_GRIDS; grid++) {
if (gridsOrder[grid] < NUM_OF_GRIDS)
m_gridsOrder[grid] = gridsOrder[grid];
else
m_gridsOrder[grid] = grid; // out of range; set default value instead of specified one
}
}

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_GRIDS_FOR_USE, TSegmentAddressing>::clear(bool flush)
{
for (auto grid = 0u; grid < NUM_OF_GRIDS_FOR_USE; grid++)
m_segmentBits[grid] = 0b00000000;
writeSegmentBitsUnchecked(grid, 0b00000000);

if (flush)
flushAllGrids();
Expand All @@ -136,7 +173,7 @@ template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAd
void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_GRIDS_FOR_USE, TSegmentAddressing>::clearCharacterSegments(bool flush)
{
for (auto grid = 0u; grid < NUM_OF_GRIDS_FOR_USE; grid++)
m_segmentBits[grid] &= TSegmentAddressing::SEGMENTBITS_MASK_DECIMALPOINT;
writeSegmentBitsUnchecked(grid, readSegmentBitsUnchecked(grid) & TSegmentAddressing::SEGMENTBITS_MASK_DECIMALPOINT);

if (flush)
flushAllGrids();
Expand All @@ -146,7 +183,7 @@ template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAd
void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_GRIDS_FOR_USE, TSegmentAddressing>::clearDecimalPointSegments(bool flush)
{
for (auto grid = 0u; grid < NUM_OF_GRIDS_FOR_USE; grid++)
m_segmentBits[grid] &= TSegmentAddressing::SEGMENTBITS_MASK_CHARACTER;
writeSegmentBitsUnchecked(grid, readSegmentBitsUnchecked(grid) & TSegmentAddressing::SEGMENTBITS_MASK_CHARACTER);

if (flush)
flushAllGrids();
Expand All @@ -155,7 +192,7 @@ void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_G
template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_GRIDS_FOR_USE, TSegmentAddressing>::flushAllGrids()
{
this->transferDataToAddressAutomatic(m_segmentBits, NUM_OF_GRIDS_FOR_USE, LSBFIRST, 0);
this->transferDataToAddressAutomatic(m_segmentBits, NUM_OF_GRIDS, LSBFIRST, 0);
}

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
Expand All @@ -168,10 +205,10 @@ void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_G
if (NUM_OF_GRIDS_FOR_USE <= grid)
return; // out of range

m_segmentBits[grid] = segmentBits;
writeSegmentBitsUnchecked(grid, segmentBits);

if (flush)
this->transferDataToAddressFixed(m_segmentBits[grid], LSBFIRST, grid);
this->transferDataToAddressFixed(readSegmentBitsUnchecked(grid), LSBFIRST, getGrid(grid));
}

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
Expand All @@ -185,16 +222,17 @@ void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_G
if (NUM_OF_GRIDS_FOR_USE <= grid)
return; // out of range

if (trueForOnOtherwiseOff)
m_segmentBits[grid] |= segmentBits;
else
m_segmentBits[grid] &= ~segmentBits;
writeSegmentBitsUnchecked(
grid,
trueForOnOtherwiseOff
? readSegmentBitsUnchecked(grid) | segmentBits
: readSegmentBitsUnchecked(grid) & ~segmentBits
);

if (flush)
this->transferDataToAddressFixed(m_segmentBits[grid], LSBFIRST, grid);
this->transferDataToAddressFixed(readSegmentBitsUnchecked(grid), LSBFIRST, getGrid(grid));
}


template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_GRIDS_FOR_USE, TSegmentAddressing>::setSegmentBitsAt(
const unsigned int digit,
Expand All @@ -205,10 +243,10 @@ void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_G
if (NUM_OF_DIGITS <= digit)
return; // out of range

m_segmentBits[digit] = segmentBits;
writeSegmentBitsUnchecked(digit, segmentBits);

if (flush)
this->transferDataToAddressFixed(m_segmentBits[digit], LSBFIRST, digit);
this->transferDataToAddressFixed(readSegmentBitsUnchecked(digit), LSBFIRST, getGrid(digit));
}

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
Expand All @@ -232,10 +270,16 @@ void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_G
const bool flush
)
{
if (NUM_OF_DIGITS <= digit)
return; // out of range
if (10 <= decimalNumber)
return; // out of range

setSegmentBitsAt(digit, (m_segmentBits[digit] & TSegmentAddressing::SEGMENTBITS_MASK_DECIMALPOINT) | TSegmentAddressing::CHARS_HEXADECIMALS[decimalNumber], flush);
setSegmentBitsAt(
digit,
(readSegmentBitsUnchecked(digit) & TSegmentAddressing::SEGMENTBITS_MASK_DECIMALPOINT) | TSegmentAddressing::CHARS_HEXADECIMALS[decimalNumber],
flush
);
}

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
Expand All @@ -245,10 +289,16 @@ void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_G
const bool flush
)
{
if (NUM_OF_DIGITS <= digit)
return; // out of range
if (0x10 <= hexadecimalNumber)
return; // out of range

setSegmentBitsAt(digit, (m_segmentBits[digit] & TSegmentAddressing::SEGMENTBITS_MASK_DECIMALPOINT) | TSegmentAddressing::CHARS_HEXADECIMALS[hexadecimalNumber], flush);
setSegmentBitsAt(
digit,
(readSegmentBitsUnchecked(digit) & TSegmentAddressing::SEGMENTBITS_MASK_DECIMALPOINT) | TSegmentAddressing::CHARS_HEXADECIMALS[hexadecimalNumber],
flush
);
}

template <size_t NUM_OF_DIGITS, size_t NUM_OF_GRIDS_FOR_USE, typename TSegmentAddressing>
Expand All @@ -270,7 +320,7 @@ void TM1637_7SegmentLEDCustomSegmentAddressingController<NUM_OF_DIGITS, NUM_OF_G
)
{
for (auto grid = 0u; grid < NUM_OF_GRIDS_FOR_USE; grid++)
m_segmentBits[grid] = segmentBits[grid];
writeSegmentBitsUnchecked(grid, segmentBits[grid]);

if (flush)
flushAllGrids();
Expand Down
7 changes: 7 additions & 0 deletions src/TM1637_OSL40391IXController.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,13 @@ class TM1637_OSL40391IXCustomSegmentAddressingController
)
: TM1637_4Digit5Grid7SegmentLEDCustomSegmentAddressingController<TSegmentAddressing>(pinDIO, pinCLK) {}

TM1637_OSL40391IXCustomSegmentAddressingController(
uint8_t pinDIO,
uint8_t pinCLK,
const unsigned int (&gridsOrder)[TM1637Controller::NUM_OF_GRIDS]
)
: TM1637_4Digit5Grid7SegmentLEDCustomSegmentAddressingController<TSegmentAddressing>(pinDIO, pinCLK, gridsOrder) {}

inline _ATTR_ALWAYS_INLINE_ void setColonOn(const bool flush = true) { setColon(true, flush); }
inline _ATTR_ALWAYS_INLINE_ void setColonOff(const bool flush = true) { setColon(false, flush); }
inline _ATTR_ALWAYS_INLINE_ void setColon(const bool trueForOnOtherwiseOff, const bool flush = true) { this->switchSegmentBitsAtGrid(gridForDots(), TSegmentAddressing::SEGMENTBITS_COLON, trueForOnOtherwiseOff, flush); }
Expand Down