Skip to content

Commit

Permalink
update driver to handle incoming data from nScope v2
Browse files Browse the repository at this point in the history
  • Loading branch information
davidjmeyer committed Jan 20, 2024
1 parent 4310761 commit ec3199d
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 81 deletions.
10 changes: 5 additions & 5 deletions src/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,10 @@ impl Nscope {
a2: AnalogOutput::create(command_tx.clone(), 1),
p1: PulseOutput::create(command_tx.clone(), 0),
p2: PulseOutput::create(command_tx.clone(), 1),
ch1: AnalogInput::default(),
ch2: AnalogInput::default(),
ch3: AnalogInput::default(),
ch4: AnalogInput::default(),
ch1: AnalogInput::create(is_legacy),
ch2: AnalogInput::create(is_legacy),
ch3: AnalogInput::create(is_legacy),
ch4: AnalogInput::create(is_legacy),
fw_version,
power_status,
command_tx,
Expand All @@ -129,7 +129,7 @@ impl Nscope {
return Ok(scope);
}
// Wait for the response from the backend
if let Ok(_) = init_rx.recv_timeout(Duration::from_secs(5)) {
if init_rx.recv_timeout(Duration::from_secs(5)).is_ok() {
return Ok(scope);
}
}
Expand Down
79 changes: 70 additions & 9 deletions src/scope/analog_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,49 @@
*
**************************************************************************************************/

mod voltages_legacy;
mod voltages;

use voltages_legacy::AnalogInterfaceLegacy;
use voltages::AnalogInterfaceModern;

#[derive(Debug, Copy, Clone)]
enum AnalogInterface {
Legacy(AnalogInterfaceLegacy),
Modern(AnalogInterfaceModern)
}

/// Interface to a single scope channel
#[derive(Debug, Copy, Clone)]
pub struct AnalogInput {
pub(crate) is_on: bool,
pub(crate) gain_setting: u8,
pub(crate) offset_setting: u8,
analog_interface: AnalogInterface,
}

impl Default for AnalogInput {
fn default() -> Self {
let mut analog_input = AnalogInput {
is_on: true,
gain_setting: 0,
offset_setting: 0
impl AnalogInput {
pub(crate) fn create(is_legacy: bool) -> Self {
let mut analog_input = match is_legacy {
true => AnalogInput {
is_on: true,
analog_interface: AnalogInterface::Legacy(
AnalogInterfaceLegacy {
gain_setting: 0,
offset_setting: 0,
}),
},
false => AnalogInput {
is_on: true,
analog_interface: AnalogInterface::Modern(
AnalogInterfaceModern {
}),
}
};
analog_input.set_range(-5.0, 5.0);
analog_input
}
}

impl AnalogInput {

pub fn is_on(&self) -> bool {
self.is_on
}
Expand All @@ -43,5 +62,47 @@ impl AnalogInput {
pub fn turn_off(&mut self) {
self.is_on = false;
}

pub fn set_range(&mut self, vmin: f64, vmax: f64) {
match self.analog_interface {
AnalogInterface::Legacy(mut interface) => { interface.set_range(vmin, vmax) }
AnalogInterface::Modern(mut interface) => { interface.set_range(vmin, vmax) }
}
}

pub fn gain(&self) -> f64 {
match self.analog_interface {
AnalogInterface::Legacy(interface) => { interface.gain() }
AnalogInterface::Modern(interface) => { interface.gain() }
}
}

pub(crate) fn measurement_from_voltage(&self, voltage: f64) -> i16 {
match self.analog_interface {
AnalogInterface::Legacy(interface) => { interface.measurement_from_voltage(voltage) }
AnalogInterface::Modern(interface) => { interface.measurement_from_voltage(voltage) }
}
}

pub(crate) fn voltage_from_measurement(&self, adc_data: u16) -> f64 {
match self.analog_interface {
AnalogInterface::Legacy(interface) => { interface.voltage_from_measurement(adc_data) }
AnalogInterface::Modern(interface) => { interface.voltage_from_measurement(adc_data) }
}
}

pub(crate) fn gain_cmd(&self) -> u8 {
match self.analog_interface {
AnalogInterface::Legacy(interface) => { interface.gain_setting }
AnalogInterface::Modern(_interface) => { 0 }
}
}

pub(crate) fn offset_cmd(&self) -> u8 {
match self.analog_interface {
AnalogInterface::Legacy(interface) => { interface.offset_setting }
AnalogInterface::Modern(_interface) => { 0 }
}
}
}

63 changes: 18 additions & 45 deletions src/scope/analog_input/voltages.rs
Original file line number Diff line number Diff line change
@@ -1,63 +1,36 @@
const DELTA1: f64 = 1.65;
const DELTA2: f64 = 3.3 / 10.0;

const ALPHA1: f64 = 50.0 / 5000.0;
const ALPHA2: f64 = 20.0 / 256.0;
#[derive(Debug, Copy, Clone)]
pub(super) struct AnalogInterfaceModern {
}

const BETA1: f64 = 3.3 * 50.0 / 62.0 / 5000.0;
const BETA2: f64 = 3.3 * 20.0 / 62.0 / 256.0;
impl AnalogInterfaceModern {
pub(super) fn set_level(&mut self, _level: f64) {

impl super::AnalogInput {
fn set_level(&mut self, level: f64) {
let ch_gain = self.gain_setting as f64;
let gain = 1.0 + ALPHA1 + ALPHA2 * ch_gain;
if gain < 1.1 {
self.offset_setting = 31;
return;
}
let desired_level_setting = (level * DELTA2 * gain + DELTA1 * (gain - 1.0)) / (BETA1 + BETA2 * ch_gain);
self.offset_setting = (desired_level_setting + 0.5) as u8
}

fn set_gain(&mut self, gain: f64) {
let desired_gain_setting = (gain - 1.0 - ALPHA1) / ALPHA2;
self.gain_setting = desired_gain_setting as u8;
}
pub(super) fn set_gain(&mut self, _gain: f64) {

pub fn gain(&self) -> f64 {
self.gain_setting as f64 * ALPHA2 + ALPHA1 + 1.0
}
}
impl super::AnalogInput {
pub(crate) fn measurement_from_voltage(&self, voltage: f64) -> i16 {
let ch_gain = self.gain_setting as f64;
let ch_level = self.offset_setting as f64;

let gain = 1.0 + ALPHA1 + ALPHA2 * ch_gain;
let level = (ch_level * (BETA1 + BETA2 * ch_gain) - DELTA1 * (gain - 1.0)) / DELTA2 / gain;

((voltage - level) * gain / 10.0 * 4095.0 + 2047.0) as i16
pub(super) fn gain(&self) -> f64 {
1.0
}
}

impl super::AnalogInput {
pub(crate) fn voltage_from_measurement(&self, adc_data: u16) -> f64 {
let adc_reading = adc_data as f64;
let ch_gain = self.gain_setting as f64;
let ch_level = self.offset_setting as f64;
pub(super) fn measurement_from_voltage(&self, _voltage: f64) -> i16 {
0i16
}

let gain = 1.0 + ALPHA1 + ALPHA2 * ch_gain;
let level = (ch_level * (BETA1 + BETA2 * ch_gain) - DELTA1 * (gain - 1.0)) / DELTA2 / gain;
pub(super) fn voltage_from_measurement(&self, adc_data: u16) -> f64 {
let gain = 1.0f64;
let v_offset = 0.0f64;

10.0 / gain * (adc_reading - 2047.0) / 4095.0 + level
let adc_voltage = adc_data as f64 * 2.5 / 4095.0;
((adc_voltage / gain + v_offset * (gain - 1.0) / (gain)) - 1.25) * 10.0 / 2.5
}
}


impl super::AnalogInput {
pub fn set_range(&mut self, vmin: f64, vmax: f64) {
pub(super) fn set_range(&mut self, vmin: f64, vmax: f64) {
let level = (vmax + vmin) / 2.0;
let gain = 10.0 / (vmax - vmin);
let gain = 10.0 / (vmax - vmin);

self.set_gain(gain);
self.set_level(level);
Expand Down
65 changes: 65 additions & 0 deletions src/scope/analog_input/voltages_legacy.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
const DELTA1: f64 = 1.65;
const DELTA2: f64 = 3.3 / 10.0;

const ALPHA1: f64 = 50.0 / 5000.0;
const ALPHA2: f64 = 20.0 / 256.0;

const BETA1: f64 = 3.3 * 50.0 / 62.0 / 5000.0;
const BETA2: f64 = 3.3 * 20.0 / 62.0 / 256.0;

#[derive(Debug, Copy, Clone)]
pub(super) struct AnalogInterfaceLegacy {
pub(super) gain_setting: u8,
pub(super) offset_setting: u8,
}

impl AnalogInterfaceLegacy {
pub(super) fn set_level(&mut self, level: f64) {
let ch_gain = self.gain_setting as f64;
let gain = 1.0 + ALPHA1 + ALPHA2 * ch_gain;
if gain < 1.1 {
self.offset_setting = 31;
return;
}
let desired_level_setting = (level * DELTA2 * gain + DELTA1 * (gain - 1.0)) / (BETA1 + BETA2 * ch_gain);
self.offset_setting = (desired_level_setting + 0.5) as u8
}

pub(super) fn set_gain(&mut self, gain: f64) {
let desired_gain_setting = (gain - 1.0 - ALPHA1) / ALPHA2;
self.gain_setting = desired_gain_setting as u8;
}

pub(super) fn gain(&self) -> f64 {
self.gain_setting as f64 * ALPHA2 + ALPHA1 + 1.0
}

pub(super) fn measurement_from_voltage(&self, voltage: f64) -> i16 {
let ch_gain = self.gain_setting as f64;
let ch_level = self.offset_setting as f64;

let gain = 1.0 + ALPHA1 + ALPHA2 * ch_gain;
let level = (ch_level * (BETA1 + BETA2 * ch_gain) - DELTA1 * (gain - 1.0)) / DELTA2 / gain;

((voltage - level) * gain / 10.0 * 4095.0 + 2047.0) as i16
}

pub(super) fn voltage_from_measurement(&self, adc_data: u16) -> f64 {
let adc_reading = adc_data as f64;
let ch_gain = self.gain_setting as f64;
let ch_level = self.offset_setting as f64;

let gain = 1.0 + ALPHA1 + ALPHA2 * ch_gain;
let level = (ch_level * (BETA1 + BETA2 * ch_gain) - DELTA1 * (gain - 1.0)) / DELTA2 / gain;

10.0 / gain * (adc_reading - 2047.0) / 4095.0 + level
}

pub(super) fn set_range(&mut self, vmin: f64, vmax: f64) {
let level = (vmax + vmin) / 2.0;
let gain = 10.0 / (vmax - vmin);

self.set_gain(gain);
self.set_level(level);
}
}
2 changes: 1 addition & 1 deletion src/scope/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ impl Command {
Command::SetAnalogOutput(cmd) => { cmd.handle_rx(buffer) }
Command::SetPulseOutput(cmd) => { cmd.handle_rx(buffer) }
Command::RequestData(cmd) => { cmd.handle_rx(buffer) }
Command::StopData => {}
Command::StopData => { }
}
}

Expand Down
Loading

0 comments on commit ec3199d

Please sign in to comment.