From e78f4811772645a426871626328f597369950fa0 Mon Sep 17 00:00:00 2001 From: Jon Langseth Date: Fri, 2 Sep 2011 20:15:29 +0200 Subject: [PATCH] Removed a few uses of a number, replaced with (existing) define, removed unused variables, updated comments. --- source/RCTXDuino/RCTXDuino.pde | 41 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 15 deletions(-) diff --git a/source/RCTXDuino/RCTXDuino.pde b/source/RCTXDuino/RCTXDuino.pde index 5d81bc1..15eada2 100644 --- a/source/RCTXDuino/RCTXDuino.pde +++ b/source/RCTXDuino/RCTXDuino.pde @@ -40,9 +40,6 @@ LiquidCrystal lcd( 12, 11, 10, 6, 7, 8, 9); // The PPM generation is handled by Timer0 interrupts, and needs // all modifiable variables to be global and volatile... -//int max_channels = 6; // How many channels should PPM generate ... -// Moved to model_t struct... - volatile long sum = 0; // Frame-time spent so far volatile int cchannel = 0; // Current channnel volatile bool do_channel = true; // Is next operation a channel or a separator @@ -251,7 +248,7 @@ void mplx_select(int pin) void calibrate() { - int i, r0, r1, r2, adc_in; + int i, adc_in; int num_calibrations = 200; lcd.clear(); @@ -307,14 +304,13 @@ void write_settings(void) void scan_keys ( void ) { - int i, r0, r1, r2; boolean key_in; // To get more inputs, another 4051 analog multiplexer is used, // but this time it is used for digital inputs. 8 digital inputs // on one input line, as long as proper debouncing and filtering // is done in hardware :P - for (i=0; i<=7; i++) { + for (int i=0; i<=7; i++) { // To be able to detect that a key has changed state, preserve the previous.. prev_keys[i] = keys[i]; @@ -338,27 +334,35 @@ void process_inputs(void ) model.raw[current_input] = adc_in; // New format on stick values + // The calculations happen around the center point, the values + // need to arrive at 0...100 of the range "center-to-edge", + // and must end up as negative on the ... negative side of center. + if ( adc_in < input_cal.center[current_input] ) { + // The stick is on the negative side, so the range is + // from the lowest possible value to center, and we must + // make this a negative percentage value. max = input_cal.min[current_input]; min = input_cal.center[current_input]; fact = -100; } else { + // The stick is at center, or on the positive side. + // Thus, the range is from center to max, and + // we need positive percentages. min = input_cal.center[current_input]; max = input_cal.max[current_input]; fact = 100; } + // Calculate the percentage that the current stick position is at + // in the given range, referenced to or from center, depending :P model.stick[current_input] = fact * ((float)adc_in - min ) / (max - min); + + // If this input is configured to be reversed, simply do a sign-flip :D if ( model.rev[current_input] ) model.stick[current_input] *= -1; - // Old format on stick values... - /* - model.stick[current_input] = ((float)adc_in - (float)input_cal.min[current_input]) / (float)(input_cal.max[current_input]-input_cal.min[current_input]); - if ( model.rev[current_input] ) model.stick[current_input] = 1.0f - model.stick[current_input]; - */ - // Dual-rate calculation :D // This is very repetitive code. It should be fast, but it may waste code-space. float dr_val; @@ -423,9 +427,11 @@ void ISR_timer(void) // range in half, and finally dividing by 100, we should get the ratio value. // Some loss of presicion occurs, perhaps the algo' should be reconsidered :P long next_timer = (( chwidht * ((model.stick[cchannel]+100)/200) ) + chmin); - // Do sanity-check of next_timer compared to chmax ... + // Do sanity-check of next_timer compared to chmax and chmin... while ( chmax < next_timer ) next_timer--; while ( next_timer < chmin ) next_timer++; + + // Update the sum of elapsed time sum += next_timer; // Done with channel separator and value, @@ -440,7 +446,7 @@ void ISR_timer(void) void serial_debug() { int current_input; - for (current_input=0; current_input<=7; current_input++) { + for (current_input=0; current_input