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);
72 * Read from a Strain gauge
73 * @param id - The strain gauge to read
74 * @param val - Will store the read value if successful
75 * @returns true on successful read
77 bool Strain_Read(int id, double * value)
79 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // CRITICAL SECTION - Only one Strain gauge can be read at a time
80 pthread_mutex_lock(&mutex);
82 int gpio_num = Strain_To_GPIO(id);
84 // Set the multiplexer
85 if (!GPIO_Set(gpio_num, true))
86 Fatal("Couldn't set GPIO%d for strain sensor %d to HIGH (before reading)", gpio_num,id);
90 bool result = ADC_Read(STRAIN_ADC, &tmp); // If this fails, it's not fatal
93 *value = Strain_Calibrated(tmp);
95 // Unset the multiplexer
96 if (!GPIO_Set(gpio_num, false))
97 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW (after reading)", gpio_num, id);
99 pthread_mutex_unlock(&mutex);