1 #include <LiquidCrystal.h>
6 // --------------- ADC related stuffs.... --------------------
8 struct input_cal_t // Struct type for input calibration values
12 int center[MAX_INPUTS];
14 input_cal_t input_cal;
18 int channels; // How many channels should PPM generate for this model ...
19 float stick[8]; // The (potentially recalc'ed) value of stick/input channel.
21 int dr[8]; // The Dual-rate array uses magic numbers :P
22 /* dr[0] = Input channel #1 of 2 for D/R switch #1. 0 means off, 1-4 valid values.
23 dr[1] = Input channel #2 of 2 for D/R switch #1. 0 means off, 1-4 valid values.
24 dr[2] = Input channel #1 of 2 for D/R switch #2. 0 means off, 1-4 valid values.
25 dr[3] = Input channel #2 of 2 for D/R switch #2. 0 means off, 1-4 valid values.
26 dr[4] = D/R value for switch # 1 LOW(off). Value -100 to 100 in steps of 5.
27 dr[5] = D/R value for switch # 1 HIGH(on). Value -100 to 100 in steps of 5.
28 dr[6] = D/R value for switch # 1 LOW(off). Value -100 to 100 in steps of 5.
29 dr[7] = D/R value for switch # 1 HIGH(on). Value -100 to 100 in steps of 5.
32 volatile model_t model;
34 // ----------------- Display related stuffs --------------------
35 LiquidCrystal lcd( 12, 11, 10, 6, 7, 8, 9);
36 // Parameters are: rs, rw, enable, d4, d5, d6, d7 pin numbers.
38 // ----------------- PPM related stuffs ------------------------
39 // The PPM generation is handled by Timer0 interrupts, and needs
40 // all modifiable variables to be global and volatile...
42 //int max_channels = 6; // How many channels should PPM generate ...
43 // Moved to model_t struct...
45 volatile long sum = 0; // Frame-time spent so far
46 volatile int cchannel = 0; // Current channnel
47 volatile bool do_channel = true; // Is next operation a channel or a separator
50 // All time values in usecs
52 // The timing here (and/or in the ISR) needs to be tweaked to provide valid
53 // RC PPM signals accepted by standard RC RX'es and the Microcopter...
55 #define framelength 21500 // Max length of frame
56 #define seplength 400 // Lenght of a channel separator
57 #define chmax 1700 // Max lenght of channel pulse
58 #define chmin 600 // Min length of channel
59 #define chwidht (chmax - chmin)// Useable time of channel pulse
61 // ----------------- Menu/IU related stuffs --------------------
63 // Keys/buttons/switches for UI use, including dual-rate/expo
64 // are digital inputs connected to a 4051 multiplexer, giving
65 // 8 inputs on a single input pin.
75 // Voltage sense pin is connected to a 1/3'd voltage divider.
76 #define BATTERY_CONV (10 * 3 * (5.0f/1024.0f))
77 #define BATTERY_LOW 92
91 EXPOS, // Some radios have "drawn curves", i.e. loopup tables stored in external EEPROM ...
103 // The display/UI is handled only when more
104 // than UI_INTERVAL milliecs has passed since last...
105 #define UI_INTERVAL 250
106 unsigned long last = 0;
116 // ----------------- DEBUG-STUFF --------------------
117 unsigned long prev_loop_time;
118 unsigned long avg_loop_time;
122 // ---------- CODE! -----------------------------------
124 // ---------- Arduino SETUP code ----------------------
126 pinMode(13, OUTPUT); // led
128 pinMode(2, OUTPUT); // s0
129 pinMode(3, OUTPUT); // s1
130 pinMode(4, OUTPUT); // s2
131 pinMode(5, OUTPUT); // e
134 lcd.print("Starting....");
137 Serial.println("Starting....");
144 pinMode(A5, OUTPUT); // PPM output pin
146 set_timer( seplength );
147 Timer1.initialize(framelength);
148 Timer1.attachInterrupt(ISR_timer);
150 displaystate = VALUES;
152 // Arduino believes all pins on Port C are Analog.
153 // In reality they are tri-purpose; ADC, Digital, Digital Interrupts
154 // Unfortunately the interrupt mode is unusable in this scenario, but digital I/O works :P
156 digitalWrite(A2, HIGH);
158 // Debugging: how long does the main loop take on avg...
163 // Setting this here to be sure I do not forget to init' it....
164 // These initializations should be done by read_settings from eeprom,
165 // and this "default model values" should probably be moved
166 // out to a section of read_settings when handling "new model", or
167 // to a separate model_defaults function...
169 model.rev[0] = model.rev[1] = model.rev[2] = model.rev[3] =
170 model.rev[4] = model.rev[5] = model.rev[6] = model.rev[7] = false;
171 model.dr[0] = model.dr[1] = model.dr[2] = model.dr[3] = 0;
172 model.dr[4] = model.dr[5] = model.dr[6] = model.dr[7] = 100;
174 // Initializing the stopwatch timer/clock values...
175 clock_timer = (clock_timer_t){0, 0, 0, false};
178 // ---------- Arduino main loop -----------------------
181 // Determine if the UI needs to run...
183 if ( millis() - last > UI_INTERVAL ) {
191 // Wasting a full I/O pin on battery status monitoring!
192 battery_val = analogRead(1) * BATTERY_CONV;
193 if ( battery_val < BATTERY_LOW ) {
194 digitalWrite(13, 1); // Simulate alarm :P
195 displaystate = BATTERY;
202 if ( displaystate != MENU )
204 // Debugging: how long does the main loop take on avg,
205 // when not handling the UI...
207 avg_loop_time = ( t - prev_loop_time + avg_loop_time ) / 2;
211 // Whoa! Slow down partner! Let everything settle down before proceeding.
215 // ----- Simple support functions used by more complex functions ----
217 void set_ppm_output( bool state )
219 digitalWrite(A5, state); // Hard coded PPM output
222 void set_timer(long time)
224 Timer1.detachInterrupt();
225 Timer1.attachInterrupt(ISR_timer, time);
228 boolean check_key( int key)
230 return ( !keys[key] && prev_keys[key] );
233 void mplx_select(int pin)
236 delayMicroseconds(24);
238 digitalWrite(2, bitRead(pin,0)); // Arduino alias for non-modifying bitshift operation
239 digitalWrite(3, bitRead(pin,1)); // us used to extract individual bits from the int (0..7)
240 digitalWrite(4, bitRead(pin,2)); // Select the appropriate input by setting s1,s2,s3 and e
241 digitalWrite(5, 0); // on the 4051 multiplexer.
243 // May need to slow the following read down to be able to
244 // get fully reliable values from the 4051 multiplex.
245 delayMicroseconds(24);
249 // ----- "Complex" functions follow ---------------------------------
253 int i, r0, r1, r2, adc_in;
255 int num_calibrations = 200;
258 lcd.print("Move controls to");
260 lcd.print("their extremes..");
261 Serial.print("Calibration. Move all controls to their extremes.");
263 for (i=0; i< MAX_INPUTS; i++) {
264 input_cal.min[i] = 1024;
265 input_cal.max[i] = 0;
267 while ( calcount <= num_calibrations )
269 for (i=0; i<=MAX_INPUTS; i++) {
271 adc_in = analogRead(0);
273 // Naive min/max calibration
274 if ( adc_in < input_cal.min[i] ) {
275 input_cal.min[i] = adc_in;
277 if ( adc_in > input_cal.max[i] ) {
278 input_cal.max[i] = adc_in;
286 // TODO: WILL need to do center-point calibration after min-max...
289 lcd.print("Done calibrating");
291 Serial.print("Done calibrating");
295 void read_settings(void)
297 // Dummy. Will be modified to read model settings from EEPROM
298 for (int i=0; i<=7; i++) {
299 input_cal.min[i] = 0;
300 input_cal.center[i] = 512;
301 input_cal.max[i] = 1024;
305 void write_settings(void)
307 // Dummy. Not used anywhere. Will be fleshed out to save settings to EEPROM.
310 void scan_keys ( void )
315 // To get more inputs, another 4051 analog multiplexer is used,
316 // but this time it is used for digital inputs. 8 digital inputs
317 // on one input line, as long as proper debouncing and filtering
318 // is done in hardware :P
319 for (i=0; i<=7; i++) {
320 // To be able to detect that a key has changed state, preserve the previous..
321 prev_keys[i] = keys[i];
323 // Select and read input.
325 keys[i] = digitalRead(A2);
331 void process_inputs(void )
333 int current_input, r0, r1, r2, adc_in;
334 for (current_input=0; current_input<=7; current_input++) {
336 mplx_select(current_input);
337 adc_in = analogRead(0);
339 model.stick[current_input] = ((float)adc_in - (float)input_cal.min[current_input]) / (float)(input_cal.max[current_input]-input_cal.min[current_input]);
340 if ( model.rev[current_input] ) model.stick[current_input] = 1.0f - model.stick[current_input];
347 Timer1.stop(); // Make sure we do not run twice while working :P
351 set_ppm_output( LOW );
354 set_timer(seplength);
358 if ( cchannel >= model.channels )
360 set_ppm_output( HIGH );
361 long framesep = framelength - sum;
366 set_timer ( framesep );
372 set_ppm_output( HIGH );
373 long next_timer = (( chwidht * model.stick[cchannel] ) + chmin);
374 // Do sanity-check of next_timer compared to chmax ...
375 while ( chmax < next_timer ) next_timer--;
378 // Done with channel separator and value,
379 // prepare for next channel...
382 set_timer ( next_timer );
390 for (current_input=0; current_input<=7; current_input++) {
391 int v = (int)(model.stick[current_input] * 100);
393 Serial.print("Input #");
394 Serial.print(current_input);
395 Serial.print(" value: ");
396 Serial.print(model.stick[current_input]);
397 Serial.print(" pct: ");
399 Serial.print(" min: ");
400 Serial.print(input_cal.min[current_input]);
401 Serial.print(" max: ");
402 Serial.print(input_cal.max[current_input]);
405 Serial.print("Battery level is: ");
406 Serial.println(battery_val);
408 Serial.print("Average loop time:");
409 Serial.println(avg_loop_time);
413 void dr_inputselect( int no, int in )
415 if ( model.dr[menu_substate] < 0 ) model.dr[menu_substate] = 4;
416 if ( model.dr[menu_substate] > 4 ) model.dr[menu_substate] = 0;
418 lcd.setCursor(0 , 0);
419 lcd.print("D/R switch ");
422 lcd.setCursor(0 , 1);
427 if ( ! model.dr[menu_substate] ) lcd.print("Off");
428 else lcd.print(model.dr[menu_substate]);
430 if ( check_key(KEY_INC) ) {
431 model.dr[menu_substate]++;
434 else if ( check_key(KEY_DEC) ) {
435 model.dr[menu_substate]--;
448 if ( menu_substate == 4) state = keys[KEY_DR1];
449 else state = keys[KEY_DR2];
451 pos = 4 + (menu_substate - 4) * 2;
454 lcd.setCursor(0 , 0);
455 lcd.print("D/R switch ");
456 lcd.print( menu_substate - 3 );
458 lcd.setCursor(0 , 1);
459 lcd.print( state ? "HI" : "LO" );
460 lcd.print(" Value :");
462 lcd.print( model.dr[pos] );
464 if ( !keys[KEY_INC] ) {
465 if ( model.dr[pos] < 100) model.dr[pos] += 5;
468 else if ( !keys[KEY_DEC] ) {
469 if ( model.dr[pos] > -100) model.dr[pos] -= 5;
481 if ( displaystate != MENU )
484 if ( check_key(KEY_UP) && displaystate == VALUES ) {
485 displaystate = BATTERY;
488 else if ( check_key(KEY_UP) && displaystate == BATTERY ) {
489 displaystate = TIMER;
492 else if ( check_key(KEY_UP) && displaystate == TIMER ) {
493 displaystate = VALUES;
497 else if ( check_key(KEY_DOWN) ) {
503 digitalWrite(13, digitalRead(13) ^ 1 );
505 switch ( displaystate )
509 for (current_input=0; current_input<=7; current_input++) {
510 // In channel value display, do a simple calc
511 // of the LCD row & column location. With 8 channels
512 // we can fit eight channels as percentage values on
513 // a simple 16x2 display...
514 if ( current_input < 4 )
516 col = current_input * 4;
521 col = (current_input-4) * 4;
524 // Overwriting the needed positions with
525 // blanks cause less display-flicker than
526 // actually clearing the display...
527 lcd.setCursor(col, row);
529 lcd.setCursor(col, row);
530 // Display uses percents, while PPM uses ratio....
531 int v = (int)(model.stick[current_input] * 100);
539 lcd.print("Battery level: ");
540 lcd.setCursor(0 , 1);
541 lcd.print( (float)battery_val/10);
543 if ( battery_val < BATTERY_LOW ) lcd.print(" - WARNING");
544 else lcd.print(" - OK");
556 lcd.print("Timer: ");
557 lcd.print( clock_timer.running ? "Running" : "Stopped" );
558 lcd.setCursor(5 , 1);
559 if ( clock_timer.running )
561 clock_timer.value = millis() - (clock_timer.start + clock_timer.init);
563 hours = ( clock_timer.value / 1000 ) / 3600;
564 clock_timer.value = clock_timer.value % 3600000;
565 minutes = ( clock_timer.value / 1000 ) / 60;
566 seconds = ( clock_timer.value / 1000 ) % 60;
571 if ( minutes < 10 ) lcd.print("0");
572 lcd.print( minutes );
574 if ( seconds < 10 ) lcd.print("0");
575 lcd.print( seconds );
577 if ( check_key(KEY_INC) ) {
578 if ( !clock_timer.running && !clock_timer.start )
580 clock_timer.start = millis();
581 clock_timer.value = 0;
582 clock_timer.running = true;
583 } else if ( !clock_timer.running && clock_timer.start ) {
584 clock_timer.start = millis() - clock_timer.value;
585 clock_timer.running = true;
586 } else if ( clock_timer.running ) {
587 clock_timer.running = false;
590 } else if ( check_key(KEY_DEC) ) {
591 if ( !clock_timer.running && clock_timer.start ) {
592 clock_timer.value = 0;
593 clock_timer.start = 0;
594 clock_timer.init = 0;
602 switch ( menu_mainstate )
605 lcd.print("In MENU mode!");
606 lcd.setCursor(0 , 1);
607 lcd.print("Esc UP. Scrl DN.");
609 if ( check_key(KEY_UP) ) {
610 displaystate = VALUES;
613 else if ( check_key(KEY_DOWN) ) {
614 menu_mainstate = INVERTS;
621 if ( menu_substate >= model.channels ) menu_substate = 0;
622 if ( menu_substate < 0) menu_substate = (model.channels - 1);
623 lcd.print("Channel invert");
624 lcd.setCursor(0 , 1);
626 lcd.print(menu_substate+1);
627 lcd.print( (model.rev[menu_substate] ? ": Invert" : ": Normal"));
629 if ( check_key(KEY_UP) ) {
630 menu_mainstate = TOP;
633 else if ( check_key(KEY_DOWN) ) {
634 menu_mainstate = DUALRATES;
638 if ( check_key(KEY_RIGHT) ) {
642 else if ( check_key(KEY_LEFT) ) {
646 else if ( check_key(KEY_INC) || check_key(KEY_DEC) ) {
647 model.rev[menu_substate] ^= 1;
653 if ( menu_substate > 5 ) menu_substate = 0;
654 if ( menu_substate < 0) menu_substate = 5;
656 if ( check_key(KEY_UP) ) {
657 menu_mainstate = INVERTS;
660 if ( check_key(KEY_DOWN) ) {
661 menu_mainstate = EXPOS;
664 if ( check_key(KEY_RIGHT) ) {
668 else if ( check_key(KEY_LEFT) ) {
672 switch (menu_substate)
675 dr_inputselect(0, 0);
678 dr_inputselect(0, 1);
681 dr_inputselect(1, 0);
684 dr_inputselect(1, 1);
698 lcd.print("Input expo curve");
699 lcd.setCursor(0 , 1);
700 lcd.print("Not implemented");
701 // Possible, if input values are mapped to +/- 100 rather than 0..1 ..
702 // plot ( x*(1 - 1.0*cos (x/(20*PI)) )) 0 to 100
703 // Run in wolfram to see result, adjust the 1.0 factor to inc/red effect.
704 // Problem: -100 to 100 is terribly bad presicion, esp. considering that
705 // the values started as 0...1024, and we have 1000usec to "spend" on channels.
706 if ( check_key(KEY_UP ) ) {
707 menu_mainstate = DUALRATES;
710 if ( check_key(KEY_DOWN ) ) {
711 menu_mainstate = DEBUG;
717 lcd.setCursor(0 , 0);
718 lcd.print("Dumping debug to");
719 lcd.setCursor(0 , 1);
720 lcd.print("serial port 0");
722 if ( check_key(KEY_UP ) ) {
723 // FIXME: Remember to update the "Scroll up" state!
724 menu_mainstate = EXPOS;
726 } else if ( check_key(KEY_DOWN ) ) {
727 menu_mainstate = SAVE;
733 lcd.print("Not implemented");
734 lcd.setCursor(0 , 1);
735 lcd.print("Press DOWN...");
736 if ( check_key(KEY_DOWN ) ) menu_mainstate = TOP;