Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active July 25, 2022 14:45
Show Gist options
  • Save todbot/69f8cdf22aa3e5818b8a85c4a86d6c4a to your computer and use it in GitHub Desktop.
Save todbot/69f8cdf22aa3e5818b8a85c4a86d6c4a to your computer and use it in GitHub Desktop.
Simple test of built-in DotStar on Trinket M0 (based on MIDI2Host code)
/*
* MIDIHost2Host -- Connect two USB-MIDI host devices together
* 2019 @todbot / Tod E. Kurt
*
* This sketch is meant to be installed on two Adafruit Trinket M0s.
* The connections between the two Trinket M0s are:
* - TrinketA Gnd --------------------- TrinketB Gnd
* - TrinketA pin 3 --- 1k resistor -- TrinketB pin 4
* - TrinketA pin 4 --- 1k resistor -- TrinketB pin 3
*
* When compiling:
* - Install libraries: Adafruit_TinyUSB & MIDI
* - Be sure to have updated all boards and libraries
* - Select "Tools" -> "USB Stack" -> "TinyUSB"
*
* .
* The following libraries are required:
* - Adafruit_TinyUSB library by Adafruit
* https://github.com/adafruit/Adafruit_TinyUSB_Arduino
* - MIDI Library by Forty Seven Effects
* https://github.com/FortySevenEffects/arduino_midi_library
*/
#include <Arduino.h>
#include <Adafruit_TinyUSB.h>
#include <MIDI.h>
#include <Adafruit_DotStar.h>
//#define DOTSTAR_DATPIN 7 // trinket m0
//#define DOTSTAR_CLKPIN 8 // trinket m0
#define DOTSTAR_DATPIN INTERNAL_DS_DATA
#define DOTSTAR_CLKPIN INTERNAL_DS_CLK
// Dotstar RGB LED
Adafruit_DotStar strip = Adafruit_DotStar(1, DOTSTAR_DATPIN,DOTSTAR_CLKPIN, DOTSTAR_BGR);
int RED = 0xFF0000;
int GREEN = 0x00FF00;
int BLUE = 0x0000FF;
int YELLOW = 0xAAAA00;
int BLACK = 0x000000;
int mycolors[] = {RED,GREEN,BLUE,YELLOW,BLACK};
int colorindex=0;
// USB MIDI object
Adafruit_USBD_MIDI usb_midi;
// Create instance of Arduino MIDI library,
// and attach usb_midi as the transport.
MIDI_CREATE_INSTANCE(Adafruit_USBD_MIDI, usb_midi, midiA);
// Create instance of Arduino MIDI library,
// and attach HardwareSerial Serial1 (TrinketM0 pins 3 & 4)
MIDI_CREATE_INSTANCE(HardwareSerial, Serial1, midiB);
void setup()
{
strip.begin();
strip.setBrightness(60);
dot(YELLOW);
pinMode(LED_BUILTIN, OUTPUT);
// Initialize MIDI, and listen to all MIDI channels
// This will also call usb_midi's and Serial1's begin()
midiA.begin(MIDI_CHANNEL_OMNI);
midiB.begin(MIDI_CHANNEL_OMNI);
midiA.turnThruOff();
midiB.turnThruOff();
Serial.begin(115200);
// wait until device mounted
while ( !USBDevice.mounted() ) delay(1);
dot(GREEN);
}
void loop()
{
dot(mycolors[colorindex++]);
if(colorindex == sizeof(mycolors)/sizeof(int)) colorindex=0;
delay(500);
}
void looporig()
{
// read any new MIDI messages
if ( midiA.read() ) {
midiB.send(midiA.getType(),
midiA.getData1(),
midiA.getData2(),
midiA.getChannel());
dot(RED);
dot(BLACK);
// Serial.println("midiA");
}
if ( midiB.read() ) {
midiA.send(midiB.getType(),
midiB.getData1(),
midiB.getData2(),
midiB.getChannel());
dot(BLUE);
dot(BLACK);
// Serial.println("midiB");
}
}
void dot(int color) {
strip.setPixelColor(0, color);
strip.show();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment