Hello and thanks for looking at my post. I am stumped on implementing PROGMEM in a certain context. My code follows below.
#include <inttypes.h>
#include <avr/pgmspace.h>
// sketch is based on an API
// it retrieves floats from large tables
// PROGMEM is needed as the tables are very large
// progmem_read_float function was tested to read floats properly
// but I am having problems with implementation here, see below
// descriptions
static char def1[6] = {'o','n','e', 0, 0, 0};
static char def2[6] = {'t','w','o', 0, 0, 0};
static char def3[6] = {'t','h','r','e','e', 0};
static char def4[6] = {'f','o','u','r', 0, 0};
// float tables
static float Table1[] PROGMEM = {1.1, 2.1, 3.1};
static float Table2[] PROGMEM = {2.2, 3.2, 4.2};
static float Table3[] PROGMEM = {3.3, 4.3, 5.3};
static float Table4[] PROGMEM = {4.4, 5.4, 6.4};
// a structure to hold the tables and definitions
struct objdef
{
char *definition; // holds object definition
float *tbl; // holds object float table
};
// create structures with pointers to table and definitions
objdef t1 = {def1, Table1};
objdef t2 = {def2, Table2};
objdef t3 = {def3, Table3};
objdef t4 = {def4, Table4};
// make an array of the structures
objdef *Objects[] =
{
&t1,
&t2,
&t3,
&t4,
};
// retrieve floats from PROGMEM
// function from http://www.8051projects.net/lofiversion/t22383/avr-gcc-tutorial.html
inline float progmem_read_float(const float *addr)
{
union{
uint16_t i[2];
float f;
} u;
u.i[0]=pgm_read_word((PGM_P) addr);
u.i[1]=pgm_read_word((PGM_P) addr + 2);
return(u.f);
}
void setup()
{
Serial.begin(9600);
}
void loop()
{
float *flptr;
float fl; // float value
int i;
int j;
// show retrieved float tables
for (i=0; i<4; i++)
{
// show object definition
Serial.println((String) Objects[i]->definition);
// get pointer to table
// Problem Here
flptr = progmem_read_float(Objects[i]->tbl);
// show float table for object
for (j=0; j<3; j++)
{
Serial.print(" j[");
Serial.print(j);
Serial.print("]=");
// Problem Here
fl = progmem_read_float(*flptr++);
Serial.println(fl, 2);
}
Serial.println("");
}
}
In the two lines following 'Problem Here' I have tried all kinds of things, casting (float ), passing the pointer to a temporary variable, etc without any luck, so I left it as-is (the compiler will throw an error about float and float not being happy with each-other. I would really appreciate it if someone could show me how this should be done.
Muchos Gracias and Merry Christmas !
J
edit: i accidentally a word, a few clarifications