AceSegment  0.4.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
LedMatrixSingleShiftRegister.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_SHIFT_REGISTER_H
26 #define ACE_SEGMENT_LED_MATRIX_SINGLE_SHIFT_REGISTER_H
27 
28 #include <Arduino.h> // OUTPUT, INPUT
29 #include "LedMatrixBase.h"
30 
31 class LedMatrixSingleShiftRegisterTest_drawElements;
32 
33 namespace ace_segment {
34 
49 template<typename H, typename SA>
51  public:
53  const H& hardware,
54  const SA& spiAdapter,
55  uint8_t groupOnPattern,
56  uint8_t elementOnPattern,
57  uint8_t numGroups,
58  const uint8_t* groupPins
59  ) :
60  LedMatrixBase(groupOnPattern, elementOnPattern),
61  mHardware(hardware),
62  mSpiAdapter(spiAdapter),
63  mGroupPins(groupPins),
64  mNumGroups(numGroups)
65  {}
66 
67  void begin() const {
68  // Set pins to OUTPUT mode but set LEDs to OFF.
69  uint8_t output = (0x00 ^ mGroupXorMask) & 0x1;
70  for (uint8_t group = 0; group < mNumGroups; group++) {
71  uint8_t pin = mGroupPins[group];
72  mHardware.pinMode(pin, OUTPUT);
73  mHardware.digitalWrite(pin, output);
74  }
75  }
76 
77  void end() const {
78  // Set pins to INPUT mode.
79  for (uint8_t group = 0; group < mNumGroups; group++) {
80  uint8_t pin = mGroupPins[group];
81  mHardware.pinMode(pin, INPUT);
82  }
83  }
84 
85  void draw(uint8_t group, uint8_t elementPattern) const {
86  if (group != mPrevGroup) {
87  disableGroup(mPrevGroup);
88  }
89 
90  drawElements(elementPattern);
91  enableGroup(group);
92  mPrevGroup = group;
93  }
94 
95  void enableGroup(uint8_t group) const {
96  writeGroupPin(group, 0x1);
97  mPrevGroup = group;
98  }
99 
100  void disableGroup(uint8_t group) const {
101  writeGroupPin(group, 0x0);
102  mPrevGroup = group;
103  }
104 
105  void clear() const {
106  for (uint8_t group = 0; group < mNumGroups; group++) {
107  disableGroup(group);
108  }
109  drawElements(0x00);
110  }
111 
112  private:
113  friend class ::LedMatrixSingleShiftRegisterTest_drawElements;
114 
116  void drawElements(uint8_t pattern) const {
117  uint8_t actualPattern = pattern ^ mElementXorMask;
118  mSpiAdapter.transfer(actualPattern);
119  }
120 
122  void writeGroupPin(uint8_t group, uint8_t output) const {
123  uint8_t groupPin = mGroupPins[group];
124  mHardware.digitalWrite(groupPin, (output ^ mGroupXorMask) & 0x1);
125  }
126 
127  private:
128  const H& mHardware;
129  const SA& mSpiAdapter;
130  const uint8_t* const mGroupPins;
131  uint8_t const mNumGroups;
132 
134  mutable uint8_t mPrevGroup = 0;
135 };
136 
137 }
138 #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:72
ace_segment::LedMatrixBase::LedMatrixBase
LedMatrixBase(uint8_t groupOnPattern, uint8_t elementOnPattern)
Definition: LedMatrixBase.h:85
ace_segment::LedMatrixSingleShiftRegister
An implementation of LedMatrixBase with an 74HC595 Serial-To-Parallel converter chip on the segment p...
Definition: LedMatrixSingleShiftRegister.h:50