X-Git-Url: https://git.defcon.no/?a=blobdiff_plain;f=source%2FRCTXDuino%2FRCTXDuino.pde;h=ebe62ad503afde00aea28315d61861a35b48b52e;hb=9553d62397f79611e4d4f96cf9f224e4193038e3;hp=5254bb67a3748c03345a63be32f9541f78d612c4;hpb=88e0716888b769522dfabb9d7af68d3757e7a53c;p=rctxduino diff --git a/source/RCTXDuino/RCTXDuino.pde b/source/RCTXDuino/RCTXDuino.pde index 5254bb6..ebe62ad 100644 --- a/source/RCTXDuino/RCTXDuino.pde +++ b/source/RCTXDuino/RCTXDuino.pde @@ -2,6 +2,11 @@ #include #include +// Undefine this whenever a "release" or "flight-test" build is made. +// Defining DEBUG sets some crazy values for things like battery warning, +// and includes a whole bunch of debugging-related code ... +#define DEBUG 1 + #define MAX_INPUTS 8 // Update this _every_ time a change in datastructures that @@ -85,8 +90,8 @@ volatile bool do_channel = true; // Is next operation a channel or a sepa #define framelength 21000 // Max length of frame #define seplength 300 // Lenght of a channel separator -#define chmax 1550 // Max lenght of channel pulse -#define chmin 620 // Min length of channel +#define chmax 1600 // Max lenght of channel pulse +#define chmin 495 // Min length of channel #define chwidht (chmax - chmin)// Useable time of channel pulse // ----------------- Menu/IU related stuffs -------------------- @@ -105,7 +110,15 @@ volatile bool do_channel = true; // Is next operation a channel or a sepa // Voltage sense pin is connected to a 1/3'd voltage divider. #define BATTERY_CONV (10 * 3 * (5.0f/1024.0f)) + +#ifdef DEBUG +// The following values are for DEBUGGING ONLY!! #define BATTERY_LOW 92 +#define BATTERY_CRITICAL 0 +#else +#define BATTERY_LOW 92 +#define BATTERY_CRITICAL 92 +#endif enum { VALUES, @@ -121,7 +134,7 @@ enum { INVERTS, DUALRATES, EXPOS, // Some radios have "drawn curves", i.e. loopup tables stored in external EEPROM ... - DEBUG, + DEBUG_DUMP, SAVE } menu_mainstate; @@ -145,11 +158,12 @@ struct clock_timer_t boolean running; } clock_timer; +#ifdef DEBUG // ----------------- DEBUG-STUFF -------------------- unsigned long prev_loop_time; unsigned long avg_loop_time; unsigned long t; - +#endif // ---------- CODE! ----------------------------------- @@ -183,11 +197,12 @@ void setup(){ if ( !keys[KEY_UP]) calibrate(); +#ifdef DEBUG // Debugging: how long does the main loop take on avg... t = micros(); avg_loop_time = t; prev_loop_time = t; - +#endif // Initializing the stopwatch timer/clock values... clock_timer = (clock_timer_t){0, 0, 0, false}; @@ -215,7 +230,7 @@ void model_defaults( void ) // data format changes. // NOTE: This means that stored model conficuration is reset // to defaults when the EEPROM version/format changes. - model.channels = 6; + model.channels = 8; model.rev[0] = model.rev[1] = model.rev[2] = model.rev[3] = model.rev[4] = model.rev[5] = model.rev[6] = model.rev[7] = false; model.dr[0] = model.dr[1] = model.dr[2] = model.dr[3] = 0; @@ -224,15 +239,8 @@ void model_defaults( void ) } // ---------- Arduino main loop ----------------------- -void loop () { - - // Determine if the UI needs to run... - boolean disp; - if ( millis() - last > UI_INTERVAL ) { - last = millis(); - disp = true; - } - else disp = false; +void loop () +{ process_inputs(); @@ -240,13 +248,18 @@ void loop () { battery_val = analogRead(1) * BATTERY_CONV; if ( battery_val < BATTERY_LOW ) { digitalWrite(13, 1); // Simulate alarm :P + } + if ( battery_val < BATTERY_CRITICAL ) { displaystate = BATTERY; } - if ( disp ) - { + if ( millis() - last > UI_INTERVAL ) + { + last = millis(); ui_handler(); } + +#ifdef DEBUG if ( displaystate != MENU ) { // Debugging: how long does the main loop take on avg, @@ -255,6 +268,7 @@ void loop () { avg_loop_time = ( t - prev_loop_time + avg_loop_time ) / 2; prev_loop_time = t; } +#endif // Whoa! Slow down partner! Let everything settle down before proceeding. delay(5); @@ -298,7 +312,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(); @@ -431,7 +445,9 @@ void read_model_settings(unsigned char mod_no) for (i = 0; i < sizeof(model_t); i++) *p++ = EEPROM.read( model_address++ ); +#ifdef DEBUG serial_dump_model(); +#endif lcd.setCursor(0 , 1); lcd.print("... Loaded."); @@ -476,6 +492,7 @@ void write_model_settings(unsigned char mod_no) delay(200); } +#ifdef DEBUG void serial_dump_model ( void ) { int i; @@ -536,17 +553,17 @@ void serial_dump_model ( void ) Serial.println(); } } +#endif 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]; @@ -570,27 +587,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; @@ -655,9 +680,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, @@ -669,10 +696,11 @@ void ISR_timer(void) } } +#ifdef DEBUG void serial_debug() { int current_input; - for (current_input=0; current_input<=7; current_input++) { + for (current_input=0; current_input