Arduino Helpers master
Utility library for Arduino
MAX7219SevenSegmentDisplay.hpp
Go to the documentation of this file.
1#pragma once
2
3#include "MAX7219_Base.hpp"
4#include <AH/STL/cmath> // abs
5
7
20static constexpr uint8_t SevenSegmentCharacters[0x40] = {
21 0b00000000, 0b01110111, 0b00011111, 0b01001110, // @ A B C
22 0b00111101, 0b01001111, 0b01000111, 0b01011110, // D E F G
23 0b00010111, 0b00110000, 0b00111100, 0b00000111, // H I J K
24 0b00001110, 0b01110110, 0b00010101, 0b00011101, // L M N O
25 0b01100111, 0b01110011, 0b00000101, 0b01011011, // P Q R S
26 0b00001111, 0b00011100, 0b00100111, 0b00111110, // T U V W
27 0b00110111, 0b00111011, 0b01101101, 0b01001110, // X Y Z [
28 0b00010011, 0b01111000, 0b01100010, 0b00001000, // \ ] ^ _
29 0b00000000, 0b00000000, 0b00100010, 0b01100011, // ! " #
30 0b01011011, 0b01100111, 0b01111101, 0b00000010, // $ % & '
31 0b01001110, 0b01111000, 0b01100011, 0b00110001, // ( ) * +
32 0b00000100, 0b00000001, 0b00001000, 0b00100101, // , - . /
33 0b01111110, 0b00110000, 0b01101101, 0b01111001, // 0 1 2 3
34 0b00110011, 0b01011011, 0b01011111, 0b01110000, // 4 5 6 7
35 0b01111111, 0b01111011, 0b00000000, 0b01000100, // 8 9 : ;
36 0b00110001, 0b00001001, 0b00000111, 0b01100101, // < = > ?
37};
38
39static constexpr const uint8_t *AlphaChars = &SevenSegmentCharacters[0x01];
40static constexpr const uint8_t *NumericChars = &SevenSegmentCharacters[0x30];
41
50template <class SPIDriver = decltype(SPI) &>
51class MAX7219SevenSegmentDisplay : public MAX7219_Base<SPIDriver> {
52 public:
64 uint8_t chainlength = 1)
65 : MAX7219_Base<SPIDriver>(std::forward<SPIDriver>(spi), loadPin,
66 chainlength) {}
67
71
83 void sendDigit(uint16_t digit, uint8_t value) {
84 this->sendRaw((digit % 8) + 1, value, digit / 8);
85 }
86
91 uint8_t getNumberOfDigits() const { return 8 * this->getChainLength(); }
92
113 int16_t display(long number, int16_t startDigit = 0,
114 int16_t endDigit = -1) {
115 if (startDigit < 0)
116 startDigit += getNumberOfDigits();
117 if (endDigit < 0)
118 endDigit += getNumberOfDigits();
119 unsigned long anumber = std::abs(number);
120 int16_t i = startDigit;
121 do {
122 sendDigit(i++, NumericChars[anumber % 10]);
123 anumber /= 10;
124 } while (anumber && i <= endDigit);
125 if (number < 0 && i <= endDigit) {
126 sendDigit(i++, 0b00000001); // minus sign
127 }
128 if (anumber != 0) {
129 for (int16_t i = startDigit; i <= endDigit;)
130 sendDigit(i++, 0b00000001);
131 } else {
132 // clear unused digits within range
133 while (i <= endDigit)
134 sendDigit(i++, 0b00000000);
135 }
136 return endDigit - startDigit;
137 }
138
140 int16_t display(int number, int16_t startDigit = 0, int16_t endDigit = -1) {
141 return display(long(number), startDigit, endDigit);
142 }
143
164 int16_t display(unsigned long number, int16_t startDigit = 0,
165 int16_t endDigit = -1) {
166 if (startDigit < 0)
167 startDigit += getNumberOfDigits();
168 if (endDigit < 0)
169 endDigit += getNumberOfDigits();
170 int16_t i = startDigit;
171 do {
172 sendDigit(i++, NumericChars[number % 10]);
173 number /= 10;
174 } while (number && i <= endDigit);
175 if (number != 0) {
176 for (int16_t i = startDigit; i <= endDigit;)
177 sendDigit(i++, 0b00000001);
178 } else {
179 // clear unused digits within range
180 while (i <= endDigit)
181 sendDigit(i++, 0b00000000);
182 }
183 return endDigit - startDigit;
184 }
185
187 int16_t display(unsigned int number, int16_t startDigit = 0,
188 int16_t endDigit = -1) {
189 return display((unsigned long)(number), startDigit, endDigit);
190 }
191
202 int16_t display(const char *text, int16_t startPos = 0) {
203 if (startPos < 0)
204 startPos += getNumberOfDigits();
205 int16_t i = getNumberOfDigits() - startPos;
206 char prevD = '\0';
207 while (*text && (i > 0 || *text == '.')) {
208 char c = *text++;
209 uint8_t d = 0;
210 if (c == '.') {
211 if (prevD) {
212 sendDigit(i, prevD | 0b10000000);
213 prevD = '\0';
214 continue;
215 } else {
216 sendDigit(--i, 0b10000000);
217 continue;
218 }
219 } else if (c >= '@' && c <= '_')
220 d = SevenSegmentCharacters[(uint8_t)c - '@'];
221 else if (c >= '!' && c <= '?')
222 d = SevenSegmentCharacters[(uint8_t)c];
223 else if (c >= 'a' && c <= 'z')
224 d = SevenSegmentCharacters[(uint8_t)c - 'a' + 'A' - '@'];
225 sendDigit(--i, d);
226 prevD = d;
227 }
228 return getNumberOfDigits() - i - startPos;
229 }
230
239 void printHexChar(int16_t digit, uint8_t value) {
240 if (digit < 0)
241 digit += getNumberOfDigits();
242 value &= 0x0F;
243 uint8_t c = value >= 0x0A //
244 ? AlphaChars[value - 0x0A]
245 : NumericChars[value];
246 sendDigit(digit, c);
247 }
248
267 int16_t displayHex(unsigned long number, int16_t startDigit = 0,
268 int16_t endDigit = -1) {
269 if (startDigit < 0)
270 startDigit += getNumberOfDigits();
271 if (endDigit < 0)
272 endDigit += getNumberOfDigits();
273 int16_t i = startDigit;
274 do {
275 printHexChar(i++, uint8_t(number));
276 number >>= 4;
277 } while (number && i <= endDigit);
278 if (number != 0) {
279 for (int16_t i = startDigit; i <= endDigit;)
280 sendDigit(i++, 0b00000001);
281 } else {
282 // clear unused digits within range
283 while (i <= endDigit)
284 sendDigit(i++, 0b00000000);
285 }
286 return endDigit - startDigit;
287 }
288};
289
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
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