Contiki 2.6

fader.c

00001 /*
00002  * Copyright (c) 2005, Swedish Institute of Computer Science.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions
00007  * are met:
00008  * 1. Redistributions of source code must retain the above copyright
00009  *    notice, this list of conditions and the following disclaimer.
00010  * 2. Redistributions in binary form must reproduce the above copyright
00011  *    notice, this list of conditions and the following disclaimer in the
00012  *    documentation and/or other materials provided with the distribution.
00013  * 3. Neither the name of the Institute nor the names of its contributors
00014  *    may be used to endorse or promote products derived from this software
00015  *    without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
00018  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED.  IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
00021  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00022  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
00023  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
00024  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
00025  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
00026  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
00027  * SUCH DAMAGE.
00028  *
00029  * This file is part of the Contiki operating system.
00030  *
00031  * Author: Adam Dunkels <adam@sics.se>
00032  *
00033  * $Id: fader.c,v 1.1 2006/06/18 07:48:48 adamdunkels Exp $
00034  */
00035 
00036 #include "contiki-esb.h"
00037 
00038 PROCESS(fader_process, "LED fader");
00039 AUTOSTART_PROCESSES(&fader_process);
00040 
00041 #define ON  1
00042 #define OFF 0
00043 
00044 static unsigned char onoroff;
00045 static struct etimer etimer;
00046 static struct pt fade_pt, fade_in_pt, fade_out_pt;
00047 /*---------------------------------------------------------------------------*/
00048 static
00049 PT_THREAD(fade_in(struct pt *pt))
00050 {
00051   static int delay;
00052   
00053   PT_BEGIN(pt);
00054   
00055   for(delay = 3980; delay > 20; delay -= 20) {
00056     leds_on(LEDS_ALL);
00057     clock_delay(4000 - delay);
00058     leds_off(LEDS_ALL);
00059     clock_delay(delay);
00060     PT_YIELD(pt);
00061   }
00062   
00063   PT_END(pt);
00064 }
00065 /*---------------------------------------------------------------------------*/
00066 static
00067 PT_THREAD(fade_out(struct pt *pt))
00068 {
00069   static int delay;
00070   
00071   PT_BEGIN(pt);
00072 
00073   for(delay = 20; delay < 3980; delay += 20) {
00074     leds_on(LEDS_ALL);
00075     clock_delay(4000 - delay);
00076     leds_off(LEDS_ALL);
00077     clock_delay(delay);
00078     PT_YIELD(pt);
00079   }
00080     
00081   PT_END(pt);
00082 }
00083 /*---------------------------------------------------------------------------*/
00084 static
00085 PT_THREAD(fade(struct pt *pt))
00086 {
00087   PT_BEGIN(pt);
00088 
00089   PT_SPAWN(pt, &fade_in_pt, fade_in(&fade_in_pt));
00090   PT_SPAWN(pt, &fade_out_pt, fade_out(&fade_out_pt));
00091 
00092   etimer_set(&etimer, CLOCK_SECOND * 10);
00093   PT_WAIT_UNTIL(pt, etimer_expired(&etimer));
00094   
00095   PT_END(pt);
00096 }
00097 /*---------------------------------------------------------------------------*/
00098 PROCESS_THREAD(fader_process, ev, data)
00099 {
00100   PROCESS_BEGIN();
00101   
00102   PT_INIT(&fade_pt);
00103   PT_INIT(&fade_in_pt);
00104   PT_INIT(&fade_out_pt);
00105   onoroff = ON;
00106   etimer_set(&etimer, CLOCK_SECOND * 32);
00107 
00108   while(1) {
00109     PROCESS_WAIT_EVENT();
00110   
00111     if(ev == PROCESS_EVENT_TIMER) {
00112       etimer_reset(&etimer);
00113       PT_INIT(&fade_pt);
00114       process_poll(&fader_process);
00115     }
00116     
00117     if(onoroff == ON &&
00118        PT_SCHEDULE(fade(&fade_pt))) {
00119       process_poll(&fader_process);
00120     }
00121   }
00122   PROCESS_END();
00123 }
00124 /*---------------------------------------------------------------------------*/
00125 void
00126 fader_on(void)
00127 {
00128   onoroff = ON;
00129   process_poll(&fader_process);
00130 }
00131 /*---------------------------------------------------------------------------*/
00132 void
00133 fader_off(void)
00134 {
00135   onoroff = OFF;
00136 }
00137 /*---------------------------------------------------------------------------*/