AceSegment  0.7.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 
53 template <typename T_SPII>
55  public:
66  const T_SPII& spiInterface,
67  uint8_t elementOnPattern,
68  uint8_t groupOnPattern,
69  uint8_t byteOrder,
70  const uint8_t* remapArrayInverted = nullptr
71  ) :
72  LedMatrixBase(elementOnPattern, groupOnPattern),
73  mSpiInterface(spiInterface),
74  mRemapArrayInverted(remapArrayInverted),
75  mByteOrder(byteOrder)
76  {}
77 
78  void begin() const {}
79 
80  void end() const {}
81 
106  void draw(uint8_t group, uint8_t elementPattern) const {
107  // Find the logical address which will cause this pattern to appear in
108  // the correct physical position.
109  uint8_t logicalGroup = remapPhysicalToLogical(group);
110  uint8_t groupPattern = 0x1 << logicalGroup;
111 
112  drawPatterns(groupPattern, elementPattern);
113  mPrevElementPattern = elementPattern;
114  }
115 
120  void enableGroup(uint8_t group) const {
121  draw(group, mPrevElementPattern);
122  }
123 
125  void disableGroup(uint8_t group) const {
126  (void) group;
127  drawPatterns(0x00, 0x00);
128  // Don't update mPrevElementPattern.
129  }
130 
132  void clear() const {
133  drawPatterns(0x00, 0x00);
134  mPrevElementPattern = 0x00;
135  }
136 
137  private:
144  void drawPatterns(uint8_t groupPattern, uint8_t elementPattern) const {
145  uint8_t actualGroupPattern = (groupPattern ^ mGroupXorMask);
146  uint8_t actualElementPattern = (elementPattern ^ mElementXorMask);
147  uint16_t data = (mByteOrder == kByteOrderGroupHighElementLow)
148  ? actualGroupPattern << 8 | actualElementPattern
149  : actualElementPattern << 8 | actualGroupPattern;
150  mSpiInterface.send16(data);
151  }
152 
154  uint8_t remapPhysicalToLogical(uint8_t pos) const {
155  return mRemapArrayInverted ? mRemapArrayInverted[pos] : pos;
156  }
157 
158  private:
159  friend class ::LedMatrixDualHc595Test_draw;
160  friend class ::LedMatrixDualHc595Test_enableGroup;
161  friend class ::LedMatrixDualHc595Test_disableGroup;
162 
163  const T_SPII& mSpiInterface;
164 
169  const uint8_t* const mRemapArrayInverted;
170 
172  const uint8_t mByteOrder;
173 
178  mutable uint8_t mPrevElementPattern;
179 };
180 
181 }
182 
183 #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:54
ace_segment::LedMatrixDualHc595::disableGroup
void disableGroup(uint8_t group) const
Turn off the given group.
Definition: LedMatrixDualHc595.h:125
ace_segment::LedMatrixDualHc595::clear
void clear() const
Clear the entire display.
Definition: LedMatrixDualHc595.h:132
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:106
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:120
ace_segment::LedMatrixDualHc595::LedMatrixDualHc595
LedMatrixDualHc595(const T_SPII &spiInterface, uint8_t elementOnPattern, uint8_t groupOnPattern, uint8_t byteOrder, const uint8_t *remapArrayInverted=nullptr)
Definition: LedMatrixDualHc595.h:65