AceSegment  0.3.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
DriverBuilder.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 "LedMatrix.h"
27 #include "LedMatrixDirect.h"
28 #include "LedMatrixSerial.h"
29 #include "LedMatrixSpi.h"
30 #include "Driver.h"
31 #include "DigitDriver.h"
32 #include "ModulatingDigitDriver.h"
33 #include "SegmentDriver.h"
34 #include "DriverBuilder.h"
35 
36 namespace ace_segment {
37 
39 LedMatrix* DriverBuilder::buildLedMatrix() {
40  LedMatrix* matrix;
41  if (mLedMatrixType == kTypeLedMatrixDirect) {
42  if (mResistorsOnSegments) {
43  LedMatrixDirect* ledMatrix =
44  new LedMatrixDirect(mHardware, mNumDigits, mNumSegments);
45  ledMatrix->setGroupPins(mDigitPins);
46  ledMatrix->setElementPins(mSegmentPins);
47  if (mCommonCathode) {
48  ledMatrix->setCathodeOnGroup();
49  } else {
50  ledMatrix->setAnodeOnGroup();
51  }
52  matrix = ledMatrix;
53  } else {
54  LedMatrixDirect* ledMatrix =
55  new LedMatrixDirect(mHardware, mNumSegments, mNumDigits);
56  ledMatrix->setGroupPins(mSegmentPins);
57  ledMatrix->setElementPins(mDigitPins);
58  // If the resistors are on the Digit pins, then the "anode" and "cathode"
59  // pins become flipped electrically, because we're scanning the LED
60  // matrix in the other direction.
61  if (mCommonCathode) {
62  ledMatrix->setAnodeOnGroup();
63  } else {
64  ledMatrix->setCathodeOnGroup();
65  }
66  matrix = ledMatrix;
67  }
68  } else {
69  // We support only resistors on segments for SerialToParallel
70  LedMatrixSerial* ledMatrix;
71  if (mLedMatrixType == kTypeLedMatrixSerial) {
72  ledMatrix = new LedMatrixSerial(mHardware, mNumDigits, mNumSegments);
73  } else {
74  ledMatrix = new LedMatrixSpi(mHardware, mNumDigits, mNumSegments);
75  }
76  ledMatrix->setGroupPins(mDigitPins);
77  ledMatrix->setElementPins(mLatchPin, mDataPin, mClockPin);
78  if (mCommonCathode) {
79  ledMatrix->setCathodeOnGroup();
80  } else {
81  ledMatrix->setAnodeOnGroup();
82  }
83  matrix = ledMatrix;
84  }
85 
86  if (mUseTransistors) {
87  matrix->invertGroupLevels();
88  }
89  return matrix;
90 }
91 
92 Driver* DriverBuilder::build() {
93  LedMatrix* ledMatrix = buildLedMatrix();
94 
95  if (mResistorsOnSegments) {
96  if (mUseModulatingDriver) {
97  return new ModulatingDigitDriver(ledMatrix, mDimmablePatterns,
98  mNumDigits, mNumSubFields, true /* ownsLedMatrix */);
99  } else {
100  return new DigitDriver(ledMatrix, mDimmablePatterns, mNumDigits,
101  true /* ownsLedMatrix */);
102  }
103  } else {
104  return new SegmentDriver(ledMatrix, mDimmablePatterns, mNumDigits,
105  true /* ownsLedMatrix */);
106  }
107 }
108 
109 }