-
Notifications
You must be signed in to change notification settings - Fork 0
/
DisplayRRF.ino
314 lines (253 loc) · 6.68 KB
/
DisplayRRF.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
//G10 To turn off -273.15
//M408 fanPercent shows all fans not just adjustable.
//M408 fanPercent shows 0.0 for thermostatic fans set to S0 but is clamped 0.5-1.0
extern "C"
{
#include "json.h"
}
#include "Globals.h"
#include "Connecting.h"
#include "Utility.h"
#include "MainScreen.h"
#include "MainMenu.h"
//#include "Menu.h"
#include <PinChangeInterrupt.h>
#define POLL_RATE 1000 //ms
#define CONN_TIMEOUT 8000
int ReadResponse();
bool ParseM408S1(const char* Buffer, const int BytesRead);
int MakeRequestP(PGM_P Req, char* Resp, const int Len);
PGM_P DecodeStatus(const char Code);
void ConnectingScreen();
void MainScreen();
void DrawMenu();
void setup()
{
pinMode(ENC_SW_PIN, INPUT_PULLUP);
attachPCINT(digitalPinToPCINT(ENC_SW_PIN), EncSwInt, CHANGE);
pinMode(RST_SW_PIN, INPUT_PULLUP);
attachPCINT(digitalPinToPCINT(RST_SW_PIN), ResetSwInt, CHANGE);
Serial.begin(57600);
Serial.setTimeout(CONN_TIMEOUT);
DEBUG_PRINT_P("hello world");
gLCD.begin();
gLCD.setFont(u8g2_font_5x7_tr);
UpdateConnecting();
//MENU_RESET();
//gCurrentPage = PG_MENU1;
}
void EncSwInt()
{
if (digitalRead(ENC_SW_PIN))
{
if (!(gFlags & FLAGS_ENC_SW_PREV))
gFlags |= (FLAGS_ENC_SW_PREV | FLAGS_ENC_SW);
}
else
{
gFlags &= ~FLAGS_ENC_SW_PREV;
}
}
void ResetSwInt()
{
if (digitalRead(RST_SW_PIN))
{
if (!(gFlags & FLAGS_RST_SW_PREV))
gFlags |= (FLAGS_RST_SW_PREV | FLAGS_RST_SW);
}
else
{
gFlags &= ~FLAGS_RST_SW_PREV;
}
}
void loop()
{
if (gFlags & FLAGS_RST_SW)
{
gFlags &= ~FLAGS_RST_SW;
MakeRequestP(PSTR("M999"), NULL, 0);
};
switch (gCurrentPage)
{
case PG_MAIN:
UpdateMain();
break;
case PG_CONNECTING:
UpdateConnecting();
break;
case PG_MENU1:
DrawMenu();
break;
case PG_RUNMACRO:
DrawMacrosMenu();
break;
case PG_PRINT:
DrawPrintMenu();
break;
case PG_BABYSTEP:
DrawBabyStepMenu();
break;
}
}
void RateLimit()
{
static int Timer = 0;
const int Now = millis();
if (Now - Timer < POLL_RATE)
delay(POLL_RATE - (Now - Timer));
Timer = millis();
}
int MakeRequestP(PGM_P Req, char* Resp, const int Len)
{
uint8_t val;
RateLimit();
#ifndef DEBUG_NO_DATA
//Clear out anything that came in between requests. Maybe there is an "ok" from a previous request?
while (Serial.available())
Serial.read();
while (true)
{
val = pgm_read_byte(Req);
if (!val)
break;
Serial.write(val);
Req++;
}
Serial.write('\n');
if (Resp)
{
const size_t BytesRead = Serial.readBytesUntil('\n', Resp, Len-1);
if (BytesRead < 1)
return 0;//Timeout or some other mess
Resp[BytesRead] = 0;
return BytesRead+1;
}
#else
return 0;
#endif
}
int MakeRequest(const char* Req, char* Resp, const int Len)
{
RateLimit();
//Clear out anything that came in between requests. Maybe there is an "ok" from a previous request?
while (Serial.available())
Serial.read();
Serial.println(Req);
if (Resp)
{
const size_t BytesRead = Serial.readBytesUntil('\n', Resp, Len-1);
if (BytesRead < 1)
return 0;//Timeout or some other mess
Resp[BytesRead] = 0;
return BytesRead+1;
}
return 0;
}
bool ParseM408S1(const char* Buffer, const int BytesRead)
{
char* jstate;
unsigned char v1type;
char* v1begin;
int v1len;
unsigned char v2type;
char* v2begin;
int v2len;
char result = json_obj(&jstate, Buffer, &v1type, &v1begin, &v1len, &v2type, &v2begin, &v2len);
char* astate;
unsigned char Index;
char** PosIter;
gData.MS.FractionPrinted = NULL;
while (result > 0)
{
if (strcmpJP(PSTR("myName"), v1begin, v1len) == 0)
{
strcpyJ(gPrinterName, v2begin, min(31,v2len));
}
else if (strcmpJP(PSTR("numTools"), v1begin, v1len) == 0)
{
gNumTools = v2begin[0] - '0';
}
else if (strcmpJP(PSTR("status"), v1begin, v1len) == 0)
{
gStatusStr = v2begin[0];
}
else if (strcmpJP(PSTR("pos"), v1begin, v1len) == 0)
{
PosIter = &gData.MS.Pos.X;
result = json_arr(&astate, v2begin, &v1type, &v1begin, &v1len);
while (result > 0)
{
*PosIter = v1begin;
PosIter++;
if (PosIter > &gData.MS.Pos.Z)
break;
result = json_arr(&astate, NULL, &v1type, &v1begin, &v1len);
}
}
else if (strcmpJP(PSTR("heaters"), v1begin, v1len) == 0)
{
gData.MS.HeaterCount = 0;
result = json_arr(&astate, v2begin, &v1type, &v1begin, &v1len);
while (result > 0)
{
gData.MS.Heaters[gData.MS.HeaterCount].Current = v1begin;
gData.MS.HeaterCount++;
if (gData.MS.HeaterCount == HEATERS_MAX)
break;
result = json_arr(&astate, NULL, &v1type, &v1begin, &v1len);
}
}
else if (strcmpJP(PSTR("active"), v1begin, v1len) == 0)
{
Index = 0;
result = json_arr(&astate, v2begin, &v1type, &v1begin, &v1len);
while (result > 0)
{
gData.MS.Heaters[Index].Active = v1begin;
Index++;
if (Index == HEATERS_MAX)
break;
result = json_arr(&astate, NULL, &v1type, &v1begin, &v1len);
}
}
else if (strcmpJP(PSTR("standby"), v1begin, v1len) == 0)
{
Index = 0;
result = json_arr(&astate, v2begin, &v1type, &v1begin, &v1len);
while (result > 0)
{
gData.MS.Heaters[Index].Standby = v1begin;
Index++;
if (Index == HEATERS_MAX)
break;
result = json_arr(&astate, NULL, &v1type, &v1begin, &v1len);
}
}
else if (strcmpJP(PSTR("hstat"), v1begin, v1len) == 0)
{
Index = 0;
result = json_arr(&astate, v2begin, &v1type, &v1begin, &v1len);
while (result > 0)
{
gData.MS.Heaters[Index].Status = v1begin[0] - '0';
Index++;
if (Index == HEATERS_MAX)
break;
result = json_arr(&astate, NULL, &v1type, &v1begin, &v1len);
}
}
else if (strcmpJP(PSTR("tool"), v1begin, v1len) == 0)
{
if (isdigit(v2begin[0]))
gData.MS.Tool = v2begin[0] - '0';
else
gData.MS.Tool = -1;
}
else if (strcmpJP(PSTR("fraction_printed"), v1begin, v1len) == 0)
{
gData.MS.FractionPrinted = v2begin;
}
result = json_obj(&jstate, NULL, &v1type, &v1begin, &v1len, &v2type, &v2begin, &v2len);
}
return true;
}