mat1jaczyyy / teensy-underlights

LED Strip MIDI for Teensy - controlling LED strips via MIDI
15 stars 4 forks source link

How to add matrix button code to it #2

Open DRCRecoveryData opened 6 months ago

DRCRecoveryData commented 6 months ago

Hi,

I see you modified mf64 can you add matrix button 8*8 code with the leonardo underlights from my previous fork?

mat1jaczyyy commented 6 months ago

my mf64 stuff is based on djtt mf64 stuff this repo is pretty dead

DRCRecoveryData commented 6 months ago

my mf64 stuff is based on djtt mf64 stuff this repo is pretty dead

Can you check this code react like a midi fighter?

#include "MIDIUSB.h"
#include <Adafruit_NeoPixel.h>

#define NEO_PIN 10
#define NUM_PXL 32

// Color palette from the first code
const byte _R[128] = {0, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 45, 93, 142, 223, 190, 28, 61, 93, 190, 125, 12, 28, 45, 158, 61, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 12, 28, 45, 158, 61, 28, 61, 93, 190, 125, 45, 93, 142, 223, 190, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 36, 73, 109, 146, 182, 219, 255};
const byte _G[128] = {0, 0, 0, 0, 125, 0, 12, 28, 45, 158, 61, 28, 61, 93, 190, 125, 45, 93, 142, 223, 190, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 45, 93, 142, 223, 190, 28, 61, 93, 190, 125, 12, 28, 45, 158, 61, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 36, 73, 109, 146, 182, 219, 255};
const byte _B[128] = {0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 0, 0, 0, 125, 0, 12, 28, 45, 158, 61, 28, 61, 93, 190, 125, 45, 93, 142, 223, 190, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 61, 125, 190, 255, 255, 45, 93, 142, 223, 190, 28, 61, 93, 190, 125, 12, 28, 45, 158, 61, 36, 73, 109, 146, 182, 219, 255};

Adafruit_NeoPixel pixels = Adafruit_NeoPixel(NUM_PXL, NEO_PIN, NEO_GRB + NEO_KHZ800);

// Define AddressMatrix template
template <size_t ROWS, size_t COLS>
class AddressMatrix {
public:
  uint8_t matrix[ROWS][COLS];

  // Constructor
  AddressMatrix(uint8_t data[ROWS][COLS]) {
    for (size_t i = 0; i < ROWS; i++) {
      for (size_t j = 0; j < COLS; j++) {
        matrix[i][j] = data[i][j];
      }
    }
  }
};

// Define NoteButtonMatrix template
template <size_t ROWS, size_t COLS>
class NoteButtonMatrix {
public:
  uint8_t row_pins[ROWS];
  uint8_t col_pins[COLS];
  AddressMatrix<ROWS, COLS> address_matrix;
  uint8_t channel;

  // Constructor
  NoteButtonMatrix(uint8_t row_pins[ROWS], uint8_t col_pins[COLS], AddressMatrix<ROWS, COLS> address_matrix, uint8_t channel)
      : address_matrix(address_matrix) {
    for (size_t i = 0; i < ROWS; i++) {
      this->row_pins[i] = row_pins[i];
    }
    for (size_t j = 0; j < COLS; j++) {
      this->col_pins[j] = col_pins[j];
    }
    this->channel = channel;
  }

  int read() {
    // Implement your button read logic here
    // Return the button states as an integer
    return 0;
  }
};

// The note numbers corresponding to the buttons in the matrix
const uint8_t button_addresses[4][4] = {
  {1, 2, 3, 4},
  {5, 6, 7, 8},
  {9, 10, 11, 12},
  {13, 14, 15, 16},
};

// Define your NoteButtonMatrix object
uint8_t row_pins[4] = {2, 3, 4, 5}; // row pins
uint8_t col_pins[4] = {6, 7, 8, 9};  // column pins
NoteButtonMatrix<4, 4> buttonmatrix(row_pins, col_pins, AddressMatrix<4, 4>(button_addresses), 1);

void noteOn(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOn = {0x09, 0x90 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOn);
}

void noteOff(byte channel, byte pitch, byte velocity) {
  midiEventPacket_t noteOff = {0x08, 0x80 | channel, pitch, velocity};
  MidiUSB.sendMIDI(noteOff);
}

void setPixels(int pinStates){
  for(int i=0; i<16; i++){
    if(bitRead(pinStates, i)){ // Button down
      byte velocity = 127; // Maximum velocity when button is pressed
      noteOn(0, (36+i), velocity); // Send MIDI note on
      pixels.setPixelColor((i*2), pixels.Color(_R[velocity], _G[velocity], _B[velocity])); // Set LED color based on velocity
      pixels.setPixelColor((i*2)+1, pixels.Color(_R[velocity], _G[velocity], _B[velocity]));
    } else { // Button up
      byte velocity = 0; // Velocity 0 when button is released
      noteOff(0, (36+i), velocity); // Send MIDI note off
      pixels.setPixelColor((i*2), pixels.Color(_R[velocity], _G[velocity], _B[velocity])); // Set LED color based on velocity
      pixels.setPixelColor((i*2)+1, pixels.Color(_R[velocity], _G[velocity], _B[velocity]));
    }
  }
  pixels.show();
}

void setup() {
  pixels.begin();
  Serial.begin(115200);
}

void loop(){
  int pinStates = buttonmatrix.read();

  // Check for button state changes
  setPixels(pinStates);
}
mat1jaczyyy commented 6 months ago

how would i know