From 3149a781aee91644defbeebf5d449c7ed1239c9b Mon Sep 17 00:00:00 2001 From: Gergely Nagy Date: Sat, 28 Aug 2021 19:49:24 +0200 Subject: [PATCH] gd32: a pretty dumb key scanner Signed-off-by: Gergely Nagy --- .../src/kaleidoscope/device/gd32/Eval.h | 4 + .../device/gd32/eval/KeyScanner.cpp | 87 +++++++++++++++++++ .../device/gd32/eval/KeyScanner.h | 74 ++++++++++++++++ 3 files changed, 165 insertions(+) create mode 100644 plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.cpp create mode 100644 plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.h diff --git a/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/Eval.h b/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/Eval.h index b88ae4dc..f736e67a 100644 --- a/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/Eval.h +++ b/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/Eval.h @@ -25,6 +25,8 @@ #include "kaleidoscope/driver/storage/GD32Flash.h" #include "kaleidoscope/driver/bootloader/gd32/Base.h" +#include "kaleidoscope/device/gd32/eval/KeyScanner.h" + namespace kaleidoscope { namespace device { namespace gd32 { @@ -33,6 +35,8 @@ struct EvalStorageProps: kaleidoscope::driver::storage::GD32FlashProps {}; struct EvalProps: kaleidoscope::device::BaseProps { typedef kaleidoscope::driver::bootloader::gd32::Base BootLoader; + typedef eval::KeyScannerProps KeyScannerProps; + typedef eval::KeyScanner KeyScanner; typedef EvalStorageProps StorageProps; typedef kaleidoscope::driver::storage::GD32Flash Storage; static constexpr const char *short_name = "GD32Eval"; diff --git a/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.cpp b/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.cpp new file mode 100644 index 00000000..cf47d75c --- /dev/null +++ b/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.cpp @@ -0,0 +1,87 @@ +/* -*- mode: c++ -*- + * Kaleidoscope - Firmware for computer input devices + * Copyright (C) 2021 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 . + */ + +#ifdef ARDUINO_GD32F303ZE_EVAL + +#include "Arduino.h" +#include "kaleidoscope/Runtime.h" +#include "kaleidoscope/device/gd32/eval/KeyScanner.h" + +namespace kaleidoscope { +namespace device { +namespace gd32 { +namespace eval { + +uint8_t KeyScanner::keyState; +uint8_t KeyScanner::previousKeyState; +int KeyScanner::prevPinState[2]; + +void KeyScanner::setup() { + pinMode(PA3, INPUT); + pinMode(PB13, INPUT); + + prevPinState[0] = HIGH; + prevPinState[1] = HIGH; +} + +void KeyScanner::scanMatrix() { + readMatrix(); + actOnMatrixScan(); +} + +void KeyScanner::readMatrix() { + int b1, b2; + + b1 = digitalRead(PA3); + b2 = digitalRead(PB13); + + previousKeyState = keyState; + + if ((b1 != prevPinState[0]) && (b1 == HIGH)) { + bitSet(keyState, 0); + } + prevPinState[0] = b1; + + if ((b2 != prevPinState[1]) && (b2 == HIGH)) { + bitSet(keyState, 1); + } + prevPinState[1] = b2; +} + +void KeyScanner::actOnMatrixScan() { + if (bitRead(keyState, 0) != bitRead(previousKeyState, 0)) { + ThisType::handleKeyswitchEvent( + Key_NoKey, + typename Props_::KeyAddr(0, 0), + keyState + ); + } + if (bitRead(keyState, 1) != bitRead(previousKeyState, 1)) { + ThisType::handleKeyswitchEvent( + Key_NoKey, + typename Props_::KeyAddr(0, 1), + keyState + ); + } +} + +} // namespace eval +} // namespace gd32 +} // namespace device +} // namespace kaleidoscope + +#endif diff --git a/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.h b/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.h new file mode 100644 index 00000000..3cd0dc3d --- /dev/null +++ b/plugins/Kaleidoscope-Hardware-GD32-Eval/src/kaleidoscope/device/gd32/eval/KeyScanner.h @@ -0,0 +1,74 @@ +/* -*- mode: c++ -*- + * Kaleidoscope - Firmware for computer input devices + * Copyright (C) 2021 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 . + */ + +#pragma once + +#ifdef ARDUINO_GD32F303ZE_EVAL + +#include + +#include "kaleidoscope/device/gd32/eval/KeyScanner.h" + +namespace kaleidoscope { +namespace device { +namespace gd32 { +namespace eval { + +struct KeyScannerProps : public kaleidoscope::driver::keyscanner::BaseProps { + static constexpr uint8_t matrix_rows = 1; + static constexpr uint8_t matrix_columns = 2; + typedef MatrixAddr KeyAddr; +}; + +class KeyScanner: public kaleidoscope::driver::keyscanner::Base { + private: + typedef KeyScanner ThisType; + typedef KeyScannerProps Props_; + public: + static void setup(); + static void scanMatrix(); + static void readMatrix(); + static void actOnMatrixScan(); + + static bool isKeyswitchPressed(KeyAddr key_addr) { + if (key_addr.row() != 0) + return false; + return bitRead(keyState, key_addr.col()); + } + static uint8_t pressedKeyswitchCount() { + return __builtin_popcount(keyState); + } + + static bool wasKeyswitchPressed(KeyAddr key_addr) { + if (key_addr.row() != 0) + return false; + return bitRead(previousKeyState, key_addr.col()); + } + static uint8_t previousPressedKeyswitchCount() { + return __builtin_popcount(previousKeyState); + } + protected: + static uint8_t keyState, previousKeyState; + static int prevPinState[2]; +}; + +} // namespace eval +} // namespace gd32 +} // namespace device +} // namespace kaleidoscope + +#endif