sensors.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 <string.h>
00037
00038 #include "contiki.h"
00039
00040 #include "lib/sensors.h"
00041
00042 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 struct sensors_sensor *
00067 sensors_first(void)
00068 {
00069 return sensors[0];
00070 }
00071
00072 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 struct sensors_sensor *
00086 sensors_find(const char *prefix)
00087 {
00088 int i;
00089 unsigned short len;
00090
00091
00092
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, 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