I am writing my own assert() macro on Arduino as shown below.
When I run it from the main() it always works correctly (prints the message and gets into an infinite loop), however when I call it within a class function it gets stuck without printing anything. Why is that?
assert.cpp/.h
#ifndef _ASSERT_H_
#define _ASSERT_H_
void assertion_failure(const char* expr, const char* file, int linenum);
#define assert(expr) \
if (expr) ; \
else assertion_failure(#expr, __FILE__, __LINE__)
#endif /* _ASSERT_H_ */
#include "assert.h"
#include "Arduino.h"
void assertion_failure(const char* expr, const char* file, int linenum)
{
Serial.print("Assertion failed in '");
Serial.print(file);
Serial.print("' on line ");
Serial.print(linenum);
Serial.print(": ");
Serial.println(expr);
Serial.flush();
while (1);
}
And the context in which the assert gets stuck below (it works well when I call it in main() however):
int ADS1256::assert_debug()
{
uint8_t id = read_register(STATUS) >> 4;
assert(false); // Gets stuck here without printing anything to screen
return id;
}
EDIT: looks like it has something to do with build configuration. I am using PlatformIO build system. It does not work when I use this file structure:
├── include
│ └── assert.h
├── lib
│ ├── ADS1256
│ ├── ads1256.cpp
│ ├── ads1256.h
│ └── ads1256_regmap.h
├── src
├── assert.cpp
└── main.cpp
However, if I move assert.cpp/.h into a dedicated directory within lib/ it works fine. What is the reason behind this? How can I investigate why this happened?
It works with this layout (again, using PlatformIO default build configuration):
├── include
├── lib
│ ├── ADS1256/
│ ├── Assert/
│ ├── assert.cpp
│ └── assert.h
├── src
└── main.cpp
main()
rather thansetup()
andloop()
? If so, do you callinit()
at the start ofmain()
?ADS1256
class: it works as expected. Your problem thus lies in some part of your code that you are not showing.assert_debug()
being called from?#include
directive?