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.
85 lines
2.2 KiB
85 lines
2.2 KiB
6 years ago
|
/* -*- mode: c++ -*-
|
||
|
* Kaleidoscope-FocusSerial -- Bidirectional communication plugin
|
||
|
* 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/>.
|
||
|
*/
|
||
|
|
||
|
#include <Kaleidoscope-FocusSerial.h>
|
||
|
|
||
|
#ifdef __AVR__
|
||
|
#include <avr/pgmspace.h>
|
||
|
#endif
|
||
|
|
||
|
namespace kaleidoscope {
|
||
6 years ago
|
namespace plugin {
|
||
6 years ago
|
|
||
|
char FocusSerial::command_[32];
|
||
|
|
||
|
void FocusSerial::drain(void) {
|
||
5 years ago
|
if (Runtime.serialPort().available())
|
||
|
while (Runtime.serialPort().peek() != '\n')
|
||
|
Runtime.serialPort().read();
|
||
6 years ago
|
}
|
||
|
|
||
|
EventHandlerResult FocusSerial::beforeReportingState() {
|
||
5 years ago
|
if (Runtime.serialPort().available() == 0)
|
||
6 years ago
|
return EventHandlerResult::OK;
|
||
|
|
||
|
uint8_t i = 0;
|
||
|
do {
|
||
5 years ago
|
command_[i++] = Runtime.serialPort().read();
|
||
6 years ago
|
|
||
5 years ago
|
if (Runtime.serialPort().peek() == '\n')
|
||
6 years ago
|
break;
|
||
|
} while (command_[i - 1] != ' ' && i < 32);
|
||
|
if (command_[i - 1] == ' ')
|
||
|
command_[i - 1] = '\0';
|
||
|
else
|
||
|
command_[i] = '\0';
|
||
|
|
||
5 years ago
|
Runtime.onFocusEvent(command_);
|
||
6 years ago
|
|
||
5 years ago
|
Runtime.serialPort().println(F("\r\n."));
|
||
6 years ago
|
|
||
|
drain();
|
||
|
|
||
5 years ago
|
if (Runtime.serialPort().peek() == '\n')
|
||
|
Runtime.serialPort().read();
|
||
6 years ago
|
|
||
|
return EventHandlerResult::OK;
|
||
|
}
|
||
|
|
||
6 years ago
|
bool FocusSerial::handleHelp(const char *command,
|
||
|
const char *help_message) {
|
||
|
if (strcmp_P(command, PSTR("help")) != 0)
|
||
|
return false;
|
||
|
|
||
5 years ago
|
Runtime.serialPort().println((const __FlashStringHelper *)help_message);
|
||
6 years ago
|
return true;
|
||
|
}
|
||
|
|
||
6 years ago
|
EventHandlerResult FocusSerial::onFocusEvent(const char *command) {
|
||
|
handleHelp(command, PSTR("help"));
|
||
|
return EventHandlerResult::OK;
|
||
|
}
|
||
|
|
||
6 years ago
|
void FocusSerial::printBool(bool b) {
|
||
5 years ago
|
Runtime.serialPort().print((b) ? F("true") : F("false"));
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
kaleidoscope::plugin::FocusSerial Focus;
|