[riot-notifications] [RIOT-OS/RIOT] periph/gpio: add gpio_set_cb() function (#12082)

benpicco notifications at github.com
Tue Sep 17 14:22:47 CEST 2019


benpicco commented on this pull request.



> @@ -416,6 +416,25 @@ int gpio_init_int(gpio_t pin, gpio_mode_t mode, gpio_flank_t flank,
     return 0;
 }
 
+int gpio_set_cb(gpio_t pin, gpio_cb_t cb, void *arg)
+{
+    int8_t int_num = _int_num(pin);
+
+    if (int_num < 0) {
+        return -1;
+    }
+
+    if (cb) {
+        config[int_num].cb = cb;
+    }
+
+    if (arg) {
+        config[int_num].arg = arg;
+    }

Hm, there are two ways to fulfill both requirements (allow to set `arg = NULL` and allow to update the callback without updating the arg) and neither of them is very pretty:

a) 
```C
#define GPIO_UPDATE_CB_NOOP    ((void*) -1)
…
gpio_update_cb(pin, new_cb, GPIO_UPDATE_CB_NOOP);
```

b)
```C
int gpio_update_cb(gpio_t pin, gpio_cb_t cb, void *arg)
{
    if (cb) {
        config[pin].cb = cb;
    }
    
    if (arg || cb == NULL) {
        config[pin].arg = arg;
    }
}
```

But I think I would prefer a).

-- 
You are receiving this because you are subscribed to this thread.
Reply to this email directly or view it on GitHub:
https://github.com/RIOT-OS/RIOT/pull/12082#discussion_r325135223
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.riot-os.org/pipermail/notifications/attachments/20190917/da80d94c/attachment.htm>


More information about the notifications mailing list