From: Jon Langseth Date: Fri, 2 Sep 2011 16:23:34 +0000 (+0200) Subject: Added saving to and reading from internal EEPROM. In this version, calibration data... X-Git-Url: https://git.defcon.no/?a=commitdiff_plain;h=3c5f990b9bde180af15b5755df660aa11f5b7caf;p=rctxduino Added saving to and reading from internal EEPROM. In this version, calibration data and "default model settings" are saved. Selection of models, and saving/loading of models from menu is not yet implemented. --- diff --git a/source/RCTXDuino/RCTXDuino.pde b/source/RCTXDuino/RCTXDuino.pde index 5d81bc1..d238b75 100644 --- a/source/RCTXDuino/RCTXDuino.pde +++ b/source/RCTXDuino/RCTXDuino.pde @@ -1,8 +1,39 @@ #include #include +#include #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 3 + +// 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,6 +62,7 @@ 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); @@ -40,9 +72,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 @@ -81,6 +110,7 @@ enum { VALUES, BATTERY, TIMER, + CURMODEL, MENU } displaystate; @@ -137,6 +167,8 @@ void setup(){ Serial.begin(9600); Serial.println("Starting...."); delay(500); + + model_defaults(); read_settings(); pinMode(A5, OUTPUT); // PPM output pin @@ -161,19 +193,32 @@ void setup(){ avg_loop_time = t; prev_loop_time = t; - // 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... + + // Initializing the stopwatch timer/clock values... + clock_timer = (clock_timer_t){0, 0, 0, false}; +} + +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 = 6; 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 ----------------------- @@ -262,8 +307,10 @@ void calibrate() for (i=0; i