forked from cocktailyogi/EggDuino
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Functions.ino
264 lines (233 loc) · 4.6 KB
/
Functions.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
void makeComInterface(){
SCmd.addCommand("v",sendVersion);
SCmd.addCommand("EM",enableMotors);
SCmd.addCommand("SC",stepperModeConfigure);
SCmd.addCommand("SP",setPen);
SCmd.addCommand("SM",stepperMove);
SCmd.addCommand("SE",ignore);
SCmd.addCommand("TP",togglePen);
SCmd.addCommand("PO",ignore); //Engraver command, not implemented, gives fake answer
SCmd.addCommand("NI",nodeCountIncrement);
SCmd.addCommand("ND",nodeCountDecrement);
SCmd.addCommand("SN",setNodeCount);
SCmd.addCommand("QN",queryNodeCount);
SCmd.addCommand("SL",setLayer);
SCmd.addCommand("QL",queryLayer);
SCmd.addCommand("QP",queryPen);
SCmd.addCommand("QB",queryButton); //"PRG" Button,
SCmd.setDefaultHandler(unrecognized); // Handler for command that isn't matched (says "What?")
}
void queryPen() {
char state;
if (penState==penUpPos)
state='1';
else
state='0';
Serial.print(String(state)+"\r\n");
sendAck();
}
void queryButton() {
Serial.print(String(prgButtonState) +"\r\n");
sendAck();
prgButtonState = 0;
}
void queryLayer() {
Serial.print(String(layer) +"\r\n");
sendAck();
}
void setLayer() {
uint32_t value=0;
char *arg1;
arg1 = SCmd.next();
if (arg1 != NULL) {
value = atoi(arg1);
layer=value;
sendAck();
}
else
sendError();
}
void queryNodeCount() {
Serial.print(String(nodeCount) +"\r\n");
sendAck();
}
void setNodeCount() {
uint32_t value=0;
char *arg1;
arg1 = SCmd.next();
if (arg1 != NULL) {
value = atoi(arg1);
nodeCount=value;
sendAck();
}
else
sendError();
}
void nodeCountIncrement() {
nodeCount=nodeCount++;
sendAck();
}
void nodeCountDecrement() {
nodeCount=nodeCount--;
sendAck();
}
void stepperMove() {
uint16_t duration=0; //in ms
int penStepsEBB=0; //Pen
int rotStepsEBB=0; //Rot
moveToDestination();
if (!parseSMArgs(&duration, &penStepsEBB, &rotStepsEBB)) {
sendError();
return;
}
sendAck();
if ( (penStepsEBB==0) && (rotStepsEBB==0) ) {
delay(duration);
return;
}
prepareMove(duration, penStepsEBB, rotStepsEBB);
}
void setPen(){
int cmd;
int value;
char *arg;
moveToDestination();
arg = SCmd.next();
if (arg != NULL) {
cmd = atoi(arg);
switch (cmd) {
case 0:
penServo.write(penUpPos);
penState=penUpPos;
break;
case 1:
penServo.write(penDownPos);
penState=penDownPos;
break;
default:
sendError();
}
}
char *val;
val = SCmd.next();
if (val != NULL) {
value = atoi(val);
sendAck();
delay(value);
}
if (val==NULL && arg !=NULL) {
sendAck();
delay(500);
}
// Serial.println("delay");
if (val==NULL && arg ==NULL)
sendError();
}
void togglePen(){
int value;
char *arg;
moveToDestination();
arg = SCmd.next();
if (arg != NULL)
value = atoi(arg);
else
value = 500;
doTogglePen();
sendAck();
delay(value);
}
void doTogglePen() {
if (penState==penUpPos) {
penServo.write(penDownPos);
penState=penDownPos;
} else {
penServo.write(penUpPos);
penState=penUpPos;
}
}
void enableMotors(){
int cmd;
int value;
char *arg;
char *val;
arg = SCmd.next();
if (arg != NULL)
cmd = atoi(arg);
val = SCmd.next();
if (val != NULL)
value = atoi(val);
//values parsed
if ((arg != NULL) && (val == NULL)){
switch (cmd) {
case 0: motorsOff();
sendAck();
break;
case 1: motorsOn();
sendAck();
break;
default:
sendError();
}
}
//the following implementaion is a little bit cheated, because i did not know, how to implement different values for first and second argument.
if ((arg != NULL) && (val != NULL)){
switch (value) {
case 0: motorsOff();
sendAck();
break;
case 1: motorsOn();
sendAck();
break;
default:
sendError();
}
}
}
void stepperModeConfigure(){
int cmd;
int value;
char *arg;
arg = SCmd.next();
if (arg != NULL)
cmd = atoi(arg);
char *val;
val = SCmd.next();
if (val != NULL)
value = atoi(val);
if ((arg != NULL) && (val != NULL)){
switch (cmd) {
case 4: penDownPos= (int) ((float) (value-6000)/(float) 133.3); // transformation from EBB to PWM-Servo
storePenDownPosInEE();
sendAck();
break;
case 5: penUpPos= (int)((float) (value-6000)/(float) 133.3); // transformation from EBB to PWM-Servo
storePenUpPosInEE();
sendAck();
break;
case 6: //rotMin=value; ignored
sendAck();
break;
case 7: //rotMax=value; ignored
sendAck();
break;
case 11: servoRateUp=value;
sendAck();
break;
case 12: servoRateDown=value;
sendAck();
break;
default:
sendError();
}
}
}
void sendVersion(){
Serial.print(initSting);
Serial.print("\r\n");
}
void unrecognized(const char *command){
sendError();
}
void ignore(){
sendAck();
}