Contiki 2.6
|
00001 /* 00002 * Copyright (c) 2009, 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 * @(#)$Id: sensors.c,v 1.5 2010/01/14 20:04:38 nifi Exp $ 00032 */ 00033 /* exeperimental code, will be renamed to sensors.c when done */ 00034 00035 00036 #include <string.h> 00037 00038 #include "contiki.h" 00039 00040 #include "lib/sensors.h" 00041 00042 const extern struct sensors_sensor *sensors[]; 00043 extern unsigned char sensors_flags[]; 00044 00045 #define FLAG_CHANGED 0x80 00046 00047 process_event_t sensors_event; 00048 00049 static unsigned char num_sensors; 00050 00051 PROCESS(sensors_process, "Sensors"); 00052 00053 /*---------------------------------------------------------------------------*/ 00054 static int 00055 get_sensor_index(const struct sensors_sensor *s) 00056 { 00057 int i; 00058 for(i = 0; i < num_sensors; ++i) { 00059 if(sensors[i] == s) { 00060 return i; 00061 } 00062 } 00063 return i; 00064 } 00065 /*---------------------------------------------------------------------------*/ 00066 const struct sensors_sensor * 00067 sensors_first(void) 00068 { 00069 return sensors[0]; 00070 } 00071 /*---------------------------------------------------------------------------*/ 00072 const struct sensors_sensor * 00073 sensors_next(const struct sensors_sensor *s) 00074 { 00075 return sensors[get_sensor_index(s) + 1]; 00076 } 00077 /*---------------------------------------------------------------------------*/ 00078 void 00079 sensors_changed(const struct sensors_sensor *s) 00080 { 00081 sensors_flags[get_sensor_index(s)] |= FLAG_CHANGED; 00082 process_poll(&sensors_process); 00083 } 00084 /*---------------------------------------------------------------------------*/ 00085 const struct sensors_sensor * 00086 sensors_find(const char *prefix) 00087 { 00088 int i; 00089 unsigned short len; 00090 00091 /* Search through all processes and search for the specified process 00092 name. */ 00093 len = strlen(prefix); 00094 00095 for(i = 0; i < num_sensors; ++i) { 00096 if(strncmp(prefix, sensors[i]->type, len) == 0) { 00097 return sensors[i]; 00098 } 00099 } 00100 return NULL; 00101 } 00102 /*---------------------------------------------------------------------------*/ 00103 PROCESS_THREAD(sensors_process, ev, data) 00104 { 00105 static int i; 00106 static int events; 00107 00108 PROCESS_BEGIN(); 00109 00110 sensors_event = process_alloc_event(); 00111 00112 for(i = 0; sensors[i] != NULL; ++i) { 00113 sensors_flags[i] = 0; 00114 sensors[i]->configure(SENSORS_HW_INIT, 0); 00115 } 00116 num_sensors = i; 00117 00118 while(1) { 00119 00120 PROCESS_WAIT_EVENT(); 00121 00122 do { 00123 events = 0; 00124 for(i = 0; i < num_sensors; ++i) { 00125 if(sensors_flags[i] & FLAG_CHANGED) { 00126 if(process_post(PROCESS_BROADCAST, sensors_event, (void *)sensors[i]) == PROCESS_ERR_OK) { 00127 PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event); 00128 } 00129 sensors_flags[i] &= ~FLAG_CHANGED; 00130 events++; 00131 } 00132 } 00133 } while(events); 00134 } 00135 00136 PROCESS_END(); 00137 } 00138 /*---------------------------------------------------------------------------*/