From d9033ff9e57a2035cd5ab7d27ac53c05a32eb5c1 Mon Sep 17 00:00:00 2001 From: Ali Nwegy Date: Thu, 1 Feb 2024 18:37:36 +0100 Subject: [PATCH] Moved some constants and changed OutputChannel to literal[1, 2] --- .gitignore | 3 + pyproject.toml | 3 + src/fn_gen/constants/__init__.py | 2 + src/fn_gen/dg2052.py | 98 +++++++++++++++++--------------- 4 files changed, 61 insertions(+), 45 deletions(-) create mode 100644 src/fn_gen/constants/__init__.py diff --git a/.gitignore b/.gitignore index cefa04c..3b5798c 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,6 @@ rigol_dg2052_python_library/ rigol_dg2052_python_library/** rigol_dg2052_python_library.egg-info/ rigol_dg2052_python_library.egg-info/** + +# Syncthing +*.sync-conflict-* diff --git a/pyproject.toml b/pyproject.toml index 1185b91..5cafbc4 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -23,3 +23,6 @@ license = {text = "MIT"} [tool.pdm] distribution = true + +[tool.pdm.scripts] +edit = {shell = "$VISUAL src/"} diff --git a/src/fn_gen/constants/__init__.py b/src/fn_gen/constants/__init__.py new file mode 100644 index 0000000..b04c98b --- /dev/null +++ b/src/fn_gen/constants/__init__.py @@ -0,0 +1,2 @@ +ON = True +OFF = False diff --git a/src/fn_gen/dg2052.py b/src/fn_gen/dg2052.py index f87f9fa..93ed8bd 100644 --- a/src/fn_gen/dg2052.py +++ b/src/fn_gen/dg2052.py @@ -1,13 +1,11 @@ ### PLEASE DO NOT MIND THE FORMATTING, IT IS DONE AUTOMATICALLY BY 'BLACK' THE PYTHON FORMATTER - -from re import DEBUG -import time import logging +from typing import Literal import pyvisa -from .errors import * -from .enums import * -from .common import * -from .constants.dg2052 import * +from .common import check_bounds +from .constants.dg2052 import (SIN_RANGE, SQU_RANGE, RAMP_RANGE) +from .enums import CommMethod, SweepSpacing, SweepTriggerSlope, SweepTriggerSource, SweepSignalType +from .errors import UndefinedValueError, UndefinedCommunicationMethodError, ValueOutOfBoundsError class DG2052(pyvisa.resources.MessageBasedResource): @@ -115,7 +113,7 @@ class DG2052(pyvisa.resources.MessageBasedResource): case _: # default case raise Undefined Communication Method Error raise UndefinedCommunicationMethodError(self.port) - def set_output(self, channel: OutputChannel, state: bool): + def set_output(self, channel: Literal[1, 2], state: bool): """ Sets the output channel ON or OFF @@ -128,15 +126,15 @@ class DG2052(pyvisa.resources.MessageBasedResource): The state of the output channel """ if state: - logging.debug(f"(PROG) :OUTP{channel.value} ON") - self.write(f":OUTP{channel.value} ON") + logging.debug(f"(PROG) :OUTP{channel} ON") + self.write(f":OUTP{channel} ON") else: - logging.debug(f"(PROG) :OUTP{channel.value} OFF") - self.write(f":OUTP{channel.value} OFF") + logging.debug(f"(PROG) :OUTP{channel} OFF") + self.write(f":OUTP{channel} OFF") - def toggle_output(self, channel: OutputChannel): - state = self.query(f":OUT{channel.value}?").strip() - logging.debug(f"(PROG) output {channel.value} state: {state}") + def toggle_output(self, channel: Literal[1, 2]): + state = self.query(f":OUT{channel}?").strip() + logging.debug(f"(PROG) output {channel} state: {state}") match (state): case "ON": self.set_output(channel, False) @@ -145,39 +143,49 @@ class DG2052(pyvisa.resources.MessageBasedResource): case _: raise UndefinedValueError(state, "ON or OFF") - def get_output_volt_limits(self, channel: OutputChannel) -> tuple[float, float]: - low: float = float(self.query(f":OUTP{channel.value}:VOLL:LOW?")) - high: float = float(self.query(f":OUTP{channel.value}:VOLL:HIGH?")) - logging.debug(f"(PROG) output {channel.value} limits: {low}, {high}") + def get_output_volt_limits(self, channel: Literal[1, 2]) -> tuple[float, float]: + low: float = float(self.query(f":OUTP{channel}:VOLL:LOW?")) + high: float = float(self.query(f":OUTP{channel}:VOLL:HIGH?")) + logging.debug(f"(PROG) output {channel} limits: {low}, {high}") return low, high - def get_output_impedance(self, channel: OutputChannel) -> float: - impedance = float(self.query(f":OUTP{channel.value}:IMP?")) - logging.debug(f"(PROG) output {channel.value} impedance: {impedance}") + def get_output_impedance(self, channel: Literal[1, 2]) -> float: + impedance = float(self.query(f":OUTP{channel}:IMP?")) + logging.debug(f"(PROG) output {channel} impedance: {impedance}") return impedance - def get_output_load(self, channel: OutputChannel) -> float: - load = float(self.query(f":OUTP{channel.value}:LOAD?")) - logging.debug(f"(PROG) output {channel.value} load: {load}") + def get_output_load(self, channel: Literal[1, 2]) -> float: + load = float(self.query(f":OUTP{channel}:LOAD?")) + logging.debug(f"(PROG) output {channel} load: {load}") return load - def get_output_signal(self, channel: OutputChannel) -> str: - signal = self.query(f":SOUR{channel.value}:APPL?").strip() - logging.debug(f"(PROG) output {channel.value} signal: {signal}") + def get_output_signal(self, channel: Literal[1, 2]) -> str: + signal = self.query(f":SOUR{channel}:APPL?").strip() + logging.debug(f"(PROG) output {channel} signal: {signal}") return signal - def get_output_state(self, channel: OutputChannel) -> str: - state = self.query(f":OUTP{channel.value}?").strip() - logging.debug(f"(PROG) output {channel.value} state: {state}") + def get_output_state(self, channel: Literal[1, 2]) -> str: + state = self.query(f":OUTP{channel}?").strip() + logging.debug(f"(PROG) output {channel} state: {state}") return state - def set_dc(self, channel: OutputChannel, offset: float): + def is_output_on(self, channel: Literal[1, 2]) -> bool: + channel_state = self.get_output_state(channel) + match channel_state: + case "ON": + return True + case "OFF": + return False + case _: + raise UndefinedValueError(channel_state, "ON or OFF") + + def set_dc(self, channel: Literal[1, 2], offset: float): logging.debug(f"(PROG) set dc signal with offset: {offset}") - self.write(f":SOUR{channel.value}:APPL:DC 1,1,{offset}") + self.write(f":SOUR{channel}:APPL:DC 1,1,{offset}") def set_sine_wave( self, - channel: OutputChannel, + channel: Literal[1, 2], freq: float = 1e3, amp: float = 5.0, offset: float = 0.0, @@ -190,11 +198,11 @@ class DG2052(pyvisa.resources.MessageBasedResource): logging.debug( f"(PROG) set sine signal with freq: {freq}, amp: {amp}, offset: {offset}, phase: {phase}" ) - self.write(f":SOUR{channel.value}:APPL:SIN {freq},{amp},{offset},{phase}") + self.write(f":SOUR{channel}:APPL:SIN {freq},{amp},{offset},{phase}") def set_square_wave( self, - channel: OutputChannel, # Sets the output channel of the ramp function + channel: Literal[1, 2], # Sets the output channel of the ramp function freq: float = 1e3, # Sets the frequency amp: float = 5.0, # Sets the amplitude offset: float = 0.0, # Sets the amplitude offset @@ -205,11 +213,11 @@ class DG2052(pyvisa.resources.MessageBasedResource): logging.debug( f"(PROG) set square signal with freq: {freq}, amp: {amp}, offset: {offset}, phase: {phase}" ) - self.write(f":SOUR{channel.value}:APPL:SQU {freq},{amp},{offset},{phase}") + self.write(f":SOUR{channel}:APPL:SQU {freq},{amp},{offset},{phase}") def set_ramp( self, - channel: OutputChannel, # Sets the output channel of the ramp function + channel: Literal[1, 2], # Sets the output channel of the ramp function freq: float = 1e3, # Sets the frequency amp: float = 5, # Sets the amplitude offset: float = 0, # Sets the amplitude offset @@ -220,11 +228,11 @@ class DG2052(pyvisa.resources.MessageBasedResource): logging.debug( f"(PROG) set ramp signal with freq: {freq}, amp: {amp}, offset: {offset}, phase: {phase}" ) - self.write(f":SOUR{channel.value}:APPL:RAMP {freq},{amp},{offset},{phase}") + self.write(f":SOUR{channel}:APPL:RAMP {freq},{amp},{offset},{phase}") def set_sweep( self, - channel: OutputChannel, # Sets the output channel of the sweep function + channel: Literal[1, 2], # Sets the output channel of the sweep function amp: float = 5, # Sets the amplitude of the sweeped signal offset: float = 0, # Sets the offset voltage of the sweeped signal phase: int = 0, # Sets the phase shift of the sweeped signal @@ -243,7 +251,7 @@ class DG2052(pyvisa.resources.MessageBasedResource): trigger_source: SweepTriggerSource = SweepTriggerSource.INTERNAL, # Sets the sweep trigger source ): time_bounds: tuple[float, float] = (0, 500) - command_header = f":SOUR{channel.value}:SWE" + command_header = f":SOUR{channel}:SWE" check_bounds(time_bounds, htime_start) check_bounds(time_bounds, htime_stop) check_bounds(time_bounds, rtime) @@ -259,10 +267,10 @@ class DG2052(pyvisa.resources.MessageBasedResource): self.write(f":SOUR:FREQ:STAR {freq_start}") self.write(f":SOUR:FREQ:STOP {freq_stop}") if marker: - self.write(f":SOUR:MARK ON") + self.write(":SOUR:MARK ON") self.write(f":SOUR:MARK:FREQ {freq_marker}") else: - self.write(f":SOUR:MARK OFF") + self.write(":SOUR:MARK OFF") self.write(f"{command_header}:SPAC {spacing}") self.write(f"{command_header}:STEP {step}") match trigger_source: @@ -291,5 +299,5 @@ class DG2052(pyvisa.resources.MessageBasedResource): ) self.write(f"{command_header}:STAT ON") - def trigger_sweep(self, channel: OutputChannel): - self.write(f":SOUR{channel.value}:SWE:TRIG:IMM") + def trigger_sweep(self, channel: Literal[1, 2]): + self.write(f":SOUR{channel}:SWE:TRIG:IMM")