AceSegment  0.8.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
LedMatrixDualHc595.h
1 /*
2 MIT License
3 
4 Copyright (c) 2018 Brian T. Park
5 
6 Permission is hereby granted, free of charge, to any person obtaining a copy
7 of this software and associated documentation files (the "Software"), to deal
8 in the Software without restriction, including without limitation the rights
9 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 copies of the Software, and to permit persons to whom the Software is
11 furnished to do so, subject to the following conditions:
12 
13 The above copyright notice and this permission notice shall be included in all
14 copies or substantial portions of the Software.
15 
16 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 SOFTWARE.
23 */
24 
25 #ifndef ACE_SEGMENT_LED_MATRIX_DUAL_HC595_H
26 #define ACE_SEGMENT_LED_MATRIX_DUAL_HC595_H
27 
28 #include "LedMatrixBase.h"
29 
30 class LedMatrixDualHc595Test_draw;
31 class LedMatrixDualHc595Test_enableGroup;
32 class LedMatrixDualHc595Test_disableGroup;
33 
34 namespace ace_segment {
35 
37 const uint8_t kByteOrderGroupHighElementLow = 0;
38 
40 const uint8_t kByteOrderElementHighGroupLow = 1;
41 
55 template <typename T_SPII>
57  public:
69  const T_SPII& spiInterface,
70  uint8_t elementOnPattern,
71  uint8_t groupOnPattern,
72  uint8_t byteOrder,
73  const uint8_t* remapArrayInverted = nullptr
74  ) :
75  LedMatrixBase(elementOnPattern, groupOnPattern),
76  mSpiInterface(spiInterface),
77  mRemapArrayInverted(remapArrayInverted),
78  mByteOrder(byteOrder)
79  {}
80 
81  void begin() const {}
82 
83  void end() const {}
84 
109  void draw(uint8_t group, uint8_t elementPattern) const {
110  // Find the logical address which will cause this pattern to appear in
111  // the correct physical position.
112  uint8_t logicalGroup = remapPhysicalToLogical(group);
113  uint8_t groupPattern = 0x1 << logicalGroup;
114 
115  drawPatterns(groupPattern, elementPattern);
116  mPrevElementPattern = elementPattern;
117  }
118 
123  void enableGroup(uint8_t group) const {
124  draw(group, mPrevElementPattern);
125  }
126 
128  void disableGroup(uint8_t group) const {
129  (void) group;
130  drawPatterns(0x00, 0x00);
131  // Don't update mPrevElementPattern.
132  }
133 
135  void clear() const {
136  drawPatterns(0x00, 0x00);
137  mPrevElementPattern = 0x00;
138  }
139 
140  private:
147  void drawPatterns(uint8_t groupPattern, uint8_t elementPattern) const {
148  uint8_t actualGroupPattern = (groupPattern ^ mGroupXorMask);
149  uint8_t actualElementPattern = (elementPattern ^ mElementXorMask);
150  uint16_t data = (mByteOrder == kByteOrderGroupHighElementLow)
151  ? actualGroupPattern << 8 | actualElementPattern
152  : actualElementPattern << 8 | actualGroupPattern;
153  mSpiInterface.send16(data);
154  }
155 
157  uint8_t remapPhysicalToLogical(uint8_t pos) const {
158  return mRemapArrayInverted ? mRemapArrayInverted[pos] : pos;
159  }
160 
161  private:
162  friend class ::LedMatrixDualHc595Test_draw;
163  friend class ::LedMatrixDualHc595Test_enableGroup;
164  friend class ::LedMatrixDualHc595Test_disableGroup;
165 
170  const T_SPII mSpiInterface;
171 
176  const uint8_t* const mRemapArrayInverted;
177 
179  const uint8_t mByteOrder;
180 
185  mutable uint8_t mPrevElementPattern;
186 };
187 
188 }
189 
190 #endif
ace_segment::LedMatrixDualHc595
An LedMatrix that whose group pins are attached to one 74HC595 shift register and the element pins ar...
Definition: LedMatrixDualHc595.h:56
ace_segment::LedMatrixDualHc595::disableGroup
void disableGroup(uint8_t group) const
Turn off the given group.
Definition: LedMatrixDualHc595.h:128
ace_segment::LedMatrixDualHc595::clear
void clear() const
Clear the entire display.
Definition: LedMatrixDualHc595.h:135
ace_segment::LedMatrixDualHc595::draw
void draw(uint8_t group, uint8_t elementPattern) const
Write out the group and element patterns in a single 16-bit stream.
Definition: LedMatrixDualHc595.h:109
ace_segment::LedMatrixBase
Class that represents the abstraction of a particular LED display wiring, and knows how to turn off a...
Definition: LedMatrixBase.h:79
ace_segment::LedMatrixDualHc595::enableGroup
void enableGroup(uint8_t group) const
Turn on the given group, using the previous segment pattern.
Definition: LedMatrixDualHc595.h:123
ace_segment::LedMatrixDualHc595::LedMatrixDualHc595
LedMatrixDualHc595(const T_SPII &spiInterface, uint8_t elementOnPattern, uint8_t groupOnPattern, uint8_t byteOrder, const uint8_t *remapArrayInverted=nullptr)
Constructor.
Definition: LedMatrixDualHc595.h:68