Skip to content

Instantly share code, notes, and snippets.

@arduinoboard
Created May 20, 2018 16:17
Show Gist options
  • Save arduinoboard/9a3ecd1134210618d3b5737bcd82165b to your computer and use it in GitHub Desktop.
Save arduinoboard/9a3ecd1134210618d3b5737bcd82165b to your computer and use it in GitHub Desktop.
The file that is currently on an Arduino/Genuino Mega or Mega 2560 with a serial number of 55731323136351C08001
void gReset() {
Serial1.write(0x18);
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gZero() {
Serial1.println("G0 X0.00 Y0.00 Z0.00 F20\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gHome() {
Serial1.println("$H\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gPause() {
Serial1.println("!\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gResume() {
Serial1.println("~\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gXJog() {
Serial1.println("G91 G0 X1.00 F10\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gXMove(float Xmove) {
Serial1.print("G20");
Serial1.print(" G91 G0 X");
Serial1.print(Xmove);
Serial1.println(" F10\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gCmd(char gCmd) {
Serial1.println("gCmd\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gYJog() {
Serial1.println("G0 Y1.00 F10\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gZJog() {
Serial1.println("G0 Z1.00 F10\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gMXJog() {
Serial1.println("G91 G0 X-1.00 F10\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gMYJog() {
Serial1.println("G0 Y-1.00 F10\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gMZJog() {
Serial1.println("G0 Z-1.00 F10\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void gUnlock() {
Serial1.println("$X\n");
delay(250);
clearPrintTitle();
displayMenu(currentMenuItem);
}
void dro ()
{
if (millis() - grblPollingTimer > grblPollingPeriod) // GRBL status timout
{
if (!interParModeAuto) {
grblStatusRequest();
}
grblStatusTimeToDisplay = true;
grblPollingTimerRestart();
}
serial1Event(); // GRBL communications
if (grblStringAvailable)
{
grblStringAvailable = false;
grblRxEvaluation(grblStringRx);
grblStringRx = "";
grblActivityTimerRestart();
}
serialEvent();
if (monCommandNew) // Arduino IDE monitor
{
monCommTreatment(monCommand);
monCommand = "";
monCommandNew = false;
}
}
//------------------------------------ GRBL subroutines
void grblStatusRequest()
{
if (!grblActivityIfActive())
{
//Serial.println("Status ?");
grblCmdSend("?");
}
}
void grblPollingTimerRestart()
{
grblPollingTimer = millis();
}
void grblActivityWatchdogSet()
{
grblActivityWatchdogPeriod = grblPollingPeriod - 50;
}
void grblCmdSend(String txCmd)
{
if (!interParModeAuto)
{
digitalWrite(grblTxSwitch, HIGH);
delay(50);
Serial1.print(txCmd + '\r');
delay(50);
digitalWrite(grblTxSwitch, LOW);
}
else
{
Serial.println("Mode auto! (" + txCmd + ")") ;
}
}
void grblRxEvaluation(String grblRx)
{
if (grblRx.startsWith("<")) // Status GRBL
{
int startStatPos = grblRx.indexOf("<"); // Cleans the status line.
int endStatPos = grblRx.indexOf(">");
grblRx = grblRx.substring(startStatPos + 1, endStatPos - 0);
if (grblRx.indexOf("|") >= 0) // GRBL version Since version 1.1 (S11)
{
grblVersion = grblVersionSince11;
grblStatusRxS11(grblRx);
}
else
{
grblVersion = grblVersionUnknown;
}
}
else if (grblRx == "ok")
{
}
else
{
Serial.println("!!! GRBL " + grblRx);
}
}
/*------------------------------------------------------------------------------
The status line can take these various states:
$10=1
<Idle|MPos:0.000,0.000,0.000|FS:0,0|WCO:0.000,0.000,0.000>
<Idle|MPos:0.000,0.000,0.000|FS:0,0|Ov:100,100,100>
<Idle|MPos:0.000,0.000,0.000|FS:0,0>
$10=2
<Idle|WPos:0.000,0.000,0.000|Bf:15,128|FS:0,0|WCO:0.000,0.000,0.000>
<Idle|WPos:0.000,0.000,0.000|Bf:15,128|FS:0,0|Ov:100,100,100>
<Idle|WPos:0.000,0.000,0.000|Bf:15,128|FS:0,0>
--------------------------------------------------------------------------------
*/
void grblStatusRxS11(String grblStat)
{
static boolean statNew;
static byte statNoNew;
String grblStatOrigin = grblStat; // For Status values array
grblStat.toUpperCase();
/*-----------------------------------------------------------
Separate data after FS:, Ov: WCO: Pn: etc.(see GRBL doc)
as StatusExtra for treatment
'*-----------------------------------------------------------
*/
int fsPos = grblStat.indexOf("|F"); // Search the end of status standard status data
fsPos = grblStat.indexOf("|", fsPos + 2);
grblStatusExtra = "";
if (fsPos >= 0)
{
grblStatusExtra = grblStat.substring(fsPos + 1); // Extract the extra status data
}
grblStat = grblStat.substring(0, fsPos); // Delete extra data from status line
grblS11PosMode = grblS11posMach;
if (grblStat.indexOf("WPOS") >= 0) {
grblS11PosMode = grblS11posWork; // Find if a Mpos or a Wpos status line
}
grblStat.replace("BF:", ""); // clean up unnecessary data
grblStat.replace("|", ",");
grblStat.replace("MPOS:", "");
grblStat.replace("WPOS:", "");
grblStat.replace("FS:", "");
if (grudrDebugOn) {
Serial.println(grblStatOrigin);
Serial.println(grblStat);
}
if (grblStat != grblStatusPrevious)
{
grblStatusPrevious = grblStat;
statNew = true;
statNoNew = 0;
}
else
{
statNew = false;
statNoNew ++;
if (statNoNew > 10) // For if there is no new status, refresh display
{
statNoNew = 0;
statNew = true;
}
}
toSplitCommand(grblStat, ",");
for (int i = 0; i < grblStatusValuesDim; i++)
{
grblStatusValues[i] = SplittingArray[i];
}
// Fill Mpos and Wpos arrays with the same received values
grblS11AxisValIntMpos[grblS11axisX] = grblValuesStrFloatToInt(grblStatusValues[grblS11MachX]); // It is easier to work integers than floating values
grblS11AxisValIntMpos[grblS11axisY] = grblValuesStrFloatToInt(grblStatusValues[grblS11MachY]); // So I work with 1/100 mm
grblS11AxisValIntMpos[grblS11axisZ] = grblValuesStrFloatToInt(grblStatusValues[grblS11MachZ]); // I get this -0.200,3.010,10.000 from GRBL
// I put this -20,301,1000 in array
grblS11AxisValIntWpos[grblS11axisX] = grblS11AxisValIntMpos[grblS11axisX];
grblS11AxisValIntWpos[grblS11axisY] = grblS11AxisValIntMpos[grblS11axisY];
grblS11AxisValIntWpos[grblS11axisZ] = grblS11AxisValIntMpos[grblS11axisZ];
if (grblStatusExtra != "")
{
if (grblStatusExtra.startsWith("WCO:"))
{
grblStatusExtraWco = grblStatusExtra;
}
if (grudrDebugOn) {
Serial.println("Extra: " + grblStatusExtra);
}
}
grblStatusWcoTreatment(grblStatusExtraWco); // Add WCO values to Mpos or Wpos values
if (grblStatusTimeToDisplay && statNew)
{
Serial.println("Stat: " + grblStat);
grblStatusDisplay();
if (interParDroConnected)
{
droStatusGrblU11send();
droStatusOtherSend();
}
grblStatusTimeToDisplay = false;
}
}
void grblStatusWcoTreatment(String wcoValues) // WCO:0.000,0.000,0.000
{
wcoValues.replace("WCO:", "");
toSplitCommand(wcoValues, ",");
for (int i = 0; i < grblS11WcoLasted; i++)
{
grblS11WcoValInt[i] = grblValuesStrFloatToInt(SplittingArray[i]);
}
switch (grblS11PosMode)
{
case grblS11posMach: // If Machine positions received create working position
grblS11AxisValIntWpos[grblS11axisX] -= grblS11WcoValInt[grblS11WcoX];
grblS11AxisValIntWpos[grblS11axisY] -= grblS11WcoValInt[grblS11WcoY];
grblS11AxisValIntWpos[grblS11axisZ] -= grblS11WcoValInt[grblS11WcoZ];
// Moves Speed and Spindle to their array position.
grblStatusValues[grblS11Feed] = grblStatusValues[grblS11BufBlocks];
grblStatusValues[grblS11Spindle] = grblStatusValues[grblS11BufBytes];
grblStatusValues[grblS11BufBlocks] = "-";
grblStatusValues[grblS11BufBytes] = "-";
break;
case grblS11posWork: // If Machine positions received creat working position
grblS11AxisValIntMpos[grblS11axisX] += grblS11WcoValInt[grblS11WcoX];
grblS11AxisValIntMpos[grblS11axisY] += grblS11WcoValInt[grblS11WcoY];
grblS11AxisValIntMpos[grblS11axisZ] += grblS11WcoValInt[grblS11WcoZ];
break;
}
}
long grblValuesStrFloatToInt(String strFloatValue)
{
return (strFloatValue.toFloat()) * pow(10, grblFormatDecimalNumbers);
}
String grblValuesIntToStrFloat(float IntValue)
{
float retVal = (float)IntValue / pow(10, grblFormatDecimalNumbers);
//float retVal = (float)IntValue;
return (String)(retVal,grblFormatDecimalNumbers);
}
void grblActivityTimerRestart()
{
grblActivityWatchdog = millis();
}
boolean grblActivityIfActive()
{
boolean actStat = true;
if (millis() - grblActivityWatchdog > grblActivityWatchdogPeriod) {
actStat = false;
}
return actStat;
}
void grblStatusDisplay()
{
static int actualVersion;
if (grblVersion != actualVersion)
{
lcdCls();
actualVersion = grblVersion;
}
switch (grblVersion)
{
case grblVersionSince11:
grblStatusDisplayS11();
break;
default:
lcdCls();
lcdPrint("Waiting data", 0, 2);
break;
}
}
void grblStatusDisplayS11()
{
static int actualPosMode;
if (grblS11PosMode != actualPosMode)
{
lcdCls();
actualPosMode = grblS11PosMode;
}
switch (grblS11PosMode)
{
case grblS11posMach:
grblS11PosModeStr = "m";
// lcdPrintLength("F" + grblStatusValues[grblS11Feed], 6, 0, 5);
// lcdPrintLength("S" + grblStatusValues[grblS11Spindle], 12, 0, 7);
break;
case grblS11posWork:
grblS11PosModeStr = "w";
// lcdPrintLength("B" + grblStatusValues[grblS11BufBlocks], 6, 0, 3);
// lcdPrintLength("F" + grblStatusValues[grblS11Feed], 10, 0, 4);
// lcdPrintLength("S" + grblStatusValues[grblS11Spindle], 15, 0, 5);
break;
default:
grblS11PosModeStr = "? ";
break;
}
lcdPrintLength(grblStatusValues[grblS11ValStatus], 12,0, 6);
//lcdPrintLength(grblS11PosModeStr, 19,3, 1);
//lcdPrintLength("Xwm " + grblValuesIntToStrFloat(grblS11AxisValIntWpos[grblS11axisX]), 0,0, 11); lcdPrintLength(grblValuesIntToStrFloat(grblS11AxisValIntMpos[grblS11axisX]), 12, 1, 8);
//lcdPrintLength("Xwm " + grblValuesIntToStrFloat(grblS11AxisValIntWpos[grblS11axisX]), 0,0, 11);
lcdPrintLength(grblValuesIntToStrFloat(grblS11AxisValIntMpos[grblS11axisX]), 0, 0, 11);
/// lcdPrintLength("Ywm " + grblValuesIntToStrFloat(grblS11AxisValIntWpos[grblS11axisY]), 0,2, 11); lcdPrintLength(grblValuesIntToStrFloat(grblS11AxisValIntMpos[grblS11axisY]), 12, 2, 8);
/// lcdPrintLength("Zwm " + grblValuesIntToStrFloat(grblS11AxisValIntWpos[grblS11axisZ]), 0,3, 11); lcdPrintLength(grblValuesIntToStrFloat(grblS11AxisValIntMpos[grblS11axisZ]), 12, 3, 7);
}
void droStatusGrblU11send()
{
//---------------------------- GRBL U11 Status
droSendValues("GSTAT", 1, grblStatusValues[grblS11ValStatus]);
droSendValues("GBUFF", 1, grblStatusValues[grblS11BufBlocks]);
droSendValues("GFEED", 1, grblStatusValues[grblS11Feed]);
droSendValues("GSPIN", 1, grblStatusValues[grblS11Spindle]);
//---------------------------- Machine positions
droSendValues("GMX", 1, grblValuesIntToStrFloat(grblS11AxisValIntMpos[grblS11axisX]));
droSendValues("GMY", 1, grblValuesIntToStrFloat(grblS11AxisValIntMpos[grblS11axisY]));
droSendValues("GMZ", 1, grblValuesIntToStrFloat(grblS11AxisValIntMpos[grblS11axisZ]));
//---------------------------- Working positions
droSendValues("GWX", 1, grblValuesIntToStrFloat(grblS11AxisValIntWpos[grblS11axisX]));
droSendValues("GWY", 1, grblValuesIntToStrFloat(grblS11AxisValIntWpos[grblS11axisY]));
droSendValues("GWZ", 1, grblValuesIntToStrFloat(grblS11AxisValIntWpos[grblS11axisZ]));
droSendValues("GWCO", 1, grblValuesIntToStrFloat(grblS11WcoValInt[grblS11WcoX]) + " " + grblValuesIntToStrFloat(grblS11WcoValInt[grblS11WcoY]) + " " +
grblValuesIntToStrFloat(grblS11WcoValInt[grblS11WcoZ]));
droSendValues("GMODE", 1, grblS11PosModeStr);
}
void droStatusOtherSend()
{
String modeAuto = "On";
if (!interParModeAuto) {
modeAuto = "Off";
}
droSendValues("GPAUTO", 1, modeAuto);
}
void droSendValues(String droName, int droIndex, String droValue)
{
if (interParDroConnected)
{
String droCommand = "";
droCommand = interParDroIdentity + "," + droName + "," + String(droIndex) + "," + droValue + "\n";
Serial.print(droCommand);
}
}
/*------------------------------------ Monitor subroutines ------------------------------------
' You can send commands to the program by IDE monitor command line or, if connected, by the DRO
' for internal parameters or, in transit, for GRBL
'
' commRx: GRBL/xxxxx/ for GRBL command
' INTERNAL/xxxxxx for internal command
'
' GRBL/G91 G0 X12 Y12 Z5
' GRBL/G90 G0 X0 Y0 z0
' INTERNAL/DROconnected/0 INTERNAL/GRBLPOLLPER/250
' INTERNAL/MODEAUTO/0
'----------------------------------------------------------------------------------------------
*/
void monCommTreatment(String commRx)
{
commRx.toUpperCase();
toSplitCommand(commRx, "/"); // To put received parameters in array
String destDevice = SplittingArray[0]; // Destination device
if (destDevice == "GRBL")
{
grblCmdSend(SplittingArray[1]);
}
else if (destDevice == "INTERNAL") // INTERNAL/POLLstat/0
{
interCmdExecute(SplittingArray[1], SplittingArray[2]);
}
else
{
Serial.println("Unknown dest. device!! " + destDevice);
}
}
//------------------------------------ Internal subroutines
void interCmdExecute(String intCmdFct, String intCmdPar1)
{
int cmdPar1Val = intCmdPar1.toInt();
if (intCmdFct == "DROCONNECTED") // Connect or disconnect DRO
{
if (cmdPar1Val == 1) {
interParDroConnected = true;
} else {
interParDroConnected = false;
}
}
else if (intCmdFct == "GRBLPOLLPER")
{
grblPollingPeriod = cmdPar1Val;
}
else if (intCmdFct == "MODEAUTO")
{
if (cmdPar1Val == 1) {
interParModeAuto = true;
} else {
interParModeAuto = false;
}
}
else
{
Serial.println("Internal cmd " + intCmdFct + " !!");
}
}
//------------------------------------ Communication subroutines
void serial1Event() // GRBL serial
{
while (Serial1.available())
{
char MinChar = (char)Serial1.read(); // Char received from GRBL
if (MinChar == '\n')
{
grblStringAvailable = true;
}
else
{
if (MinChar >= ' ') {
grblStringRx += MinChar; // >= ' ' to avoid not wanted ctrl char.
}
}
}
}
void serialEvent() // IDE monitor or DRO serial
{
while (Serial.available())
{
char monChar = (char)Serial.read(); // Char received from IDE monitor
if (monChar == '\n') // If new line char received = end of command line
{
monCommandNew = true;
}
else
{
if (monChar >= ' ') {
monCommand += monChar; // >= ' ' to avoid not wanted ctrl char.
}
}
}
}
//---------------------------------------------- Tools section
/*-----------------------------------------------------------------------------------------------------------------------------
' toSplitCommand for to split received string delimited with expected char separator par1, par2, par3, .....
' and put them in an array (SplittingArray)
'-----------------------------------------------------------------------------------------------------------------------------
*/
void toSplitCommand(String SplitText, String SplitChar) {
SplitText = SplitChar + SplitText + SplitChar;
int SplitIndex = -1;
int SplitIndex2;
for (int i = 0; i < SplittingArrayDim - 1; i++) {
SplitIndex = SplitText.indexOf(SplitChar, SplitIndex + 1);
SplitIndex2 = SplitText.indexOf(SplitChar, SplitIndex + 1);
if (SplitIndex < 0 || SplitIndex2 < 0) {
break;
}
if (SplitIndex2 < 0) SplitIndex2 = SplitText.length() ;
SplittingArray[i] = SplitText.substring(SplitIndex + 1, SplitIndex2);
}
}
//////////////////////////////////////////////////////////////////////////////////
String lcdEmptyLine = " ";
void lcdClsRow(int rowNum)
{
lcdPrint(lcdEmptyLine, 0, rowNum);
}
void lcdCls()
{
lcd.clear();
}
void lcdPrint(String lcdText, int lcdCol, int lcdRow)
{
lcd.setCursor(lcdCol, lcdRow);
lcd.print(lcdText);
}
void lcdPrintLength(String lcdText, int lcdCol, int lcdRow, int textLength)
{
String textToLength = lcdText + lcdEmptyLine;
textToLength = textToLength.substring(0, textLength);
lcdPrint(textToLength, lcdCol, lcdRow);
}
/* YET TO BE IMPLIMENTED!!!!
* TODO:
*
* 1) Figure out how to collect just the response to position query.
* 2) Figure out how to split the response into usable data parts.
* 3) Figure out spacing and alignment to sho X, Y, Z and Feed rates.
* 4) Use the backlight pin to flash screen in case of alarm.
* 5) Figure out how to actually flash the lcd backlight automatically when alarm is found.
* 6) Disable lcd backlight flashing once button is pressed
* 7) Figure out some sort of ascii art or soemthing that's animated when machine working and non-animated with idle.. perhaps a series of updaces to last character on screen...
* I.E. sequentially update last character with these: | \ - / | that should make a spining icon..
*
* 8) Do all of featuer 7, but space, and use actual text for idle, alarm, pause, working etc for larger screens (20x4)
*/
//Command to get position and state from grbl
void gPos() {
Serial1.println("?\n");
delay(2500);
// clearPrintTitle();
// displayMenu(currentMenuItem);
while (Serial1.available()) {
char PosInByte = Serial1.read();
// Serial1.write(PosInByte);
lcd.print(PosInByte);
}
}
#include <LiquidCrystal.h>
#define SERIAL_BUFFER_SIZE 256
// DRO Begin
#define grblFormatDecimalNumbers 3
enum grblS11StatusValuesIndex {grblS11ValStatus, grblS11MachX, grblS11MachY, grblS11MachZ, grblS11BufBlocks, grblS11BufBytes, grblS11Feed, grblS11Spindle,
grblS11WorkX, grblS11WorkY, grblS11WorkZ, grblS11ValLasted};
const int grblStatusValuesDim = grblS11ValLasted;
String grblStatusValues[grblS11ValLasted];
enum grblS11AxisValFloatIndex {grblS11axisX, grblS11axisY, grblS11axisZ, grblS11axisLasted};
long grblS11AxisValIntMpos[grblS11axisLasted];
long grblS11AxisValIntWpos[grblS11axisLasted];
enum grblS11WcoValuesIndex {grblS11WcoX, grblS11WcoY, grblS11WcoZ, grblS11WcoLasted};
long grblS11WcoValInt[grblS11WcoLasted]; // WCO floating values
int grblS11PosMode; // Working Pos WPos or Machine Pos MPos
String grblS11PosModeStr;
enum grblS11PosModeIndex {grblS11posMach, grblS11posWork, grblS11posLasted};
String grblStatusPrevious = ""; // thes variables to avoid sending values that do not change
boolean grblStatusTimeToDisplay; // If time to display or send GRBL status to display or DRO
// To avoid clutter in the rapid arrival of status (UGS)
String grblStatusExtraPrevious = ""; // Contain WCO: 0v: and other extra data
String grblStatusExtra;
String grblStatusExtraWco;
unsigned long grblPollingTimer = millis();
int grblPollingPeriod = 200; // For every 250 mS
unsigned long grblActivityWatchdog = millis(); // Timer for watching GRBL data line activity
int grblActivityWatchdogPeriod;
#define grblTxSwitch 7 // Electronic GRBL Tx switch command
boolean grblStringAvailable = false; // If a complete string received from GRBL
String grblStringRx = ""; // Received string
int grblVersion = 0;
enum grblVersionsIndex {grblVersionUnknown, grblVersionSince11, grblVersionLasted};
// Internal Parameters
boolean interParModeAuto = false; // Function mode program true(default): the program waits GRBL's data
// if you send a Gcode file, it is imperative to be interParModeAuto = true
// otherwise the polling might create transmission errors.
boolean interParDroConnected = true; // If a DRO connected (internal parameter false default)
String interParDroIdentity = "DROX";
// Monitor Parameters
bool monCommandNew = false; // If a new command received from IDE Arduino monitoe
String monCommand = ""; // received command
// Tools Parameters
#define SplittingArrayDim grblS11ValLasted
String SplittingArray[SplittingArrayDim];
boolean grudrDebugOn = false;
// DRO end
// Initialize the library with the numbers of the interface pins
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
//States for the menu.
int currentMenuItem = 0;
int lastState = 0;
void setup() {
Serial.begin(115200);
Serial1.begin(115200);
//Set the characters and column numbers.
lcd.begin(16, 2);
//Print default title. Purely Cosmetic.
lcd.print ("Initializing...");
delay(250);
clearPrintTitle();
}
void loop() {
//Call the main menu.
dro();
mainMenu();
//Call Serial Communication
serialComs();
}
//Print a basic header on Row 1.
void clearPrintTitle() {
lcd.clear();
lcd.setCursor(0, 0);
//lcd.print("Sinful Media CNC");
//dro();
lcd.setCursor(0, 1);
}
void mainMenu() {
//State = 0 every loop cycle.
int state = 0;
//Refresh the button pressed.
int x = analogRead (0);
//Set the Row 0, Col 0 position.
lcd.setCursor(0, 0);
//Check analog values from LCD Keypad Shield
// 100 200 400 600 800 in original sketch
if (x < 50) {
//Right jog
gXJog();
} else if (x < 195) {
//Up
state = 1;
} else if (x < 380) {
//Down
state = 2;
} else if (x < 555) {
//Left jog
gMXJog();
} else if (x < 790) {
//Select
state = 3;
}
//If we are out of bounds on the menu then reset it.
if (currentMenuItem < 0 || currentMenuItem > 12) {
currentMenuItem = 0;
}
//If we have changed Index, saves re-draws.
if (state != lastState) {
if (state == 1) {
//If Up
currentMenuItem = currentMenuItem - 1;
displayMenu(currentMenuItem);
} else if (state == 2) {
//If Down
currentMenuItem = currentMenuItem + 1;
displayMenu(currentMenuItem);
} else if (state == 3) {
//If Selected
selectMenu(currentMenuItem);
}
//Save the last State to compare.
lastState = state;
}
//Small delay
delay(5);
}
//Display Menu Option based on Index.
void displayMenu(int x) {
switch (x) {
// case 1:
// clearPrintTitle();
// lcd.print("-> Reset GRBL");
// break;
// case 2:
// clearPrintTitle();
// lcd.print("-> Zero Position");
// break;
case 1:
clearPrintTitle();
lcd.print("-> Home ");
break;
// case 4:
// clearPrintTitle();
// lcd.print("-> Pause Job");
// break;
// case 5:
// clearPrintTitle();
// lcd.print("-> Resume Job");
// break;
case 2:
clearPrintTitle();
lcd.print("-> + Right Jog");
break;
// case 7:
// clearPrintTitle();
// lcd.print("-> Y +Jog");
// break;
// case 8:
// clearPrintTitle();
// lcd.print("-> Z +Jog");
// break;
case 3:
clearPrintTitle();
lcd.print("-> - Left Jog");
break;
// case 10:
// clearPrintTitle();
// lcd.print("-> Y -Jog");
// break;
// case 11:
// clearPrintTitle();
// lcd.print("-> Z -Jog");
// break;
case 4:
clearPrintTitle();
lcd.print("-> Blade-width R");
break;
case 5:
clearPrintTitle();
lcd.print("-> Blade-width L");
break;
case 6:
clearPrintTitle();
lcd.print("-> Absolute move");
break;
case 7:
clearPrintTitle();
lcd.print("-> Incrmntl move");
break;
case 8:
clearPrintTitle();
lcd.print("-> Inches mode");
break;
case 9:
clearPrintTitle();
lcd.print("-> Metric mode");
break;
case 10:
clearPrintTitle();
lcd.print("-> Kill Alarm!");
break;
}
}
//Show the selection on Screen.
void selectMenu(int x) {
switch (x) {
// case 1:
// clearPrintTitle();
// lcd.print("GRBL Resetting...");
// gReset();
//Send command to GRBL to software reset
// break;
// case 2:
// clearPrintTitle();
// lcd.print("Zeroing POS...");
// gZero();
//Send command to GRBL to zero position
// break;
case 1:
clearPrintTitle();
lcd.print("Homing...");
gHome();
//Send command to grbl to go home
break;
// case 4:
// clearPrintTitle();
// lcd.print("Pausing...");
// gPause();
//Send command to GRBL to Pause Job Queue
// break;
// case 5:
// clearPrintTitle();
// lcd.print("Resuming...");
// gResume();
//Send command to GRBL to Resume Job Queue
// break;
case 2:
clearPrintTitle();
lcd.print("Jogging Right...");
gXJog();
//Call the function that belongs to Option 4
break;
// case 7:
// clearPrintTitle();
// lcd.print("Jogging...");
// gYJog();
// //Call the function that belongs to Option 4
// break;
// case 8:
// clearPrintTitle();
// lcd.print("Jogging...");
// gZJog();
//Call the function that belongs to Option 4
// break;
case 3:
clearPrintTitle();
lcd.print("Jogging Left...");
gMXJog();
//Call the function that belongs to Option 4
break;
// case 10:
// clearPrintTitle();
// lcd.print("Jogging...");
// gMYJog();
//Call the function that belongs to Option 4
// break;
// case 11:
// clearPrintTitle();
// lcd.print("Jogging...");
// gMZJog();
//Call the function that belongs to Option 4
// break;
case 4:
clearPrintTitle();
lcd.print("Moving Rt 3/32");
gXMove(.09375);
break;
case 5:
clearPrintTitle();
lcd.print("Moving Lft 3/32");
gXMove(-.09375);
break;
case 6:
clearPrintTitle();
lcd.print("Absolut Mvmnt...");
gCmd("G90");
//Call the function that belongs to Option 6
break;
case 7:
clearPrintTitle();
lcd.print("Incrmntl Mvmnt..");
gCmd("G91");
//Call the function that belongs to Option 7
break;
case 8:
clearPrintTitle();
lcd.print("Inches Mode...");
gCmd("G20 $13=1");
//Call the function that belongs to Option 8
break;
case 9:
clearPrintTitle();
lcd.print("Metric mode...");
gCmd("G21 $13=0");
//Call the function that belongs to Option 9
break;
case 10:
clearPrintTitle();
lcd.print("Unlocking...");
gUnlock();
//Call the function that belongs to Option 10
break;
}
}
void serialComs() {
// read from port 1 (UNO), send to port 0 (PC):
while (Serial1.available()) {
int inByte = Serial1.read();
Serial.write(inByte);
}
// read from port 0 (PC), send to port 1 (UNO):
while (Serial.available()) {
int inByte = Serial.read();
Serial1.write(inByte);
}
//static char buffer[256];
// static size_t pos; // position of next write
//while (Serial.available() && pos < sizeof buffer - 1) {
// Read incoming byte.
// char c = Serial.read();
// buffer[pos++] = c;
// Echo received message.
// if (c == '\n') { // \n means "end of message"
// buffer[pos] = '\0'; // terminate the buffer
// Serial1.write(buffer); // send echo
// pos = 0; // reset to start of buffer
// }
//}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment