OpenHantek
hantekdsocontrol.h
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 "states.h"
13 #include "utils/printutils.h"
14 
15 #include "hantekprotocol/bulkStructs.h"
16 #include "hantekprotocol/controlStructs.h"
17 #include "hantekprotocol/definitions.h"
18 
19 #include <vector>
20 
21 #include <QMutex>
22 #include <QStringList>
23 #include <QThread>
24 #include <QTimer>
25 
26 class USBDevice;
27 
30 class HantekDsoControl : public QObject {
31  Q_OBJECT
32 
33  public:
42  HantekDsoControl(USBDevice *device);
43 
46 
51  void run();
52 
55  unsigned getChannelCount() const;
56 
60 
63  const std::vector<unsigned> &getAvailableRecordLengths() const;
64 
67  double getMinSamplerate() const;
68 
71  double getMaxSamplerate() const;
72 
73  bool isSampling() const;
74 
76  const USBDevice *getDevice() const;
77 
80  int getConnectionSpeed() const;
81 
84  int getPacketSize() const;
85 
87  const DSOsamples &getLastSamples();
88 
100  Dso::ErrorCode stringCommand(const QString &commandString);
101 
102  void addCommand(BulkCommand *newCommand, bool pending = true);
103  template <class T> T *modifyCommand(Hantek::BulkCode code) {
104  command[(uint8_t)code]->pending = true;
105  return static_cast<T *>(command[(uint8_t)code]);
106  }
107  const BulkCommand *getCommand(Hantek::BulkCode code) const;
108 
109  void addCommand(ControlCommand *newCommand, bool pending = true);
110  template <class T> T *modifyCommand(Hantek::ControlCode code) {
111  control[(uint8_t)code]->pending = true;
112  return static_cast<T *>(control[(uint8_t)code]);
113  }
114  const ControlCommand *getCommand(Hantek::ControlCode code) const;
115 
116  private:
117  bool isRollMode() const;
118  bool isFastRate() const;
119  unsigned getRecordLength() const;
120 
121  Dso::ErrorCode retrieveChannelLevelData();
122 
132  double getBestSamplerate(double samplerate, bool fastRate = false, bool maximum = false,
133  unsigned *downsampler = 0) const;
134 
138  unsigned getSampleCount() const;
139 
140  void updateInterval();
141 
145  static unsigned calculateTriggerPoint(unsigned value);
146 
149  std::pair<int, unsigned> getCaptureState() const;
150 
152  std::vector<unsigned char> getSamples(unsigned &expectedSampleCount) const;
153 
155  void convertRawDataToSamples(const std::vector<unsigned char> &rawData);
156 
160  unsigned updateRecordLength(RecordLengthID size);
161 
167  unsigned updateSamplerate(unsigned downsampler, bool fastRate);
168 
170  void restoreTargets();
171 
173  void updateSamplerateLimits();
174 
175  private:
177  BulkCommand *command[255] = {0};
178  BulkCommand *firstBulkCommand = nullptr;
179  ControlCommand *control[255] = {0};
180  ControlCommand *firstControlCommand = nullptr;
181 
182  // Communication with device
183  USBDevice *device;
184  bool sampling = false;
185 
186  // Device setup
187  const Dso::ControlSpecification *specification;
188  Dso::ControlSettings controlsettings;
189 
190  // Results
191  DSOsamples result;
192  unsigned expectedSampleCount = 0;
193 
195  // State of the communication thread
196  int captureState = Hantek::CAPTURE_WAITING;
197  Hantek::RollState rollState = Hantek::RollState::STARTSAMPLING;
198  bool _samplingStarted = false;
199  Dso::TriggerMode lastTriggerMode = (Dso::TriggerMode)-1;
200  int cycleCounter = 0;
201  int startCycle = 0;
202  int cycleTime = 0;
203 
208  int bulkCommand(const std::vector<unsigned char> *command, int attempts = HANTEK_ATTEMPTS) const;
209 
210  public slots:
214  void enableSampling(bool enabled);
215 
219  Dso::ErrorCode setRecordLength(unsigned size);
224  Dso::ErrorCode setSamplerate(double samplerate = 0.0);
229  Dso::ErrorCode setRecordTime(double duration = 0.0);
230 
235  Dso::ErrorCode setChannelUsed(ChannelID channel, bool used);
240  Dso::ErrorCode setCoupling(ChannelID channel, Dso::Coupling coupling);
246  Dso::ErrorCode setGain(ChannelID channel, double gain);
251  Dso::ErrorCode setOffset(ChannelID channel, const double offset);
252 
255  Dso::ErrorCode setTriggerMode(Dso::TriggerMode mode);
260  Dso::ErrorCode setTriggerSource(bool special, unsigned id);
265  Dso::ErrorCode setTriggerLevel(ChannelID channel, double level);
269  Dso::ErrorCode setTriggerSlope(Dso::Slope slope);
273  Dso::ErrorCode setPretriggerPosition(double position);
274  void forceTrigger();
275 
276  signals:
277  void samplingStatusChanged(bool enabled);
278  void statusMessage(const QString &message, int timeout);
279  void samplesAvailable(const DSOsamples *samples);
280 
281  void availableRecordLengthsChanged(const std::vector<unsigned> &recordLengths);
282 
285  void samplerateLimitsChanged(double minimum, double maximum);
287  void samplerateSet(int mode, QList<double> sampleSteps);
288 
289  void recordLengthChanged(unsigned long duration);
290  void recordTimeChanged(double duration);
291  void samplerateChanged(double samplerate);
292 
293  void communicationError() const;
294 };
295 
296 Q_DECLARE_METATYPE(DSOsamples *)
Dso::ErrorCode setSamplerate(double samplerate=0.0)
Sets the samplerate of the oscilloscope.
Definition: hantekdsocontrol.cpp:613
Definition: bulkcommand.h:12
Dso::ErrorCode setTriggerSlope(Dso::Slope slope)
Set the trigger slope.
Definition: hantekdsocontrol.cpp:939
Dso::ErrorCode setCoupling(ChannelID channel, Dso::Coupling coupling)
Set the coupling for the given channel.
Definition: hantekdsocontrol.cpp:768
void enableSampling(bool enabled)
If sampling is disabled, no samplesAvailable() signals are send anymore, no samples are fetched from ...
Definition: hantekdsocontrol.cpp:25
void recordTimeChanged(double duration)
The record time duration has changed.
void availableRecordLengthsChanged(const std::vector< unsigned > &recordLengths)
Definition: controlcommand.h:12
const DSOsamples & getLastSamples()
Return the last sample set.
Definition: hantekdsocontrol.cpp:40
Dso::ErrorCode setPretriggerPosition(double position)
Set the trigger position.
Definition: hantekdsocontrol.cpp:968
const std::vector< unsigned > & getAvailableRecordLengths() const
Get available record lengths for this oscilloscope.
Definition: hantekdsocontrol.cpp:85
Dso::ErrorCode setOffset(ChannelID channel, const double offset)
Set the offset for the given channel. Get the actual offset for the channel from controlsettings.voltage[channel].offsetReal.
Definition: hantekdsocontrol.cpp:818
Definition: dsosamples.h:10
Dso::ErrorCode setChannelUsed(ChannelID channel, bool used)
Enables/disables filtering of the given channel.
Definition: hantekdsocontrol.cpp:711
int getConnectionSpeed() const
Gets the speed of the connection.
Definition: hantekdsocontrol.cpp:1311
~HantekDsoControl()
Cleans up.
Definition: hantekdsocontrol.cpp:57
double getMinSamplerate() const
Get minimum samplerate for this oscilloscope.
Definition: hantekdsocontrol.cpp:89
HantekDsoControl(USBDevice *device)
Definition: hantekdsocontrol.cpp:42
Dso::ErrorCode setTriggerMode(Dso::TriggerMode mode)
Set the trigger mode.
Definition: hantekdsocontrol.cpp:845
Dso::ErrorCode setGain(ChannelID channel, double gain)
Sets the gain for the given channel. Get the actual gain by specification.gainSteps[gainId].
Definition: hantekdsocontrol.cpp:782
Dso::ErrorCode setRecordLength(unsigned size)
Sets the size of the oscilloscopes sample buffer.
Definition: hantekdsocontrol.cpp:601
void samplerateSet(int mode, QList< double > sampleSteps)
The available samplerate for fixed samplerate devices has changed.
void run()
Definition: hantekdsocontrol.cpp:1064
Dso::ErrorCode setRecordTime(double duration=0.0)
Sets the time duration of one aquisition by adapting the samplerate.
Definition: hantekdsocontrol.cpp:658
void samplingStatusChanged(bool enabled)
The oscilloscope started/stopped sampling/waiting for trigger.
const USBDevice * getDevice() const
Return the associated usb device.
Definition: hantekdsocontrol.cpp:38
Stores the specifications of the currently connected device.
Definition: controlspecification.h:47
void samplesAvailable(const DSOsamples *samples)
New sample data is available.
Dso::ErrorCode stringCommand(const QString &commandString)
Sends bulk/control commands directly.
Definition: hantekdsocontrol.cpp:1017
void samplerateLimitsChanged(double minimum, double maximum)
The available samplerate range has changed.
void statusMessage(const QString &message, int timeout)
Status message about the oscilloscope.
Stores the current settings of the device.
Definition: controlsettings.h:51
This class handles the USB communication with an usb device that has one in and one out endpoint...
Definition: usbdevice.h:24
int getPacketSize() const
Gets the maximum size of one packet transmitted via bulk transfer.
Definition: hantekdsocontrol.cpp:1320
const Dso::ControlSettings * getDeviceSettings() const
Definition: hantekdsocontrol.cpp:83
void recordLengthChanged(unsigned long duration)
The record length has changed.
void samplerateChanged(double samplerate)
The samplerate has changed.
double getMaxSamplerate() const
Get maximum samplerate for this oscilloscope.
Definition: hantekdsocontrol.cpp:93
Dso::ErrorCode setTriggerSource(bool special, unsigned id)
Set the trigger source.
Definition: hantekdsocontrol.cpp:852
Dso::ErrorCode setTriggerLevel(ChannelID channel, double level)
Set the trigger level.
Definition: hantekdsocontrol.cpp:898
unsigned getChannelCount() const
Gets the physical channel count for this oscilloscope.
Definition: hantekdsocontrol.cpp:81
The DsoControl abstraction layer for Hantek USB DSOs. TODO Please anyone, refactor this class into sm...
Definition: hantekdsocontrol.h:30