~ruther/qmk_firmware

10eb70acb4c8a03fc5bda32d9c23fc41266aef7a — tmk 11 years ago 523cc6a + af19f56
Merge pull request #134 from xauser/master

Support for Lightpad keypad
A keyboard/lightpad/Makefile.lufa => keyboard/lightpad/Makefile.lufa +117 -0
@@ 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

A keyboard/lightpad/README.md => keyboard/lightpad/README.md +24 -0
@@ 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.

A keyboard/lightpad/backlight.c => keyboard/lightpad/backlight.c +129 -0
@@ 0,0 1,129 @@
/*
Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>

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 <http://www.gnu.org/licenses/>.
*/

#include <avr/io.h>
#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);
}

A keyboard/lightpad/backlight.h => keyboard/lightpad/backlight.h +39 -0
@@ 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);

A keyboard/lightpad/config.h => keyboard/lightpad/config.h +46 -0
@@ 0,0 1,46 @@
/*
Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>

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 <http://www.gnu.org/licenses/>.
*/

#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

A keyboard/lightpad/keymap.c => keyboard/lightpad/keymap.c +73 -0
@@ 0,0 1,73 @@
/*
Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>

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 <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>
#include <stdbool.h>
#include <avr/pgmspace.h>
#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;
}

A keyboard/lightpad/keymap_lightpad.h => keyboard/lightpad/keymap_lightpad.h +29 -0
@@ 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()
};

A keyboard/lightpad/led.c => keyboard/lightpad/led.c +24 -0
@@ 0,0 1,24 @@
/*
Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>

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 <http://www.gnu.org/licenses/>.
*/

#include <avr/io.h>
#include "stdint.h"
#include "led.h"

void led_set(uint8_t usb_led) {
    (usb_led & (1<<USB_LED_NUM_LOCK)) ? backlight_enable_numlock() : backlight_disable_numlock();
}

A keyboard/lightpad/matrix.c => keyboard/lightpad/matrix.c +205 -0
@@ 0,0 1,205 @@
/*
Copyright 2014 Ralf Schmitt <ralf@bunkertor.net>

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 <http://www.gnu.org/licenses/>.
*/

#include <stdint.h>
#include <stdbool.h>
#include <avr/io.h>
#include <util/delay.h>
#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<<col);
            bool curr_bit = rows & (1<<row);
            if (prev_bit != curr_bit) {
                matrix_debouncing[row] ^= ((matrix_row_t)1<<col);
                if (debouncing) {
                    dprint("bounce!: "); dprintf("%02X", debouncing); dprintln();
                }
                debouncing = DEBOUNCE;
            }
        }
        unselect_cols();
    }

    if (debouncing) {
        if (--debouncing) {
            _delay_ms(1);
        } else {
            for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
                matrix[i] = matrix_debouncing[i];
            }
        }
    }

    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<<col));
}

inline
matrix_row_t matrix_get_row(uint8_t row)
{
    return matrix[row];
}

void matrix_print(void)
{
    print("\nr/c 0123456789ABCDEF\n");
    for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
        xprintf("%02X: %032lb\n", row, bitrev32(matrix_get_row(row)));
    }
}

uint8_t matrix_key_count(void)
{
    uint8_t count = 0;
    for (uint8_t i = 0; i < MATRIX_ROWS; i++) {
        count += bitpop32(matrix[i]);
    }
    return count;
}

/* Row configuration
 *
 * row: 0    1    2    3    4    5
 * pin: PD0  PD1  PD2  PD3  PD5  PB7
 *
 * Firmware uses pin PE2
 */
static void init_rows(void)
{
    DDRD  &= ~0b00101111;
    PORTD |=  0b00101111;

    DDRB  &= ~0b10000000;
    PORTB |=  0b10000000;

    DDRE  &= ~0b00000100;
    PORTE |=  0b00000100;
}

static uint8_t read_rows(void)
{
    return (PIND&(1<<0) ? (1<<0) : 0) |
           (PIND&(1<<1) ? (1<<1) : 0) |
           (PIND&(1<<2) ? (1<<2) : 0) |
           (PIND&(1<<3) ? (1<<3) : 0) |
           (PIND&(1<<5) ? (1<<4) : 0) |
           (PINB&(1<<7) ? (1<<5) : 0);
}

static uint8_t read_fwkey(void)
{
    return PINE&(1<<2) ? 0 : (1<<0);
}

/* Column configuration
 *
 * col: 0    1    2    3
 * pin: PF0  PF1  PC7  PC6
 */
static void unselect_cols(void)
{
    DDRF |=   0b00000011;
    PORTF &= ~0b00000011;
    DDRC |=   0b11000000;
    PORTC &= ~0b11000000;
}

static void select_col(uint8_t col)
{
    switch (col) {
        case 0:
            PORTF |= (1<<0);
            break;
        case 1:
            PORTF |= (1<<1);
            break;
        case 2:
            PORTC |= (1<<7);
            break;
        case 3:
            PORTC |= (1<<6);
            break;
    }
}