Skip to content

Commit 4b6027e

Browse files
committed
libtock: sensors: Support moisture sensor
Signed-off-by: Alistair Francis <[email protected]>
1 parent c0202f9 commit 4b6027e

File tree

6 files changed

+131
-0
lines changed

6 files changed

+131
-0
lines changed

libtock-sync/sensors/moisture.c

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#include "moisture.h"
2+
3+
typedef struct {
4+
int moisture;
5+
returncode_t ret;
6+
bool fired;
7+
} moisture_result_t;
8+
9+
static moisture_result_t result = {.fired = false};
10+
11+
// callback for synchronous reads
12+
static void moisture_callback(returncode_t ret, int moisture) {
13+
result.moisture = moisture;
14+
result.ret = ret;
15+
result.fired = true;
16+
}
17+
18+
returncode_t libtocksync_moisture_read(int* moisture) {
19+
returncode_t err;
20+
21+
result.fired = false;
22+
23+
err = libtock_moisture_read(moisture_callback);
24+
if (err != RETURNCODE_SUCCESS) return err;
25+
26+
yield_for(&result.fired);
27+
if (result.ret != RETURNCODE_SUCCESS) return result.ret;
28+
29+
*moisture = result.moisture;
30+
31+
return RETURNCODE_SUCCESS;
32+
}

libtock-sync/sensors/moisture.h

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#pragma once
2+
3+
#include <libtock/sensors/moisture.h>
4+
#include <libtock/tock.h>
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
// Read the moisture sensor synchronously.
11+
//
12+
// ## Arguments
13+
//
14+
// - `moisture`: Set to the moisture in hundredths of a percent.
15+
//
16+
// ## Return Value
17+
//
18+
// A returncode indicating whether the sensor read was completed successfully.
19+
returncode_t libtocksync_moisture_read(int* moisture);
20+
21+
#ifdef __cplusplus
22+
}
23+
#endif

libtock/sensors/moisture.c

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
#include "moisture.h"
2+
3+
static void moisture_upcall(int status,
4+
int moisture,
5+
__attribute__ ((unused)) int unused2, void* opaque) {
6+
libtock_moisture_callback cb = (libtock_moisture_callback) opaque;
7+
cb(status, moisture);
8+
}
9+
10+
returncode_t libtock_moisture_read(libtock_moisture_callback cb) {
11+
returncode_t err;
12+
13+
err = libtock_moisture_set_upcall(moisture_upcall, cb);
14+
if (err != RETURNCODE_SUCCESS) return err;
15+
16+
err = libtock_moisture_command_read();
17+
return err;
18+
}

libtock/sensors/moisture.h

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#pragma once
2+
3+
#include "../tock.h"
4+
#include "syscalls/moisture_syscalls.h"
5+
6+
#ifdef __cplusplus
7+
extern "C" {
8+
#endif
9+
10+
// Function signature for moisture data callback.
11+
//
12+
// - `arg1` (`int`): Returncode indicating status from sampling the sensor.
13+
// - `arg2` (`int`): moisture in hundredths of percent.
14+
typedef void (*libtock_moisture_callback)(returncode_t, int);
15+
16+
// Start a moisture measurement. The reading will be provided via the callback.
17+
returncode_t libtock_moisture_read(libtock_moisture_callback cb);
18+
19+
#ifdef __cplusplus
20+
}
21+
#endif
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
#include "moisture_syscalls.h"
2+
3+
bool libtock_moisture_exists(void) {
4+
return driver_exists(DRIVER_NUM_MOISTURE);
5+
}
6+
7+
returncode_t libtock_moisture_set_upcall(subscribe_upcall callback, void* opaque) {
8+
subscribe_return_t sval = subscribe(DRIVER_NUM_MOISTURE, 0, callback, opaque);
9+
return tock_subscribe_return_to_returncode(sval);
10+
}
11+
12+
returncode_t libtock_moisture_command_read(void) {
13+
syscall_return_t rval = command(DRIVER_NUM_MOISTURE, 1, 0, 0);
14+
return tock_command_return_novalue_to_returncode(rval);
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#pragma once
2+
3+
#include "../../tock.h"
4+
5+
#ifdef __cplusplus
6+
extern "C" {
7+
#endif
8+
9+
#define DRIVER_NUM_MOISTURE 0x6000A
10+
11+
// Check if the moisture driver is installed.
12+
bool libtock_moisture_exists(void);
13+
14+
// Configure the upcall for when the reading is ready.
15+
returncode_t libtock_moisture_set_upcall(subscribe_upcall callback, void* opaque);
16+
17+
// Read the sensor.
18+
returncode_t libtock_moisture_command_read(void);
19+
20+
#ifdef __cplusplus
21+
}
22+
#endif

0 commit comments

Comments
 (0)