X-Git-Url: https://git.defcon.no/?a=blobdiff_plain;f=source%2FRCTXDuino%2FRCTXDuino.pde;h=1afb5ea66db2dfa7184ce33b5f5ac159f8c60540;hb=refs%2Fheads%2FBranch_7fcb511ebdc1482a0d8a5a200e0e6a3a82c4a3d9;hp=15eada2352a992aee10c1582831ea1b00271bfea;hpb=e78f4811772645a426871626328f597369950fa0;p=rctxduino diff --git a/source/RCTXDuino/RCTXDuino.pde b/source/RCTXDuino/RCTXDuino.pde index 15eada2..1afb5ea 100644 --- a/source/RCTXDuino/RCTXDuino.pde +++ b/source/RCTXDuino/RCTXDuino.pde @@ -1,8 +1,49 @@ -#include +// No longer using HD44780-comaptible display, +// Moving to a brand new world of dot-matrix display tech! +// Using LCD library from http://code.google.com/p/pcd8544/ +#include + #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 +// can/will ber written to EEPROM is done. EEPROM data is +// read/written torectly into/from the data structures using +// pointers, so every time a data-set change occurs, the EEPROM +// format changes as well.. +#define EEPROM_VERSION 7 + +// Some data is stored in fixed locations, e.g.: +// * The EEPROM version number for the stored data (loc 0) +// * The selected model configuration number (loc 1) +// * (add any other fixed-loc's here for doc-purpose) +// This means that any pointer-math-operations need a BASE +// adress to start calc'ing from. This is defined as: +#define EE_BASE_ADDR 10 + +// Having to repeat tedious base-address-calculations for the +// start of model data should be unnessecary. Plus, updating +// what data is stored before the models will mean that each +// of those calculations must be updated. A better approach is +// to define the calculation in a define! +// NOTE: If new data is added in front of the model data, +// this define must be updated! +#define EE_MDL_BASE_ADDR (EE_BASE_ADDR+(sizeof(input_cal_t)+ 10)) + +// Just as a safety-precaution, update/change this if a chip with +// a different internal EEPROM size is used. Atmega328p has 1024 bytes. +#define INT_EEPROM_SIZE 1024 + +#define MAX_MODELS 4 // Nice and random number.. + + // --------------- ADC related stuffs.... -------------------- struct input_cal_t // Struct type for input calibration values @@ -31,10 +72,11 @@ struct model_t */ }; volatile model_t model; +unsigned char current_model; // Using uchar to spend a single byte of mem.. // ----------------- Display related stuffs -------------------- -LiquidCrystal lcd( 12, 11, 10, 6, 7, 8, 9); -// Parameters are: rs, rw, enable, d4, d5, d6, d7 pin numbers. +PCD8544 lcd( 8, 9, 10, 11, 12); +// Param: sclk, sdin, dc, reset, sce // ----------------- PPM related stuffs ------------------------ // The PPM generation is handled by Timer0 interrupts, and needs @@ -52,8 +94,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 -------------------- @@ -72,12 +114,21 @@ 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, BATTERY, TIMER, + CURMODEL, MENU } displaystate; @@ -87,7 +138,7 @@ enum { INVERTS, DUALRATES, EXPOS, // Some radios have "drawn curves", i.e. loopup tables stored in external EEPROM ... - DEBUG, + DEBUG_DUMP, SAVE } menu_mainstate; @@ -111,11 +162,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! ----------------------------------- @@ -128,20 +180,19 @@ void setup(){ pinMode(4, OUTPUT); // s2 pinMode(5, OUTPUT); // e - lcd.begin(16,2); + lcd.begin(84, 48); lcd.print("Starting...."); +#ifdef DEBUG Serial.begin(9600); Serial.println("Starting...."); +#endif + delay(500); + + model_defaults(); read_settings(); - pinMode(A5, OUTPUT); // PPM output pin - do_channel = false; - set_timer( seplength ); - Timer1.initialize(framelength); - Timer1.attachInterrupt(ISR_timer); - displaystate = VALUES; // Arduino believes all pins on Port C are Analog. @@ -153,36 +204,52 @@ 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}; - // Setting this here to be sure I do not forget to init' it.... - // These initializations should be done by read_settings from eeprom, - // and this "default model values" should probably be moved - // out to a section of read_settings when handling "new model", or - // to a separate model_defaults function... - model.channels = 6; + pinMode(A5, OUTPUT); // PPM output pin + do_channel = false; + set_timer( seplength ); + Timer1.initialize(framelength); + Timer1.attachInterrupt(ISR_timer); + + lcd.clear(); + +} + +void model_defaults( void ) +{ + // This function provides default values for model data + // that is not a result of stick input, or in other words: + // provides defautls for all user-configurable model options. + + // Remember to update this when a new option/element is added + // to the model_t struct (preferably before implementing the + // menu code that sets those options ...) + + // This is used when a user wants a new, blank model, a reset + // of a configured model, and (most important) when EEPROM + // data format changes. + // NOTE: This means that stored model conficuration is reset + // to defaults when the EEPROM version/format changes. + 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; model.dr[4] = model.dr[5] = model.dr[6] = model.dr[7] = 100; - // Initializing the stopwatch timer/clock values... - clock_timer = (clock_timer_t){0, 0, 0, false}; } // ---------- 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(); @@ -190,13 +257,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, @@ -205,6 +277,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); @@ -259,8 +332,10 @@ void calibrate() for (i=0; i(__brkval) == 0) { + // if no heap use from end of bss section + free_memory = reinterpret_cast(&free_memory) + - reinterpret_cast(&__bss_end); + } else { + // use from top of stack to heap + free_memory = reinterpret_cast(&free_memory) + - reinterpret_cast(__brkval); + } + return free_memory; +} +#endif