cancel
Showing results for 
Search instead for 
Did you mean: 

How to import a long array of float constants into a project

FredS
Senior

    Dear forum members,

I work on a project that repeatedly reads 3692 pixel intensity values from a linear CCD pixel-array. The readout process and export of the data via USB is working fine.

Now I want to enhance the application by multiplying each pixel value with its specific correction factor to compensate for the individual sensitivity. I struggle to find the right method to import the correction values from a text-file.

At first I tried to add a user include file that assigns the float values (in the range 0.90 to 1.10) to the array 'g_PixFactor' that is declared in the 'main.c' file. This approach produces errors like "re-definition of array 'g_PixFactor' ". I also tried to do the declaration and the assignment of the values in the include file, which gave other complaints.

 

My questions are:

   - Is the concept of importing values by an include file a workable method to solve my challenge? 

   - Can a more experienced forum member provide me the steps to get the correction values from a file assign to the float array?

 

I already found out I need to specify the float constants with 'f' as suffix to prevent interpretation as int value.

 

Some background: I work on a PC with Windows11 and just have done a clean install of STM32CUBEIDE32 v 1.19.0 after I experienced many strange errors in pieces of code that were fine before the last upgrade. The target HW is a STM32H723VG on a WeAct board.

 

As always, thanks in advance for spending time on my request,

greetings from the Netherlands,

                Fred Schimmel

1 ACCEPTED SOLUTION

Accepted Solutions

These are all text files. Doesn't matter which program creates them, as long as they all live in the project somewhere and get compiled. You'll likely need to format the data into an array in some tool.

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

7 REPLIES 7
TDK
Super User

If they are static, this will put them into FLASH where they can be read as needed:

// in floats.h:
extern const float gArray[1024];

// in floats.c:
const float gArray[1024] = {1, 2, 3, 4, ...};

// in any other *.c file you want to use them in:
#include "floats.h"

...

    float x = gArray[124];

 

If you feel a post has answered your question, please click "Accept as Solution".

   Hallo TDK,

Thanks for the very quick response.
There is one thing in your concept I don't understand. I want to import the values  into the project via a file that I create externally in the program that measured the correction values. Is the 'floats.c' file the one that does the import?

Greetings,

                  Fred Schimmel

These are all text files. Doesn't matter which program creates them, as long as they all live in the project somewhere and get compiled. You'll likely need to format the data into an array in some tool.

If you feel a post has answered your question, please click "Accept as Solution".
mfgkw
Associate III

A file included with #include is not required to be a complete C expression or a complete declaration.

Correct C source is only needed after all preprocessor expansions.

So you can always generate a file myvalues.txt with your values in a form like:

12.0,
13.0,
14.0,
15.0

or

12.0, 13.0, 14.0, 15.0

  or

12.0, 13.0, 14.0, 15.0, 

just as you like or as your tool may generate it (even a comma separated values file like from excel...).

Then you can declare the C source in a way like:

...

const float myValues[] =
  {
#include "myvalues.txt"
  };

int main( int nargs, char **args )
{
  printf( "%g\n", myValues[0] );
  return 0;
}

 

 

FredS
Senior

    Hallo mfgkw,

Thanks for your help.

Although I have solved my "challenge", your response emphasizes the freedom one has until the compiler gets involved. In my case I coded the measuring program that determines the corrections to create the complete instructions with data and writes this to the file I incorporate in the project.

Have a nice day,

                     Fred schimmel


@mfgkw wrote:

even a comma separated values file like from excel.


I would even include the C rubric in the Excel file; eg,

AndrewNeil_0-1754384662993.png

Then you can just save as text, and give it a .c filename:

AndrewNeil_2-1754384904547.png

(note the quotes - otherwise Excel will save it as Book4.c.txt)

 

and there's your source file:

AndrewNeil_4-1754385027885.png

 

PS:

Sorry, forgot to make them float constants - but that's just a matter of the formatting in Excel:

AndrewNeil_5-1754385262225.png

AndrewNeil_6-1754385291283.png

 

#GenerateCfromExcel #CSourceFromExcel

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.

    Dear Andrew,

Thanks for thinking with me. In fact I follow a similar strategy where Excel is replaced by another application that produces a text file which directly can be added to my project.

Fred