You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
167 lines
4.9 KiB
167 lines
4.9 KiB
6 years ago
|
/* Kaleidoscope-Model01-TestMode - A factory test mode for the Model 01.
|
||
|
* Copyright (C) 2017-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/>.
|
||
|
*/
|
||
|
|
||
8 years ago
|
#include "Kaleidoscope.h"
|
||
|
#include "Kaleidoscope-Model01-TestMode.h"
|
||
|
#include "Kaleidoscope-LEDEffect-Rainbow.h"
|
||
7 years ago
|
|
||
6 years ago
|
namespace kaleidoscope {
|
||
|
namespace plugin {
|
||
7 years ago
|
|
||
6 years ago
|
constexpr uint8_t CHATTER_CYCLE_LIMIT = 30;
|
||
|
constexpr uint8_t TOGGLED_OFF = 2;
|
||
|
constexpr uint8_t TOGGLED_ON = 1;
|
||
|
constexpr uint8_t HELD = 3;
|
||
|
constexpr uint8_t RELEASED = 0;
|
||
7 years ago
|
|
||
6 years ago
|
EventHandlerResult TestMode::beforeReportingState() {
|
||
7 years ago
|
if (KeyboardHardware.isKeyswitchPressed(R0C0) &&
|
||
|
KeyboardHardware.isKeyswitchPressed(R0C6) &&
|
||
|
KeyboardHardware.isKeyswitchPressed(R3C6) &&
|
||
|
KeyboardHardware.pressedKeyswitchCount() == 3) {
|
||
8 years ago
|
run_tests();
|
||
|
}
|
||
6 years ago
|
return EventHandlerResult::OK;
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
void TestMode::waitForKeypress() {
|
||
7 years ago
|
for (uint8_t temp = 0; temp < 8; temp++) {
|
||
7 years ago
|
KeyboardHardware.readMatrix();
|
||
|
}
|
||
8 years ago
|
while (1) {
|
||
8 years ago
|
KeyboardHardware.readMatrix();
|
||
7 years ago
|
if (KeyboardHardware.isKeyswitchPressed(R3C6)
|
||
|
&& KeyboardHardware.pressedKeyswitchCount() == 1
|
||
8 years ago
|
&& KeyboardHardware.previousLeftHandState.all == 0) {
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
void TestMode::set_leds(cRGB color) {
|
||
|
::LEDControl.set_all_leds_to(color);
|
||
|
::LEDControl.syncLeds();
|
||
8 years ago
|
waitForKeypress();
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
void TestMode::test_leds(void) {
|
||
6 years ago
|
constexpr cRGB red = CRGB(201, 0, 0);
|
||
|
constexpr cRGB blue = CRGB(0, 0, 201);
|
||
|
constexpr cRGB green = CRGB(0, 201, 0);
|
||
|
constexpr cRGB brightWhite = CRGB(160, 160, 160);
|
||
7 years ago
|
|
||
8 years ago
|
// make all the LEDs bright red
|
||
7 years ago
|
set_leds(red);
|
||
8 years ago
|
// make all the LEDs bright green
|
||
7 years ago
|
set_leds(green);
|
||
8 years ago
|
// make all the LEDs bright blue
|
||
7 years ago
|
set_leds(blue);
|
||
8 years ago
|
// make all the LEDs bright white (1.6A)
|
||
7 years ago
|
set_leds(brightWhite);
|
||
8 years ago
|
// rainbow for 10 seconds
|
||
8 years ago
|
for (auto i = 0; i < 1000; i++) {
|
||
6 years ago
|
::LEDRainbowEffect.update();
|
||
|
::LEDControl.syncLeds();
|
||
8 years ago
|
}
|
||
7 years ago
|
waitForKeypress();
|
||
8 years ago
|
}
|
||
|
|
||
|
|
||
7 years ago
|
|
||
6 years ago
|
void TestMode::handleKeyEvent(side_data_t *side, keydata_t *oldState, keydata_t *newState, uint8_t row, uint8_t col, uint8_t col_offset) {
|
||
7 years ago
|
|
||
6 years ago
|
constexpr cRGB red = CRGB(201, 0, 0);
|
||
|
constexpr cRGB blue = CRGB(0, 0, 201);
|
||
|
constexpr cRGB green = CRGB(0, 201, 0);
|
||
7 years ago
|
|
||
6 years ago
|
const uint8_t keynum = (row * 8) + (col);
|
||
7 years ago
|
|
||
6 years ago
|
const uint8_t keyState = ((bitRead(oldState->all, keynum) << 1) |
|
||
|
(bitRead(newState->all, keynum) << 0));
|
||
7 years ago
|
if (keyState == TOGGLED_ON) {
|
||
|
if (side->cyclesSinceStateChange[keynum] < CHATTER_CYCLE_LIMIT) {
|
||
|
bitSet(side->badKeys, keynum);
|
||
|
}
|
||
|
side->cyclesSinceStateChange[keynum] = 0;
|
||
|
} else if (side->cyclesSinceStateChange[keynum] <= CHATTER_CYCLE_LIMIT) {
|
||
|
side->cyclesSinceStateChange[keynum]++;
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
|
||
7 years ago
|
|
||
7 years ago
|
// If the key is held down
|
||
|
if (keyState == HELD) {
|
||
|
KeyboardHardware.setCrgbAt(row, col_offset - col, green);
|
||
|
}
|
||
|
// If we triggered chatter detection ever on this key
|
||
|
else if (bitRead(side->badKeys, keynum) == 1) {
|
||
|
KeyboardHardware.setCrgbAt(row, col_offset - col, red);
|
||
|
}
|
||
7 years ago
|
|
||
7 years ago
|
// If the key was just released
|
||
|
else if (keyState == TOGGLED_OFF) {
|
||
|
KeyboardHardware.setCrgbAt(row, col_offset - col, blue);
|
||
|
}
|
||
|
}
|
||
7 years ago
|
|
||
8 years ago
|
|
||
6 years ago
|
void TestMode::testMatrix() {
|
||
7 years ago
|
// Reset bad keys from previous tests.
|
||
|
side_data_t left = {{0}, 0};
|
||
|
side_data_t right = {{0}, 0};
|
||
|
|
||
|
|
||
6 years ago
|
::LEDControl.set_all_leds_to(200, 0, 0);
|
||
7 years ago
|
// Clear out the key event buffer so we don't get messed up information from
|
||
|
// taps during LED test mode.
|
||
8 years ago
|
while (1) {
|
||
8 years ago
|
KeyboardHardware.readMatrix();
|
||
7 years ago
|
if (KeyboardHardware.isKeyswitchPressed(R0C0) &&
|
||
|
KeyboardHardware.isKeyswitchPressed(R0C6) &&
|
||
|
KeyboardHardware.isKeyswitchPressed(R3C6) &&
|
||
|
KeyboardHardware.pressedKeyswitchCount() == 3) {
|
||
8 years ago
|
break;
|
||
|
}
|
||
|
for (byte row = 0; row < 4; row++) {
|
||
|
for (byte col = 0; col < 8; col++) {
|
||
7 years ago
|
handleKeyEvent(&left, &(KeyboardHardware.previousLeftHandState), &(KeyboardHardware.leftHandState), row, col, 7);
|
||
|
handleKeyEvent(&right, &(KeyboardHardware.previousRightHandState), &(KeyboardHardware.rightHandState), row, col, 15);
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
6 years ago
|
::LEDControl.syncLeds();
|
||
8 years ago
|
}
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
void TestMode::toggle_programming_leds_on() {
|
||
8 years ago
|
PORTD |= (1 << 5);
|
||
|
PORTB |= (1 << 0);
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
void TestMode::run_tests() {
|
||
8 years ago
|
// Serial.println("Running tests");
|
||
8 years ago
|
toggle_programming_leds_on();
|
||
7 years ago
|
// Disable debouncing
|
||
7 years ago
|
KeyboardHardware.setKeyscanInterval(2);
|
||
8 years ago
|
test_leds();
|
||
8 years ago
|
testMatrix();
|
||
8 years ago
|
// Serial.println("Done running tests");
|
||
8 years ago
|
}
|
||
|
|
||
6 years ago
|
}
|
||
|
}
|
||
|
|
||
|
kaleidoscope::plugin::TestMode TestMode;
|