Skip to content

Commit 09db66d

Browse files
committed
current_servo_angle implementation
1 parent ab9999a commit 09db66d

File tree

5 files changed

+25
-35
lines changed

5 files changed

+25
-35
lines changed

examples/servo/main.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
1+
#include "../../libtock/interface/syscalls/servo_syscalls.h"
2+
#include <libtock-sync/services/alarm.h>
3+
#include <libtock/tock.h>
14
#include <stdio.h>
25
#include <stdlib.h>
36

4-
#include <libtock-sync/services/alarm.h>
5-
#include <libtock-sync/interface/servo.c>
6-
77
int main(void) {
88
// Checks if the driver exists and, if not, returns -1.
9-
if (!libtock_servo_exists()) {
9+
if (!libtock_servo_exists()) {
1010
printf("There is no available servo\n");
1111
return -1;
1212
}
13+
14+
uint16_t angle;
15+
1316
// Changes the angle of the servomotor from 0 to 180 degrees (waiting 0.1 ms between every change).
14-
for (int i = 0; i<=180; i++) {
15-
libtocksync_servo_angle(i);
17+
for (int i = 0; i <= 180; i++) {
18+
libtock_servo_angle(i);
1619
libtocksync_alarm_delay_ms(100);
20+
// Verifies if the function successfully returned the current angle.
21+
if (libtock_current_servo_angle(&angle) == RETURNCODE_SUCCESS) {
22+
printf("the current angle is: %d", angle);
23+
}
1724
}
1825
}

libtock-sync/interface/servo.c

Lines changed: 0 additions & 10 deletions
This file was deleted.

libtock-sync/interface/servo.h

Lines changed: 0 additions & 19 deletions
This file was deleted.

libtock/interface/syscalls/servo_syscalls.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,13 @@ returncode_t libtock_servo_angle(uint32_t angle) {
99
syscall_return_t cval = command(DRIVER_NUM_SERVO, 1, angle, 0);
1010
return tock_command_return_novalue_to_returncode(cval);
1111
}
12+
13+
returncode_t libtock_current_servo_angle(uint16_t* angle) {
14+
uint32_t value = 0;
15+
syscall_return_t r = command(DRIVER_NUM_SERVO, 2, 0, 0);
16+
// Converts the value returned by the servo, stored in `r`,
17+
// and assigns it to `ret`.
18+
returncode_t ret = tock_command_return_u32_to_returncode(r, &value);
19+
*angle = (uint16_t)value;
20+
return ret;
21+
}

libtock/interface/syscalls/servo_syscalls.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ bool libtock_servo_exists(void);
1313

1414
// Change the angle.
1515
returncode_t libtock_servo_angle(uint32_t angle);
16+
// Requests the current angle from the servo.
17+
returncode_t libtock_current_servo_angle(uint16_t* angle);
1618

1719
#ifdef __cplusplus
1820
}

0 commit comments

Comments
 (0)