source: firmware/FSC/src/parser.c@ 10910

Last change on this file since 10910 was 10910, checked in by neise, 14 years ago
File size: 13.0 KB
Line 
1#include "parser.h"
2#include "output.h"
3#include "application.h"
4#include "usart.h"
5#include "w5100_spi_interface.h"
6// this method parses the data,
7// which came in via USART
8// later it might as well parse the data from ethernet.
9void parse_ascii() {
10 usart_rx_buffer[USART_RX_BUFFER_SIZE-1] = 0;
11 usart_write_str((pU08)"got:");
12 usart_write_str(usart_rx_buffer);
13
14// look at first byte
15// I hope, I can manage to use one byte commands
16 switch (usart_rx_buffer[0]) {
17 case 'E': // AD7719 enable bitmaps may be set
18 set_ad7719_enable_register();
19 break;
20 case 'e': // ATmega internal ADC enable bitmaps may be set
21 // not supported yet.
22 set_adc_enable_register();
23 break;
24 case 'h':
25 usart_write_str((pU08)"\nheartbeat ");
26 heartbeat_enable = true;
27 if (usart_rx_buffer[1] == '0'){
28 heartbeat_enable = false;
29 usart_write_str((pU08)"off\n");
30 } else {
31 usart_write_str((pU08)"on\n");
32 }
33 break;
34 case 'G': // GET the Temperature channels, which are enabled
35 once_told_you = false;
36 for ( U08 i=0; i<RESISTANCE_CHANNELS/8; ++i ) {
37 ad7719_channels_ready[i]=0;
38 }
39 break;
40
41 case 'g': // GET the voltage/current/humidity channels, which are enabled
42 once_told_you = false;
43 for ( U08 i=0; i<VOLTAGE_REGS; ++i ) {
44 adc_channels_ready[i]=0;
45 }
46 break;
47
48 case 'P':
49 print_ad7719_nicely();
50 break;
51
52 case 'p':
53 print_adc_nicely();
54 break;
55
56 case 's':
57 print_status();
58 break;
59
60 case 'd':
61 usart_write_str((pU08)"\ndebug mode ");
62 debug_mode = true;
63 if (usart_rx_buffer[1] == '0'){
64 debug_mode = false;
65 usart_write_str((pU08)"off\n");
66 } else {
67 usart_write_str((pU08)"on\n");
68 }
69 break;
70 }
71
72
73 usart_write_str((pU08)"\nready?");
74 for (U08 i=0; i<USART_RX_BUFFER_SIZE; ++i)
75 usart_rx_buffer[i] = 0;
76}
77
78void parse_non_human_readable_input() {
79
80
81}
82
83// this guy checks, if an incoming byte is:
84// to be stored, because it belongs to a command
85// to be discarded, because something went wrong, and the bytemade no sense...
86// the end of a commmand, and so the 'real parser' may do his work.
87/*
88void incoming_byte_parser() {
89 static bool receiving_command = false;
90 U08 current_byte = UDR;
91
92 if (!receiving_command) {
93 switch (current_byte) {
94 case 0x01:
95 receiving_command = true;
96 break;
97 }
98 }
99
100 if (receiving_command)
101 {
102 usart_rx_buffer[usart_received_chars] = usart_last_char;
103 usart_received_chars++;
104 }
105
106
107}
108*/
109
110
111// this is the recent MSR parser
112/////////////////////////////////////////////////////////////////////////////////////////////////////
113/////////////// P A R S E R /////////////////////////////////////////////////////////////////////////
114/////////////////////////////////////////////////////////////////////////////////////////////////////
115// this method parses the data,
116// which came in via USART
117// later it might as well parse the data from ethernet.
118void MSR_parser() {
119
120U08 input_number = 0;
121bool enable = false;
122// look at first byte
123// I hope, I can manage to use one byte commands
124 usart_rx_buffer[USART_RX_BUFFER_SIZE-1] = 0;
125 usart_write_char(' ');
126 usart_write_str(usart_rx_buffer);
127 usart_write_char('\n');
128
129
130 if (usart_received_chars >0) {
131 switch (usart_rx_buffer[0]) {
132 case 'E': // user wants to enable/disable something
133 case 'e':
134 enable = true;
135 case 'D':
136 case 'd':
137
138 if (usart_received_chars>=1) { // let's see what.
139 if ( usart_rx_buffer[1] != 'r' &&
140 usart_rx_buffer[1] != 'v' &&
141 usart_rx_buffer[1] != 'h' &&
142 usart_rx_buffer[1] != 'p' )
143 {
144 //print_invalid_enable_statement();
145 break;
146 }
147 }
148
149 input_number = 0;
150 if (usart_received_chars >2) { // lets check the first digit
151 if ( usart_rx_buffer[2] >= '0' && usart_rx_buffer[2] <= '9' ) {
152 input_number = usart_rx_buffer[2] - '0';
153 } else {
154 //print_invalid_enable_statement();
155 break;
156 }
157 }
158 if (usart_received_chars >3) { // lets check the 2nd digit
159 if ( usart_rx_buffer[3] >= '0' && usart_rx_buffer[3] <= '9' ) {
160 input_number = 10*input_number + usart_rx_buffer[3] - '0';
161 } else {
162 // okay as well ... if the second digit is missing ..
163 // we might have trailing spaces
164 // that's okay .. we just accept the first number.
165 }
166 }
167 if (usart_received_chars>2) {
168 usart_write_str((pU08)"\n I will switch ");
169 if (enable)
170 usart_write_str((pU08)"on ");
171 else
172 usart_write_str((pU08)"off ");
173
174 // now we know, what the user wanted ... and we need to do it.
175 switch (usart_rx_buffer[1]) {
176 case 'r':
177 if (enable)
178 ad7719_enables[input_number/8] |= (1<<(input_number%8));
179 else {
180 ad7719_enables[input_number/8] &= ~(1<<(input_number%8));
181 ad7719_channels_ready[input_number/8] &= ~(1<<(input_number%8));
182 }
183 usart_write_str((pU08)" resistance channel ");
184 usart_write_U08(input_number,2);
185 usart_write_char('\n');
186 break;
187 case 'v':
188 if (enable)
189 adc_enables[input_number/8] |= (1<<(input_number%8));
190 else {
191 adc_enables[input_number/8] &= ~(1<<(input_number%8));
192 adc_channels_ready[input_number/8] &= ~(1<<(input_number%8));
193 }
194 usart_write_str((pU08)" voltage channel ");
195 usart_write_U08(input_number,2);
196 usart_write_char('\n');
197 break;
198
199 case 'h':
200 if (enable)
201 adc_enables[1] |= (1<<(input_number%4));
202 else {
203 adc_enables[1] &= ~(1<<(input_number%4));
204 adc_channels_ready[1] &= ~(1<<(input_number%4));
205 }
206 usart_write_str((pU08)" humidity channel ");
207 usart_write_U08(input_number,2);
208 usart_write_char('\n');
209 break;
210 case 'p':
211 if (enable)
212 adc_enables[1] |= (1<<((input_number%4) + 4));
213 else {
214 adc_enables[1] &= ~(1<<((input_number%4) + 4));
215 adc_channels_ready[1] &= ~(1<<((input_number%4) + 4));
216 }
217 usart_write_str((pU08)" pressure channel ");
218 usart_write_U08(input_number,2);
219 usart_write_char('\n');
220 break;
221 default:
222 usart_write_str((pU08)"\n DAMN! this should never happen"
223 "- enable/disable switch-case!\n");
224 break;
225 }
226
227
228
229 }// end of if usart_received_chars>2 --> this should not be necessary at all
230 break;
231
232 case 'b':
233 usart_write_str((pU08)"\nheartbeat ");
234 heartbeat_enable = true;
235 if (usart_rx_buffer[1] == '0'){
236 heartbeat_enable = false;
237 usart_write_str((pU08)"off\n");
238 } else {
239 usart_write_str((pU08)"on\n");
240 }
241 break;
242 case 'G': // GET the Temperature channels, which are enabled
243 ad7719_values_printed = false;
244 reset_resistance_done();
245 reset_resistance_values();
246 break;
247
248 case 'g': // GET the voltage/current/humidity channels, which are enabled
249 adc_values_printed = false;
250 reset_voltage_done();
251 reset_voltage_values();
252 break;
253
254 case 'p':
255 adc_print_endless = true;
256 if (usart_rx_buffer[1] == '0')
257 adc_print_endless = false;
258 break;
259
260 case 'P':
261 ad7719_print_endless = true;
262 if (usart_rx_buffer[1] == '0')
263 ad7719_print_endless = false;
264 break;
265
266 case 's':
267 print_status();
268 //print_adc_enable_status(true);
269 //usart_write_char('\n');
270
271 //print_ad7719_enable_status(true);
272 //usart_write_char('\n');
273
274 //usart_write_str((pU08)"time:");
275 //usart_write_float((float)local_ms/1000 , 1,7);
276 //usart_write_str((pU08)" sec.\n");
277 break;
278
279 case 'S':
280 write_status_via_eth();
281 break;
282
283 case '!':
284 usart_write_str((pU08)"\ndebug mode ");
285 debug_mode = true;
286 if (usart_rx_buffer[1] == '0'){
287 debug_mode = false;
288 usart_write_str((pU08)"off\n");
289 } else {
290 usart_write_str((pU08)"on\n");
291 }
292 break;
293
294 case 'h':
295 case 'H':
296 print_help();
297 break;
298 }
299 } else
300 {
301 // zero bytes received
302 }
303
304
305 for (U08 i=0; i<USART_RX_BUFFER_SIZE; ++i)
306 usart_rx_buffer[i] = 0;
307 usart_received_chars = 0;
308 usart_rx_ready = false;
309} // END of MSR_parser();
310
311
312
313///////////////////////////////////////////////////////////////////////////////////////
314// W5300 incoming commands parser
315void parse_w5300_incoming( U08 bytes_in_w5100_rx_buffer ) {
316
317 if (bytes_in_w5100_rx_buffer == 0) // it was stupid to call this parser without having any bytes downloaded
318 return;
319
320 switch (eth_read_buffer[0]) {
321 case 'w': // *w*rite to register command
322 write_FSC_register();
323 read_FSC_register();
324 break;
325
326 case 'r': // *r*ead from register command
327 read_FSC_register();
328 break;
329
330 case 't': // measure *t*emperature (resistance) channels only
331 reset_resistance_done();
332 simple_acknowledge();
333 break;
334
335 case 'v': // measure *v*oltage channels only
336 reset_voltage_done();
337 simple_acknowledge();
338 break;
339
340 case 'm': // *m*easure active channels commands
341 reset_done();
342 simple_acknowledge();
343 break;
344
345 case 's': // return *s*tatus information
346 // this might take a lot of time... about... 25us per byte .. --> less than 25ms
347 write_status_via_eth();
348 //w5100_set_TX(FSCregister, FSC_REGISTER_LENGTH);
349 break;
350
351 case 'z': // re*z*et ATmega32 and peripherals
352 eth_write_buffer[0]='!';
353 eth_write_buffer[1]='!';
354 w5100_set_TX(eth_write_buffer, 2);
355 // let watchdog occur!
356 // gotta read, how this works...
357 break;
358 }
359}
360
361void write_FSC_register() {
362 FSCregister[eth_read_buffer[2]]=eth_read_buffer[3];
363}
364
365void read_FSC_register() {
366 eth_write_buffer[0]=eth_read_buffer[0];
367 eth_write_buffer[1]=eth_read_buffer[1];
368 eth_write_buffer[2]=eth_read_buffer[2];
369 eth_write_buffer[3]=FSCregister[eth_read_buffer[2]];
370 w5100_set_TX(eth_write_buffer, 4);
371}
372
373void reset_resistance_done(){
374 for (U08 i=0; i < (RESISTANCE_CHANNELS/8); i++){
375 ad7719_channels_ready[i] = 0;
376 }
377}
378void reset_resistance_values(){
379 for (U08 i=0; i < RESISTANCE_CHANNELS; i++){
380 ad7719_values[i] = 0;
381 }
382}
383
384
385void reset_voltage_done(){
386 for (U08 i=0; i < (VOLTAGE_REGS); i++){
387 adc_channels_ready[i] = 0;
388 }
389}
390void reset_voltage_values(){
391 for (U08 i=0; i < VOLTAGE_CHANNELS; i++){
392 adc_values[i] = 0;
393 }
394}
395
396void reset_done(){
397 reset_resistance_done();
398 reset_voltage_done();
399}
400
401void simple_acknowledge(){
402 eth_write_buffer[0]=eth_read_buffer[0];
403 eth_write_buffer[1]=eth_read_buffer[1];
404 w5100_set_TX(eth_write_buffer, 2);
405}
406
407void write_status_via_eth() {
408 // take care: ethernet write buffer is just 32 bytes long.
409 eth_write_str("status: "); eth_writeln_str(nc_U32_to_hex(status));
410 // better switch off interrupts here:
411 // otherwise the numbers in time_sec and time_ms might get corrupt during output.
412 cli();
413 eth_write_str("time_s: "); eth_writeln_str(nc_U32_to_str(time_sec, 10));
414 eth_write_str("mili_s: "); eth_writeln_str(nc_U16_to_str(time_ms, 6));
415 sei();
416
417 eth_write_str("R_ref : "); eth_writeln_str(nc_U16_to_str(ref_resistor, 6));
418
419 eth_writeln_str("VOLTAGES\n");
420 eth_write_str("enable:");
421 for (U08 i=0; i<4 ; i++){
422 eth_write_str(nc_U08_to_bin(adc_enables[i]));
423 // need to tweak here in order to get some nice spaces...
424 eth_write_buffer[eth_write_index] = ' ';
425 eth_write_index++;
426 }
427 eth_writeln_str(nc_U08_to_bin(adc_enables[4]));
428
429 eth_write_str(" done:");
430 for (U08 i=0; i<4 ; i++){
431 eth_write_str(nc_U08_to_bin(adc_channels_ready[i]));
432 // need to tweak here in order to get some nice spaces...
433 eth_write_buffer[eth_write_index] = ' ';
434 eth_write_index++;
435 }
436 eth_writeln_str(nc_U08_to_bin(adc_channels_ready[4]));
437
438 eth_write_str("values:");
439 for (U08 i=0; i<73 ; i++){
440 eth_write_str(nc_U16_to_str(adc_values[i], 6) );
441 // need to tweak here in order to get some nice spaces...
442 eth_write_buffer[eth_write_index] = ' ';
443 eth_write_index++;
444 }
445 eth_writeln_str(nc_U16_to_str(adc_values[73], 6) );
446
447 eth_writeln_str("RESISTANCES\n");
448 eth_write_str("enable:");
449 for (U08 i=0; i<7 ; i++){
450 eth_write_str(nc_U08_to_bin(ad7719_enables[i]));
451 // need to tweak here in order to get some nice spaces...
452 eth_write_buffer[eth_write_index] = ' ';
453 eth_write_index++;
454 }
455 eth_writeln_str(nc_U08_to_bin(ad7719_enables[7]));
456
457 eth_write_str(" done:");
458 for (U08 i=0; i<7 ; i++){
459 eth_write_str(nc_U08_to_bin(ad7719_channels_ready[i]));
460 // need to tweak here in order to get some nice spaces...
461 eth_write_buffer[eth_write_index] = ' ';
462 eth_write_index++;
463 }
464 eth_writeln_str(nc_U08_to_bin(ad7719_channels_ready[7]));
465
466 eth_write_str("values:");
467 for (U08 i=0; i<63 ; i++){
468 eth_write_str(nc_U32_to_str(ad7719_values[i], 10) );
469 // need to tweak here in order to get some nice spaces...
470 eth_write_buffer[eth_write_index] = ' ';
471 eth_write_index++;
472 }
473 eth_writeln_str(nc_U32_to_str(ad7719_values[63], 10) );
474
475
476}
Note: See TracBrowser for help on using the repository browser.