Files
templates/tools/dsview/decoders/pwm_waveform/pd.py
T

94 lines
3.8 KiB
Python

"""PWM envelope on up to three independent logic channels."""
import sigrokdecode as srd
from common.setgui_decoders.pwm_waveform import PwmChannel
class Decoder(srd.Decoder):
api_version = 3
id = 'pwm_waveform'
name = 'PWM waveform'
longname = 'PWM waveform / sine reconstruction'
desc = 'Reconstructs normalized waveforms from PWM duty on 1-3 channels.'
license = 'gplv2+'
inputs = ['logic']
outputs = []
tags = ['Clock/timing', 'Util']
channels = ({'id': 'a', 'name': 'PWM A', 'desc': 'First PWM input'},)
optional_channels = (
{'id': 'b', 'name': 'PWM B', 'desc': 'Second PWM input'},
{'id': 'c', 'name': 'PWM C', 'desc': 'Third PWM input'},
)
options = tuple(
{'id': 'active_' + ch, 'desc': 'PWM ' + ch.upper() + ' active level',
'default': 'high', 'values': ('high', 'low')} for ch in 'abc'
) + (
{'id': 'carrier_hz', 'desc': 'Carrier Hz (0 = edge-to-edge)', 'default': 0},
{'id': 'smoothing_ms', 'desc': 'Low-pass time constant (ms, 0 = off)', 'default': 0.0},
)
annotations = (
('wave-a', 'Wave A'), ('wave-b', 'Wave B'), ('wave-c', 'Wave C'),
('duty-a', 'Duty A'), ('duty-b', 'Duty B'), ('duty-c', 'Duty C'),
)
annotation_rows = (
('wave_a', 'A: -1 .. +1', (0,)),
('wave_b', 'B: -1 .. +1', (1,)),
('wave_c', 'C: -1 .. +1', (2,)),
('duty_a', 'A duty / frequency', (3,)),
('duty_b', 'B duty / frequency', (4,)),
('duty_c', 'C duty / frequency', (5,)),
)
def __init__(self):
self.reset()
def reset(self):
self.samplerate = None
self.channels_state = {}
def metadata(self, key, value):
if key == srd.SRD_CONF_SAMPLERATE:
self.samplerate = value
def start(self):
self.out_ann = self.register(srd.OUTPUT_ANN)
def _update(self, pins):
for ch, state in self.channels_state.items():
for event in state.update(self.samplenum, pins[ch]):
label = 'D={:.3f}% {}'.format(event['duty'] * 100,
('window ' if state.window else 'f=') + '{:.3f} Hz'.format(event['frequency']))
self.put(event['start'], event['end'], self.out_ann,
[3 + ch, [label, '{:.2f}%'.format(event['duty'] * 100)]])
previous = event['previous']
if previous is not None:
# Last label is the versioned native renderer contract.
self.put(previous[0], event['midpoint'], self.out_ann, [ch, [
'PWM {}: {:+.6f}'.format('ABC'[ch], event['value']),
'PWM1 {:.9f} {:.9f}'.format(previous[1], event['value'])]])
def decode(self):
if not self.samplerate:
raise ValueError('Cannot decode without samplerate.')
self.channels_state = {ch: PwmChannel(self.samplerate,
self.options['active_' + 'abc'[ch]] == 'high',
self.options['carrier_hz'], self.options['smoothing_ms'])
for ch in range(3) if self.has_channel(ch)}
pins = list(self.wait({}))
self._update(pins)
while True:
conditions = [{ch: 'e'} for ch in self.channels_state]
boundaries = [s.next_boundary() for s in self.channels_state.values()
if s.next_boundary() is not None]
if boundaries:
conditions.append({'skip': max(1, min(boundaries) - self.samplenum)})
pins = list(self.wait(conditions))
self._update(pins)
def end(self):
# Complete fixed windows only; never infer a partial edge-based period.
if self.channels_state:
pins = [0, 0, 0]
for ch, state in self.channels_state.items():
pins[ch] = state.level == state.active_high
self._update(pins)