AceSegment  0.2.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
LedMatrixSerial.cpp
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 #include "Hardware.h"
26 #include "LedMatrixSerial.h"
27 
28 namespace ace_segment {
29 
30 void LedMatrixSerial::configure() {
31  LedMatrix::configure();
32 
33  // TODO: Are these the correct initial values for the 74HC595?
34  // Seems like there's an initial time of random values right at the beginning
35  // before the Nano gets reset a second time. I think the proper solution is
36  // to hook up the OE pin of the 74HC595 to a data pin, and set it HIGH
37  // (inactive) by default (pull up resistor?) unless actively set LOW.
38  mHardware->digitalWrite(mLatchPin, LOW);
39  mHardware->digitalWrite(mDataPin, LOW);
40  mHardware->digitalWrite(mClockPin, LOW);
41 
42  mHardware->pinMode(mLatchPin, OUTPUT);
43  mHardware->pinMode(mDataPin, OUTPUT);
44  mHardware->pinMode(mClockPin, OUTPUT);
45 
46  for (uint8_t group = 0; group < mNumGroups; group++) {
47  uint8_t pin = mGroupPins[group];
48  mHardware->pinMode(pin, OUTPUT);
49  mHardware->digitalWrite(pin, mGroupOff);
50  }
51 }
52 
53 void LedMatrixSerial::enableGroup(uint8_t group) {
54  writeGroupPin(group, mGroupOn);
55 }
56 
57 void LedMatrixSerial::disableGroup(uint8_t group) {
58  writeGroupPin(group, mGroupOff);
59 }
60 
61 void LedMatrixSerial::drawElements(uint8_t pattern) {
62  mHardware->digitalWrite(mLatchPin, LOW);
63  uint8_t actualPattern = (mElementOn == HIGH) ? pattern : ~pattern;
64  mHardware->shiftOut(mDataPin, mClockPin, MSBFIRST, actualPattern);
65  mHardware->digitalWrite(mLatchPin, HIGH);
66 }
67 
68 void LedMatrixSerial::writeGroupPin(uint8_t group, uint8_t output) {
69  uint8_t groupPin = mGroupPins[group];
70  mHardware->digitalWrite(groupPin, output);
71 }
72 
73 }
virtual void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t value)
Shift out.
Definition: Hardware.h:53
virtual void pinMode(uint8_t pin, uint8_t mode)
Set pin mode.
Definition: Hardware.h:48
virtual void digitalWrite(uint8_t pin, uint8_t value)
Write value to pin.
Definition: Hardware.h:43
void writeGroupPin(uint8_t group, uint8_t output)
Write to group pin identified by &#39;group&#39;.