4 #include "../bbb_pin.h"
8 #define STRAIN_ADC ADC0
10 #define STRAIN_GPIO 15
13 * Convert Strain gauge id number to a GPIO pin on the Mux
14 * @param id - The strain gauge id
15 * @returns - GPIO pin number
17 static int Strain_To_GPIO(StrainID id)
19 // Could use a lookup table; that would assume strict ordering of the enum in strain.h
20 //static int lookup[] = {GPIO0_30, GPIO1_28,GPIO0_31,GPIO1_16};
21 //if (id < STRAIN0 || id > STRAIN3)
22 // Fatal("unknown strain id %d", id);
36 Fatal("Unknown StrainID %d", id);
37 return -1; // Should never happen
42 * Convert ADC Strain reading to a physically meaningful value
43 * @param reading - The ADC reading
44 * @returns Something more useful
46 static double Strain_Calibrated(int reading)
48 return (double)(reading);
52 * Initialise a Strain gauge
53 * @param id - The strain gauge to initialise
54 * @param name - Name of the strain gauge; ignored
55 * @returns true if initialisation was successful
57 bool Strain_Init(const char * name, int id)
59 int gpio_num = Strain_To_GPIO(id);
60 GPIO_Export(gpio_num);
61 if (!GPIO_Set(gpio_num, false))
62 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW", gpio_num, id);
67 GPIO_Export(STRAIN_GPIO);
68 GPIO_Set(STRAIN_GPIO, true);
69 ADC_Export(STRAIN_ADC);
74 bool Strain_Cleanup(int id)
76 static int killed = 0;
80 GPIO_Set(STRAIN_GPIO, false);
81 ADC_Unexport(STRAIN_ADC);
84 int gpio_num = Strain_To_GPIO(id);
85 GPIO_Unexport(gpio_num);
89 bool Strain_Sanity(int id, double value)
95 * Read from a Strain gauge
96 * @param id - The strain gauge to read
97 * @param val - Will store the read value if successful
98 * @returns true on successful read
100 bool Strain_Read(int id, double * value)
102 static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER; // CRITICAL SECTION - Only one Strain gauge can be read at a time
103 pthread_mutex_lock(&mutex);
105 int gpio_num = Strain_To_GPIO(id);
107 // Set the multiplexer
108 if (!GPIO_Set(gpio_num, true))
109 Fatal("Couldn't set GPIO%d for strain sensor %d to HIGH (before reading)", gpio_num,id);
113 bool result = ADC_Read(STRAIN_ADC, &tmp); // If this fails, it's not fatal
116 *value = Strain_Calibrated(tmp);
118 // Unset the multiplexer
119 if (!GPIO_Set(gpio_num, false))
120 Fatal("Couldn't set GPIO%d for strain sensor %d to LOW (after reading)", gpio_num, id);
122 pthread_mutex_unlock(&mutex);