Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optical keyboard #17852

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from 18 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
152 changes: 152 additions & 0 deletions keyboards/opticalkb/rev1/config.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
// Copyright 2022 Girish Palya (@girishji)
// SPDX-License-Identifier: GPL-2.0-or-later

#pragma once

#include "config_common.h"

/* key matrix size */
#define MATRIX_ROWS 5
#define MATRIX_COLS 15

/*
* Keyboard Matrix Assignments
*
* Change this to how you wired your keyboard
* COLS: pins used for columns, left to right
* ROWS: pins used for rows, top to bottom
*
*/

// Using blackpill STM32F401
#define MATRIX_ROW_PINS \
{ B10, B1, B13, B14, B15 }
#define MATRIX_COL_PINS \
{ B0, A7, A6, A5, A4, A3, A2, A1, B3, B4, B5, B12, B9, A8, A15 }

/* PT requires time to 'rise' after IR has been powered on. Read the GPIO input
* pin only after PT voltage has stabilized. */
#define WAIT_AFTER_COL_SELECT 76
/* PT requires time to 'fall' after IR has been switched off. Wait until
* voltage across PT has stabilized. Otherwise scanning of next column will be
* affected. */
#define WAIT_AFTER_COL_UNSELECT 110

/* turn off after debugging */
// #define DEBUG_MATRIX_SCAN_RATE

// #define LED_DRIVER_COUNT 1
// #define DRIVER_LED_TOTAL 74
// #define LED_DRIVER_ADDR_1 0b1110100

/* 255 is maximum intensity */
// #define LED_DISABLE_WHEN_USB_SUSPENDED
// limits maximum brightness of LEDs (max 256)
// #define LED_MATRIX_MAXIMUM_BRIGHTNESS 90
// limits in milliseconds how frequently an animation will update the LEDs. 16
// (16ms) is equivalent to limiting to 60fps (increases keyboard
// responsiveness)
// At 16ms matrix scan frequency redues from 1000 to 350, at 100ms it is 880 and
// at 1000ms it is 980
// #define LED_MATRIX_LED_FLUSH_LIMIT 100


/*
* Split Keyboard specific options, make sure you have 'SPLIT_KEYBOARD = yes' in your rules.mk, and define SOFT_SERIAL_PIN.
*/
//#define SOFT_SERIAL_PIN D0 // or D1, D2, D3, E6

//#define LED_NUM_LOCK_PIN B0
//#define LED_CAPS_LOCK_PIN B9
//#define LED_SCROLL_LOCK_PIN B2
//#define LED_COMPOSE_PIN B3
//#define LED_KANA_PIN B4

//#define BACKLIGHT_PIN B7
//#define BACKLIGHT_LEVELS 3
//#define BACKLIGHT_BREATHING

// #define RGB_DI_PIN F0
// #define RGBLIGHT_LAYERS
// #define RGBLIGHT_LAYERS_OVERRIDE_RGB_OFF
// #ifdef RGB_DI_PIN
// # define RGBLED_NUM 16
// # define RGBLIGHT_HUE_STEP 8
// # define RGBLIGHT_SAT_STEP 8
// # define RGBLIGHT_VAL_STEP 8
// # define RGBLIGHT_LIMIT_VAL 255 /* The maximum brightness level */
// # define RGBLIGHT_SLEEP /* If defined, the RGB lighting will be switched off when the host goes to sleep */
/*== or choose animations ==*/
// # define RGBLIGHT_EFFECT_BREATHING
// # define RGBLIGHT_EFFECT_RAINBOW_MOOD
// # define RGBLIGHT_EFFECT_RAINBOW_SWIRL
// # define RGBLIGHT_EFFECT_SNAKE
// # define RGBLIGHT_EFFECT_KNIGHT
// # define RGBLIGHT_EFFECT_CHRISTMAS
// # define RGBLIGHT_EFFECT_STATIC_GRADIENT
// # define RGBLIGHT_EFFECT_RGB_TEST
// # define RGBLIGHT_EFFECT_ALTERNATING
/*== customize breathing effect ==*/
/*==== (DEFAULT) use fixed table instead of exp() and sin() ====*/
// # define RGBLIGHT_BREATHE_TABLE_SIZE 256 // 256(default) or 128 or 64
/*==== use exp() and sin() ====*/
// # define RGBLIGHT_EFFECT_BREATHE_CENTER 1.85 // 1 to 2.7
// # define RGBLIGHT_EFFECT_BREATHE_MAX 255 // 0 to 255
// #endif

/* Debounce reduces chatter (unintended double-presses) - set 0 if debouncing is not needed */
// #define DEBOUNCE 5

/* define if matrix has ghost (lacks anti-ghosting diodes) */
//#define MATRIX_HAS_GHOST

/* 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

/* If defined, GRAVE_ESC will always act as ESC when CTRL is held.
* This is useful for the Windows task manager shortcut (ctrl+shift+esc).
*/
//#define GRAVE_ESC_CTRL_OVERRIDE

/*
* Force NKRO
*
* Force NKRO (nKey Rollover) to be enabled by default, regardless of the saved
* state in the bootmagic EEPROM settings. (Note that NKRO must be enabled in the
* makefile for this to work.)
*
* If forced on, NKRO can be disabled via magic key (default = LShift+RShift+N)
* until the next keyboard reset.
*
* NKRO may prevent your keystrokes from being detected in the BIOS, but it is
* fully operational during normal computer usage.
*
* For a less heavy-handed approach, enable NKRO via magic key (LShift+RShift+N)
* or via bootmagic (hold SPACE+N while plugging in the keyboard). Once set by
* bootmagic, NKRO mode will always be enabled until it is toggled again during a
* power-up.
*
*/
//#define FORCE_NKRO

/*
* 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

/* Bootmagic Lite key configuration */
//#define BOOTMAGIC_LITE_ROW 0
//#define BOOTMAGIC_LITE_COLUMN 0
23 changes: 23 additions & 0 deletions keyboards/opticalkb/rev1/info.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
{
"keyboard_name": "opticalkb",
"url": "https://github.com/girishji/optical-keyboard-mx",
"maintainer": "girish",
"manufacturer": "girish",
"debounce": 0,
"usb": {
"vid": "0xFEFD",
"pid": "0x5302",
"device_version": "1.0.0"
},
"layouts": {
"LAYOUT": {
"layout": [
{"label":"K000", "x":0.5, "y":0}, {"label":"K001", "x":1.5, "y":0}, {"label":"K002", "x":2.5, "y":0}, {"label":"K003", "x":3.5, "y":0}, {"label":"K004", "x":4.5, "y":0}, {"label":"K005", "x":5.5, "y":0}, {"label":"K006", "x":6.5, "y":0}, {"label":"K007", "x":7.5, "y":0}, {"label":"K008", "x":8.5, "y":0}, {"label":"K009", "x":9.5, "y":0}, {"label":"K010", "x":10.5, "y":0}, {"label":"K011", "x":11.5, "y":0}, {"label":"K012", "x":12.5, "y":0}, {"label":"K013", "x":13.5, "y":0}, {"label":"K014", "x":14.5, "y":0},
{"label":"K100", "x":0.5, "y":1, "w":1.5}, {"label":"K101", "x":2, "y":1}, {"label":"K102", "x":3, "y":1}, {"label":"K103", "x":4, "y":1}, {"label":"K104", "x":5, "y":1}, {"label":"K105", "x":6, "y":1}, {"label":"K106", "x":7, "y":1}, {"label":"K107", "x":8, "y":1}, {"label":"K108", "x":9, "y":1}, {"label":"K109", "x":10, "y":1}, {"label":"K110", "x":11, "y":1}, {"label":"K111", "x":12, "y":1}, {"label":"K112", "x":13, "y":1}, {"label":"K113", "x":14, "y":1, "w":1.5},
{"label":"K200", "x":0, "y":2}, {"label":"K201", "x":1, "y":2, "w":1.25}, {"label":"K202", "x":2.25, "y":2}, {"label":"K203", "x":3.25, "y":2}, {"label":"K204", "x":4.25, "y":2}, {"label":"K205", "x":5.25, "y":2}, {"label":"K206", "x":6.25, "y":2}, {"label":"K207", "x":7.25, "y":2}, {"label":"K208", "x":8.25, "y":2}, {"label":"K209", "x":9.25, "y":2}, {"label":"K210", "x":10.25, "y":2}, {"label":"K211", "x":11.25, "y":2}, {"label":"K212", "x":12.25, "y":2}, {"label":"K213", "x":13.25, "y":2, "w":1.5}, {"label":"K214", "x":14.75, "y":2, "w":1.25},
{"label":"K300", "x":0, "y":3, "w":1.75}, {"label":"K301", "x":1.75, "y":3}, {"label":"K302", "x":2.75, "y":3}, {"label":"K303", "x":3.75, "y":3}, {"label":"K304", "x":4.75, "y":3}, {"label":"K305", "x":5.75, "y":3}, {"label":"K306", "x":6.75, "y":3}, {"label":"K307", "x":7.75, "y":3}, {"label":"K308", "x":8.75, "y":3}, {"label":"K309", "x":9.75, "y":3}, {"label":"K310", "x":10.75, "y":3}, {"label":"K311", "x":11.75, "y":3}, {"label":"K312", "x":12.75, "y":3, "w":1.25}, {"label":"K313", "x":14, "y":3}, {"label":"K314", "x":15, "y":3},
{"label":"K400", "x":0, "y":4}, {"label":"K401", "x":1, "y":4}, {"label":"K402", "x":2, "y":4}, {"label":"K403", "x":3, "y":4, "w":1.25}, {"label":"K404", "x":4.25, "y":4, "w":1.25}, {"label":"K405", "x":5.5, "y":4.5, "h":1.5}, {"label":"K406", "x":6.75, "y":4}, {"label":"K407", "x":9, "y":4, "h":1.5}, {"label":"K408", "x":9, "y":4}, {"label":"K409", "x":10, "y":4}, {"label":"K410", "x":11, "y":4}, {"label":"K411", "x":12, "y":4}, {"label":"K412", "x":13, "y":4}, {"label":"K413", "x":14, "y":4}, {"label":"K414", "x":15, "y":4}
]
}
}
}
25 changes: 25 additions & 0 deletions keyboards/opticalkb/rev1/keymaps/default/keymap.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// Copyright 2022 Girish Palya (@girishji)
// SPDX-License-Identifier: GPL-2.0-or-later

#include QMK_KEYBOARD_H

// Defines names for use in layer keycodes and the keymap
enum layer_names { _BASE, _FN };

const uint16_t PROGMEM keymaps[][MATRIX_ROWS][MATRIX_COLS] = {
/* Base */
[_BASE] = LAYOUT(
KC_GRV, KC_1, KC_2, KC_3, KC_4, KC_5, KC_6, KC_7, KC_8, KC_9, KC_0, KC_MINS, KC_EQL, LGUI(KC_LEFT), LGUI(KC_RIGHT),
KC_TAB, KC_Q, KC_W, KC_E, KC_R, KC_T, KC_Y, KC_U, KC_I, KC_O, KC_P, KC_LBRC, KC_RBRC, KC_BSLS,
OSM(MOD_LCTL), KC_ESC, KC_A, KC_S, KC_D, KC_F, KC_G, KC_H, KC_J, KC_K, KC_L, KC_SCLN, KC_QUOT, KC_ENT, KC_RIGHT,
OSM(MOD_LSFT), KC_GRV, KC_Z, KC_X, KC_C, KC_V, KC_B, KC_N, KC_M, KC_COMM, KC_DOT, KC_SLSH, OSM(MOD_RSFT), KC_UP, KC_CAPS,
OSM(MOD_LALT), KC_BTN1, KC_BTN3, OSL(_FN), OSM(MOD_LGUI), KC_BSPC, OSM(MOD_RALT), KC_SPC, KC_PGDN, KC_PGUP, KC_HOME, KC_END, KC_LEFT, KC_DOWN, KC_RIGHT
),
[_FN] = LAYOUT(
_______, KC_F1, KC_F2, KC_F3, KC_F4, KC_F5, KC_F6, KC_F7, KC_F8, KC_F9, KC_F10, KC_F11, KC_F12, KC_HOME, KC_END,
_______, DYN_REC_STOP, _______, _______, _______, _______, _______, _______, _______, _______, DYN_MACRO_PLAY1, _______, _______, _______,
_______, _______, _______, DYN_REC_START1, _______, _______, _______, LCMD(KC_LEFT), KC_PGDN, KC_PGUP, LCMD(KC_RIGHT), _______, _______, _______, _______,
_______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, _______, KC_WH_U, _______,
RESET, KC_BTN2, _______, _______, _______, _______, _______, _______, KC_END, KC_HOME, _______, DYN_MACRO_PLAY1, _______, KC_WH_D, _______
girishji marked this conversation as resolved.
Show resolved Hide resolved
)};

1 change: 1 addition & 0 deletions keyboards/opticalkb/rev1/keymaps/default/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# The default keymap
136 changes: 136 additions & 0 deletions keyboards/opticalkb/rev1/matrix.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,136 @@
/*
Copyright 2022 Girish Palya <girishji@gmail.com>

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
* https://github.com/qmk/qmk_firmware/blob/master/docs/custom_matrix.md
*/

#include "quantum.h"
#include "wait.h"
#include "print.h"

// Optical keyboard does not need debounce
#define DEBOUNCE 0

#ifndef WAIT_AFTER_COL_SELECT
#define WAIT_AFTER_COL_SELECT 100
#endif

#ifdef MATRIX_ROW_PINS
static const pin_t row_pins[MATRIX_ROWS] = MATRIX_ROW_PINS;
#endif
#ifdef MATRIX_COL_PINS
static const pin_t col_pins[MATRIX_COLS] = MATRIX_COL_PINS;
#endif

void matrix_init_custom(void) {
// initialize column pins to be Output and set them Low
for (uint8_t col = 0; col < MATRIX_COLS; col++) {
pin_t pin = col_pins[col];
if (pin != NO_PIN) {
ATOMIC_BLOCK_FORCEON {
setPinOutput(pin);
writePinLow(pin);
}
}
}
// initialize row pins to be Input
for (uint8_t row = 0; row < MATRIX_ROWS; row++) {
pin_t pin = row_pins[row];
if (pin != NO_PIN) {
ATOMIC_BLOCK_FORCEON {
#ifdef PT_INPUT_USE_PULLUP
setPinInputHigh(pin);
#else
setPinInput(pin);
#endif
}
}
}
}

static bool select_col(uint8_t col) {
pin_t pin = col_pins[col];
if (pin != NO_PIN) {
ATOMIC_BLOCK_FORCEON {
writePinHigh(pin);
}
return true;
}
return false;
}

static bool unselect_col(uint8_t col) {
pin_t pin = col_pins[col];
if (pin != NO_PIN) {
ATOMIC_BLOCK_FORCEON {
writePinLow(pin);
}
return true;
}
return false;
}

__attribute__((weak)) void matrix_read_rows_on_col(matrix_row_t current_matrix[], uint8_t current_col, matrix_row_t row_shifter) {

if (!select_col(current_col)) {
dprintf("Error: failed to power on col %d\n", current_col);
return;
}

wait_us(WAIT_AFTER_COL_SELECT);

// For each row...
for (uint8_t row_index = 0; row_index < MATRIX_ROWS; row_index++) {
if (row_pins[row_index] == NO_PIN) {
continue;
}
// Check row pin state
if (readPin(row_pins[row_index]) == 1) {
// Pin HI, set col bit
current_matrix[row_index] |= row_shifter;
} else {
// Pin LO, clear col bit
current_matrix[row_index] &= ~row_shifter;
}
}

unselect_col(current_col);

#ifndef WAIT_AFTER_COL_UNSELECT
waitInputPinDelay();
#else
wait_us(WAIT_AFTER_COL_UNSELECT);
#endif
}

uint8_t matrix_scan_custom(matrix_row_t current_matrix[]) {
matrix_row_t curr_matrix[MATRIX_ROWS] = {0};

// Set col, read rows
matrix_row_t row_shifter = MATRIX_ROW_SHIFTER;
for (uint8_t current_col = 0; current_col < MATRIX_COLS; current_col++, row_shifter <<= 1) {
matrix_read_rows_on_col(curr_matrix, current_col, row_shifter);
}
bool changed = memcmp(current_matrix, curr_matrix, sizeof(curr_matrix)) != 0;
if (changed) {
memcpy(current_matrix, curr_matrix, sizeof(curr_matrix));
}

return (uint8_t)changed;
}
51 changes: 51 additions & 0 deletions keyboards/opticalkb/rev1/readme.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Optical Keyboard - rev1

* Keyboard Maintainer: [Girish](https://github.com/girishji)
* Hardware Supported: Black pill STM32F401

This keyboard uses optical switches instead of mechanical switches.
PCB for this keyboard can be found on [github](https://github.com/girishji/optical-keyboard-mx).
The matrix implementation uses column-to-row arrangement. All the IR's (infra-red led) in a
column are turned on before reading the digital input pins connected to rows. After
reading all the rows, column is powered off and subsequent column is powered on before
repeating the reading process. There is a delay after a column is powered on in order to
accommodate PT (Phototransistor) 'rise' time.

This is not a high-performance keyboard since the IR's are provided with very
minimal current (way below the suggested operating value). This is done so as
to keep the design simple. A single GPIO pin of STM32F4 set as output
can provide 20 ma. Each IR is supplied with ~3.6 ma, so that the total current for 5
rows (IRs) falls within the allowable maximum current per pin. Yet it achieves
a scan rate of 400 hz. Since there is no debounce delay, the latency before USB
transit is 2.5 ms. Compared to mechanical keyboards (with 5 ms debounce delay)
this keyboard achieves respectable performance. It is possible to push scanning
rate much higher, but it requires a different matrix design and additional switching
components to increase current to IR.

## Build

Make example for this keyboard (after setting up your build environment):

make opticalkb/rev1:default

Flashing example for this keyboard:

make opticalkb/rev1:default:flash

See the [build environment setup](https://docs.qmk.fm/#/getting_started_build_tools) and the [make instructions](https://docs.qmk.fm/#/getting_started_make_guide) for more information. Brand new to QMK? Start with our [Complete Newbs Guide](https://docs.qmk.fm/#/newbs).

## Bootloader

Enter the bootloader in 3 ways:

* **Physical reset button**: Briefly press the button BOOT and then button NRST, release NRST first and then BOOT
* **Keycode in layout**: Press the key mapped to `RESET` if it is available

Verify that it entered DFU (bootloader) mode through QMK Toolbox before
flashing.

## Wiring Instructions

There is only one way to solder Blackpill (STM32F401) to the [PCB](https://github.com/girishji/optical-keyboard-mx).
If you are going to use Blackpill in other projects, note that certain pins are [not usable](https://docs.qmk.fm/#/platformdev_blackpill_f411).

4 changes: 4 additions & 0 deletions keyboards/opticalkb/rev1/rev1.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright 2022 Girish Palya (@girishji)
// SPDX-License-Identifier: GPL-2.0-or-later

#include "rev1.h"
Loading