Binary objects

From Dooba Wiki
Jump to navigation Jump to search


There will come a time when we need to embed something which is not "code" into our application.

It could be images, fonts, sounds, pre-computed data or anything else. How can we integrate such data into our final firmware image?

The Dooba SDK allows us to do just that very easily.

.bin files

Whatever file we put in our application's directory (or that of its dependencies) and set the extension to '.bin', dbuild will embed into our final application.

This means we can put all of our data files directly alongside our source files (.c / .h).

Simply by giving them a '.bin' extension, we can have them included into our project.

Accessing data

Once the data files are embedded into our application, how do we access these from our C code?

When dbuild embeds a binary object, it generates a matching '.h' file (based on the data file's name) which defines the data.

For example, let's imagine we want to include a small data blob of 2Kb. Let's place it inside our source directory and call it 'some_blob.bin'. dbuild will generate a 'some_blob.h' header file.

This header file will define the following:

  • #define SOME_BLOB_DATA_SIZE 2048
  • extern const uint8_t some_blob_data[SOME_BLOB_DATA_SIZE] PROGMEM;

From our application we can now #include that file and make use of these elements.

However, the data itself is not stored in RAM, but in flash memory (as indicated by the 'PROGMEM' modifier) along with the application's code.

This means we cannot simply access this data like the rest of our RAM-based variables.

To read this flash-based data, we must use the uint8_t pgm_read_byte(const void *addr); and friends as presented here: Data in Program Space