-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #281 from oss-slu/278-Analog-to-Digital
Implementation for Analog to Digital converter. Fixes #281
- Loading branch information
Showing
3 changed files
with
131 additions
and
0 deletions.
There are no files selected for viewing
46 changes: 46 additions & 0 deletions
46
...ts/src/main/java/com/opensourcewithslu/components/controllers/ADCConverterController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
package com.opensourcewithslu.components.controllers; | ||
|
||
import com.opensourcewithslu.inputdevices.ADCConverterHelper; | ||
import com.pi4j.io.gpio.digital.DigitalInput; | ||
import com.pi4j.io.gpio.digital.DigitalOutput; | ||
import io.micronaut.http.annotation.Controller; | ||
import io.micronaut.http.annotation.Get; | ||
import jakarta.inject.Named; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* ADCConverterController provides an endpoint to read values from the ADC0834. | ||
*/ | ||
@Controller("/adcConverter") | ||
public class ADCConverterController { | ||
private static final Logger log = LoggerFactory.getLogger(ADCConverterController.class); | ||
private final ADCConverterHelper adcConverterHelper; | ||
|
||
/** | ||
* Constructor for ADCConverterController. | ||
* | ||
* @param cs DigitalOutput for Chip Select | ||
* @param clk DigitalOutput for Clock | ||
* @param din DigitalOutput for Data In | ||
* @param dout DigitalInput for Data Out | ||
*/ | ||
public ADCConverterController(@Named("cs") DigitalOutput cs, | ||
@Named("clk") DigitalOutput clk, | ||
@Named("din") DigitalOutput din, | ||
@Named("dout") DigitalInput dout) { | ||
this.adcConverterHelper = new ADCConverterHelper(cs, clk, din, dout); | ||
} | ||
|
||
/** | ||
* Endpoint to get the digital value from ADC0834. | ||
* | ||
* @return The digital value read from the ADC0834. | ||
*/ | ||
@Get("/read") | ||
public int readValue() { | ||
int value = adcConverterHelper.readValue(); | ||
log.info("Value retrieved from ADC0834: {}", value); | ||
return value; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
65 changes: 65 additions & 0 deletions
65
pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ADCConverterHelper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
package com.opensourcewithslu.inputdevices; | ||
|
||
import com.pi4j.io.gpio.digital.DigitalInput; | ||
import com.pi4j.io.gpio.digital.DigitalOutput; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
|
||
/** | ||
* The ADCConverterHelper class is used to read digital values from the ADC0834, which converts analog signals to digital. | ||
*/ | ||
public class ADCConverterHelper { | ||
private static final Logger log = LoggerFactory.getLogger(ADCConverterHelper.class); | ||
|
||
private final DigitalOutput cs; // Chip Select | ||
private final DigitalOutput clk; // Clock | ||
private final DigitalOutput din; // Data In | ||
private final DigitalInput dout; // Data Out | ||
|
||
/** | ||
* Constructor for ADCConverterHelper. | ||
* | ||
* @param cs DigitalOutput for Chip Select | ||
* @param clk DigitalOutput for Clock | ||
* @param din DigitalOutput for Data In | ||
* @param dout DigitalInput for Data Out | ||
*/ | ||
public ADCConverterHelper(DigitalOutput cs, DigitalOutput clk, DigitalOutput din, DigitalInput dout) { | ||
this.cs = cs; | ||
this.clk = clk; | ||
this.din = din; | ||
this.dout = dout; | ||
} | ||
|
||
/** | ||
* Reads a value from the ADC0834. | ||
* | ||
* @return The converted digital value from the ADC0834. | ||
*/ | ||
public int readValue() { | ||
cs.low(); // Activate ADC | ||
sendStartBit(); | ||
|
||
int digitalValue = 0; | ||
for (int i = 0; i < 8; i++) { | ||
clk.high(); | ||
clk.low(); | ||
if (dout.isHigh()) { | ||
digitalValue |= (1 << (7 - i)); // Construct the digital value bit by bit | ||
} | ||
} | ||
|
||
cs.high(); // Deactivate ADC | ||
log.info("ADC0834 Digital Value Read: {}", digitalValue); | ||
return digitalValue; | ||
} | ||
|
||
/** | ||
* Sends the start bit to initialize communication with ADC0834. | ||
*/ | ||
private void sendStartBit() { | ||
clk.high(); | ||
din.high(); | ||
clk.low(); | ||
} | ||
} |