-
Notifications
You must be signed in to change notification settings - Fork 20
/
Copy pathcli.c
326 lines (289 loc) · 6.34 KB
/
cli.c
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
315
316
317
318
319
320
321
322
323
324
325
326
/*
* cli.c
*
* command line interface
*
* Copyright 2010 <[email protected]>
*
* This file is part of Freecut.
*
* Freecut is free software: you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2.
*
* Freecut is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public
* License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Freecut. If not, see http://www.gnu.org/licenses/.
*
*/
#include <avr/wdt.h>
#include <avr/pgmspace.h>
#include <avr/interrupt.h>
#include <stdarg.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <stdio.h>
#include <setjmp.h>
#include <math.h>
#include "timer.h"
#include "cli.h"
#include "usb.h"
#include "stepper.h"
#include "flash.h"
#include "version.h"
#define BUFLEN 100
#define MAX_ARGS 10
static char buf[BUFLEN + 1];
static char *argv[MAX_ARGS+1];
static uint8_t buf_len = 0;
static uint8_t argc;
static uint8_t curtok;
static char echo = 0;
static jmp_buf cmd_error;
enum
{
C_EOL = 0,
C_BAD,
C_NUM,
C_MOVE,
C_DRAW,
C_VERSION,
C_SPEED,
C_PRESS,
C_CURVE,
C_FLASH,
C_RESET,
};
static struct token
{
char *lex;
uint8_t class;
long val;
} token;
static const struct keyword
{
char str[10];
uint8_t class;
} keyword_list[] PROGMEM =
{
// commands
{ "version", C_VERSION },
{ "move", C_MOVE },
{ "draw", C_DRAW },
{ "speed", C_SPEED },
{ "curve", C_CURVE },
{ "press", C_PRESS },
{ "pressure", C_PRESS },
{ "flash", C_FLASH },
{ "reset", C_RESET },
};
#define MAX_KEYWORDS (sizeof(keyword_list) / sizeof(*keyword_list))
static const struct keyword *find_keyword( char *str )
{
unsigned i;
const struct keyword *keyword;
for( i = 0, keyword = keyword_list; i < MAX_KEYWORDS; i++, keyword++ )
{
if( !strcmp_P(str, keyword->str) )
return( keyword );
}
return( 0 );
}
/*
* get next token
*/
static uint8_t get_token( void )
{
const struct keyword *keyword;
char *lex = argv[curtok];
uint8_t class;
token.lex = lex;
if( curtok >= argc )
{
token.class = C_EOL;
return C_EOL;
}
curtok++;
keyword = find_keyword( lex );
if( keyword )
memcpy_P( &class, &keyword->class, sizeof(class) );
else if( lex[0] == '-' || isdigit(lex[0]) )
{
token.val = strtol( lex, 0, 10 );
class = C_NUM;
}
else
class = C_BAD;
token.class = class;
return( class );
}
static void expect_token( uint8_t class, const char *what )
{
if( get_token() == class )
return;
printf( "expected %s, got '%s'\n", what, token.lex );
longjmp( cmd_error, 1 );
}
static long parse_num( void )
{
expect_token( C_NUM, "number" );
return( token.val );
}
/*
* split_line: split line into args at spaces.
*/
static char split_line( char *line, char **argv )
{
uint8_t argc = 0;
char c;
for( argc = 0; argc < MAX_ARGS; argc++ )
{
while( *line == ' ' )
*line++ = 0;
if( !*line )
break;
argv[argc] = line;
while( (c = *line) != 0 && c != ' ' )
line++;
}
argv[argc] = "";
return( argc );
}
void version( void )
{
printf( "Freecut version " VERSION "\n");
}
static void parse_speed( void )
{
stepper_speed( parse_num() );
}
static void parse_move( void )
{
uint16_t x = parse_num( );
uint16_t y = parse_num( );
if( x < 0 )
x = 0;
stepper_move( x, y );
}
static void parse_draw( void )
{
uint16_t x = parse_num( );
uint16_t y = parse_num( );
stepper_draw( x, y );
}
static void bezier_prep( int *k, int *p )
{
k[0] = -p[0] + 3*p[1] - 3*p[2] + p[3];
k[1] = 3*p[0] - 6*p[1] + 3*p[2];
k[2] = -3*p[0] + 3*p[1];
k[3] = p[0];
}
static int bezier_eval( int *p, float t )
{
float s;
s = p[0]; s *= t;
s += p[1]; s *= t;
s += p[2]; s *= t;
s += p[3];
return (int) (s + 0.5);
}
static void parse_curve( void )
{
int i, x, y;
int ox, oy;
int Px[4], Py[4]; // control points, x and y
int Kx[4], Ky[4]; // cubic polynomial coefficients
for( i = 0; i < 4; i++ )
{
Px[i] = parse_num( );
Py[i] = parse_num( );
}
ox = Px[0];
oy = Py[0];
stepper_move( ox, oy );
bezier_prep( Kx, Px );
bezier_prep( Ky, Py );
for( i = 0; i <= 128; i++ )
{
float t = i / 128.0;
x = bezier_eval( Kx, t );
y = bezier_eval( Ky, t );
if( x != ox || y != oy )
stepper_draw( x, y );
ox = x;
oy = y;
}
}
static void parse_flash( void )
{
flash_test( );
}
static void parse_pressure( void )
{
stepper_pressure( parse_num() );
}
static void parse_reset( void )
{
cli( );
wdt_enable( WDTO_15MS );
while( 1 );
}
static void parse( char *buf )
{
curtok = 0;
switch( get_token() )
{
case C_VERSION: version( ); break;
case C_MOVE: parse_move( ); break;
case C_DRAW: parse_draw( ); break;
case C_SPEED: parse_speed( ); break;
case C_PRESS: parse_pressure( ); break;
case C_CURVE: parse_curve( ); break;
case C_FLASH: parse_flash( ); break;
case C_RESET: parse_reset( ); break;
case C_EOL:
break;
default:
printf( "unknown command '%s'\n", token.lex );
return;
}
if( get_token() != C_EOL )
printf( "unrecognized parameter '%s'\n", token.lex );
}
void cli_poll( void )
{
int c = usb_peek( );
if( c < 0 )
return;
if( c == '\r' || c == '\n' )
{
printf( "\n" );
buf[buf_len] = 0;
buf_len = 0;
argc = split_line( buf, argv );
if( !setjmp(cmd_error) )
parse( buf );
printf( "%d>", stepper_queued() );
}
else if( c == '\b' || c == 0x7f )
{
if( buf_len > 0 )
{
buf_len--;
if( echo )
printf( "\b \b" );
}
}
else if( c == 5 )
echo ^= 1;
else if( isprint(c) && buf_len < BUFLEN )
{
if( echo )
putchar( c );
buf[buf_len++] = c;
}
}