-
Notifications
You must be signed in to change notification settings - Fork 89
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Userland app for distance sensor #463
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include "distance.h" | ||
|
||
struct data { | ||
bool fired; | ||
int dist; | ||
returncode_t result; | ||
}; | ||
|
||
static struct data result = { .fired = false }; | ||
|
||
static void dist_cb(returncode_t ret, int distance) { | ||
result.dist = distance; | ||
result.fired = true; | ||
result.result = ret; | ||
} | ||
|
||
returncode_t libtocksync_distance_read(int* distance) { | ||
returncode_t err; | ||
result.fired = false; | ||
|
||
err = libtock_distance_read(dist_cb); | ||
if (err != RETURNCODE_SUCCESS) return err; | ||
|
||
// Wait for the callback. | ||
yield_for(&result.fired); | ||
if (result.result != RETURNCODE_SUCCESS) return result.result; | ||
|
||
*distance = result.dist; | ||
|
||
return RETURNCODE_SUCCESS; | ||
} | ||
|
||
// Function to get minimum distance | ||
int libtocksync_distance_get_minimum_distance(void) { | ||
return libtock_distance_get_minimum_distance(); | ||
} | ||
|
||
// Function to get maximum distance | ||
int libtocksync_distance_get_maximum_distance(void) { | ||
return libtock_distance_get_maximum_distance(); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
#pragma once | ||
|
||
#include <libtock/sensors/distance.h> | ||
#include <libtock/tock.h> | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Read the distance sensor synchronously. | ||
// | ||
// ## Arguments | ||
// | ||
// - `distance`: Set to the distance value in millimeters. | ||
// | ||
// ## Return Value | ||
// | ||
// A returncode indicating whether the distance read was completed | ||
// successfully. | ||
returncode_t libtocksync_distance_read(int* distance); | ||
|
||
// Get the minimum measurable distance. | ||
// | ||
// ## Return Value | ||
// | ||
// The minimum measurable distance in millimeters. | ||
int libtocksync_distance_get_minimum_distance(void); | ||
|
||
// Get the maximum measurable distance. | ||
// | ||
// ## Return Value | ||
// | ||
// The maximum measurable distance in millimeters. | ||
int libtocksync_distance_get_maximum_distance(void); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change | ||||||||
---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,20 @@ | ||||||||||
#include "distance.h" | ||||||||||
|
||||||||||
// Internal upcall for passing to the syscall driver. | ||||||||||
static void distance_upcall(int dist, | ||||||||||
__attribute__ ((unused)) int unused, | ||||||||||
__attribute__ ((unused)) int unused1, | ||||||||||
void* opaque) { | ||||||||||
libtock_distance_callback cb = (libtock_distance_callback) opaque; | ||||||||||
cb(RETURNCODE_SUCCESS, dist); | ||||||||||
} | ||||||||||
|
||||||||||
returncode_t libtock_distance_read(libtock_distance_callback cb) { | ||||||||||
returncode_t ret; | ||||||||||
|
||||||||||
ret = libtock_distance_set_upcall(distance_upcall, cb); | ||||||||||
Comment on lines
+13
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||||||
if (ret != RETURNCODE_SUCCESS) return ret; | ||||||||||
|
||||||||||
ret = libtock_distance_command_read(); | ||||||||||
return ret; | ||||||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
#pragma once | ||
|
||
#include "../tock.h" | ||
#include "syscalls/distance_syscalls.h" | ||
|
||
#ifdef __cplusplus | ||
extern "C" { | ||
#endif | ||
|
||
// Function signature for distance data callback. | ||
// | ||
// - `arg1` (`returncode_t`): Status from sampling the sensor. | ||
// - `arg2` (`int`): Distance reading in milimeters. | ||
typedef void (*libtock_distance_callback)(returncode_t, int); | ||
|
||
// Initiate a reading measurement and return results via the `cb`. | ||
returncode_t libtock_distance_read(libtock_distance_callback cb); | ||
|
||
#ifdef __cplusplus | ||
} | ||
#endif |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,23 @@ | ||||||
#include "distance_syscalls.h" | ||||||
|
||||||
bool libtock_distance_exists(void) { | ||||||
return driver_exists(DRIVER_NUM_DISTANCE); | ||||||
} | ||||||
|
||||||
returncode_t libtock_distance_set_upcall(subscribe_upcall callback, void* opaque) { | ||||||
subscribe_return_t sval = subscribe(DRIVER_NUM_DISTANCE, 0, callback, opaque); | ||||||
return tock_subscribe_return_to_returncode(sval); | ||||||
} | ||||||
|
||||||
returncode_t libtock_distance_command_read(void) { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
syscall_return_t cval = command(DRIVER_NUM_DISTANCE, 1, 0, 0); | ||||||
return tock_command_return_novalue_to_returncode(cval); | ||||||
} | ||||||
|
||||||
int libtock_distance_get_minimum_distance(void) { | ||||||
return command(DRIVER_NUM_DISTANCE, 2, 0, 0).data[0]; | ||||||
} | ||||||
|
||||||
int libtock_distance_get_maximum_distance(void) { | ||||||
return command(DRIVER_NUM_DISTANCE, 3, 0, 0).data[0]; | ||||||
} | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a newline. |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,34 @@ | ||||||
#pragma once | ||||||
|
||||||
#include "../../tock.h" | ||||||
|
||||||
#ifdef __cplusplus | ||||||
extern "C" { | ||||||
#endif | ||||||
|
||||||
#define DRIVER_NUM_DISTANCE 0x60009 | ||||||
|
||||||
// Check if distance sensor exists. | ||||||
bool libtock_distance_exists(void); | ||||||
|
||||||
// units: disntance in millimeters. | ||||||
|
||||||
// Set the callback function to be called when the distance measurement is | ||||||
// finished. | ||||||
// | ||||||
// callback - pointer to function to be called | ||||||
// opaque - pointer to data provided to the callback | ||||||
returncode_t libtock_distance_set_upcall(subscribe_upcall callback, void* opaque); | ||||||
|
||||||
// Initiate an ambient distance measurement. | ||||||
returncode_t libtock_distance_command_read(void); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
// Function to get minimum distance | ||||||
int libtock_distance_get_minimum_distance(void); | ||||||
|
||||||
// Function to get maximum distance | ||||||
int libtock_distance_get_maximum_distance(void); | ||||||
|
||||||
#ifdef __cplusplus | ||||||
} | ||||||
#endif |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
These function do not have an async version, I would place them only on
libtock/sensors
.