diff --git a/keyboard/cw40/Makefile b/keyboard/cw40/Makefile new file mode 100644 index 00000000..ed907a78 --- /dev/null +++ b/keyboard/cw40/Makefile @@ -0,0 +1,159 @@ +#---------------------------------------------------------------------------- +# 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 = cw40_lufa + +# Directory common source filess exist +TMK_DIR = ../../tmk_core_custom + +# Directory keyboard dependent files exist +TARGET_DIR = . + +# project specific files +SRC = keymap_common.c \ + matrix.c \ + led.c \ + ledmap.c \ + backlight.c \ + light_ws2812.c \ + rgb.c + +ifdef KEYMAP + SRC := keymap_$(KEYMAP).c $(SRC) +else + SRC := keymap_default.c $(SRC) +endif + +ifdef VER + OPT_DEFS += -DVER_$(VER) +endif + + +CONFIG_H = config.h + + +# MCU name +#MCU = at90usb1287 +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 = 16000000 + + +# +# 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) + +# Interrupt driven control endpoint task(+60) +OPT_DEFS += -DINTERRUPT_CONTROL_ENDPOINT + + +# Boot Section Size in *bytes* +# Teensy halfKay 512 +# Teensy++ halfKay 1024 +# Atmel DFU loader 4096 +# LUFA bootloader 4096 +# USBaspLoader 2048 +OPT_DEFS += -DBOOTLOADER_SIZE=4096 + +# Additional definitions from command line +ifdef DEFS + OPT_DEFS += $(foreach DEF,$(DEFS),-D$(DEF)) +endif + +# 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 +USB_6KRO_ENABLE = yes # USB 6key Rollover +#PS2_MOUSE_ENABLE = yes # PS/2 mouse(TrackPoint) support +#PS2_USE_BUSYWAIT = yes +BACKLIGHT_ENABLE = yes # Enable keyboard backlight functionality +KEYMAP_IN_EEPROM_ENABLE = yes # Read keymap from eeprom +#KEYMAP_SECTION_ENABLE = yes # Fixed address keymap for keymap editor +SOFTPWM_LED_ENABLE = yes # Enable SoftPWM to drive backlight +FADING_LED_ENABLE = yes # Enable fading backlight +BREATHING_LED_ENABLE = yes # Enable breathing backlight +LEDMAP_ENABLE = yes # Enable LED mapping +LEDMAP_IN_EEPROM_ENABLE = yes # Read LED mapping from eeprom + + +# Optimize size but this may cause error "relocation truncated to fit" +#EXTRALDFLAGS = -Wl,--relax + +# Search Path +VPATH += $(TARGET_DIR) +VPATH += $(TMK_DIR) + +include $(TMK_DIR)/protocol/lufa.mk +include $(TMK_DIR)/common.mk +include $(TMK_DIR)/rules.mk diff --git a/keyboard/cw40/backlight.c b/keyboard/cw40/backlight.c new file mode 100644 index 00000000..c5d9b857 --- /dev/null +++ b/keyboard/cw40/backlight.c @@ -0,0 +1,235 @@ +/* +Copyright 2016 Kai Ryu + +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 "backlight.h" +#ifdef SOFTPWM_LED_ENABLE +#include "softpwm_led.h" +#else +#include "breathing_led.h" +#endif +#include "action.h" +#include "rgb.h" + +#ifdef BACKLIGHT_ENABLE + +void backlight_enable(void); +void backlight_disable(void); +inline void backlight_set_raw(uint8_t raw); + +static const uint8_t backlight_table[] PROGMEM = { + 0, 16, 128, 255 +}; + +extern backlight_config_t backlight_config; +uint8_t backlight_brightness; + +/* Backlight pin configuration + * Backlight: PE6 - OC1B + */ +#ifndef SOFTPWM_LED_ENABLE +void backlight_enable(void) +{ + // Turn on PWM + LED4_DDR |= (1< 0) { + backlight_enable(); + backlight_set_raw(pgm_read_byte(&backlight_table[level])); + } + else { + backlight_disable(); + } +#endif +} + +#ifndef SOFTPWM_LED_ENABLE +#ifdef BREATHING_LED_ENABLE +void breathing_led_set_raw(uint8_t raw) +{ + backlight_set_raw(raw); +} +#endif +#endif + +inline void backlight_set_raw(uint8_t raw) +{ +#ifdef SOFTPWM_LED_ENABLE + softpwm_led_set_all(raw); +#else + LED4_OCR = raw; +#endif +} + +#ifndef LEDMAP_ENABLE +#ifdef SOFTPWM_LED_ENABLE +void softpwm_led_init(void) +{ + DDRE |= (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 . +*/ + +#ifndef CONFIG_H +#define CONFIG_H + + +/* USB Device descriptor parameter */ +#define VENDOR_ID 0x1209 +#define PRODUCT_ID 0x2328 +#ifdef VER_PROTOTYPE +#define DEVICE_VER 0x0400 +#else +#define DEVICE_VER 0x0401 +#endif +#define MANUFACTURER K.T.E.C. +#define PRODUCT CW40 +#define DESCRIPTION t.m.k. keyboard firmware for CW40 + +/* key matrix size */ +#define MATRIX_ROWS 4 +#define MATRIX_COLS 11 + +/* keymap in eeprom */ +#define FN_ACTIONS_COUNT 32 +#define KEYMAPS_COUNT 8 +#define EECONFIG_KEYMAP_IN_EEPROM 0x11 + +/* define if matrix has ghost */ +//#define MATRIX_HAS_GHOST + +/* Set 0 if debouncing isn't needed */ +#define DEBOUNCE 5 + +/* number of backlight levels */ +#ifdef BREATHING_LED_ENABLE +#ifdef FADING_LED_ENABLE +#define BACKLIGHT_LEVELS 8 +#else +#define BACKLIGHT_LEVELS 6 +#endif +#else +#define BACKLIGHT_LEVELS 3 +#endif + +/* enable customized backlight logic */ +#define BACKLIGHT_CUSTOM +#define RGB_LED_ENABLE +#define CUSTOM_LED_ENABLE +#define SOFTPWM_LED_FREQ 80 + +/* number of leds */ +#define LED_COUNT 4 +//#define EECONFIG_LEDMAP_IN_EEPROM 8 + +/* Mechanical locking support. Use KC_LCAP, KC_LNUM or KC_LSCR instead in keymap */ +#define LOCKING_SUPPORT_ENABLE + +/* Locking resynchronize hack */ +#define LOCKING_RESYNC_ENABLE + +/* key combination for command */ +#define IS_COMMAND() ( \ + keyboard_report->mods == (MOD_BIT(KC_LSHIFT) | MOD_BIT(KC_RSHIFT)) \ +) + +//#define SUSPEND_ACTION + +/* + * Feature disable options + * These options are also useful to firmware size reduction. + */ + +/* disable debug print */ +//#define NO_DEBUG + +/* disable print */ +//#define NO_PRINT + +/* disable action features */ +//#define NO_ACTION_LAYER +//#define NO_ACTION_TAPPING +//#define NO_ACTION_ONESHOT +//#define NO_ACTION_MACRO +//#define NO_ACTION_FUNCTION + +#endif diff --git a/keyboard/cw40/keymap_common.c b/keyboard/cw40/keymap_common.c new file mode 100644 index 00000000..dba2dd65 --- /dev/null +++ b/keyboard/cw40/keymap_common.c @@ -0,0 +1,53 @@ +/* +Copyright 2016 Kai Ryu + +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 "keymap.h" +#include "keymap_in_eeprom.h" +#include "keymap_common.h" + +/* translates key to keycode */ +uint8_t keymap_key_to_keycode(uint8_t layer, keypos_t key) +{ +#ifndef KEYMAP_IN_EEPROM_ENABLE + return pgm_read_byte(&keymaps[(layer)][(key.row)][(key.col)]); +#else + return eeconfig_read_keymap_key(layer, key.row, key.col); +#endif +} + +/* translates Fn keycode to action */ +action_t keymap_fn_to_action(uint8_t keycode) +{ + return (action_t) { +#ifndef KEYMAP_IN_EEPROM_ENABLE + .code = pgm_read_word(&fn_actions[FN_INDEX(keycode)]) +#else + .code = eeconfig_read_keymap_fn_action(FN_INDEX(keycode)) +#endif + }; +} + +#ifdef KEYMAP_IN_EEPROM_ENABLE +const uint8_t* keymaps_pointer(void) { + return (const uint8_t*)keymaps; +} + +const uint16_t* fn_actions_pointer(void) { + return fn_actions; +} +#endif diff --git a/keyboard/cw40/keymap_common.h b/keyboard/cw40/keymap_common.h new file mode 100644 index 00000000..59b30d1e --- /dev/null +++ b/keyboard/cw40/keymap_common.h @@ -0,0 +1,41 @@ +/* +Copyright 2016 Kai Ryu + +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 KEYMAP_COMMON_H +#define KEYMAP_COMMON_H + +#include + + +extern const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS]; +extern const uint16_t fn_actions[]; + + +/* CW40 keymap definition macro + */ +#define KEYMAP( \ + K0A, K0B, K0C, K0D, K0E, K0F, K0G, K0H, K0I, K0J, K0K, K3K, \ + K1A, K1B, K1C, K1D, K1E, K1F, K1G, K1H, K1I, K1J, K1K, \ + K2A, K2B, K2C, K2D, K2E, K2F, K2G, K2H, K2I, K2J, K2K, \ + K3A, K3B, K3C, K3F, K3I, K3J \ +) { \ + { KC_##K0A, KC_##K0B, KC_##K0C, KC_##K0D, KC_##K0E, KC_##K0F, KC_##K0G, KC_##K0H, KC_##K0I, KC_##K0J, KC_##K0K }, \ + { KC_##K1A, KC_##K1B, KC_##K1C, KC_##K1D, KC_##K1E, KC_##K1F, KC_##K1G, KC_##K1H, KC_##K1I, KC_##K1J, KC_##K1K }, \ + { KC_##K2A, KC_##K2B, KC_##K2C, KC_##K2D, KC_##K2E, KC_##K2F, KC_##K2G, KC_##K2H, KC_##K2I, KC_##K2J, KC_##K2K }, \ + { KC_##K3A, KC_##K3B, KC_##K3C, KC_NO, KC_NO, KC_##K3F, KC_NO, KC_NO, KC_##K3I, KC_##K3J, KC_##K3K } \ +} + +#endif diff --git a/keyboard/cw40/keymap_default.c b/keyboard/cw40/keymap_default.c new file mode 100644 index 00000000..3ed42199 --- /dev/null +++ b/keyboard/cw40/keymap_default.c @@ -0,0 +1,179 @@ +/* +Copyright 2016 Kai Ryu + +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 "keycode.h" +#include "action.h" +#include "action_util.h" +#include "keymap_common.h" +#include "rgb.h" + + +// Default +#ifdef KEYMAP_SECTION_ENABLE +const uint8_t keymaps[KEYMAPS_COUNT][MATRIX_ROWS][MATRIX_COLS] __attribute__ ((section (".keymap.keymaps"))) = { +#else +const uint8_t keymaps[][MATRIX_ROWS][MATRIX_COLS] PROGMEM = { +#endif + /* Keymap 0: Default Layer + * ,-----------------------------------------------. + * |Fn3| Q| W| E| R| T| Y| U| I| O| P| BS| + * |-----------------------------------------------| + * |Tab | A| S| D| F| G| H| J| K| L|Enter | + * |-----------------------------------------------| + * |Shift | Z| X| C| V| B| N| M| .|RShi|Fn0| + * |-----------------------------------------------| + * |Ctrl |Gui|Alt | Space |Fn1|Ctrl| + * `-----------------------------------------------' + */ + KEYMAP( + FN3, Q, W, E, R, T, Y, U, I, O, P, BSPC, \ + TAB, A, S, D, F, G, H, J, K, L, ENT, \ + LSFT, Z, X, C, V, B, N, M, DOT, RSFT,FN0, \ + LCTL,LGUI,LALT, SPC, FN1, RCTL ), + /* Keymap 1: Arrow Keys Overlay + * ,-----------------------------------------------. + * | | |Up | | | | | | | | | | + * |-----------------------------------------------| + * | |Lef|Dow|Rig| | | | | | | | + * |-----------------------------------------------| + * | | | | | | | | | | | | + * |-----------------------------------------------| + * | | | | | | | + * `-----------------------------------------------' + */ + KEYMAP( + TRNS,TRNS,UP, TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS, \ + TRNS,LEFT,DOWN,RGHT,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS, TRNS, \ + TRNS, TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS, \ + TRNS,TRNS,TRNS, TRNS, TRNS,TRNS ), + /* Keymap 2: Fn Layer 1 + * ,-----------------------------------------------. + * | 1| 2| 3| 4| 5| 6| 7| 8| 8| 9| -| =| + * |-----------------------------------------------| + * |Caps| | | | | | | | | ;| '| + * |-----------------------------------------------| + * | |Fn7|Fn8|Fn5|Fn4|Fn6| | | ,| /| | + * |-----------------------------------------------| + * | | | | Fn2 | | | + * `-----------------------------------------------' + */ + KEYMAP( + 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, MINS,EQL, \ + CAPS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,SCLN, QUOT, \ + TRNS, FN7, FN8, FN5, FN4, FN6, TRNS,TRNS,COMM,SLSH,TRNS, \ + TRNS,TRNS,TRNS, FN2, TRNS,TRNS ), + /* Keymap 3: Fn Layer 2 + * ,-----------------------------------------------. + * |F1 |F2 |F3 |F4 |F5 |F6 |F7 |F8 |F9 |F10|F11|F12| + * |-----------------------------------------------| + * |Caps| | | | | | | | [| ]|\ | + * |-----------------------------------------------| + * | |Fn7|Fn8|Fn5|Fn4|Fn6| | | ,| /| | + * |-----------------------------------------------| + * | | | | Fn2 | | | + * `-----------------------------------------------' + */ + KEYMAP( + F1, F2, F3, F4, F5, F6, F7, F8, F9, F10, F11, F12, \ + CAPS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,TRNS,LBRC,RBRC, BSLS, \ + TRNS, FN7, FN8, FN5, FN4, FN6, TRNS,TRNS,COMM,SLSH,TRNS, \ + TRNS,TRNS,TRNS, FN2, TRNS,TRNS ), +}; + +#ifndef NO_ACTION_FUNCTION +enum function_id { + AF_RGB_TOGGLE = 0, + AF_RGB_DECREASE, + AF_RGB_INCREASE, + AF_RGB_STEP, + AF_TRICKY_ESC +}; +#endif + +/* + * Fn action definition + */ +#ifdef KEYMAP_SECTION_ENABLE +const uint16_t fn_actions[FN_ACTIONS_COUNT] __attribute__ ((section (".keymap.fn_actions"))) = { +#else +const uint16_t fn_actions[] PROGMEM = { +#endif + [0] = ACTION_LAYER_MOMENTARY(2), + [1] = ACTION_LAYER_MOMENTARY(3), + [2] = ACTION_LAYER_TOGGLE(1), + [3] = ACTION_FUNCTION(AF_TRICKY_ESC), + [4] = ACTION_BACKLIGHT_TOGGLE(), + [5] = ACTION_BACKLIGHT_DECREASE(), + [6] = ACTION_BACKLIGHT_INCREASE(), + [7] = ACTION_FUNCTION(AF_RGB_TOGGLE), + [8] = ACTION_FUNCTION(AF_RGB_STEP) +}; + +#ifdef KEYMAP_IN_EEPROM_ENABLE +uint16_t keys_count(void) { + return sizeof(keymaps) / sizeof(keymaps[0]) * MATRIX_ROWS * MATRIX_COLS; +} + +uint16_t fn_actions_count(void) { + return sizeof(fn_actions) / sizeof(fn_actions[0]); +} +#endif + +#ifndef NO_ACTION_FUNCTION +#define MODS_SHIFT_MASK (MOD_BIT(KC_LSHIFT)|MOD_BIT(KC_RSHIFT)) +void action_function(keyrecord_t *record, uint8_t id, uint8_t opt) +{ +#ifdef RGB_LED_ENABLE + if (record->event.pressed) { + switch (id) { + case AF_RGB_TOGGLE: + rgb_toggle(); + break; + case AF_RGB_DECREASE: + rgb_decrease(); + break; + case AF_RGB_INCREASE: + rgb_increase(); + break; + case AF_RGB_STEP: + rgb_step(); + break; + } + } +#endif + static uint8_t tricky_esc_registered; + switch (id) { + case AF_TRICKY_ESC: + if (record->event.pressed) { + if (get_mods() & MODS_SHIFT_MASK) { + tricky_esc_registered = KC_GRV; + } + else { + tricky_esc_registered = KC_ESC; + } + register_code(tricky_esc_registered); + send_keyboard_report(); + } + else { + unregister_code(tricky_esc_registered); + send_keyboard_report(); + } + break; + } +} +#endif diff --git a/keyboard/cw40/led.c b/keyboard/cw40/led.c new file mode 100644 index 00000000..539a7905 --- /dev/null +++ b/keyboard/cw40/led.c @@ -0,0 +1,39 @@ +/* +Copyright 2016 Kai Ryu + +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" + +#ifndef LEDMAP_ENABLE + +/* LED pin configuration + */ +void led_set(uint8_t usb_led) +{ + if (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 "ledmap.h" + +#ifdef LEDMAP_ENABLE + +static const uint16_t ledmaps[LED_COUNT] PROGMEM = { + [0] = LEDMAP_CAPS_LOCK | LEDMAP_BACKLIGHT, // CapsLock - PC6 + [1] = LEDMAP_LAYER(3) | LEDMAP_BACKLIGHT, // Esc - PD7 + [2] = LEDMAP_LAYER(1) | LEDMAP_BACKLIGHT, // WASD - PD6 + [3] = LEDMAP_BACKLIGHT, // Backlight - PE6 +}; + +ledmap_t ledmap_get_code(uint8_t index) +{ + return (ledmap_t) { .code = pgm_read_word(&ledmaps[index]) }; +} + +/* LED pin configration + */ +void ledmap_led_init(void) +{ +#ifdef VER_PROTOTYPE + DDRC |= (1< +#include +#include + +void inline ws2812_setleds(struct cRGB *ledarray, uint16_t leds) +{ + ws2812_setleds_pin(ledarray,leds, _BV(ws2812_pin)); +} + +void inline ws2812_setleds_pin(struct cRGB *ledarray, uint16_t leds, uint8_t pinmask) +{ + ws2812_DDRREG |= pinmask; // Enable DDR + ws2812_sendarray_mask((uint8_t*)ledarray,leds+leds+leds,pinmask); + _delay_us(50); +} + +void ws2812_sendarray(uint8_t *data,uint16_t datlen) +{ + ws2812_sendarray_mask(data,datlen,_BV(ws2812_pin)); +} + +/* + This routine writes an array of bytes with RGB values to the Dataout pin + using the fast 800kHz clockless WS2811/2812 protocol. +*/ + +// Timing in ns +#define w_zeropulse 350 +#define w_onepulse 900 +#define w_totalperiod 1250 + +// Fixed cycles used by the inner loop +#define w_fixedlow 2 +#define w_fixedhigh 4 +#define w_fixedtotal 8 + +// Insert NOPs to match the timing, if possible +#define w_zerocycles (((F_CPU/1000)*w_zeropulse )/1000000) +#define w_onecycles (((F_CPU/1000)*w_onepulse +500000)/1000000) +#define w_totalcycles (((F_CPU/1000)*w_totalperiod +500000)/1000000) + +// w1 - nops between rising edge and falling edge - low +#define w1 (w_zerocycles-w_fixedlow) +// w2 nops between fe low and fe high +#define w2 (w_onecycles-w_fixedhigh-w1) +// w3 nops to complete loop +#define w3 (w_totalcycles-w_fixedtotal-w1-w2) + +#if w1>0 + #define w1_nops w1 +#else + #define w1_nops 0 +#endif + +// The only critical timing parameter is the minimum pulse length of the "0" +// Warn or throw error if this timing can not be met with current F_CPU settings. +#define w_lowtime ((w1_nops+w_fixedlow)*1000000)/(F_CPU/1000) +#if w_lowtime>550 + #error "Light_ws2812: Sorry, the clock speed is too low. Did you set F_CPU correctly?" +#elif w_lowtime>450 + #warning "Light_ws2812: The timing is critical and may only work on WS2812B, not on WS2812(S)." + #warning "Please consider a higher clockspeed, if possible" +#endif + +#if w2>0 +#define w2_nops w2 +#else +#define w2_nops 0 +#endif + +#if w3>0 +#define w3_nops w3 +#else +#define w3_nops 0 +#endif + +#define w_nop1 "nop \n\t" +#define w_nop2 "rjmp .+0 \n\t" +#define w_nop4 w_nop2 w_nop2 +#define w_nop8 w_nop4 w_nop4 +#define w_nop16 w_nop8 w_nop8 + +void inline ws2812_sendarray_mask(uint8_t *data,uint16_t datlen,uint8_t maskhi) +{ + uint8_t curbyte,ctr,masklo; + uint8_t sreg_prev; + + masklo =~maskhi&ws2812_PORTREG; + maskhi |= ws2812_PORTREG; + sreg_prev=SREG; + cli(); + + while (datlen--) { + curbyte=*data++; + + asm volatile( + " ldi %0,8 \n\t" + "loop%=: \n\t" + " out %2,%3 \n\t" // '1' [01] '0' [01] - re +#if (w1_nops&1) +w_nop1 +#endif +#if (w1_nops&2) +w_nop2 +#endif +#if (w1_nops&4) +w_nop4 +#endif +#if (w1_nops&8) +w_nop8 +#endif +#if (w1_nops&16) +w_nop16 +#endif + " sbrs %1,7 \n\t" // '1' [03] '0' [02] + " out %2,%4 \n\t" // '1' [--] '0' [03] - fe-low + " lsl %1 \n\t" // '1' [04] '0' [04] +#if (w2_nops&1) + w_nop1 +#endif +#if (w2_nops&2) + w_nop2 +#endif +#if (w2_nops&4) + w_nop4 +#endif +#if (w2_nops&8) + w_nop8 +#endif +#if (w2_nops&16) + w_nop16 +#endif + " out %2,%4 \n\t" // '1' [+1] '0' [+1] - fe-high +#if (w3_nops&1) +w_nop1 +#endif +#if (w3_nops&2) +w_nop2 +#endif +#if (w3_nops&4) +w_nop4 +#endif +#if (w3_nops&8) +w_nop8 +#endif +#if (w3_nops&16) +w_nop16 +#endif + + " dec %0 \n\t" // '1' [+2] '0' [+2] + " brne loop%=\n\t" // '1' [+3] '0' [+4] + : "=&d" (ctr) + : "r" (curbyte), "I" (_SFR_IO_ADDR(ws2812_PORTREG)), "r" (maskhi), "r" (masklo) + ); + } + + SREG=sreg_prev; +} diff --git a/keyboard/cw40/light_ws2812.h b/keyboard/cw40/light_ws2812.h new file mode 100644 index 00000000..bc2c4e86 --- /dev/null +++ b/keyboard/cw40/light_ws2812.h @@ -0,0 +1,63 @@ +/* + * light weight WS2812 lib include + * + * Version 2.0a3 - Jan 18th 2014 + * Author: Tim (cpldcpu@gmail.com) + * + * Please do not change this file! All configuration is handled in "ws2812_config.h" + * + * License: GNU GPL v2 (see License.txt) + + + */ + +#ifndef LIGHT_WS2812_H_ +#define LIGHT_WS2812_H_ + +#include +#include +#include "ws2812_config.h" + +/* + * Structure of the LED array + */ + +struct cRGB { uint8_t g; uint8_t r; uint8_t b; }; + +/* User Interface + * + * Input: + * ledarray: An array of GRB data describing the LED colors + * number_of_leds: The number of LEDs to write + * pinmask (optional): Bitmask describing the output bin. e.g. _BV(PB0) + * + * The functions will perform the following actions: + * - Set the data-out pin as output + * - Send out the LED data + * - Wait 50µs to reset the LEDs + */ + +void ws2812_setleds (struct cRGB *ledarray, uint16_t number_of_leds); +void ws2812_setleds_pin(struct cRGB *ledarray, uint16_t number_of_leds,uint8_t pinmask); + +/* + * Old interface / Internal functions + * + * The functions take a byte-array and send to the data output as WS2812 bitstream. + * The length is the number of bytes to send - three per LED. + */ + +void ws2812_sendarray (uint8_t *array,uint16_t length); +void ws2812_sendarray_mask(uint8_t *array,uint16_t length, uint8_t pinmask); + + +/* + * Internal defines + */ + +#define CONCAT(a, b) a ## b +#define CONCAT_EXP(a, b) CONCAT(a, b) + +#define ws2812_PORTREG CONCAT_EXP(PORT,ws2812_port) +#define ws2812_DDRREG CONCAT_EXP(DDR,ws2812_port) + +#endif /* LIGHT_WS2812_H_ */ diff --git a/keyboard/cw40/matrix.c b/keyboard/cw40/matrix.c new file mode 100644 index 00000000..c32ed208 --- /dev/null +++ b/keyboard/cw40/matrix.c @@ -0,0 +1,179 @@ +/* +Copyright 2016 Kai Ryu + +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 +#include "print.h" +#include "debug.h" +#include "util.h" +#include "matrix.h" + +#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(void); +static void init_cols(void); +static void unselect_rows(void); +static void select_row(uint8_t row); + +inline +uint8_t matrix_rows(void) +{ + return MATRIX_ROWS; +} + +inline +uint8_t matrix_cols(void) +{ + return MATRIX_COLS; +} + +void matrix_init(void) +{ + // disable JTAG + MCUCR = (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 "softpwm_led.h" +#include "backlight.h" +#include "rgb.h" +#include "light_ws2812.h" +#include "debug.h" + +#ifdef RGB_LED_ENABLE + +volatile static uint8_t rgb_fading_enable = 0; +static rgb_config_t rgb_config; +static struct cRGB rgb_color[RGB_LED_COUNT]; +static uint16_t rgb_hue = 0; +static uint8_t rgb_saturation = 255; +static uint8_t rgb_brightness = 16; +static uint8_t rgb_rainbow = 0; + +extern backlight_config_t backlight_config; +extern uint8_t backlight_brightness; + +static void rgb_write_config(void); +static void rgb_read_config(void); +static void rgb_set_level(uint8_t level); +static void rgb_refresh(void); +#if 0 +static void hue_to_rgb(uint16_t hue, struct cRGB *rgb); +#endif +static void hsb_to_rgb(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *rgb); + +void rgb_init(void) +{ + rgb_read_config(); + if (rgb_config.raw == RGB_UNCONFIGURED) { + rgb_config.enable = 0; + rgb_config.level = RGB_OFF; + rgb_write_config(); + } + if (rgb_config.enable) { + rgb_set_level(rgb_config.level); + } +} + +void rgb_read_config(void) +{ + rgb_config.raw = eeprom_read_byte(EECONFIG_RGB); +} + +void rgb_write_config(void) +{ + eeprom_write_byte(EECONFIG_RGB, rgb_config.raw); +} + +void rgb_toggle(void) +{ + if (rgb_config.enable) { + rgb_off(); + } + else { + rgb_on(); + } +} + +void rgb_on(void) +{ + rgb_config.enable = 1; + rgb_set_level(rgb_config.level); + rgb_write_config(); +} + +void rgb_off(void) +{ + rgb_config.enable = 0; + rgb_set_level(RGB_OFF); + rgb_write_config(); +} + +void rgb_decrease(void) +{ + if(rgb_config.level > 0) { + rgb_config.level--; + rgb_config.enable = (rgb_config.level != 0); + rgb_write_config(); + } + rgb_set_level(rgb_config.level); +} + +void rgb_increase(void) +{ + if(rgb_config.level < RGB_LEVELS) { + rgb_config.level++; + rgb_config.enable = 1; + rgb_write_config(); + } + rgb_set_level(rgb_config.level); +} + +void rgb_step(void) +{ + rgb_config.level++; + if(rgb_config.level > RGB_LEVELS) + { + rgb_config.level = 0; + } + rgb_config.enable = (rgb_config.level != 0); + rgb_set_level(rgb_config.level); +} + +void rgb_set_level(uint8_t level) +{ + xprintf("RGB Level: %d\n", level); + if (level == RGB_OFF) { + rgb_brightness = 0; + } + else if (backlight_config.enable) { + if (backlight_config.level >= 1 && backlight_config.level <= 3) { + rgb_brightness = backlight_brightness; + } + } + else { + rgb_brightness = 16; + } + if (level <= RGB_WHITE) { + rgb_fading_enable = 0; + rgb_rainbow = 0; + if (level != RGB_OFF) { + if (level == RGB_WHITE) { + rgb_saturation = 0; + } + else { + rgb_hue = (level - 1) * 128; + rgb_saturation = 255; + } + if (backlight_config.enable) { + if (backlight_config.level >= 1 && backlight_config.level <= 3) { + rgb_brightness = backlight_brightness; + } + } + else { + rgb_brightness = 16; + } + } + rgb_refresh(); + } + else { + rgb_saturation = 255; + rgb_fading_enable = 1; + rgb_rainbow = (level >= RGB_RAINBOW) ? 1 : 0; + } +} + +void rgb_set_brightness(uint8_t brightness) +{ + if (rgb_config.enable) { + rgb_brightness = brightness; + rgb_refresh(); + } +} + +void rgb_refresh(void) +{ + struct cRGB rgb; + uint16_t hue; + uint8_t i; + if (rgb_rainbow) { + for (i = 0; i < RGB_LED_COUNT; i++) { +#ifdef VER_PROTOTYPE + uint8_t j; + if (i == 0) j = 1; + else if (i == 1) j = 0; + else j = i; +#endif + hue = rgb_hue + (768 / RGB_LED_COUNT) * i; + hsb_to_rgb(hue, rgb_saturation, rgb_brightness, &rgb); +#ifdef VER_PROTOTYPE + rgb_color[j] = rgb; +#else + rgb_color[i] = rgb; +#endif + } + } + else { + hsb_to_rgb(rgb_hue, rgb_saturation, rgb_brightness, &rgb); + for (i = 0; i < RGB_LED_COUNT; i++) { + rgb_color[i] = rgb; + } + } + /* xprintf("R%d G%d B%d\n", rgb_color[0].r, rgb_color[0].g, rgb_color[0].b); */ + ws2812_setleds(rgb_color, RGB_LED_COUNT); +} + +#if 0 +void hue_to_rgb(uint16_t hue, struct cRGB *rgb) +{ + uint8_t hi = hue / 60; + uint16_t f = (hue % 60) * 425 / 100; + uint8_t q = 255 - f; + switch (hi) { + case 0: rgb->r = 255; rgb->g = f; rgb->b = 0; break; + case 1: rgb->r = q; rgb->g = 255; rgb->b = 0; break; + case 2: rgb->r = 0; rgb->g = 255; rgb->b = f; break; + case 3: rgb->r = 0; rgb->g = q; rgb->b = 255; break; + case 4: rgb->r = f; rgb->g = 0; rgb->b = 255; break; + case 5: rgb->r = 255; rgb->g = 0; rgb->b = q; break; + } +} +#endif + +/* + * original code: https://blog.adafruit.com/2012/03/14/constant-brightness-hsb-to-rgb-algorithm/ + */ +void hsb_to_rgb(uint16_t hue, uint8_t saturation, uint8_t brightness, struct cRGB *rgb) +{ + uint8_t temp[5]; + uint8_t n = (hue >> 8) % 3; + uint8_t x = ((((hue & 255) * saturation) >> 8) * brightness) >> 8; + uint8_t s = ((256 - saturation) * brightness) >> 8; + temp[0] = temp[3] = s; + temp[1] = temp[4] = x + s; + temp[2] = brightness - x; + rgb->r = temp[n + 2]; + rgb->g = temp[n + 1]; + rgb->b = temp[n]; +} + +void rgb_fading(void) +{ + static uint8_t step = 0; + static uint16_t hue = 0; + if (rgb_fading_enable) { + if (++step > rgb_fading_enable) { + step = 0; + rgb_hue = hue; + rgb_refresh(); + if (++hue >= 768) { + hue = 0; + } + } + } +} + +#endif diff --git a/keyboard/cw40/rgb.h b/keyboard/cw40/rgb.h new file mode 100644 index 00000000..4fb1d667 --- /dev/null +++ b/keyboard/cw40/rgb.h @@ -0,0 +1,60 @@ +/* +Copyright 2016 Kai Ryu + +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 RGB_H +#define RGB_H + +#include +#include + +typedef union { + uint8_t raw; + struct { + uint8_t level :7; + bool enable :1; + }; +} rgb_config_t; + +enum { + RGB_OFF = 0, + RGB_RED, + RGB_YELLOW, + RGB_GREEN, + RGB_CYAN, + RGB_BLUE, + RGB_MAGENTA, + RGB_WHITE, + RGB_FADE, + RGB_RAINBOW, + RGB_LEVELS = RGB_RAINBOW +}; + +#define EECONFIG_RGB (uint8_t *)10 +#define RGB_UNCONFIGURED 0xFF +#define RGB_LED_COUNT 12 + +void rgb_init(void); +void rgb_toggle(void); +void rgb_on(void); +void rgb_off(void); +void rgb_decrease(void); +void rgb_increase(void); +void rgb_step(void); +void rgb_set_brightness(uint8_t brightness); +void rgb_fading(void); + +#endif diff --git a/keyboard/cw40/ws2812_config.h b/keyboard/cw40/ws2812_config.h new file mode 100644 index 00000000..349d6266 --- /dev/null +++ b/keyboard/cw40/ws2812_config.h @@ -0,0 +1,21 @@ +/* + * light_ws2812_config.h + * + * Created: 18.01.2014 09:58:15 + * + * User Configuration file for the light_ws2812_lib + * + */ + + +#ifndef WS2812_CONFIG_H_ +#define WS2812_CONFIG_H_ + +/////////////////////////////////////////////////////////////////////// +// Define I/O pin +/////////////////////////////////////////////////////////////////////// + +#define ws2812_port C // Data port +#define ws2812_pin 7 // Data out pin + +#endif /* WS2812_CONFIG_H_ */