OpenHantek
Loading...
Searching...
No Matches
hantekdsocontrol.h
Go to the documentation of this file.
1// SPDX-License-Identifier: GPL-2.0-or-later
2
3#pragma once
4
5#ifdef Q_OS_WIN
6#ifndef NOMINMAX
7#define NOMINMAX 1 // disable windows.h min/max global methods
8#endif
9#endif
10
11#include <limits>
12
13#include "controlsettings.h"
15#include "dsosamples.h"
16#include "errorcodes.h"
17#include "mathchannel.h"
18#include "scopesettings.h"
19#include "triggering.h"
20
23
24#include "dsomodel.h"
25
26#include <vector>
27
28#include <QSettings>
29#include <QThread>
30
31class CapturingThread;
32class ScopeDevice;
33
34struct Raw {
35 unsigned channels = 0;
36 double samplerate = 0;
37 unsigned oversampling = 0;
38 unsigned gainValue[ 2 ] = { 1, 1 }; // 1,2,5,10,..
39 unsigned gainIndex[ 2 ] = { 7, 7 }; // index 0..7
40 unsigned tag = 0;
41 bool freeRun = false; // small buffer, no trigger
42 bool valid = false; // samples can be processed
43 bool rollMode = false; // one complete buffer received, start to roll
44 unsigned size = 0;
45 unsigned received = 0;
46 std::vector< unsigned char > data;
47 mutable QReadWriteLock lock;
48};
49
50
53class HantekDsoControl : public QObject {
54 Q_OBJECT
56
57 public:
67
69 ~HantekDsoControl() override;
70
74 void stateMachine();
75
77
78 double getSamplerate() const { return controlsettings.samplerate.current; }
79
80 unsigned getSamplesize() const {
81 if ( controlsettings.trigger.mode == Dso::TriggerMode::ROLL )
82 return SAMPLESIZE_ROLL;
83 else
84 return SAMPLESIZE;
85 }
86
87 bool isSamplingUI() const { return samplingUI; }
88
90 const ScopeDevice *getDevice() const { return scopeDevice; }
91
93 const DSOModel *getModel() const { return model; }
94
95
105 Dso::ErrorCode stringCommand( const QString &commandString );
106
107 void addCommand( ControlCommand *newCommand, bool pending = true );
108
109 template < class T > T *modifyCommand( Hantek::ControlCode code ) {
110 control[ uint8_t( code ) ]->pending = true;
111 return static_cast< T * >( control[ uint8_t( code ) ] );
112 }
113
114 bool hasCommand( Hantek::ControlCode code ) { return ( control[ uint8_t( code ) ] != nullptr ); }
115
116 const ControlCommand *getCommand( Hantek::ControlCode code ) const { return control[ uint8_t( code ) ]; }
117
119 void quitSampling();
120
122 void prepareForShutdown();
123
124 private:
125 std::unique_ptr< MathChannel > mathChannel;
126 std::unique_ptr< Triggering > triggering;
127 bool singleChannel = false;
129 void setSingleChannel( bool single ) { singleChannel = single; }
130 bool isSingleChannel() const { return singleChannel; }
131 bool triggerModeNONE() { return controlsettings.trigger.mode == Dso::TriggerMode::ROLL; }
132 unsigned getRecordLength() const;
133 void setDownsampling( unsigned downsampling ) { downsamplingNumber = downsampling; }
137 Dso::ErrorCode updateCalibrationValues( bool useEEPROM = false );
139
143 unsigned getSampleCount() const { return isSingleChannel() ? getRecordLength() : getRecordLength() * specification->channels; }
144
146 unsigned grossSampleCount( unsigned net ) const { return ( ( net + 1024 ) / 1024 + 2 ) * 1024; }
147
149 unsigned netSampleCount( unsigned gross ) const { return ( ( gross - 1024 ) / 1000 - 1 ) * 1000; }
150
151 void updateInterval();
152
155
157 void restoreTargets();
158
161
162 void controlSetSamplerate( uint8_t sampleIndex );
163
165 ControlCommand *control[ 255 ] = { nullptr };
167
168 // Communication with device
170 bool deviceNotConnected();
171 bool samplingUI = false;
172
173 // Device setup
177 const DsoSettingsScope *scope = nullptr;
178
179 // Results
180 unsigned downsamplingNumber = 1;
182 unsigned expectedSampleCount = 0;
185 std::unique_ptr< QSettings > calibrationSettings;
188 bool capturing = false;
189 bool samplingStarted = false;
193 unsigned activeChannels = 2;
194 bool refresh = false; // parameter changed -> new raw to result conversion and trigger search needed
195 void requestRefresh( bool active = true ) { refresh = active; }
197 bool changed = refresh;
198 refresh = false;
199 return changed;
200 }
202 unsigned debugLevel = 0;
203 uint8_t channelOffset[ 2 ] = { 0x80, 0x80 };
204
205#define dprintf( level, fmt, ... ) \
206 do { \
207 if ( debugLevel & level ) \
208 fprintf( stderr, fmt, __VA_ARGS__ ); \
209 } while ( 0 )
210
211 public slots:
215 void enableSamplingUI( bool enabled = true );
216
221 Dso::ErrorCode setSamplerate( double samplerate = 0.0 );
222
227 Dso::ErrorCode setRecordTime( double duration = 0.0 );
228
233 Dso::ErrorCode setChannelUsed( ChannelID channel, bool used );
234
239 Dso::ErrorCode setChannelInverted( ChannelID channel, bool inverted );
240
246 Dso::ErrorCode setProbe( ChannelID channel, double probeAttn );
247
252 Dso::ErrorCode setGain( ChannelID channel, double gain );
253
259
263
267 Dso::ErrorCode setTriggerSource( int channel );
268
272 Dso::ErrorCode setTriggerSmooth( int smooth );
273
278 Dso::ErrorCode setTriggerLevel( ChannelID channel, double level );
279
284
288 Dso::ErrorCode setTriggerPosition( double position );
289
293 Dso::ErrorCode setCalFreq( double calfreq = 0.0 );
294
298
300 void restartSampling();
301
303 void calibrateOffset( bool enable );
304
305 signals:
306 void showSamplingStatus( bool enabled );
307 void statusMessage( const QString &message, int timeout );
308 void samplesAvailable( const DSOsamples *samples );
309
311 void samplerateLimitsChanged( double minimum, double maximum );
313 void samplerateSet( int mode, QList< double > sampleSteps );
314
315 void samplerateCalculated( double samplerate, unsigned oversampling );
316
317 void communicationError() const;
318
319 void liveCalibrationError() const; // live calibration stopped due to noise or big offset
320};
321
322Q_DECLARE_METATYPE( DSOsamples * )
Definition capturing.h:7
Definition controlcommand.h:12
Describes a device This is the central class to describe a hantek compatible DSO. It contains all usb...
Definition dsomodel.h:17
ControlCommand * firstControlCommand
Definition hantekdsocontrol.h:166
bool isSamplingUI() const
Definition hantekdsocontrol.h:87
bool hasCommand(Hantek::ControlCode code)
Definition hantekdsocontrol.h:114
void showSamplingStatus(bool enabled)
The oscilloscope started/stopped sampling/waiting for trigger.
unsigned downsamplingNumber
Number of downsamples to reduce sample rate.
Definition hantekdsocontrol.h:180
void samplerateSet(int mode, QList< double > sampleSteps)
The available samplerate for fixed samplerate devices has changed.
void samplerateLimitsChanged(double minimum, double maximum)
The available samplerate range has changed.
Dso::ErrorCode setGain(ChannelID channel, double gain)
Sets the probe gain for the given channel.
Definition hantekdsocontrol.cpp:250
unsigned expectedSampleCount
Definition hantekdsocontrol.h:182
DSOsamples result
Definition hantekdsocontrol.h:181
bool refresh
Definition hantekdsocontrol.h:194
bool isSingleChannel() const
Definition hantekdsocontrol.h:130
bool refreshNeeded()
Definition hantekdsocontrol.h:196
Dso::ErrorCode writeCalibrationToEEPROM()
Definition hantekdsocontrol.cpp:617
Dso::ErrorCode stringCommand(const QString &commandString)
Sends control commands directly.
Definition hantekdsocontrol.cpp:957
bool samplingStarted
Definition hantekdsocontrol.h:189
const Dso::ControlSpecification * specification
The specifications of the device.
Definition hantekdsocontrol.h:175
void prepareForShutdown()
Saves calibration settings e.g. to the scope's EEPROM.
Definition hantekdsocontrol.cpp:50
void statusMessage(const QString &message, int timeout)
Status message about the oscilloscope.
bool deviceNotConnected()
USB status, always false for demo device.
Definition hantekdsocontrol.cpp:58
HantekDsoControl(ScopeDevice *scopeDevice, const DSOModel *model, int verboseLevel)
Definition hantekdsocontrol.cpp:19
Dso::ErrorCode updateCalibrationValues(bool useEEPROM=false)
Definition hantekdsocontrol.cpp:516
void stateMachine()
State machine for the device communication.
Definition hantekdsocontrol.cpp:862
void restoreTargets()
Restore the samplerate/timebase targets after divider updates.
Definition hantekdsocontrol.cpp:61
uint8_t channelOffset[2]
Definition hantekdsocontrol.h:203
Dso::ErrorCode getCalibrationFromEEPROM()
Definition hantekdsocontrol.cpp:564
void stopStateMachine()
Definition hantekdsocontrol.h:76
unsigned activeChannels
Definition hantekdsocontrol.h:193
const ScopeDevice * getDevice() const
Return the associated usb device.
Definition hantekdsocontrol.h:90
Dso::ErrorCode setTriggerLevel(ChannelID channel, double level)
Set the trigger level.
Definition hantekdsocontrol.cpp:366
void requestRefresh(bool active=true)
Definition hantekdsocontrol.h:195
void calibrateOffset(bool enable)
enable/disable offset calibration
Definition hantekdsocontrol.cpp:657
void quitSampling()
Stops the device.
Definition hantekdsocontrol.cpp:668
unsigned getSamplesize() const
Definition hantekdsocontrol.h:80
Dso::ErrorCode setCoupling(ChannelID channel, Dso::Coupling coupling)
Sets the coupling for the given channel.
Definition hantekdsocontrol.cpp:299
void samplerateCalculated(double samplerate, unsigned oversampling)
The samplerate was newly calculated.
friend CapturingThread
Definition hantekdsocontrol.h:55
void restartSampling()
Starts a new sampling block.
Definition hantekdsocontrol.cpp:432
const ControlCommand * getCommand(Hantek::ControlCode code) const
Definition hantekdsocontrol.h:116
unsigned debugLevel
Definition hantekdsocontrol.h:202
unsigned grossSampleCount(unsigned net) const
adjust for skipping of minimal 2048 leading samples
Definition hantekdsocontrol.h:146
bool replaceCalibrationEEPROM
Definition hantekdsocontrol.h:134
Dso::ErrorCode setRecordTime(double duration=0.0)
Sets the time duration of one acquisition by adapting the samplerate.
Definition hantekdsocontrol.cpp:135
unsigned getSampleCount() const
Definition hantekdsocontrol.h:143
const DSOModel * model
The attached scope model.
Definition hantekdsocontrol.h:174
Dso::ErrorCode setSamplerate(double samplerate=0.0)
Sets the samplerate of the oscilloscope.
Definition hantekdsocontrol.cpp:106
void applySettings(DsoSettingsScope *scope)
Initializes the device with the current settings.
Definition hantekdsocontrol.cpp:404
const DSOModel * getModel() const
Return the associated scope model.
Definition hantekdsocontrol.h:93
void setDownsampling(unsigned downsampling)
Definition hantekdsocontrol.h:133
std::unique_ptr< Triggering > triggering
Definition hantekdsocontrol.h:126
double gainCorrection[HANTEK_GAIN_STEPS][HANTEK_CHANNEL_NUMBER]
Definition hantekdsocontrol.h:187
Dso::ErrorCode setTriggerSlope(Dso::Slope slope)
Set the trigger slope.
Definition hantekdsocontrol.cpp:380
Dso::ErrorCode setProbe(ChannelID channel, double probeAttn)
Sets the gain for the given channel. Get the actual gain by specification.gainSteps[gainId].
Definition hantekdsocontrol.cpp:288
Raw raw
Definition hantekdsocontrol.h:201
unsigned netSampleCount(unsigned gross) const
calculate backwards to get multiples of 1000 (typical 20000 or 10000)
Definition hantekdsocontrol.h:149
int displayInterval
Definition hantekdsocontrol.h:192
bool singleChannel
Definition hantekdsocontrol.h:127
~HantekDsoControl() override
Cleans up.
Definition hantekdsocontrol.cpp:39
bool calibrationHasChanged
Definition hantekdsocontrol.h:184
void convertRawDataToSamples()
Converts raw oscilloscope data to sample data.
Definition hantekdsocontrol.cpp:730
void samplesAvailable(const DSOsamples *samples)
New sample data is available.
bool stateMachineRunning
Definition hantekdsocontrol.h:190
int verboseLevel
Definition hantekdsocontrol.h:128
void controlSetSamplerate(uint8_t sampleIndex)
Definition hantekdsocontrol.cpp:91
bool samplingUI
true, if the oscilloscope is taking samples
Definition hantekdsocontrol.h:171
Dso::ControlSettings controlsettings
The current settings of the device.
Definition hantekdsocontrol.h:176
int acquireInterval
Definition hantekdsocontrol.h:191
bool triggerModeNONE()
Definition hantekdsocontrol.h:131
unsigned getRecordLength() const
Definition hantekdsocontrol.cpp:454
Dso::ErrorCode setTriggerPosition(double position)
Set the trigger position.
Definition hantekdsocontrol.cpp:392
void liveCalibrationError() const
const DsoSettingsScope * scope
Global scope parameters and configurations.
Definition hantekdsocontrol.h:177
void setSingleChannel(bool single)
Definition hantekdsocontrol.h:129
void updateSamplerateLimits()
Update the minimum and maximum supported samplerate.
Definition hantekdsocontrol.cpp:71
Dso::ErrorCode setTriggerSource(int channel)
Set the trigger source.
Definition hantekdsocontrol.cpp:343
void addCommand(ControlCommand *newCommand, bool pending=true)
Definition hantekdsocontrol.cpp:943
Dso::ErrorCode setChannelUsed(ChannelID channel, bool used)
Enables/disables filtering of the given channel.
Definition hantekdsocontrol.cpp:203
void communicationError() const
void enableSamplingUI(bool enabled=true)
If sampling is disabled, no samplesAvailable() signals are send anymore, no samples are fetched from ...
Definition hantekdsocontrol.cpp:441
Dso::ErrorCode setTriggerMode(Dso::TriggerMode mode)
Set the trigger mode.
Definition hantekdsocontrol.cpp:321
std::unique_ptr< QSettings > calibrationSettings
Definition hantekdsocontrol.h:185
double getSamplerate() const
Definition hantekdsocontrol.h:78
bool capturing
Definition hantekdsocontrol.h:188
Dso::ErrorCode setChannelInverted(ChannelID channel, bool inverted)
Enables/disables inverting of the given channel.
Definition hantekdsocontrol.cpp:237
double offsetCorrection[HANTEK_GAIN_STEPS][HANTEK_CHANNEL_NUMBER]
Definition hantekdsocontrol.h:186
T * modifyCommand(Hantek::ControlCode code)
Definition hantekdsocontrol.h:109
void updateInterval()
Updates the interval of the periodic thread timer.
Definition hantekdsocontrol.cpp:841
ScopeDevice * scopeDevice
The USB device for the oscilloscope.
Definition hantekdsocontrol.h:169
std::unique_ptr< MathChannel > mathChannel
Definition hantekdsocontrol.h:125
ControlCommand * control[255]
Pointers to control commands.
Definition hantekdsocontrol.h:165
Dso::ErrorCode setCalFreq(double calfreq=0.0)
Sets the calibration frequency of the oscilloscope.
Definition hantekdsocontrol.cpp:182
Dso::ErrorCode setTriggerSmooth(int smooth)
Set the trigger smoothing.
Definition hantekdsocontrol.cpp:354
Dso::ErrorCode getCalibrationFromIniFile()
Definition hantekdsocontrol.cpp:464
This class handles the USB communication with an usb device that has one in and one out endpoint.
Definition scopedevice.h:33
#define HANTEK_CHANNEL_NUMBER
Definition definitions.h:10
#define HANTEK_GAIN_STEPS
Definition definitions.h:9
const int SAMPLESIZE_ROLL
Definition dsosamples.h:26
const int SAMPLESIZE
Definition dsosamples.h:25
Coupling
The coupling modes for the channels.
Definition enums.h:27
TriggerMode
The different triggering modes.
Definition enums.h:35
@ ROLL
Free running without any trigger.
Definition enums.h:39
ErrorCode
The return codes for device control methods.
Definition errorcodes.h:8
Slope
The slope that causes a trigger.
Definition enums.h:45
ControlCode
All supported control commands.
Definition controlcode.h:46
Definition dsosamples.h:11
Holds the settings for the oscilloscope.
Definition scopesettings.h:100
Stores the current settings of the device.
Definition controlsettings.h:54
Stores the specifications of the currently connected device.
Definition controlspecification.h:42
Definition hantekdsocontrol.h:34
unsigned size
Definition hantekdsocontrol.h:44
double samplerate
Definition hantekdsocontrol.h:36
std::vector< unsigned char > data
Definition hantekdsocontrol.h:46
QReadWriteLock lock
Definition hantekdsocontrol.h:47
unsigned oversampling
Definition hantekdsocontrol.h:37
bool valid
Definition hantekdsocontrol.h:42
unsigned channels
Definition hantekdsocontrol.h:35
bool freeRun
Definition hantekdsocontrol.h:41
unsigned received
Definition hantekdsocontrol.h:45
unsigned gainValue[2]
Definition hantekdsocontrol.h:38
bool rollMode
Definition hantekdsocontrol.h:43
unsigned gainIndex[2]
Definition hantekdsocontrol.h:39
unsigned tag
Definition hantekdsocontrol.h:40
unsigned ChannelID
Definition types.h:6