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