diff --git a/libtock-sync/sensors/sound_pressure.c b/libtock-sync/sensors/sound_pressure.c new file mode 100644 index 000000000..7709287f7 --- /dev/null +++ b/libtock-sync/sensors/sound_pressure.c @@ -0,0 +1,36 @@ +#include +#include + +#include "sound_pressure.h" + +struct sound_pressure_data { + bool fired; + int sound_pressure; + returncode_t ret; +}; + +static struct sound_pressure_data result = { .fired = false }; + +// Internal callback for faking synchronous reads +static void cb(returncode_t ret, uint8_t sound_pressure) { + result.sound_pressure = sound_pressure; + result.fired = true; + result.ret = ret; +} + + +returncode_t libtocksync_sound_pressure_read(uint8_t* sound_pressure) { + returncode_t err; + result.fired = false; + + err = libtock_sound_pressure_read(cb); + if (err != RETURNCODE_SUCCESS) return err; + + // Wait for the callback. + yield_for(&result.fired); + if (result.ret != RETURNCODE_SUCCESS) return result.ret; + + *sound_pressure = result.sound_pressure; + + return RETURNCODE_SUCCESS; +} diff --git a/libtock-sync/sensors/sound_pressure.h b/libtock-sync/sensors/sound_pressure.h new file mode 100644 index 000000000..72512d94a --- /dev/null +++ b/libtock-sync/sensors/sound_pressure.h @@ -0,0 +1,22 @@ +#pragma once + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +// Read the ambient sound pressure level synchronously. +// +// ## Arguments +// +// - `sound_pressure`: The pressure reading in dB. +// +// ## Return Value +// +// A returncode indicating whether the read was completed successfully. +returncode_t libtocksync_sound_pressure_read(uint8_t* sound_pressure); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/libtock/sensors/sound_pressure.c b/libtock/sensors/sound_pressure.c index 4b2a6fed0..23823be03 100644 --- a/libtock/sensors/sound_pressure.c +++ b/libtock/sensors/sound_pressure.c @@ -1,63 +1,24 @@ #include "sound_pressure.h" +#include "syscalls/sound_pressure_syscalls.h" #include "tock.h" -struct data { - bool fired; - unsigned char temp; -}; -static struct data result = { .fired = false }; - -// Internal callback for faking synchronous reads -static void cb(int temp, - __attribute__ ((unused)) int unused, - __attribute__ ((unused)) int unused1, - void* ud) { - struct data* data = (struct data*) ud; - data->temp = (unsigned char)temp; - data->fired = true; -} - -bool sound_pressure_exists(void) { - return driver_exists(DRIVER_NUM_SOUND_PRESSURE); -} - -int sound_pressure_set_callback(subscribe_upcall callback, void* callback_args) { - subscribe_return_t sval = subscribe(DRIVER_NUM_SOUND_PRESSURE, 0, callback, callback_args); - return tock_subscribe_return_to_returncode(sval); -} - -int sound_pressure_read(void) { - syscall_return_t cval = command(DRIVER_NUM_SOUND_PRESSURE, 1, 0, 0); - return tock_command_return_novalue_to_returncode(cval); -} - -// enable sound pressure sensor -int sound_pressure_enable(void) { - syscall_return_t cval = command(DRIVER_NUM_SOUND_PRESSURE, 2, 0, 0); - return tock_command_return_novalue_to_returncode(cval); +static void sound_pressure_upcall(int sound_pressure, + __attribute__ ((unused)) int unused, + __attribute__ ((unused)) int unused1, + void* opaque) { + libtock_sound_pressure_callback cb = (libtock_sound_pressure_callback) opaque; + cb(RETURNCODE_SUCCESS, (uint8_t) sound_pressure); } -// disable sound pressure sensor -int sound_pressure_disable(void) { - syscall_return_t cval = command(DRIVER_NUM_SOUND_PRESSURE, 3, 0, 0); - return tock_command_return_novalue_to_returncode(cval); -} - -int sound_pressure_read_sync(unsigned char* sound_pressure) { - int err; - result.fired = false; - - err = sound_pressure_set_callback(cb, (void*) &result); - if (err < 0) return err; - err = sound_pressure_read(); - if (err < 0) return err; - // Wait for the callback. - yield_for(&result.fired); +returncode_t libtock_sound_pressure_read(libtock_sound_pressure_callback cb) { + returncode_t err; - *sound_pressure = result.temp; + err = libtock_sound_pressure_set_upcall(sound_pressure_upcall, cb); + if (err != RETURNCODE_SUCCESS) return err; - return RETURNCODE_SUCCESS; + err = libtock_sound_pressure_command_read(); + return err; } diff --git a/libtock/sensors/sound_pressure.h b/libtock/sensors/sound_pressure.h index a29a7771f..8c159536c 100644 --- a/libtock/sensors/sound_pressure.h +++ b/libtock/sensors/sound_pressure.h @@ -6,34 +6,18 @@ extern "C" { #endif -#define DRIVER_NUM_SOUND_PRESSURE 0x60006 - -// check if sound pressure sensor exists -bool sound_pressure_exists(void); - -// units: sound_pressure in DB. - -// function to be called when the temperature measurement is finished +// Function signature for sound pressure data callback. // -// callback - pointer to function to be called -// callback_args - pointer to data provided to the callback -int sound_pressure_set_callback (subscribe_upcall callback, void* callback_args); - - -// initiate an ambient sound_pressure measurement used both for synchronous and asynchronous readings -int sound_pressure_read(void); - -// enable sound pressure sensor -int sound_pressure_enable(void); +// - `arg1` (`returncode_t`): Status from sampling the sensor. +// - `arg2` (`uint8_t`): Sound pressure reading in dB. +typedef void (*libtock_sound_pressure_callback)(returncode_t, uint8_t); -// disable sound pressure sensor -int sound_pressure_disable(void); -// initiate a synchronous ambient sound_pressure measurement +// Initiate an ambient sound pressure measurement. // -// sound_pressure - pointer/address where the result of the sound_pressure reading should be stored -int sound_pressure_read_sync (unsigned char* sound_pressure); +// The sound pressure reading will be returned via the callback. +returncode_t libtock_sound_pressure_read(libtock_sound_pressure_callback cb); #ifdef __cplusplus } diff --git a/libtock/sensors/syscalls/sound_pressure_syscalls.c b/libtock/sensors/syscalls/sound_pressure_syscalls.c new file mode 100644 index 000000000..a1c77e90d --- /dev/null +++ b/libtock/sensors/syscalls/sound_pressure_syscalls.c @@ -0,0 +1,27 @@ +#include "sound_pressure_syscalls.h" + + + +bool libtock_sound_pressure_exists(void) { + return driver_exists(DRIVER_NUM_SOUND_PRESSURE); +} + +returncode_t libtock_sound_pressure_set_upcall(subscribe_upcall callback, void* opaque) { + subscribe_return_t sval = subscribe(DRIVER_NUM_SOUND_PRESSURE, 0, callback, opaque); + return tock_subscribe_return_to_returncode(sval); +} + +returncode_t libtock_sound_pressure_command_enable(void) { + syscall_return_t cval = command(DRIVER_NUM_SOUND_PRESSURE, 2, 0, 0); + return tock_command_return_novalue_to_returncode(cval); +} + +returncode_t libtock_sound_pressure_command_disable(void) { + syscall_return_t cval = command(DRIVER_NUM_SOUND_PRESSURE, 3, 0, 0); + return tock_command_return_novalue_to_returncode(cval); +} + +returncode_t libtock_sound_pressure_command_read(void) { + syscall_return_t cval = command(DRIVER_NUM_SOUND_PRESSURE, 1, 0, 0); + return tock_command_return_novalue_to_returncode(cval); +} diff --git a/libtock/sensors/syscalls/sound_pressure_syscalls.h b/libtock/sensors/syscalls/sound_pressure_syscalls.h new file mode 100644 index 000000000..297e1f8c7 --- /dev/null +++ b/libtock/sensors/syscalls/sound_pressure_syscalls.h @@ -0,0 +1,28 @@ +#pragma once + +#include "tock.h" + +#ifdef __cplusplus +extern "C" { +#endif + +#define DRIVER_NUM_SOUND_PRESSURE 0x60006 + +// Check if the sound pressure sensor exists. +bool libtock_sound_pressure_exists(void); + +// Set the upcall function for the sound pressure sensor. +returncode_t libtock_sound_pressure_set_upcall(subscribe_upcall callback, void* opaque); + +// Enable the sound pressure sensor. +returncode_t libtock_sound_pressure_command_enable(void); + +// Disable the sound pressure sensor. +returncode_t libtock_sound_pressure_command_disable(void); + +// Read the sound pressure sensor. +returncode_t libtock_sound_pressure_command_read(void); + +#ifdef __cplusplus +} +#endif