2006-06-18 00:41:10 +02:00
|
|
|
/*
|
2010-01-14 14:29:56 +01:00
|
|
|
* Copyright (c) 2009, Swedish Institute of Computer Science
|
2006-06-18 00:41:10 +02:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
* This file is part of the Contiki operating system.
|
|
|
|
*
|
2010-01-14 21:04:38 +01:00
|
|
|
* @(#)$Id: sensors.c,v 1.5 2010/01/14 20:04:38 nifi Exp $
|
2006-06-18 00:41:10 +02:00
|
|
|
*/
|
|
|
|
/* exeperimental code, will be renamed to sensors.c when done */
|
|
|
|
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "contiki.h"
|
|
|
|
|
|
|
|
#include "lib/sensors.h"
|
|
|
|
|
|
|
|
extern struct sensors_sensor *sensors[];
|
|
|
|
extern unsigned char sensors_flags[];
|
|
|
|
|
|
|
|
#define FLAG_CHANGED 0x80
|
|
|
|
|
|
|
|
process_event_t sensors_event;
|
|
|
|
|
|
|
|
static unsigned char num_sensors;
|
|
|
|
|
|
|
|
PROCESS(sensors_process, "Sensors");
|
|
|
|
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static int
|
|
|
|
get_sensor_index(const struct sensors_sensor *s)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < num_sensors; ++i) {
|
|
|
|
if(sensors[i] == s) {
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct sensors_sensor *
|
|
|
|
sensors_first(void)
|
|
|
|
{
|
|
|
|
return sensors[0];
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct sensors_sensor *
|
|
|
|
sensors_next(const struct sensors_sensor *s)
|
|
|
|
{
|
|
|
|
return sensors[get_sensor_index(s) + 1];
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
void
|
|
|
|
sensors_changed(const struct sensors_sensor *s)
|
|
|
|
{
|
|
|
|
sensors_flags[get_sensor_index(s)] |= FLAG_CHANGED;
|
|
|
|
process_poll(&sensors_process);
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
struct sensors_sensor *
|
2010-01-14 21:04:38 +01:00
|
|
|
sensors_find(const char *prefix)
|
2006-06-18 00:41:10 +02:00
|
|
|
{
|
|
|
|
int i;
|
|
|
|
unsigned short len;
|
2010-01-14 14:29:56 +01:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
/* Search through all processes and search for the specified process
|
|
|
|
name. */
|
|
|
|
len = strlen(prefix);
|
2010-01-14 14:29:56 +01:00
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
for(i = 0; i < num_sensors; ++i) {
|
|
|
|
if(strncmp(prefix, sensors[i]->type, len) == 0) {
|
|
|
|
return sensors[i];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(sensors_process, ev, data)
|
|
|
|
{
|
2006-10-06 11:18:52 +02:00
|
|
|
static int i;
|
|
|
|
static int events;
|
2006-06-18 00:41:10 +02:00
|
|
|
|
|
|
|
PROCESS_BEGIN();
|
2010-01-14 14:29:56 +01:00
|
|
|
|
|
|
|
sensors_event = process_alloc_event();
|
|
|
|
|
2006-06-18 00:41:10 +02:00
|
|
|
for(i = 0; sensors[i] != NULL; ++i) {
|
|
|
|
sensors_flags[i] = 0;
|
2010-01-14 14:29:56 +01:00
|
|
|
sensors[i]->configure(SENSORS_HW_INIT, 0);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
num_sensors = i;
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
|
|
|
|
PROCESS_WAIT_EVENT();
|
2006-10-06 11:18:52 +02:00
|
|
|
|
|
|
|
do {
|
|
|
|
events = 0;
|
|
|
|
for(i = 0; i < num_sensors; ++i) {
|
|
|
|
if(sensors_flags[i] & FLAG_CHANGED) {
|
|
|
|
if(process_post(PROCESS_BROADCAST, sensors_event, sensors[i]) == PROCESS_ERR_OK) {
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(ev == sensors_event);
|
|
|
|
}
|
|
|
|
sensors_flags[i] &= ~FLAG_CHANGED;
|
|
|
|
events++;
|
|
|
|
}
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
2006-10-06 11:18:52 +02:00
|
|
|
} while(events);
|
2006-06-18 00:41:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|