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 "scopesettings.h"
10 #include "controlspecification.h"
11 #include "dsosamples.h"
12 #include "errorcodes.h"
13 #include "states.h"
14 #include "viewconstants.h"
15 #include "utils/printutils.h"
16 
19 
20 #include <vector>
21 
22 #include <QMutex>
23 #include <QStringList>
24 #include <QThread>
25 #include <QTimer>
26 
27 class USBDevice;
28 
31 class HantekDsoControl : public QObject {
32  Q_OBJECT
33 
34  public:
43  explicit HantekDsoControl( USBDevice *device );
44 
47 
51  void run();
52 
53  double getSamplerate() const { return controlsettings.samplerate.current; }
54 
55  unsigned getSamplesize() const { return SAMPLESIZE_USED; }
56 
57  bool isSampling() const { return sampling; }
58 
60  const USBDevice *getDevice() const { return device; }
61 
62 
72  Dso::ErrorCode stringCommand(const QString &commandString);
73 
74  void addCommand(ControlCommand *newCommand, bool pending = true);
75 
76  template <class T> T *modifyCommand(Hantek::ControlCode code) {
77  control[ uint8_t( code ) ]->pending = true;
78  return static_cast<T *>(control[ uint8_t( code ) ] );
79  }
80 
81  bool hasCommand(Hantek::ControlCode code) { return ( control[uint8_t(code)] != nullptr ); }
82 
83  const ControlCommand *getCommand(Hantek::ControlCode code) const { return control[ uint8_t( code ) ]; }
84 
85 // Attic for no more used public procedures
86 #if 0
87  unsigned getChannelCount() const { return specification->channels; }
90 
93  const Dso::ControlSettings *getDeviceSettings() const { return &controlsettings; }
94 
97  const std::vector<unsigned> &getAvailableRecordLengths() const {
99  }
100 
103  double getMinSamplerate() const {
104  //printf( "getMinSamplerate\n" );
106  }
107 
110  double getMaxSamplerate() const {
111  //printf( "channelCount %d\n", controlsettings.channelCount );
112  if (controlsettings.channelCount <= 1) {
114  } else {
116  }
117  }
118 
121  int getConnectionSpeed() const {
122  ControlGetSpeed response;
123  int errorCode = device->controlRead(&response);
124  if (errorCode < 0) return errorCode;
125  return response.getSpeed();
126  }
127 
130  int getPacketSize() const {
131  const int s = getConnectionSpeed();
132  if (s == CONNECTION_FULLSPEED)
133  return 64;
134  else if (s == CONNECTION_HIGHSPEED)
135  return 512;
136  else if (s > CONNECTION_HIGHSPEED) {
137  qWarning() << "Unknown USB speed. Please correct source code in USBDevice::getPacketSize()";
138  throw new std::runtime_error("Unknown USB speed");
139  } else if (s < 0)
140  return s;
141  return 0;
142  }
143 
145  const DSOsamples &getLastSamples() { return result; }
146 #endif
147 
148  private:
149  bool isFastRate() const {
150  return controlsettings.voltage[0].used && !controlsettings.voltage[1].used;
151  }
152 
153  unsigned getRecordLength() const;
154 
155  void setDownsampling( unsigned downsampling ) { downsamplingNumber = downsampling; }
156 
158 
162  unsigned getSampleCount() const {
164  }
165 
166  void updateInterval();
167 
171  static unsigned calculateTriggerPoint(unsigned value);
172 
174  std::vector<unsigned char> getSamples(unsigned &expectedSampleCount) const;
175 
177  void convertRawDataToSamples(const std::vector<unsigned char> &rawData);
178 
184  unsigned updateSamplerate(unsigned downsampler, bool fastRate);
185 
187  void restoreTargets();
188 
190  void updateSamplerateLimits();
191 
192  unsigned searchTriggerPoint( Dso::Slope dsoSlope, unsigned int startPos = 0 );
193 
196  }
197 
198  unsigned softwareTrigger();
199 
200  bool triggering();
201 
203  ControlCommand *control[255] = { nullptr };
205 
206  // Communication with device
208  bool sampling = false;
209 
210  // Device setup
213 
214  // Results
215  unsigned downsamplingNumber = 1;
217  unsigned expectedSampleCount = 0;
218  bool samplingStarted = false;
222  bool channelSetupChanged = false;
223  unsigned triggeredPositionRaw = 0; // not triggered
224 
225  public slots:
229  void enableSampling(bool enabled);
230 
235  Dso::ErrorCode setSamplerate(double samplerate = 0.0);
236 
241  Dso::ErrorCode setRecordTime(double duration = 0.0);
242 
247  Dso::ErrorCode setChannelUsed(ChannelID channel, bool used);
248 
253  Dso::ErrorCode setChannelInverted(ChannelID channel, bool inverted);
254 
260  Dso::ErrorCode setProbe(ChannelID channel, double probeAttn);
261 
266  Dso::ErrorCode setGain(ChannelID channel, double gain);
267 
273 
277 
281  Dso::ErrorCode setTriggerSource(ChannelID channel, bool smooth);
282 
287  Dso::ErrorCode setTriggerLevel(ChannelID channel, double level);
288 
293 
297  Dso::ErrorCode setTriggerOffset (double position);
298 
302  Dso::ErrorCode setCalFreq(double calfreq = 0.0);
303 
306  void applySettings(DsoSettingsScope *scope);
307 
308  signals:
309  void samplingStatusChanged(bool enabled);
310  void statusMessage(const QString &message, int timeout);
311  void samplesAvailable(const DSOsamples *samples);
312 
314  void samplerateLimitsChanged(double minimum, double maximum);
316  void samplerateSet(int mode, QList<double> sampleSteps);
317 
318  void recordTimeChanged(double duration);
319  void samplerateChanged(double samplerate);
320 
321  void communicationError() const;
322 };
323 
324 Q_DECLARE_METATYPE(DSOsamples *)
FullSpeed USB, 64 byte bulk transfers.
Definition: usbdevicedefinitions.h:15
Dso::ErrorCode setSamplerate(double samplerate=0.0)
Sets the samplerate of the oscilloscope.
Definition: hantekdsocontrol.cpp:105
unsigned downsamplingNumber
Number of downsamples to reduce sample rate.
Definition: hantekdsocontrol.h:215
std::vector< unsigned > recordLengths
Available record lengths, UINT_MAX means rolling.
Definition: controlspecification.h:19
Slope
The slope that causes a trigger.
Definition: enums.h:44
Dso::ErrorCode setTriggerSlope(Dso::Slope slope)
Set the trigger slope.
Definition: hantekdsocontrol.cpp:321
Dso::ErrorCode setCoupling(ChannelID channel, Dso::Coupling coupling)
Sets the coupling for the given channel.
Definition: hantekdsocontrol.cpp:274
Definition: QtAwesome.h:192
unsigned triggeredPositionRaw
Definition: hantekdsocontrol.h:223
void enableSampling(bool enabled)
If sampling is disabled, no samplesAvailable() signals are send anymore, no samples are fetched from ...
Definition: hantekdsocontrol.cpp:361
int acquireInterval
Definition: hantekdsocontrol.h:220
void recordTimeChanged(double duration)
The record time duration has changed.
ControlCode
All supported control commands.
Definition: controlcode.h:44
bool pending
Definition: controlcommand.h:16
From higher to lower voltage.
void updateInterval()
Updates the interval of the periodic thread timer.
Definition: hantekdsocontrol.cpp:678
ControlSettingsSamplerate samplerate
The samplerate settings.
Definition: controlsettings.h:60
Definition: controlcommand.h:12
Coupling
The coupling modes for the channels.
Definition: enums.h:27
static unsigned calculateTriggerPoint(unsigned value)
Calculates the trigger point from the CommandGetCaptureState data.
void setDownsampling(unsigned downsampling)
Definition: hantekdsocontrol.h:155
bool isSampling() const
Definition: hantekdsocontrol.h:57
ControlSamplerateLimits multi
The limits for multi channel mode.
Definition: controlspecification.h:25
Definition: dsosamples.h:10
Dso::ControlSettings controlsettings
The current settings of the device.
Definition: hantekdsocontrol.h:212
Dso::ErrorCode setChannelUsed(ChannelID channel, bool used)
Enables/disables filtering of the given channel.
Definition: hantekdsocontrol.cpp:191
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:265
DSOsamples result
Definition: hantekdsocontrol.h:216
~HantekDsoControl()
Cleans up.
Definition: hantekdsocontrol.cpp:45
int displayInterval
Definition: hantekdsocontrol.h:221
bool isFastRate() const
Definition: hantekdsocontrol.h:149
Dso::ErrorCode setTriggerSource(ChannelID channel, bool smooth)
Set the trigger source.
Definition: hantekdsocontrol.cpp:300
HantekDsoControl(USBDevice *device)
Definition: hantekdsocontrol.cpp:28
Dso::ErrorCode setTriggerMode(Dso::TriggerMode mode)
Set the trigger mode.
Definition: hantekdsocontrol.cpp:290
unsigned updateSamplerate(unsigned downsampler, bool fastRate)
Sets the samplerate based on the parameters calculated by Control::getBestSamplerate.
Definition: hantekdsocontrol.cpp:54
unsigned getSampleCount() const
Definition: hantekdsocontrol.h:162
TriggerMode
The different triggering modes.
Definition: enums.h:35
const USBDevice * getDevice() const
Return the associated usb device.
Definition: hantekdsocontrol.h:60
unsigned softwareTrigger()
Definition: hantekdsocontrol.cpp:591
Dso::ErrorCode setGain(ChannelID channel, double gain)
Sets the probe gain for the given channel.
Definition: hantekdsocontrol.cpp:238
void addCommand(ControlCommand *newCommand, bool pending=true)
Definition: hantekdsocontrol.cpp:763
int controlRead(const T *command)
Control read to the oscilloscope.
Definition: usbdevice.h:122
unsigned getSamplesize() const
Definition: hantekdsocontrol.h:55
unsigned channelCount
Number of activated channels.
Definition: controlsettings.h:64
USBDevice * device
The USB device for the oscilloscope.
Definition: hantekdsocontrol.h:207
HighSpeed USB, 512 byte bulk transfers.
Definition: usbdevicedefinitions.h:16
void restoreTargets()
Restore the samplerate/timebase targets after divider updates.
Definition: hantekdsocontrol.cpp:80
void samplerateSet(int mode, QList< double > sampleSteps)
The available samplerate for fixed samplerate devices has changed.
void run()
Definition: hantekdsocontrol.cpp:696
Dso::ErrorCode setRecordTime(double duration=0.0)
Sets the time duration of one aquisition by adapting the samplerate.
Definition: hantekdsocontrol.cpp:134
std::vector< unsigned char > getSamples(unsigned &expectedSampleCount) const
Gets sample data from the oscilloscope.
Definition: hantekdsocontrol.cpp:397
unsigned int maxDownsampler
The maximum downsampling ratio.
Definition: controlspecification.h:18
void samplingStatusChanged(bool enabled)
The oscilloscope started/stopped sampling/waiting for trigger.
ControlCommand * control[255]
Pointers to control commands.
Definition: hantekdsocontrol.h:203
Stores the specifications of the currently connected device.
Definition: controlspecification.h:42
Dso::Slope mirrorSlope(Dso::Slope slope)
Definition: hantekdsocontrol.h:194
double base
The base for sample rate calculations.
Definition: controlspecification.h:16
From lower to higher voltage.
unsigned expectedSampleCount
Definition: hantekdsocontrol.h:217
ControlSpecificationSamplerate samplerate
The samplerate specifications.
Definition: controlspecification.h:47
void samplesAvailable(const DSOsamples *samples)
New sample data is available.
T * modifyCommand(Hantek::ControlCode code)
Definition: hantekdsocontrol.h:76
unsigned searchTriggerPoint(Dso::Slope dsoSlope, unsigned int startPos=0)
Definition: hantekdsocontrol.cpp:530
ControlCommand * firstControlCommand
Definition: hantekdsocontrol.h:204
bool samplingStarted
Definition: hantekdsocontrol.h:219
unsigned ChannelID
Definition: types.h:6
bool hasCommand(Hantek::ControlCode code)
Definition: hantekdsocontrol.h:81
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:771
void samplerateLimitsChanged(double minimum, double maximum)
The available samplerate range has changed.
double max
The maximum sample rate.
Definition: controlspecification.h:17
unsigned getRecordLength() const
Definition: hantekdsocontrol.cpp:368
Dso::ErrorCode setTriggerOffset(double position)
Set the trigger position.
Definition: hantekdsocontrol.cpp:331
void statusMessage(const QString &message, int timeout)
Status message about the oscilloscope.
Holds the settings for the oscilloscope.
Definition: scopesettings.h:75
Stores the current settings of the device.
Definition: controlsettings.h:55
This class handles the USB communication with an usb device that has one in and one out endpoint...
Definition: usbdevice.h:28
void communicationError() const
Dso::ErrorCode setCalFreq(double calfreq=0.0)
Sets the calibration frequency of the oscilloscope.
Definition: hantekdsocontrol.cpp:177
void convertRawDataToSamples(const std::vector< unsigned char > &rawData)
Converts raw oscilloscope data to sample data.
Definition: hantekdsocontrol.cpp:433
double getSamplerate() const
Definition: hantekdsocontrol.h:53
const ChannelID channels
Definition: controlspecification.h:44
std::vector< ControlSettingsVoltage > voltage
The amplification settings.
Definition: controlsettings.h:61
bool triggering()
Definition: hantekdsocontrol.cpp:650
void samplerateChanged(double samplerate)
The samplerate has changed.
const ControlCommand * getCommand(Hantek::ControlCode code) const
Definition: hantekdsocontrol.h:83
double current
The current samplerate.
Definition: controlsettings.h:29
bool sampling
true, if the oscilloscope is taking samples
Definition: hantekdsocontrol.h:208
Dso::ErrorCode setTriggerLevel(ChannelID channel, double level)
Set the trigger level.
Definition: hantekdsocontrol.cpp:310
ControlSamplerateLimits single
The limits for single channel mode.
Definition: controlspecification.h:24
void updateSamplerateLimits()
Update the minimum and maximum supported samplerate.
Definition: hantekdsocontrol.cpp:89
Dso::ErrorCode retrieveChannelLevelData()
Definition: hantekdsocontrol.cpp:377
Dso::ErrorCode setChannelInverted(ChannelID channel, bool inverted)
Enables/disables inverting of the given channel.
Definition: hantekdsocontrol.cpp:226
The DsoControl abstraction layer for Hantek USB DSOs. TODO Please anyone, refactor this class into sm...
Definition: hantekdsocontrol.h:31
void applySettings(DsoSettingsScope *scope)
Initializes the device with the current settings.
Definition: hantekdsocontrol.cpp:341
const Dso::ControlSpecification * specification
The specifications of the device.
Definition: hantekdsocontrol.h:211
#define SAMPLESIZE_USED
Definition: viewconstants.h:5
const ControlSamplerateLimits * limits
The samplerate limits.
Definition: controlsettings.h:27
bool channelSetupChanged
Definition: hantekdsocontrol.h:222