Arduino Filters master
Filter library for Arduino
MAX7219SevenSegmentDisplay.hpp
Go to the documentation of this file.
1#pragma once
2
4AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5
6#include "MAX7219_Base.hpp"
7
9
22static constexpr uint8_t SevenSegmentCharacters[0x40] = {
23 0b00000000, 0b01110111, 0b00011111, 0b01001110, // @ A B C
24 0b00111101, 0b01001111, 0b01000111, 0b01011110, // D E F G
25 0b00010111, 0b00110000, 0b00111100, 0b00000111, // H I J K
26 0b00001110, 0b01110110, 0b00010101, 0b00011101, // L M N O
27 0b01100111, 0b01110011, 0b00000101, 0b01011011, // P Q R S
28 0b00001111, 0b00011100, 0b00100111, 0b00111110, // T U V W
29 0b00110111, 0b00111011, 0b01101101, 0b01001110, // X Y Z [
30 0b00010011, 0b01111000, 0b01100010, 0b00001000, // \ ] ^ _
31 0b00000000, 0b00000000, 0b00100010, 0b01100011, // ! " #
32 0b01011011, 0b01100111, 0b01111101, 0b00000010, // $ % & '
33 0b01001110, 0b01111000, 0b01100011, 0b00110001, // ( ) * +
34 0b00000100, 0b00000001, 0b00001000, 0b00100101, // , - . /
35 0b01111110, 0b00110000, 0b01101101, 0b01111001, // 0 1 2 3
36 0b00110011, 0b01011011, 0b01011111, 0b01110000, // 4 5 6 7
37 0b01111111, 0b01111011, 0b00000000, 0b01000100, // 8 9 : ;
38 0b00110001, 0b00001001, 0b00000111, 0b01100101, // < = > ?
39};
40
41static constexpr const uint8_t *AlphaChars = &SevenSegmentCharacters[0x01];
42static constexpr const uint8_t *NumericChars = &SevenSegmentCharacters[0x30];
43
52template <class SPIDriver = decltype(SPI) &>
53class MAX7219SevenSegmentDisplay : public MAX7219_Base<SPIDriver> {
54 public:
66 uint8_t chainlength = 1)
67 : MAX7219_Base<SPIDriver>(std::forward<SPIDriver>(spi), loadPin,
68 chainlength) {}
69
73
85 void sendDigit(uint16_t digit, uint8_t value) {
86 this->sendRaw((digit % 8) + 1, value, digit / 8);
87 }
88
93 uint8_t getNumberOfDigits() const { return 8 * this->getChainLength(); }
94
115 int16_t display(long number, int16_t startDigit = 0,
116 int16_t endDigit = -1) {
117 if (startDigit < 0)
118 startDigit += getNumberOfDigits();
119 if (endDigit < 0)
120 endDigit += getNumberOfDigits();
121 unsigned long anumber = abs(number);
122 int16_t i = startDigit;
123 do {
124 sendDigit(i++, NumericChars[anumber % 10]);
125 anumber /= 10;
126 } while (anumber && i <= endDigit);
127 if (number < 0 && i <= endDigit) {
128 sendDigit(i++, 0b00000001); // minus sign
129 }
130 if (anumber != 0) {
131 for (int16_t i = startDigit; i <= endDigit;)
132 sendDigit(i++, 0b00000001);
133 } else {
134 // clear unused digits within range
135 while (i <= endDigit)
136 sendDigit(i++, 0b00000000);
137 }
138 return endDigit - startDigit;
139 }
140
142 int16_t display(int number, int16_t startDigit = 0, int16_t endDigit = -1) {
143 return display(long(number), startDigit, endDigit);
144 }
145
166 int16_t display(unsigned long number, int16_t startDigit = 0,
167 int16_t endDigit = -1) {
168 if (startDigit < 0)
169 startDigit += getNumberOfDigits();
170 if (endDigit < 0)
171 endDigit += getNumberOfDigits();
172 int16_t i = startDigit;
173 do {
174 sendDigit(i++, NumericChars[number % 10]);
175 number /= 10;
176 } while (number && i <= endDigit);
177 if (number != 0) {
178 for (int16_t i = startDigit; i <= endDigit;)
179 sendDigit(i++, 0b00000001);
180 } else {
181 // clear unused digits within range
182 while (i <= endDigit)
183 sendDigit(i++, 0b00000000);
184 }
185 return endDigit - startDigit;
186 }
187
189 int16_t display(unsigned int number, int16_t startDigit = 0,
190 int16_t endDigit = -1) {
191 return display((unsigned long)(number), startDigit, endDigit);
192 }
193
204 int16_t display(const char *text, int16_t startPos = 0) {
205 if (startPos < 0)
206 startPos += getNumberOfDigits();
207 int16_t i = getNumberOfDigits() - startPos;
208 char prevD = '\0';
209 while (*text && (i > 0 || *text == '.')) {
210 char c = *text++;
211 uint8_t d = 0;
212 if (c == '.') {
213 if (prevD) {
214 sendDigit(i, prevD | 0b10000000);
215 prevD = '\0';
216 continue;
217 } else {
218 sendDigit(--i, 0b10000000);
219 continue;
220 }
221 } else if (c >= '@' && c <= '_')
222 d = SevenSegmentCharacters[(uint8_t)c - '@'];
223 else if (c >= '!' && c <= '?')
224 d = SevenSegmentCharacters[(uint8_t)c];
225 else if (c >= 'a' && c <= 'z')
226 d = SevenSegmentCharacters[(uint8_t)c - 'a' + 'A' - '@'];
227 sendDigit(--i, d);
228 prevD = d;
229 }
230 return getNumberOfDigits() - i - startPos;
231 }
232
241 void printHexChar(int16_t digit, uint8_t value) {
242 if (digit < 0)
243 digit += getNumberOfDigits();
244 value &= 0x0F;
245 uint8_t c = value >= 0x0A //
246 ? AlphaChars[value - 0x0A]
247 : NumericChars[value];
248 sendDigit(digit, c);
249 }
250
269 int16_t displayHex(unsigned long number, int16_t startDigit = 0,
270 int16_t endDigit = -1) {
271 if (startDigit < 0)
272 startDigit += getNumberOfDigits();
273 if (endDigit < 0)
274 endDigit += getNumberOfDigits();
275 int16_t i = startDigit;
276 do {
277 printHexChar(i++, uint8_t(number));
278 number >>= 4;
279 } while (number && i <= endDigit);
280 if (number != 0) {
281 for (int16_t i = startDigit; i <= endDigit;)
282 sendDigit(i++, 0b00000001);
283 } else {
284 // clear unused digits within range
285 while (i <= endDigit)
286 sendDigit(i++, 0b00000000);
287 }
288 return endDigit - startDigit;
289 }
290};
291
293
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
static constexpr const uint8_t * NumericChars
static constexpr const uint8_t * AlphaChars
static constexpr uint8_t SevenSegmentCharacters[0x40]
A lookup table for 7-segment characters, as specified by the Logic Control manual.
#define END_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:36
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:35
A class for 8-digit 7-segment displays with a MAX7219 driver.
uint8_t getNumberOfDigits() const
Get the total number of digits in this chain of displays, i.e.
int16_t display(unsigned int number, int16_t startDigit=0, int16_t endDigit=-1)
Display a long unsigned integer to the display.
int16_t display(long number, int16_t startDigit=0, int16_t endDigit=-1)
Display a long integer to the display.
int16_t display(const char *text, int16_t startPos=0)
Display a string of text to the display.
int16_t displayHex(unsigned long number, int16_t startDigit=0, int16_t endDigit=-1)
Print a number to the display in hexadecimal format.
int16_t display(int number, int16_t startDigit=0, int16_t endDigit=-1)
Display a long integer to the display.
MAX7219SevenSegmentDisplay(SPIDriver spi, pin_t loadPin, uint8_t chainlength=1)
Create a MAX7219SevenSegmentDisplay.
void sendDigit(uint16_t digit, uint8_t value)
Set the value of a single digit.
int16_t display(unsigned long number, int16_t startDigit=0, int16_t endDigit=-1)
Display a long unsigned integer to the display.
void printHexChar(int16_t digit, uint8_t value)
Print a single hexadecimal digit.
A base class for classes that control MAX7219 LED drivers.
uint8_t getChainLength() const
Get the number of daisy-chained chips.
void sendRaw(uint8_t opcode, uint8_t value, uint8_t chip=0)
Send a raw opcode and value to the given MAX7219.
void begin()
Initialize the Arduino pins, SPI, and the MAX7219.
Definition: vector.cpp:5