AceSegment  0.7.0
A framework for rendering seven segment LED displays using the TM1637, MAX7219, HT16K33, or 74HC595 controller chips
ScanningModule.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_SCANNING_MODULE_H
26 #define ACE_SEGMENT_SCANNING_MODULE_H
27 
28 #include <stdint.h>
29 #include <AceCommon.h> // incrementMod()
30 #include "../hw/ClockInterface.h" // ClockInterface
31 #include "../LedModule.h"
32 
33 class ScanningModuleTest_displayCurrentField;
34 
35 namespace ace_segment {
36 
75 template <
76  typename T_LM,
77  uint8_t T_DIGITS,
78  uint8_t T_SUBFIELDS = 1,
79  typename T_CI = ClockInterface>
80 class ScanningModule : public LedModule {
81 
82  public:
93  explicit ScanningModule(
94  const T_LM& ledMatrix,
95  uint8_t framesPerSecond
96  ):
97  LedModule(T_DIGITS),
98  mLedMatrix(ledMatrix),
99  mFramesPerSecond(framesPerSecond)
100  {}
101 
108  void begin() {
109  // Set up durations for the renderFieldWhenReady() polling function.
110  mMicrosPerField = (uint32_t) 1000000UL / getFieldsPerSecond();
111  mLastRenderFieldMicros = T_CI::micros();
112 
113  // Initialize variables needed for multiplexing.
114  mCurrentDigit = 0;
115  mPrevDigit = T_DIGITS - 1;
116  mCurrentSubField = 0;
117  mPattern = 0;
118 
119  // Set initial patterns and global brightness.
120  mLedMatrix.clear();
121  if (T_SUBFIELDS > 1) {
122  setBrightness(T_SUBFIELDS / 2); // half brightness
123  }
124  }
125 
126 
128  void end() {}
129 
130  //-----------------------------------------------------------------------
131  // Implement the LedModule interface.
132  //-----------------------------------------------------------------------
133 
135  uint8_t getNumDigits() const { return T_DIGITS; }
136 
137  void setPatternAt(uint8_t pos, uint8_t pattern) override {
138  mPatterns[pos] = pattern;
139  }
140 
141  uint8_t getPatternAt(uint8_t pos) override {
142  return mPatterns[pos];
143  }
144 
151  void setBrightness(uint8_t brightness) override {
152  for (uint8_t i = 0; i < T_DIGITS; i++) {
153  setBrightnessAt(i, brightness);
154  }
155  }
156 
157  //-----------------------------------------------------------------------
158  // Additional brightness control. ScanningModule allows brightness to be
159  // defined on a per-digit basis.
160  //-----------------------------------------------------------------------
161 
185  void setBrightnessAt(uint8_t pos, uint8_t brightness) {
186  if (pos >= T_DIGITS) return;
187  mBrightnesses[pos] = (brightness >= T_SUBFIELDS)
188  ? T_SUBFIELDS : brightness;
189  }
190 
191  //-----------------------------------------------------------------------
192  // Methods related to rendering.
193  //-----------------------------------------------------------------------
194 
196  uint16_t getFramesPerSecond() const { return mFramesPerSecond; }
197 
199  uint16_t getFieldsPerSecond() const {
200  return mFramesPerSecond * getFieldsPerFrame();
201  }
202 
204  uint16_t getFieldsPerFrame() const { return T_DIGITS * T_SUBFIELDS; }
205 
210  uint16_t getMicrosPerField() const { return mMicrosPerField; }
211 
221  uint16_t now = T_CI::micros();
222  uint16_t elapsedMicros = now - mLastRenderFieldMicros;
223  if (elapsedMicros >= mMicrosPerField) {
224  renderFieldNow();
225  mLastRenderFieldMicros = now;
226  return true;
227  } else {
228  return false;
229  }
230  }
231 
241  void renderFieldNow() {
242  if (T_SUBFIELDS > 1) {
243  displayCurrentFieldModulated();
244  } else {
245  displayCurrentFieldPlain();
246  }
247  }
248 
249  private:
250  friend class ::ScanningModuleTest_displayCurrentField;
251 
252  // disable copy-constructor and assignment operator
253  ScanningModule(const ScanningModule&) = delete;
254  ScanningModule& operator=(const ScanningModule&) = delete;
255 
257  void displayCurrentFieldPlain() {
258  const uint8_t pattern = mPatterns[mCurrentDigit];
259  mLedMatrix.draw(mCurrentDigit, pattern);
260  mPrevDigit = mCurrentDigit;
261  ace_common::incrementMod(mCurrentDigit, T_DIGITS);
262  }
263 
265  void displayCurrentFieldModulated() {
266  // Calculate the maximum subfield duration for current digit.
267  const uint8_t brightness = mBrightnesses[mCurrentDigit];
268 
269  // Implement pulse width modulation PWM, using the following boundaries:
270  //
271  // * If brightness == 0, then turn the digit OFF 100% of the time.
272  // * If brightness >= T_SUBFIELDS, turn the digit ON 100% of the time.
273  //
274  // The mCurrentSubField is incremented modulo T_SUBFIELDS, so will always
275  // be in the range of [0, T_SUBFIELDS-1]. The brightness will always be <=
276  // T_SUBFIELDS, with the value of T_SUBFIELDS being 100% bright. So if we
277  // turn on the LED when (mCurrentSubField < brightness), we get the
278  // desired outcome.
279  const uint8_t pattern = (mCurrentSubField < brightness)
280  ? mPatterns[mCurrentDigit]
281  : 0;
282 
283  if (pattern != mPattern || mCurrentDigit != mPrevDigit) {
284  mLedMatrix.draw(mCurrentDigit, pattern);
285  mPattern = pattern;
286  }
287 
288  mCurrentSubField++;
289  mPrevDigit = mCurrentDigit;
290  if (mCurrentSubField >= T_SUBFIELDS) {
291  ace_common::incrementMod(mCurrentDigit, T_DIGITS);
292  mCurrentSubField = 0;
293  }
294  }
295 
296  private:
297  // The ordering of the fields below partially motivated to save memory on
298  // 32-bit processors.
299 
301  const T_LM& mLedMatrix;
302 
304  volatile uint8_t mPatterns[T_DIGITS];
305 
307  volatile uint8_t mBrightnesses[T_DIGITS];
308 
309  //-----------------------------------------------------------------------
310  // Variables needed by renderFieldWhenReady() to render frames and fields at
311  // a certain rate per second.
312  //-----------------------------------------------------------------------
313 
315  uint16_t mMicrosPerField;
316 
318  uint16_t mLastRenderFieldMicros;
319 
321  uint8_t const mFramesPerSecond;
322 
323  //-----------------------------------------------------------------------
324  // Variables needed to keep track of the multiplexing of the digits,
325  // and PWM of a single digit.
326  //-----------------------------------------------------------------------
327 
333  uint8_t mCurrentDigit;
334 
342  uint8_t mPrevDigit;
343 
348  uint8_t mCurrentSubField;
349 
355  uint8_t mPattern;
356 };
357 
358 }
359 
360 #endif
ace_segment::ScanningModule::renderFieldNow
void renderFieldNow()
Render the current field immediately.
Definition: ScanningModule.h:241
ace_segment::ScanningModule::end
void end()
A no-op end() function for consistency with other classes.
Definition: ScanningModule.h:128
ace_segment::LedModule
General interface that represents a generic seven-segment LED module with multiple digits.
Definition: LedModule.h:44
ace_segment::ScanningModule::setBrightnessAt
void setBrightnessAt(uint8_t pos, uint8_t brightness)
Set the brightness for a given pos, leaving pattern unchanged.
Definition: ScanningModule.h:185
ace_segment::ScanningModule
An implementation of LedModule for display modules which do not have hardware controller chips,...
Definition: ScanningModule.h:80
ace_segment::ScanningModule::getNumDigits
uint8_t getNumDigits() const
Get the number of digits.
Definition: ScanningModule.h:135
ace_segment::ScanningModule::getPatternAt
uint8_t getPatternAt(uint8_t pos) override
Get the led digit pattern at position pos.
Definition: ScanningModule.h:141
ace_segment::ScanningModule::begin
void begin()
Configure the driver with the parameters given by in the constructor.
Definition: ScanningModule.h:108
ace_segment::ScanningModule::getFieldsPerSecond
uint16_t getFieldsPerSecond() const
Return the fields per second.
Definition: ScanningModule.h:199
ace_segment::ScanningModule::ScanningModule
ScanningModule(const T_LM &ledMatrix, uint8_t framesPerSecond)
Constructor.
Definition: ScanningModule.h:93
ace_segment::ScanningModule::getFieldsPerFrame
uint16_t getFieldsPerFrame() const
Total fields per frame across all digits.
Definition: ScanningModule.h:204
ace_segment::ScanningModule::getFramesPerSecond
uint16_t getFramesPerSecond() const
Return the requested frames per second.
Definition: ScanningModule.h:196
ace_segment::ScanningModule::renderFieldWhenReady
bool renderFieldWhenReady()
Display one field of a frame when the time is right.
Definition: ScanningModule.h:220
ace_segment::ScanningModule::setBrightness
void setBrightness(uint8_t brightness) override
Definition: ScanningModule.h:151
ace_segment::ScanningModule::setPatternAt
void setPatternAt(uint8_t pos, uint8_t pattern) override
Set the led digit pattern at position pos.
Definition: ScanningModule.h:137
ace_segment::ScanningModule::getMicrosPerField
uint16_t getMicrosPerField() const
Return micros per field.
Definition: ScanningModule.h:210