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#define NOMINMAX // disable windows.h min/max global methods
6#include <limits>
7
8#include "controlsettings.h"
10#include "dsosamples.h"
11#include "errorcodes.h"
12#include "mathchannel.h"
13#include "scopesettings.h"
14#include "triggering.h"
15#include "utils/printutils.h"
16#include "viewconstants.h"
17
20
21#include "dsomodel.h"
22
23#include <vector>
24
25#include <QSettings>
26#include <QThread>
27
28class Capturing;
29class ScopeDevice;
30
31struct Raw {
32 unsigned channels = 0;
33 double samplerate = 0;
34 unsigned oversampling = 0;
35 unsigned gainValue[ 2 ] = { 1, 1 }; // 1,2,5,10,..
36 unsigned gainIndex[ 2 ] = { 7, 7 }; // index 0..7
37 unsigned tag = 0;
38 bool freeRun = false; // small buffer, no trigger
39 bool valid = false; // samples can be processed
40 bool rollMode = false; // one complete buffer received, start to roll
41 unsigned size = 0;
42 unsigned received = 0;
43 std::vector< unsigned char > data;
44 mutable QReadWriteLock lock;
45};
46
47
50class HantekDsoControl : public QObject {
51 Q_OBJECT
52 friend Capturing;
53
54 public:
64
66 ~HantekDsoControl() override;
67
71 void stateMachine();
72
74
75 double getSamplerate() const { return controlsettings.samplerate.current; }
76
77 unsigned getSamplesize() const {
78 if ( controlsettings.trigger.mode == Dso::TriggerMode::ROLL )
79 return SAMPLESIZE_ROLL;
80 else
81 return SAMPLESIZE;
82 }
83
84 bool isSamplingUI() const { return samplingUI; }
85
87 const ScopeDevice *getDevice() const { return scopeDevice; }
88
90 const DSOModel *getModel() const { return model; }
91
92
102 Dso::ErrorCode stringCommand( const QString &commandString );
103
104 void addCommand( ControlCommand *newCommand, bool pending = true );
105
106 template < class T > T *modifyCommand( Hantek::ControlCode code ) {
107 control[ uint8_t( code ) ]->pending = true;
108 return static_cast< T * >( control[ uint8_t( code ) ] );
109 }
110
111 bool hasCommand( Hantek::ControlCode code ) { return ( control[ uint8_t( code ) ] != nullptr ); }
112
113 const ControlCommand *getCommand( Hantek::ControlCode code ) const { return control[ uint8_t( code ) ]; }
114
116 void quitSampling();
117
119 void prepareForShutdown();
120
121 private:
122 std::unique_ptr< MathChannel > mathChannel;
123 std::unique_ptr< Triggering > triggering;
124 bool singleChannel = false;
126 void setSingleChannel( bool single ) { singleChannel = single; }
127 bool isSingleChannel() const { return singleChannel; }
128 bool triggerModeNONE() { return controlsettings.trigger.mode == Dso::TriggerMode::ROLL; }
129 unsigned getRecordLength() const;
130 void setDownsampling( unsigned downsampling ) { downsamplingNumber = downsampling; }
134 Dso::ErrorCode updateCalibrationValues( bool useEEPROM = false );
136
140 unsigned getSampleCount() const { return isSingleChannel() ? getRecordLength() : getRecordLength() * specification->channels; }
141
143 unsigned grossSampleCount( unsigned net ) const { return ( ( net + 1024 ) / 1024 + 2 ) * 1024; }
144
146 unsigned netSampleCount( unsigned gross ) const { return ( ( gross - 1024 ) / 1000 - 1 ) * 1000; }
147
148 void updateInterval();
149
152
154 void restoreTargets();
155
158
159 void controlSetSamplerate( uint8_t sampleIndex );
160
162 ControlCommand *control[ 255 ] = { nullptr };
164
165 // Communication with device
167 bool deviceNotConnected();
168 bool samplingUI = false;
169
170 // Device setup
174 const DsoSettingsScope *scope = nullptr;
175
176 // Results
177 unsigned downsamplingNumber = 1;
179 unsigned expectedSampleCount = 0;
182 std::unique_ptr< QSettings > calibrationSettings;
185 bool capturing = false;
186 bool samplingStarted = false;
190 unsigned activeChannels = 2;
191 bool refresh = false; // parameter changed -> new raw to result conversion and trigger search needed
192 void requestRefresh( bool active = true ) { refresh = active; }
194 bool changed = refresh;
195 refresh = false;
196 return changed;
197 }
199 unsigned debugLevel = 0;
200
201#define dprintf( level, fmt, ... ) \
202 do { \
203 if ( debugLevel & level ) \
204 fprintf( stderr, fmt, __VA_ARGS__ ); \
205 } while ( 0 )
206
207 public slots:
211 void enableSamplingUI( bool enabled = true );
212
217 Dso::ErrorCode setSamplerate( double samplerate = 0.0 );
218
223 Dso::ErrorCode setRecordTime( double duration = 0.0 );
224
229 Dso::ErrorCode setChannelUsed( ChannelID channel, bool used );
230
235 Dso::ErrorCode setChannelInverted( ChannelID channel, bool inverted );
236
242 Dso::ErrorCode setProbe( ChannelID channel, double probeAttn );
243
248 Dso::ErrorCode setGain( ChannelID channel, double gain );
249
255
259
263 Dso::ErrorCode setTriggerSource( int channel );
264
268 Dso::ErrorCode setTriggerSmooth( int smooth );
269
274 Dso::ErrorCode setTriggerLevel( ChannelID channel, double level );
275
280
284 Dso::ErrorCode setTriggerPosition( double position );
285
289 Dso::ErrorCode setCalFreq( double calfreq = 0.0 );
290
294
296 void restartSampling();
297
299 void calibrateOffset( bool enable );
300
301 signals:
302 void showSamplingStatus( bool enabled );
303 void statusMessage( const QString &message, int timeout );
304 void samplesAvailable( const DSOsamples *samples );
305
307 void samplerateLimitsChanged( double minimum, double maximum );
309 void samplerateSet( int mode, QList< double > sampleSteps );
310
311 void samplerateChanged( double samplerate );
312
313 void communicationError() const;
314
315 void liveCalibrationError() const; // live calibration stopped due to noise or big offset
316};
317
318Q_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:163
bool isSamplingUI() const
Definition hantekdsocontrol.h:84
bool hasCommand(Hantek::ControlCode code)
Definition hantekdsocontrol.h:111
void showSamplingStatus(bool enabled)
The oscilloscope started/stopped sampling/waiting for trigger.
unsigned downsamplingNumber
Number of downsamples to reduce sample rate.
Definition hantekdsocontrol.h:177
void samplerateSet(int mode, QList< double > sampleSteps)
The available samplerate for fixed samplerate devices has changed.
friend Capturing
Definition hantekdsocontrol.h:52
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:246
unsigned expectedSampleCount
Definition hantekdsocontrol.h:179
DSOsamples result
Definition hantekdsocontrol.h:178
bool refresh
Definition hantekdsocontrol.h:191
bool isSingleChannel() const
Definition hantekdsocontrol.h:127
bool refreshNeeded()
Definition hantekdsocontrol.h:193
Dso::ErrorCode writeCalibrationToEEPROM()
Definition hantekdsocontrol.cpp:611
Dso::ErrorCode stringCommand(const QString &commandString)
Sends control commands directly.
Definition hantekdsocontrol.cpp:950
bool samplingStarted
Definition hantekdsocontrol.h:186
const Dso::ControlSpecification * specification
The specifications of the device.
Definition hantekdsocontrol.h:172
void prepareForShutdown()
Saves calibration settings e.g. to the scope's EEPROM.
Definition hantekdsocontrol.cpp:52
void statusMessage(const QString &message, int timeout)
Status message about the oscilloscope.
bool deviceNotConnected()
USB status, always false for demo device.
Definition hantekdsocontrol.cpp:60
HantekDsoControl(ScopeDevice *scopeDevice, const DSOModel *model, int verboseLevel)
Definition hantekdsocontrol.cpp:20
Dso::ErrorCode updateCalibrationValues(bool useEEPROM=false)
Definition hantekdsocontrol.cpp:510
void stateMachine()
State machine for the device communication.
Definition hantekdsocontrol.cpp:855
void restoreTargets()
Restore the samplerate/timebase targets after divider updates.
Definition hantekdsocontrol.cpp:63
Dso::ErrorCode getCalibrationFromEEPROM()
Definition hantekdsocontrol.cpp:558
void stopStateMachine()
Definition hantekdsocontrol.h:73
unsigned activeChannels
Definition hantekdsocontrol.h:190
const ScopeDevice * getDevice() const
Return the associated usb device.
Definition hantekdsocontrol.h:87
Dso::ErrorCode setTriggerLevel(ChannelID channel, double level)
Set the trigger level.
Definition hantekdsocontrol.cpp:362
void requestRefresh(bool active=true)
Definition hantekdsocontrol.h:192
void calibrateOffset(bool enable)
enable/disable offset calibration
Definition hantekdsocontrol.cpp:651
void quitSampling()
Stops the device.
Definition hantekdsocontrol.cpp:662
unsigned getSamplesize() const
Definition hantekdsocontrol.h:77
Dso::ErrorCode setCoupling(ChannelID channel, Dso::Coupling coupling)
Sets the coupling for the given channel.
Definition hantekdsocontrol.cpp:295
void restartSampling()
Starts a new sampling block.
Definition hantekdsocontrol.cpp:428
const ControlCommand * getCommand(Hantek::ControlCode code) const
Definition hantekdsocontrol.h:113
unsigned debugLevel
Definition hantekdsocontrol.h:199
unsigned grossSampleCount(unsigned net) const
adjust for skipping of minimal 2048 leading samples
Definition hantekdsocontrol.h:143
bool replaceCalibrationEEPROM
Definition hantekdsocontrol.h:131
Dso::ErrorCode setRecordTime(double duration=0.0)
Sets the time duration of one aquisition by adapting the samplerate.
Definition hantekdsocontrol.cpp:134
unsigned getSampleCount() const
Definition hantekdsocontrol.h:140
const DSOModel * model
The attached scope model.
Definition hantekdsocontrol.h:171
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:400
const DSOModel * getModel() const
Return the associated scope model.
Definition hantekdsocontrol.h:90
void setDownsampling(unsigned downsampling)
Definition hantekdsocontrol.h:130
std::unique_ptr< Triggering > triggering
Definition hantekdsocontrol.h:123
double gainCorrection[HANTEK_GAIN_STEPS][HANTEK_CHANNEL_NUMBER]
Definition hantekdsocontrol.h:184
Dso::ErrorCode setTriggerSlope(Dso::Slope slope)
Set the trigger slope.
Definition hantekdsocontrol.cpp:376
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:284
Raw raw
Definition hantekdsocontrol.h:198
unsigned netSampleCount(unsigned gross) const
calculate backwards to get multiples of 1000 (typical 20000 or 10000)
Definition hantekdsocontrol.h:146
int displayInterval
Definition hantekdsocontrol.h:189
bool singleChannel
Definition hantekdsocontrol.h:124
~HantekDsoControl() override
Cleans up.
Definition hantekdsocontrol.cpp:41
bool calibrationHasChanged
Definition hantekdsocontrol.h:181
void convertRawDataToSamples()
Converts raw oscilloscope data to sample data.
Definition hantekdsocontrol.cpp:724
void samplesAvailable(const DSOsamples *samples)
New sample data is available.
bool stateMachineRunning
Definition hantekdsocontrol.h:187
int verboseLevel
Definition hantekdsocontrol.h:125
void controlSetSamplerate(uint8_t sampleIndex)
Definition hantekdsocontrol.cpp:93
bool samplingUI
true, if the oscilloscope is taking samples
Definition hantekdsocontrol.h:168
Dso::ControlSettings controlsettings
The current settings of the device.
Definition hantekdsocontrol.h:173
Dso::ErrorCode getCalibrationValues()
Definition hantekdsocontrol.cpp:458
int acquireInterval
Definition hantekdsocontrol.h:188
bool triggerModeNONE()
Definition hantekdsocontrol.h:128
unsigned getRecordLength() const
Definition hantekdsocontrol.cpp:448
Dso::ErrorCode setTriggerPosition(double position)
Set the trigger position.
Definition hantekdsocontrol.cpp:388
void liveCalibrationError() const
const DsoSettingsScope * scope
Global scope parameters and configuations.
Definition hantekdsocontrol.h:174
void setSingleChannel(bool single)
Definition hantekdsocontrol.h:126
void updateSamplerateLimits()
Update the minimum and maximum supported samplerate.
Definition hantekdsocontrol.cpp:73
Dso::ErrorCode setTriggerSource(int channel)
Set the trigger source.
Definition hantekdsocontrol.cpp:339
void addCommand(ControlCommand *newCommand, bool pending=true)
Definition hantekdsocontrol.cpp:936
Dso::ErrorCode setChannelUsed(ChannelID channel, bool used)
Enables/disables filtering of the given channel.
Definition hantekdsocontrol.cpp:199
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:437
Dso::ErrorCode setTriggerMode(Dso::TriggerMode mode)
Set the trigger mode.
Definition hantekdsocontrol.cpp:317
std::unique_ptr< QSettings > calibrationSettings
Definition hantekdsocontrol.h:182
double getSamplerate() const
Definition hantekdsocontrol.h:75
bool capturing
Definition hantekdsocontrol.h:185
Dso::ErrorCode setChannelInverted(ChannelID channel, bool inverted)
Enables/disables inverting of the given channel.
Definition hantekdsocontrol.cpp:233
void samplerateChanged(double samplerate)
The samplerate has changed.
double offsetCorrection[HANTEK_GAIN_STEPS][HANTEK_CHANNEL_NUMBER]
Definition hantekdsocontrol.h:183
T * modifyCommand(Hantek::ControlCode code)
Definition hantekdsocontrol.h:106
void updateInterval()
Updates the interval of the periodic thread timer.
Definition hantekdsocontrol.cpp:834
ScopeDevice * scopeDevice
The USB device for the oscilloscope.
Definition hantekdsocontrol.h:166
std::unique_ptr< MathChannel > mathChannel
Definition hantekdsocontrol.h:122
ControlCommand * control[255]
Pointers to control commands.
Definition hantekdsocontrol.h:162
Dso::ErrorCode setCalFreq(double calfreq=0.0)
Sets the calibration frequency of the oscilloscope.
Definition hantekdsocontrol.cpp:178
Dso::ErrorCode setTriggerSmooth(int smooth)
Set the trigger smoothing.
Definition hantekdsocontrol.cpp:350
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:88
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:31
unsigned size
Definition hantekdsocontrol.h:41
double samplerate
Definition hantekdsocontrol.h:33
std::vector< unsigned char > data
Definition hantekdsocontrol.h:43
QReadWriteLock lock
Definition hantekdsocontrol.h:44
unsigned oversampling
Definition hantekdsocontrol.h:34
bool valid
Definition hantekdsocontrol.h:39
unsigned channels
Definition hantekdsocontrol.h:32
bool freeRun
Definition hantekdsocontrol.h:38
unsigned received
Definition hantekdsocontrol.h:42
unsigned gainValue[2]
Definition hantekdsocontrol.h:35
bool rollMode
Definition hantekdsocontrol.h:40
unsigned gainIndex[2]
Definition hantekdsocontrol.h:36
unsigned tag
Definition hantekdsocontrol.h:37
unsigned ChannelID
Definition types.h:6