Code for 'Smart Regulator' featured in 'Model Engineer', November 2020 on. Contains all work to August 2020 including all code described. Top level algorithm development is quite spares, leaving some work for you! Any questions - jon@jons-workshop.com

Dependencies:   mbed BufferedSerial Servo2 PCT2075 I2CEeprom FastPWM

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers i2c_2020_06.cpp Source File

i2c_2020_06.cpp

00001 #include "mbed.h"
00002 #include "Alternator.h"
00003 #include "I2CEeprom.h"
00004 
00005 const   int settings_ee_addr    = eeprom_page * eeprom_page_size;
00006 
00007 /*struct  sldandt  {
00008     const uint32_t     min, max, de_fault;  //  min, max, default
00009     const char * txt;     //  description
00010     char    val;
00011 }   ;
00012 */
00013 const char tabletext[] = "User Table entry 0 to 100";
00014 extern  const int numof_op_modes;
00015 
00016 struct  sldandt option_list[] = {
00017     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 0
00018     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 1
00019     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 2
00020     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 3
00021     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 4
00022     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 5
00023     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 6
00024     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 7
00025     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 8
00026     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 9
00027     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 10
00028     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 11
00029     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 12
00030     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 13
00031     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 14
00032     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 15
00033     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 16
00034     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 17
00035     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 18
00036     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 19
00037     {0, 100, 0,     tabletext},       //  Field Current Limit User Entry Table position 20
00038     {0, 120, 3,     "Engine warn-up delay seconds"},
00039     {0, 100, 20,    "Warmup servo position percent"},
00040     {0, numof_op_modes - 1, 0,     "Operating Mode - 0=Engine Only test, 1=Const Voltage, 2=Var Voltage, 3=Current feedback"},
00041     {20, 100, 50,   "Engine speed control 'P' adjust"},
00042     {0, 1, 0,       "0 Servo normal, 1 reverse"},
00043     {0, 100, 0,     "Future 26"},
00044     {0, 100, 0,     "Future 27"},
00045     {0, 100, 0,     "Future 28"},
00046     {0, 100, 0,     "Future 29"},
00047     {0, 100, 0,     "Future 30"},
00048     {0, 100, 0,     "Future 31"},
00049 }   ;
00050 
00051 /*enum    {TABL0, TABL1, TABL2, TABL3, TABL4, TABL5, TABL6, TABL7, TABL8, TABL9, TABL10, TABL11, TABL12, TABL13, TABL14, TABL15, TABL16, 
00052         TABL17, TABL18, TABL19, TABL20,   
00053         WARM_UP_DELAY, WARMUP_SERVO_POS, OP_MODE, SPEED_CTRL_P, SERVO_DIR, FUT30, FUT31 }  ;
00054 */
00055 const   int    numof_eeprom_options    = sizeof(option_list) / sizeof (struct sldandt);
00056 
00057 
00058 //I2CEeprom eeprom  (SDA_PIN, SCL_PIN, 0xa0, eeprom_page_size, 8192, 100000);
00059 extern  I2CEeprom eeprom;   //  (SDA_PIN, SCL_PIN, 0xa0, eeprom_page_size, 8192, 100000);
00060 
00061 /*
00062 class   ee_settings_2020    {
00063     char    new_settings[eeprom_page_size + 2];
00064 public:
00065     ee_settings_2020    ()  ;   //  Constructor
00066     int load    ()  ;
00067     int save    ()  ;
00068     char    rd    (uint32_t   i)  ;
00069     bool    wr   (char c, uint32_t i)  ;           //  Write one setup char value to private buffer 'settings'
00070 }   ;
00071 */
00072     ee_settings_2020::ee_settings_2020  ()  {
00073 //        load    ();   //  Can't use this here, causes core dump
00074     }  
00075     
00076 int ee_settings_2020::load    ()  {
00077     return  eeprom.read (settings_ee_addr, new_settings, eeprom_page_size);
00078 }
00079 
00080 int ee_settings_2020::save    ()  {   //  Write to eeprom only those that need changing
00081     int count = 0;
00082     char    tmp[eeprom_page_size + 2];
00083     int n = eeprom.read (settings_ee_addr, tmp, eeprom_page_size);
00084     for (int i = 0; i < eeprom_page_size; i++)  {
00085         if  (new_settings[i] != tmp[i]) {
00086             count++;
00087             eeprom.write    (settings_ee_addr + i, new_settings[i]);
00088         }
00089     }
00090     return  count;
00091 }
00092 
00093 char    ee_settings_2020::rd    (uint32_t   i)  {
00094     return  new_settings[i];
00095 }
00096 
00097 
00098 bool    ee_settings_2020::wr   (char c, uint32_t i)  {           //  Write one setup char value to private buffer 'settings'
00099     if  (i >= eeprom_page_size)
00100         return  false;
00101     new_settings[i] = c;
00102     return  true;
00103 }
00104 
00105 sldandt *    ee_settings_2020::inform (uint32_t which)  {
00106     if  (which >= numof_eeprom_options) 
00107         return  NULL;
00108     return &option_list[which];
00109 }
00110 
00111 
00112