aboutsummaryrefslogtreecommitdiff
path: root/keyboard/phantom
diff options
context:
space:
mode:
authortmk <nobody@nowhere>2012-11-23 12:10:36 +0900
committertmk <nobody@nowhere>2012-11-23 12:29:55 +0900
commit613fdb24fcdb4f8920f0d344cad5800d343f535c (patch)
tree870de14bf144d656d83211276d3aed8272786011 /keyboard/phantom
parent38effe4ae4a691dcf034c31f67d1bdb2156ba7f5 (diff)
downloadqmk_firmware-613fdb24fcdb4f8920f0d344cad5800d343f535c.tar.gz
qmk_firmware-613fdb24fcdb4f8920f0d344cad5800d343f535c.zip
Add initial files for Phantom from Tranquilite@GH.
Diffstat (limited to 'keyboard/phantom')
-rw-r--r--keyboard/phantom/Makefile.lufa120
-rw-r--r--keyboard/phantom/Makefile.pjrc90
-rw-r--r--keyboard/phantom/config.h56
-rw-r--r--keyboard/phantom/keymap.c172
-rw-r--r--keyboard/phantom/led.c34
-rw-r--r--keyboard/phantom/matrix.c249
6 files changed, 721 insertions, 0 deletions
diff --git a/keyboard/phantom/Makefile.lufa b/keyboard/phantom/Makefile.lufa
new file mode 100644
index 000000000..37d243394
--- /dev/null
+++ b/keyboard/phantom/Makefile.lufa
@@ -0,0 +1,120 @@
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).
42TARGET = hid_phantom_lufa
43
44# Directory common source filess exist
45TOP_DIR = ../..
46
47# Directory keyboard dependent files exist
48TARGET_DIR = .
49
50
51# List C source files here. (C dependencies are automatically generated.)
52SRC += keymap.c \
53 matrix.c \
54 led.c
55
56CONFIG_H = config.h
57
58
59# MCU name
60#MCU = at90usb1287
61MCU = atmega32u4
62
63# Processor frequency.
64# This will define a symbol, F_CPU, in all source code files equal to the
65# processor frequency in Hz. You can then use this symbol in your source code to
66# calculate timings. Do NOT tack on a 'UL' at the end, this will be done
67# automatically to create a 32-bit value in your source code.
68#
69# This will be an integer division of F_USB below, as it is sourced by
70# F_USB after it has run through any CPU prescalers. Note that this value
71# does not *change* the processor frequency - it should merely be updated to
72# reflect the processor speed set externally so that the code can use accurate
73# software delays.
74F_CPU = 16000000
75
76
77#
78# LUFA specific
79#
80# Target architecture (see library "Board Types" documentation).
81ARCH = AVR8
82
83# Input clock frequency.
84# This will define a symbol, F_USB, in all source code files equal to the
85# input clock frequency (before any prescaling is performed) in Hz. This value may
86# differ from F_CPU if prescaling is used on the latter, and is required as the
87# raw input clock is fed directly to the PLL sections of the AVR for high speed
88# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL'
89# at the end, this will be done automatically to create a 32-bit value in your
90# source code.
91#
92# If no clock division is performed on the input clock inside the AVR (via the
93# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU.
94F_USB = $(F_CPU)
95
96
97# Build Options
98# comment out to disable the options.
99#
100#MOUSEKEY_ENABLE = yes # Mouse keys
101EXTRAKEY_ENABLE = yes # Audio control and System control
102CONSOLE_ENABLE = yes # Console for debug
103#NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA
104#PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support
105
106
107# Boot Section Size in bytes
108# Teensy halfKay 512
109# Atmel DFU loader 4096
110# LUFA bootloader 4096
111OPT_DEFS += -DBOOT_SIZE=512
112
113
114# Search Path
115VPATH += $(TARGET_DIR)
116VPATH += $(TOP_DIR)
117
118include $(TOP_DIR)/protocol/lufa.mk
119include $(TOP_DIR)/common.mk
120include $(TOP_DIR)/rules.mk
diff --git a/keyboard/phantom/Makefile.pjrc b/keyboard/phantom/Makefile.pjrc
new file mode 100644
index 000000000..12696f344
--- /dev/null
+++ b/keyboard/phantom/Makefile.pjrc
@@ -0,0 +1,90 @@
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).
42TARGET = hid_phantom_pjrc
43
44# Directory common source filess exist
45TOP_DIR = ../..
46
47# Directory keyboard dependent files exist
48TARGET_DIR = .
49
50# keyboard dependent files
51SRC = keymap.c \
52 matrix.c \
53 led.c
54
55CONFIG_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 = at90usb162 # Teensy 1.0
61MCU = atmega32u4 # Teensy 2.0
62#MCU = at90usb646 # Teensy++ 1.0
63#MCU = at90usb1286 # Teensy++ 2.0
64
65
66# Processor frequency.
67# Normally the first thing your program should do is set the clock prescaler,
68# so your program will run at the correct speed. You should also set this
69# variable to same clock speed. The _delay_ms() macro uses this, and many
70# examples use this variable to calculate timings. Do not add a "UL" here.
71F_CPU = 16000000
72
73
74# Build Options
75# comment out to disable the options.
76#
77#MOUSEKEY_ENABLE = yes # Mouse keys
78#PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support
79EXTRAKEY_ENABLE = yes # Audio control and System control
80#NKRO_ENABLE = yes # USB Nkey Rollover
81CONSOLE_ENABLE = yes # Console for debug
82
83
84# Search Path
85VPATH += $(TARGET_DIR)
86VPATH += $(TOP_DIR)
87
88include $(TOP_DIR)/protocol/pjrc.mk
89include $(TOP_DIR)/common.mk
90include $(TOP_DIR)/rules.mk
diff --git a/keyboard/phantom/config.h b/keyboard/phantom/config.h
new file mode 100644
index 000000000..8aa02ea60
--- /dev/null
+++ b/keyboard/phantom/config.h
@@ -0,0 +1,56 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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 0xB919
25#define DEVICE_VER 0x0001
26#define MANUFACTURER t.m.k.
27#define PRODUCT Phantom
28
29
30/* message strings */
31#define DESCRIPTION t.m.k. keyboard firmware for Phantom
32
33
34/* matrix size */
35#define MATRIX_ROWS 6
36#define MATRIX_COLS 17
37
38/* define if matrix has ghost */
39//#define MATRIX_HAS_GHOST
40
41/* Set 0 if need no debouncing */
42#define DEBOUNCE 7
43
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// TODO: configurable
51#define DEBUG_LED 0
52#define DEBUG_LED_CONFIG
53#define DEBUG_LED_ON
54#define DEBUG_LED_OFF
55
56#endif
diff --git a/keyboard/phantom/keymap.c b/keyboard/phantom/keymap.c
new file mode 100644
index 000000000..0a95f9fbb
--- /dev/null
+++ b/keyboard/phantom/keymap.c
@@ -0,0 +1,172 @@
1/*
2Copyright 2011 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along with this program. If not, see <http://www.gnu.org/licenses/>.
16*/
17
18/*
19 * Keymap for Phantom controller
20 */
21#include <stdint.h>
22#include <stdbool.h>
23#include <avr/pgmspace.h>
24#include "keycode.h"
25#include "print.h"
26#include "debug.h"
27#include "util.h"
28#include "keymap.h"
29
30
31// Convert physical keyboard layout to matrix array.
32// This is a macro to define keymap easily in keyboard layout form.
33#define KEYMAP( \
34 K5A, K5C, K5D, K5E, K5F, K5G, K5H, K5I, K5J, K5K, K5L, K5M, K5N, K5O, K5P, K5Q, \
35 K5B, K4A, K4B, K4C, K4D, K4E, K4F, K4G, K4H, K4I, K4J, K4K, K4L, K4N, K4O, K4P, K4Q, \
36 K3A, K3B, K3C, K3D, K3E, K3F, K3G, K3H, K3I, K3J, K3K, K3L, K3M, K3N, K3O, K3P, K3Q, \
37 K2A, K2B, K2C, K2D, K2E, K2F, K2G, K2H, K2I, K2J, K2K, K2L, K2N, K2O, K2P, K2Q, \
38 K1A, K1C, K1D, K1E, K1F, K1G, K1H, K1I, K1J, K1K, K1L, K1N, K1O, K1P, K1Q, \
39 K0A, K0B, K0C, K0H, K0K, K0L, K0M, K0N, K0O, K0P, K0Q \
40) { \
41/* A B C D E F G H I J K L M N O P Q */ \
42/* 0 */ { KC_##K0A, KC_##K0B, KC_##K0C, KC_NO , KC_NO , KC_NO , KC_NO , KC_##K0H, KC_NO , KC_NO , KC_##K0K, KC_##K0L, KC_##K0M, KC_##K0N, KC_##K0O, KC_##K0P, KC_##K0Q}, \
43/* 1 */ { KC_##K1A, KC_NO , KC_##K1C, KC_##K1D, KC_##K1E, KC_##K1F, KC_##K1G, KC_##K1H, KC_##K1I, KC_##K1J, KC_##K1K, KC_##K1L, KC_NO , KC_##K1N, KC_##K1O, KC_##K1P, KC_##K1Q}, \
44/* 2 */ { KC_##K2A, KC_##K2B, KC_##K2C, KC_##K2D, KC_##K2E, KC_##K2F, KC_##K2G, KC_##K2H, KC_##K2I, KC_##K2J, KC_##K2K, KC_##K2L, KC_NO , KC_##K2N, KC_##K2O, KC_##K2P, KC_##K2Q}, \
45/* 3 */ { KC_##K3A, KC_##K3B, KC_##K3C, KC_##K3D, KC_##K3E, KC_##K3F, KC_##K3G, KC_##K3H, KC_##K3I, KC_##K3J, KC_##K3K, KC_##K3L, KC_##K3M, KC_##K3N, KC_##K3O, KC_##K3P, KC_##K3Q}, \
46/* 4 */ { KC_##K4A, KC_##K4B, KC_##K4C, KC_##K4D, KC_##K4E, KC_##K4F, KC_##K4G, KC_##K4H, KC_##K4I, KC_##K4J, KC_##K4K, KC_##K4L, KC_NO , KC_##K4N, KC_##K4O, KC_##K4P, KC_##K4Q}, \
47/* 5 */ { KC_##K5A, KC_##K5B, KC_##K5C, KC_##K5D, KC_##K5E, KC_##K5F, KC_##K5G, KC_##K5H, KC_##K5I, KC_##K5J, KC_##K5K, KC_##K5L, KC_##K5M, KC_##K5N, KC_##K5O, KC_##K5P, KC_##K5Q}, \
48}
49
50#define KEYCODE(layer, row, col) (pgm_read_byte(&keymaps[(layer)][(row)][(col)]))
51
52
53// Assign Fn key(0-7) to a layer to which switch with the Fn key pressed.
54static const uint8_t PROGMEM fn_layer[] = {
55 0, // Fn0
56 1, // Fn1
57 2, // Fn2
58 3, // Fn3
59 4, // Fn4
60 5, // Fn5
61 6, // Fn6
62 7 // Fn7
63};
64
65// Assign Fn key(0-7) to a keycode sent when release Fn key without use of the layer.
66// See layer.c for details.
67static const uint8_t PROGMEM fn_keycode[] = {
68 KC_NO, // Fn0
69 KC_NO, // Fn1
70 KC_NO, // Fn2
71 KC_NO, // Fn3
72 KC_NO, // Fn4
73 KC_NO, // Fn5
74 KC_NO, // Fn6
75 KC_NO // Fn7
76};
77
78/*
79 * Phantom keyboard layout with winkeys and 7bit style editing block. I am
80 * Not in the mood to implement full 7-bit keymap.
81 *
82 *
83 * ,---. ,---------------. ,---------------. ,---------------. ,-----------.
84 * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau|
85 * `---' `---------------' `---------------' `---------------' `-----------'
86 * ,-----------------------------------------------------------. ,-----------.
87 * |~ | 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backsp | |Ins|Hom|PgU|
88 * |-----------------------------------------------------------| |-----------|
89 * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \| |Del|End|PgD|
90 * |-----------------------------------------------------------| |---|---|---|
91 * |Caps | A| S| D| F| G| H| J| K| L| ;| '|Return | |???|???|???|
92 * |-----------------------------------------------------------| |---|---|---|
93 * |Shift | Z| X| C| V| B| N| M| ,| .| /|Shift | |???|Up |???|
94 * |-----------------------------------------------------------| |-----------|
95 * |Ctl|Gui|Alt| Space |Alt|Gui|App|Ctl| |Lef|Dow|Rig|
96 * `-----------------------------------------------------------' `-----------'
97 */
98
99
100static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
101/* Layer 0: Default Layer
102 *
103 * ANSI:
104 *
105 * ,---. ,---------------. ,---------------. ,---------------. ,-----------.
106 * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Pau|
107 * `---' `---------------' `---------------' `---------------' `-----------'
108 * ,-----------------------------------------------------------. ,-----------.
109 * |~ | 1| 2| 3| 4| 5| 6| 7| 8| 9| 0| -| =|Backsp | |Ins|Hom|PgU|
110 * |-----------------------------------------------------------| |-----------|
111 * |Tab | Q| W| E| R| T| Y| U| I| O| P| [| ]| \| |Del|End|PgD|
112 * |-----------------------------------------------------------| |---|---|---|
113 * |Caps | A| S| D| F| G| H| J| K| L| ;| '|Return | |???|???|???|
114 * |-----------------------------------------------------------| |---|---|---|
115 * |Shift | Z| X| C| V| B| N| M| ,| .| /|Shift | |???|Up |???|
116 * |-----------------------------------------------------------| |-----------|
117 * |Ctl|Gui|Alt| Space |Alt|Gui|App|Ctl| |Lef|Dow|Rig|
118 * `-----------------------------------------------------------' `-----------'
119 */
120
121 KEYMAP(\
122 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR, SLCK, BRK, \
123 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS, EQL, BSPC, INS, HOME, PGUP, \
124 TAB, Q, W, E, R, T, Y, U, I, O, P, LBRC, RBRC, BSLS, DEL, END, PGDN, \
125 FN1, A, S, D, F, G, H, J, K, L, SCLN, QUOT, ENT, 0, 0, 0, \
126 LSFT, Z, X, C, V, B, N, M, COMM, DOT, SLSH, RSFT, 0, UP, 0, \
127 LCTL, LGUI, LALT, SPC, RALT, RGUI, APP, RCTL, LEFT, DOWN, RGHT),
128
129
130/*
131 * ,---. ,---------------. ,---------------. ,---------------. ,-----------.
132 * |Esc| |F1 |F2 |F3 |F4 | |F5 |F6 |F7 |F8 | |F9 |F10|F11|F12| |PrS|ScL|Slp|
133 * `---' `---------------' `---------------' `---------------' `-----------'
134 * ,-----------------------------------------------------------. ,-----------.
135 * |~ | 1| 2| 3| 4| 5| 6| 7| 8| 9|Mut|V- |V+ |Backsp | |Ins|Hom|PgU|
136 * |-----------------------------------------------------------| |-----------|
137 * |Tab | Q| W| E| R| T| Y| U| I|MSt|Ply|Prv|Nxt|Media| |Del|End|PgD|
138 * |-----------------------------------------------------------| |-----------|
139 * |FN1 | A| S| D| F| G| H| J| K| L| ;| '|Return | |???|???|???|
140 * |-----------------------------------------------------------| |-----------|
141 * |Shft | Z| X|Clc| V| B| N| M| ,| .| /|Caps | |???|Up |???|
142 * |-----------------------------------------------------------| |-----------|
143 * |Ctl|Gui|Alt| Space |Alt|Gui|App|Ctl| |Lef|Dow|Rig|
144 * `-----------------------------------------------------------' `-----------'
145 */
146
147 KEYMAP(\
148 ESC, F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, PSCR, SLCK, SLEP, \
149 GRV, 1, 2, 3, 4, 5, 6, 7, 8, 9,MUTE, VOLD, VOLU, BSPC, INS, HOME, PGUP, \
150 TAB, Q, W, E, R, T, Y, U, I,MSTP,MPLY, MPRV, MNXT, MSEL, DEL, END, PGDN, \
151 FN1, A, S, D, F, G, H, J, K, L, SCLN, QUOT, ENT, 0, 0, 0, \
152 LSFT, Z, X,CALC, V, B, N, M, COMM, DOT, SLSH, CAPS, 0, UP, 0, \
153 LCTL, LGUI, LALT, SPC, RALT, RGUI, APP, RCTL, LEFT, DOWN, RGHT),
154
155
156};
157
158
159uint8_t keymap_get_keycode(uint8_t layer, uint8_t row, uint8_t col)
160{
161 return KEYCODE(layer, row, col);
162}
163
164uint8_t keymap_fn_layer(uint8_t index)
165{
166 return pgm_read_byte(&fn_layer[index]);
167}
168
169uint8_t keymap_fn_keycode(uint8_t index)
170{
171 return pgm_read_byte(&fn_keycode[index]);
172}
diff --git a/keyboard/phantom/led.c b/keyboard/phantom/led.c
new file mode 100644
index 000000000..3523cb756
--- /dev/null
+++ b/keyboard/phantom/led.c
@@ -0,0 +1,34 @@
1/*
2Copyright 2012 Jun Wako <wakojun@gmail.com>
3
4This program is free software: you can redistribute it and/or modify
5it under the terms of the GNU General Public License as published by
6the Free Software Foundation, either version 2 of the License, or
7(at your option) any later version.
8
9This program is distributed in the hope that it will be useful,
10but WITHOUT ANY WARRANTY; without even the implied warranty of
11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12GNU General Public License for more details.
13
14You should have received a copy of the GNU General Public License
15along 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
23void led_set(uint8_t usb_led)
24{
25 if (!(usb_led & (1<<USB_LED_CAPS_LOCK)))
26 PORTB &= ~(1<<6);
27 else
28 PORTB |= (1<<6);
29
30 if (!(usb_led & (1<<USB_LED_SCROLL_LOCK)))
31 PORTB &= ~(1<<7);
32 else
33 PORTB |= (1<<7);
34}
diff --git a/keyboard/phantom/matrix.c b/keyboard/phantom/matrix.c
new file mode 100644
index 000000000..07f3f4289
--- /dev/null
+++ b/keyboard/phantom/matrix.c
@@ -0,0 +1,249 @@
1/* Copyright 2012 Jun Wako <wakojun@gmail.com>
2 *
3 * This is heavily based on phantom/board.{c|h}.
4 * https://github.com/BathroomEpiphanies/AVR-Keyboard
5 *
6 * Copyright (c) 2012 Fredrik Atmer, Bathroom Epiphanies Inc
7 * http://bathroomepiphanies.com
8 *
9 * As for liscensing consult with the original files or its author.
10 */
11#include <stdint.h>
12#include <stdbool.h>
13#include <avr/io.h>
14#include <util/delay.h>
15#include "print.h"
16#include "debug.h"
17#include "util.h"
18#include "matrix.h"
19
20
21#ifndef DEBOUNCE
22# define DEBOUNCE 0
23#endif
24static uint8_t debouncing = DEBOUNCE;
25
26// bit array of key state(1:on, 0:off)
27static matrix_row_t *matrix;
28static matrix_row_t *matrix_debounced;
29static matrix_row_t _matrix0[MATRIX_ROWS];
30static matrix_row_t _matrix1[MATRIX_ROWS];
31
32
33#define _DDRA (uint8_t *const)&DDRA
34#define _DDRB (uint8_t *const)&DDRB
35#define _DDRC (uint8_t *const)&DDRC
36#define _DDRD (uint8_t *const)&DDRD
37#define _DDRE (uint8_t *const)&DDRE
38#define _DDRF (uint8_t *const)&DDRF
39
40#define _PINA (uint8_t *const)&PINA
41#define _PINB (uint8_t *const)&PINB
42#define _PINC (uint8_t *const)&PINC
43#define _PIND (uint8_t *const)&PIND
44#define _PINE (uint8_t *const)&PINE
45#define _PINF (uint8_t *const)&PINF
46
47#define _PORTA (uint8_t *const)&PORTA
48#define _PORTB (uint8_t *const)&PORTB
49#define _PORTC (uint8_t *const)&PORTC
50#define _PORTD (uint8_t *const)&PORTD
51#define _PORTE (uint8_t *const)&PORTE
52#define _PORTF (uint8_t *const)&PORTF
53
54#define _BIT0 0x01
55#define _BIT1 0x02
56#define _BIT2 0x04
57#define _BIT3 0x08
58#define _BIT4 0x10
59#define _BIT5 0x20
60#define _BIT6 0x40
61#define _BIT7 0x80
62
63/* Specifies the ports and pin numbers for the rows */
64static
65uint8_t *const row_ddr[MATRIX_ROWS] = {_DDRB, _DDRB, _DDRB, _DDRB, _DDRB, _DDRB};
66
67static
68uint8_t *const row_port[MATRIX_ROWS] = {_PORTB, _PORTB, _PORTB, _PORTB, _PORTB, _PORTB};
69
70static
71uint8_t *const row_pin[MATRIX_ROWS] = {_PINB, _PINB, _PINB, _PINB, _PINB, _PINB};
72
73static
74const uint8_t row_bit[MATRIX_ROWS] = { _BIT0, _BIT1, _BIT2, _BIT3, _BIT4, _BIT5};
75
76/* Specifies the ports and pin numbers for the columns */
77static
78uint8_t *const col_ddr[MATRIX_COLS] = { _DDRD, _DDRC, _DDRC, _DDRD, _DDRD, _DDRE,
79 _DDRF, _DDRF, _DDRF, _DDRF, _DDRF, _DDRF,
80 _DDRD, _DDRD, _DDRD, _DDRD, _DDRD};
81
82static
83uint8_t *const col_port[MATRIX_COLS] = {_PORTD, _PORTC, _PORTC, _PORTD, _PORTD, _PORTE,
84 _PORTF, _PORTF, _PORTF, _PORTF, _PORTF, _PORTF,
85 _PORTD, _PORTD, _PORTD, _PORTD, _PORTD};
86
87static
88const uint8_t col_bit[MATRIX_COLS] = { _BIT5, _BIT7, _BIT6, _BIT4, _BIT0, _BIT6,
89 _BIT0, _BIT1, _BIT4, _BIT5, _BIT6, _BIT7,
90 _BIT7, _BIT6, _BIT1, _BIT2, _BIT3};
91
92static
93inline void pull_column(int col) {
94 *col_port[col] &= ~col_bit[col];
95}
96
97static
98inline void release_column(int col) {
99 *col_port[col] |= col_bit[col];
100}
101
102/* PORTB is set as input with pull-up resistors
103 PORTC,D,E,F are set to high output */
104
105static
106void setup_io_pins(void) {
107 uint8_t row, col;
108 for(row = 0; row < MATRIX_ROWS; row++) {
109 *row_ddr[row] &= ~row_bit[row];
110 *row_port[row] |= row_bit[row];
111 }
112 for(col = 0; col < MATRIX_COLS; col++) {
113 *col_ddr[col] |= col_bit[col];
114 *col_port[col] |= col_bit[col];
115 }
116}
117
118/* LEDs are on output compare pins OC1B OC1C
119 This activates fast PWM mode on them.
120 Prescaler 256 and 8-bit counter results in
121 16000000/256/256 = 244 Hz blink frequency.
122 LED_A: Caps Lock
123 LED_B: Scroll Lock */
124/* Output on PWM pins are turned off when the timer
125 reaches the value in the output compare register,
126 and are turned on when it reaches TOP (=256). */
127static
128void setup_leds(void) {
129 TCCR1A |= // Timer control register 1A
130 (1<<WGM10) | // Fast PWM 8-bit
131 (1<<COM1B1)| // Clear OC1B on match, set at TOP
132 (1<<COM1C1); // Clear OC1C on match, set at TOP
133 TCCR1B |= // Timer control register 1B
134 (1<<WGM12) | // Fast PWM 8-bit
135 (1<<CS12); // Prescaler 256
136 OCR1B = 250; // Output compare register 1B
137 OCR1C = 250; // Output compare register 1C
138 // LEDs: LED_A -> PORTB6, LED_B -> PORTB7
139 DDRB &= 0x3F;
140 PORTB &= 0x3F;
141}
142
143
144inline
145uint8_t matrix_rows(void)
146{
147 return MATRIX_ROWS;
148}
149
150inline
151uint8_t matrix_cols(void)
152{
153 return MATRIX_COLS;
154}
155
156void matrix_init(void)
157{
158 // To use PORTF disable JTAG with writing JTD bit twice within four cycles.
159 MCUCR |= (1<<JTD);
160 MCUCR |= (1<<JTD);
161
162 // initialize row and col
163 setup_io_pins();
164 setup_leds();
165
166 // initialize matrix state: all keys off
167 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix0[i] = 0x00;
168 for (uint8_t i=0; i < MATRIX_ROWS; i++) _matrix1[i] = 0x00;
169 matrix = _matrix0;
170 matrix_debounced = _matrix1;
171}
172
173uint8_t matrix_scan(void)
174{
175 if (!debouncing) {
176 matrix_row_t *tmp = matrix_debounced;
177 matrix_debounced = matrix;
178 matrix = tmp;
179 }
180
181 for (uint8_t col = 0; col < MATRIX_COLS; col++) { // 0-16
182 pull_column(col); // output hi on theline
183 _delay_us(3); // without this wait it won't read stable value.
184 for (uint8_t row = 0; row < MATRIX_ROWS; row++) { // 0-5
185 bool prev_bit = matrix[row] & ((matrix_row_t)1<<col);
186 bool curr_bit = !(*row_pin[row] & row_bit[row]);
187 if (prev_bit != curr_bit) {
188 matrix[row] ^= ((matrix_row_t)1<<col);
189 if (debouncing) {
190 debug("bounce!: "); debug_hex(debouncing); print("\n");
191 }
192 debouncing = DEBOUNCE;
193 }
194 }
195 release_column(col);
196 }
197
198 if (debouncing) {
199 debouncing--;
200 }
201
202 return 1;
203}
204
205bool matrix_is_modified(void)
206{
207 // NOTE: no longer used
208 return true;
209}
210
211inline
212bool matrix_has_ghost(void)
213{
214 return false;
215}
216
217inline
218bool matrix_is_on(uint8_t row, uint8_t col)
219{
220 return (matrix_debounced[row] & ((matrix_row_t)1<<col));
221}
222
223inline
224matrix_row_t matrix_get_row(uint8_t row)
225{
226 return matrix_debounced[row];
227}
228
229void matrix_print(void)
230{
231 print("\nr/c 01234567\n");
232 for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
233 phex(row); print(": ");
234 pbin_reverse(matrix_get_row(row));
235 print("\n");
236 }
237}
238
239uint8_t matrix_key_count(void)
240{
241 uint8_t count = 0;
242 for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
243 for (uint8_t j = 0; j < MATRIX_COLS; j++) {
244 if (matrix_is_on(i, j))
245 count++;
246 }
247 }
248 return count;
249}