-
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.
- Loading branch information
Showing
6 changed files
with
133 additions
and
75 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
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; | ||
} |
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 | ||
|
||
// 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 |
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 |
---|---|---|
@@ -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; | ||
} |
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,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); | ||
} |
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,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 |