hardware: Start lifting out an ATMegaKeyboard base class

Both the Atreus and the Planck ports use very similar architecture, lift out the
common parts into a base class.

Signed-off-by: Jesse Vincent <jesse@keyboard.io>
pull/461/head
Jesse Vincent 6 years ago committed by Gergely Nagy
parent a9ced18068
commit 94008c6b3d

@ -0,0 +1,174 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Hardware-ATMegaKeyboard -- Base class for some ATMega-based boards
* Copyright (C) 2018 Keyboard.io, Inc
*
* 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, version 3.
*
* 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/>.
*/
#if ARDUINO_AVR_PLANCK || ARDUINO_AVR_ATREUS
#include "Kaleidoscope.h"
#include "kaleidoscope/hardware/ATMegaKeyboard.h"
namespace kaleidoscope {
namespace hardware {
uint8_t ATMegaKeyboard::debounce = 3;
void ATMegaKeyboard::setup(void) {
wdt_disable();
for (uint8_t i = 0; i < KeyboardHardware.matrix_columns; i++) {
DDR_INPUT(KeyboardHardware.matrix_col_pins[i]);
ENABLE_PULLUP(KeyboardHardware.matrix_col_pins[i]);
}
for (uint8_t i = 0; i < KeyboardHardware.matrix_rows; i++) {
DDR_OUTPUT(KeyboardHardware.matrix_row_pins[i]);
OUTPUT_HIGH(KeyboardHardware.matrix_row_pins[i]);
}
/* Set up Timer1 for 500usec */
TCCR1B = _BV(WGM13);
TCCR1A = 0;
const uint32_t cycles = (F_CPU / 2000000) * 500;
ICR1 = cycles;
TCCR1B = _BV(WGM13) | _BV(CS10);
TIMSK1 = _BV(TOIE1);
}
void ATMegaKeyboard::detachFromHost() {
UDCON |= _BV(DETACH);
}
void ATMegaKeyboard::attachToHost() {
UDCON &= ~_BV(DETACH);
}
void ATMegaKeyboard::readMatrix(void) {
for (uint8_t current_row = 0; current_row < KeyboardHardware.matrix_rows; current_row++) {
uint16_t mask, cols;
KeyboardHardware.previousKeyState_[current_row] = KeyboardHardware.keyState_[current_row];
mask = debounceMaskForRow(current_row);
OUTPUT_TOGGLE(KeyboardHardware.matrix_row_pins[current_row]);
cols = (readCols() & mask) | (KeyboardHardware.keyState_[current_row] & ~mask);
OUTPUT_TOGGLE(KeyboardHardware.matrix_row_pins[current_row]);
debounceRow(cols ^ KeyboardHardware.keyState_[current_row], current_row);
KeyboardHardware.keyState_[current_row] = cols;
}
}
uint8_t ATMegaKeyboard::pressedKeyswitchCount() {
uint8_t count = 0;
for (int8_t r = 0; r < KeyboardHardware.matrix_rows; r++) {
count += __builtin_popcount(KeyboardHardware.keyState_[r]);
}
return count;
}
bool ATMegaKeyboard::isKeyswitchPressed(uint8_t row, byte col) {
return (bitRead(KeyboardHardware.keyState_[row], col) != 0);
}
bool ATMegaKeyboard::isKeyswitchPressed(uint8_t keyIndex) {
keyIndex--;
return isKeyswitchPressed(keyIndex / KeyboardHardware.matrix_columns,
keyIndex % KeyboardHardware.matrix_columns);
}
void ATMegaKeyboard::actOnMatrixScan() {
for (byte row = 0; row < KeyboardHardware.matrix_rows; row++) {
for (byte col = 0; col < KeyboardHardware.matrix_columns; col++) {
uint8_t keyState = (bitRead(KeyboardHardware.previousKeyState_[row], col) << 0) |
(bitRead(KeyboardHardware.keyState_[row], col) << 1);
if (keyState) {
handleKeyswitchEvent(Key_NoKey, row, col, keyState);
}
}
KeyboardHardware.previousKeyState_[row] = KeyboardHardware.keyState_[row];
}
}
void ATMegaKeyboard::scanMatrix() {
if (!do_scan_)
return;
do_scan_ = false;
readMatrix();
actOnMatrixScan();
}
void ATMegaKeyboard::maskKey(byte row, byte col) {
if (row >= KeyboardHardware.matrix_rows || col >= KeyboardHardware.matrix_columns)
return;
bitWrite(KeyboardHardware.masks_[row], col, 1);
}
void ATMegaKeyboard::unMaskKey(byte row, byte col) {
if (row >= KeyboardHardware.matrix_rows || col >= KeyboardHardware.matrix_columns)
return;
bitWrite(KeyboardHardware.masks_[row], col, 0);
}
bool ATMegaKeyboard::isKeyMasked(byte row, byte col) {
if (row >= KeyboardHardware.matrix_rows || col >= KeyboardHardware.matrix_columns)
return false;
return bitRead(KeyboardHardware.masks_[row], col);
}
bool ATMegaKeyboard::do_scan_;
uint16_t ATMegaKeyboard::readCols() {
uint16_t results = 0x00 ;
for (uint8_t i = 0; i < KeyboardHardware.matrix_columns; i++) {
results |= (!READ_PIN(KeyboardHardware.matrix_col_pins[i]) << i);
}
return results;
}
uint16_t ATMegaKeyboard::debounceMaskForRow(uint8_t row) {
uint16_t result = 0;
for (uint16_t c = 0; c < KeyboardHardware.matrix_columns; ++c) {
if (KeyboardHardware.debounce_matrix_[row][c]) {
--KeyboardHardware.debounce_matrix_[row][c];
} else {
result |= _BV(c);
}
}
return result;
}
void ATMegaKeyboard::debounceRow(uint16_t change, uint8_t row) {
for (uint16_t i = 0; i < KeyboardHardware.matrix_columns; ++i) {
if (change & _BV(i)) {
KeyboardHardware.debounce_matrix_[row][i] = debounce;
}
}
}
}
}
#endif

@ -0,0 +1,148 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Hardware-ATMegaKeyboard -- Base class for some ATMega-based boards
* Copyright (C) 2018 Keyboard.io, Inc
*
* 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, version 3.
*
* 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/>.
*/
#pragma once
#if ARDUINO_AVR_PLANCK || ARDUINO_AVR_ATREUS
#include <Arduino.h>
#include <KeyboardioHID.h>
#include "Kaleidoscope-HIDAdaptor-KeyboardioHID.h"
#include "kaleidoscope/macro_helpers.h"
#include "kaleidoscope/key_events.h"
#include "kaleidoscope/hardware/avr/pins_and_ports.h"
#include <avr/wdt.h>
struct cRGB {
uint8_t r, g, b;
};
#define CRGB(r,g,b) (cRGB){b, g, r}
#define ROW_PIN_LIST(...) __VA_ARGS__
#define COL_PIN_LIST(...) __VA_ARGS__
#define ATMEGA_KEYBOARD_CONFIG( ROW_PINS_, COL_PINS_) \
static const int8_t matrix_rows = NUM_ARGS(ROW_PINS_); \
static const int8_t matrix_columns = NUM_ARGS(COL_PINS_); \
static constexpr uint8_t matrix_row_pins[matrix_rows] = ROW_PINS_ ; \
static constexpr uint8_t matrix_col_pins[matrix_columns] = COL_PINS_ ;\
\
static uint16_t previousKeyState_[matrix_rows]; \
static uint16_t keyState_[matrix_rows]; \
static uint16_t masks_[matrix_rows]; \
static uint8_t debounce_matrix_[matrix_rows][matrix_columns];
namespace kaleidoscope {
namespace hardware {
class ATMegaKeyboard {
public:
ATMegaKeyboard(void) {}
// these will be overridden by the subclass
static uint8_t debounce;
void syncLeds(void) {}
void setCrgbAt(uint8_t row, byte col, cRGB color) {}
void setCrgbAt(int8_t i, cRGB crgb) {}
cRGB getCrgbAt(int8_t i) {
return CRGB(0, 0, 0);
}
int8_t getLedIndex(uint8_t row, byte col) {
return -1;
}
void setup(void);
/** Detaching from / attaching to the host.
*
* These two functions should detach the device from (or attach it to) the
* host, preferably without rebooting the device. Their purpose is to allow
* one to do some configuration inbetween, so the re-attach happens with
* different properties. The device remains powered between these operations,
* only the connection to the host gets severed.
*/
void detachFromHost();
void attachToHost();
void readMatrix(void);
/**
* Check the number of key switches currently pressed.
*
* @returns the number of keys pressed.
*/
uint8_t pressedKeyswitchCount();
/** Key switch states
*
* These methods offer a way to peek at the key switch states, for those cases
* where we need to deal with the state closest to the hardware. Some methods
* offer a way to check if a key is pressed, others return the number of
* pressed keys.
*/
/**
* Check if a key is pressed at a given position.
*
* @param row is the row the key is located at in the matrix.
* @param col is the column the key is located at in the matrix.
*
* @returns true if the key is pressed, false otherwise.
*/
bool isKeyswitchPressed(uint8_t row, byte col);
/**
* Check if a key is pressed at a given position.
*
* @param keyIndex is the key index, as calculated by `keyIndex`.
*
* @note Key indexes start at 1, not 0!
*
* @returns true if the key is pressed, false otherwise.
*/
bool isKeyswitchPressed(uint8_t keyIndex);
void actOnMatrixScan();
void scanMatrix();
/* Key masking
* -----------
*
* There are situations when one wants to ignore key events for a while, and
* mask them out. These functions help do that. In isolation, they do nothing,
* plugins and the core firmware is expected to make use of these.
*
* See `handleKeyswitchEvent` in the Kaleidoscope sources for a use-case.
*/
void maskKey(byte row, byte col);
void unMaskKey(byte row, byte col);
bool isKeyMasked(byte row, byte col);
static bool do_scan_;
private:
uint16_t readCols();
uint16_t debounceMaskForRow(uint8_t row);
void debounceRow(uint16_t change, uint8_t row);
};
}
}
#endif

@ -30,139 +30,21 @@
#include <KeyboardioHID.h>
#include <avr/wdt.h>
static bool do_scan_ = true;
ISR(TIMER1_OVF_vect) {
do_scan_ = true;
}
namespace kaleidoscope {
namespace hardware {
uint16_t Atreus::previousKeyState_[ROWS];
uint16_t Atreus::keyState_[ROWS];
uint16_t Atreus::masks_[ROWS];
uint8_t Atreus::debounce_matrix_[ROWS][COLS];
uint8_t Atreus::debounce = 3;
constexpr uint8_t Atreus::row_pins[4];
void Atreus::setup(void) {
wdt_disable();
delay(100);
// Initialize rows
DDRD |= _BV(0) | _BV(1) | _BV(3) | _BV(2);
PORTD |= _BV(0) | _BV(1) | _BV(3) | _BV(2);
// Initialize columns
DDRB &= ~(_BV(5) | _BV(4) | _BV(6) | _BV(7));
PORTB |= (_BV(5) | _BV(4) | _BV(6) | _BV(7));
DDRD &= ~(_BV(7) | _BV(4) | _BV(6));
PORTD |= (_BV(7) | _BV(4) | _BV(6));
DDRC &= ~(_BV(6));
PORTC |= (_BV(6));
DDRE &= ~(_BV(6));
PORTE |= (_BV(6));
DDRF &= ~(_BV(6) | _BV(7));
PORTF |= (_BV(6) | _BV(7));
/* Set up Timer1 for 500usec */
TCCR1B = _BV(WGM13);
TCCR1A = 0;
const uint32_t cycles = (F_CPU / 2000000) * 500;
ICR1 = cycles;
TCCR1B = _BV(WGM13) | _BV(CS10);
TIMSK1 = _BV(TOIE1);
}
void Atreus::toggleRow(uint8_t row) {
PORTD ^= _BV(row_pins[row]);
}
uint16_t Atreus::readCols() {
return ((PINB & (_BV(4)) ? 0 : (_BV(0))) |
(PINB & (_BV(5)) ? 0 : (_BV(1))) |
(PINB & (_BV(6)) ? 0 : (_BV(2))) |
(PINB & (_BV(7)) ? 0 : (_BV(3))) |
(PINC & (_BV(6)) ? 0 : (_BV(4))) |
(PIND & (_BV(4)) ? 0 : (_BV(5))) |
(PIND & (_BV(6)) ? 0 : (_BV(6))) |
(PIND & (_BV(7)) ? 0 : (_BV(7))) |
(PINE & (_BV(6)) ? 0 : (_BV(8))) |
(PINF & (_BV(6)) ? 0 : (_BV(9))) |
(PINF & (_BV(7)) ? 0 : (_BV(10))));
}
void Atreus::readMatrixRow(uint8_t current_row) {
uint16_t mask, cols;
previousKeyState_[current_row] = keyState_[current_row];
mask = debounceMaskForRow(current_row);
toggleRow(current_row);
cols = (readCols() & mask) | (keyState_[current_row] & ~mask);
toggleRow(current_row);
debounceRow(cols ^ keyState_[current_row], current_row);
keyState_[current_row] = cols;
}
void Atreus::readMatrix() {
do_scan_ = false;
for (uint8_t current_row = 0; current_row < ROWS; current_row++) {
readMatrixRow(current_row);
}
}
void Atreus::actOnMatrixScan() {
for (byte row = 0; row < ROWS; row++) {
for (byte col = 0; col < COLS; col++) {
uint8_t keyState = (bitRead(previousKeyState_[row], col) << 0) |
(bitRead(keyState_[row], col) << 1);
if (keyState) {
handleKeyswitchEvent(Key_NoKey, row, col, keyState);
}
}
previousKeyState_[row] = keyState_[row];
}
}
void Atreus::scanMatrix() {
if (!do_scan_)
return;
readMatrix();
actOnMatrixScan();
}
void Atreus::maskKey(byte row, byte col) {
if (row >= ROWS || col >= COLS)
return;
bitWrite(masks_[row], col, 1);
}
const int8_t Atreus::matrix_rows;
const int8_t Atreus::matrix_columns;
constexpr uint8_t Atreus::matrix_row_pins[matrix_rows];
constexpr uint8_t Atreus::matrix_col_pins[matrix_columns];
constexpr int8_t Atreus::led_count;
uint16_t Atreus::previousKeyState_[matrix_rows];
uint16_t Atreus::keyState_[matrix_rows];
uint16_t Atreus::masks_[matrix_rows];
uint8_t Atreus::debounce_matrix_[matrix_rows][matrix_columns];
void Atreus::unMaskKey(byte row, byte col) {
if (row >= ROWS || col >= COLS)
return;
bitWrite(masks_[row], col, 0);
}
bool Atreus::isKeyMasked(byte row, byte col) {
if (row >= ROWS || col >= COLS)
return false;
return bitRead(masks_[row], col);
ISR(TIMER1_OVF_vect) {
Atreus::do_scan_ = true;
}
// Atreus-specific stuff
@ -200,53 +82,6 @@ void Atreus::resetDevice() {
asm volatile("jmp 0x7E00");
}
void Atreus::detachFromHost() {
UDCON |= (1 << DETACH);
}
void Atreus::attachToHost() {
UDCON &= ~(1 << DETACH);
}
bool Atreus::isKeyswitchPressed(byte row, byte col) {
return (bitRead(keyState_[row], col) != 0);
}
bool Atreus::isKeyswitchPressed(uint8_t keyIndex) {
keyIndex--;
return isKeyswitchPressed(keyIndex / COLS, keyIndex % COLS);
}
uint8_t Atreus::pressedKeyswitchCount() {
uint8_t count = 0;
for (uint8_t r = 0; r < ROWS; r++) {
count += __builtin_popcount(keyState_[r]);
}
return count;
}
uint16_t Atreus::debounceMaskForRow(uint8_t row) {
uint16_t result = 0;
for (uint16_t c = 0; c < COLS; ++c) {
if (debounce_matrix_[row][c]) {
--debounce_matrix_[row][c];
} else {
result |= (1 << c);
}
}
return result;
}
void Atreus::debounceRow(uint16_t change, uint8_t row) {
for (uint16_t i = 0; i < COLS; ++i) {
if (change & (1 << i)) {
debounce_matrix_[row][i] = debounce;
}
}
}
}
}

@ -27,119 +27,27 @@
#ifdef ARDUINO_AVR_ATREUS
#include <Arduino.h>
#define HARDWARE_IMPLEMENTATION kaleidoscope::hardware::Atreus
#include "Kaleidoscope-HIDAdaptor-KeyboardioHID.h"
#include "kaleidoscope/macro_helpers.h"
struct cRGB {
uint8_t r, g, b;
};
#define CRGB(r,g,b) (cRGB){b, g, r}
#include "kaleidoscope/hardware/ATMegaKeyboard.h"
namespace kaleidoscope {
namespace hardware {
class Atreus {
class Atreus: public kaleidoscope::hardware::ATMegaKeyboard {
public:
Atreus(void) {}
static constexpr byte matrix_columns = 11;
static constexpr byte matrix_rows = 4;
static constexpr int8_t led_count = 0;
void syncLeds(void) {}
void setCrgbAt(byte row, byte col, cRGB color) {}
void setCrgbAt(int8_t i, cRGB crgb) {}
cRGB getCrgbAt(int8_t i) {
return CRGB(0, 0, 0);
}
int8_t getLedIndex(byte row, byte col) {
return -1;
}
void scanMatrix(void);
void readMatrix(void);
void actOnMatrixScan(void);
void setup();
ATMEGA_KEYBOARD_CONFIG(
ROW_PIN_LIST({PIN_D0, PIN_D1, PIN_D3, PIN_D2}),
COL_PIN_LIST({PIN_B4, PIN_B5, PIN_B6, PIN_B7, PIN_C6, PIN_D4, PIN_D6, PIN_D7, PIN_E6, PIN_F6, PIN_F7})
);
/** Detaching from / attaching to the host.
*
* These two functions should detach the device from (or attach it to) the
* host, preferably without rebooting the device. Their purpose is to allow
* one to do some configuration inbetween, so the re-attach happens with
* different properties. The device remains powered between these operations,
* only the connection to the host gets severed.
*/
void detachFromHost();
void attachToHost();
/* Key masking
* -----------
*
* There are situations when one wants to ignore key events for a while, and
* mask them out. These functions help do that. In isolation, they do nothing,
* plugins and the core firmware is expected to make use of these.
*
* See `handleKeyswitchEvent` in the Kaleidoscope sources for a use-case.
*/
void maskKey(byte row, byte col);
void unMaskKey(byte row, byte col);
bool isKeyMasked(byte row, byte col);
/** Key switch states
*
* These methods offer a way to peek at the key switch states, for those cases
* where we need to deal with the state closest to the hardware. Some methods
* offer a way to check if a key is pressed, others return the number of
* pressed keys.
*/
/**
* Check if a key is pressed at a given position.
*
* @param row is the row the key is located at in the matrix.
* @param col is the column the key is located at in the matrix.
*
* @returns true if the key is pressed, false otherwise.
*/
bool isKeyswitchPressed(byte row, byte col);
/**
* Check if a key is pressed at a given position.
*
* @param keyIndex is the key index, as calculated by `keyIndex`.
*
* @note Key indexes start at 1, not 0!
*
* @returns true if the key is pressed, false otherwise.
*/
bool isKeyswitchPressed(uint8_t keyIndex);
/**
* Check the number of key switches currently pressed.
*
* @returns the number of keys pressed.
*/
uint8_t pressedKeyswitchCount();
static constexpr int8_t led_count = 0;
void resetDevice();
static uint8_t debounce;
private:
static constexpr uint8_t row_pins[4] = {0, 1, 3, 2};
static uint16_t previousKeyState_[matrix_rows];
static uint16_t keyState_[matrix_rows];
static uint16_t masks_[matrix_rows];
static void readMatrixRow(uint8_t row);
static uint16_t readCols();
static void toggleRow(uint8_t row);
static uint8_t debounce_matrix_[matrix_rows][matrix_columns];
static uint16_t debounceMaskForRow(uint8_t row);
static void debounceRow(uint16_t change, uint8_t row);
};
#define KEYMAP( \

@ -1,5 +1,5 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Hardware-Planck -- OLKB Planck hardware support for Kaleidoscope
* Kaleidoscope-Hardware-OLKB-Planck -- Planck hardware support for Kaleidoscope
* Copyright (C) 2018 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify
@ -18,170 +18,25 @@
#ifdef ARDUINO_AVR_PLANCK
#include <Kaleidoscope.h>
#include <KeyboardioHID.h>
#include <avr/wdt.h>
static bool do_scan_ = true;
ISR(TIMER1_OVF_vect) {
do_scan_ = true;
}
namespace kaleidoscope {
namespace hardware {
namespace olkb {
const int8_t Planck::matrix_rows;
const int8_t Planck::matrix_columns;
constexpr uint8_t Planck::matrix_row_pins[matrix_rows];
constexpr uint8_t Planck::matrix_col_pins[matrix_columns];
constexpr int8_t Planck::led_count;
uint16_t Planck::previousKeyState_[matrix_rows];
uint16_t Planck::keyState_[matrix_rows];
uint16_t Planck::masks_[matrix_rows];
uint8_t Planck::debounce_matrix_[matrix_rows][matrix_columns];
void Planck::setup(void) {
wdt_disable();
for (uint8_t i = 0; i < sizeof(matrix_col_pins); i++) {
DDR_INPUT(matrix_col_pins[i]);
ENABLE_PULLUP(matrix_col_pins[i]);
}
for (uint8_t i = 0; i < sizeof(matrix_row_pins); i++) {
DDR_OUTPUT(matrix_row_pins[i]);
OUTPUT_HIGH(matrix_row_pins[i]);
}
/* Set up Timer1 for 500usec */
TCCR1B = _BV(WGM13);
TCCR1A = 0;
const uint32_t cycles = (F_CPU / 2000000) * 500;
ICR1 = cycles;
TCCR1B = _BV(WGM13) | _BV(CS10);
TIMSK1 = _BV(TOIE1);
}
uint16_t Planck::readCols() {
uint16_t results = 0x00 ;
for (uint8_t i = 0; i < matrix_columns; i++) {
results |= (!READ_PIN(matrix_col_pins[i]) << i);
}
return results;
}
void Planck::readMatrix() {
for (uint8_t current_row = 0; current_row < sizeof(matrix_row_pins); current_row++) {
uint16_t mask, cols;
previousKeyState_[current_row] = keyState_[current_row];
mask = debounceMaskForRow(current_row);
OUTPUT_TOGGLE(matrix_row_pins[current_row]);
cols = (readCols() & mask) | (keyState_[current_row] & ~mask);
OUTPUT_TOGGLE(matrix_row_pins[current_row]);
debounceRow(cols ^ keyState_[current_row], current_row);
keyState_[current_row] = cols;
}
}
void Planck::actOnMatrixScan() {
for (byte row = 0; row < matrix_rows; row++) {
for (byte col = 0; col < matrix_columns; col++) {
uint8_t keyState = (bitRead(previousKeyState_[row], col) << 0) |
(bitRead(keyState_[row], col) << 1);
if (keyState) {
handleKeyswitchEvent(Key_NoKey, row, col, keyState);
}
}
previousKeyState_[row] = keyState_[row];
}
}
void Planck::scanMatrix() {
if (!do_scan_)
return;
do_scan_ = false;
readMatrix();
actOnMatrixScan();
}
void Planck::maskKey(byte row, byte col) {
if (row >= matrix_rows || col >= matrix_columns)
return;
bitWrite(masks_[row], col, 1);
}
void Planck::unMaskKey(byte row, byte col) {
if (row >= matrix_rows || col >= matrix_columns)
return;
bitWrite(masks_[row], col, 0);
}
bool Planck::isKeyMasked(byte row, byte col) {
if (row >= matrix_rows || col >= matrix_columns)
return false;
return bitRead(masks_[row], col);
}
void Planck::detachFromHost() {
UDCON |= _BV(DETACH);
}
void Planck::attachToHost() {
UDCON &= ~_BV(DETACH);
}
bool Planck::isKeyswitchPressed(byte row, byte col) {
return (bitRead(keyState_[row], col) != 0);
}
bool Planck::isKeyswitchPressed(uint8_t keyIndex) {
keyIndex--;
return isKeyswitchPressed(keyIndex / matrix_columns, keyIndex % matrix_columns);
}
uint8_t Planck::pressedKeyswitchCount() {
uint8_t count = 0;
for (uint8_t r = 0; r < matrix_rows; r++) {
count += __builtin_popcount(keyState_[r]);
}
return count;
}
uint16_t Planck::debounceMaskForRow(uint8_t row) {
uint16_t result = 0;
for (uint16_t c = 0; c < matrix_columns; ++c) {
if (debounce_matrix_[row][c]) {
--debounce_matrix_[row][c];
} else {
result |= _BV(c);
}
}
return result;
}
void Planck::debounceRow(uint16_t change, uint8_t row) {
for (uint16_t i = 0; i < matrix_columns; ++i) {
if (change & _BV(i)) {
debounce_matrix_[row][i] = debounce;
}
}
ISR(TIMER1_OVF_vect) {
Planck::do_scan_ = true;
}
}
}
}

@ -1,5 +1,5 @@
/* -*- mode: c++ -*-
* Kaleidoscope-Hardware-Planck -- OLKB Planck hardware support for Kaleidoscope
* Kaleidoscope-Hardware-OLKB-Planck -- Planck hardware support for Kaleidoscope
* Copyright (C) 2018 Keyboard.io, Inc
*
* This program is free software: you can redistribute it and/or modify
@ -26,126 +26,36 @@
#include "kaleidoscope/macro_helpers.h"
#include "kaleidoscope/hardware/avr/pins_and_ports.h"
struct cRGB {
uint8_t r, g, b;
};
#define CRGB(r,g,b) (cRGB){b, g, r}
#include "kaleidoscope/hardware/ATMegaKeyboard.h"
namespace kaleidoscope {
namespace hardware {
namespace olkb {
class Planck {
class Planck: public kaleidoscope::hardware::ATMegaKeyboard {
public:
Planck(void) {}
static const uint8_t matrix_rows = 4;
static const uint8_t matrix_columns = 12;
static constexpr uint8_t matrix_row_pins[matrix_rows] = { PIN_D0, PIN_D5, PIN_B5, PIN_B6 };
static constexpr uint8_t matrix_col_pins[matrix_columns] = { PIN_F1, PIN_F0, PIN_B0, PIN_C7, PIN_F4, PIN_F5, PIN_F6, PIN_F7, PIN_D4, PIN_D6, PIN_B4, PIN_D7 };
static constexpr int8_t led_count = 0;
static const uint8_t debounce = 3;
void syncLeds(void) {}
void setCrgbAt(uint8_t row, byte col, cRGB color) {}
void setCrgbAt(int8_t i, cRGB crgb) {}
cRGB getCrgbAt(int8_t i) {
return CRGB(0, 0, 0);
}
int8_t getLedIndex(uint8_t row, byte col) {
return -1;
}
void scanMatrix(void);
void readMatrix(void);
void actOnMatrixScan(void);
void setup();
/** Detaching from / attaching to the host.
*
* These two functions should detach the device from (or attach it to) the
* host, preferably without rebooting the device. Their purpose is to allow
* one to do some configuration inbetween, so the re-attach happens with
* different properties. The device remains powered between these operations,
* only the connection to the host gets severed.
*/
void detachFromHost();
void attachToHost();
/* Key masking
* -----------
*
* There are situations when one wants to ignore key events for a while, and
* mask them out. These functions help do that. In isolation, they do nothing,
* plugins and the core firmware is expected to make use of these.
*
* See `handleKeyswitchEvent` in the Kaleidoscope sources for a use-case.
*/
void maskKey(uint8_t row, byte col);
void unMaskKey(uint8_t row, byte col);
bool isKeyMasked(uint8_t row, byte col);
ATMEGA_KEYBOARD_CONFIG(
ROW_PIN_LIST({ PIN_D0, PIN_D5, PIN_B5, PIN_B6 }),
COL_PIN_LIST({ PIN_F1, PIN_F0, PIN_B0, PIN_C7, PIN_F4, PIN_F5, PIN_F6, PIN_F7, PIN_D4, PIN_D6, PIN_B4, PIN_D7 })
);
/** Key switch states
*
* These methods offer a way to peek at the key switch states, for those cases
* where we need to deal with the state closest to the hardware. Some methods
* offer a way to check if a key is pressed, others return the number of
* pressed keys.
*/
/**
* Check if a key is pressed at a given position.
*
* @param row is the row the key is located at in the matrix.
* @param col is the column the key is located at in the matrix.
*
* @returns true if the key is pressed, false otherwise.
*/
bool isKeyswitchPressed(uint8_t row, byte col);
/**
* Check if a key is pressed at a given position.
*
* @param keyIndex is the key index, as calculated by `keyIndex`.
*
* @note Key indexes start at 1, not 0!
*
* @returns true if the key is pressed, false otherwise.
*/
bool isKeyswitchPressed(uint8_t keyIndex);
/**
* Check the number of key switches currently pressed.
*
* @returns the number of keys pressed.
*/
uint8_t pressedKeyswitchCount();
private:
static uint16_t previousKeyState_[matrix_rows];
static uint16_t keyState_[matrix_rows];
static uint16_t masks_[matrix_rows];
static uint8_t debounce_matrix_[matrix_rows][matrix_columns];
static uint16_t readCols();
static uint16_t debounceMaskForRow(uint8_t row);
static void debounceRow(uint16_t change, uint8_t row);
static constexpr int8_t led_count = 0;
};
#define KEYMAP( \
R0C0, R0C1, R0C2, R0C3, R0C4, R0C5, R0C6, R0C7, R0C8, R0C9, R0C10, R0C11, \
R1C0, R1C1, R1C2, R1C3, R1C4, R1C5, R1C6, R1C7, R1C8, R1C9, R1C10, R1C11, \
R2C0, R2C1, R2C2, R2C3, R2C4, R2C5, R2C6, R2C7, R2C8, R2C9, R2C10, R2C11, \
R3C0, R3C1, R3C2, R3C3, R3C4, R3C5, R3C6, R3C7, R3C8, R3C9, R3C10, R3C11 \
R0C0, R0C1, R0C2, R0C3, R0C4, R0C5, R0C6, R0C7, R0C8, R0C9, R0C10, R0C11, \
R1C0, R1C1, R1C2, R1C3, R1C4, R1C5, R1C6, R1C7, R1C8, R1C9, R1C10, R1C11, \
R2C0, R2C1, R2C2, R2C3, R2C4, R2C5, R2C6, R2C7, R2C8, R2C9, R2C10, R2C11, \
R3C0, R3C1, R3C2, R3C3, R3C4, R3C5, R3C6, R3C7, R3C8, R3C9, R3C10, R3C11 \
) { \
{ R0C0, R0C1, R0C2, R0C3, R0C4, R0C5, R0C6, R0C7, R0C8, R0C9, R0C10, R0C11, }, \
{ R1C0, R1C1, R1C2, R1C3, R1C4, R1C5, R1C6, R1C7, R1C8, R1C9, R1C10, R1C11, }, \
{ R2C0, R2C1, R2C2, R2C3, R2C4, R2C5, R2C6, R2C7, R2C8, R2C9, R2C10, R2C11, }, \
{ R3C0, R3C1, R3C2, R3C3, R3C4, R3C5, R3C6, R3C7, R3C8, R3C9, R3C10, R3C11, } \
{ R0C0, R0C1, R0C2, R0C3, R0C4, R0C5, R0C6, R0C7, R0C8, R0C9, R0C10, R0C11, }, \
{ R1C0, R1C1, R1C2, R1C3, R1C4, R1C5, R1C6, R1C7, R1C8, R1C9, R1C10, R1C11, }, \
{ R2C0, R2C1, R2C2, R2C3, R2C4, R2C5, R2C6, R2C7, R2C8, R2C9, R2C10, R2C11, }, \
{ R3C0, R3C1, R3C2, R3C3, R3C4, R3C5, R3C6, R3C7, R3C8, R3C9, R3C10, R3C11, } \
}
}
}
}
@ -160,12 +70,6 @@ class Planck {
constexpr uint8_t keyIndex(byte row, byte col) {
return (row * kaleidoscope::hardware::olkb::Planck::matrix_columns) + col + 1;
}
/*
This oneliner will generate these constexprs:
perl -e'for($i=0;$i<6;$i++) { for ($j=0; $j<16;$j++) { print "constexpr uint8_t R${i}C${j} = keyIndex($i, $j);\n"}};'
*/
#include "kaleidoscope/hardware/key_indexes.h"

@ -135,3 +135,6 @@ int array[] = { A, B, RESTRICT_ARGS_COUNT(C, 3, B_MACRO, ##__VA_ARGS__) };
}){}, /* End of dummy lambda, the comma operator's A operand. */ __NL__ \
B /* The overall ASSERT_ARGS_COUNT evaluates to B. */ __NL__ \
)
/* Count the args in a list */
#define NUM_ARGS(...) (sizeof((int[])__VA_ARGS__)/sizeof(int))

Loading…
Cancel
Save