This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
MAX7219SevenSegmentDisplay.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include "MAX7219_Base.hpp"
4 
6 
19 static constexpr uint8_t SevenSegmentCharacters[0x40] = {
20  0b00000000, 0b01110111, 0b00011111, 0b01001110, // @ A B C
21  0b00111101, 0b01001111, 0b01000111, 0b01011110, // D E F G
22  0b00010111, 0b00110000, 0b00111100, 0b00000111, // H I J K
23  0b00001110, 0b01110110, 0b00010101, 0b00011101, // L M N O
24  0b01100111, 0b01110011, 0b00000101, 0b01011011, // P Q R S
25  0b00001111, 0b00011100, 0b00100111, 0b00111110, // T U V W
26  0b00110111, 0b00111011, 0b01101101, 0b01001110, // X Y Z [
27  0b00010011, 0b01111000, 0b01100010, 0b00001000, // \ ] ^ _
28  0b00000000, 0b00000000, 0b00100010, 0b01100011, // ! " #
29  0b01011011, 0b01100111, 0b01111101, 0b00000010, // $ % & '
30  0b01001110, 0b01111000, 0b01100011, 0b00110001, // ( ) * +
31  0b00000100, 0b00000001, 0b00001000, 0b00100101, // , - . /
32  0b01111110, 0b00110000, 0b01101101, 0b01111001, // 0 1 2 3
33  0b00110011, 0b01011011, 0b01011111, 0b01110000, // 4 5 6 7
34  0b01111111, 0b01111011, 0b00000000, 0b01000100, // 8 9 : ;
35  0b00110001, 0b00001001, 0b00000111, 0b01100101, // < = > ?
36 };
37 
38 static constexpr const uint8_t *AlphaChars = &SevenSegmentCharacters[0x01];
39 static constexpr const uint8_t *NumericChars = &SevenSegmentCharacters[0x30];
40 
47  public:
55 
58  void begin() { init(); }
59 
80  uint8_t display(long number, uint8_t startDigit = 0, uint8_t endDigit = 7) {
81  long anumber = abs(number);
82  uint8_t i = startDigit + 1;
83  endDigit++;
84  do {
85  sendRaw(i++, NumericChars[anumber % 10]);
86  anumber /= 10;
87  } while (anumber && i <= endDigit);
88  if (number < 0 && i <= endDigit)
89  sendRaw(i++, 0b00000001); // minus sign
90  while (i <= endDigit)
91  sendRaw(i++, 0b00000000); // clear unused digits within range
92  return endDigit - startDigit;
93  }
94 
105  uint8_t display(const char *text, uint8_t startPos = 0) {
106  uint8_t i = 8 - startPos;
107  char prevD = '\0';
108  while (*text && (i > 0 || *text == '.')) {
109  char c = *text++;
110  uint8_t d = 0;
111  if (c == '.') {
112  if (prevD) {
113  sendRaw(1 + i, prevD | 0b10000000);
114  prevD = '\0';
115  continue;
116  } else {
117  sendRaw(i--, 0b10000000);
118  continue;
119  }
120  } else if (c >= '@' && c <= '_')
121  d = SevenSegmentCharacters[(uint8_t)c - '@'];
122  else if (c >= '!' && c <= '?')
123  d = SevenSegmentCharacters[(uint8_t)c];
124  else if (c >= 'a' && c <= 'z')
125  d = SevenSegmentCharacters[(uint8_t)c - 'a' + 'A' - '@'];
126  sendRaw(i--, d);
127  prevD = d;
128  }
129  return 8 - i - startPos;
130  }
131 
137  template <uint8_t N>
138  uint8_t display(const uint8_t (&characters)[N], uint8_t startPos = 0) {
139  uint8_t i = 8 - startPos;
140  const uint8_t *char_p = &characters[0];
141  const uint8_t *const end_p = &characters[N];
142  while (i && char_p < end_p) {
143  uint8_t c = *char_p++;
144  sendRaw(i--, SevenSegmentCharacters[(uint8_t)c & 0x3F] |
145  (((uint8_t)c & 0x40) << 1));
146  }
147  return 8 - i - startPos;
148  }
149 
158  void printHex(uint8_t digit, uint8_t value) {
159  value &= 0x0F;
160  uint8_t c =
161  value >= 0xA ? AlphaChars[value - 0x0A] : NumericChars[value];
162  send(digit, c);
163  }
164 };
165 
MAX7219_Base
A base class for classes that control MAX7219 LED drivers.
Definition: MAX7219_Base.hpp:17
MAX7219SevenSegmentDisplay::display
uint8_t display(const uint8_t(&characters)[N], uint8_t startPos=0)
?
Definition: MAX7219SevenSegmentDisplay.hpp:138
pin_t
uint16_t pin_t
The type for Arduino pins (and ExtendedIOElement pins).
Definition: Def.hpp:17
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
MAX7219SevenSegmentDisplay::printHex
void printHex(uint8_t digit, uint8_t value)
Print a single hexadecimal digit.
Definition: MAX7219SevenSegmentDisplay.hpp:158
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
MAX7219_Base::loadPin
pin_t loadPin
Definition: MAX7219_Base.hpp:96
AlphaChars
static constexpr const uint8_t * AlphaChars
Definition: MAX7219SevenSegmentDisplay.hpp:38
MAX7219SevenSegmentDisplay::MAX7219SevenSegmentDisplay
MAX7219SevenSegmentDisplay(pin_t loadPin)
Create a MAX7219SevenSegmentDisplay.
Definition: MAX7219SevenSegmentDisplay.hpp:54
MAX7219SevenSegmentDisplay::begin
void begin()
Initialize.
Definition: MAX7219SevenSegmentDisplay.hpp:58
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:80
NumericChars
static constexpr const uint8_t * NumericChars
Definition: MAX7219SevenSegmentDisplay.hpp:39
MAX7219SevenSegmentDisplay
A class for 8-digit 7-segment displays with a MAX7219 driver.
Definition: MAX7219SevenSegmentDisplay.hpp:46
MAX7219_Base.hpp
MAX7219_Base::init
void init()
Initialize the Arduino pins, SPI, and the MAX7219.
Definition: MAX7219_Base.hpp:36
MAX7219SevenSegmentDisplay::display
uint8_t display(const char *text, uint8_t startPos=0)
Display a string of text to the display.
Definition: MAX7219SevenSegmentDisplay.hpp:105
MAX7219_Base::send
void send(uint8_t digit, uint8_t value)
Send the value to the given digit.
Definition: MAX7219_Base.hpp:64
MAX7219_Base::sendRaw
void sendRaw(uint8_t opcode, uint8_t value)
Send a raw opcode and value to the MAX7219.
Definition: MAX7219_Base.hpp:76
SevenSegmentCharacters
static constexpr uint8_t SevenSegmentCharacters[0x40]
A lookup table for 7-segment characters, as specified by the Logic Control manual.
Definition: MAX7219SevenSegmentDisplay.hpp:19