11package com .opensourcewithslu .inputdevices ;
22
3- import com .pi4j .context .Context ;
43import com .pi4j .io .spi .Spi ;
5- import com .pi4j .io .spi .SpiConfig ;
6- import com .pi4j .io .spi .SpiMode ;
74import org .slf4j .Logger ;
85import org .slf4j .LoggerFactory ;
96
107public class ThermistorHelper {
118 private static final Logger log = LoggerFactory .getLogger (ThermistorHelper .class );
129 private final Spi spi ; // SPI interface for ADC communication
10+ private final ADC0834ConverterHelper adcConverterHelper ;
1311
1412 // Constants for Steinhart-Hart equation (example values; replace with actual thermistor constants)
1513 private static final double A = 0.001129148 ;
1614 private static final double B = 0.000234125 ;
1715 private static final double C = 0.0000000876741 ;
1816
19- public ThermistorHelper (Context pi4j ) {
20- this .spi = initializeADC (pi4j );
17+ /**
18+ * Constructor for ThermistorHelper.
19+ * @param adcConverterHelper The ADC0834ConverterHelper object.
20+ */
21+ public ThermistorHelper ( ADC0834ConverterHelper adcConverterHelper ) {
22+ this .adcConverterHelper = adcConverterHelper ;
23+ this .spi = adcConverterHelper .getSpi ();
24+ log .info ("SPI and ADCConverter for thermistor initialized." );
2125 }
2226
2327 /**
@@ -30,65 +34,23 @@ Spi getSpi() {
3034 return spi ;
3135 }
3236
33- /**
34- * Initialize SPI and ADC settings specific to ADC0834.
35- */
36- private Spi initializeADC (Context pi4j ) {
37- try {
38- // Create SPI configuration for the ADC0834
39- SpiConfig spiConfig = Spi .newConfigBuilder (pi4j )
40- .id ("ADC0834" )
41- .name ("Thermistor ADC" )
42- .address (17 ) // SPI channel 0
43- .baud (1000000 ) // 1 MHz SPI clock speed
44- .mode (SpiMode .MODE_0 )
45- .build ();
46-
47- Spi spi = pi4j .create (spiConfig );
48- log .info ("SPI and ADC for thermistor initialized." );
49- return spi ;
50-
51- } catch (Exception e ) {
52- log .error ("Failed to initialize SPI for ADC0834" , e );
53- throw new RuntimeException ("SPI initialization failed" , e );
54- }
55- }
56-
5737 /**
5838 * Reads the raw value from the thermistor via ADC and converts it to resistance.
39+ * @param channel The ADC channel to read (0-3).
5940 * @return the resistance value of the thermistor
6041 */
61- public double getResistance () {
62- double rawValue = readADCValue ();
42+ public double getResistance (int channel ) {
43+ double rawValue = readADCValue (channel );
6344 return convertRawToResistance (rawValue );
6445 }
6546
6647 /**
6748 * Reads the ADC value from the thermistor on ADC0834.
49+ * @param channel The ADC channel to read (0-3).
6850 * @return the raw ADC value as a double
6951 */
70- public double readADCValue () {
71- byte [] packet = new byte [3 ];
72- byte [] response = new byte [3 ]; // To store the response from ADC0834
73-
74- // ADC0834 requires sending a "start" bit sequence to initiate the read
75- packet [0 ] = 0x01 ; // Start bit
76- packet [1 ] = (byte ) (0x80 ); // Single-ended mode and channel selection (channel 0 for thermistor)
77- packet [2 ] = 0x00 ; // Placeholder byte to read data
78-
79- try {
80- // Send the command packet and receive response from ADC0834
81- spi .write (packet ); // Send the packet
82- spi .read (response ); // Read the response into the response array
83-
84- // Process the response to extract ADC value (10-bit resolution for ADC0834)
85- int rawValue = ((response [1 ] & 0x03 ) << 8 ) + (response [2 ] & 0xFF ); // Combine bits for 10-bit ADC result
86- log .info ("Raw ADC Value: {}" , rawValue );
87- return rawValue ;
88- } catch (Exception e ) {
89- log .error ("Failed to read from ADC0834" , e );
90- return -1 ; // Return a flag value indicating an error
91- }
52+ public double readADCValue ( int channel ) {
53+ return adcConverterHelper .readValue (channel );
9254 }
9355
9456
@@ -107,10 +69,11 @@ private double convertRawToResistance(double rawValue) {
10769
10870 /**
10971 * Calculates the temperature in Celsius using the Steinhart-Hart equation.
72+ * @param channel The ADC channel to read (0-3).
11073 * @return temperature in Celsius.
11174 */
112- public double getTemperatureInCelsius () {
113- double resistance = getResistance ();
75+ public double getTemperatureInCelsius (int channel ) {
76+ double resistance = getResistance (channel );
11477 double temperatureInKelvin = 1.0 / (A + B * Math .log (resistance ) + C * Math .pow (Math .log (resistance ), 3 ));
11578 double temperatureCelsius = temperatureInKelvin - 273.15 ;
11679 log .info ("Temperature in Celsius: {}" , temperatureCelsius );
@@ -119,10 +82,11 @@ public double getTemperatureInCelsius() {
11982
12083 /**
12184 * Converts temperature in Celsius to Fahrenheit.
85+ * @param channel The ADC channel to read (0-3).
12286 * @return temperature in Fahrenheit.
12387 */
124- public double getTemperatureInFahrenheit () {
125- double temperatureFahrenheit = getTemperatureInCelsius () * 9 / 5 + 32 ;
88+ public double getTemperatureInFahrenheit (int channel ) {
89+ double temperatureFahrenheit = getTemperatureInCelsius (channel ) * 9 / 5 + 32 ;
12690 log .info ("Temperature in Fahrenheit: {}" , temperatureFahrenheit );
12791 return temperatureFahrenheit ;
12892 }
0 commit comments