#ifndef _SENSOR_H
#define _SENSOR_H
-/** Number of data points to keep in sensor buffers **/
-#define SENSOR_DATABUFSIZ 10
+#include "data.h"
+#include "device.h"
-#define SENSOR_QUERYBUFSIZ 10
-/** Number of sensors **/
-#define NUMSENSORS 4
-
-typedef enum SensorId{
- ANALOG_TEST0,
- ANALOG_TEST1,
- DIGITAL_TEST0,
- DIGITAL_TEST1
-} SensorId;
+/**
+ * Maximum number of sensors program can be compiled with
+ * (If you get an error "Increase SENSORS_MAX from %d" this is what it refers to)
+ */
+#define SENSORS_MAX 10
+extern int g_num_sensors; // in sensor.c
-extern const char * g_sensor_names[NUMSENSORS];
-/** Structure to represent data recorded by a sensor at an instant in time **/
+/** Structure to define the warning and error thresholds of the sensors **/
+//TODO: Replace with a call to an appropriate "Sanity" function? (see the actuator code)
typedef struct
{
- /** Time at which data was taken **/
- double time_stamp;
- /** Value of data **/
- double value;
-} DataPoint;
+ /** Maximum safe value **/
+ double max_error;
+ /** Minimum safe value **/
+ double min_error;
+ /** Maximum value before a warning is reported **/
+ double max_warn;
+ /** Minimum value before a warning is reported **/
+ double min_warn;
+} SensorThreshold;
/** Structure to represent a sensor **/
typedef struct
{
/** ID number of the sensor **/
- SensorId id;
- /** Buffer to store data from the sensor **/
- DataPoint buffer[SENSOR_DATABUFSIZ];
- /** Index of last point written in the data buffer **/
- int write_index;
- /** Number of points read **/
- long points_read;
- /** Binary file to write data into when buffer is full **/
- FILE * file;
- /** Thread running the sensor **/
+ int id;
+ /** User defined ID number **/
+ int user_id;
+ /** DataFile to store sensor values in **/
+ DataFile data_file;
+ /** Indicates whether the Sensor is active or not **/
+ bool activated;
+ /** Thread the Sensor is running in **/
pthread_t thread;
- /** Mutex to protect access to stuff **/
- pthread_mutex_t mutex;
+ /** Function to read the sensor **/
+ ReadFn read;
+ /** Function to initialise the sensor **/
+ InitFn init;
+ /** Function to cleanup the sensor **/
+ CleanFn cleanup;
+ /** Function to sanity check the sensor readings **/
+ SanityFn sanity;
+ /** Human readable name of the sensor **/
+ const char * name;
+ /** Sampling rate **/
+ int sample_us;
+ /** Number of averages per sample **/
+ int averages;
+
} Sensor;
+extern void Sensor_Init(); // One off initialisation of *all* sensors
+extern void Sensor_Cleanup(); // Cleanup all sensors
-extern void Sensor_Spawn(); // Initialise sensor
-extern void Sensor_Join(); //Join sensor threads
-extern void * Sensor_Main(void * args); // main loop for sensor thread; pass a Sensor* cast to void*
+extern void Sensor_SetModeAll(ControlModes mode, void * arg);
+extern void Sensor_SetMode(Sensor * s, ControlModes mode, void * arg);
-extern int Sensor_Query(Sensor * s, DataPoint * buffer, int bufsiz); // fill buffer with sensor data
+extern void * Sensor_Loop(void * args); // Main loop for a thread that handles a Sensor
+extern bool Sensor_Read(Sensor * s, DataPoint * d); // Read a single DataPoint, indicating if it has changed since the last one
+extern Sensor * Sensor_Identify(const char * str); // Identify a Sensor from a string
+
+extern void Sensor_Handler(FCGIContext *context, char * params); // Handle a FCGI request for Sensor data
+
+extern const char * Sensor_GetName(int id);
-extern void Sensor_Handler(FCGIContext *context, char * params);
#endif //_SENSOR_H
+