-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathninedof.c
117 lines (86 loc) · 2.58 KB
/
ninedof.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include <stdio.h>
#include "math.h"
#include "ninedof.h"
struct ninedof_data {
int x;
int y;
int z;
bool fired;
};
static struct ninedof_data res = { .fired = false };
// internal callback for faking synchronous reads
static void ninedof_upcall(int x, int y, int z, void* ud) {
struct ninedof_data* result = (struct ninedof_data*) ud;
result->x = x;
result->y = y;
result->z = z;
result->fired = true;
}
double ninedof_read_accel_mag(void) {
struct ninedof_data result = { .fired = false };
int err;
err = ninedof_subscribe(ninedof_upcall, (void*)(&result));
if (err < 0) return err;
err = ninedof_start_accel_reading();
if (err < 0) return err;
yield_for(&result.fired);
return sqrt(result.x * result.x + result.y * result.y + result.z * result.z);
}
int ninedof_subscribe(subscribe_upcall callback, void* userdata) {
subscribe_return_t sval = subscribe(DRIVER_NUM_NINEDOF, 0, callback, userdata);
return tock_subscribe_return_to_returncode(sval);
}
int ninedof_start_accel_reading(void) {
syscall_return_t ret = command(DRIVER_NUM_NINEDOF, 1, 0, 0);
return tock_command_return_novalue_to_returncode(ret);
}
int ninedof_start_magnetometer_reading(void) {
syscall_return_t ret = command(DRIVER_NUM_NINEDOF, 100, 0, 0);
return tock_command_return_novalue_to_returncode(ret);
}
int ninedof_start_gyro_reading(void) {
syscall_return_t ret = command(DRIVER_NUM_NINEDOF, 200, 0, 0);
return tock_command_return_novalue_to_returncode(ret);
}
int ninedof_read_acceleration_sync(int* x, int* y, int* z) {
int err;
res.fired = false;
err = ninedof_subscribe(ninedof_upcall, (void*) &res);
if (err < 0) return err;
err = ninedof_start_accel_reading();
if (err < 0) return err;
// Wait for the callback.
yield_for(&res.fired);
*x = res.x;
*y = res.y;
*z = res.z;
return RETURNCODE_SUCCESS;
}
int ninedof_read_magnetometer_sync(int* x, int* y, int* z) {
int err;
res.fired = false;
err = ninedof_subscribe(ninedof_upcall, (void*) &res);
if (err < 0) return err;
err = ninedof_start_magnetometer_reading();
if (err < 0) return err;
// Wait for the callback.
yield_for(&res.fired);
*x = res.x;
*y = res.y;
*z = res.z;
return RETURNCODE_SUCCESS;
}
int ninedof_read_gyroscope_sync(int* x, int* y, int* z) {
int err;
res.fired = false;
err = ninedof_subscribe(ninedof_upcall, (void*) &res);
if (err < 0) return err;
err = ninedof_start_gyro_reading();
if (err < 0) return err;
// Wait for the callback.
yield_for(&res.fired);
*x = res.x;
*y = res.y;
*z = res.z;
return RETURNCODE_SUCCESS;
}