OpenHantek
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"
14 #include "controlspecification.h"
15 #include "dsosamples.h"
16 #include "errorcodes.h"
17 #include "mathchannel.h"
18 #include "scopesettings.h"
19 #include "triggering.h"
20 #include "utils/printutils.h"
21 #include "viewconstants.h"
22 
25 
26 #include "dsomodel.h"
27 
28 #include <vector>
29 
30 #include <QSettings>
31 #include <QThread>
32 
33 class CapturingThread;
34 class ScopeDevice;
35 
36 struct Raw {
37  unsigned channels = 0;
38  double samplerate = 0;
39  unsigned oversampling = 0;
40  unsigned gainValue[ 2 ] = { 1, 1 }; // 1,2,5,10,..
41  unsigned gainIndex[ 2 ] = { 7, 7 }; // index 0..7
42  unsigned tag = 0;
43  bool freeRun = false; // small buffer, no trigger
44  bool valid = false; // samples can be processed
45  bool rollMode = false; // one complete buffer received, start to roll
46  unsigned size = 0;
47  unsigned received = 0;
48  std::vector< unsigned char > data;
49  mutable QReadWriteLock lock;
50 };
51 
52 
55 class HantekDsoControl : public QObject {
56  Q_OBJECT
58 
59  public:
69 
71  ~HantekDsoControl() override;
72 
76  void stateMachine();
77 
79 
80  double getSamplerate() const { return controlsettings.samplerate.current; }
81 
82  unsigned getSamplesize() const {
84  return SAMPLESIZE_ROLL;
85  else
86  return SAMPLESIZE;
87  }
88 
89  bool isSamplingUI() const { return samplingUI; }
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 
124  void prepareForShutdown();
125 
126  private:
127  std::unique_ptr< MathChannel > mathChannel;
128  std::unique_ptr< Triggering > triggering;
129  bool singleChannel = false;
130  int verboseLevel = 0;
131  void setSingleChannel( bool single ) { singleChannel = single; }
132  bool isSingleChannel() const { return singleChannel; }
134  unsigned getRecordLength() const;
135  void setDownsampling( unsigned downsampling ) { downsamplingNumber = downsampling; }
139  Dso::ErrorCode updateCalibrationValues( bool useEEPROM = false );
141 
146 
148  unsigned grossSampleCount( unsigned net ) const { return ( ( net + 1024 ) / 1024 + 2 ) * 1024; }
149 
151  unsigned netSampleCount( unsigned gross ) const { return ( ( gross - 1024 ) / 1000 - 1 ) * 1000; }
152 
153  void updateInterval();
154 
157 
159  void restoreTargets();
160 
162  void updateSamplerateLimits();
163 
164  void controlSetSamplerate( uint8_t sampleIndex );
165 
167  ControlCommand *control[ 255 ] = { nullptr };
169 
170  // Communication with device
172  bool deviceNotConnected();
173  bool samplingUI = false;
174 
175  // Device setup
176  const DSOModel *model;
179  const DsoSettingsScope *scope = nullptr;
180 
181  // Results
182  unsigned downsamplingNumber = 1;
184  unsigned expectedSampleCount = 0;
185  bool calibrationHasChanged = false;
187  std::unique_ptr< QSettings > calibrationSettings;
190  bool capturing = false;
191  bool samplingStarted = false;
192  bool stateMachineRunning = false;
195  unsigned activeChannels = 2;
196  bool refresh = false; // parameter changed -> new raw to result conversion and trigger search needed
197  void requestRefresh( bool active = true ) { refresh = active; }
198  bool refreshNeeded() {
199  bool changed = refresh;
200  refresh = false;
201  return changed;
202  }
204  unsigned debugLevel = 0;
205  uint8_t channelOffset[ 2 ] = { 0x80, 0x80 };
206 
207 #define dprintf( level, fmt, ... ) \
208  do { \
209  if ( debugLevel & level ) \
210  fprintf( stderr, fmt, __VA_ARGS__ ); \
211  } while ( 0 )
212 
213  public slots:
217  void enableSamplingUI( bool enabled = true );
218 
223  Dso::ErrorCode setSamplerate( double samplerate = 0.0 );
224 
229  Dso::ErrorCode setRecordTime( double duration = 0.0 );
230 
235  Dso::ErrorCode setChannelUsed( ChannelID channel, bool used );
236 
241  Dso::ErrorCode setChannelInverted( ChannelID channel, bool inverted );
242 
248  Dso::ErrorCode setProbe( ChannelID channel, double probeAttn );
249 
254  Dso::ErrorCode setGain( ChannelID channel, double gain );
255 
260  Dso::ErrorCode setCoupling( ChannelID channel, Dso::Coupling coupling );
261 
265 
269  Dso::ErrorCode setTriggerSource( int channel );
270 
274  Dso::ErrorCode setTriggerSmooth( int smooth );
275 
280  Dso::ErrorCode setTriggerLevel( ChannelID channel, double level );
281 
286 
290  Dso::ErrorCode setTriggerPosition( double position );
291 
295  Dso::ErrorCode setCalFreq( double calfreq = 0.0 );
296 
300 
302  void restartSampling();
303 
305  void calibrateOffset( bool enable );
306 
307  signals:
308  void showSamplingStatus( bool enabled );
309  void statusMessage( const QString &message, int timeout );
310  void samplesAvailable( const DSOsamples *samples );
311 
313  void samplerateLimitsChanged( double minimum, double maximum );
315  void samplerateSet( int mode, QList< double > sampleSteps );
316 
317  void samplerateChanged( double samplerate );
318 
319  void communicationError() const;
320 
321  void liveCalibrationError() const; // live calibration stopped due to noise or big offset
322 };
323 
324 Q_DECLARE_METATYPE( DSOsamples * )
unsigned received
Definition: hantekdsocontrol.h:47
Dso::ErrorCode setSamplerate(double samplerate=0.0)
Sets the samplerate of the oscilloscope.
Definition: hantekdsocontrol.cpp:107
unsigned downsamplingNumber
Number of downsamples to reduce sample rate.
Definition: hantekdsocontrol.h:182
Slope
The slope that causes a trigger.
Definition: enums.h:45
bool calibrationHasChanged
Definition: hantekdsocontrol.h:186
std::unique_ptr< Triggering > triggering
Definition: hantekdsocontrol.h:128
Dso::ErrorCode setTriggerSlope(Dso::Slope slope)
Set the trigger slope.
Definition: hantekdsocontrol.cpp:379
Dso::ErrorCode setCoupling(ChannelID channel, Dso::Coupling coupling)
Sets the coupling for the given channel.
Definition: hantekdsocontrol.cpp:298
Dso::ErrorCode getCalibrationFromIniFile()
Definition: hantekdsocontrol.cpp:463
int acquireInterval
Definition: hantekdsocontrol.h:193
Dso::ErrorCode setTriggerPosition(double position)
Set the trigger position.
Definition: hantekdsocontrol.cpp:391
ControlCode
All supported control commands.
Definition: controlcode.h:46
unsigned oversampling
Definition: hantekdsocontrol.h:39
bool pending
Definition: controlcommand.h:18
unsigned activeChannels
Definition: hantekdsocontrol.h:195
const DsoSettingsScope * scope
Global scope parameters and configurations.
Definition: hantekdsocontrol.h:179
unsigned size
Definition: hantekdsocontrol.h:46
void updateInterval()
Updates the interval of the periodic thread timer.
Definition: hantekdsocontrol.cpp:840
ControlSettingsSamplerate samplerate
The samplerate settings.
Definition: controlsettings.h:59
const ScopeDevice * getDevice() const
Return the associated usb device.
Definition: hantekdsocontrol.h:92
double offsetCorrection[HANTEK_GAIN_STEPS][HANTEK_CHANNEL_NUMBER]
Definition: hantekdsocontrol.h:188
double samplerate
Definition: hantekdsocontrol.h:38
const int SAMPLESIZE_ROLL
Definition: dsosamples.h:26
Definition: controlcommand.h:12
unsigned gainIndex[2]
Definition: hantekdsocontrol.h:41
unsigned tag
Definition: hantekdsocontrol.h:42
Definition: hantekdsocontrol.h:36
Coupling
The coupling modes for the channels.
Definition: enums.h:27
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:135
#define HANTEK_CHANNEL_NUMBER
Definition: definitions.h:10
bool valid
Definition: hantekdsocontrol.h:44
bool refresh
Definition: hantekdsocontrol.h:196
Definition: dsosamples.h:11
Dso::ErrorCode setTriggerSource(int channel)
Set the trigger source.
Definition: hantekdsocontrol.cpp:342
QReadWriteLock lock
Definition: hantekdsocontrol.h:49
bool deviceNotConnected()
USB status, always false for demo device.
Definition: hantekdsocontrol.cpp:59
Dso::ControlSettings controlsettings
The current settings of the device.
Definition: hantekdsocontrol.h:178
Dso::ErrorCode setChannelUsed(ChannelID channel, bool used)
Enables/disables filtering of the given channel.
Definition: hantekdsocontrol.cpp:202
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:287
DSOsamples result
Definition: hantekdsocontrol.h:183
uint8_t channelOffset[2]
Definition: hantekdsocontrol.h:205
unsigned debugLevel
Definition: hantekdsocontrol.h:204
int displayInterval
Definition: hantekdsocontrol.h:194
std::unique_ptr< QSettings > calibrationSettings
Definition: hantekdsocontrol.h:187
int verboseLevel
Definition: hantekdsocontrol.h:130
Dso::ErrorCode setTriggerMode(Dso::TriggerMode mode)
Set the trigger mode.
Definition: hantekdsocontrol.cpp:320
bool refreshNeeded()
Definition: hantekdsocontrol.h:198
bool isSamplingUI() const
Definition: hantekdsocontrol.h:89
void prepareForShutdown()
Saves calibration settings e.g. to the scope&#39;s EEPROM.
Definition: hantekdsocontrol.cpp:51
unsigned getSampleCount() const
Definition: hantekdsocontrol.h:145
TriggerMode
The different triggering modes.
Definition: enums.h:35
unsigned channels
Definition: hantekdsocontrol.h:37
bool isSingleChannel() const
Definition: hantekdsocontrol.h:132
Dso::ErrorCode setTriggerSmooth(int smooth)
Set the trigger smoothing.
Definition: hantekdsocontrol.cpp:353
void stateMachine()
State machine for the device communication.
Definition: hantekdsocontrol.cpp:861
HantekDsoControl(ScopeDevice *scopeDevice, const DSOModel *model, int verboseLevel)
Definition: hantekdsocontrol.cpp:20
unsigned grossSampleCount(unsigned net) const
adjust for skipping of minimal 2048 leading samples
Definition: hantekdsocontrol.h:148
Dso::ErrorCode setGain(ChannelID channel, double gain)
Sets the probe gain for the given channel.
Definition: hantekdsocontrol.cpp:249
bool freeRun
Definition: hantekdsocontrol.h:43
void addCommand(ControlCommand *newCommand, bool pending=true)
Definition: hantekdsocontrol.cpp:942
void restartSampling()
Starts a new sampling block.
Definition: hantekdsocontrol.cpp:431
bool singleChannel
Definition: hantekdsocontrol.h:129
unsigned netSampleCount(unsigned gross) const
calculate backwards to get multiples of 1000 (typical 20000 or 10000)
Definition: hantekdsocontrol.h:151
std::unique_ptr< MathChannel > mathChannel
Definition: hantekdsocontrol.h:127
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:62
void quitSampling()
Stops the device.
Definition: hantekdsocontrol.cpp:667
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 acquisition by adapting the samplerate.
Definition: hantekdsocontrol.cpp:135
unsigned gainValue[2]
Definition: hantekdsocontrol.h:40
void setSingleChannel(bool single)
Definition: hantekdsocontrol.h:131
void stopStateMachine()
Definition: hantekdsocontrol.h:78
bool replaceCalibrationEEPROM
Definition: hantekdsocontrol.h:136
ControlCommand * control[255]
Pointers to control commands.
Definition: hantekdsocontrol.h:167
Stores the specifications of the currently connected device.
Definition: controlspecification.h:42
bool capturing
Definition: hantekdsocontrol.h:190
void controlSetSamplerate(uint8_t sampleIndex)
Definition: hantekdsocontrol.cpp:92
double gainCorrection[HANTEK_GAIN_STEPS][HANTEK_CHANNEL_NUMBER]
Definition: hantekdsocontrol.h:189
const int SAMPLESIZE
Definition: dsosamples.h:25
void showSamplingStatus(bool enabled)
The oscilloscope started/stopped sampling/waiting for trigger.
unsigned expectedSampleCount
Definition: hantekdsocontrol.h:184
friend CapturingThread
Definition: hantekdsocontrol.h:57
void samplesAvailable(const DSOsamples *samples)
New sample data is available.
bool triggerModeNONE()
Definition: hantekdsocontrol.h:133
T * modifyCommand(Hantek::ControlCode code)
Definition: hantekdsocontrol.h:111
const DSOModel * model
The attached scope model.
Definition: hantekdsocontrol.h:176
ControlCommand * firstControlCommand
Definition: hantekdsocontrol.h:168
Definition: capturing.h:7
~HantekDsoControl() override
Cleans up.
Definition: hantekdsocontrol.cpp:40
bool samplingStarted
Definition: hantekdsocontrol.h:191
bool rollMode
Definition: hantekdsocontrol.h:45
unsigned ChannelID
Definition: types.h:6
bool hasCommand(Hantek::ControlCode code)
Definition: hantekdsocontrol.h:116
void enableSamplingUI(bool enabled=true)
If sampling is disabled, no samplesAvailable() signals are send anymore, no samples are fetched from ...
Definition: hantekdsocontrol.cpp:440
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:956
Dso::ErrorCode getCalibrationFromEEPROM()
Definition: hantekdsocontrol.cpp:563
void samplerateLimitsChanged(double minimum, double maximum)
The available samplerate range has changed.
#define HANTEK_GAIN_STEPS
Definition: definitions.h:9
void convertRawDataToSamples()
Converts raw oscilloscope data to sample data.
Definition: hantekdsocontrol.cpp:729
bool stateMachineRunning
Definition: hantekdsocontrol.h:192
unsigned getRecordLength() const
Definition: hantekdsocontrol.cpp:453
void statusMessage(const QString &message, int timeout)
Status message about the oscilloscope.
Holds the settings for the oscilloscope.
Definition: scopesettings.h:100
void calibrateOffset(bool enable)
enable/disable offset calibration
Definition: hantekdsocontrol.cpp:656
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:181
double getSamplerate() const
Definition: hantekdsocontrol.h:80
Dso::ErrorCode updateCalibrationValues(bool useEEPROM=false)
Definition: hantekdsocontrol.cpp:515
const ChannelID channels
Definition: controlspecification.h:44
ScopeDevice * scopeDevice
The USB device for the oscilloscope.
Definition: hantekdsocontrol.h:171
void samplerateChanged(double samplerate)
The samplerate has changed.
std::vector< unsigned char > data
Definition: hantekdsocontrol.h:48
const ControlCommand * getCommand(Hantek::ControlCode code) const
Definition: hantekdsocontrol.h:118
double current
The current samplerate.
Definition: controlsettings.h:29
Dso::ErrorCode writeCalibrationToEEPROM()
Definition: hantekdsocontrol.cpp:616
Free running without any trigger.
Dso::ErrorCode setTriggerLevel(ChannelID channel, double level)
Set the trigger level.
Definition: hantekdsocontrol.cpp:365
bool samplingUI
true, if the oscilloscope is taking samples
Definition: hantekdsocontrol.h:173
void updateSamplerateLimits()
Update the minimum and maximum supported samplerate.
Definition: hantekdsocontrol.cpp:72
Dso::ErrorCode setChannelInverted(ChannelID channel, bool inverted)
Enables/disables inverting of the given channel.
Definition: hantekdsocontrol.cpp:236
Raw raw
Definition: hantekdsocontrol.h:203
The DsoControl abstraction layer for Hantek USB DSOs. TODO Please anyone, refactor this class into sm...
Definition: hantekdsocontrol.h:55
void applySettings(DsoSettingsScope *scope)
Initializes the device with the current settings.
Definition: hantekdsocontrol.cpp:403
void requestRefresh(bool active=true)
Definition: hantekdsocontrol.h:197
const Dso::ControlSpecification * specification
The specifications of the device.
Definition: hantekdsocontrol.h:177
void liveCalibrationError() const