-
Notifications
You must be signed in to change notification settings - Fork 5
/
LCD (Mega Only).ino
190 lines (170 loc) · 4.72 KB
/
LCD (Mega Only).ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// By Chris Andriessen
// Requires second Arduino UNO or MEGA for pendant and another MEGA for LCD.
// Needs Serial TX from pendant Arduino to Serial RX on GRBL Arduino connected.
// Needs Serial RX(pin 10) from LCD Arduino to Serial TX on GRBL Arduino connected.
// Can be used while connected to PC with Gcode sender software.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SoftwareSerial.h>
#include <Regexp.h>
LiquidCrystal_I2C lcd(0x27, 20, 4);
SoftwareSerial mySerial(10, 11); // RX, TX
String data = "";
String Posd = "";
void setup()
{
mySerial.begin(115200);
lcd.begin();
drawEmpty();
}
void loop() // run over and over
{
while(mySerial.available()==0)
{}
char character;
while(mySerial.available()>0)
{
character = mySerial.read();
data += character;
}
getStatus();
getPOS();
data = "";
}
void drawEmpty(){
lcd.setCursor(0,0);
lcd.print("X: 0.000 ");
lcd.setCursor(0,1);
lcd.print("Y: 0.000 ");
lcd.setCursor(0,2);
lcd.print("Z: 0.000 ");
lcd.setCursor(0,3);
lcd.print("FS: 0 ");
lcd.setCursor(11,3);
lcd.print(">: NoData");
}
void getStatus(){
String str = data;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
char buf [100];
MatchState ms;
ms.Target (char_array);
char result = ms.Match ("<([A-z]+)\|", 0);
if (result == REGEXP_MATCHED){
lcd.setCursor(11,3);
lcd.print(">: ");
lcd.print(ms.GetCapture (buf,0));
String buflen(ms.GetCapture (buf,0));
int len = 9 - (3 + buflen.length());
while (len > 0) {
lcd.print(" ");
len -= 1;
}
}
buf [100] = "";
}
void getPOS(){
String str = data;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
char buf [100];
MatchState ms;
ms.Target (char_array);
char result = ms.Match ("MPos:((.*))\|FS", 0);
Posd = ms.GetCapture (buf,0);
getX();
getY();
getZ();
getFeed();
}
void getX(){
String str = Posd;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
char buf [100];
MatchState ms;
ms.Target (char_array);
char result = ms.Match ("([0-9-]+[.][0-9]+)", 0);
if (result == REGEXP_MATCHED){
lcd.setCursor(0,0);
lcd.print("X: ");
lcd.print(ms.GetCapture (buf,0));
String buflen(ms.GetCapture (buf,0));
int len = 20 - (3 + buflen.length());
while (len > 0) {
lcd.print(" ");
len -= 1;
}
}
buf [100] = "";
}
void getY(){
String str = Posd;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
char buf [100];
MatchState ms;
ms.Target (char_array);
char result = ms.Match (",([0-9-]+[.][0-9]+),", 0);
if (result == REGEXP_MATCHED){
lcd.setCursor(0,1);
lcd.print("Y: ");
lcd.print(ms.GetCapture (buf,0));
String buflen(ms.GetCapture (buf,0));
int len = 20 - (3 + buflen.length());
while (len > 0) {
lcd.print(" ");
len -= 1;
}
}
buf [100] = "";
}
void getZ(){
String str = Posd;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
char buf [100];
MatchState ms;
ms.Target (char_array);
char result = ms.Match ("([0-9-]+[.][0-9]+)$", 0);
if (result == REGEXP_MATCHED){
lcd.setCursor(0,2);
lcd.print("Z: ");
lcd.print(ms.GetCapture (buf,0));
String buflen(ms.GetCapture (buf,0));
int len = 20 - (3 + buflen.length());
while (len > 0) {
lcd.print(" ");
len -= 1;
}
}
buf [100] = "";
}
void getFeed(){
String str = data;
int str_len = str.length() + 1;
char char_array[str_len];
str.toCharArray(char_array, str_len);
char buf [100];
MatchState ms;
ms.Target (char_array);
char result = ms.Match ("FS:([0-9]+),[0-9]+", 0);
if (result == REGEXP_MATCHED){
lcd.setCursor(0,3);
lcd.print("FS: ");
lcd.print(ms.GetCapture (buf,0));
String buflen(ms.GetCapture (buf,0));
int len = 10 - (3 + buflen.length());
while (len > 0) {
lcd.print(" ");
len -= 1;
}
}
buf [100] = "";
}