From de80e2c756508fbd14562ece23b0cbdfbb959e1f Mon Sep 17 00:00:00 2001 From: npoirey Date: Fri, 23 Jun 2017 21:25:46 +0200 Subject: Moved frenchdev to handwired --- keyboards/handwired/frenchdev/matrix.c | 394 +++++++++++++++++++++++++++++++++ 1 file changed, 394 insertions(+) create mode 100644 keyboards/handwired/frenchdev/matrix.c (limited to 'keyboards/handwired/frenchdev/matrix.c') diff --git a/keyboards/handwired/frenchdev/matrix.c b/keyboards/handwired/frenchdev/matrix.c new file mode 100644 index 000000000..df2b8e8b0 --- /dev/null +++ b/keyboards/handwired/frenchdev/matrix.c @@ -0,0 +1,394 @@ +/* + +Note for ErgoDox EZ customizers: Here be dragons! +This is not a file you want to be messing with. +All of the interesting stuff for you is under keymaps/ :) +Love, Erez + +Note to self, the "column" and "row" in here actually refers to the opposite on the keyboard +see definition of KEYMAP in v1.h, the grid is transposed so that a "row" in here is actually a "column" on the physical keyboard +Nicolas + +Copyright 2013 Oleg Kostyuk + +This program is free software: you can redistribute it and/or modify +it under the terms of the GNU General Public License as published by +the Free Software Foundation, either version 2 of the License, or +(at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program. If not, see . +*/ + +/* + * scan matrix + */ +#include +#include +#include +#include "wait.h" +#include "action_layer.h" +#include "print.h" +#include "debug.h" +#include "util.h" +#include "matrix.h" +#include "frenchdev.h" +#include "i2cmaster.h" +#ifdef DEBUG_MATRIX_SCAN_RATE +#include "timer.h" +#endif + +/* + * This constant define not debouncing time in msecs, but amount of matrix + * scan loops which should be made to get stable debounced results. + * + * On Ergodox matrix scan rate is relatively low, because of slow I2C. + * Now it's only 317 scans/second, or about 3.15 msec/scan. + * According to Cherry specs, debouncing time is 5 msec. + * + * And so, there is no sense to have DEBOUNCE higher than 2. + */ + +#ifndef DEBOUNCE +# define DEBOUNCE 5 +#endif +static uint8_t debouncing = DEBOUNCE; + +/* matrix state(1:on, 0:off) */ +static matrix_row_t matrix[MATRIX_ROWS]; +static matrix_row_t matrix_debouncing[MATRIX_ROWS]; + +static matrix_row_t read_cols(uint8_t row); +static void init_cols(void); +static void unselect_rows(void); +static void select_row(uint8_t row); + +static uint8_t mcp23018_reset_loop; + +#ifdef DEBUG_MATRIX_SCAN_RATE +uint32_t matrix_timer; +uint32_t matrix_scan_count; +#endif + + +__attribute__ ((weak)) +void matrix_init_user(void) {} + +__attribute__ ((weak)) +void matrix_scan_user(void) {} + +__attribute__ ((weak)) +void matrix_init_kb(void) { + matrix_init_user(); +} + +__attribute__ ((weak)) +void matrix_scan_kb(void) { + matrix_scan_user(); +} + +inline +uint8_t matrix_rows(void) +{ + return MATRIX_ROWS; +} + +inline +uint8_t matrix_cols(void) +{ + return MATRIX_COLS; +} + +void matrix_init(void) +{ + // initialize row and col + debug_enable = true; + debug_matrix = true; + debug_keyboard = true; + debug_mouse = true; + + mcp23018_status = init_mcp23018(); + + + unselect_rows(); + init_cols(); + + // initialize matrix state: all keys off + for (uint8_t i=0; i < MATRIX_ROWS; i++) { + matrix[i] = 0; + matrix_debouncing[i] = 0; + } + +#ifdef DEBUG_MATRIX_SCAN_RATE + matrix_timer = timer_read32(); + matrix_scan_count = 0; +#endif + + matrix_init_quantum(); + +} + +void matrix_power_up(void) { + mcp23018_status = init_mcp23018(); + + unselect_rows(); + init_cols(); + + // initialize matrix state: all keys off + for (uint8_t i=0; i < MATRIX_ROWS; i++) { + matrix[i] = 0; + matrix_debouncing[i] = 0; + } + +#ifdef DEBUG_MATRIX_SCAN_RATE + matrix_timer = timer_read32(); + matrix_scan_count = 0; +#endif + +} + +uint8_t matrix_scan(void) +{ + if (mcp23018_status) { // if there was an error + if (++mcp23018_reset_loop == 0) { + // since mcp23018_reset_loop is 8 bit - we'll try to reset once in 255 matrix scans + // this will be approx bit more frequent than once per second + print("trying to reset mcp23018\n"); + mcp23018_status = init_mcp23018(); + if (mcp23018_status) { + print("left side not responding\n"); + } else { + print("left side attached\n"); + ergodox_blink_all_leds(); + } + } + } + +#ifdef DEBUG_MATRIX_SCAN_RATE + matrix_scan_count++; + + uint32_t timer_now = timer_read32(); + if (TIMER_DIFF_32(timer_now, matrix_timer)>1000) { + print("matrix scan frequency: "); + pdec(matrix_scan_count); + print("\n"); + + matrix_timer = timer_now; + matrix_scan_count = 0; + } +#endif + + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + select_row(i); + wait_us(30); // without this wait read unstable value. + matrix_row_t cols = read_cols(i); + if (matrix_debouncing[i] != cols) { + matrix_debouncing[i] = cols; + if (debouncing) { + debug("bounce!: "); debug_hex(debouncing); debug("\n"); + } + debouncing = DEBOUNCE; + } + unselect_rows(); + } + + if (debouncing) { + if (--debouncing) { + wait_us(1); + // this should be wait_ms(1) but has been left as-is at EZ's request + } else { + for (uint8_t i = 0; i < MATRIX_ROWS; i++) { + matrix[i] = matrix_debouncing[i]; + } + } + } + + matrix_scan_quantum(); + + return 1; +} + +bool matrix_is_modified(void) +{ + if (debouncing) return false; + return true; +} + +inline +bool matrix_is_on(uint8_t row, uint8_t col) +{ + return (matrix[row] & ((matrix_row_t)1< Date: Fri, 23 Jun 2017 21:35:33 +0200 Subject: Refactoring frenchdev --- keyboards/handwired/frenchdev/frenchdev.c | 27 +++++----- keyboards/handwired/frenchdev/frenchdev.h | 63 +++++++++++----------- .../handwired/frenchdev/keymaps/default/keymap.c | 16 +++--- keyboards/handwired/frenchdev/matrix.c | 12 +++-- keyboards/handwired/frenchdev/rules.mk | 2 +- 5 files changed, 61 insertions(+), 59 deletions(-) (limited to 'keyboards/handwired/frenchdev/matrix.c') diff --git a/keyboards/handwired/frenchdev/frenchdev.c b/keyboards/handwired/frenchdev/frenchdev.c index 101fe92e0..6d5883a3a 100644 --- a/keyboards/handwired/frenchdev/frenchdev.c +++ b/keyboards/handwired/frenchdev/frenchdev.c @@ -18,29 +18,30 @@ void matrix_init_kb(void) { PORTD |= (1<<5 | 1<<4); PORTE |= (1<<6); - ergodox_blink_all_leds(); + frenchdev_blink_all_leds(); + frenchdev_blink_all_leds(); + frenchdev_blink_all_leds(); + frenchdev_blink_all_leds(); matrix_init_user(); } -void ergodox_blink_all_leds(void) +void frenchdev_blink_all_leds(void) { - ergodox_led_all_off(); - ergodox_led_all_set(LED_BRIGHTNESS_HI); - ergodox_right_led_1_on(); + frenchdev_led_all_off(); + frenchdev_led_all_set(LED_BRIGHTNESS_HI); + frenchdev_led_1_on(); _delay_ms(50); - ergodox_right_led_2_on(); + frenchdev_led_2_on(); _delay_ms(50); - ergodox_right_led_3_on(); + frenchdev_led_3_on(); _delay_ms(50); - ergodox_right_led_1_off(); + frenchdev_led_1_off(); _delay_ms(50); - ergodox_right_led_2_off(); + frenchdev_led_2_off(); _delay_ms(50); - ergodox_right_led_3_off(); - //ergodox_led_all_on(); - //_delay_ms(333); - ergodox_led_all_off(); + frenchdev_led_3_off(); + frenchdev_led_all_off(); } uint8_t init_mcp23018(void) { diff --git a/keyboards/handwired/frenchdev/frenchdev.h b/keyboards/handwired/frenchdev/frenchdev.h index bf4df18ef..82121e044 100644 --- a/keyboards/handwired/frenchdev/frenchdev.h +++ b/keyboards/handwired/frenchdev/frenchdev.h @@ -10,7 +10,7 @@ #define CPU_PRESCALE(n) (CLKPR = 0x80, CLKPR = (n)) #define CPU_16MHz 0x00 -// I2C aliases and register addresses (see "mcp23018.md") +// I2C aliases and register addresses (see "mcp23018.md" on tmk repository) #define I2C_ADDR 0b0100000 #define I2C_ADDR_WRITE ( (I2C_ADDR<<1) | I2C_WRITE ) #define I2C_ADDR_READ ( (I2C_ADDR<<1) | I2C_READ ) @@ -25,57 +25,56 @@ extern uint8_t mcp23018_status; -void init_ergodox(void); -void ergodox_blink_all_leds(void); +void init_frenchdev(void); +void frenchdev_blink_all_leds(void); uint8_t init_mcp23018(void); -uint8_t ergodox_left_leds_update(void); #define LED_BRIGHTNESS_LO 15 #define LED_BRIGHTNESS_HI 255 -inline void ergodox_board_led_on(void) { DDRD |= (1<<6); PORTD |= (1<<6); } -inline void ergodox_right_led_1_on(void) { DDRB |= (1<<5); PORTB |= (1<<5); } -inline void ergodox_right_led_2_on(void) { DDRB |= (1<<6); PORTB |= (1<<6); } -inline void ergodox_right_led_3_on(void) { DDRB |= (1<<7); PORTB |= (1<<7); } -inline void ergodox_right_led_on(uint8_t led) { DDRB |= (1<<(led+4)); PORTB |= (1<<(led+4)); } +inline void frenchdev_board_led_on(void) { DDRD |= (1<<6); PORTD |= (1<<6); } +inline void frenchdev_led_1_on(void) { DDRB |= (1<<5); PORTB |= (1<<5); } +inline void frenchdev_led_2_on(void) { DDRB |= (1<<6); PORTB |= (1<<6); } +inline void frenchdev_led_3_on(void) { DDRB |= (1<<7); PORTB |= (1<<7); } +inline void frenchdev_led_on(uint8_t led) { DDRB |= (1<<(led+4)); PORTB |= (1<<(led+4)); } -inline void ergodox_board_led_off(void) { DDRD &= ~(1<<6); PORTD &= ~(1<<6); } -inline void ergodox_right_led_1_off(void) { DDRB &= ~(1<<5); PORTB &= ~(1<<5); } -inline void ergodox_right_led_2_off(void) { DDRB &= ~(1<<6); PORTB &= ~(1<<6); } -inline void ergodox_right_led_3_off(void) { DDRB &= ~(1<<7); PORTB &= ~(1<<7); } -inline void ergodox_right_led_off(uint8_t led) { DDRB &= ~(1<<(led+4)); PORTB &= ~(1<<(led+4)); } +inline void frenchdev_board_led_off(void) { DDRD &= ~(1<<6); PORTD &= ~(1<<6); } +inline void frenchdev_led_1_off(void) { DDRB &= ~(1<<5); PORTB &= ~(1<<5); } +inline void frenchdev_led_2_off(void) { DDRB &= ~(1<<6); PORTB &= ~(1<<6); } +inline void frenchdev_led_3_off(void) { DDRB &= ~(1<<7); PORTB &= ~(1<<7); } +inline void frenchdev_led_off(uint8_t led) { DDRB &= ~(1<<(led+4)); PORTB &= ~(1<<(led+4)); } -inline void ergodox_led_all_on(void) +inline void frenchdev_led_all_on(void) { - ergodox_board_led_on(); - ergodox_right_led_1_on(); - ergodox_right_led_2_on(); - ergodox_right_led_3_on(); + frenchdev_board_led_on(); + frenchdev_led_1_on(); + frenchdev_led_2_on(); + frenchdev_led_3_on(); } -inline void ergodox_led_all_off(void) +inline void frenchdev_led_all_off(void) { - ergodox_board_led_off(); - ergodox_right_led_1_off(); - ergodox_right_led_2_off(); - ergodox_right_led_3_off(); + frenchdev_board_led_off(); + frenchdev_led_1_off(); + frenchdev_led_2_off(); + frenchdev_led_3_off(); } -inline void ergodox_right_led_1_set(uint8_t n) { OCR1A = n; } -inline void ergodox_right_led_2_set(uint8_t n) { OCR1B = n; } -inline void ergodox_right_led_3_set(uint8_t n) { OCR1C = n; } -inline void ergodox_right_led_set(uint8_t led, uint8_t n) { +inline void frenchdev_led_1_set(uint8_t n) { OCR1A = n; } +inline void frenchdev_led_2_set(uint8_t n) { OCR1B = n; } +inline void frenchdev_led_3_set(uint8_t n) { OCR1C = n; } +inline void frenchdev_led_set(uint8_t led, uint8_t n) { (led == 1) ? (OCR1A = n) : (led == 2) ? (OCR1B = n) : (OCR1C = n); } -inline void ergodox_led_all_set(uint8_t n) +inline void frenchdev_led_all_set(uint8_t n) { - ergodox_right_led_1_set(n); - ergodox_right_led_2_set(n); - ergodox_right_led_3_set(n); + frenchdev_led_1_set(n); + frenchdev_led_2_set(n); + frenchdev_led_3_set(n); } #define KEYMAP( \ diff --git a/keyboards/handwired/frenchdev/keymaps/default/keymap.c b/keyboards/handwired/frenchdev/keymaps/default/keymap.c index 74b20cb0f..e6d72d013 100644 --- a/keyboards/handwired/frenchdev/keymaps/default/keymap.c +++ b/keyboards/handwired/frenchdev/keymaps/default/keymap.c @@ -374,18 +374,18 @@ uint8_t old_layer=_BASE; void matrix_scan_user(void) { uint8_t layer = biton32(layer_state); - ergodox_right_led_1_off(); - ergodox_right_led_2_off(); + frenchdev_led_1_off(); + frenchdev_led_2_off(); switch (layer) { case _BASE: - ergodox_right_led_2_on(); + frenchdev_led_2_on(); break; case _SYMBOLS: - ergodox_right_led_1_on(); + frenchdev_led_1_on(); break; case _MEDIA: - ergodox_right_led_1_on(); - ergodox_right_led_2_on(); + frenchdev_led_1_on(); + frenchdev_led_2_on(); default: // none break; @@ -399,9 +399,9 @@ bool process_record_user(uint16_t keycode, keyrecord_t *record) { void led_set_user(uint8_t usb_led) { if (usb_led & (1< +Copyright 2013 Nicolas Poirey This program is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by @@ -164,7 +166,7 @@ uint8_t matrix_scan(void) print("left side not responding\n"); } else { print("left side attached\n"); - ergodox_blink_all_leds(); + frenchdev_blink_all_leds(); } } } diff --git a/keyboards/handwired/frenchdev/rules.mk b/keyboards/handwired/frenchdev/rules.mk index 40a3e4676..16203a5f5 100644 --- a/keyboards/handwired/frenchdev/rules.mk +++ b/keyboards/handwired/frenchdev/rules.mk @@ -75,7 +75,7 @@ MOUSEKEY_ENABLE ?= yes # Mouse keys(+4700) EXTRAKEY_ENABLE ?= yes # Audio control and System control(+450) CONSOLE_ENABLE ?= yes # Console for debug(+400) COMMAND_ENABLE ?= yes # Commands for debug and configuration -CUSTOM_MATRIX ?= yes # Custom matrix file (taken and adapted from the ErgoDox EZ to acomodate custom number of columns) +CUSTOM_MATRIX ?= yes # Custom matrix file (taken and adapted from the ErgoDox EZ to handle custom number of columns) SLEEP_LED_ENABLE ?= no # Breathing sleep LED during USB suspend NKRO_ENABLE ?= yes # USB Nkey Rollover - if this doesn't work, see here: https://github.com/tmk/tmk_keyboard/wiki/FAQ#nkro-doesnt-work UNICODE_ENABLE ?= yes # Unicode -- cgit v1.2.3-70-g09d2