Moved some constants and changed OutputChannel to literal[1, 2]

This commit is contained in:
Ali Nwegy 2024-02-01 18:37:36 +01:00
parent ee5d585f01
commit d9033ff9e5
4 changed files with 61 additions and 45 deletions

3
.gitignore vendored
View File

@ -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-*

View File

@ -23,3 +23,6 @@ license = {text = "MIT"}
[tool.pdm]
distribution = true
[tool.pdm.scripts]
edit = {shell = "$VISUAL src/"}

View File

@ -0,0 +1,2 @@
ON = True
OFF = False

View File

@ -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")