Abstract away direct Serial access

In preparation for making Serial access configurable on a per-board basis,
introduce `KeyboardHardware.serialPort()`, which - for the time being - returns
the Serial object.

All users of Serial have been updated to use the new API.

Signed-off-by: Gergely Nagy <algernon@keyboard.io>
pull/701/head
Gergely Nagy 5 years ago
parent f84de9f292
commit 32bafc7adc
No known key found for this signature in database
GPG Key ID: AC1E90BAC433F68F

@ -17,7 +17,7 @@ the box, without any further configuration:
KALEIDOSCOPE_INIT_PLUGINS(CycleTimeReport); KALEIDOSCOPE_INIT_PLUGINS(CycleTimeReport);
void setup (void) { void setup (void) {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup (); Kaleidoscope.setup ();
} }
``` ```

@ -32,11 +32,11 @@ dictionary:
#include <Kaleidoscope-Leader.h> #include <Kaleidoscope-Leader.h>
static void leaderA(uint8_t seq_index) { static void leaderA(uint8_t seq_index) {
Serial.println("leaderA"); KeyboardHardware.serialPort().println("leaderA");
} }
static void leaderTX(uint8_t seq_index) { static void leaderTX(uint8_t seq_index) {
Serial.println("leaderTX"); KeyboardHardware.serialPort().println("leaderTX");
} }
static const kaleidoscope::Leader::dictionary_t leader_dictionary[] PROGMEM = static const kaleidoscope::Leader::dictionary_t leader_dictionary[] PROGMEM =
@ -46,7 +46,7 @@ static const kaleidoscope::Leader::dictionary_t leader_dictionary[] PROGMEM =
KALEIDOSCOPE_INIT_PLUGINS(Leader); KALEIDOSCOPE_INIT_PLUGINS(Leader);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();

@ -34,8 +34,8 @@ void systerAction(kaleidoscope::plugin::Syster::action_t action, const char *sym
kaleidoscope::hid::sendKeyboardReport (); kaleidoscope::hid::sendKeyboardReport ();
break; break;
case kaleidoscope::plugin::Syster::SymbolAction: case kaleidoscope::plugin::Syster::SymbolAction:
Serial.print ("systerAction: symbol="); KeyboardHardware.serialPort().print ("systerAction: symbol=");
Serial.println (symbol); KeyboardHardware.serialPort().println (symbol);
if (strcmp (symbol, "coffee") == 0) { if (strcmp (symbol, "coffee") == 0) {
Unicode.type (0x2615); Unicode.type (0x2615);
} }
@ -46,7 +46,7 @@ void systerAction(kaleidoscope::plugin::Syster::action_t action, const char *sym
KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings, HostOS, Unicode, Syster); KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings, HostOS, Unicode, Syster);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup (); Kaleidoscope.setup ();
} }
``` ```

@ -86,7 +86,7 @@ static kaleidoscope::plugin::LEDSolidColor solidViolet(70, 0, 60);
const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) { const macro_t *macroAction(uint8_t macroIndex, uint8_t keyState) {
if (macroIndex == 1 && keyToggledOn(keyState)) { if (macroIndex == 1 && keyToggledOn(keyState)) {
Serial.print("Keyboard.IO keyboard driver v0.00"); KeyboardHardware.serialPort().print("Keyboard.IO keyboard driver v0.00");
return MACRO(I(25), return MACRO(I(25),
D(LeftShift), T(M), U(LeftShift), T(O), T(D), T(E), T(L), D(LeftShift), T(M), U(LeftShift), T(O), T(D), T(E), T(L),
T(Spacebar), T(Spacebar),

@ -167,7 +167,7 @@ KALEIDOSCOPE_INIT_PLUGINS(Macros);
void setup() { void setup() {
Kaleidoscope.setup(); Kaleidoscope.setup();
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
} }
void loop() { void loop() {

@ -50,7 +50,7 @@ KEYMAPS(
KALEIDOSCOPE_INIT_PLUGINS(GeminiPR); KALEIDOSCOPE_INIT_PLUGINS(GeminiPR);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();
} }

@ -43,7 +43,7 @@ KEYMAPS(
KALEIDOSCOPE_INIT_PLUGINS(CycleTimeReport); KALEIDOSCOPE_INIT_PLUGINS(CycleTimeReport);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();
} }

@ -56,7 +56,7 @@ KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings,
Macros); Macros);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();

@ -42,16 +42,16 @@ KEYMAPS(
KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings); KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();
while (!Serial) { while (!KeyboardHardware.serialPort()) {
} }
Serial.println(EEPROMSettings.isValid() ? F("valid EEPROM settings") : F("invalid EEPROM settings")); KeyboardHardware.serialPort().println(EEPROMSettings.isValid() ? F("valid EEPROM settings") : F("invalid EEPROM settings"));
Serial.println(EEPROMSettings.crc(), HEX); KeyboardHardware.serialPort().println(EEPROMSettings.crc(), HEX);
Serial.println(EEPROMSettings.version()); KeyboardHardware.serialPort().println(EEPROMSettings.version());
} }
void loop() { void loop() {

@ -43,12 +43,12 @@ KEYMAPS(
KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings, HostOS); KALEIDOSCOPE_INIT_PLUGINS(EEPROMSettings, HostOS);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();
Serial.print("Host OS id is: "); KeyboardHardware.serialPort().print("Host OS id is: ");
Serial.println(HostOS.os(), DEC); KeyboardHardware.serialPort().println(HostOS.os(), DEC);
} }
void loop() { void loop() {

@ -61,7 +61,7 @@ KEYMAPS(
KALEIDOSCOPE_INIT_PLUGINS(GeminiPR); KALEIDOSCOPE_INIT_PLUGINS(GeminiPR);
void setup() { void setup() {
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
Kaleidoscope.setup(); Kaleidoscope.setup();
} }

@ -40,11 +40,11 @@ KEYMAPS(
// *INDENT-ON* // *INDENT-ON*
static void leaderTestA(uint8_t seq_index) { static void leaderTestA(uint8_t seq_index) {
Serial.println(F("leaderTestA")); KeyboardHardware.serialPort().println(F("leaderTestA"));
} }
static void leaderTestAA(uint8_t seq_index) { static void leaderTestAA(uint8_t seq_index) {
Serial.println(F("leaderTestAA")); KeyboardHardware.serialPort().println(F("leaderTestAA"));
} }
static const kaleidoscope::plugin::Leader::dictionary_t leader_dictionary[] PROGMEM = static const kaleidoscope::plugin::Leader::dictionary_t leader_dictionary[] PROGMEM =

@ -56,8 +56,8 @@ void systerAction(kaleidoscope::plugin::Syster::action_t action, const char *sym
kaleidoscope::hid::sendKeyboardReport(); kaleidoscope::hid::sendKeyboardReport();
break; break;
case kaleidoscope::plugin::Syster::SymbolAction: case kaleidoscope::plugin::Syster::SymbolAction:
Serial.print("systerAction: symbol="); KeyboardHardware.serialPort().print("systerAction: symbol=");
Serial.println(symbol); KeyboardHardware.serialPort().println(symbol);
if (strcmp(symbol, "coffee") == 0) { if (strcmp(symbol, "coffee") == 0) {
Unicode.type(0x2615); Unicode.type(0x2615);
} }

@ -394,6 +394,13 @@ class Hardware {
return EEPROM; return EEPROM;
} }
/**
* Method to return the serial port object used by the hardware.
*/
auto serialPort() -> decltype(Serial) & {
return Serial;
}
/** @} */ /** @} */
}; };
} }

@ -34,7 +34,7 @@ Kaleidoscope_::setup(void) {
// //
// TODO(anyone): Figure out a way we can get rid of this, and fix the bug // TODO(anyone): Figure out a way we can get rid of this, and fix the bug
// properly. // properly.
Serial.begin(9600); KeyboardHardware.serialPort().begin(9600);
kaleidoscope::Hooks::onSetup(); kaleidoscope::Hooks::onSetup();

@ -27,20 +27,20 @@ namespace plugin {
char FocusSerial::command_[32]; char FocusSerial::command_[32];
void FocusSerial::drain(void) { void FocusSerial::drain(void) {
if (Serial.available()) if (KeyboardHardware.serialPort().available())
while (Serial.peek() != '\n') while (KeyboardHardware.serialPort().peek() != '\n')
Serial.read(); KeyboardHardware.serialPort().read();
} }
EventHandlerResult FocusSerial::beforeReportingState() { EventHandlerResult FocusSerial::beforeReportingState() {
if (Serial.available() == 0) if (KeyboardHardware.serialPort().available() == 0)
return EventHandlerResult::OK; return EventHandlerResult::OK;
uint8_t i = 0; uint8_t i = 0;
do { do {
command_[i++] = Serial.read(); command_[i++] = KeyboardHardware.serialPort().read();
if (Serial.peek() == '\n') if (KeyboardHardware.serialPort().peek() == '\n')
break; break;
} while (command_[i - 1] != ' ' && i < 32); } while (command_[i - 1] != ' ' && i < 32);
if (command_[i - 1] == ' ') if (command_[i - 1] == ' ')
@ -50,12 +50,12 @@ EventHandlerResult FocusSerial::beforeReportingState() {
Kaleidoscope.onFocusEvent(command_); Kaleidoscope.onFocusEvent(command_);
Serial.println(F("\r\n.")); KeyboardHardware.serialPort().println(F("\r\n."));
drain(); drain();
if (Serial.peek() == '\n') if (KeyboardHardware.serialPort().peek() == '\n')
Serial.read(); KeyboardHardware.serialPort().read();
return EventHandlerResult::OK; return EventHandlerResult::OK;
} }
@ -65,7 +65,7 @@ bool FocusSerial::handleHelp(const char *command,
if (strcmp_P(command, PSTR("help")) != 0) if (strcmp_P(command, PSTR("help")) != 0)
return false; return false;
Serial.println((const __FlashStringHelper *)help_message); KeyboardHardware.serialPort().println((const __FlashStringHelper *)help_message);
return true; return true;
} }
@ -75,7 +75,7 @@ EventHandlerResult FocusSerial::onFocusEvent(const char *command) {
} }
void FocusSerial::printBool(bool b) { void FocusSerial::printBool(bool b) {
Serial.print((b) ? F("true") : F("false")); KeyboardHardware.serialPort().print((b) ? F("true") : F("false"));
} }
} }

@ -37,12 +37,12 @@ class FocusSerial : public kaleidoscope::Plugin {
} }
void send(const bool b) { void send(const bool b) {
printBool(b); printBool(b);
Serial.print(SEPARATOR); KeyboardHardware.serialPort().print(SEPARATOR);
} }
template <typename V> template <typename V>
void send(V v) { void send(V v) {
Serial.print(v); KeyboardHardware.serialPort().print(v);
Serial.print(SEPARATOR); KeyboardHardware.serialPort().print(SEPARATOR);
} }
template <typename Var, typename... Vars> template <typename Var, typename... Vars>
void send(Var v, const Vars&... vars) { void send(Var v, const Vars&... vars) {
@ -53,31 +53,31 @@ class FocusSerial : public kaleidoscope::Plugin {
void sendRaw() {} void sendRaw() {}
template <typename Var, typename... Vars> template <typename Var, typename... Vars>
void sendRaw(Var v, const Vars&... vars) { void sendRaw(Var v, const Vars&... vars) {
Serial.print(v); KeyboardHardware.serialPort().print(v);
sendRaw(vars...); sendRaw(vars...);
} }
const char peek() { const char peek() {
return Serial.peek(); return KeyboardHardware.serialPort().peek();
} }
void read(Key &key) { void read(Key &key) {
key.raw = Serial.parseInt(); key.raw = KeyboardHardware.serialPort().parseInt();
} }
void read(cRGB &color) { void read(cRGB &color) {
color.r = Serial.parseInt(); color.r = KeyboardHardware.serialPort().parseInt();
color.g = Serial.parseInt(); color.g = KeyboardHardware.serialPort().parseInt();
color.b = Serial.parseInt(); color.b = KeyboardHardware.serialPort().parseInt();
} }
void read(uint8_t &u8) { void read(uint8_t &u8) {
u8 = Serial.parseInt(); u8 = KeyboardHardware.serialPort().parseInt();
} }
void read(uint16_t &u16) { void read(uint16_t &u16) {
u16 = Serial.parseInt(); u16 = KeyboardHardware.serialPort().parseInt();
} }
bool isEOL() { bool isEOL() {
return Serial.peek() == '\n'; return KeyboardHardware.serialPort().peek() == '\n';
} }
static constexpr char COMMENT = '#'; static constexpr char COMMENT = '#';

@ -39,7 +39,7 @@ EventHandlerResult GeminiPR::onKeyswitchEvent(Key &mapped_key, KeyAddr key_addr,
if (keys_held_ == 0) { if (keys_held_ == 0) {
state_[0] |= 0x80; state_[0] |= 0x80;
Serial.write(state_, sizeof(state_)); KeyboardHardware.serialPort().write(state_, sizeof(state_));
memset(state_, 0, sizeof(state_)); memset(state_, 0, sizeof(state_));
} }
} }

@ -159,13 +159,13 @@ void TestMode::toggle_programming_leds_on() {
} }
void TestMode::run_tests() { void TestMode::run_tests() {
// Serial.println("Running tests"); // KeyboardHardware.serialPort().println("Running tests");
toggle_programming_leds_on(); toggle_programming_leds_on();
// Disable debouncing // Disable debouncing
KeyboardHardware.setKeyscanInterval(2); KeyboardHardware.setKeyscanInterval(2);
test_leds(); test_leds();
testMatrix(); testMatrix();
// Serial.println("Done running tests"); // KeyboardHardware.serialPort().println("Done running tests");
} }
} }

Loading…
Cancel
Save