/* | |||||
Copyright 2012,2013 Jun Wako <[email protected]> | |||||
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 KEYMAP_COMMON_H | |||||
#define KEYMAP_COMMON_H | |||||
#include <stdint.h> | |||||
#include <stdbool.h> | |||||
#include "keycode.h" | |||||
#include "action.h" | |||||
#include "action_macro.h" | |||||
#include "report.h" | |||||
#include "host.h" | |||||
#include "print.h" | |||||
#include "debug.h" | |||||
#include "keymap.h" | |||||
#define KEYMAP( \ | |||||
K00, K01, K02, K03, K04, K05, K06, K07, K08, K09, K0a, K0b, \ | |||||
K10, K11, K12, K13, K14, K15, K16, K17, K18, K19, K1a, K1b, \ | |||||
K20, K21, K22, K23, K24, K25, K26, K27, K28, K29, K2a, K2b \ | |||||
) \ | |||||
{ \ | |||||
{ KC_##K00, KC_##K01, KC_##K02, KC_##K03, KC_##K04, KC_##K05, KC_##K06, KC_##K07, KC_##K08, KC_##K09, KC_##K0a, KC_##K0b }, \ | |||||
{ KC_##K10, KC_##K11, KC_##K12, KC_##K13, KC_##K14, KC_##K15, KC_##K16, KC_##K17, KC_##K18, KC_##K19, KC_##K1a, KC_##K1b }, \ | |||||
{ KC_##K20, KC_##K21, KC_##K22, KC_##K23, KC_##K24, KC_##K25, KC_##K26, KC_##K27, KC_##K28, KC_##K29, KC_##K2a, KC_##K2b } \ | |||||
} | |||||
#endif |
/* | |||||
Copyright 2012 Jun Wako <[email protected]> | |||||
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) | |||||
{ | |||||
} |
/* | |||||
* light weight WS2812 lib V2.0b | |||||
* | |||||
* Controls WS2811/WS2812/WS2812B RGB-LEDs | |||||
* Author: Tim ([email protected]) | |||||
* | |||||
* Jan 18th, 2014 v2.0b Initial Version | |||||
* Nov 29th, 2015 v2.3 Added SK6812RGBW support | |||||
* | |||||
* License: GNU GPL v2 (see License.txt) | |||||
*/ | |||||
#include "light_ws2812.h" | |||||
#include <avr/interrupt.h> | |||||
#include <avr/io.h> | |||||
#include <util/delay.h> | |||||
#include "debug.h" | |||||
// Setleds for standard RGB | |||||
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); | |||||
} | |||||
// Setleds for SK6812RGBW | |||||
void inline ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t leds) | |||||
{ | |||||
ws2812_DDRREG |= _BV(ws2812_pin); // Enable DDR | |||||
ws2812_sendarray_mask((uint8_t*)ledarray,leds<<2,_BV(ws2812_pin)); | |||||
_delay_us(80); | |||||
} | |||||
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; | |||||
} |
/* | |||||
* light weight WS2812 lib include | |||||
* | |||||
* Version 2.3 - Nev 29th 2015 | |||||
* Author: Tim ([email protected]) | |||||
* | |||||
* 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 <avr/io.h> | |||||
#include <avr/interrupt.h> | |||||
//#include "ws2812_config.h" | |||||
/* | |||||
* Structure of the LED array | |||||
* | |||||
* cRGB: RGB for WS2812S/B/C/D, SK6812, SK6812Mini, SK6812WWA, APA104, APA106 | |||||
* cRGBW: RGBW for SK6812RGBW | |||||
*/ | |||||
struct cRGB { uint8_t g; uint8_t r; uint8_t b; }; | |||||
struct cRGBW { uint8_t g; uint8_t r; uint8_t b; uint8_t w;}; | |||||
/* 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); | |||||
void ws2812_setleds_rgbw(struct cRGBW *ledarray, uint16_t number_of_leds); | |||||
/* | |||||
* 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 | |||||
*/ | |||||
#ifndef CONCAT | |||||
#define CONCAT(a, b) a ## b | |||||
#endif | |||||
#ifndef CONCAT_EXP | |||||
#define CONCAT_EXP(a, b) CONCAT(a, b) | |||||
#endif | |||||
// #define ws2812_PORTREG CONCAT_EXP(PORT,ws2812_port) | |||||
// #define ws2812_DDRREG CONCAT_EXP(DDR,ws2812_port) | |||||
#endif /* LIGHT_WS2812_H_ */ |
/* | |||||
Copyright 2012 Jun Wako <[email protected]> | |||||
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/>. | |||||
*/ | |||||
/* | |||||
* scan matrix | |||||
*/ | |||||
#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" | |||||
#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) | |||||
{ | |||||
UCSR0B = 0; //Turn off serial | |||||
rgblight_init(); | |||||
// initialize row and col | |||||
unselect_rows(); | |||||
init_cols(); | |||||
// initialize matrix state: all keys off | |||||
for (uint8_t i=0; i < MATRIX_ROWS; i++) { | |||||
matrix[i] = 0; | |||||
matrix_debouncing[i] = 0; | |||||
} | |||||
} | |||||
uint8_t matrix_scan(void) | |||||
{ | |||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | |||||
select_row(i); | |||||
_delay_us(100); // without this wait read unstable value. | |||||
matrix_row_t cols = read_cols(); | |||||
if (matrix_debouncing[i] != cols) { | |||||
matrix_debouncing[i] = cols; | |||||
if (debouncing) { | |||||
debug("bounce!: "); debug_hex(debouncing); debug("\n"); | |||||
} | |||||
debouncing = DEBOUNCE; | |||||
} | |||||
unselect_rows(); | |||||
} | |||||
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++) { | |||||
phex(row); print(": "); | |||||
pbin_reverse16(matrix_get_row(row)); | |||||
print("\n"); | |||||
} | |||||
} | |||||
uint8_t matrix_key_count(void) | |||||
{ | |||||
uint8_t count = 0; | |||||
for (uint8_t i = 0; i < MATRIX_ROWS; i++) { | |||||
count += bitpop16(matrix[i]); | |||||
} | |||||
return count; | |||||
} | |||||
/* Column pin configuration | |||||
* col: 0 1 2 3 4 5 6 7 8 9 10 11 | |||||
* pin: D0 D1 B0 B1 D3 D5 D6 D7 B5 C0 C1 C2 | |||||
*/ | |||||
static void init_cols(void) | |||||
{ | |||||
// Input with pull-up(DDR:0, PORT:1) | |||||
DDRB &= ~(1<<0 | 1<<1 | 1<<5); | |||||
PORTB |= (1<<0 | 1<<1 | 1<<5); | |||||
DDRC &= ~(1<<0 | 1<<1 | 1<<2); | |||||
PORTC |= (1<<0 | 1<<1 | 1<<2); | |||||
DDRD &= ~(1<<0 | 1<<1 | 1<<3 | 1<<5 | 1<<6 | 1<<7); | |||||
PORTD |= (1<<0 | 1<<1 | 1<<3 | 1<<5 | 1<<6 | 1<<7); | |||||
} | |||||
static matrix_row_t read_cols(void) | |||||
{ | |||||
return (PIND&(1<<0) ? 0 : (1<<0)) | | |||||
(PIND&(1<<1) ? 0 : (1<<1)) | | |||||
(PINB&(1<<0) ? 0 : (1<<2)) | | |||||
(PINB&(1<<1) ? 0 : (1<<3)) | | |||||
(PIND&(1<<3) ? 0 : (1<<4)) | | |||||
(PIND&(1<<5) ? 0 : (1<<5)) | | |||||
(PIND&(1<<6) ? 0 : (1<<6)) | | |||||
(PIND&(1<<7) ? 0 : (1<<7)) | | |||||
(PINB&(1<<5) ? 0 : (1<<8)) | | |||||
(PINC&(1<<0) ? 0 : (1<<9)) | | |||||
(PINC&(1<<1) ? 0 : (1<<10)) | | |||||
(PINC&(1<<2) ? 0 : (1<<11)); | |||||
} | |||||
/* Row pin configuration | |||||
* row: 0 1 2 | |||||
* pin: C3 C4 C5 | |||||
*/ | |||||
static void unselect_rows(void) | |||||
{ | |||||
// Hi-Z(DDR:0, PORT:0) to unselect | |||||
DDRC &= ~0b00111000; | |||||
PORTC &= ~0b00111000; | |||||
} | |||||
static void select_row(uint8_t row) | |||||
{ | |||||
// Output low(DDR:1, PORT:0) to select | |||||
switch (row) { | |||||
case 0: | |||||
DDRC |= (1<<3); | |||||
PORTC &= ~(1<<3); | |||||
break; | |||||
case 1: | |||||
DDRC |= (1<<4); | |||||
PORTC &= ~(1<<4); | |||||
break; | |||||
case 2: | |||||
DDRC |= (1<<5); | |||||
PORTC &= ~(1<<5); | |||||
break; | |||||
} | |||||
} |