AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
LedMatrixSingleHc595.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_SINGLE_HC595_H
26 #define ACE_SEGMENT_LED_MATRIX_SINGLE_HC595_H
27 
28 #include <Arduino.h> // OUTPUT, INPUT
29 #include "../hw/GpioInterface.h"
30 #include "LedMatrixBase.h"
31 
32 class LedMatrixSingleHc595Test_drawElements;
33 
34 namespace ace_segment {
35 
50 template <typename T_SPII, typename T_GPIOI = GpioInterface>
52  public:
54  const T_SPII& spiInterface,
55  uint8_t elementOnPattern,
56  uint8_t groupOnPattern,
57  uint8_t numGroups,
58  const uint8_t* groupPins
59  ) :
60  LedMatrixBase(elementOnPattern, groupOnPattern),
61  mSpiInterface(spiInterface),
62  mGroupPins(groupPins),
63  mNumGroups(numGroups)
64  {}
65 
66  void begin() const {
67  // Set pins to OUTPUT mode but set LEDs to OFF.
68  uint8_t output = (0x00 ^ mGroupXorMask) & 0x1;
69  for (uint8_t group = 0; group < mNumGroups; group++) {
70  uint8_t pin = mGroupPins[group];
71  T_GPIOI::pinMode(pin, OUTPUT);
72  T_GPIOI::digitalWrite(pin, output);
73  }
74  }
75 
76  void end() const {
77  // Set pins to INPUT mode.
78  for (uint8_t group = 0; group < mNumGroups; group++) {
79  uint8_t pin = mGroupPins[group];
80  T_GPIOI::pinMode(pin, INPUT);
81  }
82  }
83 
84  void draw(uint8_t group, uint8_t elementPattern) const {
85  if (group != mPrevGroup) {
86  disableGroup(mPrevGroup);
87  }
88 
89  drawElements(elementPattern);
90  enableGroup(group);
91  mPrevGroup = group;
92  }
93 
94  void enableGroup(uint8_t group) const {
95  writeGroupPin(group, 0x1);
96  mPrevGroup = group;
97  }
98 
99  void disableGroup(uint8_t group) const {
100  writeGroupPin(group, 0x0);
101  mPrevGroup = group;
102  }
103 
104  void clear() const {
105  for (uint8_t group = 0; group < mNumGroups; group++) {
106  disableGroup(group);
107  }
108  drawElements(0x00);
109  }
110 
111  private:
112  friend class ::LedMatrixSingleHc595Test_drawElements;
113 
115  void drawElements(uint8_t pattern) const {
116  uint8_t actualPattern = pattern ^ mElementXorMask;
117  mSpiInterface.send8(actualPattern);
118  }
119 
121  void writeGroupPin(uint8_t group, uint8_t output) const {
122  uint8_t groupPin = mGroupPins[group];
123  T_GPIOI::digitalWrite(groupPin, (output ^ mGroupXorMask) & 0x1);
124  }
125 
126  private:
127  const T_SPII& mSpiInterface;
128  const uint8_t* const mGroupPins;
129  uint8_t const mNumGroups;
130 
132  mutable uint8_t mPrevGroup = 0;
133 };
134 
135 }
136 #endif
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::LedMatrixBase::LedMatrixBase
LedMatrixBase(uint8_t elementOnPattern, uint8_t groupOnPattern)
Definition: LedMatrixBase.h:86
ace_segment::LedMatrixSingleHc595
An implementation of LedMatrixBase with an 74HC595 Shift Register chip on the segment pins,...
Definition: LedMatrixSingleHc595.h:51