Hi happy holidays.
I have a question about whether I can create more elaborate error messages using the static_assert function.
Below is a snippet of a program I've made, which saves a struct in multiple locations on an external I2C FRAM. I'm running the program on an ESP32.
The example struct is 20 bytes in size. You alter the "numWriteLocations" variable to specify the number of separate 20 byte memory locations that you want the struct to be stored in.
#include <assert.h>
struct __attribute__((packed)) STRUCT {
uint32_t counter;
float variableOne;
uint32_t variableTwo;
uint32_t variableThree;
uint32_t CRC;
} testStruct;
const uint8_t sizeOfData = sizeof(testStruct);
const uint8_t numWriteLocations = 2;
static_assert(((sizeOfData * numWriteLocations) < 70), "Error: too many write locations");
if you set numWriteLocations = 2, that means the struct is saved in 2 separate locations (so 40 bytes of FRAM total). These are then saved in first 20 bytes of the FRAM (addresses 0-19), and then the second 20 bytes of FRAM (addresses 20-39).
I am using the static_assert function, to set a limit on the amount of memory that can be allocated for all of these copies of the struct.
If the limit is set at 70 bytes, then you get an error message if you set the number of write locations (numWriteLocations) to equal 4, as this will take up 80 bytes (20*4) of memory.
This basic error message above works as expected.
The question:
I am wondering if I can print out more information about the error, such as how many bytes over the limit I am, and the maximum amount of allowable write locations I can have.
So, using the previous example, if I set numWriteLocations = 4, and I am 10 bytes over the limit, the error message would say;
"Error: too many write locations, you are 10 bytes over the 70 byte limit. The maximum allowable write locations are 3"
This desired error message would then adapt to changes in the sketch. For example, if you modified the struct to be 30 bytes in size, kept the numWriteLocations = 4, and kept the 70 byte limit, then you would get the error;
"Error: too many write locations, you are 50 bytes over the 70 byte limit. The maximum allowable write locations are 2"
Is this all possible? Would be handy.
thanks