|
| 1 | +""" |
| 2 | +DWF Python Example |
| 3 | +
|
| 4 | +This file is part of dwfpy: https://github.com/mariusgreuel/dwfpy |
| 5 | +""" |
| 6 | + |
| 7 | +import dwfpy as dwf |
| 8 | + |
| 9 | +print(f'DWF Version: {dwf.Application.get_version()}') |
| 10 | + |
| 11 | +FILENAME = 'temp_recording.bin' |
| 12 | +SAMPLE_RATE = 1e6 |
| 13 | +SAMPLE_COUNT = 1e6 |
| 14 | + |
| 15 | +with dwf.Device() as device: |
| 16 | + print(f'Found device: {device.name} ({device.serial_number})') |
| 17 | + |
| 18 | + logic = device.digital_input |
| 19 | + pattern = device.digital_output |
| 20 | + |
| 21 | + # setup a binary counter |
| 22 | + counter = round(pattern.clock_frequency / SAMPLE_RATE) |
| 23 | + |
| 24 | + for i in range(16): |
| 25 | + pattern.channels[i].setup(divider=1 << i, low_counter=counter, high_counter=counter) |
| 26 | + |
| 27 | + pattern.setup(start=True) |
| 28 | + |
| 29 | + # for Digital Discovery bit order: DIO24:39; with 32 bit sampling [DIO24:39 + DIN0:15] |
| 30 | + logic.dio_first = True |
| 31 | + |
| 32 | + print(f"Creating '{FILENAME}'...") |
| 33 | + with open(FILENAME, 'wb') as bin_file: |
| 34 | + |
| 35 | + def callback(recorder: dwf.DigitalRecorder) -> bool: |
| 36 | + """Recorder callback""" |
| 37 | + if recorder.lost_samples > 0: |
| 38 | + raise RuntimeError('Samples lost, reduce sample rate.') |
| 39 | + if recorder.corrupted_samples > 0: |
| 40 | + raise RuntimeError('Samples corrupted, reduce sample rate.') |
| 41 | + |
| 42 | + bin_file.write(recorder.data_samples) |
| 43 | + |
| 44 | + if recorder.total_samples < SAMPLE_COUNT: |
| 45 | + return True |
| 46 | + |
| 47 | + print( |
| 48 | + f'Status: {recorder.status}, ' |
| 49 | + f'lost={recorder.lost_samples}, ' |
| 50 | + f'corrupted={recorder.corrupted_samples}, ' |
| 51 | + f'total={recorder.total_samples}' |
| 52 | + ) |
| 53 | + |
| 54 | + return False |
| 55 | + |
| 56 | + print(f'Streaming data for {SAMPLE_COUNT / SAMPLE_RATE}s...') |
| 57 | + logic.stream( |
| 58 | + callback=callback, sample_rate=SAMPLE_RATE, sample_format=16, configure=True, start=True |
| 59 | + ) |
0 commit comments