Skip to content

Commit

Permalink
Add GPIO.ReadOut RPC method (#2)
Browse files Browse the repository at this point in the history
* Add GPIO.ReadOut

The RPC method wrapping mgos_gpio_read_out().
  • Loading branch information
QRPp committed Nov 24, 2020
1 parent 7b8c83e commit 5312619
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 0 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,22 @@ Example usage:
}</code></pre>


## GPIO.ReadOut
Set given pin in OUTPUT mode, read GPIO pin, return its value. Arguments:
```javascript
{
"pin": 15 // Required. Pin number.
}
```

Example usage:

<pre class="command-line language-bash" data-user="chris" data-host="localhost" data-output="2-100"><code>mos call GPIO.ReadOut '{"pin": 1}'
{
"value": 0
}</code></pre>


## GPIO.Write
Set given pin in OUTPUT mode, set GPIO pin. Arguments:
```javascript
Expand Down
22 changes: 22 additions & 0 deletions src/mgos_gpio_service.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,26 @@ static void gpio_read_handler(struct mg_rpc_request_info *ri, void *cb_arg,
(void) fi;
}

static void gpio_read_out_handler(struct mg_rpc_request_info *ri, void *cb_arg,
struct mg_rpc_frame_info *fi,
struct mg_str args) {
int pin;
if (json_scanf(args.p, args.len, ri->args_fmt, &pin) != 1) {
mg_rpc_send_errorf(ri, 400, "pin is required");
goto exit;
}
if (!mgos_gpio_set_mode(pin, MGOS_GPIO_MODE_OUTPUT)) {
mg_rpc_send_errorf(ri, 400, "error setting pin mode");
goto exit;
}
mg_rpc_send_responsef(ri, "{value: %d}", mgos_gpio_read_out(pin));

exit:
ri = NULL;
(void) cb_arg;
(void) fi;
}

static void gpio_write_handler(struct mg_rpc_request_info *ri, void *cb_arg,
struct mg_rpc_frame_info *fi,
struct mg_str args) {
Expand Down Expand Up @@ -269,6 +289,8 @@ static void gpio_blink_handler(struct mg_rpc_request_info *ri, void *cb_arg,
bool mgos_rpc_service_gpio_init(void) {
struct mg_rpc *c = mgos_rpc_get_global();
mg_rpc_add_handler(c, "GPIO.Read", "{pin: %d}", gpio_read_handler, NULL);
mg_rpc_add_handler(c, "GPIO.ReadOut", "{pin: %d}", gpio_read_out_handler,
NULL);
mg_rpc_add_handler(c, "GPIO.Write", "{pin: %d, value: %d}",
gpio_write_handler, NULL);
mg_rpc_add_handler(c, "GPIO.Toggle", "{pin: %d}", gpio_toggle_handler, NULL);
Expand Down

0 comments on commit 5312619

Please sign in to comment.