Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added functionality to invoke key presses over serial. #165

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 58 additions & 7 deletions h4/h4.ino
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ const float LINEAR_INTERPOLATION_PRECISION = 0.1; // 0 < x <= 1, smaller values
const long GCODE_WAIT_EPSILON_STEPS = 10;

// To be incremented whenever a measurable improvement is made.
#define SOFTWARE_VERSION 9
#define SOFTWARE_VERSION 10

// To be changed whenever a different PCB / encoder / stepper / ... design is used.
#define HARDWARE_VERSION 4
Expand Down Expand Up @@ -265,6 +265,9 @@ bool inNumpad = false;
int numpadDigits[20];
int numpadIndex = 0;

#include <queue>
std::queue<int> keyEventQueue;

bool isOn = false;
bool nextIsOn; // isOn value that should be applied asap
bool nextIsOnFlag; // whether nextIsOn requires attention
Expand Down Expand Up @@ -1334,8 +1337,41 @@ void taskMoveA1(void *param) {
}

void taskGcode(void *param) {

while (emergencyStop == ESTOP_NONE) {
if (mode != MODE_GCODE) {

int charCode = 0;
char receivedChar = 0;
bool keyEmulation = false;
if (Serial.available() > 0) {

receivedChar = Serial.read();
charCode = int(receivedChar);
if (receivedChar == '=' /* keycode commands*/) {
keyEmulation = true;
// read the rest of the command from the Serial interface
String command = Serial.readStringUntil('\n');

// split the command by comma
int commaIndex = command.indexOf(',');
while (commaIndex != -1) {
String keycodeStr = command.substring(0, commaIndex);
int keycode = keycodeStr.toInt();
// add the keycode to the array for processKeypadEvent to handle
keyEventQueue.push(keycode);

// remove the processed keycode from the command
command = command.substring(commaIndex + 1);
commaIndex = command.indexOf(',');
}

// process the last (or only) keycode
int keycode = command.toInt();
keyEventQueue.push(keycode);
}
}

if (mode != MODE_GCODE && !keyEmulation) {
gcodeInitialized = false;
taskYIELD();
continue;
Expand All @@ -1349,9 +1385,7 @@ void taskGcode(void *param) {
gcodeInSemicolon = false;
}
// Implementing a relevant subset of RS274 (Gcode) and GRBL (state management) covering basic use cases.
if (Serial.available() > 0) {
char receivedChar = Serial.read();
int charCode = int(receivedChar);
if (!keyEmulation) {
if (gcodeInBrace) {
if (receivedChar == ')') gcodeInBrace = false;
} else if (receivedChar == '(') {
Expand All @@ -1378,6 +1412,8 @@ void taskGcode(void *param) {
Serial.print(round(gcodeFeedDuPerSec * 60 / 10000.0));
Serial.print(",");
Serial.print(getApproxRpm());
Serial.print("|");
Serial.print("V:" + String(SOFTWARE_VERSION));
Serial.print(">"); // no new line to allow client to easily cut out the status response
} else if (isOn) {
if (gcodeInBrace && charCode < 32) {
Expand All @@ -1403,6 +1439,7 @@ void taskGcode(void *param) {
// to flush any commands coming after an error
}
}
keyEmulation = false;
taskYIELD();
}
vTaskDelete(NULL);
Expand Down Expand Up @@ -2259,8 +2296,22 @@ bool processNumpadResult(int keyCode) {
}

void processKeypadEvent() {
if (keypad.available() == 0) return;
int event = keypad.getEvent();
// process keycodes from keyEventArray
if (keyEventQueue.size() > 0) {
int event = keyEventQueue.front(); // Get the first event
keyEventQueue.pop(); // Remove the first event

processKeyEvent(event);
return;
}

if (keypad.available() > 0) {
int event = keypad.getEvent();
processKeyEvent(event);
}
}

void processKeyEvent(int event) {
int keyCode = event;
bitWrite(keyCode, 7, 0);
bool isPress = bitRead(event, 7) == 1; // 1 - press, 0 - release
Expand Down