From f71315db3a05f443ca2630e36f8e494744faadf4 Mon Sep 17 00:00:00 2001 From: ThomasMacas Date: Mon, 11 Nov 2024 12:10:12 -0600 Subject: [PATCH] Implementation for Analog to Digital converter --- .../controllers/ADCConverterController.java | 46 +++++++++++++ components/src/main/resources/application.yml | 21 ++++++ .../inputdevices/ADCConverterHelper.java | 65 +++++++++++++++++++ 3 files changed, 132 insertions(+) create mode 100644 components/src/main/java/com/opensourcewithslu/components/controllers/ADCConverterController.java create mode 100644 pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ADCConverterHelper.java diff --git a/components/src/main/java/com/opensourcewithslu/components/controllers/ADCConverterController.java b/components/src/main/java/com/opensourcewithslu/components/controllers/ADCConverterController.java new file mode 100644 index 00000000..4516fca8 --- /dev/null +++ b/components/src/main/java/com/opensourcewithslu/components/controllers/ADCConverterController.java @@ -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; + } +} diff --git a/components/src/main/resources/application.yml b/components/src/main/resources/application.yml index 9e27c6d2..f6ef95cf 100644 --- a/components/src/main/resources/application.yml +++ b/components/src/main/resources/application.yml @@ -138,6 +138,12 @@ pi4j: pulses-per-revolution: 20 # <3> provider: pigpio-digital-input # <4> debounce: 500 # <5> + adc-dout: + name: ADC DataOut + address: 8 # <4> Specify the GPIO pin for Data Out + pull: PULL_DOWN # Configure pull-up or pull-down as needed + debounce: 100 # Optional debounce time for stable readings + provider: pigpio-digital-input # end::digitalInput[] # tag::digitalOutput[] @@ -166,6 +172,21 @@ pi4j: shutdown: LOW initial: LOW provider: pigpio-digital-output + adc-cs: + name: ADCChipSelect + address: 5 # <1> Specify the GPIO pin for CS (Chip Select) + initial: HIGH # Initial state for Chip Select (could be HIGH or LOW based on needs) + provider: pigpio-digital-output + adc-clk: + name: ADCClock + address: 6 # <2> Specify the GPIO pin for CLK (Clock) + initial: LOW # Initial state for Clock + provider: pigpio-digital-output + adc-din: + name: ADCDataIn + address: 7 # <3> Specify the GPIO pin for Data In + initial: LOW # Initial state for Data In + provider: pigpio-digital-output # end::digitalOutput[] # tag::multiInput[] diff --git a/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ADCConverterHelper.java b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ADCConverterHelper.java new file mode 100644 index 00000000..4d1d149c --- /dev/null +++ b/pi4micronaut-utils/src/main/java/com/opensourcewithslu/inputdevices/ADCConverterHelper.java @@ -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(); + } +}