ハードシーケンサ開発。(その2・プログラム検証の続き)
次はタイマ割り込み+MIDI出力
たまに割り込みのときは特定ライブラリが使えないなんてこともあるので、
問題がないことを確認するためにサンプルプログラムを書いていた。
タイマー割り込みについては高レベルAPIが存在するのでそれを使うことにした。
static関数との付き合い方を考慮すれば、付き合い方はそれほど難しくない。
Adafruitのライブラリが使えるのですごい助かった。
あとはMIDIメッセージのパーシングは関数が用意されてるし、楽ではある。
#include <Arduino.h>;
#include <Adafruit_TinyUSB.h>;
#include <MIDI.h>;
// USB MIDI object
Adafruit_USBD_MIDI usb_midi;
// Create a new instance of the Arduino MIDI Library,
// and attach usb_midi as the transport.
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, MIDI);
int led = 25;
int cnt=0;
bool led_on=false;
int note_no=0;
struct repeating_timer st_tm1ms;
bool tm1ms(struct repeating_timer *t) {
const int led_time=1000;
if( cnt > led_time){
if(led_on==false){
note_no=random(20, 70);
digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level)
MIDI.sendNoteOn(note_no, 127, 1);
led_on=true;
}
else{
digitalWrite(led, LOW); // turn the LED off by making the voltage LOW
MIDI.sendNoteOff(note_no, 0, 1);
led_on=false;
}
cnt-=led_time;
}
cnt++;
//固定値でTrue返すよ。
return true;
}
// the setup routine runs once when you press reset:
void setup() {
#if defined(ARDUINO_ARCH_MBED) &amp;&amp; defined(ARDUINO_ARCH_RP2040)
// Manual begin() is required on core without built-in support for TinyUSB such as mbed rp2040
TinyUSB_Device_Init(0);
#endif
MIDI.begin(MIDI_CHANNEL_OMNI);
while( !TinyUSBDevice.mounted() )
delay(1);
// initialize the digital pin as an output.
pinMode(led, OUTPUT);
//割り込み命令
add_repeating_timer_us(1000, tm1ms, NULL, &amp;st_tm1ms); // タイマー割り込み起動(2ms)
}
// the loop routine runs over and over again forever:
void loop() {
}
参考資料
ラジオペンチ Raspberry Pi Picoで72LEDのクリスマスツリーイルミネーションを作る (fc2.com)
Raspberry Pi Pico SDK: repeating_timer
少し気をつけたほうがよさそうな項目
経過秒数を調べるので、こういう項目が必要になってきそう。
Arduino 日本語リファレンス (musashinodenpa.com)
ミリ秒のほかにナノ秒も計算できる
