I would recommend looking up the definition of PINB
first. I've found this link to a power point presentation that seems to have a reference to the right header files.
Noteworthy files are:
where all the information you need to define your own pointers to PINB resides.
The following should work as you want it to:
typedef struct {
volatile uint8_t * port;
uint8_t pin;
unsigned long timestamp;
} BUTTONS;
BUTTONS button_1;
BUTTONS button_2;
BUTTONS button_3;
BUTTONS* button[BUTTONS_ID_COUNT] = {&button_1,&button_2,&button_3};
void Button_init(void){
button[BUTTONS_ID_1]->port = &PINB;
button[BUTTONS_ID_1]->pin = PINB4;
button[BUTTONS_ID_1]->timestamp = 0;
}
unsigned char Button_instantRead(int id){
//return PINB & (1 << PINB4);
return *(button[id]->port) & (1 << button[id]->pin);
}
Note that the port is a pointer.
Edit:
I had to check myself for volatile
struct member use and found this SO question and this other SO question quite interesting.
Edit 2:
If you are using gcc (which I think you are not since it is AVR but I could be wrong) you can skip the step of finding out what the exact type of PINB
is and declare your struct as follows:
typedef struct {
typeof(PINB)* port;
uint8_t pin;
unsigned long timestamp;
};
The typeof
operator is a gcc extension to the C language so you might not be able to use it but it is good to know about it.