You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
282 lines
8.7 KiB
282 lines
8.7 KiB
4 months ago
|
// #include "Adafruit_Keypad.h"
|
||
|
// #include "led_strip_rmt_ws2812.cpp"
|
||
|
// #include "ESPRotary.h"
|
||
|
#include <Wire.h> // I2C
|
||
|
#include "driver/gpio.h"
|
||
|
#include "controls.h"
|
||
|
#include "tinyusb.h"
|
||
|
#include <ezButton.h>
|
||
|
|
||
|
#define LED_STRIP_LED_NUMBERS 30
|
||
|
#define I2C_INTERRUPT_REQUEST 57 //
|
||
|
|
||
|
#define ROWS 5 // rows
|
||
|
#define COLS 6 // columns
|
||
|
|
||
|
#define CLICKS_PER_STEP 4 // this number depends on your rotary encoder
|
||
|
#define BUTTON_PIN 1
|
||
|
|
||
|
#define BUTTON_DEBOUNCE 20
|
||
|
|
||
|
// buttons
|
||
|
ezButton button[1] = {{7}};
|
||
|
|
||
|
// joystick
|
||
|
#define BUTTON_PIN2 41
|
||
|
#define JOY_X 40
|
||
|
#define JOY_Y 39
|
||
|
// I2C
|
||
|
#define I2C_SDA 37
|
||
|
#define I2C_SCL 36
|
||
|
|
||
|
#define I2C_INTERUPT GPIO_NUM_38
|
||
|
|
||
|
static const char *TAG = "controls";
|
||
|
// #define SERIAL_SPEED 115200
|
||
|
|
||
|
uint8_t controls[512] = {0};
|
||
|
|
||
|
// Basic MIDI Messages
|
||
|
#define NOTE_OFF 0x80
|
||
|
#define NOTE_ON 0x90
|
||
|
#define SET_CONTROL 0xB0
|
||
|
|
||
|
void setControl(uint16_t controlNum, uint8_t value)
|
||
|
{
|
||
|
uint16_t keynum = controlNum;
|
||
|
bool litValue = false;
|
||
|
if (value > 0)
|
||
|
{
|
||
|
litValue = true;
|
||
|
}
|
||
|
if ((controlNum / 6) % 2 == 1)
|
||
|
{
|
||
|
keynum = controlNum + 5 - (controlNum % 6) * 2;
|
||
|
}
|
||
|
ESP_LOGI(TAG, "begin litValue %i ,%i, %i", (controlNum % 6), controlNum, keynum);
|
||
|
// lit[keynum] = litValue; // invert neopixel status
|
||
|
controls[controlNum] = value;
|
||
|
}
|
||
|
|
||
|
|
||
|
void task_read_all(void *args)
|
||
|
{
|
||
|
// init
|
||
|
static uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint
|
||
|
static uint8_t const channel = 0; // 0 for channel 1
|
||
|
bool changeDetected = false;
|
||
|
// BUTTON
|
||
|
for (int i = 0; i < 1; i++)
|
||
|
{
|
||
|
button[i].setDebounceTime(BUTTON_DEBOUNCE);
|
||
|
}
|
||
|
uint8_t buttonLastState[9] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||
|
uint8_t buttonNextState[9] = {0, 0, 0, 0, 0, 0, 0, 0};
|
||
|
uint8_t buttonExternalState[9] = {0};
|
||
|
// FADER
|
||
|
ESP_LOGI(TAG, "begin ");
|
||
|
uint16_t threshold = 250;
|
||
|
uint16_t speed = 100;
|
||
|
uint16_t joy_x_init = 0;
|
||
|
uint16_t joy_y_init = 0;
|
||
|
uint16_t joy_zoom_init = 0;
|
||
|
for (int i=0;i<10;i++)
|
||
|
{
|
||
|
joy_x_init += analogRead(4);
|
||
|
joy_y_init += analogRead(5);
|
||
|
joy_zoom_init += analogRead(6);
|
||
|
}
|
||
|
joy_x_init = joy_x_init/10;
|
||
|
joy_y_init = joy_y_init/10;
|
||
|
joy_zoom_init = joy_zoom_init/10;
|
||
|
ESP_LOGI(TAG, "joy_x_init %i",joy_x_init);
|
||
|
ESP_LOGI(TAG, "joy_y_init %i",joy_y_init);
|
||
|
ESP_LOGI(TAG, "joy_zoom_init %i",joy_zoom_init);
|
||
|
uint16_t joy_x = 0;
|
||
|
uint16_t joy_y = 0;
|
||
|
uint16_t joy_zoom = 0;
|
||
|
int16_t joy_x_dir = 0;
|
||
|
int16_t joy_y_dir = 0;
|
||
|
int16_t joy_zoom_dir = 0;
|
||
|
float joy_float_x = 0;
|
||
|
float joy_float_y = 0;
|
||
|
float joy_float_zoom = 0;
|
||
|
// uint8_t controls[64] = 0;
|
||
|
// uint8_t controls[65] = 0;
|
||
|
// uint8_t controls[66] = 0;
|
||
|
uint8_t faderLastState[3] = {0, 0, 0};
|
||
|
for (;;)
|
||
|
{
|
||
|
// BUTTON
|
||
|
for (int i = 0; i < 1; i++)
|
||
|
{
|
||
|
// uint8_t buttonValue = digitalRead(buttonPins[i]);
|
||
|
// buttonNextState[i] = buttonValue ? 0 : 255;
|
||
|
button[i].loop();
|
||
|
if (button[i].isPressed())
|
||
|
{
|
||
|
buttonNextState[i] = 255;
|
||
|
}
|
||
|
else if (button[i].isReleased())
|
||
|
{
|
||
|
buttonNextState[i] = 0;
|
||
|
}
|
||
|
// Serial.print("r");
|
||
|
// Serial.print(read);
|
||
|
// Serial.print(": ");
|
||
|
// Serial.println(faderNextState[i]);
|
||
|
}
|
||
|
// JOY
|
||
|
if(controls[64] != (uint8_t)round(joy_float_x)){
|
||
|
ESP_LOGI(TAG, "joy_float_x = controls[64] %f %i",joy_float_x,controls[64]);
|
||
|
joy_float_x = controls[64];
|
||
|
}
|
||
|
if(controls[65] != (uint8_t)round(joy_float_y)){
|
||
|
joy_float_y = controls[65];
|
||
|
}
|
||
|
|
||
|
joy_x = analogRead(4);
|
||
|
joy_y = analogRead(5);
|
||
|
|
||
|
joy_x_dir = joy_x_init-joy_x;
|
||
|
joy_y_dir = joy_y_init-joy_y;
|
||
|
if(abs(joy_x_dir)>threshold){
|
||
|
joy_x_dir = joy_x_dir+ (joy_x_dir <0 ? threshold:-threshold);
|
||
|
// ESP_LOGI(TAG, "joy_x_dir %i",joy_x_dir);
|
||
|
joy_float_x = joy_float_x + ((float)joy_x_dir / 900000 * speed);
|
||
|
if(joy_float_x > 255){
|
||
|
joy_float_x = 255;
|
||
|
}
|
||
|
if(joy_float_x < 0){
|
||
|
joy_float_x = 0;
|
||
|
}
|
||
|
}
|
||
|
if(abs(joy_y_dir)>threshold){
|
||
|
joy_y_dir = joy_y_dir+ (joy_y_dir <0 ? threshold:-threshold);
|
||
|
// ESP_LOGI(TAG, "joy_y_dir %i",joy_y_dir);
|
||
|
joy_float_y = joy_float_y + ((float)joy_y_dir / 900000 * speed);
|
||
|
if(joy_float_y > 255){
|
||
|
joy_float_y = 255;
|
||
|
}
|
||
|
if(joy_float_y < 0){
|
||
|
joy_float_y = 0;
|
||
|
}
|
||
|
}
|
||
|
if(controls[64] != (uint8_t)round(joy_float_x)){
|
||
|
controls[64] = (uint8_t)round(joy_float_x);
|
||
|
ESP_LOGI(TAG, "controls[64] %i",controls[64]);
|
||
|
uint8_t control[3] = {SET_CONTROL | channel, 64, (uint8_t)(controls[64]>>1)};
|
||
|
tud_midi_stream_write(cable_num, control, 3);
|
||
|
}
|
||
|
if(controls[65] != (uint8_t)round(joy_float_y)){
|
||
|
controls[65] = (uint8_t)round(joy_float_y);
|
||
|
ESP_LOGI(TAG, "controls[65] %i",controls[65]);
|
||
|
uint8_t control[3] = {SET_CONTROL | channel, 65, (uint8_t)(controls[65]>>1)};
|
||
|
tud_midi_stream_write(cable_num, control, 3);
|
||
|
}
|
||
|
|
||
|
// ZOOM
|
||
|
if(controls[66] != (uint8_t)round(joy_float_zoom)){
|
||
|
joy_float_zoom = controls[66];
|
||
|
}
|
||
|
joy_zoom = analogRead(6);
|
||
|
joy_zoom_dir = joy_zoom_init-joy_zoom;
|
||
|
if(abs(joy_zoom_dir)>threshold){
|
||
|
joy_zoom_dir = joy_zoom_dir+ (joy_zoom_dir <0 ? threshold:-threshold);
|
||
|
joy_float_zoom = joy_float_zoom + ((float)joy_zoom_dir / 900000 * speed);
|
||
|
if(joy_float_zoom > 255){
|
||
|
joy_float_zoom = 255;
|
||
|
}
|
||
|
if(joy_float_zoom < 0){
|
||
|
joy_float_zoom = 0;
|
||
|
}
|
||
|
}
|
||
|
if(controls[66] != (uint8_t)round(joy_float_zoom)){
|
||
|
controls[66] = (uint8_t)round(joy_float_zoom);
|
||
|
ESP_LOGI(TAG, "controls[66] %i",controls[66]);
|
||
|
uint8_t control[3] = {SET_CONTROL | channel, 66, (uint8_t)(controls[66]>>1)};
|
||
|
tud_midi_stream_write(cable_num, control, 3);
|
||
|
}
|
||
|
// WIRE I2C
|
||
|
bool nextChangeDetected = false;
|
||
|
// better only on change?
|
||
|
for (uint8_t i = 0; i < 1; i++)
|
||
|
{
|
||
|
// Buttons
|
||
|
if (buttonNextState[i] != buttonLastState[i])
|
||
|
{
|
||
|
buttonLastState[i] = buttonNextState[i];
|
||
|
Serial.print("B");
|
||
|
Serial.print(i);
|
||
|
Serial.print(": ");
|
||
|
Serial.println(buttonNextState[i]);
|
||
|
nextChangeDetected = true;
|
||
|
uint8_t control[3] = {SET_CONTROL | channel, i, 127};
|
||
|
ESP_LOGI(TAG, "buttonNextState[%i] %i",i,buttonNextState[i]);
|
||
|
tud_midi_stream_write(cable_num, control, 3);
|
||
|
}
|
||
|
if (buttonLastState[i] == 255 || buttonExternalState[i] == 255)
|
||
|
{
|
||
|
// digitalWrite(ledPins[i], HIGH);
|
||
|
|
||
|
}
|
||
|
else
|
||
|
{
|
||
|
// digitalWrite(ledPins[i], LOW);
|
||
|
}
|
||
|
}
|
||
|
if (nextChangeDetected && !changeDetected)
|
||
|
{
|
||
|
changeDetected = nextChangeDetected;
|
||
|
// pinMode(I2C_INTERRUPT_REQUEST, OUTPUT);
|
||
|
// resetIRQCount++;
|
||
|
}
|
||
|
else if (!changeDetected)
|
||
|
{
|
||
|
// pinMode(I2C_INTERRUPT_REQUEST, INPUT);
|
||
|
}
|
||
|
|
||
|
vTaskDelay(1);
|
||
|
}
|
||
|
|
||
|
// bool lastAvailable = false;
|
||
|
// I2C
|
||
|
// Wire.begin(I2C_SDA, I2C_SCL, 400000L);
|
||
|
// pinMode(I2C_INTERUPT, INPUT_PULLUP); // i2c available
|
||
|
// for (;;)
|
||
|
// {
|
||
|
// // i2c
|
||
|
// bool nextAvailable = gpio_get_level(I2C_INTERUPT) == 0;
|
||
|
// if (nextAvailable) // || lastAvailable
|
||
|
// {
|
||
|
|
||
|
// // ESP_LOGI(TAG, "i2cAvailable: %i", i2cAvailable);
|
||
|
// // fader9
|
||
|
// Wire.requestFrom(55, 18);
|
||
|
// while (Wire.available())
|
||
|
// { // peripheral may send less than requested
|
||
|
// int c = Wire.read(); // receive a byte
|
||
|
// ESP_LOGI(TAG, "value: %i", c);
|
||
|
// }
|
||
|
// // button8
|
||
|
// Wire.requestFrom(56, 8);
|
||
|
// while (Wire.available())
|
||
|
// { // peripheral may send less than requested
|
||
|
// int c = Wire.read(); // receive a byte
|
||
|
// ESP_LOGI(TAG, "value: %i", c);
|
||
|
// }
|
||
|
// // lastAvailable = nextAvailable;
|
||
|
// }
|
||
|
// // task delay time?
|
||
|
// vTaskDelay(1);
|
||
|
// }
|
||
|
}
|
||
|
|
||
|
void init_controls(controls_config *cfg)
|
||
|
{
|
||
|
// ESP_LOGI(TAG, "cfg.matrix_keys.led_io: %i", cfg->matrix_keys.led_io);
|
||
|
for (int i = 0; i < 9; i++)
|
||
|
{
|
||
|
button[i].setDebounceTime(BUTTON_DEBOUNCE);
|
||
|
}
|
||
|
}
|