Skip to content

Commit

Permalink
Merge pull request #475 from alistair23/alistair/moisture
Browse files Browse the repository at this point in the history
examples: sensors: Support moisture readings
  • Loading branch information
ppannuto authored Nov 18, 2024
2 parents c0202f9 + 0b1a550 commit 874efe2
Show file tree
Hide file tree
Showing 7 changed files with 138 additions and 0 deletions.
7 changes: 7 additions & 0 deletions examples/sensors/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include <libtock-sync/sensors/ambient_light.h>
#include <libtock-sync/sensors/humidity.h>
#include <libtock-sync/sensors/moisture.h>
#include <libtock-sync/sensors/ninedof.h>
#include <libtock-sync/sensors/proximity.h>
#include <libtock-sync/sensors/sound_pressure.h>
Expand All @@ -20,12 +21,14 @@ static bool ninedof_mag = false;
static bool ninedof_gyro = false;
static bool proximity = false;
static bool sound_pressure = false;
static bool moisture = false;
static void alarm_cb(__attribute__ ((unused)) uint32_t now,
__attribute__ ((unused)) uint32_t scheduled,
__attribute__ ((unused)) void* opaque) {
int lite = 0;
int temp = 0;
int humi = 0;
int mois = 0;
int ninedof_accel_x = 0, ninedof_accel_y = 0, ninedof_accel_z = 0;
int ninedof_magneto_x = 0, ninedof_magneto_y = 0, ninedof_magneto_z = 0;
int ninedof_gyro_x = 0, ninedof_gyro_y = 0, ninedof_gyro_z = 0;
Expand All @@ -41,6 +44,7 @@ static void alarm_cb(__attribute__ ((unused)) uint32_t now,
if (ninedof_gyro) libtocksync_ninedof_read_gyroscope(&ninedof_gyro_x, &ninedof_gyro_y, &ninedof_gyro_z);
if (proximity) libtocksync_proximity_read(&prox_reading);
if (sound_pressure) libtocksync_sound_pressure_read(&sound_pressure_reading);
if (moisture) libtocksync_moisture_read(&mois);

if (light) printf("Amb. Light: Light Intensity: %d\n", lite);
if (temperature) printf("Temperature: %d deg C\n", temp/100);
Expand All @@ -50,6 +54,7 @@ static void alarm_cb(__attribute__ ((unused)) uint32_t now,
if (ninedof_gyro) printf("Gyro: X: %d Y: %d Z: %d\n", ninedof_gyro_x, ninedof_gyro_y, ninedof_gyro_z);
if (proximity) printf("Proximity: %u\n", prox_reading);
if (sound_pressure) printf("Sound Pressure: %u\n", sound_pressure_reading);
if (moisture) printf("Moisture: %d%%\n", mois/100);

/* *INDENT-ON* */

Expand All @@ -68,6 +73,7 @@ int main(void) {
ninedof = libtock_ninedof_exists();
proximity = libtock_proximity_exists();
sound_pressure = libtock_sound_pressure_exists();
moisture = libtock_moisture_exists();
/* *INDENT-ON* */

if (ninedof) {
Expand All @@ -86,6 +92,7 @@ int main(void) {
if (ninedof_gyro) printf("[Sensors] Sampling Gyroscope.\n");
if (proximity) printf("[Sensors] Sampling Proximity sensor.\n");
if (sound_pressure) printf("[Sensors] Sampling Sound Pressure sensor.\n");
if (moisture) printf("[Sensors] Sampling Moisture sensor.\n");
/* *INDENT-ON* */

if (sound_pressure) {
Expand Down
32 changes: 32 additions & 0 deletions libtock-sync/sensors/moisture.c
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;
}
23 changes: 23 additions & 0 deletions libtock-sync/sensors/moisture.h
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
18 changes: 18 additions & 0 deletions libtock/sensors/moisture.c
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;
}
21 changes: 21 additions & 0 deletions libtock/sensors/moisture.h
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
15 changes: 15 additions & 0 deletions libtock/sensors/syscalls/moisture_syscalls.c
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);
}
22 changes: 22 additions & 0 deletions libtock/sensors/syscalls/moisture_syscalls.h
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

0 comments on commit 874efe2

Please sign in to comment.