Skip to content

Commit

Permalink
updated code
Browse files Browse the repository at this point in the history
  • Loading branch information
nikkoxp committed Jul 30, 2024
1 parent 4888f53 commit 6e4199e
Showing 1 changed file with 25 additions and 7 deletions.
32 changes: 25 additions & 7 deletions apis/peripherals/adc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,14 @@ impl<S: Syscalls> Adc<S> {
}

// Initiate a sample reading
pub fn read_single_sample(channel: size) -> Result<(), ErrorCode> {
S::command(DRIVER_NUM, SINGLE_SAMPLE, channel, 0).to_result()
pub fn read_single_sample(channel: usize) -> Result<(), ErrorCode> {
S::command(
DRIVER_NUM,
SINGLE_SAMPLE,
(channel as usize).try_into().unwrap(),
0,
)
.to_result()
}

// Register a listener to be called when the ADC conversion is finished
Expand All @@ -47,7 +53,7 @@ impl<S: Syscalls> Adc<S> {

/// Initiates a synchronous ADC conversion
/// Returns the converted ADC value or an error
pub fn read_single_sample_sync(channel: u32) -> Result<u16, ErrorCode> {
pub fn read_single_sample_sync(channel: usize) -> Result<u16, ErrorCode> {
let sample: Cell<Option<u16>> = Cell::new(None);
let listener = ADCListener(|adc_val| {
sample.set(Some(adc_val));
Expand All @@ -67,13 +73,25 @@ impl<S: Syscalls> Adc<S> {
}

/// Returns the number of ADC resolution bits
pub fn get_resolution_bits(channel: size) -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, GET_RES_BITS, channel, 0).to_result()
pub fn get_resolution_bits(channel: usize) -> Result<u32, ErrorCode> {
S::command(
DRIVER_NUM,
GET_RES_BITS,
(channel as usize).try_into().unwrap(),
0,
)
.to_result()
}

/// Returns the reference voltage in millivolts (mV)
pub fn get_reference_voltage_mv(channel: u32) -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, GET_VOLTAGE_REF, channel, 0).to_result()
pub fn get_reference_voltage_mv(channel: usize) -> Result<u32, ErrorCode> {
S::command(
DRIVER_NUM,
GET_VOLTAGE_REF,
(channel as usize).try_into().unwrap(),
0,
)
.to_result()
}
}

Expand Down

0 comments on commit 6e4199e

Please sign in to comment.