3 * @purpose Implementation of Pressure reading functions
7 #include "../bbb_pin.h"
8 #include "../log.h" // For Fatal()
10 #define PSI_TO_KPA 6.89475729
13 * Get the ADC number of a Pressure sensor
14 * @param id - Id of the sensor
15 * @returns ADC as defined in bbb_pin_defines.h
17 static int Pressure_GetADC(int id)
28 Fatal("Unknown Pressure id %d", id);
29 return -1; // Should never happen
34 * Convert an ADC voltage into a Pressure reading
35 * @param id - Sensor ID
36 * @param adc - ADC reading
37 * @returns Pressure in kPa
39 double Pressure_Callibrate(int id, int adc)
41 //double voltage = ADC_TO_VOLTS(adc); // convert reading to voltage
51 static const double Vs = 5e3; // In mVs
52 static const double Pmin = 0.0 * PSI_TO_KPA;
53 static const double Pmax = 150.0 * PSI_TO_KPA;
54 double Vout = ADC_TO_MVOLTS(adc);
55 return ((Vout - 0.1*Vs)/(0.8*Vs))*(Pmax - Pmin) + Pmin;
58 return (200.0 * (adc / ADC_RAW_MAX));
60 Fatal("Unknown Pressure id %d", id);
61 return -1; // Should never happen
67 * Initialise a Pressure sensor
68 * @param name - Ignored
69 * @param id - The id of the Pressure sensor
70 * @returns true on success, false on error
72 bool Pressure_Init(const char * name, int id)
74 return ADC_Export(Pressure_GetADC(id));
78 * Cleanup a Pressure Sensor
79 * @param id - The id of the sensor to cleanup
80 * @returns true on success, false on failure
82 bool Pressure_Cleanup(int id)
84 ADC_Unexport(Pressure_GetADC(id));
89 * Read a Pressure Sensor
90 * @param id - id of the sensor to read
91 * @param value - Where the value will be stored on a successful read
92 * @returns true on success, false on failure
94 bool Pressure_Read(int id, double * value)
96 //static pthread_mutex_t mutex = PTHREAD_MUTEX_INITIALIZER;
97 //pthread_mutex_lock(&mutex);
100 if (ADC_Read(Pressure_GetADC(id), &adc))
102 *value = Pressure_Callibrate(id, adc);
105 //pthread_mutex_unlock(&mutex);