I am reading in a single byte over TWI. Each bit stands for something. I want to be able to read that byte into a data structure, then be able to reference each bit. I had thought of something like this
structs keys mykeys;
mykeys = SOME BYTE I HAVE.
Serial.print(mykyes.key1); // would print a zero or one.
But the above doesn't work. I think I an misunderstanding how struct works. Anyone have any thoughts on what would be the simplest way to get the value of each bit?
I would use something like the following to test a given bit specified by n where 0 <= n <= 7
value = (keys >> n) & 1;
With this expression value will be either 0 or 1 depending upon what the bit contained.
tempalte has got a lot of the right parts, just not all together.
The packed bitfield structure is correct. That will let you access the 8 bits of the byte individually. But if you want to assign a byte into it, then you either need to put it inside a union or do some typecasting.
For whatever reason, it is not the habit of Arduino programmers or the avr-gcc people to use bit structures. They tend to shift and mask. At a first glance, the object code is about the same so it is a matter of preference. (Personally, I like bit structs for fixed structures, but I don't code that way here.)