by bsoles · 1y ago · 0 comments
How about using Booleans for binary things? Is the LED on or off, is the button pressed or not, is the microcontroller pin low or high? Using Enums, etc. to represent those values in the embedded world would be a monumental waste of memory, where a single bit would normally suffice.
And usually you use operations to isolate the bit from a status byte or word, which is how it's also stored and accessed in registers anyway.
So its still no boolean type despite expressing boolean things.
Enums also help keep the state machine clear. {Init, on, off, error} capture a larger part of the program behavior in a clear format than 2-3 binary flags, despite describing the same function. Every new boolean flag is a two state composite state machine hiding edgecases.
Id prefer if they just added std::bitvector.
I'm with you by the way, but you can often think of a way to use enums instead (not saying you should).
enum Bool
{
True,
False,
FileNotFound
};
https://thedailywtf.com/articles/What_Is_Truth_0x3f_edit: The 24th of October will be the 20th anniversary of that post.
If embedded projects start using C standards from the past quarter century, they can join in on type discourse.
Sure we could store the data by logging the start timestamp and a stop timestamp but our data is stored on a time series basis (i.e. in a Timeseries DB, the timestamp is already the primary key for each record) When you are viewing the trend (such a on control room screen) you get a nice square-wave type effect you can easily see when the state changes.
This also makes things like total run time easy to compute, just sum the flag value over 1 second increments to get number of seconds in a shift the conveyor was running for.
Sure in my example you could just store something like motor current in Amps (and we do) and use this to infer the conveyor state but hopefully I've illustrated why a on/off flag is cleaner.
In C++ you can use enums in bit-fields, not sure what the case is in C.