+/** Array of sensors, initialised by Sensor_Init **/
+static Sensor g_sensors[NUMSENSORS]; //global to this file
+
+/** Array of sensor threshold structures defining the safety values of each sensor**/
+const SensorThreshold thresholds[NUMSENSORS]= {
+ //Max Safety, Min safety, Max warning, Min warning
+ {1,-1,1,-1}, // ANALOG_TEST0
+ {500,0,499,0}, // ANALOG_TEST1
+ {5,-5,4,-4}, // ANALOG_FAIL0
+ {1,0,1,0}, // DIGITAL_TEST0
+ {1,0,1,0}, // DIGITAL_TEST1
+ {1,0,1,0} // DIGITAL_FAIL0
+};
+
+/** Human readable names for the sensors **/
+const char * g_sensor_names[NUMSENSORS] = {
+ "analog_test0", "analog_test1",
+ "analog_fail0", "digital_test0",
+ "digital_test1", "digital_fail0"
+};
+
+/**
+ * One off initialisation of *all* sensors
+ */
+void Sensor_Init()
+{
+ for (int i = 0; i < NUMSENSORS; ++i)
+ {
+ g_sensors[i].id = i;
+ Data_Init(&(g_sensors[i].data_file));
+ }
+}
+
+/**
+ * Sets the sensor to the desired control mode. No checks are
+ * done to see if setting to the desired mode will conflict with
+ * the current mode - the caller must guarantee this itself.
+ * @param s The sensor whose mode is to be changed
+ * @param mode The mode to be changed to
+ * @param arg An argument specific to the mode to be set.
+ * e.g for CONTROL_START it represents the experiment name.
+ */
+void Sensor_SetMode(Sensor * s, ControlModes mode, void * arg)
+{
+ switch(mode)
+ {
+ case CONTROL_START:
+ {
+ // Set filename
+ char filename[BUFSIZ];
+ const char *experiment_name = (const char*) arg;
+
+ if (snprintf(filename, BUFSIZ, "%s_s%d", experiment_name, s->id) >= BUFSIZ)
+ {
+ Fatal("Experiment name \"%s\" too long (>%d)", experiment_name, BUFSIZ);
+ }
+
+ Log(LOGDEBUG, "Sensor %d with DataFile \"%s\"", s->id, filename);
+ // Open DataFile
+ Data_Open(&(s->data_file), filename);
+ }
+ case CONTROL_RESUME: //Case fallthrough, no break before
+ {
+ int ret;
+ s->activated = true; // Don't forget this!
+
+ // Create the thread
+ ret = pthread_create(&(s->thread), NULL, Sensor_Loop, (void*)(s));
+ if (ret != 0)
+ {
+ Fatal("Failed to create Sensor_Loop for Sensor %d", s->id);
+ }
+
+ Log(LOGDEBUG, "Resuming sensor %d", s->id);
+ }
+ break;
+
+ case CONTROL_EMERGENCY:
+ case CONTROL_PAUSE:
+ s->activated = false;
+ pthread_join(s->thread, NULL);
+ Log(LOGDEBUG, "Paused sensor %d", s->id);
+ break;
+
+ case CONTROL_STOP:
+ if (s->activated) //May have been paused before
+ {
+ s->activated = false;
+ pthread_join(s->thread, NULL);
+ }
+
+ Data_Close(&(s->data_file)); // Close DataFile
+ s->newest_data.time_stamp = 0;
+ s->newest_data.value = 0;
+ Log(LOGDEBUG, "Stopped sensor %d", s->id);
+ break;
+ default:
+ Fatal("Unknown control mode: %d", mode);
+ }
+}
+
+/**
+ * Sets all sensors to the desired mode.
+ * @see Sensor_SetMode for more information.
+ * @param mode The mode to be changed to
+ * @param arg An argument specific to the mode to be set.
+ */
+void Sensor_SetModeAll(ControlModes mode, void * arg)
+{
+ for (int i = 0; i < NUMSENSORS; i++)
+ Sensor_SetMode(&g_sensors[i], mode, arg);
+}
+
+