Skip to content

Commit 2615a58

Browse files
committed
pbio/light_animation: Convert to pbio/os.
1 parent 816ad14 commit 2615a58

File tree

4 files changed

+67
-46
lines changed

4 files changed

+67
-46
lines changed

lib/pbio/include/pbio/light_animation.h

Lines changed: 30 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#include <stdbool.h>
88
#include <stdint.h>
99

10-
#include <contiki.h>
10+
#include <pbio/os.h>
11+
#include <pbio/config.h>
1112

1213
typedef struct _pbio_light_animation_t pbio_light_animation_t;
1314

@@ -24,17 +25,44 @@ typedef uint32_t (*pbio_light_animation_next_t)(pbio_light_animation_t *animatio
2425

2526
struct _pbio_light_animation_t {
2627
/** Animation update timer. */
27-
struct etimer timer;
28+
pbio_os_timer_t timer;
2829
/** Animation iterator callback. */
2930
pbio_light_animation_next_t next;
3031
/** Linked list */
3132
pbio_light_animation_t *next_animation;
3233
};
3334

35+
#if PBIO_CONFIG_LIGHT
36+
37+
void pbio_light_animation_init_module(void);
3438
void pbio_light_animation_init(pbio_light_animation_t *animation, pbio_light_animation_next_t next);
3539
void pbio_light_animation_start(pbio_light_animation_t *animation);
3640
void pbio_light_animation_stop(pbio_light_animation_t *animation);
3741
void pbio_light_animation_stop_all(void);
3842
bool pbio_light_animation_is_started(pbio_light_animation_t *animation);
3943

44+
#else // PBIO_CONFIG_LIGHT
45+
46+
static inline void pbio_light_animation_init_module(void) {
47+
}
48+
49+
static inline void pbio_light_animation_init(pbio_light_animation_t *animation, pbio_light_animation_next_t next) {
50+
}
51+
52+
static inline void pbio_light_animation_start(pbio_light_animation_t *animation) {
53+
}
54+
55+
static inline void pbio_light_animation_stop(pbio_light_animation_t *animation) {
56+
}
57+
58+
static inline void pbio_light_animation_stop_all(void) {
59+
}
60+
61+
static inline bool pbio_light_animation_is_started(pbio_light_animation_t *animation) {
62+
return false;
63+
}
64+
65+
#endif // PBIO_CONFIG_LIGHT
66+
67+
4068
#endif // _PBIO_LIGHT_ANIMATION_H_

lib/pbio/src/light/animation.c

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,19 +8,16 @@
88
#include <assert.h>
99
#include <stdbool.h>
1010

11-
#include <contiki.h>
12-
13-
#include <pbio/util.h>
14-
1511
#include <pbio/light_animation.h>
12+
#include <pbio/os.h>
13+
#include <pbio/util.h>
1614

1715
/**
1816
* This is used as a value for the next_animation field to indicate when an
1917
* animation is stopped.
2018
*/
2119
#define PBIO_LIGHT_ANIMATION_STOPPED ((pbio_light_animation_t *)1)
2220

23-
PROCESS(pbio_light_animation_process, "light animation");
2421
static pbio_light_animation_t *pbio_light_animation_list_head;
2522

2623
/**
@@ -46,14 +43,12 @@ void pbio_light_animation_init(pbio_light_animation_t *animation, pbio_light_ani
4643
void pbio_light_animation_start(pbio_light_animation_t *animation) {
4744
assert(animation->next_animation == PBIO_LIGHT_ANIMATION_STOPPED);
4845

46+
// Insert at head of active list.
4947
animation->next_animation = pbio_light_animation_list_head;
5048
pbio_light_animation_list_head = animation;
5149

52-
process_start(&pbio_light_animation_process);
5350
// Fake a timer event to load the first cell.
54-
PROCESS_CONTEXT_BEGIN(&pbio_light_animation_process);
55-
etimer_set(&animation->timer, 0);
56-
PROCESS_CONTEXT_END(&pbio_light_animation_process);
51+
pbio_os_timer_set(&animation->timer, 0);
5752

5853
assert(animation->next_animation != PBIO_LIGHT_ANIMATION_STOPPED);
5954
}
@@ -69,13 +64,8 @@ void pbio_light_animation_stop(pbio_light_animation_t *animation) {
6964
assert(pbio_light_animation_list_head != NULL);
7065
assert(animation->next_animation != PBIO_LIGHT_ANIMATION_STOPPED);
7166

72-
etimer_stop(&animation->timer);
73-
7467
if (pbio_light_animation_list_head == animation) {
7568
pbio_light_animation_list_head = animation->next_animation;
76-
if (pbio_light_animation_list_head == NULL) {
77-
process_exit(&pbio_light_animation_process);
78-
}
7969
} else {
8070
for (pbio_light_animation_t *a = pbio_light_animation_list_head; a != NULL; a = a->next_animation) {
8171
if (a->next_animation == animation) {
@@ -110,20 +100,22 @@ bool pbio_light_animation_is_started(pbio_light_animation_t *animation) {
110100
return animation->next_animation != PBIO_LIGHT_ANIMATION_STOPPED;
111101
}
112102

113-
PROCESS_THREAD(pbio_light_animation_process, ev, data) {
114-
PROCESS_BEGIN();
103+
static pbio_error_t pbio_light_animation_process_thread(pbio_os_state_t *state, void *context) {
115104

116-
for (;;) {
117-
PROCESS_WAIT_EVENT_UNTIL(ev == PROCESS_EVENT_TIMER && etimer_expired(data));
118-
struct etimer *timer = data;
119-
pbio_light_animation_t *animation = PBIO_CONTAINER_OF(timer, pbio_light_animation_t, timer);
120-
if (pbio_light_animation_is_started(animation)) {
121-
clock_time_t interval = animation->next(animation);
122-
etimer_reset_with_new_interval(&animation->timer, interval);
105+
for (pbio_light_animation_t *a = pbio_light_animation_list_head; a != NULL; a = a->next_animation) {
106+
if (pbio_os_timer_is_expired(&a->timer)) {
107+
pbio_os_timer_set(&a->timer, a->next(a));
123108
}
124109
}
125110

126-
PROCESS_END();
111+
// This process does not have a state. It is effectively just a poll handler.
112+
return PBIO_ERROR_AGAIN;
113+
}
114+
115+
void pbio_light_animation_init_module(void) {
116+
117+
static pbio_os_process_t pbio_light_animation_process;
118+
pbio_os_process_start(&pbio_light_animation_process, pbio_light_animation_process_thread, NULL);
127119
}
128120

129121
#endif // PBIO_CONFIG_LIGHT

lib/pbio/src/main.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ void pbio_init(bool start_processes) {
3232

3333
pbio_battery_init();
3434
pbio_imu_init();
35+
pbio_light_animation_init_module();
3536

3637
if (!start_processes) {
3738
return;

lib/pbio/test/src/test_animation.c

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,45 +15,49 @@
1515

1616
#define TEST_ANIMATION_TIME 10
1717

18-
PROCESS_NAME(pbio_light_animation_process);
19-
2018
static uint8_t test_animation_set_hsv_call_count;
2119

2220
static uint32_t test_animation_next(pbio_light_animation_t *animation) {
2321
test_animation_set_hsv_call_count++;
2422
return TEST_ANIMATION_TIME;
2523
}
2624

27-
static PT_THREAD(test_light_animation(struct pt *pt)) {
28-
PT_BEGIN(pt);
25+
#define YIELD(state) \
26+
do { \
27+
do_yield_now = 1; \
28+
PBIO_OS_ASYNC_SET_CHECKPOINT(state); \
29+
if (do_yield_now) { \
30+
return PBIO_ERROR_AGAIN; \
31+
} \
32+
} while (0)
33+
34+
static pbio_error_t test_light_animation(pbio_os_state_t *state, void *context) {
35+
PBIO_OS_ASYNC_BEGIN(state);
2936

3037
static pbio_light_animation_t test_animation;
3138
pbio_light_animation_init(&test_animation, test_animation_next);
3239

33-
// process should not be running yet
34-
tt_want(!process_is_running(&pbio_light_animation_process));
40+
// animation should not be started yet
3541
tt_want(!pbio_light_animation_is_started(&test_animation));
3642

37-
// starting animation should start process and set a timer at 0ms to call
43+
// starting animation should set a timer at 0ms to call
3844
// next() after handling pending events
3945
pbio_light_animation_start(&test_animation);
4046
tt_want(pbio_light_animation_is_started(&test_animation));
41-
tt_want(process_is_running(&pbio_light_animation_process));
42-
pbio_handle_pending_events();
47+
YIELD(state);
4348
tt_want_uint_op(test_animation_set_hsv_call_count, ==, 1);
4449

4550
// next() should not be called again until after a delay
46-
pbio_test_clock_tick(TEST_ANIMATION_TIME - 1);
47-
PT_YIELD(pt);
51+
pbio_test_clock_tick(TEST_ANIMATION_TIME - 2);
52+
YIELD(state);
4853
tt_want_uint_op(test_animation_set_hsv_call_count, ==, 1);
4954
pbio_test_clock_tick(1);
50-
PT_YIELD(pt);
55+
YIELD(state);
5156
tt_want_uint_op(test_animation_set_hsv_call_count, ==, 2);
5257

53-
// stopping the animation stops the process
58+
// stopping the animation stops the animation
5459
pbio_light_animation_stop(&test_animation);
5560
tt_want(!pbio_light_animation_is_started(&test_animation));
56-
tt_want(!process_is_running(&pbio_light_animation_process));
5761

5862
// exercise multiple animations for code coverage
5963
static pbio_light_animation_t test_animation2;
@@ -65,27 +69,23 @@ static PT_THREAD(test_light_animation(struct pt *pt)) {
6569
pbio_light_animation_stop(&test_animation);
6670
tt_want(!pbio_light_animation_is_started(&test_animation));
6771
tt_want(pbio_light_animation_is_started(&test_animation2));
68-
tt_want(process_is_running(&pbio_light_animation_process));
6972
pbio_light_animation_stop(&test_animation2);
7073
tt_want(!pbio_light_animation_is_started(&test_animation));
7174
tt_want(!pbio_light_animation_is_started(&test_animation2));
72-
tt_want(!process_is_running(&pbio_light_animation_process));
7375

7476
// stopping all animations stops the process
7577
pbio_light_animation_start(&test_animation);
7678
pbio_light_animation_start(&test_animation2);
7779
tt_want(pbio_light_animation_is_started(&test_animation));
7880
tt_want(pbio_light_animation_is_started(&test_animation2));
79-
tt_want(process_is_running(&pbio_light_animation_process));
8081
pbio_light_animation_stop_all();
8182
tt_want(!pbio_light_animation_is_started(&test_animation));
8283
tt_want(!pbio_light_animation_is_started(&test_animation2));
83-
tt_want(!process_is_running(&pbio_light_animation_process));
8484

85-
PT_END(pt);
85+
PBIO_OS_ASYNC_END(PBIO_SUCCESS);
8686
}
8787

8888
struct testcase_t pbio_light_animation_tests[] = {
89-
PBIO_PT_THREAD_TEST(test_light_animation),
89+
PBIO_PT_THREAD_TEST_WITH_PBIO_OS(test_light_animation),
9090
END_OF_TESTCASES
9191
};

0 commit comments

Comments
 (0)