Skip to content

Commit f11f9bd

Browse files
Merge pull request #428 from sbera13/fix-379
Refactoring Thermistor component to use ADC0834Converter
2 parents f7d2d10 + 881ad5a commit f11f9bd

7 files changed

Lines changed: 99 additions & 129 deletions

File tree

components/src/main/java/com/opensourcewithslu/components/controllers/ADC0834ConverterController.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
package com.opensourcewithslu.components.controllers;
22

3-
import com.pi4j.io.spi.Spi;
43
import com.opensourcewithslu.inputdevices.ADC0834ConverterHelper;
4+
import com.pi4j.context.Context;
5+
import com.pi4j.io.spi.SpiConfig;
56
import io.micronaut.http.annotation.Controller;
67
import io.micronaut.http.annotation.Get;
78
import jakarta.inject.Named;
@@ -19,10 +20,11 @@ public class ADC0834ConverterController {
1920
/**
2021
* Constructor for ADC0834ConverterController.
2122
*
22-
* @param spi SPI interface
23+
* @param spiConfig A Pi4J SPIConfig object which holds the SPI configuration.
24+
* @param pi4jContext The Pi4J context object.
2325
*/
24-
public ADC0834ConverterController(@Named("adc0834") Spi spi) {
25-
this.adcConverterHelper = new ADC0834ConverterHelper(spi);
26+
public ADC0834ConverterController(@Named("adc0834") SpiConfig spiConfig, Context pi4jContext) {
27+
this.adcConverterHelper = new ADC0834ConverterHelper(spiConfig, pi4jContext);
2628
log.info("ADC0834ConverterController initialized with SPI");
2729
}
2830

@@ -52,4 +54,4 @@ public double readVoltage(int channel, double referenceVoltage) {
5254
log.info("Voltage retrieved from ADC0834 channel {}: {}V", channel, voltage);
5355
return voltage;
5456
}
55-
}
57+
}
Lines changed: 22 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,44 @@
11
package com.opensourcewithslu.components.controllers;
22

3+
import com.opensourcewithslu.inputdevices.ADC0834ConverterHelper;
34
import com.opensourcewithslu.inputdevices.ThermistorHelper;
45
import com.pi4j.context.Context;
5-
import com.pi4j.io.gpio.digital.DigitalInput;
6+
import com.pi4j.io.spi.SpiConfig;
67
import io.micronaut.http.annotation.Controller;
78
import io.micronaut.http.annotation.Get;
8-
import jakarta.inject.Inject;
99
import jakarta.inject.Named;
1010

1111
@Controller("/thermistor")
1212
public class ThermistorController {
1313

1414
private final ThermistorHelper thermistorHelper;
15-
16-
// Inject Pi4J Context and digital pins for the thermistor
17-
@Inject
18-
public ThermistorController(Context pi4jContext) {
19-
20-
// Initialize the ThermistorHelper with SPI configuration
21-
this.thermistorHelper = new ThermistorHelper(pi4jContext);
15+
private final ADC0834ConverterHelper adcConverterHelper;
16+
17+
/**
18+
* Constructor for ThermistorController.
19+
*
20+
* @param spiConfig A Pi4J SPIConfig object which holds the SPI configuration for the ADC0834.
21+
* @param pi4jContext The Pi4J context object.
22+
*/
23+
public ThermistorController(@Named("adc0834") SpiConfig spiConfig, Context pi4jContext) {
24+
adcConverterHelper = new ADC0834ConverterHelper(spiConfig, pi4jContext);
25+
thermistorHelper = new ThermistorHelper( adcConverterHelper );
2226
}
2327

2428
// Endpoint to get temperature in Celsius
25-
@Get("/temperature/celsius")
26-
public double getTemperatureCelsius() {
27-
return thermistorHelper.getTemperatureInCelsius();
29+
@Get("/temperature/celsius/{channel}")
30+
public double getTemperatureCelsius(int channel) {
31+
return thermistorHelper.getTemperatureInCelsius( channel );
2832
}
2933

3034
// Endpoint to get temperature in Fahrenheit
31-
@Get("/temperature/fahrenheit")
32-
public double getTemperatureFahrenheit() {
33-
return thermistorHelper.getTemperatureInFahrenheit();
35+
@Get("/temperature/fahrenheit/{channel}")
36+
public double getTemperatureFahrenheit(int channel) {
37+
return thermistorHelper.getTemperatureInFahrenheit( channel );
3438
}
3539

36-
@Get("/rawValue")
37-
public double readADCValue(){
38-
return thermistorHelper.readADCValue();
40+
@Get("/rawValue/{channel}")
41+
public double readADCValue(int channel){
42+
return thermistorHelper.readADCValue( channel );
3943
}
4044
}

components/src/main/resources/application.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pi4j:
1414
name: ADC0834 # <1>
1515
address: 17 # <2> # SPI channel 0
1616
baud: 1000000 # <3> # 1 MHz SPI clock speed
17-
mode: SPI_MODE_0
17+
reset-pin: 27
1818
# end::spi[]
1919

2020
# tag::pwm[]

pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ADC0834ConverterHelper.java

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.opensourcewithslu.inputdevices;
22

3+
import com.pi4j.context.Context;
34
import com.pi4j.io.spi.Spi;
5+
import com.pi4j.io.spi.SpiConfig;
46
import org.slf4j.Logger;
57
import org.slf4j.LoggerFactory;
68

@@ -14,13 +16,23 @@ public class ADC0834ConverterHelper {
1416

1517
/**
1618
* Constructor for ADC0834ConverterHelper.
17-
* @param spi The SPI interface.
19+
* @param spiConfig A Pi4J SPIConfig object which holds the SPI configuration.
20+
* @param pi4jContext The Pi4J context object.
1821
*/
19-
public ADC0834ConverterHelper(Spi spi) {
20-
this.spi = spi;
22+
public ADC0834ConverterHelper(SpiConfig spiConfig, Context pi4jContext) {
23+
this.spi = pi4jContext.create(spiConfig);
2124
log.info("ADC0834ConverterHelper initialized with SPI");
2225
}
2326

27+
28+
/* Gets the SPI interface used by this class.
29+
*
30+
* @return the SPI interface
31+
*/
32+
Spi getSpi() {
33+
return spi;
34+
}
35+
2436
/**
2537
* Reads the digital value from the specified channel (0-3) of the ADC0834.
2638
* @param channel The ADC channel to read (0-3).
@@ -67,4 +79,4 @@ public double readVoltage(int channel, double referenceVoltage) {
6779
log.info("Channel {} voltage: {}V (raw = {}, ref = {}V)", channel, voltage, rawValue, referenceVoltage);
6880
return voltage;
6981
}
70-
}
82+
}

pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ThermistorHelper.java

Lines changed: 21 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,27 @@
11
package com.opensourcewithslu.inputdevices;
22

3-
import com.pi4j.context.Context;
43
import com.pi4j.io.spi.Spi;
5-
import com.pi4j.io.spi.SpiConfig;
6-
import com.pi4j.io.spi.SpiMode;
74
import org.slf4j.Logger;
85
import org.slf4j.LoggerFactory;
96

107
public 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
}

pi4micronaut-utils/src/test/java/com/opensourcewithslu/inputdevices/ADC0834ConverterHelperTest.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,17 @@ public class ADC0834ConverterHelperTest {
1717
private static final Logger log = LoggerFactory.getLogger(ADC0834ConverterHelperTest.class);
1818

1919
private Context mockContext;
20+
private SpiConfig mockSpiConfig;
2021
private Spi mockSpi;
2122
private ADC0834ConverterHelper adc;
2223

2324
@BeforeEach
2425
public void setUp() {
2526
mockContext = Mockito.mock(Context.class);
2627
mockSpi = Mockito.mock(Spi.class);
28+
mockSpiConfig = Mockito.mock(SpiConfig.class);
2729
when(mockContext.create(any(SpiConfig.class))).thenReturn(mockSpi);
28-
adc = new ADC0834ConverterHelper(mockSpi);
30+
adc = new ADC0834ConverterHelper(mockSpiConfig, mockContext);
2931
}
3032

3133
@Test

0 commit comments

Comments
 (0)