]> git.defcon.no Git - rctxduino/blob - source/RCTXDuino/RCTXDuino.pde
d560fb4b8e6cedb8e89bcd4b508b118b21612eb2
[rctxduino] / source / RCTXDuino / RCTXDuino.pde
1 #include <LiquidCrystal.h>
2 #include <TimerOne.h>
3 #include <EEPROM.h>
4
5 #define MAX_INPUTS 8
6
7 // Update this _every_ time a change in datastructures that
8 // can/will ber written to EEPROM is done. EEPROM data is
9 // read/written torectly into/from the data structures using
10 // pointers, so every time a data-set change occurs, the EEPROM
11 // format changes as well..
12 #define EEPROM_VERSION 7
13
14 // Some data is stored in fixed locations, e.g.:
15 // * The EEPROM version number for the stored data (loc 0)
16 // * The selected model configuration number (loc 1)
17 // * (add any other fixed-loc's here for doc-purpose)
18 // This means that any pointer-math-operations need a BASE
19 // adress to start calc'ing from. This is defined as:
20 #define EE_BASE_ADDR 10
21
22 // Having to repeat tedious base-address-calculations for the
23 // start of model data should be unnessecary. Plus, updating
24 // what data is stored before the models will mean that each
25 // of those calculations must be updated. A better approach is
26 // to define the calculation in a define!
27 // NOTE: If new data is added in front of the model data,
28 // this define must be updated!
29 #define EE_MDL_BASE_ADDR (EE_BASE_ADDR+(sizeof(input_cal_t)+ 10))
30
31 // Just as a safety-precaution, update/change this if a chip with
32 // a different internal EEPROM size is used. Atmega328p has 1024 bytes.
33 #define INT_EEPROM_SIZE 1024
34
35 #define MAX_MODELS 4 // Nice and random number..
36
37
38 // --------------- ADC related stuffs.... --------------------
39
40 struct input_cal_t // Struct type for input calibration values
41 {
42 int min[MAX_INPUTS];
43 int max[MAX_INPUTS];
44 int center[MAX_INPUTS];
45 } ;
46 input_cal_t input_cal;
47
48 struct model_t
49 {
50 int channels; // How many channels should PPM generate for this model ...
51 float stick[8]; // The (potentially recalc'ed) value of stick/input channel.
52 int raw[8];
53 boolean rev[8];
54 int dr[8]; // The Dual-rate array uses magic numbers :P
55 /* dr[0] = Input channel #1 of 2 for D/R switch #1. 0 means off, 1-4 valid values.
56 dr[1] = Input channel #2 of 2 for D/R switch #1. 0 means off, 1-4 valid values.
57 dr[2] = Input channel #1 of 2 for D/R switch #2. 0 means off, 1-4 valid values.
58 dr[3] = Input channel #2 of 2 for D/R switch #2. 0 means off, 1-4 valid values.
59 dr[4] = D/R value for switch # 1 LOW(off). Value -100 to 100 in steps of 5.
60 dr[5] = D/R value for switch # 1 HIGH(on). Value -100 to 100 in steps of 5.
61 dr[6] = D/R value for switch # 1 LOW(off). Value -100 to 100 in steps of 5.
62 dr[7] = D/R value for switch # 1 HIGH(on). Value -100 to 100 in steps of 5.
63 */
64 };
65 volatile model_t model;
66 unsigned char current_model; // Using uchar to spend a single byte of mem..
67
68 // ----------------- Display related stuffs --------------------
69 LiquidCrystal lcd( 12, 11, 10, 6, 7, 8, 9);
70 // Parameters are: rs, rw, enable, d4, d5, d6, d7 pin numbers.
71
72 // ----------------- PPM related stuffs ------------------------
73 // The PPM generation is handled by Timer0 interrupts, and needs
74 // all modifiable variables to be global and volatile...
75
76 volatile long sum = 0; // Frame-time spent so far
77 volatile int cchannel = 0; // Current channnel
78 volatile bool do_channel = true; // Is next operation a channel or a separator
79
80
81 // All time values in usecs
82 // TODO:
83 // The timing here (and/or in the ISR) needs to be tweaked to provide valid
84 // RC PPM signals accepted by standard RC RX'es and the Microcopter...
85
86 #define framelength 21500 // Max length of frame
87 #define seplength 250 // Lenght of a channel separator
88 #define chmax 1600 // Max lenght of channel pulse
89 #define chmin 480 // Min length of channel
90 #define chwidht (chmax - chmin)// Useable time of channel pulse
91
92 // ----------------- Menu/IU related stuffs --------------------
93
94 // Keys/buttons/switches for UI use, including dual-rate/expo
95 // are digital inputs connected to a 4051 multiplexer, giving
96 // 8 inputs on a single input pin.
97 #define KEY_UP 0
98 #define KEY_DOWN 1
99 #define KEY_RIGHT 2
100 #define KEY_LEFT 3
101 #define KEY_INC 4
102 #define KEY_DEC 5
103 #define KEY_DR1 6
104 #define KEY_DR2 7
105
106 // Voltage sense pin is connected to a 1/3'd voltage divider.
107 #define BATTERY_CONV (10 * 3 * (5.0f/1024.0f))
108 #define BATTERY_LOW 92
109
110 enum {
111 VALUES,
112 BATTERY,
113 TIMER,
114 CURMODEL,
115 MENU
116 }
117 displaystate;
118
119 enum {
120 TOP,
121 INVERTS,
122 DUALRATES,
123 EXPOS, // Some radios have "drawn curves", i.e. loopup tables stored in external EEPROM ...
124 DEBUG,
125 SAVE
126 }
127 menu_mainstate;
128 int menu_substate;
129
130 boolean keys[8];
131 boolean prev_keys[8];
132
133 int battery_val;
134
135 // The display/UI is handled only when more
136 // than UI_INTERVAL milliecs has passed since last...
137 #define UI_INTERVAL 250
138 unsigned long last = 0;
139
140 struct clock_timer_t
141 {
142 unsigned long start;
143 unsigned long init;
144 unsigned long value;
145 boolean running;
146 } clock_timer;
147
148 // ----------------- DEBUG-STUFF --------------------
149 unsigned long prev_loop_time;
150 unsigned long avg_loop_time;
151 unsigned long t;
152
153
154 // ---------- CODE! -----------------------------------
155
156 // ---------- Arduino SETUP code ----------------------
157 void setup(){
158 pinMode(13, OUTPUT); // led
159
160 pinMode(2, OUTPUT); // s0
161 pinMode(3, OUTPUT); // s1
162 pinMode(4, OUTPUT); // s2
163 pinMode(5, OUTPUT); // e
164
165 lcd.begin(16,2);
166 lcd.print("Starting....");
167
168 Serial.begin(9600);
169 Serial.println("Starting....");
170 delay(500);
171
172 model_defaults();
173 read_settings();
174
175 displaystate = VALUES;
176
177 // Arduino believes all pins on Port C are Analog.
178 // In reality they are tri-purpose; ADC, Digital, Digital Interrupts
179 // Unfortunately the interrupt mode is unusable in this scenario, but digital I/O works :P
180 pinMode(A2, INPUT);
181 digitalWrite(A2, HIGH);
182 scan_keys();
183 if ( !keys[KEY_UP])
184 calibrate();
185
186 // Debugging: how long does the main loop take on avg...
187 t = micros();
188 avg_loop_time = t;
189 prev_loop_time = t;
190
191
192 // Initializing the stopwatch timer/clock values...
193 clock_timer = (clock_timer_t){0, 0, 0, false};
194
195 pinMode(A5, OUTPUT); // PPM output pin
196 do_channel = false;
197 set_timer( seplength );
198 Timer1.initialize(framelength);
199 Timer1.attachInterrupt(ISR_timer);
200
201 }
202
203 void model_defaults( void )
204 {
205 // This function provides default values for model data
206 // that is not a result of stick input, or in other words:
207 // provides defautls for all user-configurable model options.
208
209 // Remember to update this when a new option/element is added
210 // to the model_t struct (preferably before implementing the
211 // menu code that sets those options ...)
212
213 // This is used when a user wants a new, blank model, a reset
214 // of a configured model, and (most important) when EEPROM
215 // data format changes.
216 // NOTE: This means that stored model conficuration is reset
217 // to defaults when the EEPROM version/format changes.
218 model.channels = 8;
219 model.rev[0] = model.rev[1] = model.rev[2] = model.rev[3] =
220 model.rev[4] = model.rev[5] = model.rev[6] = model.rev[7] = false;
221 model.dr[0] = model.dr[1] = model.dr[2] = model.dr[3] = 0;
222 model.dr[4] = model.dr[5] = model.dr[6] = model.dr[7] = 100;
223
224 }
225
226 // ---------- Arduino main loop -----------------------
227 void loop ()
228 {
229
230 process_inputs();
231
232 // Wasting a full I/O pin on battery status monitoring!
233 battery_val = analogRead(1) * BATTERY_CONV;
234 if ( battery_val < BATTERY_LOW ) {
235 digitalWrite(13, 1); // Simulate alarm :P
236 displaystate = BATTERY;
237 }
238
239 if ( millis() - last > UI_INTERVAL )
240 {
241 last = millis();
242 ui_handler();
243 }
244
245
246 if ( displaystate != MENU )
247 {
248 // Debugging: how long does the main loop take on avg,
249 // when not handling the UI...
250 t = micros();
251 avg_loop_time = ( t - prev_loop_time + avg_loop_time ) / 2;
252 prev_loop_time = t;
253 }
254
255 // Whoa! Slow down partner! Let everything settle down before proceeding.
256 delay(5);
257 }
258
259 // ----- Simple support functions used by more complex functions ----
260
261 void set_ppm_output( bool state )
262 {
263 digitalWrite(A5, state); // Hard coded PPM output
264 }
265
266 void set_timer(long time)
267 {
268 Timer1.detachInterrupt();
269 Timer1.attachInterrupt(ISR_timer, time);
270 }
271
272 boolean check_key( int key)
273 {
274 return ( !keys[key] && prev_keys[key] );
275 }
276
277 void mplx_select(int pin)
278 {
279 digitalWrite(5, 1);
280 delayMicroseconds(24);
281
282 digitalWrite(2, bitRead(pin,0)); // Arduino alias for non-modifying bitshift operation
283 digitalWrite(3, bitRead(pin,1)); // us used to extract individual bits from the int (0..7)
284 digitalWrite(4, bitRead(pin,2)); // Select the appropriate input by setting s1,s2,s3 and e
285 digitalWrite(5, 0); // on the 4051 multiplexer.
286
287 // May need to slow the following read down to be able to
288 // get fully reliable values from the 4051 multiplex.
289 delayMicroseconds(24);
290
291 }
292
293 // ----- "Complex" functions follow ---------------------------------
294
295 void calibrate()
296 {
297 int i, adc_in;
298 int num_calibrations = 200;
299
300 lcd.clear();
301 lcd.print("Move controls to");
302 lcd.setCursor(0,1);
303 lcd.print("their extremes..");
304 Serial.print("Calibration. Move all controls to their extremes.");
305
306 for (i=0; i<MAX_INPUTS; i++) {
307 input_cal.min[i] = 1024;
308 input_cal.center[i] = 512;
309 input_cal.max[i] = 0;
310 }
311
312 while ( num_calibrations-- )
313 {
314 for (i=0; i<MAX_INPUTS; i++) {
315 mplx_select(i);
316 adc_in = analogRead(0);
317
318 // Naive min/max calibration
319 if ( adc_in < input_cal.min[i] ) {
320 input_cal.min[i] = adc_in;
321 }
322 if ( adc_in > input_cal.max[i] ) {
323 input_cal.max[i] = adc_in;
324 }
325 delay(10);
326 }
327 }
328
329 // TODO: WILL need to do center-point calibration after min-max...
330
331 lcd.clear();
332 lcd.print("Saving to EEPROM");
333 write_calibration();
334 lcd.setCursor(0 , 1);
335 lcd.print("Done calibrating");
336
337 Serial.print("Done calibrating");
338 delay(2000);
339 }
340
341 void write_calibration(void)
342 {
343 int i;
344 unsigned char v;
345 const byte *p;
346
347 // Set p to be a pointer to the start of the input calibration struct.
348 p = (const byte*)(const void*)&input_cal;
349
350 // Iterate through the bytes of the struct...
351 for (i = 0; i < sizeof(input_cal_t); i++)
352 {
353 // Get a byte of data from the struct...
354 v = (unsigned char) *p;
355 // write it to EEPROM
356 EEPROM.write( EE_BASE_ADDR + i, v);
357 // and move the pointer to the next byte in the struct.
358 *p++;
359 }
360 }
361
362 void read_settings(void)
363 {
364 int i;
365 unsigned char v;
366 byte *p;
367
368 v = EEPROM.read(0);
369 if ( v != EEPROM_VERSION )
370 {
371 // All models have been reset. Set the current model to 0
372 current_model = 0;
373 EEPROM.write(1, current_model);
374
375 calibrate();
376 model_defaults();
377 // The following does not yet work...
378 for ( i = 0; i < MAX_MODELS; i++)
379 write_model_settings(i);
380
381
382 // After saving calibration data and model defaults,
383 // update the saved version-identifier to the current ver.
384 EEPROM.write(0, EEPROM_VERSION);
385 }
386
387 // Read calibration values from EEPROM.
388 // This uses simple pointer-arithmetic and byte-by-byte
389 // to put bytes read from EEPROM to the data-struct.
390 p = (byte*)(void*)&input_cal;
391 for (i = 0; i < sizeof(input_cal_t); i++)
392 *p++ = EEPROM.read( EE_BASE_ADDR + i);
393
394 // Get the previously selected model from EEPROM.
395 current_model = EEPROM.read(1);
396 read_model_settings( current_model );
397 }
398
399 void read_model_settings(unsigned char mod_no)
400 {
401 int model_address;
402 int i;
403 unsigned char v;
404 byte *p;
405
406 // Calculate the EEPROM start adress for the given model (mod_no)
407 model_address = EE_MDL_BASE_ADDR + (mod_no * sizeof(model_t));
408
409 // Do not try to write the model to EEPROM if it won't fit.
410 if ( INT_EEPROM_SIZE < (model_address + sizeof(model_t)) )
411 {
412 lcd.clear();
413 lcd.print("Aborting READ");
414 lcd.setCursor(0 , 1);
415 lcd.print("Invalid location");
416 delay(2000);
417 return;
418 }
419
420 lcd.clear();
421 lcd.print("Reading model ");
422 lcd.print( (int)mod_no );
423
424 // Pointer to the start of the model_t data struct,
425 // used for byte-by-byte reading of data...
426 p = (byte*)(void*)&model;
427 for (i = 0; i < sizeof(model_t); i++)
428 *p++ = EEPROM.read( model_address++ );
429
430 serial_dump_model();
431
432 lcd.setCursor(0 , 1);
433 lcd.print("... Loaded.");
434 delay(1000);
435 }
436
437 void write_model_settings(unsigned char mod_no)
438 {
439 int model_address;
440 int i;
441 unsigned char v;
442 byte *p;
443
444 // Calculate the EEPROM start adress for the given model (mod_no)
445 model_address = EE_MDL_BASE_ADDR + (mod_no * sizeof(model_t));
446
447 // Do not try to write the model to EEPROM if it won't fit.
448 if ( INT_EEPROM_SIZE < (model_address + sizeof(model_t)) )
449 {
450 lcd.clear();
451 lcd.print("Aborting SAVE");
452 lcd.setCursor(0 , 1);
453 lcd.print("No room for data");
454 delay(2000);
455 return;
456 }
457
458 lcd.clear();
459 lcd.print("Saving model ");
460 lcd.print( (int)mod_no);
461
462 // Pointer to the start of the model_t data struct,
463 // used for byte-by-byte reading of data...
464 p = (byte*)(void*)&model;
465
466 // Write/serialize the model data struct to EEPROM...
467 for (i = 0; i < sizeof(model_t); i++)
468 EEPROM.write( model_address++, *p++);
469
470 lcd.setCursor(0 , 1);
471 lcd.print(".. done saving.");
472 delay(200);
473 }
474
475 void serial_dump_model ( void )
476 {
477 int i;
478 int model_address;
479 // Calculate the EEPROM start adress for the given model (mod_no)
480 model_address = EE_MDL_BASE_ADDR + (current_model * sizeof(model_t));
481 Serial.print("Current model: ");
482 Serial.println( (int)current_model );
483 Serial.print("Models base addr: ");
484 Serial.println( EE_MDL_BASE_ADDR );
485 Serial.print("Model no: ");
486 Serial.println( current_model, 10 );
487 Serial.print("Size of struct: ");
488 Serial.println( sizeof( model_t) );
489 Serial.print("Model address: ");
490 Serial.println( model_address );
491 Serial.print("End of model: ");
492 Serial.println( model_address + sizeof(model_t) );
493
494 Serial.println();
495
496 Serial.print("Channel reversions: ");
497 for ( i = 0; i<8; i++)
498 {
499 Serial.print(i);
500 Serial.print("=");
501 Serial.print(model.rev[i], 10);
502 Serial.print(" ");
503 }
504 Serial.println();
505
506 Serial.print("DR1 inp 0: ");
507 Serial.println(model.dr[0]);
508 Serial.print("DR1 inp 1: ");
509 Serial.println(model.dr[1]);
510 Serial.print("DR1 LO val: ");
511 Serial.println(model.dr[4]);
512 Serial.print("DR1 HI val: ");
513 Serial.println(model.dr[5]);
514 Serial.print("DR2 inp 0: ");
515 Serial.println(model.dr[2]);
516 Serial.print("DR2 inp 1: ");
517 Serial.println(model.dr[3]);
518 Serial.print("DR2 LO val: ");
519 Serial.println(model.dr[6]);
520 Serial.print("DR2 HI val: ");
521 Serial.println(model.dr[7]);
522
523 for (i=0; i<MAX_INPUTS; i++) {
524 Serial.print("Input #");
525 Serial.print(i);
526 Serial.print(" pct: ");
527 Serial.print(model.stick[i]);
528 Serial.print(" min: ");
529 Serial.print(input_cal.min[i]);
530 Serial.print(" max: ");
531 Serial.print(input_cal.max[i]);
532 Serial.println();
533 }
534 }
535
536 void scan_keys ( void )
537 {
538 boolean key_in;
539
540 // To get more inputs, another 4051 analog multiplexer is used,
541 // but this time it is used for digital inputs. 8 digital inputs
542 // on one input line, as long as proper debouncing and filtering
543 // is done in hardware :P
544 for (int i=0; i<=7; i++) {
545 // To be able to detect that a key has changed state, preserve the previous..
546 prev_keys[i] = keys[i];
547
548 // Select and read input.
549 mplx_select(i);
550 keys[i] = digitalRead(A2);
551 delay(2);
552 }
553 }
554
555
556 void process_inputs(void )
557 {
558 int current_input, adc_in, fact;
559 float min, max;
560
561 for (current_input=0; current_input<MAX_INPUTS; current_input++) {
562
563 mplx_select(current_input);
564 adc_in = analogRead(0);
565
566 model.raw[current_input] = adc_in;
567 // New format on stick values
568 // The calculations happen around the center point, the values
569 // need to arrive at 0...100 of the range "center-to-edge",
570 // and must end up as negative on the ... negative side of center.
571
572 if ( adc_in < input_cal.center[current_input] )
573 {
574 // The stick is on the negative side, so the range is
575 // from the lowest possible value to center, and we must
576 // make this a negative percentage value.
577 max = input_cal.min[current_input];
578 min = input_cal.center[current_input];
579 fact = -100;
580 }
581 else
582 {
583 // The stick is at center, or on the positive side.
584 // Thus, the range is from center to max, and
585 // we need positive percentages.
586 min = input_cal.center[current_input];
587 max = input_cal.max[current_input];
588 fact = 100;
589 }
590 // Calculate the percentage that the current stick position is at
591 // in the given range, referenced to or from center, depending :P
592 model.stick[current_input] = fact * ((float)adc_in - min ) / (max - min);
593
594 // If this input is configured to be reversed, simply do a sign-flip :D
595 if ( model.rev[current_input] ) model.stick[current_input] *= -1;
596
597 // Dual-rate calculation :D
598 // This is very repetitive code. It should be fast, but it may waste code-space.
599 float dr_val;
600 // Test to see if dualrate-switch #1 applies to channel...
601 if ( ( current_input == ( model.dr[0]-1) ) || ( current_input == ( model.dr[1]-1) ) )
602 {
603 if ( !keys[KEY_DR1] )
604 dr_val = ((float)model.dr[4])/100.0;
605 else
606 dr_val = ((float)model.dr[5])/100.0;
607
608 model.stick[current_input] *= dr_val;
609 }
610 else
611 // Test to see if dualrate-switch #1 applies to channel...
612 if ( ( current_input == ( model.dr[2]-1) ) || ( current_input == ( model.dr[3]-1) ) )
613 {
614 if ( !keys[KEY_DR1] )
615 dr_val = ((float)model.dr[6])/100.0;
616 else
617 dr_val = ((float)model.dr[7])/100.0;
618
619 model.stick[current_input] *= dr_val;
620 }
621 }
622 }
623
624
625 void ISR_timer(void)
626 {
627 Timer1.stop(); // Make sure we do not run twice while working :P
628
629 if ( !do_channel )
630 {
631 set_ppm_output( LOW );
632 sum += seplength;
633 do_channel = true;
634 set_timer(seplength);
635 return;
636 }
637
638 if ( cchannel >= model.channels )
639 {
640 set_ppm_output( HIGH );
641 long framesep = framelength - sum;
642
643 sum = 0;
644 do_channel = false;
645 cchannel = 0;
646 set_timer ( framesep );
647 return;
648 }
649
650 if ( do_channel )
651 {
652 set_ppm_output( HIGH );
653
654 // New format on stick values
655 // model.stick contains percentages, -100% to 100% in float. To make the timer-handling
656 // here as simple as possible. We want to calc the channel value as a "ratio-value",
657 // a float in the range 0..1.0. So, by moving the lower bound to 0, then cutting the
658 // range in half, and finally dividing by 100, we should get the ratio value.
659 // Some loss of presicion occurs, perhaps the algo' should be reconsidered :P
660 long next_timer = (( chwidht * ((model.stick[cchannel]+100)/200) ) + chmin);
661 // Do sanity-check of next_timer compared to chmax and chmin...
662 while ( chmax < next_timer ) next_timer--;
663 while ( next_timer < chmin ) next_timer++;
664
665 // Update the sum of elapsed time
666 sum += next_timer;
667
668 // Done with channel separator and value,
669 // prepare for next channel...
670 cchannel++;
671 do_channel = false;
672 set_timer ( next_timer );
673 return;
674 }
675 }
676
677 void serial_debug()
678 {
679 int current_input;
680 for (current_input=0; current_input<MAX_INPUTS; current_input++) {
681
682 Serial.print("Input #");
683 Serial.print(current_input);
684 Serial.print(" pct: ");
685 Serial.print(model.stick[current_input]);
686 Serial.print(" raw value: ");
687 Serial.print(model.raw[current_input]);
688 Serial.print(" min: ");
689 Serial.print(input_cal.min[current_input]);
690 Serial.print(" max: ");
691 Serial.print(input_cal.max[current_input]);
692 Serial.println();
693 }
694 Serial.print("Battery level is: ");
695 Serial.println(battery_val);
696
697 Serial.print("Average loop time:");
698 Serial.println(avg_loop_time);
699
700 Serial.println();
701 }
702
703 void dr_inputselect( int no, int in )
704 {
705 if ( model.dr[menu_substate] < 0 ) model.dr[menu_substate] = 4;
706 if ( model.dr[menu_substate] > 4 ) model.dr[menu_substate] = 0;
707
708 lcd.setCursor(0 , 0);
709 lcd.print("D/R switch ");
710 lcd.print( no + 1 );
711 lcd.print(" ");
712 lcd.setCursor(0 , 1);
713 lcd.print("Input ");
714 lcd.print(in+1);
715
716 lcd.print(": ");
717 if ( ! model.dr[menu_substate] ) lcd.print("Off");
718 else lcd.print(model.dr[menu_substate]);
719
720 if ( check_key(KEY_INC) ) {
721 model.dr[menu_substate]++;
722 return;
723 }
724 else if ( check_key(KEY_DEC) ) {
725 model.dr[menu_substate]--;
726 return;
727 }
728 // Wrap around.
729 return;
730
731 }
732
733 void dr_value()
734 {
735 int pos;
736 int state;
737
738 if ( menu_substate == 4) state = keys[KEY_DR1];
739 else state = keys[KEY_DR2];
740
741 pos = 4 + (menu_substate - 4) * 2;
742 if (state) pos++;
743
744 lcd.setCursor(0 , 0);
745 lcd.print("D/R switch ");
746 lcd.print( menu_substate - 3 );
747 lcd.print(" ");
748 lcd.setCursor(0 , 1);
749 lcd.print( state ? "HI" : "LO" );
750 lcd.print(" Value :");
751
752 lcd.print( model.dr[pos] );
753
754 if ( !keys[KEY_INC] ) {
755 if ( model.dr[pos] < 100) model.dr[pos] += 5;
756 return;
757 }
758 else if ( !keys[KEY_DEC] ) {
759 if ( model.dr[pos] > -100) model.dr[pos] -= 5;
760 return;
761 }
762
763 return;
764 }
765 void ui_handler()
766 {
767 int row;
768 int col;
769 scan_keys();
770
771 if ( displaystate != MENU )
772 {
773 menu_substate = 0;
774 if ( check_key(KEY_UP) && displaystate == VALUES ) {
775 displaystate = BATTERY;
776 return;
777 }
778 else if ( check_key(KEY_UP) && displaystate == BATTERY ) {
779 displaystate = TIMER;
780 return;
781 }
782 else if ( check_key(KEY_UP) && displaystate == TIMER ) {
783 displaystate = CURMODEL;
784 return;
785 }
786 else if ( check_key(KEY_UP) && displaystate == CURMODEL ) {
787 displaystate = VALUES;
788 return;
789 }
790
791 else if ( check_key(KEY_DOWN) ) {
792 displaystate = MENU;
793 return;
794 }
795 }
796
797 digitalWrite(13, digitalRead(13) ^ 1 );
798
799 switch ( displaystate )
800 {
801 case VALUES:
802 int current_input;
803 for (current_input=0; current_input<MAX_INPUTS; current_input++) {
804 // In channel value display, do a simple calc
805 // of the LCD row & column location. With 8 channels
806 // we can fit eight channels as percentage values on
807 // a simple 16x2 display...
808 if ( current_input < 4 )
809 {
810 col = current_input * 4;
811 row = 0;
812 }
813 else
814 {
815 col = (current_input-4) * 4;
816 row = 1;
817 }
818 // Overwriting the needed positions with
819 // blanks cause less display-flicker than
820 // actually clearing the display...
821 lcd.setCursor(col, row);
822 lcd.print(" ");
823 lcd.setCursor(col, row);
824 // Display uses percents, while PPM uses ratio....
825 // New format on stick values
826 lcd.print( (int)model.stick[current_input] );
827 }
828 break;
829
830
831 case BATTERY:
832 lcd.clear();
833 lcd.print("Battery level: ");
834 lcd.setCursor(0 , 1);
835 lcd.print( (float)battery_val/10);
836 lcd.print("V");
837 if ( battery_val < BATTERY_LOW ) lcd.print(" - WARNING");
838 else lcd.print(" - OK");
839 break;
840
841
842
843 case TIMER:
844 unsigned long delta;
845 int hours;
846 int minutes;
847 int seconds;
848
849 lcd.clear();
850 lcd.print("Timer: ");
851 lcd.print( clock_timer.running ? "Running" : "Stopped" );
852 lcd.setCursor(5 , 1);
853 if ( clock_timer.running )
854 {
855 clock_timer.value = millis() - (clock_timer.start + clock_timer.init);
856 }
857 hours = ( clock_timer.value / 1000 ) / 3600;
858 clock_timer.value = clock_timer.value % 3600000;
859 minutes = ( clock_timer.value / 1000 ) / 60;
860 seconds = ( clock_timer.value / 1000 ) % 60;
861 if ( hours ) {
862 lcd.print(hours);
863 lcd.print(":");
864 }
865 if ( minutes < 10 ) lcd.print("0");
866 lcd.print( minutes );
867 lcd.print(":");
868 if ( seconds < 10 ) lcd.print("0");
869 lcd.print( seconds );
870
871 if ( check_key(KEY_INC) ) {
872 if ( !clock_timer.running && !clock_timer.start )
873 {
874 clock_timer.start = millis();
875 clock_timer.value = 0;
876 clock_timer.running = true;
877 } else if ( !clock_timer.running && clock_timer.start ) {
878 clock_timer.start = millis() - clock_timer.value;
879 clock_timer.running = true;
880 } else if ( clock_timer.running ) {
881 clock_timer.running = false;
882 }
883 return;
884 } else if ( check_key(KEY_DEC) ) {
885 if ( !clock_timer.running && clock_timer.start ) {
886 clock_timer.value = 0;
887 clock_timer.start = 0;
888 clock_timer.init = 0;
889 }
890 return;
891 }
892 break;
893
894
895
896 case CURMODEL:
897 lcd.clear();
898 lcd.print("Model #: ");
899 lcd.print( (int)current_model );
900 lcd.setCursor(0 , 1);
901 lcd.print("NAME (not impl)");
902 break;
903
904
905
906 case MENU:
907 lcd.clear();
908 switch ( menu_mainstate )
909 {
910 case TOP:
911 lcd.print("In MENU mode!");
912 lcd.setCursor(0 , 1);
913 lcd.print("Esc UP. Scrl DN.");
914 menu_substate = 0;
915 if ( check_key(KEY_UP) ) {
916 displaystate = VALUES;
917 return;
918 }
919 else if ( check_key(KEY_DOWN) ) {
920 menu_mainstate = INVERTS;
921 return;
922 }
923 break;
924
925
926 case INVERTS:
927 if ( menu_substate >= model.channels ) menu_substate = 0;
928 if ( menu_substate < 0) menu_substate = (model.channels - 1);
929 lcd.print("Channel invert");
930 lcd.setCursor(0 , 1);
931 lcd.print("Ch ");
932 lcd.print(menu_substate+1);
933 lcd.print( (model.rev[menu_substate] ? ": Invert" : ": Normal"));
934
935 if ( check_key(KEY_UP) ) {
936 menu_mainstate = TOP;
937 return;
938 }
939 else if ( check_key(KEY_DOWN) ) {
940 menu_mainstate = DUALRATES;
941 return;
942 }
943
944 if ( check_key(KEY_RIGHT) ) {
945 menu_substate++;
946 return;
947 }
948 else if ( check_key(KEY_LEFT) ) {
949 menu_substate--;
950 return;
951 }
952 else if ( check_key(KEY_INC) || check_key(KEY_DEC) ) {
953 model.rev[menu_substate] ^= 1;
954 return;
955 }
956 break;
957
958 case DUALRATES:
959 if ( menu_substate > 5 ) menu_substate = 0;
960 if ( menu_substate < 0) menu_substate = 5;
961
962 if ( check_key(KEY_UP) ) {
963 menu_mainstate = INVERTS;
964 return;
965 }
966 if ( check_key(KEY_DOWN) ) {
967 menu_mainstate = EXPOS;
968 return;
969 }
970 if ( check_key(KEY_RIGHT) ) {
971 menu_substate++;
972 return;
973 }
974 else if ( check_key(KEY_LEFT) ) {
975 menu_substate--;
976 return;
977 }
978 switch (menu_substate)
979 {
980 case 0:
981 dr_inputselect(0, 0);
982 return;
983 case 1:
984 dr_inputselect(0, 1);
985 return;
986 case 2:
987 dr_inputselect(1, 0);
988 return;
989 case 3:
990 dr_inputselect(1, 1);
991 return;
992 case 4:
993 case 5:
994 dr_value();
995 return;
996 default:
997 menu_substate = 0;
998 break;
999 }
1000 break;
1001
1002 case EXPOS:
1003 //________________
1004 lcd.print("Input expo curve");
1005 lcd.setCursor(0 , 1);
1006 lcd.print("Not implemented");
1007 // Possible, if input values are mapped to +/- 100 rather than 0..1 ..
1008 // plot ( x*(1 - 1.0*cos (x/(20*PI)) )) 0 to 100
1009 // Run in wolfram to see result, adjust the 1.0 factor to inc/red effect.
1010 // Problem: -100 to 100 is terribly bad presicion, esp. considering that
1011 // the values started as 0...1024, and we have 1000usec to "spend" on channels.
1012
1013 // NEW IDEA provided my ivarf @ hig: use bezier curves og hermite curves!
1014 // Looks like a promising idea, but the implementation is still a bitt off
1015 // on the time-horizon :P
1016 if ( check_key(KEY_UP ) ) {
1017 menu_mainstate = DUALRATES;
1018 return;
1019 }
1020 if ( check_key(KEY_DOWN ) ) {
1021 menu_mainstate = DEBUG;
1022 return;
1023 }
1024 break;
1025
1026 case DEBUG:
1027 lcd.setCursor(0 , 0);
1028 lcd.print("Dumping debug to");
1029 lcd.setCursor(0 , 1);
1030 lcd.print("serial port 0");
1031 serial_debug();
1032 if ( check_key(KEY_UP ) ) {
1033 // FIXME: Remember to update the "Scroll up" state!
1034 menu_mainstate = EXPOS;
1035 return;
1036 } else if ( check_key(KEY_DOWN ) ) {
1037 menu_mainstate = SAVE;
1038 return;
1039 }
1040 break;
1041
1042 default:
1043 lcd.print("Not implemented");
1044 lcd.setCursor(0 , 1);
1045 lcd.print("Press DOWN...");
1046 if ( check_key(KEY_DOWN ) ) menu_mainstate = TOP;
1047 }
1048 break;
1049
1050
1051 default:
1052 // Invalid
1053 return;
1054 }
1055
1056 return;
1057 }
1058
1059
1060
1061
1062