Control Surface  1.1.0
MIDI Control Surface library for Arduino
MAX7219SevenSegmentDisplay.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 AH_DIAGNOSTIC_WERROR() // Enable errors on warnings
5 
6 #include "MAX7219_Base.hpp"
7 
9 
10 /**
11  * @brief A lookup table for 7-segment characters, as specified by the Logic
12  * Control manual.
13  *
14  * __A__
15  * | |
16  * F | | B
17  * |__G__|
18  * | |
19  * E | | C
20  * |__D__| o DP
21  */
22 static 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 
41 static constexpr const uint8_t *AlphaChars = &SevenSegmentCharacters[0x01];
42 static constexpr const uint8_t *NumericChars = &SevenSegmentCharacters[0x30];
43 
44 /**
45  * @brief A class for 8-digit 7-segment displays with a MAX7219 driver.
46  *
47  * @ingroup AH_HardwareUtils
48  */
50  public:
51  /**
52  * @brief Create a MAX7219SevenSegmentDisplay.
53  *
54  * @param loadPin
55  * The pin connected to the load pin (C̄S̄) of the MAX7219.
56  */
58 
59  /// Initialize.
60  /// @see MAX7219#init
61  void begin() { init(); }
62 
63  /**
64  * @brief Display a long integer number to the display.
65  * The number will be right-aligned.
66  *
67  * @param number
68  * The number to display.
69  * @param startDigit
70  * The digit (zero-based, counting from the right) to start
71  * printing the number.
72  * Digits: 7 6 5 4 3 2 1 0
73  * @param endDigit
74  * The last digit (zero-based, counting from the right) that can
75  * be printed.
76  * If the number is larger than `endDigit - startDigit`, the number
77  * is truncated on the left. If the number is smaller than
78  * `endDigit - startDigit`, the leftmost digits including
79  * `endDigit` are cleared.
80  * @return The number of digits that have been overwritten
81  * (`endDigit - startDigit`).
82  */
83  uint8_t display(long number, uint8_t startDigit = 0, uint8_t endDigit = 7) {
84  long anumber = abs(number);
85  uint8_t i = startDigit + 1;
86  endDigit++;
87  do {
88  sendRaw(i++, NumericChars[anumber % 10]);
89  anumber /= 10;
90  } while (anumber && i <= endDigit);
91  if (number < 0 && i <= endDigit)
92  sendRaw(i++, 0b00000001); // minus sign
93  while (i <= endDigit)
94  sendRaw(i++, 0b00000000); // clear unused digits within range
95  return endDigit - startDigit;
96  }
97 
98  /**
99  * @brief Display a string of text to the display.
100  *
101  * Full stops are printed to the decimal point between characters.
102  *
103  * @param text
104  * The null-terminated string to display.
105  * @param startPos
106  * The position to start printing.
107  */
108  uint8_t display(const char *text, uint8_t startPos = 0) {
109  uint8_t i = 8 - startPos;
110  char prevD = '\0';
111  while (*text && (i > 0 || *text == '.')) {
112  char c = *text++;
113  uint8_t d = 0;
114  if (c == '.') {
115  if (prevD) {
116  sendRaw(1 + i, prevD | 0b10000000);
117  prevD = '\0';
118  continue;
119  } else {
120  sendRaw(i--, 0b10000000);
121  continue;
122  }
123  } else if (c >= '@' && c <= '_')
124  d = SevenSegmentCharacters[(uint8_t)c - '@'];
125  else if (c >= '!' && c <= '?')
126  d = SevenSegmentCharacters[(uint8_t)c];
127  else if (c >= 'a' && c <= 'z')
128  d = SevenSegmentCharacters[(uint8_t)c - 'a' + 'A' - '@'];
129  sendRaw(i--, d);
130  prevD = d;
131  }
132  return 8 - i - startPos;
133  }
134 
135  /**
136  * @brief ?
137  *
138  * @todo Find out what this function does, and write documentation.
139  */
140  template <uint8_t N>
141  uint8_t display(const uint8_t (&characters)[N], uint8_t startPos = 0) {
142  uint8_t i = 8 - startPos;
143  const uint8_t *char_p = &characters[0];
144  const uint8_t *const end_p = &characters[N];
145  while (i && char_p < end_p) {
146  uint8_t c = *char_p++;
147  sendRaw(i--, SevenSegmentCharacters[(uint8_t)c & 0x3F] |
148  (((uint8_t)c & 0x40) << 1));
149  }
150  return 8 - i - startPos;
151  }
152 
153  /**
154  * @brief Print a single hexadecimal digit.
155  *
156  * @param digit
157  * The digit to print to [0, 7].
158  * @param value
159  * The 4-bit value to print [0, 15].
160  *
161  * @todo Rename to `printHexChar` and create function that actually
162  * prints longer hexadecimal numbers.
163  */
164  void printHex(uint8_t digit, uint8_t value) {
165  value &= 0x0F;
166  uint8_t c =
167  value >= 0xA ? AlphaChars[value - 0x0A] : NumericChars[value];
168  send(digit, c);
169  }
170 };
171 
173 
AH::MAX7219SevenSegmentDisplay
A class for 8-digit 7-segment displays with a MAX7219 driver.
Definition: MAX7219SevenSegmentDisplay.hpp:49
AH::MAX7219_Base
A base class for classes that control MAX7219 LED drivers.
Definition: MAX7219_Base.hpp:23
AH::NumericChars
static constexpr const uint8_t * NumericChars
Definition: MAX7219SevenSegmentDisplay.hpp:42
Warnings.hpp
AH::MAX7219SevenSegmentDisplay::display
uint8_t display(long number, uint8_t startDigit=0, uint8_t endDigit=7)
Display a long integer number to the display.
Definition: MAX7219SevenSegmentDisplay.hpp:83
AH_DIAGNOSTIC_POP
#define AH_DIAGNOSTIC_POP()
Definition: Warnings.hpp:17
AH::MAX7219SevenSegmentDisplay::display
uint8_t display(const char *text, uint8_t startPos=0)
Display a string of text to the display.
Definition: MAX7219SevenSegmentDisplay.hpp:108
AH::MAX7219SevenSegmentDisplay::begin
void begin()
Initialize.
Definition: MAX7219SevenSegmentDisplay.hpp:61
AH::SevenSegmentCharacters
static constexpr uint8_t SevenSegmentCharacters[0x40]
A lookup table for 7-segment characters, as specified by the Logic Control manual.
Definition: MAX7219SevenSegmentDisplay.hpp:22
AH::AlphaChars
static constexpr const uint8_t * AlphaChars
Definition: MAX7219SevenSegmentDisplay.hpp:41
AH::MAX7219SevenSegmentDisplay::printHex
void printHex(uint8_t digit, uint8_t value)
Print a single hexadecimal digit.
Definition: MAX7219SevenSegmentDisplay.hpp:164
AH::pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Hardware-Types.hpp:17
AH::MAX7219SevenSegmentDisplay::MAX7219SevenSegmentDisplay
MAX7219SevenSegmentDisplay(pin_t loadPin)
Create a MAX7219SevenSegmentDisplay.
Definition: MAX7219SevenSegmentDisplay.hpp:57
AH_DIAGNOSTIC_WERROR
#define AH_DIAGNOSTIC_WERROR()
Definition: Warnings.hpp:16
AH::MAX7219SevenSegmentDisplay::display
uint8_t display(const uint8_t(&characters)[N], uint8_t startPos=0)
?
Definition: MAX7219SevenSegmentDisplay.hpp:141
BEGIN_AH_NAMESPACE
#define BEGIN_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:9
END_AH_NAMESPACE
#define END_AH_NAMESPACE
Definition: AH/Settings/NamespaceSettings.hpp:10