This is an old version of the documentation. View the latest version here.
Control Surface  1.0.0
MIDI Control Surface library for Arduino
LCD.hpp
Go to the documentation of this file.
1 #pragma once
2 
3 #include <Helpers/Debug.hpp>
5 #include <string.h> // memcpy
6 
7 #ifndef ARDUINO
8 #include <cassert>
9 #endif
10 
12 
13 namespace MCU {
14 
15 class LCDCounter {
16  public:
19 
20  static uint8_t getInstances() { return instances; }
21 
22  private:
23  static uint8_t instances;
24 };
25 
26 template <uint8_t BufferSize = 120>
27 class LCD : public MIDIInputElementSysEx, private LCDCounter {
28  public:
29  LCD(uint8_t offset = 0, uint8_t CN = 0)
31  buffer[BufferSize] = '\0';
32  for (uint8_t i = 0; i < BufferSize; i++)
33  buffer[i] = ' ';
34  }
35 
36  const char *getText() const { return &buffer[0]; }
37 
38  private:
39  bool updateImpl(SysExMessage midimsg) override {
40  // Format:
41  // F0 mm mm mm nn 12 oo yy... F7
42  // mm = manufacturer ID (00 00 66 for Mackie)
43  // nn = model number (10 for Logic Control, 11 for Logic Control XT)
44  // oo = offset [0x00, 0x6F]
45  // yy... = ASCII data
46  if (midimsg.data[5] != 0x12)
47  return false;
48 
49  const uint8_t midiOffset = midimsg.data[6];
50  const uint8_t midiLength = midimsg.length - 8;
51  const uint8_t *text = midimsg.data + 7;
52  const uint8_t midiBufferEnd = midiOffset + midiLength;
53 
54  const uint8_t bufferEnd = this->offset + BufferSize;
55 
56  // no overlap between incoming range and this range
57  if (midiOffset >= bufferEnd || this->offset >= midiBufferEnd)
58  return getInstances() == 1;
59 
60  uint8_t srcStart = max(0, this->offset - midiOffset);
61  uint8_t dstStart = max(0, midiOffset - this->offset);
62  uint8_t length = midiBufferEnd - midiOffset -
63  max(0, this->offset - midiOffset) -
64  max(0, midiBufferEnd - bufferEnd);
65  // uint8_t length =
66  // BufferSize - //
67  // max(0, midiOffset - this->offset) - //
68  // max(0, BufferSize - midiLength - (midiOffset - this->offset)); //
69 
70  DEBUGVAL(this->offset, midiOffset, BufferSize, midiLength, srcStart,
71  dstStart, length);
72 
73 #ifdef ARDUINO
74  memcpy(&buffer[dstStart], &text[srcStart], length);
75 #else
76  for (uint8_t i = 0; i < length; ++i) {
77  buffer[dstStart + i] = text[srcStart + i];
78  assert(dstStart + i < BufferSize);
79  assert(srcStart + i < midiLength);
80  }
81 #endif
82 
83  DEBUGFN(getText());
84 
85  // If this is the only instance, the others don't have to be updated
86  // anymore
87  return getInstances() == 1;
88  }
89 
91  uint8_t offset;
92 };
93 
94 } // namespace MCU
95 
SysExMessage
Definition: MIDI_Parser.hpp:64
MIDIInputElementSysEx.hpp
MCU::LCDCounter::~LCDCounter
~LCDCounter()
Definition: LCD.hpp:18
MCU::LCDCounter::LCDCounter
LCDCounter()
Definition: LCD.hpp:17
MCU::LCD::updateImpl
bool updateImpl(SysExMessage midimsg) override
Definition: LCD.hpp:39
SysExMessage::length
uint8_t length
Definition: MIDI_Parser.hpp:73
MCU::LCD::buffer
Array< char, BufferSize+1 > buffer
Definition: LCD.hpp:90
MCU
Names and note and controller numbers for the Mackie Control Universal (MCU) protocol.
Definition: LCDDisplay.hpp:10
Array< char, BufferSize+1 >
MCU::LCD::LCD
LCD(uint8_t offset=0, uint8_t CN=0)
Definition: LCD.hpp:29
MCU::LCDCounter::getInstances
static uint8_t getInstances()
Definition: LCD.hpp:20
DEBUGVAL
#define DEBUGVAL(...)
Definition: Debug.hpp:172
BEGIN_CS_NAMESPACE
#define BEGIN_CS_NAMESPACE
Definition: NamespaceSettings.hpp:9
MCU::LCD::offset
uint8_t offset
Definition: LCD.hpp:91
DEBUGFN
#define DEBUGFN(x)
Definition: Debug.hpp:164
MIDIInputElementSysEx::CN
uint8_t CN
Definition: MIDIInputElementSysEx.hpp:121
MIDIInputElementSysEx
Class for objects that listen for incoming MIDI SysEx events.
Definition: MIDIInputElementSysEx.hpp:20
END_CS_NAMESPACE
#define END_CS_NAMESPACE
Definition: NamespaceSettings.hpp:10
MCU::LCDCounter
Definition: LCD.hpp:15
MCU::LCD::getText
const char * getText() const
Definition: LCD.hpp:36
max
constexpr auto max(const T &a, const U &b) -> decltype(b< a ? b :a)
Definition: MinMaxFix.hpp:19
MCU::LCD
Definition: LCD.hpp:27
MCU::LCDCounter::instances
static uint8_t instances
Definition: LCD.hpp:23
Debug.hpp
SysExMessage::data
const uint8_t * data
Definition: MIDI_Parser.hpp:72