Contiki-NG
fader.c
1 /*
2  * Copyright (c) 2005, Swedish Institute of Computer Science.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  * 1. Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the Institute nor the names of its contributors
14  * may be used to endorse or promote products derived from this software
15  * without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
18  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
21  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
22  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
23  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
25  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
26  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27  * SUCH DAMAGE.
28  *
29  * This file is part of the Contiki operating system.
30  *
31  * Author: Adam Dunkels <adam@sics.se>
32  *
33  */
34 
35 #include "contiki.h"
36 #include "dev/leds.h"
37 
38 PROCESS(fader_process, "LED fader");
39 AUTOSTART_PROCESSES(&fader_process);
40 
41 #define ON 1
42 #define OFF 0
43 
44 struct fader {
45  struct pt fade_pt, fade_in_pt, fade_out_pt;
46  struct etimer etimer;
47  int led;
48  int delay;
49 };
50 
51 static unsigned char onoroff;
52 
53 /*---------------------------------------------------------------------------*/
54 static
55 PT_THREAD(fade_in(struct fader *f))
56 {
57  PT_BEGIN(&f->fade_in_pt);
58 
59  for(f->delay = 3980; f->delay > 20; f->delay -= 20) {
60  leds_on(f->led);
61  clock_delay(4000 - f->delay);
62  leds_off(f->led);
63  clock_delay(f->delay);
64  PT_YIELD(&f->fade_in_pt);
65  }
66 
67  PT_END(&f->fade_in_pt);
68 }
69 /*---------------------------------------------------------------------------*/
70 static
71 PT_THREAD(fade_out(struct fader *f))
72 {
73  PT_BEGIN(&f->fade_out_pt);
74 
75  for(f->delay = 20; f->delay < 3980; f->delay += 20) {
76  leds_on(f->led);
77  clock_delay(4000 - f->delay);
78  leds_off(f->led);
79  clock_delay(f->delay);
80  PT_YIELD(&f->fade_out_pt);
81  }
82 
83  PT_END(&f->fade_out_pt);
84 }
85 /*---------------------------------------------------------------------------*/
86 static
87 PT_THREAD(fade(struct fader *f))
88 {
89  PT_BEGIN(&f->fade_pt);
90 
91  while(1) {
92 
93  PT_SPAWN(&f->fade_pt, &f->fade_in_pt, fade_in(f));
94  PT_SPAWN(&f->fade_pt, &f->fade_out_pt, fade_out(f));
95 
96  etimer_set(&f->etimer, CLOCK_SECOND * 4);
97  PT_WAIT_UNTIL(&f->fade_pt, etimer_expired(&f->etimer));
98  }
99 
100  PT_END(&f->fade_pt);
101 }
102 /*---------------------------------------------------------------------------*/
103 static void
104 init_fader(struct fader *f, int led)
105 {
106  PT_INIT(&f->fade_pt);
107  PT_INIT(&f->fade_in_pt);
108  PT_INIT(&f->fade_out_pt);
109  f->led = led;
110 }
111 /*---------------------------------------------------------------------------*/
112 PROCESS_THREAD(fader_process, ev, data)
113 {
114  static struct fader red, green, yellow;
115  static struct timer timer;
116  static struct etimer etimer;
117 
118  PROCESS_BEGIN();
119 
120  init_fader(&red, LEDS_RED);
121  init_fader(&green, LEDS_GREEN);
122  init_fader(&yellow, LEDS_YELLOW);
123 
125  while(!timer_expired(&timer)) {
126  PT_SCHEDULE(fade(&red));
127  }
129  while(!timer_expired(&timer)) {
130  PT_SCHEDULE(fade(&red));
131  PT_SCHEDULE(fade(&green));
132  }
133 
135  while(!timer_expired(&timer)) {
136  PT_SCHEDULE(fade(&green));
137  PT_SCHEDULE(fade(&yellow));
138  }
139 
141  fader_on();
142 
143  while(1) {
145 
146  if(ev == PROCESS_EVENT_TIMER) {
148  process_poll(&fader_process);
149  }
150 
151  if(onoroff == ON &&
152  PT_SCHEDULE(fade(&red)) &&
153  PT_SCHEDULE(fade(&yellow)) &&
154  PT_SCHEDULE(fade(&green))) {
155  process_poll(&fader_process);
156  }
157  }
158  PROCESS_END();
159 }
160 /*---------------------------------------------------------------------------*/
161 void
162 fader_on(void)
163 {
164  onoroff = ON;
165  process_poll(&fader_process);
166 }
167 /*---------------------------------------------------------------------------*/
168 void
169 fader_off(void)
170 {
171  onoroff = OFF;
172 }
173 /*---------------------------------------------------------------------------*/
void timer_set(struct timer *t, clock_time_t interval)
Set a timer.
Definition: timer.c:64
#define PROCESS(name, strname)
Declare a process.
Definition: process.h:307
#define PROCESS_WAIT_EVENT()
Wait for an event to be posted to the process.
Definition: process.h:141
#define PROCESS_BEGIN()
Define the beginning of a process.
Definition: process.h:120
#define PROCESS_END()
Define the end of a process.
Definition: process.h:131
#define PT_BEGIN(pt)
Declare the start of a protothread inside the C function implementing the protothread.
Definition: pt.h:114
#define PT_WAIT_UNTIL(pt, condition)
Block and wait until condition is true.
Definition: pt.h:147
A timer.
Definition: timer.h:82
void leds_on(unsigned char leds)
Turn on multiple LEDs.
Definition: minileds.c:63
#define PT_SPAWN(pt, child, thread)
Spawn a child protothread and wait until it exits.
Definition: pt.h:205
void leds_off(unsigned char leds)
Turn off multiple LEDs.
Definition: minileds.c:69
#define PT_INIT(pt)
Initialize a protothread.
Definition: pt.h:79
#define CLOCK_SECOND
A second, measured in system clock time.
Definition: clock.h:82
#define PT_END(pt)
Declare the end of a protothread.
Definition: pt.h:126
#define PT_SCHEDULE(f)
Schedule a protothread.
Definition: pt.h:270
void process_poll(struct process *p)
Request a process to be polled.
Definition: process.c:371
#define PT_YIELD(pt)
Yield from the current protothread.
Definition: pt.h:289
int timer_expired(struct timer *t)
Check if a timer has expired.
Definition: timer.c:123
void clock_delay(unsigned int i)
Obsolete delay function but we implement it here since some code still uses it.
Definition: clock.c:164
int etimer_expired(struct etimer *et)
Check if an event timer has expired.
Definition: etimer.c:213
#define PT_THREAD(name_args)
Declaration of a protothread.
Definition: pt.h:99
A timer.
Definition: etimer.h:76
PROCESS_THREAD(cc2538_rf_process, ev, data)
Implementation of the cc2538 RF driver process.
Definition: cc2538-rf.c:1107
Header file for the LED HAL.
void etimer_set(struct etimer *et, clock_time_t interval)
Set an event timer.
Definition: etimer.c:177