From beaf774a81af8d09ded3ce0c0a2d517d59d95665 Mon Sep 17 00:00:00 2001 From: Jack Humbert Date: Wed, 19 Dec 2018 20:11:54 -0500 Subject: [PATCH] leds working on both sides --- drivers/arm/i2c_master.c | 6 +- keyboards/moonlander/config.h | 8 ++ keyboards/moonlander/matrix.c | 98 +++++++++++++++- keyboards/moonlander/moonlander.c | 187 ++++++++++++++++++++++++++++++ keyboards/moonlander/moonlander.h | 2 + keyboards/moonlander/rules.mk | 2 +- quantum/stm32/halconf.h | 2 +- quantum/stm32/mcuconf.h | 2 +- 8 files changed, 300 insertions(+), 7 deletions(-) diff --git a/drivers/arm/i2c_master.c b/drivers/arm/i2c_master.c index ab962ea959..77d7b8919e 100644 --- a/drivers/arm/i2c_master.c +++ b/drivers/arm/i2c_master.c @@ -52,7 +52,7 @@ void i2c_init(void) palSetPadMode(GPIOB, 7, PAL_MODE_INPUT); chThdSleepMilliseconds(10); - + palSetPadMode(GPIOB, 6, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); palSetPadMode(GPIOB, 7, PAL_MODE_ALTERNATE(4) | PAL_STM32_OTYPE_OPENDRAIN | PAL_STM32_PUPDR_PULLUP); @@ -81,6 +81,10 @@ uint8_t i2c_receive(uint8_t address, uint8_t* data, uint16_t length, uint16_t ti return i2cMasterReceiveTimeout(&I2C_DRIVER, (i2c_address >> 1), data, length, MS2ST(timeout)); } +uint8_t i2c_transmit_receive(uint8_t address, uint8_t * tx_body, uint16_t tx_length, uint8_t * rx_body, uint16_t rx_length) { + return i2cMasterTransmitTimeout(&I2C_DRIVER, address/2, tx_body, tx_length, rx_body, rx_length, MS2ST(100)); +} + uint8_t i2c_writeReg(uint8_t devaddr, uint8_t regaddr, uint8_t* data, uint16_t length, uint16_t timeout) { i2c_address = devaddr; diff --git a/keyboards/moonlander/config.h b/keyboards/moonlander/config.h index cf2f8e8e01..c55553174a 100644 --- a/keyboards/moonlander/config.h +++ b/keyboards/moonlander/config.h @@ -76,3 +76,11 @@ along with this program. If not, see . //#define NO_ACTION_ONESHOT //#define NO_ACTION_MACRO //#define NO_ACTION_FUNCTION + +#define DRIVER_ADDR_1 0b1110100 +#define DRIVER_ADDR_2 0b1110111 + +#define DRIVER_COUNT 2 +#define DRIVER_1_LED_TOTAL 35 +#define DRIVER_2_LED_TOTAL 35 +#define DRIVER_LED_TOTAL DRIVER_1_LED_TOTAL + DRIVER_2_LED_TOTAL diff --git a/keyboards/moonlander/matrix.c b/keyboards/moonlander/matrix.c index 8bf923b60f..e9241274a8 100644 --- a/keyboards/moonlander/matrix.c +++ b/keyboards/moonlander/matrix.c @@ -28,6 +28,8 @@ along with this program. If not, see . #include "action.h" #include "keycode.h" #include +#include "moonlander.h" +#include "i2c_master.h" /* #define MATRIX_ROW_PINS { B10, B11, B12, B13, B14, B15 } outputs @@ -35,9 +37,11 @@ along with this program. If not, see . */ /* matrix state(1:on, 0:off) */ static matrix_row_t matrix[MATRIX_ROWS]; -static matrix_row_t matrix_debouncing[MATRIX_COLS]; +static matrix_row_t matrix_debouncing[MATRIX_ROWS]; static bool debouncing = false; static uint16_t debouncing_time = 0; +static bool debouncing_right = false; +static uint16_t debouncing_time_right = 0; __attribute__ ((weak)) void matrix_init_user(void) {} @@ -55,6 +59,41 @@ void matrix_scan_kb(void) { matrix_scan_user(); } +bool mcp23018_initd = false; +uint8_t mcp23018_tx[3]; +uint8_t mcp23018_rx[1]; + +void mcp23018_init(void) { + + i2c_init(); + i2c_start(MCP23018_DEFAULT_ADDRESS << 1); + +// #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs +// #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs + + mcp23018_tx[0] = 0x00; // IODIRA + mcp23018_tx[1] = 0x00; // ? + mcp23018_tx[2] = 0b00111111; // A is inputs + + if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, + mcp23018_tx, 3, 100 + )) { + printf("error hori\n"); + } else { + mcp23018_tx[0] = 0x0C; // GPPUA + mcp23018_tx[1] = 0x00; // ? + mcp23018_tx[2] = 0b00111111; // pull-up As + + if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, + mcp23018_tx, 3, 100 + )) { + printf("error hori\n"); + } else { + mcp23018_initd = true; + } + } +} + void matrix_init(void) { printf("matrix init\n"); //debug_matrix = true; @@ -79,13 +118,15 @@ void matrix_init(void) { memset(matrix, 0, MATRIX_ROWS * sizeof(matrix_row_t)); memset(matrix_debouncing, 0, MATRIX_ROWS * sizeof(matrix_row_t)); + mcp23018_init(); + matrix_init_quantum(); } uint8_t matrix_scan(void) { // actual matrix - for (int row = 0; row < MATRIX_ROWS; row++) { + for (int row = 0; row < 6; row++) { matrix_row_t data = 0; // strobe row @@ -127,15 +168,66 @@ uint8_t matrix_scan(void) { debouncing = true; debouncing_time = timer_read(); } + + + + // right side + + if (!mcp23018_initd) { + printf("trying to init right\n"); + mcp23018_init(); + } + + // #define MCP23_ROW_PINS { GPB5, GBP4, GBP3, GBP2, GBP1, GBP0 } outputs + // #define MCP23_COL_PINS { GPA0, GBA1, GBA2, GBA3, GBA4, GBA5, GBA6 } inputs + + // select row + + mcp23018_tx[0] = 0x12; // GPIOA + mcp23018_tx[1] = 0xFF & ~(1<<(row)); // activate row + + if (MSG_OK != i2c_transmit(MCP23018_DEFAULT_ADDRESS << 1, + mcp23018_tx, 2, 100 + )) { + printf("error hori\n"); + } + + // read col + + mcp23018_tx[0] = 0x13; // GPIOB + + if (MSG_OK != i2c_transmit_receive(MCP23018_DEFAULT_ADDRESS << 1, + mcp23018_tx, 1, + mcp23018_rx, 1 + )) { + printf("error vert\n"); + } + + data = mcp23018_rx[0]; + + if (matrix_debouncing[row + 6] != data) { + matrix_debouncing[row + 6] = data; + debouncing_right = true; + debouncing_time_right = timer_read(); + } + } + if (debouncing && timer_elapsed(debouncing_time) > DEBOUNCING_DELAY) { - for (int row = 0; row < MATRIX_ROWS; row++) { + for (int row = 0; row < 6; row++) { matrix[row] = matrix_debouncing[row]; } debouncing = false; } + if (debouncing_right && timer_elapsed(debouncing_time_right) > DEBOUNCING_DELAY) { + for (int row = 6; row < 12; row++) { + matrix[row] = matrix_debouncing[row]; + } + debouncing_right = false; + } + matrix_scan_quantum(); return 1; diff --git a/keyboards/moonlander/moonlander.c b/keyboards/moonlander/moonlander.c index a8267eb8b6..a0045419a4 100644 --- a/keyboards/moonlander/moonlander.c +++ b/keyboards/moonlander/moonlander.c @@ -72,3 +72,190 @@ uint32_t layer_state_set_kb(uint32_t state) { return state; } + +const is31_led g_is31_leds[DRIVER_LED_TOTAL] = { +/* Refer to IS31 manual for these locations + * driver + * | R location + * | | G location + * | | | B location + * | | | | */ + {0, C3_2, C1_1, C4_2}, // 1 + {0, C2_2, C1_2, C4_3}, + {0, C2_3, C1_3, C3_3}, + {0, C2_4, C1_4, C3_4}, + {0, C2_5, C1_5, C3_5}, + {0, C2_6, C1_6, C3_6}, + {0, C2_7, C1_7, C3_7}, + {0, C2_8, C1_8, C3_8}, + {0, C3_1, C2_1, C4_1}, + + {0, C7_8, C6_8, C8_8}, // 10 + {0, C7_7, C6_7, C9_8}, + {0, C8_7, C6_6, C9_7}, + {0, C8_6, C7_6, C9_6}, + {0, C8_5, C7_5, C9_5}, + {0, C8_4, C7_4, C9_4}, + {0, C8_3, C7_3, C9_3}, + {0, C8_2, C7_2, C9_2}, + {0, C8_1, C7_1, C9_1}, + + {0, C3_10, C1_9, C4_10}, // 19 + {0, C2_10, C1_10, C4_11}, + {0, C2_11, C1_11, C3_11}, + {0, C2_12, C1_12, C3_12}, + {0, C2_13, C1_13, C3_13}, + {0, C2_14, C1_14, C3_14}, + {0, C2_15, C1_15, C3_15}, + {0, C2_16, C1_16, C3_16}, + {0, C3_9, C2_9, C4_9}, + + {0, C7_16, C6_16, C8_16}, // 28 + {0, C7_15, C6_15, C9_16}, + {0, C8_15, C6_14, C9_15}, + {0, C8_10, C7_10, C9_10}, + {0, C8_9, C7_9, C9_9}, + {0, C8_11, C7_11, C9_11}, + {0, C8_12, C7_12, C9_12}, + {0, C8_13, C7_13, C9_13}, +// {0, C8_14, C7_14, C9_4} + + {1, C3_2, C1_1, C4_2}, // 1 + {1, C2_2, C1_2, C4_3}, + {1, C2_3, C1_3, C3_3}, + {1, C2_4, C1_4, C3_4}, + {1, C2_5, C1_5, C3_5}, + {1, C2_6, C1_6, C3_6}, + {1, C2_7, C1_7, C3_7}, + {1, C2_8, C1_8, C3_8}, + {1, C3_1, C2_1, C4_1}, + + {1, C7_8, C6_8, C8_8}, // 10 + {1, C7_7, C6_7, C9_8}, + {1, C8_7, C6_6, C9_7}, + {1, C8_6, C7_6, C9_6}, + {1, C8_5, C7_5, C9_5}, + {1, C8_4, C7_4, C9_4}, + {1, C8_3, C7_3, C9_3}, + {1, C8_2, C7_2, C9_2}, + {1, C8_1, C7_1, C9_1}, + + {1, C3_10, C1_9, C4_10}, // 19 + {1, C2_10, C1_10, C4_11}, + {1, C2_11, C1_11, C3_11}, + {1, C2_12, C1_12, C3_12}, + {1, C2_13, C1_13, C3_13}, + {1, C2_14, C1_14, C3_14}, + {1, C2_15, C1_15, C3_15}, + {1, C2_16, C1_16, C3_16}, + {1, C3_9, C2_9, C4_9}, + + {1, C7_16, C6_16, C8_16}, // 28 + {1, C7_15, C6_15, C9_16}, + {1, C8_15, C6_14, C9_15}, + {1, C8_10, C7_10, C9_10}, + {1, C8_9, C7_9, C9_9}, + {1, C8_11, C7_11, C9_11}, + {1, C8_12, C7_12, C9_12}, + {1, C8_13, C7_13, C9_13}, + +}; + +const rgb_led g_rgb_leds[DRIVER_LED_TOTAL] = { + + /*{row | col << 4} + | {x=0..224, y=0..64} + | | modifier + | | | */ + {{0|(0<<4)}, {20.36*0, 21.33*0}, 1}, + {{1|(0<<4)}, {20.36*1, 21.33*0}, 0}, + {{2|(0<<4)}, {20.36*2, 21.33*0}, 0}, + {{3|(0<<4)}, {20.36*3, 21.33*0}, 0}, + {{4|(0<<4)}, {20.36*4, 21.33*0}, 0}, + + {{0|(1<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(1<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(1<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(1<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(1<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(2<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(2<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(2<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(2<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(2<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(3<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(3<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(3<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(3<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(3<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(4<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(4<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(4<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(4<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(4<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(5<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(5<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(5<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(5<<4)}, {20.36*8, 21.33*0}, 0}, + + {{0|(6<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(6<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(6<<4)}, {20.36*7, 21.33*0}, 0}, + + // cluster + {{5|(0<<4)}, {20.36*5, 21.33*0}, 0}, + {{5|(1<<4)}, {20.36*6, 21.33*0}, 0}, + {{5|(2<<4)}, {20.36*7, 21.33*0}, 0}, + + + + + {{0|(0<<4)}, {20.36*0, 21.33*0}, 1}, + {{1|(0<<4)}, {20.36*1, 21.33*0}, 0}, + {{2|(0<<4)}, {20.36*2, 21.33*0}, 0}, + {{3|(0<<4)}, {20.36*3, 21.33*0}, 0}, + {{4|(0<<4)}, {20.36*4, 21.33*0}, 0}, + + {{0|(1<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(1<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(1<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(1<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(1<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(2<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(2<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(2<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(2<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(2<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(3<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(3<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(3<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(3<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(3<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(4<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(4<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(4<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(4<<4)}, {20.36*8, 21.33*0}, 0}, + {{4|(4<<4)}, {20.36*9, 21.33*0}, 0}, + + {{0|(5<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(5<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(5<<4)}, {20.36*7, 21.33*0}, 0}, + {{3|(5<<4)}, {20.36*8, 21.33*0}, 0}, + + {{0|(6<<4)}, {20.36*5, 21.33*0}, 0}, + {{1|(6<<4)}, {20.36*6, 21.33*0}, 0}, + {{2|(6<<4)}, {20.36*7, 21.33*0}, 0}, + + // cluster + {{5|(0<<4)}, {20.36*5, 21.33*0}, 0}, + {{5|(1<<4)}, {20.36*6, 21.33*0}, 0}, + {{5|(2<<4)}, {20.36*7, 21.33*0}, 0}, + +}; diff --git a/keyboards/moonlander/moonlander.h b/keyboards/moonlander/moonlander.h index f55e334a6e..1acbf5df0f 100644 --- a/keyboards/moonlander/moonlander.h +++ b/keyboards/moonlander/moonlander.h @@ -17,6 +17,8 @@ along with this program. If not, see . #pragma once +#define MCP23018_DEFAULT_ADDRESS 0b0100000 + #include "quantum.h" #define LAYOUT_moonlander( \ diff --git a/keyboards/moonlander/rules.mk b/keyboards/moonlander/rules.mk index 46072f928d..16514208ea 100644 --- a/keyboards/moonlander/rules.mk +++ b/keyboards/moonlander/rules.mk @@ -19,5 +19,5 @@ COMMAND_ENABLE = yes # Commands for debug and configuration NKRO_ENABLE = yes # USB Nkey Rollover CUSTOM_MATRIX = yes # Custom matrix file AUDIO_ENABLE = no -RGBLIGHT_ENABLE = no +RGB_MATRIX_ENABLE = IS31FL3731 # SERIAL_LINK_ENABLE = yes diff --git a/quantum/stm32/halconf.h b/quantum/stm32/halconf.h index 8fe8e0c6f5..c3e0cbb728 100644 --- a/quantum/stm32/halconf.h +++ b/quantum/stm32/halconf.h @@ -76,7 +76,7 @@ * @brief Enables the I2C subsystem. */ #if !defined(HAL_USE_I2C) || defined(__DOXYGEN__) -#define HAL_USE_I2C FALSE +#define HAL_USE_I2C TRUE #endif /** diff --git a/quantum/stm32/mcuconf.h b/quantum/stm32/mcuconf.h index 7c3c6e570c..36f8ca2252 100644 --- a/quantum/stm32/mcuconf.h +++ b/quantum/stm32/mcuconf.h @@ -154,7 +154,7 @@ /* * I2C driver system settings. */ -#define STM32_I2C_USE_I2C1 FALSE +#define STM32_I2C_USE_I2C1 TRUE #define STM32_I2C_USE_I2C2 FALSE #define STM32_I2C_BUSY_TIMEOUT 50 #define STM32_I2C_I2C1_IRQ_PRIORITY 10