+ * 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));
+ g_sensors[i].record_data = false;
+ }
+
+ // Get the ADCs
+ ADC_Export();
+
+ // GPIO1_28 used as a pulse for sampling
+ GPIO_Export(GPIO1_28);
+ // GPIO0_30 toggled during sampling
+ GPIO_Export(GPIO0_30);
+}
+
+/**
+ * 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;
+ int ret;
+
+ 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);
+
+ s->activated = true;
+ s->record_data = 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);
+ }
+ }
+ break;
+ case CONTROL_EMERGENCY:
+ case CONTROL_PAUSE:
+ s->record_data = false;
+ break;
+ case CONTROL_RESUME:
+ s->record_data = true;
+ break;
+ case CONTROL_STOP:
+ s->activated = false;
+ s->record_data = false;
+ pthread_join(s->thread, NULL);
+
+ Data_Close(&(s->data_file)); // Close DataFile
+ s->newest_data.time_stamp = 0;
+ s->newest_data.value = 0;
+ 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);
+}
+
+
+/**
+ * Checks the sensor data for unsafe or unexpected results