OpenHantek
hantekdsocontrol.h
Go to the documentation of this file.
1 // SPDX-License-Identifier: GPL-2.0+
2 
3 #pragma once
4 
5 #define NOMINMAX // disable windows.h min/max global methods
6 #include <limits>
7 
8 #include "controlsettings.h"
9 #include "controlspecification.h"
10 #include "dsosamples.h"
11 #include "errorcodes.h"
12 #include "scopesettings.h"
13 #include "utils/printutils.h"
14 #include "viewconstants.h"
15 
18 
19 #include "dsomodel.h"
20 
21 #include <vector>
22 
23 #include <QMutex>
24 #include <QReadLocker>
25 #include <QReadWriteLock>
26 #include <QStringList>
27 #include <QThread>
28 #include <QTimer>
29 #include <QWriteLocker>
30 
31 class Capturing;
32 class ScopeDevice;
33 
34 struct 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 
53 class HantekDsoControl : public QObject {
54  Q_OBJECT
55  friend Capturing;
56 
57  public:
66  explicit HantekDsoControl( ScopeDevice *scopeDevice, const DSOModel *model, unsigned verboseLevel );
67 
70 
74  void stateMachine();
75 
77 
78  double getSamplerate() const { return controlsettings.samplerate.current; }
79 
80  static const unsigned SAMPLESIZE = 20000;
81  static const unsigned SAMPLESIZE_ROLL = 39 * 256;
82  unsigned getSamplesize() const {
84  return SAMPLESIZE_ROLL;
85  else
86  return SAMPLESIZE;
87  }
88 
89  bool isSampling() const { return sampling; }
90 
92  const ScopeDevice *getDevice() const { return scopeDevice; }
93 
95  const DSOModel *getModel() const { return model; }
96 
97 
107  Dso::ErrorCode stringCommand( const QString &commandString );
108 
109  void addCommand( ControlCommand *newCommand, bool pending = true );
110 
111  template < class T > T *modifyCommand( Hantek::ControlCode code ) {
112  control[ uint8_t( code ) ]->pending = true;
113  return static_cast< T * >( control[ uint8_t( code ) ] );
114  }
115 
116  bool hasCommand( Hantek::ControlCode code ) { return ( control[ uint8_t( code ) ] != nullptr ); }
117 
118  const ControlCommand *getCommand( Hantek::ControlCode code ) const { return control[ uint8_t( code ) ]; }
119 
121  void quitSampling();
122 
123  private:
124  bool singleChannel = false;
125  unsigned verboseLevel = 0;
126  void setSingleChannel( bool single ) { singleChannel = single; }
127  bool isSingleChannel() const { return singleChannel; }
129  unsigned getRecordLength() const;
130  void setDownsampling( unsigned downsampling ) { downsamplingNumber = downsampling; }
132 
137 
139  unsigned grossSampleCount( unsigned net ) const { return ( ( net + 1024 ) / 1024 + 2 ) * 1024; }
140 
142  unsigned netSampleCount( unsigned gross ) const { return ( ( gross - 1024 ) / 1000 - 1 ) * 1000; }
143 
144  void updateInterval();
145 
149  static unsigned calculateTriggerPoint( unsigned value );
150 
151  // void capture( HantekDsoControl *hdc );
152 
154  // void convertRawDataToSamples( const std::vector< unsigned char > &rawData );
156 
158  void restoreTargets();
159 
161  void updateSamplerateLimits();
162 
163  unsigned searchTriggerPoint( Dso::Slope dsoSlope, unsigned int startPos = 0 );
164 
167  }
168 
169  unsigned searchTriggeredPosition();
170 
171  bool provideTriggeredData();
172 
173  void controlSetSamplerate( uint8_t sampleIndex );
174 
176  ControlCommand *control[ 255 ] = { nullptr };
178 
179  // Communication with device
181  bool deviceNotConnected();
182  bool sampling = false;
183 
184  // Device setup
185  const DSOModel *model;
188  const DsoSettingsScope *scope = nullptr;
189 
190  // Results
191  unsigned downsamplingNumber = 1;
193  unsigned expectedSampleCount = 0;
194  bool capturing = false;
196  bool samplingStarted = false;
197  bool stateMachineRunning = false;
200  unsigned triggeredPositionRaw = 0; // not triggered
201  unsigned activeChannels = 2;
202  bool newTriggerParam = false; // parameter changed -> new trigger search needed
203  bool triggerChanged() {
204  bool changed = newTriggerParam;
205  newTriggerParam = false;
206  return changed;
207  }
208 
210 
211  std::vector< QString > controlNames = { "SETGAIN_CH1", "SETGAIN_CH2", "SETSAMPLERATE", "STARTSAMPLING",
212  "SETNUMCHANNELS", "SETCOUPLING", "SETCALFREQ" };
213 
214  unsigned debugLevel = 0;
215 
216 #define dprintf( level, fmt, ... ) \
217  do { \
218  if ( debugLevel & level ) \
219  fprintf( stderr, fmt, __VA_ARGS__ ); \
220  } while ( 0 )
221 
222  public slots:
226  void enableSampling( bool enabled );
227 
232  Dso::ErrorCode setSamplerate( double samplerate = 0.0 );
233 
238  Dso::ErrorCode setRecordTime( double duration = 0.0 );
239 
244  Dso::ErrorCode setChannelUsed( ChannelID channel, bool used );
245 
250  Dso::ErrorCode setChannelInverted( ChannelID channel, bool inverted );
251 
257  Dso::ErrorCode setProbe( ChannelID channel, double probeAttn );
258 
263  Dso::ErrorCode setGain( ChannelID channel, double gain );
264 
269  Dso::ErrorCode setCoupling( ChannelID channel, Dso::Coupling coupling );
270 
274 
278  Dso::ErrorCode setTriggerSource( int channel );
279 
283  Dso::ErrorCode setTriggerSmooth( int smooth );
284 
289  Dso::ErrorCode setTriggerLevel( ChannelID channel, double level );
290 
295 
299  Dso::ErrorCode setTriggerPosition( double position );
300 
304  Dso::ErrorCode setCalFreq( double calfreq = 0.0 );
305 
309 
311  void restartSampling();
312 
313  signals:
314  void samplingStatusChanged( bool enabled );
315  void statusMessage( const QString &message, int timeout );
316  void samplesAvailable( const DSOsamples *samples );
317 
319  void samplerateLimitsChanged( double minimum, double maximum );
321  void samplerateSet( int mode, QList< double > sampleSteps );
322 
323  void samplerateChanged( double samplerate );
324 
325  void communicationError() const;
326 };
327 
328 Q_DECLARE_METATYPE( DSOsamples * )
unsigned received
Definition: hantekdsocontrol.h:45
Dso::ErrorCode setSamplerate(double samplerate=0.0)
Sets the samplerate of the oscilloscope.
Definition: hantekdsocontrol.cpp:100
unsigned downsamplingNumber
Number of downsamples to reduce sample rate.
Definition: hantekdsocontrol.h:191
Slope
The slope that causes a trigger.
Definition: enums.h:45
Dso::ErrorCode setTriggerSlope(Dso::Slope slope)
Set the trigger slope.
Definition: hantekdsocontrol.cpp:366
Dso::ErrorCode setCoupling(ChannelID channel, Dso::Coupling coupling)
Sets the coupling for the given channel.
Definition: hantekdsocontrol.cpp:285
Definition: QtAwesome.h:298
unsigned triggeredPositionRaw
Definition: hantekdsocontrol.h:200
void enableSampling(bool enabled)
If sampling is disabled, no samplesAvailable() signals are send anymore, no samples are fetched from ...
Definition: hantekdsocontrol.cpp:424
int acquireInterval
Definition: hantekdsocontrol.h:198
static const unsigned SAMPLESIZE_ROLL
Definition: hantekdsocontrol.h:81
Dso::ErrorCode setTriggerPosition(double position)
Set the trigger position.
Definition: hantekdsocontrol.cpp:378
ControlCode
All supported control commands.
Definition: controlcode.h:44
unsigned oversampling
Definition: hantekdsocontrol.h:37
bool pending
Definition: controlcommand.h:18
unsigned activeChannels
Definition: hantekdsocontrol.h:201
const DsoSettingsScope * scope
Global scope parameters and configuations.
Definition: hantekdsocontrol.h:188
unsigned size
Definition: hantekdsocontrol.h:44
From higher to lower voltage.
void updateInterval()
Updates the interval of the periodic thread timer.
Definition: hantekdsocontrol.cpp:734
ControlSettingsSamplerate samplerate
The samplerate settings.
Definition: controlsettings.h:59
const ScopeDevice * getDevice() const
Return the associated usb device.
Definition: hantekdsocontrol.h:92
double samplerate
Definition: hantekdsocontrol.h:36
Definition: controlcommand.h:12
unsigned gainIndex[2]
Definition: hantekdsocontrol.h:39
unsigned tag
Definition: hantekdsocontrol.h:40
Definition: hantekdsocontrol.h:34
Coupling
The coupling modes for the channels.
Definition: enums.h:27
static unsigned calculateTriggerPoint(unsigned value)
Calculates the trigger point from the CommandGetCaptureState data.
Dso::TriggerMode mode
The trigger mode.
Definition: controlsettings.h:37
ControlSettingsTrigger trigger
The trigger settings.
Definition: controlsettings.h:61
void setDownsampling(unsigned downsampling)
Definition: hantekdsocontrol.h:130
bool isSampling() const
Definition: hantekdsocontrol.h:89
Definition: capturing.h:7
bool valid
Definition: hantekdsocontrol.h:42
Definition: dsosamples.h:10
Dso::ErrorCode setTriggerSource(int channel)
Set the trigger source.
Definition: hantekdsocontrol.cpp:329
QReadWriteLock lock
Definition: hantekdsocontrol.h:47
bool deviceNotConnected()
USB status, always false for demo device.
Definition: hantekdsocontrol.cpp:54
Dso::ControlSettings controlsettings
The current settings of the device.
Definition: hantekdsocontrol.h:187
Dso::ErrorCode setChannelUsed(ChannelID channel, bool used)
Enables/disables filtering of the given channel.
Definition: hantekdsocontrol.cpp:192
const DSOModel * getModel() const
Return the associated scope model.
Definition: hantekdsocontrol.h:95
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:274
DSOsamples result
Definition: hantekdsocontrol.h:192
~HantekDsoControl()
Cleans up.
Definition: hantekdsocontrol.cpp:43
unsigned debugLevel
Definition: hantekdsocontrol.h:214
int displayInterval
Definition: hantekdsocontrol.h:199
Dso::ErrorCode setTriggerMode(Dso::TriggerMode mode)
Set the trigger mode.
Definition: hantekdsocontrol.cpp:307
unsigned getSampleCount() const
Definition: hantekdsocontrol.h:136
TriggerMode
The different triggering modes.
Definition: enums.h:35
unsigned channels
Definition: hantekdsocontrol.h:35
bool isSingleChannel() const
Definition: hantekdsocontrol.h:127
Dso::ErrorCode setTriggerSmooth(int smooth)
Set the trigger smoothing.
Definition: hantekdsocontrol.cpp:340
void stateMachine()
State machine for the device communication.
Definition: hantekdsocontrol.cpp:755
unsigned grossSampleCount(unsigned net) const
adjust for skipping of minimal 2048 leading samples
Definition: hantekdsocontrol.h:139
Dso::ErrorCode setGain(ChannelID channel, double gain)
Sets the probe gain for the given channel.
Definition: hantekdsocontrol.cpp:238
bool freeRun
Definition: hantekdsocontrol.h:41
void addCommand(ControlCommand *newCommand, bool pending=true)
Definition: hantekdsocontrol.cpp:828
void restartSampling()
Starts a new sampling block.
Definition: hantekdsocontrol.cpp:415
bool singleChannel
Definition: hantekdsocontrol.h:124
unsigned netSampleCount(unsigned gross) const
calculate backwards to get multiples of 1000 (typical 20000 or 10000)
Definition: hantekdsocontrol.h:142
This class handles the USB communication with an usb device that has one in and one out endpoint...
Definition: scopedevice.h:33
unsigned getSamplesize() const
Definition: hantekdsocontrol.h:82
Describes a device This is the central class to describe a hantek compatible DSO. It contains all usb...
Definition: dsomodel.h:17
void restoreTargets()
Restore the samplerate/timebase targets after divider updates.
Definition: hantekdsocontrol.cpp:57
void quitSampling()
Stops the device.
Definition: hantekdsocontrol.cpp:472
bool provideTriggeredData()
Definition: hantekdsocontrol.cpp:705
friend Capturing
Definition: hantekdsocontrol.h:55
void samplerateSet(int mode, QList< double > sampleSteps)
The available samplerate for fixed samplerate devices has changed.
Dso::ErrorCode setRecordTime(double duration=0.0)
Sets the time duration of one aquisition by adapting the samplerate.
Definition: hantekdsocontrol.cpp:128
unsigned gainValue[2]
Definition: hantekdsocontrol.h:38
bool triggerChanged()
Definition: hantekdsocontrol.h:203
void setSingleChannel(bool single)
Definition: hantekdsocontrol.h:126
void stopStateMachine()
Definition: hantekdsocontrol.h:76
unsigned verboseLevel
Definition: hantekdsocontrol.h:125
void samplingStatusChanged(bool enabled)
The oscilloscope started/stopped sampling/waiting for trigger.
ControlCommand * control[255]
Pointers to control commands.
Definition: hantekdsocontrol.h:176
bool newTriggerParam
Definition: hantekdsocontrol.h:202
Stores the specifications of the currently connected device.
Definition: controlspecification.h:42
Dso::Slope mirrorSlope(Dso::Slope slope)
Definition: hantekdsocontrol.h:165
bool capturing
Definition: hantekdsocontrol.h:195
void controlSetSamplerate(uint8_t sampleIndex)
Definition: hantekdsocontrol.cpp:87
HantekDsoControl(ScopeDevice *scopeDevice, const DSOModel *model, unsigned verboseLevel)
Definition: hantekdsocontrol.cpp:27
From lower to higher voltage.
unsigned expectedSampleCount
Definition: hantekdsocontrol.h:193
void samplesAvailable(const DSOsamples *samples)
New sample data is available.
bool triggerModeNONE()
Definition: hantekdsocontrol.h:128
T * modifyCommand(Hantek::ControlCode code)
Definition: hantekdsocontrol.h:111
const DSOModel * model
The attached scope model.
Definition: hantekdsocontrol.h:185
unsigned searchTriggeredPosition()
Definition: hantekdsocontrol.cpp:650
unsigned searchTriggerPoint(Dso::Slope dsoSlope, unsigned int startPos=0)
Definition: hantekdsocontrol.cpp:572
ControlCommand * firstControlCommand
Definition: hantekdsocontrol.h:177
bool samplingStarted
Definition: hantekdsocontrol.h:196
bool rollMode
Definition: hantekdsocontrol.h:43
unsigned ChannelID
Definition: types.h:6
bool hasCommand(Hantek::ControlCode code)
Definition: hantekdsocontrol.h:116
ErrorCode
The return codes for device control methods.
Definition: errorcodes.h:8
Dso::ErrorCode stringCommand(const QString &commandString)
Sends control commands directly.
Definition: hantekdsocontrol.cpp:841
void samplerateLimitsChanged(double minimum, double maximum)
The available samplerate range has changed.
void convertRawDataToSamples()
Converts raw oscilloscope data to sample data.
Definition: hantekdsocontrol.cpp:492
bool stateMachineRunning
Definition: hantekdsocontrol.h:197
unsigned getRecordLength() const
Definition: hantekdsocontrol.cpp:433
void statusMessage(const QString &message, int timeout)
Status message about the oscilloscope.
Holds the settings for the oscilloscope.
Definition: scopesettings.h:82
std::vector< QString > controlNames
Definition: hantekdsocontrol.h:211
Stores the current settings of the device.
Definition: controlsettings.h:54
void communicationError() const
Dso::ErrorCode setCalFreq(double calfreq=0.0)
Sets the calibration frequency of the oscilloscope.
Definition: hantekdsocontrol.cpp:173
double getSamplerate() const
Definition: hantekdsocontrol.h:78
const ChannelID channels
Definition: controlspecification.h:44
ScopeDevice * scopeDevice
The USB device for the oscilloscope.
Definition: hantekdsocontrol.h:180
static const unsigned SAMPLESIZE
Definition: hantekdsocontrol.h:80
void samplerateChanged(double samplerate)
The samplerate has changed.
std::vector< unsigned char > data
Definition: hantekdsocontrol.h:46
const ControlCommand * getCommand(Hantek::ControlCode code) const
Definition: hantekdsocontrol.h:118
double current
The current samplerate.
Definition: controlsettings.h:29
Free running without any trigger.
bool sampling
true, if the oscilloscope is taking samples
Definition: hantekdsocontrol.h:182
Dso::ErrorCode setTriggerLevel(ChannelID channel, double level)
Set the trigger level.
Definition: hantekdsocontrol.cpp:352
void updateSamplerateLimits()
Update the minimum and maximum supported samplerate.
Definition: hantekdsocontrol.cpp:67
Dso::ErrorCode retrieveChannelLevelData()
Definition: hantekdsocontrol.cpp:443
Dso::ErrorCode setChannelInverted(ChannelID channel, bool inverted)
Enables/disables inverting of the given channel.
Definition: hantekdsocontrol.cpp:225
Raw raw
Definition: hantekdsocontrol.h:209
The DsoControl abstraction layer for Hantek USB DSOs. TODO Please anyone, refactor this class into sm...
Definition: hantekdsocontrol.h:53
void applySettings(DsoSettingsScope *scope)
Initializes the device with the current settings.
Definition: hantekdsocontrol.cpp:390
const Dso::ControlSpecification * specification
The specifications of the device.
Definition: hantekdsocontrol.h:186