Larus glider flight sensor system 3.9.2024
Software-In-The-Loop test and validation system
Loading...
Searching...
No Matches
EEPROM_emulation.cpp
Go to the documentation of this file.
1/***********************************************************************/
25#include <iostream>
26#include <fstream>
27#include "assert.h"
28#include "stdio.h"
29#include "stdlib.h"
30#include "string.h"
31#include "math.h"
33#include "ascii_support.h"
34
35using namespace std;
36
37#include "EEPROM_emulation.h"
38
39#include <istream>
40#include <string>
41
43
45{
46 if( id < EEPROM_PARAMETER_ID_END && config_parameters[id].identifier == id)
47 return config_parameters[id].value;
48 else
49 return 0.0f;
50}
51
53
55{
56 if( id < EEPROM_PARAMETER_ID_END && config_parameters[id].identifier == id)
57 {
58 value = config_parameters[id].value;
59 return false;
60 }
61
62 return true;
63}
64
66{
67#if EEPROM_WRITES_LOGGED
69 float old_value = config_parameters[id].value;
70 config_parameters[id].value = value;
71 cout << "EEPROM(" << id << ")=" << old_value << "->" << value << endl;
72#endif
73 return false;
74}
75
77{
78 return true;
79}
80
81int
82read_identifier (const char *s)
83{
84 if (s[2] != ' ')
86 int identifier = atoi (s);
87 if ((identifier > 0) && (identifier < EEPROM_PARAMETER_ID_END))
88 return identifier;
89 return EEPROM_PARAMETER_ID_END; // error
90}
91bool lock_EEPROM(bool)
92{
93 return true; // just a stub
94}
95
97{
98 char buf[200];
99 strcpy (buf, basename);
100 strcat (buf, ".EEPROM");
101
102#ifdef _WIN32 // _AUGUST
103 const char *line = NULL;
104 size_t len = 0;
106 string sline;
107
108 inFile.open(buf);
109
110 for (getline(inFile, sline);
111 sline.length();
112 getline(inFile, sline)) {
113 line = sline.c_str();
114#else
115 FILE *fp = fopen(buf, "r");
116 if (fp == NULL)
117 return (EXIT_FAILURE);
118
119 char *line = NULL;
120 size_t len = 0;
121 while ((getline(&line, &len, fp)) != -1)
122 {
123#endif
125 if (identifier == EEPROM_PARAMETER_ID_END)
126 continue;
128 if( param == 0)
129 continue;
130 unsigned name_len = strlen(param->mnemonic);
131 if (0 != strncmp((const char *)(param->mnemonic), (const char *)(line + 3),
132 name_len))
133 continue;
134 if (line[name_len + 4] != '=')
135 continue;
136 float value = atof(line + name_len + 6);
137
138 if( param->is_an_angle)
139 value *= (M_PI / 180.0);
140
141 config_parameters[identifier].identifier = identifier;
142 config_parameters[identifier].value = value;
143
144#ifdef _WIN32 // _AUGUST
145 }
146 inFile.close();
147#else
148 }
149 fclose (fp);
150 if (line)
151 free (line);
152#endif
153
154 return 0;
155}
156
158{
159 char buffer[200];
160 char *next = buffer;
161
163 strcat( buffer, "/config.EEPROM");
165 if ( ! outfile.is_open ())
166 return true;
167
168 for( unsigned index = 0; index < PERSISTENT_DATA_ENTRIES; ++index)
169 {
170 float value;
171 bool result = read_EEPROM_value( PERSISTENT_DATA[index].id, value);
172 if( ! result)
173 {
174 if( PERSISTENT_DATA[index].is_an_angle)
175 value *= 180.0 / M_PI; // format it human readable
176
177 next = buffer;
179 *next++=' ';
181 next = append_string (next," = ");
182 next = my_ftoa (next, value);
183 *next++='\r';
184 *next++='\n';
185 *next=0;
186
187 outfile.write ( (const char *)buffer, next-buffer);
188 }
189 }
190
191 outfile.close ();
192 return false;
193}
194
195
196
197
float configuration(EEPROM_PARAMETER_ID id)
int read_EEPROM_file(char *basename)
bool write_EEPROM_value(EEPROM_PARAMETER_ID id, float value)
bool write_EEPROM_dump(char *basename)
bool read_EEPROM_value(EEPROM_PARAMETER_ID id, float &value)
int read_identifier(const char *s)
bool EEPROM_initialize(void)
const persistent_data_t * find_parameter_from_ID(EEPROM_PARAMETER_ID id)
config_param_type config_parameters[EEPROM_PARAMETER_ID_END]
bool lock_EEPROM(bool)
Replacement on the PC for the nonvolatile memory of the micro-controller.
socklen_t len
char * my_ftoa(char *target, float value)
Simple and fast ASCII converters.
char * format_2_digits(char *target, uint32_t data)
char * append_string(char *target, const char *source)
basically: kind of strcat returning the pointer to the string-end
float s[2][6]
mathematical vector of arbitrary type and size
Definition vector.h:40
vector(void)
Definition vector.h:45
ROM persistent_data_t PERSISTENT_DATA[]
ROM unsigned PERSISTENT_DATA_ENTRIES
this structure describes one persistent parameter
collection of system tuning parameters
#define assert(x)
Definition vector.h:29