AceSegment  0.3.0
An adjustable, configurable, and extensible framework for rendering seven segment LED displays.
Renderer.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_RENDERER_H
26 #define ACE_SEGMENT_RENDERER_H
27 
28 #include <stdint.h>
29 #include "Driver.h"
30 #include "TimingStats.h"
31 
32 namespace ace_segment {
33 
34 class StyledPattern;
35 class Hardware;
36 class Styler;
37 
48 class Renderer {
49  public:
55  static const uint8_t kNumStyles = 6;
56 
58  explicit Renderer(Hardware* hardware, Driver* driver,
59  StyledPattern* styledPatterns, uint8_t numDigits,
60  uint8_t framesPerSecond, uint16_t statsResetInterval,
61  Styler** stylers):
62  mHardware(hardware),
63  mDriver(driver),
64  mStyledPatterns(styledPatterns),
65  mNumDigits(numDigits),
66  mFramesPerSecond(framesPerSecond),
67  mStatsResetInterval(statsResetInterval)
68  {
69  for (uint8_t i = 0; i < kNumStyles; i++) {
70  mStylers[i] = (stylers != nullptr) ? stylers[i] : nullptr;
71  }
72  }
73 
75  virtual ~Renderer() {}
76 
83  virtual void configure();
84 
86  uint8_t getNumDigits() { return mNumDigits; }
87 
89  uint16_t getFramesPerSecond() { return mFramesPerSecond; }
90 
92  uint16_t getFieldsPerSecond() {
93  return mFramesPerSecond * mDriver->getFieldsPerFrame();
94  }
95 
103  void writeBrightness(uint8_t brightness) {
104  mBrightness = brightness;
105  }
106 
112  void writePatternAt(uint8_t digit, uint8_t pattern, uint8_t style);
113 
118  void writePatternAt(uint8_t digit, uint8_t pattern);
119 
124  void writeStyleAt(uint8_t digit, uint8_t style);
125 
127  void writeDecimalPointAt(uint8_t digit, bool state = true);
128 
130  void clear();
131 
141  bool renderFieldWhenReady();
142 
147  void renderField();
148 
153  // TODO: make this a pointer and make stats gathering optional
155 
158  return mStyledPatterns[i];
159  }
160 
165  bool isStylerSupported(Styler* styler);
166 
168  uint8_t* getActiveStyles() { return mActiveStyles; }
169 
170  private:
171  // disable copy-constructor and assignment operator
172  Renderer(const Renderer&) = delete;
173  Renderer& operator=(const Renderer&) = delete;
174 
176  void updateFrame();
177 
179  void updateStylers();
180 
182  void renderStyledPatterns();
183 
184  Hardware* const mHardware;
185  Driver* const mDriver;
186  StyledPattern* const mStyledPatterns;
187  const uint8_t mNumDigits;
188  const uint8_t mFramesPerSecond;
189 
190  // statistics
191  const uint16_t mStatsResetInterval;
192  TimingStats mStats;
193 
194  // Array of Stylers. Index 0 is reserved and represents the "no-style"
195  // setting which does nothing. It cannot be set by the the client code.
196  Styler* mStylers[kNumStyles];
197 
198  // Count of the number of times the given style index is used in the
199  // mStyledPatterns array. We update this map during writePatternAt() and
200  // writeStyleAt() to avoid calculating this in renderField() which saves
201  // CPU cycles.
202  uint8_t mActiveStyles[kNumStyles];
203 
204  // global brightness, can be changed during runtime
205  uint8_t mBrightness = 255;
206 
207  // does the Driver support brightness?
208  bool mIsBrightnessEnabled = false;
209 
210  // variables to support renderFieldWhenReady()
211  uint16_t mMicrosPerField;
212  uint16_t mLastRenderFieldMicros;
213 
214  // each call to renderField() increment current field counter modulo the
215  // fieldsPerFrame, so certain calculations can be performed once per frame
216  uint16_t mFieldsPerFrame;
217  uint16_t mCurrentField;
218 };
219 
220 }
221 
222 #endif
uint16_t getFramesPerSecond()
Return the frames per second.
Definition: Renderer.h:89
StyledPattern & getStyledPattern(uint8_t i)
Return a reference the styled digit.
Definition: Renderer.h:157
bool renderFieldWhenReady()
Display one field of a frame when the time is right.
Definition: Renderer.cpp:104
TimingStats getTimingStats()
Return stats.
Definition: Renderer.cpp:156
static const uint8_t kNumStyles
Maximum number of styles.
Definition: Renderer.h:55
bool isStylerSupported(Styler *styler)
Return true if the given Styler is supported by the current Driver.
Definition: Renderer.cpp:150
void writePatternAt(uint8_t digit, uint8_t pattern, uint8_t style)
Write the pattern and style for a given digit.
Definition: Renderer.cpp:56
void renderField()
Render the current field immediately.
Definition: Renderer.cpp:116
void writeStyleAt(uint8_t digit, uint8_t style)
Write the style for a given digit, leaving pattern unchanged.
Definition: Renderer.cpp:76
virtual void configure()
Configure the driver with the parameters given by in the constructor.
Definition: Renderer.cpp:34
void writeBrightness(uint8_t brightness)
Set brightness expressed as a fraction of 256.
Definition: Renderer.h:103
A class that knows how to translate an array of led segement bit patterns with style attributes to a ...
Definition: Renderer.h:48
void clear()
Clear all digits, preserving the styles at each digit.
Definition: Renderer.cpp:98
uint16_t getFieldsPerSecond()
Return the fields per second.
Definition: Renderer.h:92
void writeDecimalPointAt(uint8_t digit, bool state=true)
Write the decimal point for the digit.
Definition: Renderer.cpp:89
Base class of drivers which knows how to transfer the bit patterns stored in the array of DimmablePat...
Definition: Driver.h:44
Renderer(Hardware *hardware, Driver *driver, StyledPattern *styledPatterns, uint8_t numDigits, uint8_t framesPerSecond, uint16_t statsResetInterval, Styler **stylers)
Constructor.
Definition: Renderer.h:58
virtual ~Renderer()
Destructor.
Definition: Renderer.h:75
uint8_t getNumDigits()
Get the number of digits.
Definition: Renderer.h:86
Data structure that keeps track of the state of each digit (its segment bit pattern and its style)...
Definition: StyledPattern.h:36
Interface for classes which apply a style to the given bit pattern and brightness.
Definition: Styler.h:34
virtual uint16_t getFieldsPerFrame()=0
Return number of fields per frame.
uint8_t * getActiveStyles()
Retrieve the array of active styles.
Definition: Renderer.h:168