-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSimplePicoMidiController.cpp
More file actions
81 lines (67 loc) · 2.08 KB
/
SimplePicoMidiController.cpp
File metadata and controls
81 lines (67 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#include "bsp/board.h"
#include "hardware/gpio.h"
#include "PicoDebounceButton/PicoDebounceButton.hpp"
#include "PicoResponsiveAnalogRead/PicoResponsiveAnalogRead.hpp"
#include "PicoOSC/PicoOSC.hpp"
#include "pico/stdlib.h"
#include "tusb.h"
#include <cstdint>
#include <stdio.h>
// Setup Pin 1 as an input
constexpr auto button_pin = 0;
auto debounceButton =
picodebounce::PicoDebounceButton(button_pin, 10, picodebounce::PicoDebounceButton::PRESSED, false);
// Midi buffer
constexpr auto buffer_size = 3;
uint8_t midibuffer[buffer_size];
void sendNoteOn(unsigned int note, unsigned int velocity,
unsigned int channel) {
// Create and send a single MIDI note on message
const auto cable_num = 0;
const auto eventType = 0x90;
midibuffer[0] = static_cast<uint8_t>(eventType | channel);
midibuffer[1] = note;
midibuffer[2] = velocity;
tud_midi_stream_write(cable_num, midibuffer, buffer_size);
}
void sendNoteOff(unsigned int note, unsigned int velocity,
unsigned int channel) {
// Create and send a single MIDI note off message
const auto cable_num = 0;
const auto eventType = 0x80;
midibuffer[0] = static_cast<uint8_t>(eventType | channel);
// midibuffer[1] = static_cast<uint8_t>(eventType | channel);
midibuffer[1] = note;
midibuffer[2] = velocity;
tud_midi_stream_write(cable_num, midibuffer, buffer_size);
}
// This is where the button is being read and the appropriate functions called
void buttonReadTask() {
// Read the button state
auto newButtonReadState = debounceButton.update();
// If the button state has changed
if (newButtonReadState) {
// If the button is pressed
if (debounceButton.getPressed()) {
// Send a note on message
sendNoteOn(60, 127, 0);
} else {
// Send a note off message
sendNoteOff(60, 127, 0);
}
}
}
int main() {
board_init();
stdio_init_all();
tusb_init();
// Setup button pin
gpio_init(button_pin);
gpio_set_dir(button_pin, GPIO_IN);
gpio_pull_up(button_pin);
while (true) {
buttonReadTask();
tud_task(); // tinyusb device task
}
return 0;
}