Contiki 2.5
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  * $Id: fader.c,v 1.1 2007/03/21 23:17:28 adamdunkels Exp $
34  */
35 
36 #include "contiki.h"
37 #include "dev/leds.h"
38 
39 PROCESS(fader_process, "LED fader");
40 AUTOSTART_PROCESSES(&fader_process);
41 
42 #define ON 1
43 #define OFF 0
44 
45 struct fader {
46  struct pt fade_pt, fade_in_pt, fade_out_pt;
47  struct etimer etimer;
48  int led;
49  int delay;
50 };
51 
52 static unsigned char onoroff;
53 
54 /*---------------------------------------------------------------------------*/
55 static
56 PT_THREAD(fade_in(struct fader *f))
57 {
58  PT_BEGIN(&f->fade_in_pt);
59 
60  for(f->delay = 3980; f->delay > 20; f->delay -= 20) {
61  leds_on(f->led);
62  clock_delay(4000 - f->delay);
63  leds_off(f->led);
64  clock_delay(f->delay);
65  PT_YIELD(&f->fade_in_pt);
66  }
67 
68  PT_END(&f->fade_in_pt);
69 }
70 /*---------------------------------------------------------------------------*/
71 static
72 PT_THREAD(fade_out(struct fader *f))
73 {
74  PT_BEGIN(&f->fade_out_pt);
75 
76  for(f->delay = 20; f->delay < 3980; f->delay += 20) {
77  leds_on(f->led);
78  clock_delay(4000 - f->delay);
79  leds_off(f->led);
80  clock_delay(f->delay);
81  PT_YIELD(&f->fade_out_pt);
82  }
83 
84  PT_END(&f->fade_out_pt);
85 }
86 /*---------------------------------------------------------------------------*/
87 static
88 PT_THREAD(fade(struct fader *f))
89 {
90  PT_BEGIN(&f->fade_pt);
91 
92  while(1) {
93 
94  PT_SPAWN(&f->fade_pt, &f->fade_in_pt, fade_in(f));
95  PT_SPAWN(&f->fade_pt, &f->fade_out_pt, fade_out(f));
96 
97  etimer_set(&f->etimer, CLOCK_SECOND * 4);
98  PT_WAIT_UNTIL(&f->fade_pt, etimer_expired(&f->etimer));
99  }
100 
101  PT_END(&f->fade_pt);
102 }
103 /*---------------------------------------------------------------------------*/
104 static void
105 init_fader(struct fader *f, int led)
106 {
107  PT_INIT(&f->fade_pt);
108  PT_INIT(&f->fade_in_pt);
109  PT_INIT(&f->fade_out_pt);
110  f->led = led;
111 }
112 /*---------------------------------------------------------------------------*/
113 PROCESS_THREAD(fader_process, ev, data)
114 {
115  static struct fader red, green, yellow;
116  static struct timer timer;
117  static struct etimer etimer;
118 
119  PROCESS_BEGIN();
120 
121  init_fader(&red, LEDS_RED);
122  init_fader(&green, LEDS_GREEN);
123  init_fader(&yellow, LEDS_YELLOW);
124 
126  while(!timer_expired(&timer)) {
127  PT_SCHEDULE(fade(&red));
128  }
130  while(!timer_expired(&timer)) {
131  PT_SCHEDULE(fade(&red));
132  PT_SCHEDULE(fade(&green));
133  }
134 
136  while(!timer_expired(&timer)) {
137  PT_SCHEDULE(fade(&green));
138  PT_SCHEDULE(fade(&yellow));
139  }
140 
142  fader_on();
143 
144  while(1) {
146 
147  if(ev == PROCESS_EVENT_TIMER) {
149  process_poll(&fader_process);
150  }
151 
152  if(onoroff == ON &&
153  PT_SCHEDULE(fade(&red)) &&
154  PT_SCHEDULE(fade(&yellow)) &&
155  PT_SCHEDULE(fade(&green))) {
156  process_poll(&fader_process);
157  }
158  }
159  PROCESS_END();
160 }
161 /*---------------------------------------------------------------------------*/
162 void
163 fader_on(void)
164 {
165  onoroff = ON;
166  process_poll(&fader_process);
167 }
168 /*---------------------------------------------------------------------------*/
169 void
170 fader_off(void)
171 {
172  onoroff = OFF;
173 }
174 /*---------------------------------------------------------------------------*/