nRF52840, nRF Connect
I was looking at the button example and it is apparently a little different then in the nRF5 SDK kit. In the SDK you had a callback and inside that you had pressed and released options. The button example does not go into details on how to implement this. Also i would like to use 1 callback for multiple buttons without hardcoding in values.
I am using the basic button example in nordic. Only difference is I adapted for 2 buttons. So if you see button in the example, i copy and pasted everything and made button1 and button 2. used SW0 and SW1
Right now I got it working by having a separate callback function per button. I know pins is a mask that has bit set of pin number associated with the button. So this following code would work if the fact i get an error when i call button1.pin saying it needs to be a constant. If i exchange button1.pin and button2.pin with 11 and 12, then it works fine. but i am looking for a more dynamic method.
int mask_to_pin(uint32_t mask, int *pin){
uint32_t val = mask;
*pin = 0;
while(val % 2 == 0){
val /= 2;
*pin = *pin + 1;
}
if(val == 1) return 0;
return 1;
}
void button_pressed(const struct device *dev, struct gpio_callback *cb,
uint32_t pins){
int err;
int pin;
err = mask_to_pin(pins, &pin);
if(err){
printk("pins is not a power of 2\n");
return;
}
switch(pin){
case button1.pin: // button 1. hardcoded as i do not know how ot get this form info
printk("Button 1 %d pressed at %" PRIu32 "\n", button1.pin, k_cycle_get_32());
status_flags.bit.request_pair = 1;
break;
case button2.pin: // button 2
printk("Button 2 %d pressed at %" PRIu32 "\n", button2.pin, k_cycle_get_32());
status_flags.bit.request_disconnect = 1;
break;
}
}