From af19f56ec98d27ab939b024effefcbebbab8f125 Mon Sep 17 00:00:00 2001 From: Ralf Schmitt Date: Sat, 23 Aug 2014 17:16:16 +0200 Subject: Support for Lightpad keypad --- keyboard/lightpad/Makefile.lufa | 117 ++++++++++++++++++++ keyboard/lightpad/README.md | 24 +++++ keyboard/lightpad/backlight.c | 129 +++++++++++++++++++++++ keyboard/lightpad/backlight.h | 39 +++++++ keyboard/lightpad/config.h | 46 ++++++++ keyboard/lightpad/keymap.c | 73 +++++++++++++ keyboard/lightpad/keymap_lightpad.h | 29 +++++ keyboard/lightpad/led.c | 24 +++++ keyboard/lightpad/matrix.c | 205 ++++++++++++++++++++++++++++++++++++ 9 files changed, 686 insertions(+) create mode 100644 keyboard/lightpad/Makefile.lufa create mode 100644 keyboard/lightpad/README.md create mode 100644 keyboard/lightpad/backlight.c create mode 100644 keyboard/lightpad/backlight.h create mode 100644 keyboard/lightpad/config.h create mode 100644 keyboard/lightpad/keymap.c create mode 100644 keyboard/lightpad/keymap_lightpad.h create mode 100644 keyboard/lightpad/led.c create mode 100644 keyboard/lightpad/matrix.c diff --git a/keyboard/lightpad/Makefile.lufa b/keyboard/lightpad/Makefile.lufa new file mode 100644 index 0000000000..7bce7ebffc --- /dev/null +++ b/keyboard/lightpad/Makefile.lufa @@ -0,0 +1,117 @@ +#---------------------------------------------------------------------------- +# On command line: +# +# make all = Make software. +# +# make clean = Clean out built project files. +# +# make coff = Convert ELF to AVR COFF. +# +# make extcoff = Convert ELF to AVR Extended COFF. +# +# make program = Download the hex file to the device. +# Please customize your programmer settings(PROGRAM_CMD) +# +# make teensy = Download the hex file to the device, using teensy_loader_cli. +# (must have teensy_loader_cli installed). +# +# make dfu = Download the hex file to the device, using dfu-programmer (must +# have dfu-programmer installed). +# +# make flip = Download the hex file to the device, using Atmel FLIP (must +# have Atmel FLIP installed). +# +# make dfu-ee = Download the eeprom file to the device, using dfu-programmer +# (must have dfu-programmer installed). +# +# make flip-ee = Download the eeprom file to the device, using Atmel FLIP +# (must have Atmel FLIP installed). +# +# make debug = Start either simulavr or avarice as specified for debugging, +# with avr-gdb or avr-insight as the front end for debugging. +# +# make filename.s = Just compile filename.c into the assembler code only. +# +# make filename.i = Create a preprocessed source file for use in submitting +# bug reports to the GCC project. +# +# To rebuild project do "make clean" then "make all". +#---------------------------------------------------------------------------- + +# Target file name (without extension). +TARGET = lightpad_lufa + +# Directory common source filess exist +TOP_DIR = ../.. + +# Directory keyboard dependent files exist +TARGET_DIR = . + +# List C source files here. (C dependencies are automatically generated.) +SRC = keymap.c \ + matrix.c \ + led.c \ + backlight.c + +CONFIG_H = config.h + +# MCU name +MCU = atmega32u4 + +# Processor frequency. +# This will define a symbol, F_CPU, in all source code files equal to the +# processor frequency in Hz. You can then use this symbol in your source code to +# calculate timings. Do NOT tack on a 'UL' at the end, this will be done +# automatically to create a 32-bit value in your source code. +# +# This will be an integer division of F_USB below, as it is sourced by +# F_USB after it has run through any CPU prescalers. Note that this value +# does not *change* the processor frequency - it should merely be updated to +# reflect the processor speed set externally so that the code can use accurate +# software delays. +F_CPU = 8000000 + +# +# LUFA specific +# +# Target architecture (see library "Board Types" documentation). +ARCH = AVR8 + +# Input clock frequency. +# This will define a symbol, F_USB, in all source code files equal to the +# input clock frequency (before any prescaling is performed) in Hz. This value may +# differ from F_CPU if prescaling is used on the latter, and is required as the +# raw input clock is fed directly to the PLL sections of the AVR for high speed +# clock generation for the USB and other AVR subsections. Do NOT tack on a 'UL' +# at the end, this will be done automatically to create a 32-bit value in your +# source code. +# +# If no clock division is performed on the input clock inside the AVR (via the +# CPU clock adjust registers or the clock division fuses), this will be equal to F_CPU. +F_USB = $(F_CPU) + +# Build Options +# comment out to disable the options. +# +BOOTMAGIC_ENABLE = yes # Virtual DIP switch configuration(+1000) +#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 +#SLEEP_LED_ENABLE = yes # Breathing sleep LED during USB suspend +#NKRO_ENABLE = yes # USB Nkey Rollover - not yet supported in LUFA +BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality + +# Boot Section Size in bytes +# Teensy halfKay 512 +# Atmel DFU loader 4096 +# LUFA bootloader 4096 +OPT_DEFS += -DBOOTLOADER_SIZE=4096 + +# Search Path +VPATH += $(TARGET_DIR) +VPATH += $(TOP_DIR) + +include $(TOP_DIR)/protocol/lufa.mk +include $(TOP_DIR)/common.mk +include $(TOP_DIR)/rules.mk diff --git a/keyboard/lightpad/README.md b/keyboard/lightpad/README.md new file mode 100644 index 0000000000..b21cccc6a2 --- /dev/null +++ b/keyboard/lightpad/README.md @@ -0,0 +1,24 @@ +Lightpad keypad firmware +====================== +Korean custom keypad designed by Duck. + +*Note that this is not the official firmware* + + +Supported models +---------------- +All pcb options are supported. + + +Build +----- +Move to this directory then just run `make` like: + + $ make -f Makefile.lufa + + +Bootloader +--------- +The PCB is hardwired to run the bootloader if the key at the `top left` position is held down when connecting the keyboard. + +It is still possible to use Boot Magic and Command to access the bootloader though. diff --git a/keyboard/lightpad/backlight.c b/keyboard/lightpad/backlight.c new file mode 100644 index 0000000000..693c566fc5 --- /dev/null +++ b/keyboard/lightpad/backlight.c @@ -0,0 +1,129 @@ +/* +Copyright 2014 Ralf Schmitt + +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 . +*/ + +#include +#include "backlight.h" + +/* Backlight pin configuration + * + * FN1 PB0 (low) + * FN2 PB5 (low) + * FN3 PB4 (low) + * FN4 PD7 (low) + * REAR PD6 (high) + * NUMPAD PB2 (high) + * NUMLOCK PB1 (low) + */ +void backlight_init_ports() { + DDRB |= (1<<0) | (1<<1) | (1<<2) | (1<<4) | (1<<5); + DDRD |= (1<<6) | (1<<7); + + backlight_disable_numlock(); +} + +void backlight_set(uint8_t level) { + (level & BACKLIGHT_FN1) ? backlight_enable_fn1() : backlight_disable_fn1(); + (level & BACKLIGHT_FN2) ? backlight_enable_fn2() : backlight_disable_fn2(); + (level & BACKLIGHT_FN3) ? backlight_enable_fn3() : backlight_disable_fn3(); + (level & BACKLIGHT_FN4) ? backlight_enable_fn4() : backlight_disable_fn4(); + (level & BACKLIGHT_NUMPAD) ? backlight_enable_numpad() : backlight_disable_numpad(); + (level & BACKLIGHT_REAR) ? backlight_enable_rear() : backlight_disable_rear(); +} + +void backlight_enable_fn1() { + PORTB &= ~(1<<0); +} + +void backlight_disable_fn1() { + PORTB |= (1<<0); +} + +void backlight_invert_fn1() { + PORTB ^= (1<<0); +} + +void backlight_enable_fn2() { + PORTB &= ~(1<<5); +} + +void backlight_disable_fn2() { + PORTB |= (1<<5); +} + +void backlight_invert_fn2() { + PORTB ^= (1<<5); +} + +void backlight_enable_fn3() { + PORTB &= ~(1<<4); +} + +void backlight_disable_fn3() { + PORTB |= (1<<4); +} + +void backlight_invert_fn3() { + PORTB ^= (1<<4); +} + +void backlight_enable_fn4() { + PORTD &= ~(1<<7); +} + +void backlight_disable_fn4() { + PORTD |= (1<<7); +} + +void backlight_invert_fn4() { + PORTD ^= (1<<7); +} + +void backlight_enable_numpad() { + PORTB |= (1<<2); +} + +void backlight_disable_numpad() { + PORTB &= ~(1<<2); +} + +void backlight_invert_numpad() { + PORTB ^= (1<<2); +} + +void backlight_enable_numlock() { + PORTB &= ~(1<<1); +} + +void backlight_disable_numlock() { + PORTB |= (1<<1); +} + +void backlight_invert_numlock() { + PORTB ^= (1<<1); +} + +void backlight_enable_rear() { + PORTD |= (1<<6); +} + +void backlight_disable_rear() { + PORTD &= ~(1<<6); +} + +void backlight_invert_rear() { + PORTD ^= (1<<6); +} diff --git a/keyboard/lightpad/backlight.h b/keyboard/lightpad/backlight.h new file mode 100644 index 0000000000..3b3cfd9aea --- /dev/null +++ b/keyboard/lightpad/backlight.h @@ -0,0 +1,39 @@ + +enum backlight_level { + BACKLIGHT_FN1 = 0b0000001, + BACKLIGHT_FN2 = 0b0000010, + BACKLIGHT_FN3 = 0b0000100, + BACKLIGHT_FN4 = 0b0001000, + BACKLIGHT_NUMPAD = 0b0010000, + BACKLIGHT_REAR = 0b0100000, +}; + +void backlight_init_ports(void); + +void backlight_invert_fn1(void); +void backlight_enable_fn1(void); +void backlight_disable_fn1(void); + +void backlight_invert_fn2(void); +void backlight_enable_fn2(void); +void backlight_disable_fn2(void); + +void backlight_invert_fn3(void); +void backlight_enable_fn3(void); +void backlight_disable_fn3(void); + +void backlight_invert_fn4(void); +void backlight_enable_fn4(void); +void backlight_disable_fn4(void); + +void backlight_invert_numlock(void); +void backlight_enable_numlock(void); +void backlight_disable_numlock(void); + +void backlight_enable_numpad(void); +void backlight_disable_numpad(void); +void backlight_invert_numpad(void); + +void backlight_enable_rear(void); +void backlight_disable_rear(void); +void backlight_invert_rear(void); diff --git a/keyboard/lightpad/config.h b/keyboard/lightpad/config.h new file mode 100644 index 0000000000..7f5a596c0c --- /dev/null +++ b/keyboard/lightpad/config.h @@ -0,0 +1,46 @@ +/* +Copyright 2014 Ralf Schmitt + +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 . +*/ + +#ifndef CONFIG_H +#define CONFIG_H + +/* USB Device descriptor parameter */ +#define VENDOR_ID 0xFEED +#define PRODUCT_ID 0x6050 +#define DEVICE_VER 0x0104 +#define MANUFACTURER Duck +#define PRODUCT Lightpad + +/* message strings */ +#define DESCRIPTION t.m.k. keyboard firmware for Lightpad + +/* matrix size */ +#define MATRIX_ROWS 6 +#define MATRIX_COLS 4 + +/* number of backlight levels */ +#define BACKLIGHT_LEVELS 1 + +/* Set 0 if need no debouncing */ +#define DEBOUNCE 5 + +/* key combination for command */ +#define IS_COMMAND() ( \ + keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) \ +) + +#endif diff --git a/keyboard/lightpad/keymap.c b/keyboard/lightpad/keymap.c new file mode 100644 index 0000000000..6d078230bb --- /dev/null +++ b/keyboard/lightpad/keymap.c @@ -0,0 +1,73 @@ +/* +Copyright 2014 Ralf Schmitt + +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 . +*/ + +#include +#include +#include +#include "keycode.h" +#include "action.h" +#include "action_macro.h" +#include "report.h" +#include "host.h" +#include "debug.h" +#include "keymap.h" + +/* Map physical keyboard layout to matrix array */ +#define KEYMAP( \ + K5A, K5B, K5C, K5D, \ + K4A, K4B, K4C, K4D, \ + K3A, K3B, K3C, K3D, \ + K2A, K2B, K2C, \ + K1A, K1B, K1C, K1D, \ + K0A, K0B, K0C \ +) { \ +/* 0 1 2 3 */ \ +/* 5 */ { KC_##K5A, KC_##K5B, KC_##K5C, KC_##K5D}, \ +/* 4 */ { KC_##K4A, KC_##K4B, KC_##K4C, KC_##K4D}, \ +/* 3 */ { KC_##K3A, KC_##K3B, KC_##K3C, KC_##K3D}, \ +/* 2 */ { KC_##K2A, KC_##K2B, KC_##K2C, KC_NO}, \ +/* 1 */ { KC_##K1A, KC_##K1B, KC_##K1C, KC_##K1D}, \ +/* 0 */ { KC_##K0A, KC_##K0B, KC_##K0C, KC_NO, } \ +} + +#include "keymap_lightpad.h" + +#define KEYMAPS_SIZE (sizeof(keymaps) / sizeof(keymaps[0])) +#define FN_ACTIONS_SIZE (sizeof(fn_actions) / sizeof(fn_actions[0])) + +/* translates key to keycode */ +uint8_t keymap_key_to_keycode(uint8_t layer, key_t key) +{ + if (layer < KEYMAPS_SIZE) { + return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]); + } else { + // fall back to layer 0 + return pgm_read_byte(&keymaps[0][(key.row)][(key.col)]); + } +} + +/* translates Fn keycode to action */ +action_t keymap_fn_to_action(uint8_t keycode) +{ + action_t action; + if (FN_INDEX(keycode) < FN_ACTIONS_SIZE) { + action.code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]); + } else { + action.code = ACTION_NO; + } + return action; +} diff --git a/keyboard/lightpad/keymap_lightpad.h b/keyboard/lightpad/keymap_lightpad.h new file mode 100644 index 0000000000..9333964e39 --- /dev/null +++ b/keyboard/lightpad/keymap_lightpad.h @@ -0,0 +1,29 @@ +#include "backlight.h" + +static const uint8_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = { + KEYMAP(\ + FN0, F1, DEL, BSPC, \ + NLCK,PSLS,PAST,PMNS, \ + P7, P8, P9, PPLS, \ + P4, P5, P6, \ + P1, P2, P3, PENT, \ + P0, NO, PDOT), \ + KEYMAP(\ + TRNS,PGDN,PGUP,MUTE, \ + MSEL,MPRV,MNXT,VOLD, \ + P7, P8, P9, VOLU, \ + FN4, FN5, FN6, \ + FN1, FN2, FN3, MPLY, \ + FN7, NO, MSTP) +}; + +static const uint16_t PROGMEM fn_actions[] = { + [0] = ACTION_LAYER_MOMENTARY(1), + [1] = ACTION_BACKLIGHT_LEVEL(BACKLIGHT_FN1), + [2] = ACTION_BACKLIGHT_LEVEL(BACKLIGHT_FN2), + [3] = ACTION_BACKLIGHT_LEVEL(BACKLIGHT_FN3), + [4] = ACTION_BACKLIGHT_LEVEL(BACKLIGHT_FN4), + [5] = ACTION_BACKLIGHT_LEVEL(BACKLIGHT_NUMPAD), + [6] = ACTION_BACKLIGHT_LEVEL(BACKLIGHT_REAR), + [7] = ACTION_BACKLIGHT_TOGGLE() +}; diff --git a/keyboard/lightpad/led.c b/keyboard/lightpad/led.c new file mode 100644 index 0000000000..ebfac3af89 --- /dev/null +++ b/keyboard/lightpad/led.c @@ -0,0 +1,24 @@ +/* +Copyright 2014 Ralf Schmitt + +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 . +*/ + +#include +#include "stdint.h" +#include "led.h" + +void led_set(uint8_t usb_led) { + (usb_led & (1< + +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 . +*/ + +#include +#include +#include +#include +#include "print.h" +#include "debug.h" +#include "util.h" +#include "matrix.h" +#include "eeconfig.h" +#include "action_layer.h" +#include "backlight.h" + +#ifndef DEBOUNCE +# define DEBOUNCE 0 +#endif +static uint8_t debouncing = DEBOUNCE; + +static matrix_row_t matrix[MATRIX_ROWS]; +static matrix_row_t matrix_debouncing[MATRIX_ROWS]; + +static uint8_t read_rows(void); +static uint8_t read_fwkey(void); +static void init_rows(void); +static void unselect_cols(void); +static void select_col(uint8_t col); + +inline +uint8_t matrix_rows(void) +{ + return MATRIX_ROWS; +} + +inline +uint8_t matrix_cols(void) +{ + return MATRIX_COLS; +} + +void misc_init(void) { +} + +void matrix_init(void) +{ + backlight_init_ports(); + unselect_cols(); + init_rows(); + for (uint8_t i=0; i < MATRIX_ROWS; i++) { + matrix[i] = 0; + matrix_debouncing[i] = 0; + } +} + +uint8_t matrix_scan(void) +{ + for (uint8_t col = 0; col < MATRIX_COLS; col++) { + select_col(col); + _delay_us(3); + uint8_t rows = read_rows(); + // Use the otherwise unused col: 0 row: 0 for firmware key + if(col == 0) { + rows |= read_fwkey(); + } + for (uint8_t row = 0; row < MATRIX_ROWS; row++) { + bool prev_bit = matrix_debouncing[row] & ((matrix_row_t)1<