Control Surface  1.2.0
MIDI Control Surface library for Arduino
SevenSegmentDisplay.hpp
Go to the documentation of this file.
1 #pragma once
2 
4 #include <Print.h>
5 #include <string.h>
6 
8 
9 namespace MCU {
10 
11 template <uint8_t LENGTH>
12 class SevenSegmentDisplay : public MIDIInputElementCC, public Printable {
13  public:
21  }
22 
23  void fillWithSpaces() {
24  for (char &c : text)
25  c = ' ';
26  }
27 
28  void reset() override {
29 #ifdef SEVENSEG_RESET
31 #endif
32  }
33 
34  private:
38  virtual bool updateImpl(const ChannelMessageMatcher &midimsg,
39  const MIDIAddress &target) override {
40  uint8_t index = LENGTH - 1 - getRangeIndex(target);
41  // MIDI msg: character data → bits 0-5
42  // MIDI msg: decimal point → bit 6 set, no decimal point → bit 6 not set
43  // text: decimal point → bit 7 set, no decimal point → bit 7 not set
44  uint8_t decimalPt = (midimsg.data2 & 0x40) << 1;
45  uint8_t chardata = midimsg.data2 & 0x3F;
46  uint8_t character = chardata >= 0x20 ? chardata : chardata + 0x40;
47  character |= decimalPt;
48  text[index] = character;
49  return true;
50  }
51 
54  bool match(const MIDIAddress &target) const override {
56  LENGTH);
57  }
58 
60  uint8_t getRangeIndex(const MIDIAddress &target) const {
61  return target.getAddress() - this->address.getAddress();
62  }
63 
64  public:
78  void getText(char *buffer, uint8_t offset = 0,
79  uint8_t length = LENGTH) const {
80  if (offset >= LENGTH)
81  offset = LENGTH - 1;
82  if (length > LENGTH - offset)
83  length = LENGTH - offset;
84  for (uint8_t i = 0; i < length; i++)
85  buffer[i] = getCharacterAt(i + offset);
86  buffer[length] = '\0';
87  }
88 
93  char getCharacterAt(uint8_t index) const { return text[index] & 0x7F; }
94 
102  void getDecimalPoints(bool *buffer) const {
103  for (uint8_t i = 0; i < LENGTH; i++)
104  buffer[i] = getDecimalPointAt(i);
105  }
106 
111  bool getDecimalPointAt(uint8_t index) const { return text[index] & 0x80; }
112 
116  size_t printTo(Print &printer) const override {
117  size_t s = 0;
118  for (uint8_t i = 0; i < LENGTH; i++) {
119  s += printer.print(getCharacterAt(i));
120  if (getDecimalPointAt(i))
121  s += printer.print('.');
122  }
123  return s;
124  }
125 
126  private:
127  char text[LENGTH];
128 };
129 
130 } // namespace MCU
131 
MCU::SevenSegmentDisplay::getRangeIndex
uint8_t getRangeIndex(const MIDIAddress &target) const
Definition: SevenSegmentDisplay.hpp:60
MIDIAddress
A type-safe utility class for saving a MIDI address consisting of a 7-bit address,...
Definition: MIDIAddress.hpp:91
MIDIInputElement::address
const MIDIAddress address
Definition: MIDIInputElement.hpp:83
MCU::SevenSegmentDisplay::match
bool match(const MIDIAddress &target) const override
Check if the address of the incoming MIDI message is within the range of addresses of this element.
Definition: SevenSegmentDisplay.hpp:54
MCU::SevenSegmentDisplay::updateImpl
virtual bool updateImpl(const ChannelMessageMatcher &midimsg, const MIDIAddress &target) override
Update a character.
Definition: SevenSegmentDisplay.hpp:38
ChannelMessageMatcher::data2
uint8_t data2
Definition: ChannelMessageMatcher.hpp:20
MCU
Definition: LCDDisplay.hpp:10
MCU::SevenSegmentDisplay::getDecimalPoints
void getDecimalPoints(bool *buffer) const
Copy the decimal points into the given buffer.
Definition: SevenSegmentDisplay.hpp:102
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:9
MIDIInputElementCC
Class for objects that listen for incoming MIDI Controller Change events.
Definition: MIDIInputElementCC.hpp:26
MCU::SevenSegmentDisplay::getDecimalPointAt
bool getDecimalPointAt(uint8_t index) const
Get the decimal point state at the given index.
Definition: SevenSegmentDisplay.hpp:111
ChannelMessageMatcher
Struct for easily matching MIDI messages.
Definition: ChannelMessageMatcher.hpp:10
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: Settings/NamespaceSettings.hpp:10
MIDIAddress::matchAddressInRange
static bool matchAddressInRange(const MIDIAddress &toMatch, const MIDIAddress &base, uint8_t length)
Check if an address falls within a range of MIDI addresses, starting with address base,...
Definition: MIDIAddress.cpp:46
MCU::SevenSegmentDisplay
Definition: SevenSegmentDisplay.hpp:12
MIDIInputElementCC.hpp
MCU::SevenSegmentDisplay::SevenSegmentDisplay
SevenSegmentDisplay(const MIDIAddress &address)
Constructor.
Definition: SevenSegmentDisplay.hpp:18
MCU::SevenSegmentDisplay::getText
void getText(char *buffer, uint8_t offset=0, uint8_t length=LENGTH) const
Copy the ASCII text into the given buffer.
Definition: SevenSegmentDisplay.hpp:78
MCU::SevenSegmentDisplay::reset
void reset() override
Reset the input element to its initial state.
Definition: SevenSegmentDisplay.hpp:28
MCU::SevenSegmentDisplay::printTo
size_t printTo(Print &printer) const override
Print out the text of the display to the given Print.
Definition: SevenSegmentDisplay.hpp:116
MCU::SevenSegmentDisplay::text
char text[LENGTH]
Definition: SevenSegmentDisplay.hpp:127
MIDIAddress::getAddress
constexpr uint8_t getAddress() const
Get the address [0, 127].
Definition: MIDIAddress.hpp:223
MCU::SevenSegmentDisplay::getCharacterAt
char getCharacterAt(uint8_t index) const
Get the character at the given index.
Definition: SevenSegmentDisplay.hpp:93
MCU::SevenSegmentDisplay::fillWithSpaces
void fillWithSpaces()
Definition: SevenSegmentDisplay.hpp:23