Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementation for Analog to Digital converter #281

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
21 changes: 21 additions & 0 deletions components/src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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[]
Expand Down Expand Up @@ -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[]
Expand Down
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();
}
}
Loading