|

楼主 |
发表于 2014-5-10 09:55:02
|
显示全部楼层
// our point structure to make things nice." ]# h( n, q; s5 D
struct LongPoint {
9 Z5 m3 r. q* |+ Y! ?5 \ long x;
6 k1 r1 r& d% [- z! } long y;4 T9 S, x5 D& U9 I
long z;& P% A: j6 t3 B; c2 y0 _, g
long u;4 ?) O" A% s5 H! T2 ]4 l E
};
6 u9 P k- H" }7 U: C: Q/ c9 J% o4 D+ f/ J6 S0 Y1 u' G% j! \# Q
struct FloatPoint {
/ D! {, Q- x7 U0 R' L0 g float x;8 M( e/ w' ^* r' S0 @; e& r" F; g
float y;" N: `7 ^+ P( m7 [3 C
float z;: h% R! A: s+ Q5 b8 w
float u;
* a) X" r# e1 W3 v2 W};( }) u/ k" U5 \
# u: O! E0 j" f4 @$ ]
FloatPoint current_units;% Z1 a2 t5 D, @1 T% a
FloatPoint target_units;
/ n7 l- Z+ H* B5 d" X2 ]FloatPoint delta_units;; l1 v- @$ l. p% w5 F& b$ |1 v; q
* L) [+ ]/ c7 B. j9 t/ ]# B7 J# _
FloatPoint current_steps;
( |5 I) o- T2 q& a: `: f8 SFloatPoint target_steps;
; X2 p, ~2 @8 I* u1 p1 @FloatPoint delta_steps;. s) l% l& V2 u" _' e
% Y% J5 f! ]7 u V5 `9 I+ D x4 p( Lboolean abs_mode = false; //0 = 增量位置模式; 1 = 绝对位置模式
3 w0 j' C4 V9 K4 k8 P+ R
8 |6 Z8 r0 D/ n; `' a& n4 f//default to inches for units
8 ?% y, e) `7 S. `float x_units = X_STEPS_PER_INCH;
7 \! X' P1 ]( b: {# wfloat y_units = Y_STEPS_PER_INCH; S) f% ^' ?, `! C9 d, B8 w. i
float z_units = Z_STEPS_PER_INCH;
+ z. S, H. f! D) Afloat u_units = U_STEPS_PER_INCH;
8 e6 [9 i1 ], e" Z/ Dfloat curve_section = CURVE_SECTION_INCHES;, h: P4 q% I1 `; j+ E! G
3 o; p8 e7 M/ l' f- O//our direction vars' [" B, J' @1 ^- w& C. A/ ]7 C
byte x_direction = 1;
9 c5 d1 `. W" b" Hbyte y_direction = 1;+ r3 @$ D8 D A' _" S8 U8 ~" r
byte z_direction = 1;! t0 B/ v7 z' v. ~ E0 S; ~3 [: K
byte u_direction = 1;( F# c" z8 J. R F
# n5 R' C1 K8 t- Z: ~9 Y. ]
//初始化字符串处理. d7 Q, K* a3 E9 ]5 u. p7 d4 Q
void init_process_string()
- G/ B! }% k: S{
' b7 f P- \7 p4 s+ R //init our command
3 t3 a+ d/ l. I8 x) d6 n for (byte i=0; i<COMMAND_SIZE; i++)) W. t- j/ Q# z, R" s4 Q' g/ u
word_old[i] = 0;
* s4 p" h; A! O serial_count = 0;
3 C, [" i8 C" j& S t& ^8 X4 N}
- D' d1 L7 L! D& `9 M, [: [, a# @0 T6 T1 v
//our feedrate variables.( ^ t; }" n4 j: _5 }. q7 S
float feedrate = 0.0;
! o9 E# g0 W. b8 t9 |3 V9 ]- G& Glong feedrate_micros = 0;% h% u( x* a: \1 v- }; Z
% Y# b- c& a4 W1 L+ ]
//读取并执行命令1 R) y- e& C. p0 O9 y5 L1 g
void process_string(char instruction[], int size)6 x' l& f( ]) q; ]0 ~1 ?
{
, g6 ~+ n6 a# |& G# O4 e1 C //the character / means delete block... used for comments and stuff.
+ e, I m1 z! F& S5 c if (instruction[0] == '/')& w9 L x- `1 u5 A( ^9 x5 h
{
9 ~; O1 U) i, B/ C // SerialUSB.print("ok");7 G4 n- X ]& H3 y7 L9 L+ r
// SerialUSB.print(byte(78));: Y9 A. B4 P: C
return;* p3 _( H3 k( A( y) B6 z6 H# m* |
}; V2 O9 g5 M3 w7 u& J( f
//init baby!) o+ i) t v0 Q9 S
FloatPoint fp;
* ^' \6 }7 r- l9 y& { g0 T* n fp.x = 0.0;
* n) d4 p6 U8 z2 [6 l* g* O fp.y = 0.0;
7 G* D O# L: M/ A8 X/ y7 c" D fp.z = 0.0;
- n$ Y; D& F+ Y# V: V* S fp.u = 0.0;5 m! Z& K. l+ {* ~4 J" H
" `* q. |7 Z0 N n% g
byte code = 0;
4 ^; S, v+ B% u P* m8 R9 j$ S5 [' ?+ D
//显示在处理的命令/ V5 i, o3 w I' x8 q& C) q
#if (LCD_TYPE == 1604 )
% e/ \- A# o8 {) _" N: \ // lcd.setCursor(0, 4);, D' l" B& V/ m7 S2 {
// lcd.print(word_old);9 V# r& C; {) G" [6 Z" _5 q% c) N
#endif% |' @7 H, b5 U, C+ K
SerialUSB.println();9 u% ?: c( E5 c" X' Z8 \8 Z; \
SerialUSB.print(instruction);
2 q9 H I9 g3 y2 ?7 B SerialUSB.print("\t");2 I4 m( F6 P4 L4 ^
& w# c/ Q( E3 \% }7 @: e
//what line are we at?
/ k3 z) K& U0 W // long line = -1;
) U/ h) I2 X& {1 Q // if (has_command('N', instruction, size))& R6 u) |4 S; u `( q
// line = (long)search_string('N', instruction, size);
. p0 l1 V( x! ~$ v: d! D, N4 w1 y% I' x8 ^& \
/*1 u2 e2 I. P4 T
Serial.print("line: ");5 K( A+ W- n8 ?: M$ G! B
Serial.println(line);
3 E y; }* R n2 _5 o/ B Serial.println(instruction);
1 v/ x8 K; B1 x( L2 |& i8 R */
9 r+ K! ` j4 Y% M" I //判断是否读取了个 G代码?
% G8 S. M# V# o" y if (
3 o, A% M/ Y1 C P; f5 f4 e has_command('G', instruction, size) ||
. ^' P2 w t8 e! n0 M has_command('X', instruction, size) ||1 u9 `$ o& w$ k. l. e. P3 n
has_command('Y', instruction, size) ||
e5 R. w# c1 @" f3 A# S+ [* X has_command('Z', instruction, size) ||
! |% k& [' ~; X- I( D6 } has_command('U', instruction, size), u! o* Z/ |! O( x; _. q! ~6 B
)0 h+ a7 d7 j" Z Y4 H
{ H+ i9 j5 A6 k2 k9 l K9 O% e0 G
//which one?
; Y" Z! w& l+ \8 L code = (int)search_string('G', instruction, size);
" t @7 ?& o" ~5 I // Get co-ordinates if required by the code type given6 w: x) t! i9 } a! b9 d
switch (code)
6 s# m* _0 Q' F) w z { S3 i% V+ c7 n* O$ `
case 0:
- d/ Q$ N/ f$ M* C% W& d case 1:- v1 U4 Q& _8 ^; x
case 2:
7 M; a% E. k/ x4 K case 3:$ o1 Z0 |2 V; V7 m' B. t
if(abs_mode)
, u. d* k2 W2 @0 X7 y {
' O! f7 T& w0 u8 ^, R+ w5 N //we do it like this to save time. makes curves better.8 m$ ~1 }) k- ^7 v9 S" D3 k
//eg. if only x and y are specified, we dont have to waste time looking up z.
0 c3 a3 W' ~! F: p' D if (has_command('X', instruction, size))" K) ?, P9 J! d( s
fp.x = search_string('X', instruction, size);% @7 a& h: D' H' h3 A
else) y2 D J* x! j) Y
fp.x = current_units.x;7 \' [2 _1 D0 ~( B2 g
# z( x! L: z/ |) y+ p; b9 i if (has_command('Y', instruction, size))
! J. E, L! u% k% V0 Z& v fp.y = search_string('Y', instruction, size);
, s: W7 k. Z# `* m else, s6 G- a& K* m J9 M
fp.y = current_units.y;
0 @$ @+ q* f# g+ k; Q& k( o4 F
7 ?- `* Q% U! ?' _' N! `- G if (has_command('Z', instruction, size))) i6 b3 P, w+ x0 G$ {
fp.z = search_string('Z', instruction, size);6 V1 C" g: m# R( G& C2 z
else/ A2 ]. v, h) s% f2 s6 Z
fp.z = current_units.z;
' Q; }6 ~6 K. {7 }+ T ( p0 L! I5 \! k* t( N+ C9 p
if (has_command('U', instruction, size))/ a9 N' u/ Q5 S8 p+ q: V
fp.u = search_string('U', instruction, size);3 o0 v+ F' C P6 x' `
else
$ z, [' `* T% P4 F, k fp.u = current_units.u;4 F# t. g G& ?# X
}
7 [# ]/ D7 x, ]3 p5 m+ y else; [# B2 F. V9 d$ J
{
2 H+ h; b9 w% d( y fp.x = search_string('X', instruction, size) + current_units.x;
; W, j/ s c `# C fp.y = search_string('Y', instruction, size) + current_units.y;
' q0 B/ N3 b) \1 N# } fp.z = search_string('Z', instruction, size) + current_units.z;/ ]$ e5 g7 w) c5 n# q: C
fp.u = search_string('U', instruction, size) + current_units.u;% ~4 i. M, k7 j7 O
}2 j$ o9 j& a/ @3 P1 }
break;
; k2 I9 ~4 {6 y+ m& U7 J }
. s8 |% \3 i. W1 \# D2 m; t0 f3 \, @ //do something!
+ A- U R3 ^; p g$ N! j( ?& D switch (code) j0 q) t2 I5 q
{" [ U( F# r( i+ c! ?5 f
//Rapid Positioning+ f6 q3 L+ P: S$ X, a0 L( M6 V
//Linear Interpolation
4 S, e9 @4 U5 y% ]* z3 @7 B //these are basically the same thing.% |3 m1 B! W/ h! U" n
case 0:$ r* u5 X* G' g5 F9 r# |) X( T
case 1:# e( S" U4 V- q" j6 w8 A
//set our target.
$ f) p8 T' ^% X. O1 n$ \ set_target(fp.x, fp.y, fp.z, fp.u);
6 U) b) G8 w. T/ |! M: L! B; E //set_targeta( fp.a);$ J) S2 `: X- W% f. D, {
//do we have a set speed?
9 ~$ Z. H% O) I1 S$ J: ?. q if (has_command('G', instruction, size)); U, Q/ p3 t! u9 k5 T& m
{; a) m2 [$ }5 H
//adjust if we have a specific feedrate.4 F/ w; S3 S6 w; A/ w" F
if (code == 1)3 ]! l9 k- n0 D& I
{
$ F2 `/ h. b7 w. c* t1 s //how fast do we move? C8 P# R& V) ]: U2 @% b2 a9 Q
feedrate = search_string('F', instruction, size);
$ j7 x8 j% e! [# l7 | if (feedrate > 0)
, Y% r" q2 _/ Z" a! [* z feedrate_micros = calculate_feedrate_delay(feedrate);8 C* i3 I1 @, s$ A' }! P9 Z. u
//nope, no feedrate
1 x2 U& f) N- b) M: b else3 Y7 o8 \# ]2 b# f" G+ ]7 t
feedrate_micros = getMaxSpeed();3 n9 q+ `' f0 Y1 T4 j
}
" d) ^* ^1 o/ T) I9 m, C //use our max for normal moves.! F2 Y' a1 z% }5 g, r/ z5 n M
else( M; v) I8 S' }8 @& {! {" z# i0 s
feedrate_micros = getMaxSpeed();$ z M4 O6 x: w1 e/ {" ~
}( \, U7 [( h" K( g! e) i4 k
//nope, just coordinates!
! T8 e! ~3 g% U$ ~, Y else
. U% q# N8 y0 J- H8 U( l {
7 e% L. E7 ?6 c2 s) f& m8 X //do we have a feedrate yet?
+ L+ }+ O/ k' s if (feedrate > 0)
8 M3 A% J" y' W( s. c2 N feedrate_micros = calculate_feedrate_delay(feedrate);
% Z/ t J1 V& v/ w- d0 D5 J" O4 n //nope, no feedrate8 n2 ~$ G) f" V5 s3 s p) y2 t
else
5 e O3 R( z( A* i* [/ G& w+ Z( x7 x feedrate_micros = getMaxSpeed();' I1 R {6 M1 j! o, q
}! o& w' {/ G" b* h/ u
' W5 s8 j; w g2 J9 c% u) A) @
//finally move.
; g m; O" v0 J; @! g" B) J dda_move(feedrate_micros);
0 T/ }; P8 X p0 ?! b& p' t if(stop_flag) return;! R2 T$ H9 x9 H* `7 f J8 C7 Q
break;
. I. M) [& N/ ]; r L& B: \# V* c
& c4 D; Z0 Y% c- i( Z //Clockwise arc
1 E" ~ _# ]) ~- b' T case 2:7 c' Y" w" p" T, n: z
//Counterclockwise arc
- o0 u# M- r* A7 `. j) x case 3:
0 k/ r# d+ u1 |9 N& F FloatPoint cent;8 z7 d _: `) |5 j/ ?0 b
// Centre coordinates are always relative
! G% E9 Z& G& F& y" t cent.x = search_string('I', instruction, size) + current_units.x;
: K3 l, p$ w% I4 t C3 r/ o cent.y = search_string('J', instruction, size) + current_units.y;
' X3 _+ i. J6 R: h5 A% w float angleA, angleB, angle, radius, length, aX, aY, bX, bY;
: Y- A( @1 l: _# B9 O/ l C$ x6 ~* i, ?3 U# ?
aX = (current_units.x - cent.x);# A+ L+ T2 B @9 A* H: c
aY = (current_units.y - cent.y);& Q" ]. B4 C# e/ l
bX = (fp.x - cent.x);3 G2 k$ f( z1 Q# W& S% H: V
bY = (fp.y - cent.y);
A, {" F. _6 _/ Y
* }' O- W* k6 B* O if (code == 2) { // Clockwise+ V* W9 V8 O, W) d9 q2 F! U
angleA = atan2(bY, bX);$ F8 @" A7 y3 s8 k% G9 _0 i
angleB = atan2(aY, aX);8 O; a/ d! P. a2 L2 f$ |
} ) d$ z1 |; y, `
else { // Counterclockwise. X+ j+ a6 v( w( @4 [" ]) e: V
angleA = atan2(aY, aX); W" a7 O' s, K: T- j9 k6 v4 ]3 t1 @
angleB = atan2(bY, bX);4 l7 V4 H# P* {8 s6 w1 t
}; Y& G9 }+ _0 D+ A! f8 Q% e1 T2 B
// Make sure angleB is always greater than angleA, J Z) T) ]/ Z* r2 g3 B1 @
// and if not add 2PI so that it is (this also takes2 x. ^; i( L9 m5 I: N
// care of the special case of angleA == angleB,
7 |" j( F% t/ [ q4 b+ k // ie we want a complete circle)! g7 {: ?# A0 Z
if (angleB <= angleA) angleB += 2 * M_PI;
+ M' B2 t2 c3 d+ L" K angle = angleB - angleA;/ m+ w2 b8 l. b# a: Y+ E# Q: o
9 F9 i0 s- a3 }
radius = sqrt(aX * aX + aY * aY);
! H; j. Z" s3 B4 R7 O length = radius * angle;3 K% B" C8 B9 t6 i" B# S2 l: S
int steps, s, step;
6 H8 }4 y. T2 G steps = (int) ceil(length / curve_section);1 l1 q! H: e U; S" N( u
* m0 d; R7 u% s& T: A1 [
FloatPoint newPoint;
! @4 T% Z& F% u/ b( t6 Z: L: X# ~ for (s = 1; s <= steps; s++) {7 k" R& p; T5 S" }8 C& @) p
step = (code == 3) ? s : steps - s; // Work backwards for CW- L' g2 ?$ [: }5 I9 J- w% z
newPoint.x = cent.x + radius * cos(angleA + angle * ((float) step / steps));, K, N: w( y( z1 Z* N
newPoint.y = cent.y + radius * sin(angleA + angle * ((float) step / steps));
9 I: r' G3 p! P2 m set_target(newPoint.x, newPoint.y, fp.z, fp.u);
/ G A/ s6 x3 f& k2 J% h$ J( k$ n8 d$ R" \8 x
// Need to calculate rate for each section of curve( V: k+ H5 A4 n! [ i$ e
if (feedrate > 0)( r# S1 y0 c1 d7 ?- }
feedrate_micros = calculate_feedrate_delay(feedrate);. c( v: c* C# V, Z# F0 j6 y
else) p) h4 H2 a7 D
feedrate_micros = getMaxSpeed();8 \- h& {1 x% p( z$ D
' n1 r% ~5 w: r# \4 d
// Make step7 X, N, f( E4 J4 F+ X! J: }
dda_move(feedrate_micros);
' _& `! i. w5 g" T/ g: F if(stop_flag) return;
- V+ _% d$ Y- R3 v; Y: k% _- Z }4 d7 t3 z) @0 z$ M
( o# [9 j- x: h6 T3 a break;! R- ~& F! k p" t" M- m7 o
3 s# Y$ x& [3 F8 p6 q8 T6 G2 q //Dwell
. J3 f; A3 l9 c( m0 O- t" x9 G- t case 4:
' {8 |8 l C" i3 V5 Y+ ~0 U1 s- m delay((int)search_string('P', instruction, size));
3 |0 O! @( T; Q3 S break;
. D& @, A+ C' R) L7 N/ J$ S
9 c- \6 l- _3 ~9 Z //Inches for Units% V0 @$ C6 M! ?9 }4 M
case 20:" ?0 J( A* k& z2 U
x_units = X_STEPS_PER_INCH;
% \9 ?, U5 g9 B; b3 ?- z y_units = Y_STEPS_PER_INCH;
4 Q! r4 M: H3 b0 ]9 ?* d* L z_units = Z_STEPS_PER_INCH;! I u& K8 }( m V, ~( m& h9 R
u_units = U_STEPS_PER_INCH;
t! ~' ?# y0 r2 X$ M8 d curve_section = CURVE_SECTION_INCHES;9 f. I& [; C7 i6 X
calculate_deltas();0 @( @9 X4 g& h$ F% F( q5 `% K/ V
break;
3 }8 f6 t( L' H' [ h! @" t4 i/ I- I0 ?0 g8 y
//mm for Units
' {& a" V2 a/ ^( n; e case 21:% z) t6 Z) ~7 m5 x/ ^* q
x_units = X_STEPS_PER_MM;
* M. r* h9 o9 Q2 z y_units = Y_STEPS_PER_MM;* z* g) Q4 K7 [& v9 h
z_units = Z_STEPS_PER_MM; ], z. @$ N- J7 @9 w* f/ z- i
u_units = U_STEPS_PER_MM;! F! @$ O1 }* L1 u$ e
curve_section = CURVE_SECTION_MM;
3 }4 {/ m" j' u' g# D" H calculate_deltas();' t# e& Q: T) @6 ]2 t
break;! s. o' N( U2 l/ E
1 U4 Q8 S, I. Z4 B
//go home.
: o6 k( E% e1 z2 Z; B. G5 X; ` case 28:
" s% e/ W/ f6 C& p- x' l( {' l set_target(0.0, 0.0, 0.0, 0.0);- h8 l$ v' }- b# j0 d
dda_move(getMaxSpeed());
{& f% u! U8 k! h if(stop_flag) return;" v6 T1 }9 m1 I( C
break;
* }3 N8 Y: {- x$ Y4 I
6 T9 g1 h9 o7 x Q7 j //go home via an intermediate point.
2 x9 v$ ~ b+ o0 m( s& j E1 z case 30:; R5 O* F" [" k* e6 R
fp.x = search_string('X', instruction, size); G: F4 z: S# l5 X' T2 D
fp.y = search_string('Y', instruction, size);' Q+ ?4 U) Y4 f- y
fp.z = search_string('Z', instruction, size);
% e1 _$ _& X4 t+ P% J fp.u = search_string('U', instruction, size);
- p1 |$ @ [ U4 g. t //set our target.
" P2 _& b& V% L1 H" E9 ^& X' r& L if(abs_mode), ^+ [+ w9 m- u6 p, c
{
* O9 @" i A; X4 b! @ if (!has_command('X', instruction, size))6 w& t+ x$ t, _# d( y
fp.x = current_units.x;+ K4 O4 h. j, J) S# y
if (!has_command('Y', instruction, size))
& M2 g ?( N& ~$ T fp.y = current_units.y;- t# e: x4 }: A* ~3 r+ P7 s
if (!has_command('Z', instruction, size))
) f/ G2 f( p3 L& p1 _# k fp.z = current_units.z;: `4 V) M8 W# I
if (!has_command('U', instruction, size))
7 i4 x) v: u; m+ ?# J+ B fp.u = current_units.u;
* G, f) S; y7 V; H set_target(fp.x, fp.y, fp.z, fp.u);
7 G) }" c8 f9 _9 T/ T+ q
/ u4 O }; Y k" [! B }
2 o ?) H2 M, J+ X# |% ]( _ else N/ i3 `8 j+ ^8 k
set_target(current_units.x + fp.x, current_units.y + fp.y, current_units.z + fp.z, current_units.u + fp.u );
! X4 G- J2 L; V7 j6 S/ G 9 O4 s' P' H9 N$ ^' J4 O
//go there.: f% c; _$ v1 }
dda_move(getMaxSpeed());
" N2 K/ r* D9 p1 J if(stop_flag) return;
& t; A5 k6 L. e- C# t0 Q! V
" t* c( ]0 D* V3 p x) M- P* w //go home.
& |6 Q% V$ M4 o/ X: G8 O( }7 A { set_target(0.0, 0.0, 0.0, 0.0 );
8 H' v# J" u' {
6 o# H4 M* u3 F( R& R* w dda_move(getMaxSpeed());
0 u5 n }7 N& @% r if(stop_flag) return; _$ R, l. U1 I( k8 [& i
break;
3 V% K; F% t7 Q4 V. N5 F. o/ I
# N7 O3 _0 a t! R //Absolute Positioning. j8 G; Z7 m8 `' P' a# S l t B# w
case 90:8 {4 R4 f8 B$ s5 ]: p
abs_mode = true;& A6 L }# o( Z' @6 y
break;% F) {3 K0 e* W. J
, U+ {8 u/ k8 e //Incremental Positioning) [. q, Q( z$ C# A
case 91:* x [( t! S% K& h t
abs_mode = false;
6 E7 ^) E, h2 M, J) t3 H1 d break;4 `) ?5 b# T T' e ~% N
7 i! f p/ {3 W- F3 ]* d
//Set as home3 s& ~* j! {/ H# ^
case 92:
) e. h# O8 P6 Y2 p& L9 x5 [" v ; D9 o! _$ D' I' e8 M
set_position(0.0, 0.0, 0.0, 0.0 );
# e/ z+ b8 ?; G0 @
: G! O4 L; H9 d ~ Y break;
3 i$ G% G) Z9 v# P2 o: u$ H5 h( s7 g2 p3 V
/*) u: m" R( N4 ]9 y3 D; Q
//Inverse Time Feed Mode
r2 n, S: `) ?1 x case 93:
6 L$ `& ^8 U- I' ?' B# }
2 q4 V. d7 H$ N break; //TODO: add this
1 b7 R! u. m, g- Z
4 a6 m- X. ]& j6 U( o7 p# n3 n //Feed per Minute Mode( _. c! w0 e# o+ P
case 94:
+ \% I9 K$ ^6 y0 ^ ( c3 B) d' S0 K& J4 `
break; //TODO: add this, C; ~% U( ?8 B }' i4 v
*/6 Y+ I) s. O9 |' m5 r$ i
2 Y0 S+ H. P9 O0 }, |/ A! e9 @
default:
* G! [' o, t+ m% F8 c' Z8 Q SerialUSB.print("huh? G");5 K# U) O# S7 \2 \
SerialUSB.println(code,DEC);$ e4 ?8 A9 T; i3 R3 ~, Y3 J
}
$ o% @( b u% l& b+ I8 G) I9 G& h }
8 B( ?1 r2 V B+ Z+ F/ A5 h V. E0 L* e# R, s7 _( @! k5 @3 N
//find us an m code.
* d" I0 K& V& t if (has_command('M', instruction, size))6 b4 Q3 ^0 W0 w& J/ v+ `
{3 z# I5 ~* a3 E4 H4 m7 `/ M
code = search_string('M', instruction, size);! `' R+ N3 J- X7 A2 u+ [+ g
switch (code)7 Q4 n% g0 y4 r) X) G
{
* C. s! d$ g$ ~' F! r/ W //TODO: this is a bug because search_string returns 0. gotta fix that.. C' {; z+ _) ~1 t ?* x
case 0:
4 F+ U! _9 N+ q2 G. i2 g8 a6 O true;
8 C" t. Q" c+ l* c0 U4 E' e- h8 ` break;, Y$ l3 k+ I+ i+ _
) x7 V! }8 |4 b" k& d. K P case 100:
s& ~$ [; g( u: v) w4 O break;
3 N* r! q5 {$ ?
! G, T/ N0 g; E% f+ O2 w // fire camera relay
: n1 B# F8 ^. a. b) @& [( d3 Y8 I case 101:
: X/ C- ^2 k: V/ I) ~ camera_shutter1();
8 L) A: Q9 m" ] break;. J7 V% r F" x
- W5 M2 h' ]9 `; _3 L7 U$ b // fire camera relay2. \9 z s! D: _8 c0 N L
case 102:
' m1 ]* V6 I2 J& d camera_shutter2();
/ o7 c; Z0 |) N5 k! o5 t0 T6 g3 K break;8 R" C) }8 N& x+ o
/*& n7 m5 g: c6 N) f7 d& _
// turn aux 1 relay on" z& A8 R* P& }
case 103:
8 ]3 N0 J$ W+ ^" m2 j8 Z camera_aux1_on();
( \& O% P5 a1 @2 T break;
# U2 r( W! \3 ^0 M, i" S( \( [) t
// turn aux 1 relay off* R2 e2 q* V8 n( ^
case 104:1 k* a2 G* s) u
camera_aux1_off();
4 M; S9 A; F$ p- O9 k7 O. u break;6 t' \, L- E& u2 K1 n# X* m- p) r
7 c" z+ F8 l+ ~6 t- c! W& ~
// turn aux 2 relay on
) w. _- Z5 f: j, @9 Q case 105:
4 k" p5 k+ o0 j, H a: e/ ?) W) W camera_aux2_on();
u/ n2 @& H: o! Y9 i break;5 a& }0 J# y" e7 \! f+ x/ _: q
$ O" c8 |- d! X4 g! [0 a# Z6 J* n5 W$ s // turn aux 2 relay off
2 z; A: O1 d% t; Z9 ~, ] case 106:! z1 a6 Z* l$ B* `7 O! Q; Z
camera_aux2_off();( B+ d/ b( V: i8 \0 A* |4 C
break;
4 Q+ P8 F3 x. {7 |* |6 r& h3 q9 W; v& r/ u& ]2 r5 g5 ?
// turn aux 3 relay on
! @- u/ d/ J( `6 z case 107:# n/ i5 r* o1 H
camera_aux3_on();9 {0 r0 ]9 ^ O6 O' y
break;
& Z/ P, a, V+ c0 ]' g. U; z
& ~( ?$ O# c. Z# | // turn aux 3 relay off
2 e3 w+ D; P" U' R, [! \9 @ case 108:
6 i7 d1 s* J' H3 Z camera_aux3_off();: h3 x. k* J: `" F& k% f( ~; {
break;
# s+ U( r9 A5 q( o& A. g5 a: x' a4 Y$ l
// turn aux 4 relay on
8 Y5 j: v/ C1 q case 109:
! S$ w1 a& o' v% @1 @4 `( H camera_aux4_on();4 {( d7 [8 C8 ~4 x- n$ p0 }9 ^) I
break;
0 n5 N o2 ?+ \6 T& O. B6 S2 k, e$ I9 f q) W5 Y" L
// turn aux 4 relay off2 n- n5 [4 G0 U4 W8 ~% }5 `' f
case 110:7 |6 [, I. N& i* F
camera_aux4_off();1 I' K3 Z: ?' g9 \
break;+ J' b8 s. i9 v' d* }$ O- x; S
*/8 S* S( D% D: [3 o4 S
default:; a0 k4 Q. b4 p+ F7 o5 t. q! m
6 H9 n; e* H" s! s4 m
SerialUSB.print("Huh? M");: Z2 T8 H- ~: J9 y: l! O
SerialUSB.println(code);6 W9 `% l7 A& s% g/ X2 m) v% w
}
2 {1 `4 G: G6 d( k$ [8 B }
- a) v; _2 R( f- k8 j$ ^0 X; N& @
//tell our host we're done.8 T; z: k' }- ?! |) q
SerialUSB.print(byte(78));4 j% g" @9 K. p+ b- _) |
; E; a) H4 d/ G+ l$ a3 r! m3 V}
0 D6 q* D% G+ [7 w# g: Z% {1 V) z; n+ F0 U( n4 A
//look for the number that appears after the char key and return it
4 E$ ~! |& d2 bdouble search_string(char key, char instruction[], int string_size)
# U8 B3 d' } c) l{) k8 u" [4 T: o9 E
char temp[10] = " ";
8 {& v! \1 A6 y4 b& c$ w for (byte i=0; i<string_size; i++). k% \) L. U# J' m* C0 [
{
0 W+ C0 A8 l+ n* A+ n) r if (instruction[i] == key)/ ^# Y( H4 S( J8 g7 o% r
{ d3 _. I$ S# s" N. L4 z, g4 j- j' Q
i++;
* N3 j+ { t4 |- \7 O int k = 0;
% M* S- E4 j, u' v# w- f while (i < string_size && k < 10)
5 y" W; P8 [5 {5 O7 z {
$ f) f; {* m1 C8 H if (instruction[i] == 0 || instruction[i] == ' ')
* ]' t0 Y3 a/ D6 a break;
$ G6 {7 j7 r4 Q/ o' v' U
5 S* W) `$ `9 ^& ~ temp[k] = instruction[i];
& }! V: q# J9 T9 r; _ i++;4 A' V. V. W8 p/ R# u! a$ z4 j
k++;/ \8 a. A6 ~% n; S
} F$ Y+ {3 ^/ o! l
return strtod(temp, NULL);
0 X. G3 u7 r* C! z" F }. Z+ F* L# S2 a! m4 q
}: _% ?! D; t# Q5 E! B* d, x
% u$ N$ ?/ G' X% O0 y
return 0;
7 Q9 z+ {. u9 w! r+ K4 Y' _}
! Z" m3 h0 P+ p5 h! V1 ? M
* z! ]6 p; S$ c) V! [# f//look for the command if it exists.
: X6 Q0 p- }& b% obool has_command(char key, char instruction[], int string_size)
" A: f" W- X5 }" L& T( n7 ~5 f{
( U: r5 A* E* t: ~3 l4 B for (byte i=0; i<string_size; i++)) q" x7 q+ z. j8 @" Z
{+ p; _3 t; r0 |; h8 ]/ k
if (instruction[i] == key){3 R. O/ f2 S! o: r9 s/ o5 M; d
. }2 l3 R8 R0 h' ^0 L
return true;
; w& r4 Y: X) d% M }$ G4 H9 M$ H" k6 i2 O
}. H8 j* K; j: j( n
( [! Z. s f" @. S return false;- X# V* S/ x1 Y+ Q& X
}4 i0 j7 \* l8 |2 j2 `
0 v5 p6 t: [8 D' p P3 { p
; D$ y0 \$ B2 \( i, c3 j, Z$ D9 Z$ s k8 ~
|
|