AceSegment  0.12.0
A library for rendering seven segment LED displays using the TM1637, TM1638, 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 
52 template <typename T_SPII, typename T_GPIOI = GpioInterface>
54  public:
64  const T_SPII& spiInterface,
65  uint8_t elementOnPattern,
66  uint8_t groupOnPattern,
67  uint8_t numGroups,
68  const uint8_t* groupPins
69  ) :
70  LedMatrixBase(elementOnPattern, groupOnPattern),
71  mSpiInterface(spiInterface),
72  mGroupPins(groupPins),
73  mNumGroups(numGroups)
74  {}
75 
76  void begin() const {
77  // Set pins to OUTPUT mode but set LEDs to OFF.
78  uint8_t output = (0x00 ^ mGroupXorMask) & 0x1;
79  for (uint8_t group = 0; group < mNumGroups; group++) {
80  uint8_t pin = mGroupPins[group];
81  T_GPIOI::pinMode(pin, OUTPUT);
82  T_GPIOI::digitalWrite(pin, output);
83  }
84  }
85 
86  void end() const {
87  // Set pins to INPUT mode.
88  for (uint8_t group = 0; group < mNumGroups; group++) {
89  uint8_t pin = mGroupPins[group];
90  T_GPIOI::pinMode(pin, INPUT);
91  }
92  }
93 
94  void draw(uint8_t group, uint8_t elementPattern) const {
95  if (group != mPrevGroup) {
96  disableGroup(mPrevGroup);
97  }
98 
99  drawElements(elementPattern);
100  enableGroup(group);
101  mPrevGroup = group;
102  }
103 
104  void enableGroup(uint8_t group) const {
105  writeGroupPin(group, 0x1);
106  mPrevGroup = group;
107  }
108 
109  void disableGroup(uint8_t group) const {
110  writeGroupPin(group, 0x0);
111  mPrevGroup = group;
112  }
113 
114  void clear() const {
115  for (uint8_t group = 0; group < mNumGroups; group++) {
116  disableGroup(group);
117  }
118  drawElements(0x00);
119  }
120 
121  private:
122  friend class ::LedMatrixSingleHc595Test_drawElements;
123 
125  void drawElements(uint8_t pattern) const {
126  uint8_t actualPattern = pattern ^ mElementXorMask;
127  mSpiInterface.send8(actualPattern);
128  }
129 
131  void writeGroupPin(uint8_t group, uint8_t output) const {
132  uint8_t groupPin = mGroupPins[group];
133  T_GPIOI::digitalWrite(groupPin, (output ^ mGroupXorMask) & 0x1);
134  }
135 
136  private:
141  const T_SPII mSpiInterface;
142 
143  const uint8_t* const mGroupPins;
144 
145  uint8_t const mNumGroups;
146 
148  mutable uint8_t mPrevGroup = 0;
149 };
150 
151 }
152 #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::LedMatrixSingleHc595
An implementation of LedMatrixBase with an 74HC595 Shift Register chip on the segment pins,...
Definition: LedMatrixSingleHc595.h:53
ace_segment::LedMatrixSingleHc595::LedMatrixSingleHc595
LedMatrixSingleHc595(const T_SPII &spiInterface, uint8_t elementOnPattern, uint8_t groupOnPattern, uint8_t numGroups, const uint8_t *groupPins)
Constructor.
Definition: LedMatrixSingleHc595.h:63