-
Notifications
You must be signed in to change notification settings - Fork 90
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #475 from alistair23/alistair/moisture
examples: sensors: Support moisture readings
- Loading branch information
Showing
7 changed files
with
138 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
#include "moisture.h" | ||
|
||
typedef struct { | ||
int moisture; | ||
returncode_t ret; | ||
bool fired; | ||
} moisture_result_t; | ||
|
||
static moisture_result_t result = {.fired = false}; | ||
|
||
// callback for synchronous reads | ||
static void moisture_callback(returncode_t ret, int moisture) { | ||
result.moisture = moisture; | ||
result.ret = ret; | ||
result.fired = true; | ||
} | ||
|
||
returncode_t libtocksync_moisture_read(int* moisture) { | ||
returncode_t err; | ||
|
||
result.fired = false; | ||
|
||
err = libtock_moisture_read(moisture_callback); | ||
if (err != RETURNCODE_SUCCESS) return err; | ||
|
||
yield_for(&result.fired); | ||
if (result.ret != RETURNCODE_SUCCESS) return result.ret; | ||
|
||
*moisture = result.moisture; | ||
|
||
return RETURNCODE_SUCCESS; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
#pragma once | ||
|
||
#include <libtock/sensors/moisture.h> | ||
#include <libtock/tock.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Read the moisture sensor synchronously. | ||
// | ||
// ## Arguments | ||
// | ||
// - `moisture`: Set to the moisture in hundredths of a percent. | ||
// | ||
// ## Return Value | ||
// | ||
// A returncode indicating whether the sensor read was completed successfully. | ||
returncode_t libtocksync_moisture_read(int* moisture); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
#include "moisture.h" | ||
|
||
static void moisture_upcall(int status, | ||
int moisture, | ||
__attribute__ ((unused)) int unused2, void* opaque) { | ||
libtock_moisture_callback cb = (libtock_moisture_callback) opaque; | ||
cb(status, moisture); | ||
} | ||
|
||
returncode_t libtock_moisture_read(libtock_moisture_callback cb) { | ||
returncode_t err; | ||
|
||
err = libtock_moisture_set_upcall(moisture_upcall, cb); | ||
if (err != RETURNCODE_SUCCESS) return err; | ||
|
||
err = libtock_moisture_command_read(); | ||
return err; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include "../tock.h" | ||
#include "syscalls/moisture_syscalls.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Function signature for moisture data callback. | ||
// | ||
// - `arg1` (`int`): Returncode indicating status from sampling the sensor. | ||
// - `arg2` (`int`): moisture in hundredths of percent. | ||
typedef void (*libtock_moisture_callback)(returncode_t, int); | ||
|
||
// Start a moisture measurement. The reading will be provided via the callback. | ||
returncode_t libtock_moisture_read(libtock_moisture_callback cb); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#include "moisture_syscalls.h" | ||
|
||
bool libtock_moisture_exists(void) { | ||
return driver_exists(DRIVER_NUM_MOISTURE); | ||
} | ||
|
||
returncode_t libtock_moisture_set_upcall(subscribe_upcall callback, void* opaque) { | ||
subscribe_return_t sval = subscribe(DRIVER_NUM_MOISTURE, 0, callback, opaque); | ||
return tock_subscribe_return_to_returncode(sval); | ||
} | ||
|
||
returncode_t libtock_moisture_command_read(void) { | ||
syscall_return_t rval = command(DRIVER_NUM_MOISTURE, 1, 0, 0); | ||
return tock_command_return_novalue_to_returncode(rval); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
#pragma once | ||
|
||
#include "../../tock.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
#define DRIVER_NUM_MOISTURE 0x6000A | ||
|
||
// Check if the moisture driver is installed. | ||
bool libtock_moisture_exists(void); | ||
|
||
// Configure the upcall for when the reading is ready. | ||
returncode_t libtock_moisture_set_upcall(subscribe_upcall callback, void* opaque); | ||
|
||
// Read the sensor. | ||
returncode_t libtock_moisture_command_read(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |