Code to drive a CNC machine via a PC LPT port lookalike 25 pin 'D', experiment in 'PC/Mach3' replacement. Designed to compile and run on mbed LPC1768, Freescale KL25Z and Freescale KL46Z. Proved on LPC1768 and KL25Z, problem with serial port on KL46Z. Reads subset of 'G Codes' through usb/serial port and drives 3 stepper/servo drives for X, Y and Z, also similar Step/Dir outputs for spindle motor control. Emulates PC LPT, outputs 'charge pump', proved driving Seig KX3 CNC mill

Dependencies:   MODSERIAL mbed

Committer:
JonFreeman
Date:
Thu Feb 20 09:27:18 2014 +0000
Revision:
2:b3c668ec43ac
Parent:
1:66ee619f206b
Child:
3:7aaf0072cc22
As used to produce Lissajous patterns for EiM articles.  XYZ axis good, Axis A not implemented, spindle 'S' working.  Using MODSERIAL, therefore no longer compatible with KL46Z, good for KL25Z and Mbed LPC1768

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JonFreeman 0:5d0f270bfc87 1 #include "mbed.h"
JonFreeman 1:66ee619f206b 2 #include "rtos.h"
JonFreeman 2:b3c668ec43ac 3 #include "MODSERIAL.h"
JonFreeman 0:5d0f270bfc87 4 #include "cnc.h"
JonFreeman 0:5d0f270bfc87 5 using namespace std;
JonFreeman 0:5d0f270bfc87 6
JonFreeman 2:b3c668ec43ac 7 extern MODSERIAL pc;
JonFreeman 2:b3c668ec43ac 8 //extern Serial pc;
JonFreeman 2:b3c668ec43ac 9 //extern bool liss_active;
JonFreeman 1:66ee619f206b 10 extern struct Gparams last_position;
JonFreeman 2:b3c668ec43ac 11 extern void move_to_XYZ (struct pirbufgrain & ins) ;
JonFreeman 2:b3c668ec43ac 12 //extern struct digital_readouts dro; //
JonFreeman 2:b3c668ec43ac 13 //extern void scmd (struct singleGparam * a) ;
JonFreeman 2:b3c668ec43ac 14 extern void flags_report_cmd (struct singleGparam * a) ;
JonFreeman 2:b3c668ec43ac 15 extern void report_inputs () ;
JonFreeman 0:5d0f270bfc87 16
JonFreeman 2:b3c668ec43ac 17 double feed_rate = 1.0, // global scope, mm per minute. DEFAULTS to 1.0mm per min, very slow.
JonFreeman 2:b3c668ec43ac 18 spindle_rpm = 0.0; // global scope
JonFreeman 0:5d0f270bfc87 19
JonFreeman 0:5d0f270bfc87 20 bool isdigit (int a)
JonFreeman 0:5d0f270bfc87 21 {
JonFreeman 0:5d0f270bfc87 22 if(a > ('0' - 1) && a < ('9' + 1))
JonFreeman 0:5d0f270bfc87 23 return true;
JonFreeman 0:5d0f270bfc87 24 return false;
JonFreeman 0:5d0f270bfc87 25 }
JonFreeman 0:5d0f270bfc87 26
JonFreeman 0:5d0f270bfc87 27 bool isupper (int a)
JonFreeman 0:5d0f270bfc87 28 {
JonFreeman 0:5d0f270bfc87 29 if ((a >= 'A') && (a <= 'Z')) return true;
JonFreeman 0:5d0f270bfc87 30 return false;
JonFreeman 0:5d0f270bfc87 31 }
JonFreeman 0:5d0f270bfc87 32
JonFreeman 0:5d0f270bfc87 33 int tolower (int a)
JonFreeman 0:5d0f270bfc87 34 {
JonFreeman 0:5d0f270bfc87 35 if (isupper(a))
JonFreeman 0:5d0f270bfc87 36 a += 'a' - 'A';
JonFreeman 0:5d0f270bfc87 37 return a;
JonFreeman 0:5d0f270bfc87 38 }
JonFreeman 0:5d0f270bfc87 39
JonFreeman 0:5d0f270bfc87 40
JonFreeman 0:5d0f270bfc87 41 const int goodcodes[] = {0,'a','b','c','i','j','l','r','x','y','z'}; // possible G Code options
JonFreeman 0:5d0f270bfc87 42 const int const_numofcodes = sizeof(goodcodes) / sizeof(int);
JonFreeman 0:5d0f270bfc87 43
JonFreeman 0:5d0f270bfc87 44 int find_char_in_goodcodes (int target) // Returns position of char in goodcodes[], 0 if not found.
JonFreeman 0:5d0f270bfc87 45 {
JonFreeman 0:5d0f270bfc87 46 for (int i = 1; i < const_numofcodes; i++)
JonFreeman 0:5d0f270bfc87 47 if (goodcodes[i] == target)
JonFreeman 0:5d0f270bfc87 48 return i;
JonFreeman 0:5d0f270bfc87 49 return 0;
JonFreeman 0:5d0f270bfc87 50 }
JonFreeman 0:5d0f270bfc87 51
JonFreeman 0:5d0f270bfc87 52 /*
JonFreeman 0:5d0f270bfc87 53 void get_codepositions (struct singleGparam * a, struct Gparams & p)
JonFreeman 0:5d0f270bfc87 54 Only call from "void g0g1cmdcore (struct singleGparam * a, double f_rate)"
JonFreeman 0:5d0f270bfc87 55 Purpose:
JonFreeman 0:5d0f270bfc87 56 G code line may have any number of valid axes or parameters entered in any order or position.
JonFreeman 0:5d0f270bfc87 57 This function detects any X,Y,Z,A,I,J,R entries in 'p' if present and copies values into their
JonFreeman 0:5d0f270bfc87 58 respective positions within singleGparam 'a', setting the 'changed' flag for each to true if found,
JonFreeman 0:5d0f270bfc87 59 false if not found
JonFreeman 0:5d0f270bfc87 60 struct Gparams { // Where possibly messy G code line gets ordered and sorted into
JonFreeman 0:5d0f270bfc87 61 struct singleGparam x, y, z, i, j, r, a, b, c, d; // After sorting, know where to find any X, Y etc values !
JonFreeman 0:5d0f270bfc87 62 } ;
JonFreeman 0:5d0f270bfc87 63 */
JonFreeman 0:5d0f270bfc87 64 void get_codepositions (struct singleGparam * source_array, struct Gparams & dest)
JonFreeman 0:5d0f270bfc87 65 {
JonFreeman 0:5d0f270bfc87 66 //const int goodcodes[] = {0,'a','b','c','i','j','l','r','x','y','z'}; // possible G Code options
JonFreeman 0:5d0f270bfc87 67 //const int const_numofcodes = sizeof(goodcodes) / sizeof(int);
JonFreeman 1:66ee619f206b 68 // source_array is the array filled by function 'void command_line_interpreter ()'.
JonFreeman 1:66ee619f206b 69 // It contains any parameters read from the command line :
JonFreeman 1:66ee619f206b 70 // source_array[i].c may contain 'x' or 'y' etc to tie this entry to one of the 'goodcodes' - or not
JonFreeman 0:5d0f270bfc87 71 int codecnt[const_numofcodes +1];
JonFreeman 0:5d0f270bfc87 72 int codepos[const_numofcodes +1];
JonFreeman 0:5d0f270bfc87 73 int j;
JonFreeman 0:5d0f270bfc87 74 for (j = 0; j < const_numofcodes; j++)
JonFreeman 0:5d0f270bfc87 75 codecnt[j] = codepos[j] = 0; // Zero all results
JonFreeman 0:5d0f270bfc87 76 for (int i = 1; i <= source_array[0].i; i++) { // for number of parameters passed to us here
JonFreeman 0:5d0f270bfc87 77 for(j = 0; j < const_numofcodes; j++) { // for a, for b, ... for x, then y, then z
JonFreeman 0:5d0f270bfc87 78 if (source_array[i].c == goodcodes[j]) {
JonFreeman 0:5d0f270bfc87 79 codecnt[j]++; // Count of number of 'a's, 'b's ... 'x's, 'y's, 'z's. All should be 0 or 1 but could be more
JonFreeman 0:5d0f270bfc87 80 codepos[j] = i; // Identifies the a[?] containing last incidence of goodcodes[j]
JonFreeman 0:5d0f270bfc87 81 }
JonFreeman 0:5d0f270bfc87 82 }
JonFreeman 0:5d0f270bfc87 83 }
JonFreeman 0:5d0f270bfc87 84 dest.x.changed = dest.y.changed = dest.z.changed = dest.a.changed = false;
JonFreeman 0:5d0f270bfc87 85 dest.i.changed = dest.j.changed = dest.r.changed = false;
JonFreeman 0:5d0f270bfc87 86 dest.x.dbl = last_position.x.dbl; // copy previous coordinates in case not re-specified
JonFreeman 1:66ee619f206b 87 dest.y.dbl = last_position.y.dbl;
JonFreeman 1:66ee619f206b 88 dest.z.dbl = last_position.z.dbl;
JonFreeman 1:66ee619f206b 89 dest.a.dbl = last_position.a.dbl;
JonFreeman 1:66ee619f206b 90 dest.i.dbl = last_position.i.dbl;
JonFreeman 0:5d0f270bfc87 91 dest.j.dbl = last_position.j.dbl; dest.r.dbl = last_position.r.dbl;
JonFreeman 0:5d0f270bfc87 92 j = codepos[find_char_in_goodcodes('a')];
JonFreeman 0:5d0f270bfc87 93 if (j) {
JonFreeman 0:5d0f270bfc87 94 dest.a.changed = true;
JonFreeman 0:5d0f270bfc87 95 dest.a.dbl = source_array[j].dbl;
JonFreeman 0:5d0f270bfc87 96 }
JonFreeman 0:5d0f270bfc87 97 j = codepos[find_char_in_goodcodes('x')];
JonFreeman 0:5d0f270bfc87 98 if (j) {
JonFreeman 0:5d0f270bfc87 99 dest.x.changed = true;
JonFreeman 0:5d0f270bfc87 100 dest.x.dbl = source_array[j].dbl;
JonFreeman 0:5d0f270bfc87 101 }
JonFreeman 0:5d0f270bfc87 102 j = codepos[find_char_in_goodcodes('y')];
JonFreeman 0:5d0f270bfc87 103 if (j) {
JonFreeman 0:5d0f270bfc87 104 dest.y.changed = true;
JonFreeman 0:5d0f270bfc87 105 dest.y.dbl = source_array[j].dbl;
JonFreeman 0:5d0f270bfc87 106 }
JonFreeman 0:5d0f270bfc87 107 j = codepos[find_char_in_goodcodes('z')];
JonFreeman 0:5d0f270bfc87 108 if (j) {
JonFreeman 0:5d0f270bfc87 109 dest.z.changed = true;
JonFreeman 0:5d0f270bfc87 110 dest.z.dbl = source_array[j].dbl;
JonFreeman 0:5d0f270bfc87 111 }
JonFreeman 0:5d0f270bfc87 112 j = codepos[find_char_in_goodcodes('i')];
JonFreeman 0:5d0f270bfc87 113 if (j) {
JonFreeman 0:5d0f270bfc87 114 dest.i.changed = true;
JonFreeman 0:5d0f270bfc87 115 dest.i.dbl = source_array[j].dbl;
JonFreeman 0:5d0f270bfc87 116 }
JonFreeman 0:5d0f270bfc87 117 j = codepos[find_char_in_goodcodes('j')];
JonFreeman 0:5d0f270bfc87 118 if (j) {
JonFreeman 0:5d0f270bfc87 119 dest.j.changed = true;
JonFreeman 0:5d0f270bfc87 120 dest.j.dbl = source_array[j].dbl;
JonFreeman 0:5d0f270bfc87 121 }
JonFreeman 0:5d0f270bfc87 122 j = codepos[find_char_in_goodcodes('r')];
JonFreeman 0:5d0f270bfc87 123 if (j) {
JonFreeman 0:5d0f270bfc87 124 dest.r.changed = true;
JonFreeman 0:5d0f270bfc87 125 dest.r.dbl = source_array[j].dbl;
JonFreeman 0:5d0f270bfc87 126 }
JonFreeman 0:5d0f270bfc87 127 }
JonFreeman 0:5d0f270bfc87 128
JonFreeman 1:66ee619f206b 129
JonFreeman 1:66ee619f206b 130 void g2g3cmdcore (struct singleGparam * source_array, int twoorthree) {
JonFreeman 1:66ee619f206b 131 struct Gparams tmp;
JonFreeman 1:66ee619f206b 132 struct pirbufgrain start_point, end_point, centre_point, next_point;
JonFreeman 1:66ee619f206b 133 int state = 0, arc_steps;
JonFreeman 1:66ee619f206b 134 double rad_start, rad_end, start_angle, end_angle, next_angle, swept_angle, angle_step, arc_len, z_step;
JonFreeman 1:66ee619f206b 135 if (twoorthree != 2 && twoorthree != 3) {
JonFreeman 1:66ee619f206b 136 pc.printf("Err got %d when should be 2 or 3", twoorthree);
JonFreeman 1:66ee619f206b 137 return;
JonFreeman 1:66ee619f206b 138 }
JonFreeman 1:66ee619f206b 139 if (twoorthree == 2)
JonFreeman 1:66ee619f206b 140 pc.printf("g2 Clockwise Arc\r\n");
JonFreeman 1:66ee619f206b 141 else
JonFreeman 1:66ee619f206b 142 pc.printf("g3 CounterClockwise Arc\r\n");
JonFreeman 1:66ee619f206b 143 get_codepositions (source_array, tmp); // will overwrite with new where entered
JonFreeman 1:66ee619f206b 144 pc.printf("X %s\r\n", tmp.x.changed ? "T":"F");
JonFreeman 1:66ee619f206b 145 pc.printf("Y %s\r\n", tmp.y.changed ? "T":"F");
JonFreeman 1:66ee619f206b 146 pc.printf("Z %s\r\n", tmp.z.changed ? "T":"F");
JonFreeman 1:66ee619f206b 147 pc.printf("R %s\r\n", tmp.r.changed ? "T":"F");
JonFreeman 1:66ee619f206b 148 pc.printf("I %s\r\n", tmp.i.changed ? "T":"F");
JonFreeman 1:66ee619f206b 149 pc.printf("J %s\r\n", tmp.j.changed ? "T":"F");
JonFreeman 1:66ee619f206b 150 if (!tmp.x.changed || !tmp.y.changed) state |= 0x10000; // Error, X or Y missing
JonFreeman 1:66ee619f206b 151 if (tmp.r.changed && !tmp.i.changed && !tmp.j.changed) state |= 1; // Validated R mode got R not I not J
JonFreeman 1:66ee619f206b 152 if (!tmp.r.changed && tmp.i.changed && tmp.j.changed) state |= 2; // Validated IJ mode not R got I got J
JonFreeman 1:66ee619f206b 153 start_point.x = last_position.x.dbl;
JonFreeman 1:66ee619f206b 154 start_point.y = last_position.y.dbl;
JonFreeman 1:66ee619f206b 155 start_point.z = last_position.z.dbl;
JonFreeman 1:66ee619f206b 156 end_point.x = tmp.x.dbl;
JonFreeman 1:66ee619f206b 157 end_point.y = tmp.y.dbl;
JonFreeman 1:66ee619f206b 158 end_point.z = tmp.z.dbl;
JonFreeman 1:66ee619f206b 159 switch (state) {
JonFreeman 1:66ee619f206b 160 case 1: // Radius format arc
JonFreeman 1:66ee619f206b 161 pc.printf("Valid Radius format arc TO DO - not yet implemeted\r\n");
JonFreeman 1:66ee619f206b 162 break;
JonFreeman 1:66ee619f206b 163 case 2: // Centre format arc ** OFFSETS ARE RELATIVE ** Abs coordinates not catered for
JonFreeman 1:66ee619f206b 164 pc.printf("Valid Centre format arc\r\n");
JonFreeman 1:66ee619f206b 165 centre_point.x = start_point.x + tmp.i.dbl;
JonFreeman 1:66ee619f206b 166 centre_point.y = start_point.y + tmp.j.dbl;
JonFreeman 1:66ee619f206b 167 rad_start = hypot(start_point.x - centre_point.x, start_point.y - centre_point.y);
JonFreeman 1:66ee619f206b 168 rad_end = hypot(end_point.x - centre_point.x, end_point.y - centre_point.y);
JonFreeman 1:66ee619f206b 169 pc.printf("Start point X %.3f, Y %.3f\r\n", start_point.x, start_point.y);
JonFreeman 1:66ee619f206b 170 pc.printf("Centre point X %.3f, Y %.3f\r\n", centre_point.x, centre_point.y);
JonFreeman 1:66ee619f206b 171 pc.printf("End point X %.3f, Y %.3f\r\n", end_point.x, end_point.y);
JonFreeman 1:66ee619f206b 172 pc.printf("Rad start %.3f, Rad end %.3f\r\n", rad_start, rad_end);
JonFreeman 1:66ee619f206b 173 if (fabs(rad_start - rad_end) > 0.001) {
JonFreeman 1:66ee619f206b 174 // if ((rad_start - rad_end) > 0.001 || (rad_start - rad_end) < -0.001) {
JonFreeman 1:66ee619f206b 175 state |= 0x20000;
JonFreeman 1:66ee619f206b 176 pc.printf("Radii mismatch error in g2g3\r\n");
JonFreeman 1:66ee619f206b 177 }
JonFreeman 1:66ee619f206b 178 start_angle = atan2(start_point.y - centre_point.y, start_point.x - centre_point.x);
JonFreeman 1:66ee619f206b 179 end_angle = atan2(end_point.y - centre_point.y, end_point.x - centre_point.x);
JonFreeman 1:66ee619f206b 180 swept_angle = end_angle - start_angle;
JonFreeman 1:66ee619f206b 181 //swept_angle = 0.0; //=IF((B$8=2);IF((H$24>-0.0001);(H$24-2*PI());(H$24))
JonFreeman 1:66ee619f206b 182 // ;IF((H$24>0.0001);(H$24);(H$24+2*PI())))
JonFreeman 1:66ee619f206b 183 if (twoorthree == 2) {
JonFreeman 1:66ee619f206b 184 if (swept_angle > -epsilon)
JonFreeman 1:66ee619f206b 185 swept_angle -= TWO_PI;
JonFreeman 1:66ee619f206b 186 }
JonFreeman 1:66ee619f206b 187 else { // twoorthree is 3
JonFreeman 1:66ee619f206b 188 if (!(swept_angle > epsilon))
JonFreeman 1:66ee619f206b 189 swept_angle += TWO_PI;
JonFreeman 1:66ee619f206b 190 }
JonFreeman 1:66ee619f206b 191 arc_len = fabs(rad_start * swept_angle);
JonFreeman 1:66ee619f206b 192 pc.printf("start_angle %.3f, end_angle %.3f, swept_angle %.3f, arc_len %.3f\r\n", start_angle, end_angle, swept_angle, arc_len);
JonFreeman 1:66ee619f206b 193 arc_steps = (int)(4.0 + fabs(1.7 * rad_end * swept_angle)); // fiddle factors adjusted empirically !
JonFreeman 1:66ee619f206b 194 angle_step = swept_angle / arc_steps;
JonFreeman 1:66ee619f206b 195 next_angle = start_angle;
JonFreeman 1:66ee619f206b 196 z_step = (end_point.z - start_point.z) / arc_steps;
JonFreeman 1:66ee619f206b 197 next_point.z = start_point.z;
JonFreeman 1:66ee619f206b 198 pc.printf("Number of steps = %d, angle_step %.3f\r\n", arc_steps, angle_step);
JonFreeman 1:66ee619f206b 199 for (int i = 0; i < arc_steps; i++) { // cut 'arc_steps' straight lines
JonFreeman 1:66ee619f206b 200 next_angle += angle_step;
JonFreeman 1:66ee619f206b 201 next_point.x = centre_point.x + (rad_start * cos(next_angle));
JonFreeman 1:66ee619f206b 202 next_point.y = centre_point.y + (rad_start * sin(next_angle));
JonFreeman 1:66ee619f206b 203 next_point.z += z_step;
JonFreeman 1:66ee619f206b 204 pc.printf("X %.3f, Y %.3f\r\n", next_point.x, next_point.y);
JonFreeman 1:66ee619f206b 205 Thread::wait(300);
JonFreeman 2:b3c668ec43ac 206 next_point.f_rate = feed_rate;
JonFreeman 2:b3c668ec43ac 207 move_to_XYZ (next_point);
JonFreeman 1:66ee619f206b 208 }
JonFreeman 1:66ee619f206b 209 break; // end of case 2: // Centre format arc ** OFFSETS ARE RELATIVE ** Abs coordinates not catered for
JonFreeman 1:66ee619f206b 210 default: // Input error detected
JonFreeman 1:66ee619f206b 211 pc.printf("Input error detected in g2g3, code %x\r\n", state);
JonFreeman 1:66ee619f206b 212 break;
JonFreeman 1:66ee619f206b 213 } // end of switch(state)
JonFreeman 1:66ee619f206b 214 }
JonFreeman 0:5d0f270bfc87 215
JonFreeman 0:5d0f270bfc87 216 void g0g1cmdcore (struct singleGparam * source_array, double f_rate) // Updates any / all of x, y, z NCOs
JonFreeman 1:66ee619f206b 217 { // Only get here when some G0 or G1 input has been read. G0 or G1 determined by f_rate
JonFreeman 2:b3c668ec43ac 218 struct pirbufgrain ins;//, outs;
JonFreeman 1:66ee619f206b 219 struct Gparams tmp;
JonFreeman 1:66ee619f206b 220 get_codepositions (source_array, tmp); // will overwrite with new where entered
JonFreeman 1:66ee619f206b 221 if (!tmp.x.changed && !tmp.y.changed && !tmp.z.changed) {
JonFreeman 1:66ee619f206b 222 pc.printf("No change in X, Y or Z in G0/G1. Ignoring\r\n");
JonFreeman 1:66ee619f206b 223 return;
JonFreeman 1:66ee619f206b 224 }
JonFreeman 1:66ee619f206b 225 ins.x = tmp.x.dbl;
JonFreeman 1:66ee619f206b 226 ins.y = tmp.y.dbl;
JonFreeman 1:66ee619f206b 227 ins.z = tmp.z.dbl;
JonFreeman 1:66ee619f206b 228 ins.f_rate = f_rate;
JonFreeman 2:b3c668ec43ac 229 move_to_XYZ (ins);
JonFreeman 0:5d0f270bfc87 230 }
JonFreeman 0:5d0f270bfc87 231
JonFreeman 0:5d0f270bfc87 232 void g0cmd (struct singleGparam * a) // Updates any / all of x, y, z NCOs
JonFreeman 0:5d0f270bfc87 233 {
JonFreeman 0:5d0f270bfc87 234 g0g1cmdcore (a, feed_rate_max); // Defined parameter in code
JonFreeman 0:5d0f270bfc87 235 }
JonFreeman 0:5d0f270bfc87 236
JonFreeman 0:5d0f270bfc87 237 void g1cmd (struct singleGparam * a) // Updates any / all of x, y, z NCOs
JonFreeman 0:5d0f270bfc87 238 {
JonFreeman 0:5d0f270bfc87 239 g0g1cmdcore (a, feed_rate); // Settable feed_rate
JonFreeman 0:5d0f270bfc87 240 }
JonFreeman 0:5d0f270bfc87 241
JonFreeman 2:b3c668ec43ac 242 void g2cmd (struct singleGparam * a) { // Clockwise arc
JonFreeman 2:b3c668ec43ac 243 g2g3cmdcore (a, 2);
JonFreeman 2:b3c668ec43ac 244 }
JonFreeman 2:b3c668ec43ac 245
JonFreeman 2:b3c668ec43ac 246 void g3cmd (struct singleGparam * a) { // Counter clockwise arc
JonFreeman 2:b3c668ec43ac 247 g2g3cmdcore (a, 3);
JonFreeman 2:b3c668ec43ac 248 }
JonFreeman 2:b3c668ec43ac 249
JonFreeman 1:66ee619f206b 250 void fcmd (struct singleGparam * a) { // Set Feed Rate command
JonFreeman 1:66ee619f206b 251 if (a[1].dbl < 0.0) {
JonFreeman 2:b3c668ec43ac 252 pc.printf("feed rate %.1f ? Setting to 0\r\n", a[1].dbl);
JonFreeman 1:66ee619f206b 253 a[1].dbl = 0.0;
JonFreeman 1:66ee619f206b 254 }
JonFreeman 1:66ee619f206b 255 if (a[1].dbl > feed_rate_max) {
JonFreeman 2:b3c668ec43ac 256 pc.printf ("Error, can't set feed rate to %.1f, max is %.1f, ", a[1].dbl, feed_rate_max);
JonFreeman 1:66ee619f206b 257 a[1].dbl = feed_rate_max;
JonFreeman 0:5d0f270bfc87 258 }
JonFreeman 2:b3c668ec43ac 259 pc.printf ("Setting feed_rate to %.1f\r\n", a[1].dbl);
JonFreeman 0:5d0f270bfc87 260 feed_rate = a[1].dbl;
JonFreeman 0:5d0f270bfc87 261 }
JonFreeman 2:b3c668ec43ac 262 extern void spindle_control (double ss) ;
JonFreeman 2:b3c668ec43ac 263 extern bool spindle_running () ;
JonFreeman 0:5d0f270bfc87 264
JonFreeman 2:b3c668ec43ac 265 void M3cmd (struct singleGparam * a) { spindle_control (spindle_rpm); }
JonFreeman 2:b3c668ec43ac 266 void M5cmd (struct singleGparam * a) { spindle_control (0.0); }
JonFreeman 0:5d0f270bfc87 267
JonFreeman 0:5d0f270bfc87 268 void scmd (struct singleGparam * a) {
JonFreeman 2:b3c668ec43ac 269 if (fabs(a[1].dbl) > spindle_max) {
JonFreeman 2:b3c668ec43ac 270 pc.printf ("Errror setting spindle RPM, can't set to %.0f, ignoring request\r\n", a[1].dbl);
JonFreeman 2:b3c668ec43ac 271 return;
JonFreeman 0:5d0f270bfc87 272 }
JonFreeman 2:b3c668ec43ac 273 pc.printf ("Setting spindle RPM to %.0f Can set Pos or Neg for fwd/rev\r\n", a[1].dbl);
JonFreeman 2:b3c668ec43ac 274 spindle_rpm = a[1].dbl;
JonFreeman 2:b3c668ec43ac 275 if (spindle_running())
JonFreeman 2:b3c668ec43ac 276 spindle_control (spindle_rpm);
JonFreeman 2:b3c668ec43ac 277 /*
JonFreeman 2:b3c668ec43ac 278
JonFreeman 2:b3c668ec43ac 279 pir_spin = (signed long) (a[1].dbl * spindle_factor);
JonFreeman 2:b3c668ec43ac 280 t = ticks;
JonFreeman 2:b3c668ec43ac 281 while (t == ticks) {} // wait until just after next interrupt
JonFreeman 2:b3c668ec43ac 282 p = mysteppers;
JonFreeman 2:b3c668ec43ac 283 if (pir_spin & 0x80000000)
JonFreeman 2:b3c668ec43ac 284 p |= SDi;
JonFreeman 2:b3c668ec43ac 285 else
JonFreeman 2:b3c668ec43ac 286 p &= ~SDi;
JonFreeman 2:b3c668ec43ac 287 mysteppers = p;
JonFreeman 2:b3c668ec43ac 288 */
JonFreeman 2:b3c668ec43ac 289
JonFreeman 0:5d0f270bfc87 290 }
JonFreeman 0:5d0f270bfc87 291
JonFreeman 2:b3c668ec43ac 292 extern void target_cmd (struct singleGparam * a) ;
JonFreeman 2:b3c668ec43ac 293
JonFreeman 2:b3c668ec43ac 294 void stopcmd (struct singleGparam * a) {pc.printf("Stop ! er, not working yet\r\n");}
JonFreeman 0:5d0f270bfc87 295 void m1cmd (struct singleGparam * a) {pc.printf("m1 Optional Programme Stop\r\n");}
JonFreeman 0:5d0f270bfc87 296 void m3cmd (struct singleGparam * a) {pc.printf("m3 Rotate Spindle Clockwise\r\n");}
JonFreeman 0:5d0f270bfc87 297 void m4cmd (struct singleGparam * a) {pc.printf("m4 Rotate Spindle Counter Clockwise\r\n");}
JonFreeman 0:5d0f270bfc87 298 void m5cmd (struct singleGparam * a) {pc.printf("m5 Stop Spindle\r\n");}
JonFreeman 0:5d0f270bfc87 299 /*void m30cmd (struct singleGparam * a) {pc.printf("m30 Programme End and Rewind\r\n");}
JonFreeman 0:5d0f270bfc87 300 void m47cmd (struct singleGparam * a) {pc.printf("m47 Repeat Prog from First Line\r\n");}
JonFreeman 0:5d0f270bfc87 301 void m48cmd (struct singleGparam * a) {pc.printf("m48 Enable Speed and Feed Override\r\n");}
JonFreeman 0:5d0f270bfc87 302 void m49cmd (struct singleGparam * a) {pc.printf("m49 Disable Speed and Feed Override\r\n");}
JonFreeman 0:5d0f270bfc87 303 void m98cmd (struct singleGparam * a) {pc.printf("m98 Call Subroutine\r\n");}
JonFreeman 0:5d0f270bfc87 304 void m99cmd (struct singleGparam * a) {pc.printf("m99 Return from Subroutine\r\n");}
JonFreeman 0:5d0f270bfc87 305 void g10cmd (struct singleGparam * a) {pc.printf("g10 Coord System Origin Set\r\n");}
JonFreeman 0:5d0f270bfc87 306 void g17cmd (struct singleGparam * a) {pc.printf("g17 XY Plane Select\r\n");}
JonFreeman 0:5d0f270bfc87 307 void g20cmd (struct singleGparam * a) {pc.printf("g20 Inch\r\n");}
JonFreeman 0:5d0f270bfc87 308 void g21cmd (struct singleGparam * a) {pc.printf("g21 mm\r\n");}
JonFreeman 0:5d0f270bfc87 309
JonFreeman 0:5d0f270bfc87 310 void g40cmd (struct singleGparam * a) {pc.printf("g40 Cutter Compensation Off\r\n");}
JonFreeman 0:5d0f270bfc87 311 void g50cmd (struct singleGparam * a) {pc.printf("g50 Reset Scale Factors\r\n");}
JonFreeman 0:5d0f270bfc87 312 void g53cmd (struct singleGparam * a) {pc.printf("g53 Move in Absolute Coordinates\r\n");}
JonFreeman 0:5d0f270bfc87 313 void g90cmd (struct singleGparam * a) {pc.printf("g90 Absolute Distance Mode\r\n");}
JonFreeman 0:5d0f270bfc87 314 */
JonFreeman 2:b3c668ec43ac 315 //void g4cmd (struct singleGparam * a) {pc.printf("g4 Dwell\r\n");}
JonFreeman 2:b3c668ec43ac 316 //void g91p1cmd (struct singleGparam * a) {pc.printf("g91.1 \r\n");}
JonFreeman 0:5d0f270bfc87 317
JonFreeman 2:b3c668ec43ac 318 //void report_inputs () {
JonFreeman 2:b3c668ec43ac 319 void report_ins_cmd (struct singleGparam * a) {
JonFreeman 2:b3c668ec43ac 320 report_inputs();
JonFreeman 2:b3c668ec43ac 321 }
JonFreeman 2:b3c668ec43ac 322 /*void tasktstone (void const * name)
JonFreeman 1:66ee619f206b 323 {
JonFreeman 1:66ee619f206b 324 static int i = 0;
JonFreeman 1:66ee619f206b 325 pc.printf("Arrived at tasktstone\r\n");
JonFreeman 1:66ee619f206b 326 while (true) {
JonFreeman 1:66ee619f206b 327 pc.printf("%s %d\r\n", name, i++);
JonFreeman 1:66ee619f206b 328 Thread::wait(9500);
JonFreeman 1:66ee619f206b 329 osThreadYield();
JonFreeman 1:66ee619f206b 330 }
JonFreeman 2:b3c668ec43ac 331 }*/
JonFreeman 1:66ee619f206b 332
JonFreeman 1:66ee619f206b 333 /*void tasktestcmd (struct singleGparam * a) {
JonFreeman 1:66ee619f206b 334 pc.printf("At tasktestcmd\r\n");
JonFreeman 1:66ee619f206b 335 Thread tasktest (tasktstone, (void *) "Bollocks");
JonFreeman 1:66ee619f206b 336 pc.printf("Leaving tasktestcmd\r\n");
JonFreeman 1:66ee619f206b 337 }
JonFreeman 1:66ee619f206b 338 */
JonFreeman 2:b3c668ec43ac 339 extern void lissajous (double) ;
JonFreeman 2:b3c668ec43ac 340
JonFreeman 1:66ee619f206b 341 void lisscmd (struct singleGparam * a) {
JonFreeman 2:b3c668ec43ac 342 lissajous (feed_rate);
JonFreeman 2:b3c668ec43ac 343 /* if(liss_active) {
JonFreeman 1:66ee619f206b 344 pc.printf("Can not add Lissajous, already running.\r\n");
JonFreeman 1:66ee619f206b 345 }
JonFreeman 1:66ee619f206b 346 else {
JonFreeman 1:66ee619f206b 347 pc.printf("Adding Lissajous task\r\n");
JonFreeman 1:66ee619f206b 348 liss_active = true;
JonFreeman 2:b3c668ec43ac 349 }*/
JonFreeman 1:66ee619f206b 350 }
JonFreeman 2:b3c668ec43ac 351 /*
JonFreeman 0:5d0f270bfc87 352 void drooncmd (struct singleGparam * a)
JonFreeman 0:5d0f270bfc87 353 {
JonFreeman 0:5d0f270bfc87 354 dro.dro_output = true; // Enable continuous dro display update
JonFreeman 0:5d0f270bfc87 355 }
JonFreeman 0:5d0f270bfc87 356 void drooffcmd (struct singleGparam * a)
JonFreeman 0:5d0f270bfc87 357 {
JonFreeman 0:5d0f270bfc87 358 dro.dro_output = false; // Disable continuous dro display update
JonFreeman 0:5d0f270bfc87 359 }
JonFreeman 2:b3c668ec43ac 360 */
JonFreeman 0:5d0f270bfc87 361
JonFreeman 2:b3c668ec43ac 362 /*void g90p1cmd (struct singleGparam * a)
JonFreeman 0:5d0f270bfc87 363 {
JonFreeman 0:5d0f270bfc87 364 pc.printf ("Arrived at function fredcmd with %d parameters\r\n", a[0].i);
JonFreeman 0:5d0f270bfc87 365 for (int i = 1; i <= a[0].i; i++) {
JonFreeman 0:5d0f270bfc87 366 pc.printf ("*%c* ", a[i].c);
JonFreeman 0:5d0f270bfc87 367 pc.printf ("%d, ", a[i].i);
JonFreeman 0:5d0f270bfc87 368 pc.printf ("%f\r\n", a[i].dbl);
JonFreeman 0:5d0f270bfc87 369 }
JonFreeman 0:5d0f270bfc87 370 pc.printf (" endof param list\r\n");
JonFreeman 0:5d0f270bfc87 371 }
JonFreeman 2:b3c668ec43ac 372 */
JonFreeman 0:5d0f270bfc87 373 void menucmd (struct singleGparam * a);
JonFreeman 1:66ee619f206b 374 struct kb_command {
JonFreeman 0:5d0f270bfc87 375 const char * cmd_word; // points to text e.g. "menu"
JonFreeman 0:5d0f270bfc87 376 const char * explan;
JonFreeman 0:5d0f270bfc87 377 void (*f)(struct singleGparam *); // points to function
JonFreeman 1:66ee619f206b 378 } ;
JonFreeman 1:66ee619f206b 379
JonFreeman 1:66ee619f206b 380 struct kb_command const * command_list_ptr = NULL; // Pointer switched between 'input_syntax_check' and 'command_execute'
JonFreeman 1:66ee619f206b 381
JonFreeman 1:66ee619f206b 382 struct kb_command const input_syntax_check [] = {
JonFreeman 1:66ee619f206b 383 {"menu", "Lists available commands, same as ls", menucmd},
JonFreeman 1:66ee619f206b 384 {"ls", "Lists available commands, same as menu", menucmd}
JonFreeman 1:66ee619f206b 385 } ;
JonFreeman 1:66ee619f206b 386
JonFreeman 1:66ee619f206b 387 struct kb_command const command_execute[] = {
JonFreeman 1:66ee619f206b 388 {"menu", "Lists available commands, same as ls", menucmd},
JonFreeman 1:66ee619f206b 389 {"ls", "Lists available commands, same as menu", menucmd},
JonFreeman 0:5d0f270bfc87 390 {"stop", "To Stop the Machine !", stopcmd},
JonFreeman 0:5d0f270bfc87 391 {"f ", "To set Feed Rate mm/min, e.g. f 25", fcmd},
JonFreeman 0:5d0f270bfc87 392 {"s ", "To set Spindle RPM, e.g. S 1250", scmd},
JonFreeman 2:b3c668ec43ac 393 {"m3", "Start Spindle at last 'S'", M3cmd},
JonFreeman 2:b3c668ec43ac 394 {"m5", "Stop Spindle", M5cmd},
JonFreeman 1:66ee619f206b 395 {"g0", "Rapid move", g0cmd},
JonFreeman 0:5d0f270bfc87 396 /*{"m30", "Not Implemented", m30cmd},
JonFreeman 0:5d0f270bfc87 397 {"m47", "Not Implemented", m47cmd},
JonFreeman 0:5d0f270bfc87 398 {"m48", "Not Implemented", m48cmd},
JonFreeman 0:5d0f270bfc87 399 {"m49", "Not Implemented", m49cmd},
JonFreeman 0:5d0f270bfc87 400 {"m98", "Not Implemented", m98cmd},
JonFreeman 0:5d0f270bfc87 401 {"m99", "Not Implemented", m99cmd},
JonFreeman 0:5d0f270bfc87 402 {"m1", "Not Implemented", m1cmd},
JonFreeman 0:5d0f270bfc87 403 {"m3", "Not Implemented", m3cmd},
JonFreeman 0:5d0f270bfc87 404 {"m4", "Not Implemented", m4cmd},
JonFreeman 0:5d0f270bfc87 405 {"m5", "Not Implemented", m5cmd},
JonFreeman 0:5d0f270bfc87 406 {"g10", "Not Implemented", g10cmd},
JonFreeman 0:5d0f270bfc87 407 {"g17", "Not Implemented", g17cmd},
JonFreeman 0:5d0f270bfc87 408 {"g20", "Not Implemented", g20cmd},
JonFreeman 0:5d0f270bfc87 409 {"g21", "Not Implemented", g21cmd},
JonFreeman 0:5d0f270bfc87 410 {"g40", "Not Implemented", g40cmd},
JonFreeman 0:5d0f270bfc87 411 {"g50", "Not Implemented", g50cmd},
JonFreeman 0:5d0f270bfc87 412 {"g90.1", "Not Implemented", g90p1cmd},
JonFreeman 0:5d0f270bfc87 413 {"g91.1", "Not Implemented", g91p1cmd},
JonFreeman 0:5d0f270bfc87 414 {"g90", "Not Implemented", g90cmd},
JonFreeman 0:5d0f270bfc87 415 */
JonFreeman 1:66ee619f206b 416 {"g1", "Linear Interpolation - move straight at current feed rate", g1cmd},
JonFreeman 1:66ee619f206b 417 {"g2", "Helical Interpolation CW (Arc, circle)", g2cmd},
JonFreeman 1:66ee619f206b 418 {"g3", "Helical Interpolation CCW (Arc, circle)", g3cmd},
JonFreeman 2:b3c668ec43ac 419 {"liss", "Run Lissajous pattern generator", lisscmd},
JonFreeman 2:b3c668ec43ac 420 {"flags", "Report System Flags", flags_report_cmd},
JonFreeman 2:b3c668ec43ac 421 {"inputs", "Report State of Input bits", report_ins_cmd},
JonFreeman 2:b3c668ec43ac 422 {"target", "Identify computer device", target_cmd},
JonFreeman 1:66ee619f206b 423 // {"ttest", "Add a task to prove we can, or not", tasktestcmd},
JonFreeman 2:b3c668ec43ac 424 // {"dro on", "Turn dro readout on", drooncmd},
JonFreeman 2:b3c668ec43ac 425 // {"dro off", "Turn dro readout off", drooffcmd}
JonFreeman 0:5d0f270bfc87 426 };
JonFreeman 1:66ee619f206b 427 //const int numof_menu_items = sizeof(kbc2) / sizeof(kb_command);
JonFreeman 1:66ee619f206b 428 //const int numof_menu_items_sc = sizeof(input_syntax_check) / sizeof(kb_command);
JonFreeman 1:66ee619f206b 429 //const int numof_menu_items_ce = sizeof(command_execute) / sizeof(kb_command);
JonFreeman 1:66ee619f206b 430 int numof_menu_items;
JonFreeman 0:5d0f270bfc87 431 void menucmd (struct singleGparam * a)
JonFreeman 0:5d0f270bfc87 432 {
JonFreeman 0:5d0f270bfc87 433 pc.printf("At menucmd function - listing commands:-\r\n");
JonFreeman 0:5d0f270bfc87 434 for(int i = 0; i < numof_menu_items; i++)
JonFreeman 1:66ee619f206b 435 pc.printf("[%s]\t\t%s\r\n", command_list_ptr[i].cmd_word, command_list_ptr[i].explan);
JonFreeman 0:5d0f270bfc87 436 pc.printf("End of List of Commands\r\n");
JonFreeman 0:5d0f270bfc87 437 }
JonFreeman 0:5d0f270bfc87 438
JonFreeman 0:5d0f270bfc87 439 bool isalpha (int c)
JonFreeman 0:5d0f270bfc87 440 {
JonFreeman 0:5d0f270bfc87 441 if ((c >= 'a') && (c <= 'z')) return true;
JonFreeman 0:5d0f270bfc87 442 if ((c >= 'A') && (c <= 'Z')) return true;
JonFreeman 0:5d0f270bfc87 443 return false;
JonFreeman 0:5d0f270bfc87 444 }
JonFreeman 0:5d0f270bfc87 445
JonFreeman 2:b3c668ec43ac 446 void nudger (int code) {
JonFreeman 2:b3c668ec43ac 447 // Using <Ctrl>+ 'F', 'B' for Y, 'L', 'R' for X, 'U', 'D' for Z
JonFreeman 2:b3c668ec43ac 448 // 6 2 12 18 21 4
JonFreeman 2:b3c668ec43ac 449 // struct Gparams dest;
JonFreeman 2:b3c668ec43ac 450 struct pirbufgrain dest;
JonFreeman 2:b3c668ec43ac 451 dest.x = last_position.x.dbl;
JonFreeman 2:b3c668ec43ac 452 dest.y = last_position.y.dbl;
JonFreeman 2:b3c668ec43ac 453 dest.z = last_position.z.dbl;
JonFreeman 2:b3c668ec43ac 454 dest.f_rate = feed_rate;
JonFreeman 2:b3c668ec43ac 455 switch (code) {
JonFreeman 2:b3c668ec43ac 456 case 6: // 'F' move -Y
JonFreeman 2:b3c668ec43ac 457 dest.y -= 0.1;
JonFreeman 2:b3c668ec43ac 458 break;
JonFreeman 2:b3c668ec43ac 459 case 2: // 'B' move +Y
JonFreeman 2:b3c668ec43ac 460 dest.y += 0.1;
JonFreeman 2:b3c668ec43ac 461 break;
JonFreeman 2:b3c668ec43ac 462 case 12: // 'L' move +X
JonFreeman 2:b3c668ec43ac 463 dest.x += 0.1;
JonFreeman 2:b3c668ec43ac 464 break;
JonFreeman 2:b3c668ec43ac 465 case 18: // 'R' move -X
JonFreeman 2:b3c668ec43ac 466 dest.x -= 0.1;
JonFreeman 2:b3c668ec43ac 467 break;
JonFreeman 2:b3c668ec43ac 468 case 21: // 'U' move +Z
JonFreeman 2:b3c668ec43ac 469 dest.z += 0.1;
JonFreeman 2:b3c668ec43ac 470 break;
JonFreeman 2:b3c668ec43ac 471 case 4: // 'D' move -Z
JonFreeman 2:b3c668ec43ac 472 dest.z -= 0.1;
JonFreeman 2:b3c668ec43ac 473 default:
JonFreeman 2:b3c668ec43ac 474 break;
JonFreeman 2:b3c668ec43ac 475 // pc.printf("Nuffink to do in nudger\r\n");
JonFreeman 2:b3c668ec43ac 476 } // end of switch
JonFreeman 2:b3c668ec43ac 477 move_to_XYZ (dest);
JonFreeman 2:b3c668ec43ac 478 }
JonFreeman 2:b3c668ec43ac 479
JonFreeman 0:5d0f270bfc87 480 ////class CLI {
JonFreeman 0:5d0f270bfc87 481
JonFreeman 0:5d0f270bfc87 482 /*
JonFreeman 0:5d0f270bfc87 483 void command_line_interpreter ()
JonFreeman 0:5d0f270bfc87 484 Purpose:
JonFreeman 0:5d0f270bfc87 485
JonFreeman 0:5d0f270bfc87 486 */
JonFreeman 1:66ee619f206b 487 void command_line_interpreter (void const * name)
JonFreeman 0:5d0f270bfc87 488 {
JonFreeman 1:66ee619f206b 489 const int MAX_PARAMS = 10, MAX_CMD_LEN = 120;
JonFreeman 1:66ee619f206b 490 static char cmd_line[MAX_CMD_LEN + 4];
JonFreeman 1:66ee619f206b 491 static struct singleGparam params[MAX_PARAMS + 1];
JonFreeman 1:66ee619f206b 492 static int cl_index = 0, lastalpha = 0;
JonFreeman 2:b3c668ec43ac 493 // pc.printf("Got to cli, Starting cli\r\n");
JonFreeman 1:66ee619f206b 494 if (true) {
JonFreeman 1:66ee619f206b 495 command_list_ptr = command_execute;
JonFreeman 1:66ee619f206b 496 numof_menu_items = sizeof(command_execute) / sizeof(kb_command);
JonFreeman 1:66ee619f206b 497 }
JonFreeman 1:66ee619f206b 498 else {
JonFreeman 1:66ee619f206b 499 command_list_ptr = input_syntax_check;
JonFreeman 1:66ee619f206b 500 numof_menu_items = sizeof(input_syntax_check) / sizeof(kb_command);
JonFreeman 1:66ee619f206b 501 }
JonFreeman 1:66ee619f206b 502 while (true) {
JonFreeman 1:66ee619f206b 503 while (pc.readable()) {
JonFreeman 1:66ee619f206b 504 int ch;
JonFreeman 1:66ee619f206b 505 if (cl_index > MAX_CMD_LEN) { // trap out stupidly long command lines
JonFreeman 1:66ee619f206b 506 pc.printf ("Keyboard Error!! Killing stupidly long command line");
JonFreeman 1:66ee619f206b 507 cl_index = 0;
JonFreeman 1:66ee619f206b 508 }
JonFreeman 1:66ee619f206b 509 ch = tolower(pc.getc());
JonFreeman 2:b3c668ec43ac 510 if (ch == '\r' || ch >= ' ' && ch <= 'z')
JonFreeman 2:b3c668ec43ac 511 pc.printf("%c", ch);
JonFreeman 2:b3c668ec43ac 512 else { // Using <Ctrl>+ 'F', 'B' for Y, 'L', 'R' for X, 'U', 'D' for Z
JonFreeman 2:b3c668ec43ac 513 cl_index = 0; // 6 2 12 18 21 4
JonFreeman 2:b3c668ec43ac 514 pc.printf("[%d]", ch);
JonFreeman 2:b3c668ec43ac 515 nudger (ch);
JonFreeman 2:b3c668ec43ac 516 }
JonFreeman 1:66ee619f206b 517 if(ch != '\r') // was this the 'Enter' key?
JonFreeman 1:66ee619f206b 518 cmd_line[cl_index++] = ch; // added char to command being assembled
JonFreeman 1:66ee619f206b 519 else { // key was CR, may or may not be command to lookup
JonFreeman 1:66ee619f206b 520 cmd_line[cl_index] = 0; // null terminate command string
JonFreeman 1:66ee619f206b 521 if(cl_index) { // If have got some chars to lookup
JonFreeman 1:66ee619f206b 522 int i, wrdlen;
JonFreeman 1:66ee619f206b 523 for (i = 0; i < numof_menu_items; i++) { // Look for input match in command list
JonFreeman 1:66ee619f206b 524 wrdlen = strlen(command_list_ptr[i].cmd_word);
JonFreeman 1:66ee619f206b 525 if(strncmp(command_list_ptr[i].cmd_word, cmd_line, wrdlen) == 0) { // If match found
JonFreeman 1:66ee619f206b 526 bool negflag = false;
JonFreeman 1:66ee619f206b 527 int state = 0, paramindex;
JonFreeman 1:66ee619f206b 528 double fracmul;
JonFreeman 1:66ee619f206b 529 // pc.printf("Found match for word [%s]\r\n", kbc[i].wrd);
JonFreeman 1:66ee619f206b 530 for(paramindex = 0; paramindex < MAX_PARAMS; paramindex++) {
JonFreeman 1:66ee619f206b 531 // Clear out whole set of old parameters ready for anything new on this line
JonFreeman 1:66ee619f206b 532 params[paramindex].i = 0; // for integer parameters
JonFreeman 1:66ee619f206b 533 params[paramindex].c = 0; // for last alpha char, helps tie 'X' to '-23.5' etc
JonFreeman 1:66ee619f206b 534 params[paramindex].dbl = 0.0; // for floating point parameters
JonFreeman 1:66ee619f206b 535 params[paramindex].ul = 0;
JonFreeman 1:66ee619f206b 536 params[paramindex].changed = false;
JonFreeman 1:66ee619f206b 537 }
JonFreeman 1:66ee619f206b 538 paramindex = 0;
JonFreeman 1:66ee619f206b 539 // read any parameters from command line here
JonFreeman 1:66ee619f206b 540 // Using parameters[0] as count of parameters to follow
JonFreeman 1:66ee619f206b 541 while (wrdlen <= cl_index) {
JonFreeman 1:66ee619f206b 542 ch = cmd_line[wrdlen++];
JonFreeman 1:66ee619f206b 543 if(isalpha(ch)) lastalpha = ch;
JonFreeman 1:66ee619f206b 544 if(ch == '-') negflag = true;
JonFreeman 1:66ee619f206b 545 if(ch == '+') negflag = false;
JonFreeman 1:66ee619f206b 546 switch (state) {
JonFreeman 1:66ee619f206b 547 case 0: // looking for start of a number string
JonFreeman 1:66ee619f206b 548 if(isdigit(ch)) { // found first digit of a number string
JonFreeman 1:66ee619f206b 549 paramindex++;
JonFreeman 1:66ee619f206b 550 if(paramindex > MAX_PARAMS) {
JonFreeman 1:66ee619f206b 551 wrdlen = cl_index; // exit condition
JonFreeman 1:66ee619f206b 552 pc.printf("WARNING - too many parameters, ignoring extra\r\n");
JonFreeman 1:66ee619f206b 553 } else {
JonFreeman 1:66ee619f206b 554 params[paramindex].i = ch - '0';
JonFreeman 1:66ee619f206b 555 params[paramindex].c = lastalpha;
JonFreeman 1:66ee619f206b 556 state = 1; // Found first digit char of number string
JonFreeman 1:66ee619f206b 557 }
JonFreeman 0:5d0f270bfc87 558 }
JonFreeman 1:66ee619f206b 559 break;
JonFreeman 1:66ee619f206b 560 case 1: // looking for end of a number string
JonFreeman 1:66ee619f206b 561 if(isdigit(ch)) { // accumulating integer from string
JonFreeman 1:66ee619f206b 562 params[paramindex].i *= 10;
JonFreeman 1:66ee619f206b 563 params[paramindex].i += ch - '0';
JonFreeman 1:66ee619f206b 564 } else { // found non-digit terminating number
JonFreeman 1:66ee619f206b 565 if (ch == '.') {
JonFreeman 1:66ee619f206b 566 state = 2;
JonFreeman 1:66ee619f206b 567 fracmul = 0.1;
JonFreeman 1:66ee619f206b 568 params[paramindex].dbl = (double)params[paramindex].i;
JonFreeman 1:66ee619f206b 569 } else {
JonFreeman 1:66ee619f206b 570 params[0].i++; // count of validated parameters
JonFreeman 1:66ee619f206b 571 state = 0; // Have read past last digit of number string
JonFreeman 1:66ee619f206b 572 if(negflag) {
JonFreeman 1:66ee619f206b 573 params[paramindex].i = -params[paramindex].i;
JonFreeman 1:66ee619f206b 574 negflag = false;
JonFreeman 1:66ee619f206b 575 }
JonFreeman 1:66ee619f206b 576 params[paramindex].dbl = (double)params[paramindex].i;
JonFreeman 1:66ee619f206b 577 }
JonFreeman 1:66ee619f206b 578 }
JonFreeman 1:66ee619f206b 579 break;
JonFreeman 1:66ee619f206b 580 case 2: // looking for fractional part of double
JonFreeman 1:66ee619f206b 581 if(isdigit(ch)) { // accumulating fractional part from string
JonFreeman 1:66ee619f206b 582 params[paramindex].dbl += (double)((ch - '0') * fracmul);
JonFreeman 1:66ee619f206b 583 fracmul /= 10.0;
JonFreeman 1:66ee619f206b 584 } else { // found non-digit terminating double precision number
JonFreeman 0:5d0f270bfc87 585 params[0].i++; // count of validated parameters
JonFreeman 0:5d0f270bfc87 586 state = 0; // Have read past last digit of number string
JonFreeman 0:5d0f270bfc87 587 if(negflag) {
JonFreeman 0:5d0f270bfc87 588 params[paramindex].i = -params[paramindex].i;
JonFreeman 1:66ee619f206b 589 params[paramindex].dbl = -params[paramindex].dbl;
JonFreeman 0:5d0f270bfc87 590 negflag = false;
JonFreeman 0:5d0f270bfc87 591 }
JonFreeman 0:5d0f270bfc87 592 }
JonFreeman 1:66ee619f206b 593 break;
JonFreeman 1:66ee619f206b 594 default:
JonFreeman 1:66ee619f206b 595 break;
JonFreeman 1:66ee619f206b 596 } // end of switch state
JonFreeman 1:66ee619f206b 597 } // end of while wrdlen < cl_index
JonFreeman 1:66ee619f206b 598 // pc.printf("Found match to [%s] with %d parameters\r\n", command_list_ptr[i].wrd, paramindex);
JonFreeman 1:66ee619f206b 599 command_list_ptr[i].f(params); // execute command
JonFreeman 1:66ee619f206b 600 i = numof_menu_items + 1; // to exit for loop
JonFreeman 1:66ee619f206b 601 }
JonFreeman 1:66ee619f206b 602 } // End of for numof_menu_items
JonFreeman 1:66ee619f206b 603 if(i == numof_menu_items)
JonFreeman 1:66ee619f206b 604 pc.printf("No Match Found for CMD [%s]\r\n", cmd_line);
JonFreeman 1:66ee619f206b 605 } // End of If have got some chars to lookup
JonFreeman 2:b3c668ec43ac 606 pc.printf("\r\n>");
JonFreeman 1:66ee619f206b 607 cl_index = lastalpha = 0;
JonFreeman 1:66ee619f206b 608 } // End of else key was CR, may or may not be command to lookup
JonFreeman 1:66ee619f206b 609 } // End of while (pc.readable())
JonFreeman 1:66ee619f206b 610 // pc.printf("cli yielding\r\n");
JonFreeman 1:66ee619f206b 611 osThreadYield(); // Using RTOS on this project
JonFreeman 1:66ee619f206b 612 }
JonFreeman 0:5d0f270bfc87 613 }
JonFreeman 0:5d0f270bfc87 614
JonFreeman 0:5d0f270bfc87 615 ////} cli;
JonFreeman 0:5d0f270bfc87 616