Skip to content
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

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions examples/sensors/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include <stdio.h>

#include <libtock-sync/sensors/ambient_light.h>
#include <libtock-sync/sensors/distance.h>
#include <libtock-sync/sensors/humidity.h>
#include <libtock-sync/sensors/ninedof.h>
#include <libtock-sync/sensors/proximity.h>
Expand All @@ -20,12 +21,15 @@ static bool ninedof_mag = false;
static bool ninedof_gyro = false;
static bool proximity = false;
static bool sound_pressure = false;
static bool distance = 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 dist = 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 +45,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 (distance) libtocksync_distance_read(&dist);

if (light) printf("Amb. Light: Light Intensity: %d\n", lite);
if (temperature) printf("Temperature: %d deg C\n", temp/100);
Expand All @@ -50,6 +55,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 (distance) printf("Distance: %d mm\n", dist);

/* *INDENT-ON* */

Expand All @@ -68,8 +74,15 @@ int main(void) {
ninedof = libtock_ninedof_exists();
proximity = libtock_proximity_exists();
sound_pressure = libtock_sound_pressure_exists();
distance = libtock_distance_exists();
/* *INDENT-ON* */

if (distance) {
int min_distance = libtocksync_distance_get_minimum_distance();
int max_distance = libtocksync_distance_get_maximum_distance();
printf("[Sensors] Distance sensor: Min = %d mm, Max = %d mm\n", min_distance, max_distance);
}

if (ninedof) {
int buffer;
ninedof_accel = (libtocksync_ninedof_read_accelerometer(&buffer, &buffer, &buffer) == RETURNCODE_SUCCESS);
Expand All @@ -86,6 +99,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 (distance) printf("[Sensors] Sampling Distance sensor.\n");
/* *INDENT-ON* */

if (sound_pressure) {
Expand Down
41 changes: 41 additions & 0 deletions libtock-sync/sensors/distance.c
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();
}
38 changes: 38 additions & 0 deletions libtock-sync/sensors/distance.h
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);
Copy link
Contributor

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.


// 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
20 changes: 20 additions & 0 deletions libtock/sensors/distance.c
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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
returncode_t ret;
ret = libtock_distance_set_upcall(distance_upcall, cb);
returncode_t ret = libtock_distance_set_upcall(distance_upcall, cb);

if (ret != RETURNCODE_SUCCESS) return ret;

ret = libtock_distance_command_read();
return ret;
}
21 changes: 21 additions & 0 deletions libtock/sensors/distance.h
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
23 changes: 23 additions & 0 deletions libtock/sensors/syscalls/distance_syscalls.c
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) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
returncode_t libtock_distance_command_read(void) {
returncode_t libtock_distance_read(void) {

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];
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add a newline.

34 changes: 34 additions & 0 deletions libtock/sensors/syscalls/distance_syscalls.h
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);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
returncode_t libtock_distance_command_read(void);
returncode_t libtock_distance_read(void);


// 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
Loading