-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathrt-leds.c
73 lines (60 loc) · 1.59 KB
/
rt-leds.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
#include "contiki.h"
#include "sys/rtimer.h"
#include "dev/leds.h"
/*---------------------------------------------------------------------------*/
PROCESS(test_rtimer_process, "Test RT");
AUTOSTART_PROCESSES(&test_rtimer_process);
struct fade {
struct rtimer rt;
struct pt pt;
int led;
rtimer_clock_t ontime, offtime;
int addend;
};
/*---------------------------------------------------------------------------*/
static char
fade(struct rtimer *t, void *ptr)
{
struct fade *f = ptr;
PT_BEGIN(&f->pt);
while(1) {
leds_on(f->led);
rtimer_set(t, RTIMER_TIME(t) + f->ontime, 1,
(rtimer_callback_t)fade, ptr);
PT_YIELD(&f->pt);
leds_off(f->led);
rtimer_set(t, RTIMER_TIME(t) + f->offtime, 1,
(rtimer_callback_t)fade, ptr);
f->ontime += f->addend;
f->offtime -= f->addend;
if(f->offtime <= 4 || f->offtime >= 100) {
f->addend = -f->addend;
}
PT_YIELD(&f->pt);
}
PT_END(&f->pt);
}
/*---------------------------------------------------------------------------*/
static void
init_fade(struct fade *f, int led)
{
f->led = led;
f->addend = 4;
f->ontime = 4;
f->offtime = 100;
PT_INIT(&f->pt);
rtimer_set(&f->rt, RTIMER_NOW() + led, 1, (rtimer_callback_t)fade, f);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(test_rtimer_process, ev, data)
{
static struct fade red;
PROCESS_BEGIN();
rtimer_init();
init_fade(&red, LEDS_RED);
while(1) {
PROCESS_WAIT_EVENT();
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/