Skip to content

Commit

Permalink
Merge pull request #281 from oss-slu/278-Analog-to-Digital
Browse files Browse the repository at this point in the history
Implementation for Analog to Digital converter. Fixes #281
  • Loading branch information
yrlmanoharreddy authored Nov 19, 2024
2 parents 4b39fb3 + cbcca77 commit dcb8426
Show file tree
Hide file tree
Showing 3 changed files with 131 additions and 0 deletions.
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;
}
}
20 changes: 20 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,20 @@ 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
digit-0:
name: Digit 0
address: 17
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();
}
}

0 comments on commit dcb8426

Please sign in to comment.