From 99f12948d7fa4f4dad312d4fe982ad29e7e8db8e Mon Sep 17 00:00:00 2001
From: Jon Langseth <jon.langseth@lilug.no>
Date: Tue, 30 Aug 2011 14:35:52 +0200
Subject: [PATCH] Changed stick values from 0..1 to -100..100, added dual-rate calc.

---
 source/RCTXDuino/RCTXDuino.pde |   65 +++++++++++++++++++++++++++++++++++-----
 1 files changed, 57 insertions(+), 8 deletions(-)

diff --git a/source/RCTXDuino/RCTXDuino.pde b/source/RCTXDuino/RCTXDuino.pde
index b6c8d26..03c126e 100644
--- a/source/RCTXDuino/RCTXDuino.pde
+++ b/source/RCTXDuino/RCTXDuino.pde
@@ -330,15 +330,58 @@ void scan_keys ( void )
 
 void process_inputs(void )
 {
-  int current_input, r0, r1, r2, adc_in;
+  int current_input, adc_in;
+  float min, max; 
+
   for (current_input=0; current_input<=7; current_input++) {
 
     mplx_select(current_input);
     adc_in = analogRead(0);
 
-	// TODO: New format on stick values
+	// New format on stick values
+    if ( adc_in < input_cal.center[current_input] )
+    {
+            min = input_cal.min[current_input];
+            max = input_cal.center[current_input];
+    } 
+    else 
+    {
+            min = input_cal.center[current_input];
+            max = input_cal.max[current_input];
+    }
+    model.stick[current_input] =  100 * ((float)adc_in - min ) / (max - min);
+    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;
+    // Test to see if dualrate-switch #1 applies to channel...
+    if ( ( current_input == ( model.dr[0]-1) ) || ( current_input == ( model.dr[1]-1) ) )
+    {
+            if ( keys[KEY_DR1] )
+                    dr_val = ((float)model.dr[4])/100.0; 
+            else
+                    dr_val = ((float)model.dr[5])/100.0;
+
+            model.stick[current_input] *= dr_val; 
+    }
+    else
+    // Test to see if dualrate-switch #1 applies to channel...
+    if ( ( current_input == ( model.dr[2]-1) ) || ( current_input == ( model.dr[3]-1) ) )
+    {
+            if ( keys[KEY_DR1] )
+                    dr_val = ((float)model.dr[6])/100.0; 
+            else
+                    dr_val = ((float)model.dr[7])/100.0;
+
+            model.stick[current_input] *= dr_val; 
+    }
   }
 }
 
@@ -371,8 +414,14 @@ void ISR_timer(void)
   if ( do_channel )
   {
     set_ppm_output( HIGH );
-	// TODO: New format on stick values
-    long next_timer = (( chwidht * model.stick[cchannel] ) + chmin);
+
+	// New format on stick values
+    // model.stick contains percentages, -100% to 100% in float. To make the timer-handling
+    // here as simple as possible. We want to calc the channel value as a  "ratio-value",
+    // a float in the range 0..1.0. So, by moving the lower bound to 0, then cutting the
+    // 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 ...
     while ( chmax < next_timer ) next_timer--;
     sum += next_timer;
@@ -390,8 +439,8 @@ void serial_debug()
 {
   int current_input;
   for (current_input=0; current_input<=7; current_input++) {
-	// TODO: New format on stick values
-    int v = (int)(model.stick[current_input] * 100);
+	// New format on stick values
+    int v = (int)(model.stick[current_input]);// * 100);
 
     Serial.print("Input #");
     Serial.print(current_input);
@@ -531,8 +580,8 @@ void ui_handler()
         lcd.print("    ");
         lcd.setCursor(col, row);
         // Display uses percents, while PPM uses ratio....
-		// TODO: New format on stick values
-        int v = (int)(model.stick[current_input] * 100);
+		// New format on stick values
+        int v = (int)(model.stick[current_input]);// * 100);
         lcd.print(v);
       }
       break;
-- 
1.7.4.1

