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.
133 lines
4.1 KiB
133 lines
4.1 KiB
6 years ago
|
/* Kaleidoscope-HardwareTestMode - A factory test mode for the Model 01.
|
||
|
* Copyright (C) 2017-2019 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/>.
|
||
|
*/
|
||
|
|
||
5 years ago
|
#include "kaleidoscope/Runtime.h"
|
||
6 years ago
|
#include "Kaleidoscope-HardwareTestMode.h"
|
||
|
#include "Kaleidoscope-LEDEffect-Rainbow.h"
|
||
|
|
||
|
namespace kaleidoscope {
|
||
|
namespace plugin {
|
||
|
|
||
|
|
||
|
constexpr uint8_t CHATTER_CYCLE_LIMIT = 30;
|
||
|
|
||
|
uint8_t HardwareTestMode::actionKey;
|
||
|
|
||
|
void HardwareTestMode::setActionKey(uint8_t key) {
|
||
6 years ago
|
actionKey = key;
|
||
6 years ago
|
}
|
||
|
|
||
|
void HardwareTestMode::waitForKeypress() {
|
||
|
while (1) {
|
||
5 years ago
|
Runtime.device().readMatrix();
|
||
|
if (Runtime.device().isKeyswitchPressed(actionKey) &&
|
||
|
! Runtime.device().wasKeyswitchPressed(actionKey)) {
|
||
6 years ago
|
break;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void HardwareTestMode::setLeds(cRGB color) {
|
||
|
::LEDControl.set_all_leds_to(color);
|
||
|
::LEDControl.syncLeds();
|
||
|
waitForKeypress();
|
||
|
}
|
||
|
|
||
|
void HardwareTestMode::testLeds(void) {
|
||
|
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);
|
||
|
|
||
|
setLeds(brightWhite);
|
||
|
setLeds(blue);
|
||
|
setLeds(green);
|
||
|
setLeds(red);
|
||
6 years ago
|
|
||
|
// This works under the assumption that LEDRainbowEffect
|
||
|
// has been registered with KALEIDOSCOPE_INIT_PLUGINS in
|
||
|
// the sketch. Otherwise LEDRainbowEffect would not be
|
||
|
// known to LEDControl.
|
||
|
//
|
||
|
::LEDControl.activate(&::LEDRainbowEffect);
|
||
|
|
||
6 years ago
|
// rainbow for 10 seconds
|
||
|
::LEDRainbowEffect.update_delay(5);
|
||
4 years ago
|
for (uint8_t i = 0; i < 254; i++) {
|
||
6 years ago
|
::LEDControl.update();
|
||
6 years ago
|
::LEDControl.syncLeds();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
void HardwareTestMode::testMatrix() {
|
||
|
// Reset bad keys from previous tests.
|
||
5 years ago
|
chatter_data state[Runtime.device().numKeys()] = {{0, 0, 0}};
|
||
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 yellow = CRGB(201, 100, 0);
|
||
|
|
||
|
while (1) {
|
||
5 years ago
|
Runtime.device().readMatrix();
|
||
5 years ago
|
for (auto key_addr : KeyAddr::all()) {
|
||
|
uint8_t keynum = key_addr.toInt();
|
||
|
|
||
|
// If the key is toggled on
|
||
5 years ago
|
if (Runtime.device().isKeyswitchPressed(key_addr) && ! Runtime.device().wasKeyswitchPressed(key_addr)) {
|
||
5 years ago
|
// And it's too soon (in terms of cycles between changes)
|
||
|
state[keynum].tested = 1;
|
||
|
if (state[keynum].cyclesSinceStateChange < CHATTER_CYCLE_LIMIT) {
|
||
|
state[keynum].bad = 1;
|
||
6 years ago
|
}
|
||
5 years ago
|
state[keynum].cyclesSinceStateChange = 0;
|
||
|
} else if (state[keynum].cyclesSinceStateChange < CHATTER_CYCLE_LIMIT) {
|
||
|
state[keynum].cyclesSinceStateChange++;
|
||
|
}
|
||
|
// If the key is held down
|
||
5 years ago
|
if (Runtime.device().isKeyswitchPressed(key_addr) && Runtime.device().wasKeyswitchPressed(key_addr)) {
|
||
|
Runtime.device().setCrgbAt(key_addr, green);
|
||
4 years ago
|
} else if (state[keynum].bad == 1) {
|
||
|
// If we triggered chatter detection ever on this key
|
||
5 years ago
|
Runtime.device().setCrgbAt(key_addr, red);
|
||
5 years ago
|
} else if (state[keynum].tested == 0) {
|
||
5 years ago
|
Runtime.device().setCrgbAt(key_addr, yellow);
|
||
4 years ago
|
} else if (! Runtime.device().isKeyswitchPressed(key_addr)) {
|
||
|
// If the key is not currently pressed and was not just released and is not marked bad
|
||
5 years ago
|
Runtime.device().setCrgbAt(key_addr, blue);
|
||
6 years ago
|
}
|
||
|
}
|
||
|
::LEDControl.syncLeds();
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void HardwareTestMode::runTests() {
|
||
|
// When we start test mode, we -may- have some keys held, so empty it
|
||
|
// out and send a new report
|
||
5 years ago
|
kaleidoscope::Runtime.hid().keyboard().releaseAllKeys();
|
||
|
kaleidoscope::Runtime.hid().keyboard().sendReport();
|
||
5 years ago
|
Runtime.device().enableHardwareTestMode();
|
||
6 years ago
|
testLeds();
|
||
|
testMatrix();
|
||
|
}
|
||
|
|
||
|
}
|
||
|
}
|
||
|
|
||
|
kaleidoscope::plugin::HardwareTestMode HardwareTestMode;
|