diff options
| author | tmk <nobody@nowhere> | 2013-08-05 16:22:08 +0900 |
|---|---|---|
| committer | tmk <nobody@nowhere> | 2013-08-05 16:23:17 +0900 |
| commit | b6677f10e33bfa49557dfebd0226928477ede56a (patch) | |
| tree | 1716eca59ab7edc0b4084559db18e2822d67117a /keyboard/onekey | |
| parent | 60103a12b2ca0a572da1e72eb46bff19c499fc95 (diff) | |
| download | qmk_firmware-b6677f10e33bfa49557dfebd0226928477ede56a.tar.gz qmk_firmware-b6677f10e33bfa49557dfebd0226928477ede56a.zip | |
Add files for onekey(issue #56)
- https://github.com/tmk/tmk_keyboard/issues/56
Diffstat (limited to 'keyboard/onekey')
| -rw-r--r-- | keyboard/onekey/Makefile.lufa | 141 | ||||
| -rw-r--r-- | keyboard/onekey/Makefile.pjrc | 110 | ||||
| -rw-r--r-- | keyboard/onekey/README.md | 5 | ||||
| -rw-r--r-- | keyboard/onekey/config.h | 70 | ||||
| -rw-r--r-- | keyboard/onekey/keymap.c | 66 | ||||
| -rw-r--r-- | keyboard/onekey/led.c | 25 | ||||
| -rw-r--r-- | keyboard/onekey/matrix.c | 175 |
7 files changed, 592 insertions, 0 deletions
diff --git a/keyboard/onekey/Makefile.lufa b/keyboard/onekey/Makefile.lufa new file mode 100644 index 000000000..03cfd83a5 --- /dev/null +++ b/keyboard/onekey/Makefile.lufa | |||
| @@ -0,0 +1,141 @@ | |||
| 1 | #---------------------------------------------------------------------------- | ||
| 2 | # On command line: | ||
| 3 | # | ||
| 4 | # make all = Make software. | ||
| 5 | # | ||
| 6 | # make clean = Clean out built project files. | ||
| 7 | # | ||
| 8 | # make coff = Convert ELF to AVR COFF. | ||
| 9 | # | ||
| 10 | # make extcoff = Convert ELF to AVR Extended COFF. | ||
| 11 | # | ||
| 12 | # make program = Download the hex file to the device. | ||
| 13 | # Please customize your programmer settings(PROGRAM_CMD) | ||
| 14 | # | ||
| 15 | # make teensy = Download the hex file to the device, using teensy_loader_cli. | ||
| 16 | # (must have teensy_loader_cli installed). | ||
| 17 | # | ||
| 18 | # make dfu = Download the hex file to the device, using dfu-programmer (must | ||
| 19 | # have dfu-programmer installed). | ||
| 20 | # | ||
| 21 | # make flip = Download the hex file to the device, using Atmel FLIP (must | ||
| 22 | # have Atmel FLIP installed). | ||
| 23 | # | ||
| 24 | # make dfu-ee = Download the eeprom file to the device, using dfu-programmer | ||
| 25 | # (must have dfu-programmer installed). | ||
| 26 | # | ||
| 27 | # make flip-ee = Download the eeprom file to the device, using Atmel FLIP | ||
| 28 | # (must have Atmel FLIP installed). | ||
| 29 | # | ||
| 30 | # make debug = Start either simulavr or avarice as specified for debugging, | ||
| 31 | # with avr-gdb or avr-insight as the front end for debugging. | ||
| 32 | # | ||
| 33 | # make filename.s = Just compile filename.c into the assembler code only. | ||
| 34 | # | ||
| 35 | # make filename.i = Create a preprocessed source file for use in submitting | ||
| 36 | # bug reports to the GCC project. | ||
| 37 | # | ||
| 38 | # To rebuild project do "make clean" then "make all". | ||
| 39 | #---------------------------------------------------------------------------- | ||
| 40 | |||
| 41 | # Target file name (without extension). | ||
| 42 | TARGET = onekey_lufa | ||
| 43 | |||
| 44 | # Directory common source filess exist | ||
| 45 | TOP_DIR = ../.. | ||
| 46 | |||
| 47 | # Directory keyboard dependent files exist | ||
| 48 | TARGET_DIR = . | ||
| 49 | |||
| 50 | # project specific files | ||
| 51 | SRC = keymap.c \ | ||
| 52 | matrix.c \ | ||
| 53 | led.c | ||
| 54 | |||
| 55 | CONFIG_H = config.h | ||
| 56 | |||
| 57 | |||
| 58 | # MCU name | ||
| 59 | #MCU = at90usb1287 | ||
| 60 | MCU = atmega32u4 | ||
| 61 | |||
| 62 | # Processor frequency. | ||
| 63 | # This will define a symbol, F_CPU, in all source code files equal to the | ||
| 64 | # processor frequency in Hz. You can then use this symbol in your source code to | ||
| 65 | # calculate timings. Do NOT tack on a 'UL' at the end, this will be done | ||
| 66 | # automatically to create a 32-bit value in your source code. | ||
| 67 | # | ||
| 68 | # This will be an integer division of F_USB below, as it is sourced by | ||
| 69 | # F_USB after it has run through any CPU prescalers. Note that this value | ||
| 70 | # does not *change* the processor frequency - it should merely be updated to | ||
| 71 | # reflect the processor speed set externally so that the code can use accurate | ||
| 72 | # software delays. | ||
| 73 | F_CPU = 16000000 | ||
| 74 | |||
| 75 | |||
| 76 | # | ||
| 77 | # LUFA specific | ||
| 78 | # | ||
| 79 | # Target architecture (see library "Board Types" documentation). | ||
| 80 | ARCH = AVR8 | ||
| 81 | |||
| 82 | # Input clock frequency. | ||
| 83 | # This will define a symbol, F_USB, in all source code files equal to the | ||
| 84 | # input clock frequency (before any prescaling is performed) in Hz. This value may | ||
| 85 | # differ from F_CPU if prescaling is used on the latter, and is required as the | ||
| 86 | # raw input clock is fed directly to the PLL sections of the AVR for high speed | ||
| 87 | # clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL' | ||
| 88 | # at the end, this will be done automatically to create a 32-bit value in your | ||
| 89 | # source code. | ||
| 90 | # | ||
| 91 | # If no clock division is performed on the input clock inside the AVR (via the | ||
| 92 | # CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU. | ||
| 93 | F_USB = $(F_CPU) | ||
| 94 | |||
| 95 | # Interrupt driven control endpoint task(+60) | ||
| 96 | #OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT | ||
| 97 | |||
| 98 | |||
| 99 | # Boot Section Size in *bytes* | ||
| 100 | # Teensy halfKay 512 | ||
| 101 | # Teensy++ halfKay 1024 | ||
| 102 | # Atmel DFU loader 4096 | ||
| 103 | # LUFA bootloader 4096 | ||
| 104 | # USBaspLoader 2048 | ||
| 105 | OPT_DEFS += -DBOOTLOADER_SIZE=4096 | ||
| 106 | |||
| 107 | |||
| 108 | # Build Options | ||
| 109 | # comment out to disable the options. | ||
| 110 | # | ||
| 111 | BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) | ||
| 112 | MOUSEKEY_ENABLE = yes # Mouse keys(+4700) | ||
| 113 | EXTRAKEY_ENABLE = yes # Audio control and System control(+450) | ||
| 114 | CONSOLE_ENABLE = yes # Console for debug(+400) | ||
| 115 | COMMAND_ENABLE = yes # Commands for debug and configuration | ||
| 116 | SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend | ||
| 117 | #NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA | ||
| 118 | |||
| 119 | |||
| 120 | # Optimize size but this may cause error "relocation truncated to fit" | ||
| 121 | #EXTRALDFLAGS = -Wl,--relax | ||
| 122 | |||
| 123 | # Search Path | ||
| 124 | VPATH += $(TARGET_DIR) | ||
| 125 | VPATH += $(TOP_DIR) | ||
| 126 | |||
| 127 | include $(TOP_DIR)/protocol/lufa.mk | ||
| 128 | include $(TOP_DIR)/common.mk | ||
| 129 | include $(TOP_DIR)/rules.mk | ||
| 130 | |||
| 131 | plain: OPT_DEFS += -DKEYMAP_PLAIN | ||
| 132 | plain: all | ||
| 133 | |||
| 134 | poker: OPT_DEFS += -DKEYMAP_POKER | ||
| 135 | poker: all | ||
| 136 | |||
| 137 | poker_set: OPT_DEFS += -DKEYMAP_POKER_SET | ||
| 138 | poker_set: all | ||
| 139 | |||
| 140 | poker_bit: OPT_DEFS += -DKEYMAP_POKER_BIT | ||
| 141 | poker_bit: all | ||
diff --git a/keyboard/onekey/Makefile.pjrc b/keyboard/onekey/Makefile.pjrc new file mode 100644 index 000000000..194a2b975 --- /dev/null +++ b/keyboard/onekey/Makefile.pjrc | |||
| @@ -0,0 +1,110 @@ | |||
| 1 | #---------------------------------------------------------------------------- | ||
| 2 | # On command line: | ||
| 3 | # | ||
| 4 | # make all = Make software. | ||
| 5 | # | ||
| 6 | # make clean = Clean out built project files. | ||
| 7 | # | ||
| 8 | # make coff = Convert ELF to AVR COFF. | ||
| 9 | # | ||
| 10 | # make extcoff = Convert ELF to AVR Extended COFF. | ||
| 11 | # | ||
| 12 | # make program = Download the hex file to the device. | ||
| 13 | # Please customize your programmer settings(PROGRAM_CMD) | ||
| 14 | # | ||
| 15 | # make teensy = Download the hex file to the device, using teensy_loader_cli. | ||
| 16 | # (must have teensy_loader_cli installed). | ||
| 17 | # | ||
| 18 | # make dfu = Download the hex file to the device, using dfu-programmer (must | ||
| 19 | # have dfu-programmer installed). | ||
| 20 | # | ||
| 21 | # make flip = Download the hex file to the device, using Atmel FLIP (must | ||
| 22 | # have Atmel FLIP installed). | ||
| 23 | # | ||
| 24 | # make dfu-ee = Download the eeprom file to the device, using dfu-programmer | ||
| 25 | # (must have dfu-programmer installed). | ||
| 26 | # | ||
| 27 | # make flip-ee = Download the eeprom file to the device, using Atmel FLIP | ||
| 28 | # (must have Atmel FLIP installed). | ||
| 29 | # | ||
| 30 | # make debug = Start either simulavr or avarice as specified for debugging, | ||
| 31 | # with avr-gdb or avr-insight as the front end for debugging. | ||
| 32 | # | ||
| 33 | # make filename.s = Just compile filename.c into the assembler code only. | ||
| 34 | # | ||
| 35 | # make filename.i = Create a preprocessed source file for use in submitting | ||
| 36 | # bug reports to the GCC project. | ||
| 37 | # | ||
| 38 | # To rebuild project do "make clean" then "make all". | ||
| 39 | #---------------------------------------------------------------------------- | ||
| 40 | |||
| 41 | # Target file name (without extension). | ||
| 42 | TARGET = onekey_pjrc | ||
| 43 | |||
| 44 | # Directory common source filess exist | ||
| 45 | TOP_DIR = ../.. | ||
| 46 | |||
| 47 | # Directory keyboard dependent files exist | ||
| 48 | TARGET_DIR = . | ||
| 49 | |||
| 50 | # project specific files | ||
| 51 | SRC = keymap.c \ | ||
| 52 | matrix.c \ | ||
| 53 | led.c | ||
| 54 | |||
| 55 | CONFIG_H = config.h | ||
| 56 | |||
| 57 | |||
| 58 | # MCU name, you MUST set this to match the board you are using | ||
| 59 | # type "make clean" after changing this, so all files will be rebuilt | ||
| 60 | MCU = atmega32u4 | ||
| 61 | #MCU = at90usb1286 | ||
| 62 | |||
| 63 | |||
| 64 | # Processor frequency. | ||
| 65 | # Normally the first thing your program should do is set the clock prescaler, | ||
| 66 | # so your program will run at the correct speed. You should also set this | ||
| 67 | # variable to same clock speed. The _delay_ms() macro uses this, and many | ||
| 68 | # examples use this variable to calculate timings. Do not add a "UL" here. | ||
| 69 | F_CPU = 16000000 | ||
| 70 | |||
| 71 | |||
| 72 | # Boot Section Size in *bytes* | ||
| 73 | # Teensy halfKay 512 | ||
| 74 | # Atmel DFU loader 4096 | ||
| 75 | # LUFA bootloader 4096 | ||
| 76 | OPT_DEFS += -DBOOTLOADER_SIZE=4096 | ||
| 77 | |||
| 78 | |||
| 79 | # Build Options | ||
| 80 | # comment out to disable the options. | ||
| 81 | # | ||
| 82 | BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) | ||
| 83 | MOUSEKEY_ENABLE = yes # Mouse keys(+5000) | ||
| 84 | EXTRAKEY_ENABLE = yes # Audio control and System control(+600) | ||
| 85 | CONSOLE_ENABLE = yes # Console for debug | ||
| 86 | COMMAND_ENABLE = yes # Commands for debug and configuration | ||
| 87 | SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend | ||
| 88 | NKRO_ENABLE = yes # USB Nkey Rollover(+500) | ||
| 89 | #PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support | ||
| 90 | |||
| 91 | |||
| 92 | # Search Path | ||
| 93 | VPATH += $(TARGET_DIR) | ||
| 94 | VPATH += $(TOP_DIR) | ||
| 95 | |||
| 96 | include $(TOP_DIR)/protocol/pjrc.mk | ||
| 97 | include $(TOP_DIR)/common.mk | ||
| 98 | include $(TOP_DIR)/rules.mk | ||
| 99 | |||
| 100 | plain: OPT_DEFS += -DKEYMAP_PLAIN | ||
| 101 | plain: all | ||
| 102 | |||
| 103 | poker: OPT_DEFS += -DKEYMAP_POKER | ||
| 104 | poker: all | ||
| 105 | |||
| 106 | poker_set: OPT_DEFS += -DKEYMAP_POKER_SET | ||
| 107 | poker_set: all | ||
| 108 | |||
| 109 | poker_bit: OPT_DEFS += -DKEYMAP_POKER_BIT | ||
| 110 | poker_bit: all | ||
diff --git a/keyboard/onekey/README.md b/keyboard/onekey/README.md new file mode 100644 index 000000000..6ccc99929 --- /dev/null +++ b/keyboard/onekey/README.md | |||
| @@ -0,0 +1,5 @@ | |||
| 1 | Onekey | ||
| 2 | ====== | ||
| 3 | Just one key keyboard for example. It sends 'a' key if pins PD0 and PD1 are short-circuited. | ||
| 4 | |||
| 5 | https://github.com/tmk/tmk_keyboard/issues/56 | ||
diff --git a/keyboard/onekey/config.h b/keyboard/onekey/config.h new file mode 100644 index 000000000..c5bc64de9 --- /dev/null +++ b/keyboard/onekey/config.h | |||
| @@ -0,0 +1,70 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #ifndef CONFIG_H | ||
| 19 | #define CONFIG_H | ||
| 20 | |||
| 21 | |||
| 22 | /* USB Device descriptor parameter */ | ||
| 23 | #define VENDOR_ID 0xFEED | ||
| 24 | #define PRODUCT_ID 0x1111 | ||
| 25 | #define DEVICE_VER 0x0001 | ||
| 26 | #define MANUFACTURER geekhack | ||
| 27 | #define PRODUCT Onekey | ||
| 28 | #define DESCRIPTION t.m.k. keyboard firmware for Onekey | ||
| 29 | |||
| 30 | /* key matrix size */ | ||
| 31 | #define MATRIX_ROWS 1 | ||
| 32 | #define MATRIX_COLS 1 | ||
| 33 | |||
| 34 | /* define if matrix has ghost */ | ||
| 35 | //#define MATRIX_HAS_GHOST | ||
| 36 | |||
| 37 | /* Set 0 if debouncing isn't needed */ | ||
| 38 | #define DEBOUNCE 5 | ||
| 39 | |||
| 40 | /* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ | ||
| 41 | #define LOCKING_SUPPORT_ENABLE | ||
| 42 | /* Locking resynchronize hack */ | ||
| 43 | #define LOCKING_RESYNC_ENABLE | ||
| 44 | |||
| 45 | /* key combination for command */ | ||
| 46 | #define IS_COMMAND() ( \ | ||
| 47 | keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) \ | ||
| 48 | ) | ||
| 49 | |||
| 50 | |||
| 51 | |||
| 52 | /* | ||
| 53 | * Feature disable options | ||
| 54 | * These options are also useful to firmware size reduction. | ||
| 55 | */ | ||
| 56 | |||
| 57 | /* disable debug print */ | ||
| 58 | //#define NO_DEBUG | ||
| 59 | |||
| 60 | /* disable print */ | ||
| 61 | //#define NO_PRINT | ||
| 62 | |||
| 63 | /* disable action features */ | ||
| 64 | //#define NO_ACTION_LAYER | ||
| 65 | //#define NO_ACTION_TAPPING | ||
| 66 | //#define NO_ACTION_ONESHOT | ||
| 67 | //#define NO_ACTION_MACRO | ||
| 68 | //#define NO_ACTION_FUNCTION | ||
| 69 | |||
| 70 | #endif | ||
diff --git a/keyboard/onekey/keymap.c b/keyboard/onekey/keymap.c new file mode 100644 index 000000000..90f195c2b --- /dev/null +++ b/keyboard/onekey/keymap.c | |||
| @@ -0,0 +1,66 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012,2013 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | #include <stdint.h> | ||
| 18 | #include <stdbool.h> | ||
| 19 | #include <avr/pgmspace.h> | ||
| 20 | #include "keycode.h" | ||
| 21 | #include "action.h" | ||
| 22 | #include "action_macro.h" | ||
| 23 | #include "report.h" | ||
| 24 | #include "host.h" | ||
| 25 | #include "print.h" | ||
| 26 | #include "debug.h" | ||
| 27 | #include "keymap.h" | ||
| 28 | |||
| 29 | |||
| 30 | static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { | ||
| 31 | {{KC_A}}, | ||
| 32 | }; | ||
| 33 | |||
| 34 | /* | ||
| 35 | * Fn action definition | ||
| 36 | */ | ||
| 37 | static const uint16_t PROGMEM fn_actions[] = { | ||
| 38 | }; | ||
| 39 | |||
| 40 | |||
| 41 | |||
| 42 | #define KEYMAPS_SIZE (sizeof(keymaps) / sizeof(keymaps[0])) | ||
| 43 | #define FN_ACTIONS_SIZE (sizeof(fn_actions) / sizeof(fn_actions[0])) | ||
| 44 | |||
| 45 | /* translates key to keycode */ | ||
| 46 | uint8_t keymap_key_to_keycode(uint8_t layer, key_t key) | ||
| 47 | { | ||
| 48 | if (layer < KEYMAPS_SIZE) { | ||
| 49 | return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]); | ||
| 50 | } else { | ||
| 51 | // fall back to layer 0 | ||
| 52 | return pgm_read_byte(&keymaps[0][(key.row)][(key.col)]); | ||
| 53 | } | ||
| 54 | } | ||
| 55 | |||
| 56 | /* translates Fn keycode to action */ | ||
| 57 | action_t keymap_fn_to_action(uint8_t keycode) | ||
| 58 | { | ||
| 59 | action_t action; | ||
| 60 | if (FN_INDEX(keycode) < FN_ACTIONS_SIZE) { | ||
| 61 | action.code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]); | ||
| 62 | } else { | ||
| 63 | action.code = ACTION_NO; | ||
| 64 | } | ||
| 65 | return action; | ||
| 66 | } | ||
diff --git a/keyboard/onekey/led.c b/keyboard/onekey/led.c new file mode 100644 index 000000000..2d52fbf1c --- /dev/null +++ b/keyboard/onekey/led.c | |||
| @@ -0,0 +1,25 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | #include <avr/io.h> | ||
| 19 | #include "stdint.h" | ||
| 20 | #include "led.h" | ||
| 21 | |||
| 22 | |||
| 23 | void led_set(uint8_t usb_led) | ||
| 24 | { | ||
| 25 | } | ||
diff --git a/keyboard/onekey/matrix.c b/keyboard/onekey/matrix.c new file mode 100644 index 000000000..cd0789c60 --- /dev/null +++ b/keyboard/onekey/matrix.c | |||
| @@ -0,0 +1,175 @@ | |||
| 1 | /* | ||
| 2 | Copyright 2012 Jun Wako <wakojun@gmail.com> | ||
| 3 | |||
| 4 | This program is free software: you can redistribute it and/or modify | ||
| 5 | it under the terms of the GNU General Public License as published by | ||
| 6 | the Free Software Foundation, either version 2 of the License, or | ||
| 7 | (at your option) any later version. | ||
| 8 | |||
| 9 | This program is distributed in the hope that it will be useful, | ||
| 10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
| 11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
| 12 | GNU General Public License for more details. | ||
| 13 | |||
| 14 | You should have received a copy of the GNU General Public License | ||
| 15 | along with this program. If not, see <http://www.gnu.org/licenses/>. | ||
| 16 | */ | ||
| 17 | |||
| 18 | /* | ||
| 19 | * scan matrix | ||
| 20 | */ | ||
| 21 | #include <stdint.h> | ||
| 22 | #include <stdbool.h> | ||
| 23 | #include <avr/io.h> | ||
| 24 | #include <util/delay.h> | ||
| 25 | #include "print.h" | ||
| 26 | #include "debug.h" | ||
| 27 | #include "util.h" | ||
| 28 | #include "matrix.h" | ||
| 29 | |||
| 30 | |||
| 31 | #ifndef DEBOUNCE | ||
| 32 | # define DEBOUNCE 5 | ||
| 33 | #endif | ||
| 34 | static uint8_t debouncing = DEBOUNCE; | ||
| 35 | |||
| 36 | /* matrix state(1:on, 0:off) */ | ||
| 37 | static matrix_row_t matrix[MATRIX_ROWS]; | ||
| 38 | static matrix_row_t matrix_debouncing[MATRIX_ROWS]; | ||
| 39 | |||
| 40 | static matrix_row_t read_cols(void); | ||
| 41 | static void init_cols(void); | ||
| 42 | static void unselect_rows(void); | ||
| 43 | static void select_row(uint8_t row); | ||
| 44 | |||
| 45 | |||
| 46 | inline | ||
| 47 | uint8_t matrix_rows(void) | ||
| 48 | { | ||
| 49 | return MATRIX_ROWS; | ||
| 50 | } | ||
| 51 | |||
| 52 | inline | ||
| 53 | uint8_t matrix_cols(void) | ||
| 54 | { | ||
| 55 | return MATRIX_COLS; | ||
| 56 | } | ||
| 57 | |||
| 58 | void matrix_init(void) | ||
| 59 | { | ||
| 60 | debug_enable = true; | ||
| 61 | debug_matrix = true; | ||
| 62 | // initialize row and col | ||
| 63 | unselect_rows(); | ||
| 64 | init_cols(); | ||
| 65 | |||
| 66 | // initialize matrix state: all keys off | ||
| 67 | for (uint8_t i=0; i < MATRIX_ROWS; i++) { | ||
| 68 | matrix[i] = 0; | ||
| 69 | matrix_debouncing[i] = 0; | ||
| 70 | } | ||
| 71 | } | ||
| 72 | |||
| 73 | uint8_t matrix_scan(void) | ||
| 74 | { | ||
| 75 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 76 | select_row(i); | ||
| 77 | _delay_us(30); // without this wait read unstable value. | ||
| 78 | matrix_row_t cols = read_cols(); | ||
| 79 | if (matrix_debouncing[i] != cols) { | ||
| 80 | matrix_debouncing[i] = cols; | ||
| 81 | if (debouncing) { | ||
| 82 | debug("bounce!: "); debug_hex(debouncing); debug("\n"); | ||
| 83 | } | ||
| 84 | debouncing = DEBOUNCE; | ||
| 85 | } | ||
| 86 | unselect_rows(); | ||
| 87 | } | ||
| 88 | |||
| 89 | if (debouncing) { | ||
| 90 | if (--debouncing) { | ||
| 91 | _delay_ms(1); | ||
| 92 | } else { | ||
| 93 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 94 | matrix[i] = matrix_debouncing[i]; | ||
| 95 | } | ||
| 96 | } | ||
| 97 | } | ||
| 98 | |||
| 99 | return 1; | ||
| 100 | } | ||
| 101 | |||
| 102 | bool matrix_is_modified(void) | ||
| 103 | { | ||
| 104 | if (debouncing) return false; | ||
| 105 | return true; | ||
| 106 | } | ||
| 107 | |||
| 108 | inline | ||
| 109 | bool matrix_is_on(uint8_t row, uint8_t col) | ||
| 110 | { | ||
| 111 | return (matrix[row] & ((matrix_row_t)1<<col)); | ||
| 112 | } | ||
| 113 | |||
| 114 | inline | ||
| 115 | matrix_row_t matrix_get_row(uint8_t row) | ||
| 116 | { | ||
| 117 | return matrix[row]; | ||
| 118 | } | ||
| 119 | |||
| 120 | void matrix_print(void) | ||
| 121 | { | ||
| 122 | print("\nr/c 0123456789ABCDEF\n"); | ||
| 123 | for (uint8_t row = 0; row < MATRIX_ROWS; row++) { | ||
| 124 | phex(row); print(": "); | ||
| 125 | pbin_reverse16(matrix_get_row(row)); | ||
| 126 | print("\n"); | ||
| 127 | } | ||
| 128 | } | ||
| 129 | |||
| 130 | uint8_t matrix_key_count(void) | ||
| 131 | { | ||
| 132 | uint8_t count = 0; | ||
| 133 | for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | ||
| 134 | count += bitpop16(matrix[i]); | ||
| 135 | } | ||
| 136 | return count; | ||
| 137 | } | ||
| 138 | |||
| 139 | /* Column pin configuration | ||
| 140 | * col: 0 | ||
| 141 | * pin: D0 | ||
| 142 | */ | ||
| 143 | static void init_cols(void) | ||
| 144 | { | ||
| 145 | // Input with pull-up(DDR:0, PORT:1) | ||
| 146 | DDRD &= ~(1<<0); | ||
| 147 | PORTD |= (1<<0); | ||
| 148 | } | ||
| 149 | |||
| 150 | static matrix_row_t read_cols(void) | ||
| 151 | { | ||
| 152 | return (PIND&(1<<0) ? 0 : (1<<0)); | ||
| 153 | } | ||
| 154 | |||
| 155 | /* Row pin configuration | ||
| 156 | * row: 0 | ||
| 157 | * pin: D1 | ||
| 158 | */ | ||
| 159 | static void unselect_rows(void) | ||
| 160 | { | ||
| 161 | // Hi-Z(DDR:0, PORT:0) to unselect | ||
| 162 | DDRD &= ~0b00000010; | ||
| 163 | PORTD &= ~0b00000010; | ||
| 164 | } | ||
| 165 | |||
| 166 | static void select_row(uint8_t row) | ||
| 167 | { | ||
| 168 | // Output low(DDR:1, PORT:0) to select | ||
| 169 | switch (row) { | ||
| 170 | case 0: | ||
| 171 | DDRD |= (1<<1); | ||
| 172 | PORTD &= ~(1<<1); | ||
| 173 | break; | ||
| 174 | } | ||
| 175 | } | ||
