Skip to content
Permalink
Browse files

Optimized Serial Communication Code & Added Key Binding RequestMapping

  • Loading branch information...
ltxom committed Mar 7, 2019
1 parent f816c07 commit a27db95dbf11b54b2c5b80aaca22121f4cdf04e2
@@ -3,58 +3,109 @@

#define prefix "[Controller]"
#define keyPrefix "@"
#define suffix "*"
#define suffix String("*")
#define splitter ","
#define lenghtOfRxTxData 40
#define keyPressDuration 10
LinkedList<String> toPressKeysList;

void setup() {
Serial.begin(19200);
cPrintln("Init...");
Serial1.begin(57600);
Serial1.setTimeout(lenghtOfRxTxData);
toPressKeysList = LinkedList < String > ();
Keyboard.begin();
#define keyPressDuration 40

class KeyCommand {
public:
KeyCommand(int commandNumber, String command) {
this -> _commandNumber = commandNumber;
this -> _command = command;
}
int getCommandNumber() {
return this -> _commandNumber;
}
String getCommand() {
return this -> _command;
}
String toString() {
return this -> _command + splitter + String(this -> _commandNumber);
}
private:
int _commandNumber;
String _command;
};


LinkedList<KeyCommand*> toPressKeysList;
LinkedList<int> pressedCommandList;
void setup(){
Serial.begin(19200);
cPrintln("Init...");
Serial1.begin(57600);
Serial1.setTimeout(lenghtOfRxTxData);
toPressKeysList = LinkedList<KeyCommand*>();
pressedCommandList = LinkedList<int>();
Keyboard.begin();

}

void loop() {
cPrintln("\nNew loop...");
while (toPressKeysList.size() > 0) {
String nextCommand = toPressKeysList.shift();
char todo[nextCommand.length()];
nextCommand.toCharArray(todo, nextCommand.length() + 1);
cPrintln("To Print: " + nextCommand);
for (int i = 0; i < sizeof(todo); i++) {
void loop(){
cPrintln("\nNew loop...");
while(toPressKeysList.size() > 0){
KeyCommand * nextCommand = toPressKeysList.shift();
int commandNumber = nextCommand->getCommandNumber();
if(!isCommandPressed(commandNumber)){
String command = (nextCommand -> getCommand());
command.replace("f-1", "!");
command.replace("ctrl-2", "@");
char todo[command.length()];
command.toCharArray(todo, command.length()+1);
cPrintln("To Print: " + command);
for(int i = 0; i < sizeof(todo); i++){
if(todo[i] == '!'){
cPrintln("Pressing: F1");
Keyboard.press(KEY_F1);
delay(keyPressDuration);
Keyboard.release(KEY_F1);
}else if(todo[i] == '@'){
cPrintln("Pressing: Ctrl+2");
Keyboard.press(KEY_LEFT_CTRL);
Keyboard.press('2');
delay(500);
Keyboard.releaseAll();
}else{
cPrintln("Pressing: " + String(todo[i]));
Keyboard.press(todo[i]);
delay(keyPressDuration);
Keyboard.release(todo[i]);
}
}
}
delay(keyPressDuration-20);
}
pressedCommandList.add(commandNumber);
Serial1.print(keyPrefix + commandNumber + suffix);
}
}

cPrintln("Waiting for data");
while (!Serial1.available()); //等待数据传送过来,若没有数据,一直等待
cPrintln("Reading data");

String str = Serial1.readString();
cPrintln("Fininshed reading");
cPrintln("Raw: " + str);
int prefixIndex = str.indexOf(keyPrefix);
int suffixIndex = str.indexOf(suffix);
String commandNumber = "";
if (prefixIndex >= 0 && suffixIndex >= 0) {
str = str.substring(prefixIndex + sizeof(keyPrefix) - 1, suffixIndex);
int splitterIndex = str.indexOf(splitter);
commandNumber = str.substring(splitterIndex + 1);
str = str.substring(0, splitterIndex);
Serial.println(str + ":" + commandNumber);
toPressKeysList.add(str);
Serial1.print(keyPrefix + commandNumber + suffix);
}
delay(1);
cPrintln("Waiting for data");
while(!Serial1.available());//等待数据传送过来,若没有数据,一直等待
String str = Serial1.readString();
cPrintln("Fininshed reading");
cPrintln("Raw: "+str);
int prefixIndex = str.indexOf(keyPrefix);
int suffixIndex = str.indexOf(suffix);
String commandNumber = "";
if(prefixIndex >= 0 && suffixIndex >= 0){
str = str.substring(prefixIndex + sizeof(keyPrefix) - 1,suffixIndex);
int splitterIndex = str.indexOf(splitter);
commandNumber = str.substring(splitterIndex + 1);
str = str.substring(0,splitterIndex);
Serial.println(str + ":" + commandNumber);
toPressKeysList.add(new KeyCommand(commandNumber.toInt(),str));
}
delay(1);
}

void cPrintln(String str) {
Serial.println(prefix + str + suffix);
}
boolean isCommandPressed(int commandNumber){
for(int i = 0; i < pressedCommandList.size(); i++){
if(commandNumber == pressedCommandList.get(i))
return true;
}
return false;
}

void cPrintln(String str){
Serial.println(prefix + str + suffix);
}
@@ -6,22 +6,22 @@
#define suffix "*"
#define spliter ","
#define lengthOfRxTxData 40
#define keyPressDuration 10
#define keyPressDuration 40
#define transferTime 50
class KeyCommand {
public:
KeyCommand(int commandNumber, String command) {
this - > _commandNumber = commandNumber;
this - > _command = command;
this -> _commandNumber = commandNumber;
this -> _command = command;
}
int getCommandNumber() {
return this - > _commandNumber;
return this -> _commandNumber;
}
String getCommand() {
return this - > _command;
return this -> _command;
}
String toString() {
return this - > _command + spliter + String(this - > _commandNumber);
return this -> _command + spliter + String(this -> _commandNumber);
}
private:
int _commandNumber;
@@ -51,39 +51,41 @@ void loop() {
if (str.indexOf(keyPrefix) >= 0 && str.indexOf(suffix) > 0) {
KeyCommand * command = new KeyCommand(commandCounter++, str.substring(str.indexOf(keyPrefix) + 1, str.indexOf(suffix)));
commandList.add(command);
Serial1.print(keyPrefix + command -> toString() + suffix);
}
}

// 暂时移除第二三次握手验证
// 如果有未处理的指令
if (commandList.size() > 0) {
KeyCommand * nextCommand = commandList.get(0);
Serial.println(nextCommand - > toString());
Serial1.print(keyPrefix + nextCommand - > toString() + suffix);

// 等待输出
int commandLength = (nextCommand - > getCommand()).length();
delay(keyPressDuration * (commandLength) + transferTime);

// 如果有返回的状态
if (Serial1.available()) {
String str = Serial1.readString();
if (str.indexOf(keyPrefix) >= 0 && str.indexOf(suffix) > 0) {
str = str.substring(str.indexOf(keyPrefix) + 1, str.indexOf(suffix));
// 从队列中将该编号命令移除
int commandNumber = str.toInt();
for (int i = 0; i < commandList.size(); i++) {
if (commandList.get(i) - > getCommandNumber() == commandNumber) {

cPrintln("Command completed: " + commandList.get(i) - > toString());
lcdPrint("Return: " + String(commandNumber));
commandList.remove(i);
break;
}
}
}
}

}
// if (commandList.size() > 0) {
// KeyCommand * nextCommand = commandList.get(0);
// Serial.println(nextCommand -> toString());
// Serial1.print(keyPrefix + nextCommand -> toString() + suffix);
//
// // 等待输出
// int commandLength = (nextCommand -> getCommand()).length();
// delay(keyPressDuration * (commandLength) + transferTime);
//
// // 如果有返回的状态
// if (Serial1.available()) {
// String str = Serial1.readString();
// if (str.indexOf(keyPrefix) >= 0 && str.indexOf(suffix) > 0) {
// str = str.substring(str.indexOf(keyPrefix) + 1, str.indexOf(suffix));
// // 从队列中将该编号命令移除
// int commandNumber = str.toInt();
// for (int i = 0; i < commandList.size(); i++) {
// if (commandList.get(i) -> getCommandNumber() == commandNumber) {
//
// cPrintln("Command completed: " + commandList.get(i) -> toString());
// lcdPrint("Return: " + String(commandNumber));
// commandList.remove(i);
// break;
// }
// }
// }
// }
//
// }

}

@@ -118,4 +120,4 @@ void lcdPrint(String str) {
} else {
lcd.print(str);
}
}
}
@@ -13,25 +13,44 @@
private SerialCommunicationService serialCommunicationService;

@RequestMapping(value = "/init", method = {RequestMethod.GET})
public Object init(){
public Object init() {
serialCommunicationService.init();
return "started";
}

@RequestMapping(value = "/pause", method = {RequestMethod.GET})
public Object pause(){
public Object pause() {
serialCommunicationService.pauseLogging();
return "paused";
}

@RequestMapping(value = "/resume", method = {RequestMethod.GET})
public Object resume(){
public Object resume() {
serialCommunicationService.resumeLogging();
return "resumed";
}

@RequestMapping(value = "/write", method = {RequestMethod.GET})
public Object write(@RequestParam("str") String str){
public Object write(@RequestParam("str") String str) {
serialCommunicationService.writeString(str);
return str;
}

@RequestMapping(value = "/press", method = {RequestMethod.GET})
public Object press(@RequestParam("str") String str) {
str = str.toLowerCase();
str = str.equals("a") ? " " : str;
str = str.equals("b") ? "c" : str;
str = str.equals("x") ? "f" : str;
str = str.equals("y") ? "r" : str;
str = str.equals("up") ? "f-1" : str;
str = str.equals("down") ? "2" : str;
str = str.equals("left") ? "1" : str;
str = str.equals("right") ? "3" : str;
str = str.equals("l") ? "q" : str;
str = str.equals("r") ? "e" : str;

str = "@" + str + "*";
serialCommunicationService.writeString(str);
return str;
}

0 comments on commit a27db95

Please sign in to comment.
You can’t perform that action at this time.