cs-midi-docs/docs/manual/01-getting-started/02-first-output.md

96 lines
2.2 KiB
Markdown

# First Output
> Original: [Getting Started — First Output](https://tttapa.github.io/Control-Surface/Doxygen/d5/d7d/md_pages_Getting-Started.html)
A minimal sketch that reads a physical control and sends MIDI.
## 1. Include the library
```cpp pico
#include "pico/stdlib.h"
#include <cs_midi.h>
using namespace cs;
```
`cs_midi.h` provides all Control Surface classes. The `cs::` namespace contains everything; `using namespace cs` keeps examples concise.
## 2. Instantiate a MIDI interface
At least one MIDI interface must exist before `Control_Surface.begin()`.
```cpp pico
BluetoothMIDI_Interface midi;
```
Available interfaces:
| Class | Transport | CMake option |
|-------|-----------|-------------|
| `BluetoothMIDI_Interface` | BLE MIDI (BTstack) | `CS_MIDI_BLE` |
| `USBMIDI_Interface` | USB MIDI (TinyUSB) | `CS_MIDI_USB` |
| `HardwareSerialMIDI_Interface` | 5-pin DIN MIDI | `CS_MIDI_SERIAL` |
| `AppleMIDI_Interface` | RTP-MIDI over WiFi | `CS_MIDI_APPLEMIDI` |
## 3. Add MIDI output elements
Output elements read physical inputs and send MIDI messages automatically.
A single potentiometer sending CC:
```cpp pico
CCPotentiometer pot {26, {MIDI_CC::Channel_Volume, Channel_1}};
```
Constructor arguments: GPIO pin, then MIDI address (controller number, channel).
Multiple buttons:
```cpp pico
NoteButton buttons[] {
{5, {MIDI_Notes::C[4], Channel_1}},
{6, {MIDI_Notes::D[4], Channel_1}},
{7, {MIDI_Notes::E[4], Channel_1}},
};
```
## 4. Initialize and run
```cpp pico
int main() {
stdio_init_all();
Control_Surface.begin();
while (true) {
Control_Surface.loop();
sleep_ms(1);
}
}
```
`Control_Surface.begin()` initializes all interfaces and elements. `Control_Surface.loop()` polls inputs and sends MIDI when values change.
## Complete example
```cpp pico
#include "pico/stdlib.h"
#include "pico/cyw43_arch.h"
#include <cs_midi.h>
using namespace cs;
BluetoothMIDI_Interface midi;
CCPotentiometer pot {26, {MIDI_CC::Channel_Volume, Channel_1}};
NoteButton button {5, {MIDI_Notes::C[4], Channel_1}};
int main() {
stdio_init_all();
if (cyw43_arch_init()) return 1;
Control_Surface.begin();
while (true) {
Control_Surface.loop();
sleep_ms(1);
}
}
```