fader.c
00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
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