Skip to content

Commit

Permalink
sound_pressure
Browse files Browse the repository at this point in the history
  • Loading branch information
bradjc committed Mar 9, 2024
1 parent 955e36c commit c57a4c8
Show file tree
Hide file tree
Showing 6 changed files with 133 additions and 75 deletions.
36 changes: 36 additions & 0 deletions libtock-sync/sensors/sound_pressure.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#include <sensors/sound_pressure.h>
#include <tock.h>

#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;
}
22 changes: 22 additions & 0 deletions libtock-sync/sensors/sound_pressure.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

// 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
65 changes: 13 additions & 52 deletions libtock/sensors/sound_pressure.c
Original file line number Diff line number Diff line change
@@ -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;
}
30 changes: 7 additions & 23 deletions libtock/sensors/sound_pressure.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
27 changes: 27 additions & 0 deletions libtock/sensors/syscalls/sound_pressure_syscalls.c
Original file line number Diff line number Diff line change
@@ -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);
}
28 changes: 28 additions & 0 deletions libtock/sensors/syscalls/sound_pressure_syscalls.h
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c57a4c8

Please sign in to comment.