4 #include "../bbb_pin.h"
8 #define STRAIN_ADC ADC0
11 * Convert Strain gauge id number to a GPIO pin on the Mux
12 * @param id - The strain gauge id
13 * @returns - GPIO pin number
15 static int Strain_To_GPIO(StrainID id)
17 // Could use a lookup table; that would assume strict ordering of the enum in strain.h
18 //static int lookup[] = {GPIO0_30, GPIO1_28,GPIO0_31,GPIO1_16};
19 //if (id < STRAIN0 || id > STRAIN3)
20 // Fatal("unknown strain id %d", id);
34 Fatal("Unknown StrainID %d", id);
35 return -1; // Should never happen
40 * Convert ADC Strain reading to a physically meaningful value
41 * @param reading - The ADC reading
42 * @returns Something more useful
44 static double Strain_Calibrated(int reading)
46 return (double)(reading);
50 * Initialise a Strain gauge
51 * @param id - The strain gauge to initialise
52 * @param name - Name of the strain gauge; ignored
53 * @returns true if initialisation was successful
55 bool Strain_Init(const char * name, int id)
57 int gpio_num = Strain_To_GPIO(id);
58 GPIO_Export(gpio_num);
59 if (!GPIO_Set(gpio_num, false))
60 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW", gpio_num, id);
62 static bool init_adc = false;
66 ADC_Export(STRAIN_ADC);
71 bool Strain_Cleanup(int id)
73 static bool kill_adc = false;
77 ADC_Unexport(STRAIN_ADC);
79 int gpio_num = Strain_To_GPIO(id);
80 GPIO_Unexport(gpio_num);
84 bool Strain_Sanity(int id, double value)
90 * Read from a Strain gauge
91 * @param id - The strain gauge to read
92 * @param val - Will store the read value if successful
93 * @returns true on successful read
95 bool Strain_Read(int id, double * value)
97 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // CRITICAL SECTION - Only one Strain gauge can be read at a time
98 pthread_mutex_lock(&mutex);
100 int gpio_num = Strain_To_GPIO(id);
102 // Set the multiplexer
103 if (!GPIO_Set(gpio_num, true))
104 Fatal("Couldn't set GPIO%d for strain sensor %d to HIGH (before reading)", gpio_num,id);
108 bool result = ADC_Read(STRAIN_ADC, &tmp); // If this fails, it's not fatal
111 *value = Strain_Calibrated(tmp);
113 // Unset the multiplexer
114 if (!GPIO_Set(gpio_num, false))
115 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW (after reading)", gpio_num, id);
117 pthread_mutex_unlock(&mutex);