Fix everything
[matches/MCTX3420.git] / server / sensors / strain.c
1 #include "strain.h"
2
3 #include "../log.h"
4 #include "../bbb_pin.h"
5
6 #include <pthread.h>
7
8 #define STRAIN_ADC ADC0
9
10 /**
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
14  */
15 static int Strain_To_GPIO(StrainID id)
16 {
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);
21         //return lookup[id];
22
23         switch (id)
24         {
25                 case STRAIN0:
26                         return GPIO0_30;
27                 case STRAIN1:
28                         return GPIO1_28;
29                 case STRAIN2:
30                         return GPIO0_31;
31                 case STRAIN3:
32                         return GPIO1_16;
33                 default:
34                         Fatal("Unknown StrainID %d", id);
35                         return -1; // Should never happen
36   }
37 }
38
39 /**
40  * Convert ADC Strain reading to a physically meaningful value
41  * @param reading - The ADC reading
42  * @returns Something more useful
43  */
44 static double Strain_Calibrated(int reading)
45 {
46         return (double)(reading);
47 }
48
49 /**
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
54  */
55 bool Strain_Init(const char * name, int id)
56 {
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);
61
62         static bool init_adc = false;
63         if (!init_adc)
64         {
65                 init_adc = true;
66                 ADC_Export(STRAIN_ADC);
67         }
68         return true;
69 }
70
71 bool Strain_Cleanup(int id)
72 {
73         static bool kill_adc = false;
74         if (!kill_adc)
75         {
76                 kill_adc = true;
77                 ADC_Unexport(STRAIN_ADC);
78         }
79         int gpio_num = Strain_To_GPIO(id);
80         GPIO_Unexport(gpio_num);
81         return true;
82 }
83
84 bool Strain_Sanity(int id, double value)
85 {
86         return true;
87 }
88
89 /**
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
94  */
95 bool Strain_Read(int id, double * value)
96 {
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);
99
100         int gpio_num = Strain_To_GPIO(id);
101         
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);
105
106         // Read the ADC
107         int tmp = 0;
108         bool result = ADC_Read(STRAIN_ADC, &tmp); // If this fails, it's not fatal
109         
110         //TODO: Callibrate?
111         *value = Strain_Calibrated(tmp);
112
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);
116
117         pthread_mutex_unlock(&mutex);
118
119         return result;
120 }

UCC git Repository :: git.ucc.asn.au