#include "Adafruit_Keypad.h" #include "led_strip_rmt_ws2812.cpp" #include "ESPRotary.h" #include // I2C #include "driver/gpio.h" #include "controls.h" #define LED_STRIP_LED_NUMBERS 30 #define ROWS 5 // rows #define COLS 6 // columns // #define NEOPIXEL_PIN 21 #define NUM_PIXELS (ROWS * COLS) #define ROTARY_PIN1 42 #define ROTARY_PIN2 2 #define CLICKS_PER_STEP 4 // this number depends on your rotary encoder #define BUTTON_PIN 1 // 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 bool lit[ROWS * COLS] = {0}; // define the symbols on the buttons of the keypads char keys[ROWS][COLS] = { {'1', '2', '3', '4', '5', '6'}, {'7', '8', '9', 'A', 'B', 'C'}, {'D', 'E', 'F', 'G', 'H', 'I'}, {'J', 'K', 'L', 'M', 'N', 'O'}, {'P', 'Q', 'R', 'S', 'T', 'U'}}; uint8_t rowPins[ROWS] = {46, 3, 8, 18, 17}; // connect to the row pinouts of the keypad uint8_t colPins[COLS] = {16, 15, 7, 6, 5, 4}; // connect to the column pinouts of the keypad uint8_t controls[512] = {0}; ESPRotary r; Adafruit_Keypad customKeypad = Adafruit_Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS); 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 rotate(ESPRotary &r) { ESP_LOGI(TAG, "%i", r.getPosition()); } // on left or right rotation void showDirection(ESPRotary &r) { ESP_LOGI(TAG, "[%s]", r.directionToString(r.getDirection()).c_str()); } void task_read_all(void *args) { // init r.begin(ROTARY_PIN1, ROTARY_PIN2, CLICKS_PER_STEP); r.setChangedHandler(rotate); r.setLeftRotationHandler(showDirection); r.setRightRotationHandler(showDirection); // Setup matrix static uint8_t const cable_num = 0; // MIDI jack associated with USB endpoint static uint8_t const channel = 0; // 0 for channel 1 ESP_LOGI(TAG, "begin task_keyboard_read"); customKeypad.begin(); // bool lastAvailable = false; // I2C Wire.begin(I2C_SDA, I2C_SCL, 400000L); pinMode(I2C_INTERUPT, INPUT_PULLUP); // i2c available for (;;) { // TODO delay? r.loop(); // b.loop(); // b2.loop(); customKeypad.tick(); // ESP_LOGI(TAG, "%i", customKeypad.available()); while (customKeypad.available()) { keypadEvent e = customKeypad.read(); ESP_LOGI(TAG, "%c", (char)e.bit.KEY); uint8_t row = e.bit.ROW; uint8_t col = e.bit.COL; uint8_t controlNum = row * COLS + col; if (e.bit.EVENT == KEY_JUST_PRESSED) { ESP_LOGI(TAG, " pressed"); ESP_LOGI(TAG, "Row: "); ESP_LOGI(TAG, "%i", row); ESP_LOGI(TAG, " col: "); ESP_LOGI(TAG, "%i", col); ESP_LOGI(TAG, " -> "); uint16_t keynum; if (row % 2 == 0) { // even row keynum = row * COLS + col; } else { // odd row the neopixels go BACKWARDS! keynum = row * COLS + (5 - col); } ESP_LOGI(TAG, "%i", keynum); // lit[keynum] = !lit[keynum]; // invert neopixel status // control[controlNum] = control[controlNum]; setControl(controlNum, 255); // TODO queue write // uint8_t base_mac_addr[6] = {0}; // uint8_t control[3] = {SET_CONTROL | channel, controlNum, 127}; // tud_midi_stream_write(cable_num, control, 3); } else if (e.bit.EVENT == KEY_JUST_RELEASED) { // uint8_t control[3] = {SET_CONTROL | channel, controlNum, 0}; setControl(controlNum, 0); // TODO queue write // tud_midi_stream_write(cable_num, control, 3); ESP_LOGI(TAG, " released"); } } // 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); } } uint8_t j = 0; // color ticker void task_led_strip(void *arg) { led_strip_handle_t led_strip = configure_led(); ESP_LOGI(TAG1, "Start blinking LED strip"); for (;;) { for (int i = 0; i < LED_STRIP_LED_NUMBERS; i++) { if (lit[i]) { uint8_t r = 0; uint8_t g = 0; uint8_t b = 0; byte WheelPos = ((i * 256 / LED_STRIP_LED_NUMBERS) + j) & 255; if (WheelPos < 85) { r = WheelPos * 3; g = 255 - WheelPos * 3; b = 0; } else if (WheelPos < 170) { WheelPos -= 85; r = 255 - WheelPos * 3; g = 0; b = WheelPos * 3; } else { WheelPos -= 170; r = 0; g = WheelPos * 3; b = 255 - WheelPos * 3; } led_strip_set_pixel(led_strip, i, r, g, b); } else { led_strip_set_pixel(led_strip, i, 0, 0, 0); } } ESP_ERROR_CHECK(led_strip_refresh(led_strip)); j++; vTaskDelay(pdMS_TO_TICKS(20)); } } void init_controls(controls_config *cfg) { ESP_LOGI(TAG1, "cfg.matrix_keys.led_io: %i", cfg->matrix_keys.led_io); }