control.cpp
52.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
////////////////////////////////////////////////////////////////////////////////
//
// OpenHantek
// hantek/control.cpp
//
// Copyright (C) 2008, 2009 Oleg Khudyakov
// prcoder@potrebitel.ru
// Copyright (C) 2010, 2011 Oliver Haag
// oliver.haag@gmail.com
//
// This program is free software: you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by the Free
// Software Foundation, either version 3 of the License, or (at your option)
// any later version.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
// more details.
//
// You should have received a copy of the GNU General Public License along with
// this program. If not, see <http://www.gnu.org/licenses/>.
//
////////////////////////////////////////////////////////////////////////////////
#include <limits>
#include <QList>
#include <QMutex>
#include "hantek/control.h"
#include "helper.h"
#include "hantek/device.h"
#include "hantek/types.h"
namespace Hantek {
/// \brief Initializes the command buffers and lists.
/// \param parent The parent widget.
Control::Control(QObject *parent) : DsoControl(parent) {
// Use DSO-2090 specification as default
this->specification.command.bulk.setRecordLength = (BulkCode) -1;
this->specification.command.bulk.setFilter = (BulkCode) -1;
this->specification.command.bulk.setGain = (BulkCode) -1;
this->specification.command.bulk.setSamplerate = (BulkCode) -1;
this->specification.command.bulk.setTrigger = (BulkCode) -1;
this->specification.command.bulk.setPretrigger = (BulkCode) -1;
this->specification.command.control.setOffset = (ControlCode) -1;
this->specification.command.control.setRelays = (ControlCode) -1;
this->specification.command.values.offsetLimits = (ControlValue) -1;
this->specification.command.values.voltageLimits = (ControlValue) -1;
this->specification.recordLengths << 0;
this->specification.samplerate.single.base = 50e6;
this->specification.samplerate.single.max = 50e6;
this->specification.samplerate.multi.base = 100e6;
this->specification.samplerate.multi.max = 100e6;
for(unsigned int channel = 0; channel < HANTEK_CHANNELS; ++channel) {
for(unsigned int gainId = 0; gainId < 9; ++gainId) {
this->specification.offsetLimit[channel][gainId][OFFSET_START] = 0x0000;
this->specification.offsetLimit[channel][gainId][OFFSET_END] = 0xffff;
}
}
// Set settings to default values
this->settings.samplerate.limits = &(this->specification.samplerate.single);
this->settings.samplerate.downsampling = 1;
this->settings.samplerate.current = 0.0;
this->settings.trigger.position = 0;
this->settings.trigger.point = 0;
this->settings.trigger.mode = Dso::TRIGGERMODE_NORMAL;
this->settings.trigger.slope = Dso::SLOPE_POSITIVE;
this->settings.trigger.special = false;
this->settings.trigger.source = 0;
for(unsigned int channel = 0; channel < HANTEK_CHANNELS; ++channel) {
this->settings.trigger.level[channel] = 0.0;
this->settings.voltage[channel].gain = 0;
this->settings.voltage[channel].offset = 0.0;
this->settings.voltage[channel].offsetReal = 0.0;
this->settings.voltage[channel].used = false;
}
this->settings.recordLengthId = 0;
this->settings.usedChannels = 0;
// Special trigger sources
this->specialTriggerSources << tr("EXT") << tr("EXT/10");
// Instantiate bulk command later, some are not the same for all models
for(int command = 0; command < BULK_COUNT; ++command) {
this->command[command] = 0;
this->commandPending[command] = false;
}
// Transmission-ready control commands
this->control[CONTROLINDEX_SETOFFSET] = new ControlSetOffset();
this->controlCode[CONTROLINDEX_SETOFFSET] = CONTROL_SETOFFSET;
this->control[CONTROLINDEX_SETRELAYS] = new ControlSetRelays();
this->controlCode[CONTROLINDEX_SETRELAYS] = CONTROL_SETRELAYS;
for(int control = 0; control < CONTROLINDEX_COUNT; ++control)
this->controlPending[control] = false;
// USB device
this->device = new Device(this);
// Sample buffers
for(unsigned int channel = 0; channel < HANTEK_CHANNELS; ++channel) {
this->samples.append(0);
this->samplesSize.append(0);
}
connect(this->device, SIGNAL(disconnected()), this, SLOT(disconnectDevice()));
}
/// \brief Disconnects the device.
Control::~Control() {
this->device->disconnect();
// Clean up commands
for(int command = 0; command < BULK_COUNT; ++command) {
if(this->command[command])
delete this->command[command];
}
}
/// \brief Gets the physical channel count for this oscilloscope.
/// \return The number of physical channels.
unsigned int Control::getChannelCount() {
return HANTEK_CHANNELS;
}
/// \brief Handles all USB things until the device gets disconnected.
void Control::run() {
int errorCode, cycleCounter = 0, startCycle = 0;
// The control loop is running until the device is disconnected
int captureState = CAPTURE_WAITING;
bool samplingStarted = false;
Dso::TriggerMode lastTriggerMode = (Dso::TriggerMode) -1;
while(captureState != LIBUSB_ERROR_NO_DEVICE && !this->terminate) {
// Send all pending bulk commands
for(int command = 0; command < BULK_COUNT; ++command) {
if(!this->commandPending[command])
continue;
#ifdef DEBUG
qDebug("Sending bulk command:%s", Helper::hexDump(this->command[command]->data(), this->command[command]->getSize()).toLocal8Bit().data());
#endif
errorCode = this->device->bulkCommand(this->command[command]);
if(errorCode < 0) {
qWarning("Sending bulk command %02x failed: %s", command, Helper::libUsbErrorString(errorCode).toLocal8Bit().data());
if(errorCode == LIBUSB_ERROR_NO_DEVICE) {
captureState = LIBUSB_ERROR_NO_DEVICE;
break;
}
}
else
this->commandPending[command] = false;
}
if(captureState == LIBUSB_ERROR_NO_DEVICE)
break;
// Send all pending control commands
for(int control = 0; control < CONTROLINDEX_COUNT; ++control) {
if(!this->controlPending[control])
continue;
#ifdef DEBUG
qDebug("Sending control command %02x:%s", this->controlCode[control], Helper::hexDump(this->control[control]->data(), this->control[control]->getSize()).toLocal8Bit().data());
#endif
errorCode = this->device->controlWrite(this->controlCode[control], this->control[control]->data(), this->control[control]->getSize());
if(errorCode < 0) {
qWarning("Sending control command %2x failed: %s", this->controlCode[control], Helper::libUsbErrorString(errorCode).toLocal8Bit().data());
if(errorCode == LIBUSB_ERROR_NO_DEVICE) {
captureState = LIBUSB_ERROR_NO_DEVICE;
break;
}
}
else
this->controlPending[control] = false;
}
if(captureState == LIBUSB_ERROR_NO_DEVICE)
break;
// Check the current oscilloscope state everytime 25% of the time the buffer should be refilled
// Not more often than every 10 ms though
int cycleTime = qMax((unsigned long int) (this->specification.recordLengths[this->settings.recordLengthId] / this->settings.samplerate.current * 250), 10lu);
this->msleep(cycleTime);
if(!this->sampling) {
samplingStarted = false;
continue;
}
#ifdef DEBUG
int lastCaptureState = captureState;
#endif
captureState = this->getCaptureState();
if(captureState < 0)
qWarning("Getting capture state failed: %s", Helper::libUsbErrorString(captureState).toLocal8Bit().data());
#ifdef DEBUG
else if(captureState != lastCaptureState)
qDebug("Capture state changed to %d", captureState);
#endif
switch(captureState) {
case CAPTURE_READY:
case CAPTURE_READY2250:
case CAPTURE_READY5200:
// Get data and process it, if we're still sampling
errorCode = this->getSamples(samplingStarted);
if(errorCode < 0)
qWarning("Getting sample data failed: %s", Helper::libUsbErrorString(errorCode).toLocal8Bit().data());
#ifdef DEBUG
else
qDebug("Received %d B of sampling data", errorCode);
#endif
// Check if we're in single trigger mode
if(this->settings.trigger.mode == Dso::TRIGGERMODE_SINGLE && samplingStarted)
this->stopSampling();
// Sampling completed, restart it when necessary
samplingStarted = false;
// Start next capture if necessary by leaving out the break statement
if(!this->sampling)
break;
case CAPTURE_WAITING:
if(samplingStarted && lastTriggerMode == this->settings.trigger.mode) {
++cycleCounter;
if(cycleCounter == startCycle) {
// Buffer refilled completely since start of sampling, enable the trigger now
errorCode = this->device->bulkCommand(this->command[BULK_ENABLETRIGGER]);
if(errorCode < 0) {
if(errorCode == LIBUSB_ERROR_NO_DEVICE)
captureState = LIBUSB_ERROR_NO_DEVICE;
break;
}
#ifdef DEBUG
qDebug("Enabling trigger");
#endif
}
else if(cycleCounter >= 8 + startCycle && this->settings.trigger.mode == Dso::TRIGGERMODE_AUTO) {
// Force triggering
errorCode = this->device->bulkCommand(this->command[BULK_FORCETRIGGER]);
if(errorCode == LIBUSB_ERROR_NO_DEVICE)
captureState = LIBUSB_ERROR_NO_DEVICE;
#ifdef DEBUG
qDebug("Forcing trigger");
#endif
}
if(cycleCounter < 20 || cycleCounter < 4000 / cycleTime)
break;
}
// Start capturing
errorCode = this->device->bulkCommand(this->command[BULK_STARTSAMPLING]);
if(errorCode < 0) {
if(errorCode == LIBUSB_ERROR_NO_DEVICE)
captureState = LIBUSB_ERROR_NO_DEVICE;
break;
}
#ifdef DEBUG
qDebug("Starting to capture");
#endif
samplingStarted = true;
cycleCounter = 0;
startCycle = this->settings.trigger.position * 1000 / cycleTime + 1;
lastTriggerMode = this->settings.trigger.mode;
break;
case CAPTURE_SAMPLING:
break;
default:
break;
}
}
this->device->disconnect();
emit statusMessage(tr("The device has been disconnected"), 0);
}
/// \brief Calculates the trigger point from the CommandGetCaptureState data.
/// \param value The data value that contains the trigger point.
/// \return The calculated trigger point for the given data.
unsigned long int Control::calculateTriggerPoint(unsigned long int value) {
unsigned long int result = value;
// Each set bit inverts all bits with a lower value
for(unsigned long int bitValue = 1; bitValue; bitValue <<= 1)
if(result & bitValue)
result ^= bitValue - 1;
return result;
}
/// \brief Gets the current state.
/// \return The current CaptureState of the oscilloscope, libusb error code on error.
int Control::getCaptureState() {
int errorCode;
errorCode = this->device->bulkCommand(this->command[BULK_GETCAPTURESTATE], 1);
if(errorCode < 0)
return errorCode;
BulkResponseGetCaptureState response;
errorCode = this->device->bulkRead(response.data(), response.getSize());
if(errorCode < 0)
return errorCode;
this->settings.trigger.point = this->calculateTriggerPoint(response.getTriggerPoint());
return (int) response.getCaptureState();
}
/// \brief Gets sample data from the oscilloscope and converts it.
/// \return sample count on success, libusb error code on error.
int Control::getSamples(bool process) {
int errorCode;
// Request data
errorCode = this->device->bulkCommand(this->command[BULK_GETDATA], 1);
if(errorCode < 0)
return errorCode;
// Save raw data to temporary buffer
unsigned int dataCount = this->specification.recordLengths[this->settings.recordLengthId] * HANTEK_CHANNELS;
unsigned int dataLength = dataCount;
if(this->specification.sampleSize > 8)
dataLength *= 2;
unsigned char data[dataLength];
errorCode = this->device->bulkReadMulti(data, dataLength);
if(errorCode < 0)
return errorCode;
// Process the data only if we want it
if(process) {
// How much data did we really receive?
dataLength = errorCode;
if(this->specification.sampleSize > 8)
dataCount = dataLength / 2;
else
dataCount = dataLength;
this->samplesMutex.lock();
// Convert channel data
if(this->settings.samplerate.limits == &this->specification.samplerate.multi) {
// Fast rate mode, one channel is using all buffers
int channel = 0;
for(; channel < HANTEK_CHANNELS; ++channel) {
if(this->settings.voltage[0].used)
break;
}
// Clear unused channels
for(int channelCounter = 0; channelCounter < HANTEK_CHANNELS; ++channelCounter)
if(channelCounter != channel && this->samples[channelCounter]) {
delete this->samples[channelCounter];
this->samples[channelCounter] = 0;
}
if(channel < HANTEK_CHANNELS) {
// Reallocate memory for samples if the sample count has changed
if(!this->samples[channel] || this->samplesSize[channel] != dataCount) {
if(this->samples[channel])
delete this->samples[channel];
this->samples[channel] = new double[dataCount];
this->samplesSize[channel] = dataCount;
}
// Convert data from the oscilloscope and write it into the sample buffer
unsigned int bufferPosition = (this->settings.trigger.point + 1) * 2;
if(this->specification.sampleSize > 8) {
// Additional most significant bits after the normal data
unsigned int extraBitsPosition; // Track the position of the extra bits in the additional byte
unsigned int extraBitsSize = this->specification.sampleSize - 8; // Number of extra bits
unsigned short int extraBitsMask = (0x00ff << extraBitsSize) & 0xff00; // Mask for extra bits extraction
for(unsigned int realPosition = 0; realPosition < dataCount; ++realPosition, ++bufferPosition) {
if(bufferPosition >= dataCount)
bufferPosition %= dataCount;
extraBitsPosition = bufferPosition % HANTEK_CHANNELS;
this->samples[channel][realPosition] = ((double) ((unsigned short int) data[bufferPosition] + (((unsigned short int) data[dataCount + bufferPosition - extraBitsPosition] << (8 - (HANTEK_CHANNELS - 1 - extraBitsPosition) * extraBitsSize)) & extraBitsMask)) / this->specification.voltageLimit[channel][this->settings.voltage[channel].gain] - this->settings.voltage[channel].offsetReal) * this->specification.gainSteps[this->settings.voltage[channel].gain];
}
}
else {
for(unsigned int realPosition = 0; realPosition < dataCount; ++realPosition, ++bufferPosition) {
if(bufferPosition >= dataCount)
bufferPosition %= dataCount;
this->samples[channel][realPosition] = ((double) data[bufferPosition] / this->specification.voltageLimit[channel][this->settings.voltage[channel].gain] - this->settings.voltage[channel].offsetReal) * this->specification.gainSteps[this->settings.voltage[channel].gain];
}
}
}
}
else {
// Normal mode, channels are using their separate buffers
unsigned int channelDataCount = dataCount / HANTEK_CHANNELS;
for(int channel = 0; channel < HANTEK_CHANNELS; ++channel) {
if(this->settings.voltage[channel].used) {
// Reallocate memory for samples if the sample count has changed
if(!this->samples[channel] || this->samplesSize[channel] != channelDataCount) {
if(this->samples[channel])
delete this->samples[channel];
this->samples[channel] = new double[channelDataCount];
this->samplesSize[channel] = channelDataCount;
}
// Convert data from the oscilloscope and write it into the sample buffer
unsigned int bufferPosition = (this->settings.trigger.point + 1) * 2;
if(this->specification.sampleSize > 8) {
// Additional most significant bits after the normal data
unsigned int extraBitsSize = this->specification.sampleSize - 8; // Number of extra bits
unsigned short int extraBitsMask = (0x00ff << extraBitsSize) & 0xff00; // Mask for extra bits extraction
unsigned int extraBitsIndex = 8 - channel * 2; // Bit position offset for extra bits extraction
for(unsigned int realPosition = 0; realPosition < channelDataCount; ++realPosition, bufferPosition += 2) {
if(bufferPosition >= dataCount)
bufferPosition %= dataCount;
this->samples[channel][realPosition] = ((double) ((unsigned short int) data[bufferPosition + HANTEK_CHANNELS - 1 - channel] + (((unsigned short int) data[dataCount + bufferPosition] << extraBitsIndex) & extraBitsMask)) / this->specification.voltageLimit[channel][this->settings.voltage[channel].gain] - this->settings.voltage[channel].offsetReal) * this->specification.gainSteps[this->settings.voltage[channel].gain];
}
}
else {
for(unsigned int realPosition = 0; realPosition < channelDataCount; ++realPosition, bufferPosition += 2) {
if(bufferPosition >= dataCount)
bufferPosition %= dataCount;
this->samples[channel][realPosition] = ((double) data[bufferPosition + HANTEK_CHANNELS - 1 - channel] / this->specification.voltageLimit[channel][this->settings.voltage[channel].gain] - this->settings.voltage[channel].offsetReal) * this->specification.gainSteps[this->settings.voltage[channel].gain];
}
}
}
else if(this->samples[channel]) {
// Clear unused channels
delete this->samples[channel];
this->samples[channel] = 0;
this->samplesSize[channel] = 0;
}
}
}
this->samplesMutex.unlock();
emit samplesAvailable(&(this->samples), &(this->samplesSize), this->settings.samplerate.current, &(this->samplesMutex));
}
return errorCode;
}
/// \brief Sets the size of the sample buffer without updating dependencies.
/// \param size The record length that should be met (S).
/// \return The record length that has been set, 0 on error.
unsigned long int Control::updateRecordLength(unsigned long int size) {
// Get the record length supporting the highest samplerate while meeting the requirement
int bestSizeId = -1;
for(int sizeId = 0; sizeId < this->specification.recordLengths.count(); ++sizeId) {
if(this->specification.recordLengths[sizeId] >= size) {
// We meet the size-requirement, check if we provide the highest possible samplerate
if(bestSizeId == -1 || this->specification.recordLengths[bestSizeId] < size || this->specification.bufferDividers[sizeId] < this->specification.bufferDividers[bestSizeId])
bestSizeId = sizeId;
}
else {
// We don't meet the size-requirement, but maybe we're still the one coming closest
if(bestSizeId == -1 || this->specification.recordLengths[sizeId] > this->specification.recordLengths[bestSizeId])
bestSizeId = sizeId;
}
}
switch(this->specification.command.bulk.setRecordLength) {
case BULK_SETTRIGGERANDSAMPLERATE:
// SetTriggerAndSamplerate bulk command for record length
static_cast<BulkSetTriggerAndSamplerate *>(this->command[BULK_SETTRIGGERANDSAMPLERATE])->setRecordLength(bestSizeId);
this->commandPending[BULK_SETTRIGGERANDSAMPLERATE] = true;
break;
case BULK_DSETBUFFER:
if(this->specification.command.bulk.setPretrigger == BULK_FSETBUFFER) {
// Pointers to needed commands
BulkSetRecordLength2250 *commandSetRecordLength2250 = static_cast<BulkSetRecordLength2250 *>(this->command[BULK_DSETBUFFER]);
commandSetRecordLength2250->setRecordLength(bestSizeId);
}
else {
// SetBuffer5200 bulk command for record length
BulkSetBuffer5200 *commandSetBuffer5200 = static_cast<BulkSetBuffer5200 *>(this->command[BULK_DSETBUFFER]);
commandSetBuffer5200->setUsedPre(DTRIGGERPOSITION_ON);
commandSetBuffer5200->setUsedPost(DTRIGGERPOSITION_ON);
commandSetBuffer5200->setRecordLength(bestSizeId);
}
this->commandPending[BULK_DSETBUFFER] = true;
break;
default:
return 0;
}
this->settings.recordLengthId = bestSizeId;
return this->specification.recordLengths[this->settings.recordLengthId];
}
/// \brief Try to connect to the oscilloscope.
void Control::connectDevice() {
int errorCode;
emit statusMessage(this->device->search(), 0);
if(!this->device->isConnected())
return;
// Clean up commands and their pending state
for(int command = 0; command < BULK_COUNT; ++command) {
if(this->command[command])
delete this->command[command];
this->commandPending[command] = false;
}
// Instantiate the commands needed for all models
this->command[BULK_FORCETRIGGER] = new BulkForceTrigger();
this->command[BULK_STARTSAMPLING] = new BulkCaptureStart();
this->command[BULK_ENABLETRIGGER] = new BulkTriggerEnabled();
this->command[BULK_GETDATA] = new BulkGetData();
this->command[BULK_GETCAPTURESTATE] = new BulkGetCaptureState();
this->command[BULK_SETGAIN] = new BulkSetGain();
// Initialize the command versions to the ones used on the DSO-2090
this->specification.command.bulk.setRecordLength = (BulkCode) -1;
this->specification.command.bulk.setFilter = (BulkCode) -1;
this->specification.command.bulk.setGain = BULK_SETGAIN;
this->specification.command.bulk.setSamplerate = (BulkCode) -1;
this->specification.command.bulk.setTrigger = (BulkCode) -1;
this->specification.command.bulk.setPretrigger = (BulkCode) -1;
this->specification.command.control.setOffset = CONTROL_SETOFFSET;
this->specification.command.control.setRelays = CONTROL_SETRELAYS;
this->specification.command.values.offsetLimits = VALUE_OFFSETLIMITS;
this->specification.command.values.voltageLimits = (ControlValue) -1;
// Determine the command version we need for this model
bool unsupported = false;
switch(this->device->getModel()) {
case MODEL_DSO2150:
unsupported = true;
case MODEL_DSO2090:
// Instantiate additional commands for the DSO-2090
this->command[BULK_SETFILTER] = new BulkSetFilter();
this->command[BULK_SETTRIGGERANDSAMPLERATE] = new BulkSetTriggerAndSamplerate();
this->specification.command.bulk.setRecordLength = BULK_SETTRIGGERANDSAMPLERATE;
this->specification.command.bulk.setFilter = BULK_SETFILTER;
this->specification.command.bulk.setSamplerate = BULK_SETTRIGGERANDSAMPLERATE;
this->specification.command.bulk.setTrigger = BULK_SETTRIGGERANDSAMPLERATE;
this->specification.command.bulk.setPretrigger = BULK_SETTRIGGERANDSAMPLERATE;
// Initialize those as pending
this->commandPending[BULK_SETFILTER] = true;
this->commandPending[BULK_SETTRIGGERANDSAMPLERATE] = true;
break;
case MODEL_DSO2250:
// Instantiate additional commands for the DSO-2250
this->command[BULK_BSETFILTER] = new BulkSetFilter2250();
this->command[BULK_CSETTRIGGERORSAMPLERATE] = new BulkSetTrigger2250();
this->command[BULK_DSETBUFFER] = new BulkSetRecordLength2250();
this->command[BULK_ESETTRIGGERORSAMPLERATE] = new BulkSetSamplerate2250();
this->command[BULK_FSETBUFFER] = new BulkSetBuffer2250();
this->specification.command.bulk.setRecordLength = BULK_DSETBUFFER;
this->specification.command.bulk.setFilter = BULK_BSETFILTER;
this->specification.command.bulk.setSamplerate = BULK_ESETTRIGGERORSAMPLERATE;
this->specification.command.bulk.setTrigger = BULK_CSETTRIGGERORSAMPLERATE;
this->specification.command.bulk.setPretrigger = BULK_FSETBUFFER;
this->commandPending[BULK_BSETFILTER] = true;
this->commandPending[BULK_CSETTRIGGERORSAMPLERATE] = true;
this->commandPending[BULK_DSETBUFFER] = true;
this->commandPending[BULK_ESETTRIGGERORSAMPLERATE] = true;
this->commandPending[BULK_FSETBUFFER] = true;
break;
case MODEL_DSO5200A:
unsupported = true;
case MODEL_DSO5200:
// Instantiate additional commands for the DSO-5200
this->command[BULK_CSETTRIGGERORSAMPLERATE] = new BulkSetSamplerate5200();
this->command[BULK_DSETBUFFER] = new BulkSetBuffer5200();
this->command[BULK_ESETTRIGGERORSAMPLERATE] = new BulkSetTrigger5200();
this->specification.command.bulk.setRecordLength = BULK_DSETBUFFER;
this->specification.command.bulk.setSamplerate = BULK_CSETTRIGGERORSAMPLERATE;
this->specification.command.bulk.setTrigger = BULK_ESETTRIGGERORSAMPLERATE;
this->specification.command.bulk.setPretrigger = BULK_ESETTRIGGERORSAMPLERATE;
//this->specification.command.values.voltageLimits = VALUE_ETSCORRECTION;
this->commandPending[BULK_CSETTRIGGERORSAMPLERATE] = true;
this->commandPending[BULK_DSETBUFFER] = true;
this->commandPending[BULK_ESETTRIGGERORSAMPLERATE] = true;
break;
default:
this->device->disconnect();
emit statusMessage(tr("Unknown model"), 0);
return;
}
if(unsupported)
qWarning("Warning: This Hantek DSO model isn't supported officially, so it may not be working as expected. Reports about your experiences are very welcome though (Please open a feature request in the tracker at https://sf.net/projects/openhantek/ or email me directly to oliver.haag@gmail.com). If it's working perfectly I can remove this warning, if not it should be possible to get it working with your help soon.");
for(int control = 0; control < CONTROLINDEX_COUNT; ++control)
this->controlPending[control] = true;
// Maximum possible samplerate for a single channel and dividers for record lengths
this->specification.bufferDividers.clear();
this->specification.recordLengths.clear();
this->specification.gainSteps.clear();
for(int channel = 0; channel < HANTEK_CHANNELS; ++channel)
this->specification.voltageLimit[channel].clear();
switch(this->device->getModel()) {
case MODEL_DSO5200:
case MODEL_DSO5200A:
this->specification.samplerate.single.base = 100e6;
this->specification.samplerate.single.max = 125e6;
this->specification.samplerate.multi.base = 200e6;
this->specification.samplerate.multi.max = 250e6;
this->specification.bufferDividers << 1000 << 1 << 1;
this->specification.recordLengths << ULONG_MAX << 10240 << 14336;
this->specification.gainSteps
<< 0.16 << 0.40 << 0.80 << 1.60 << 4.00 << 8.0 << 16.0 << 40.0 << 80.0;
/// \todo Use calibration data to get the DSO-5200(A) sample ranges
for(int channel = 0; channel < HANTEK_CHANNELS; ++channel)
this->specification.voltageLimit[channel]
<< 368 << 454 << 908 << 368 << 454 << 908 << 368 << 454 << 908;
this->specification.gainIndex
<< 1 << 0 << 0 << 1 << 0 << 0 << 1 << 0 << 0;
this->specification.sampleSize = 10;
break;
case MODEL_DSO2250:
this->specification.samplerate.single.base = 100e6;
this->specification.samplerate.single.max = 100e6;
this->specification.samplerate.multi.base = 200e6;
this->specification.samplerate.multi.max = 250e6;
this->specification.bufferDividers << 1000 << 1 << 1;
this->specification.recordLengths << ULONG_MAX << 10240 << 524288;
this->specification.gainSteps
<< 0.08 << 0.16 << 0.40 << 0.80 << 1.60 << 4.00 << 8.0 << 16.0 << 40.0;
for(int channel = 0; channel < HANTEK_CHANNELS; ++channel)
this->specification.voltageLimit[channel]
<< 255 << 255 << 255 << 255 << 255 << 255 << 255 << 255 << 255;
this->specification.gainIndex
<< 0 << 2 << 3 << 0 << 2 << 3 << 0 << 2 << 3;
this->specification.sampleSize = 8;
break;
case MODEL_DSO2150:
this->specification.samplerate.single.base = 50e6;
this->specification.samplerate.single.max = 75e6;
this->specification.samplerate.multi.base = 100e6;
this->specification.samplerate.multi.max = 150e6;
this->specification.bufferDividers << 1000 << 1 << 1;
this->specification.recordLengths << ULONG_MAX << 10240 << 32768;
this->specification.gainSteps
<< 0.08 << 0.16 << 0.40 << 0.80 << 1.60 << 4.00 << 8.0 << 16.0 << 40.0;
for(int channel = 0; channel < HANTEK_CHANNELS; ++channel)
this->specification.voltageLimit[channel]
<< 255 << 255 << 255 << 255 << 255 << 255 << 255 << 255 << 255;
this->specification.gainIndex
<< 0 << 1 << 2 << 0 << 1 << 2 << 0 << 1 << 2;
this->specification.sampleSize = 8;
break;
default:
this->specification.samplerate.single.base = 50e6;
this->specification.samplerate.single.max = 50e6;
this->specification.samplerate.multi.base = 100e6;
this->specification.samplerate.multi.max = 100e6;
this->specification.bufferDividers << 1000 << 1 << 1;
this->specification.recordLengths << ULONG_MAX << 10240 << 32768;
this->specification.gainSteps
<< 0.08 << 0.16 << 0.40 << 0.80 << 1.60 << 4.00 << 8.0 << 16.0 << 40.0;
for(int channel = 0; channel < HANTEK_CHANNELS; ++channel)
this->specification.voltageLimit[channel]
<< 255 << 255 << 255 << 255 << 255 << 255 << 255 << 255 << 255;
this->specification.gainIndex
<< 0 << 1 << 2 << 0 << 1 << 2 << 0 << 1 << 2;
this->specification.sampleSize = 8;
break;
}
this->settings.samplerate.limits = &(this->specification.samplerate.single);
this->settings.samplerate.downsampling = 1;
// Get channel level data
errorCode = this->device->controlRead(CONTROL_VALUE, (unsigned char *) &(this->specification.offsetLimit), sizeof(this->specification.offsetLimit), (int) VALUE_OFFSETLIMITS);
if(errorCode < 0) {
this->device->disconnect();
emit statusMessage(tr("Couldn't get channel level data from oscilloscope"), 0);
return;
}
DsoControl::connectDevice();
}
/// \brief Sets the size of the oscilloscopes sample buffer.
/// \param size The record length that should be met (S).
/// \return The record length that has been set, 0 on error.
unsigned long int Control::setRecordLength(unsigned long int size) {
if(!this->device->isConnected())
return 0;
this->updateRecordLength(size);
this->setSamplerate();
this->setPretriggerPosition(this->settings.trigger.position);
return this->specification.recordLengths[this->settings.recordLengthId];
}
/// \brief Sets the samplerate of the oscilloscope.
/// \param samplerate The samplerate that should be met (S/s).
/// \return The samplerate that has been set, 0 on error.
unsigned long int Control::setSamplerate(unsigned long int samplerate) {
if(!this->device->isConnected())
return 0;
// Keep samplerate if no parameter was given
if(!samplerate)
samplerate = this->settings.samplerate.current;
// Abort samplerate calculation if we didn't get a valid value yet
if(!samplerate)
return samplerate;
// Calculate with fast rate first if only one channel is used
bool fastRate = false;
this->settings.samplerate.limits = &(this->specification.samplerate.single);
if(this->settings.usedChannels <= 1) {
fastRate = true;
this->settings.samplerate.limits = &(this->specification.samplerate.multi);
}
// Get downsampling factor that would provide the requested rate
this->settings.samplerate.downsampling = this->settings.samplerate.limits->base / this->specification.bufferDividers[this->settings.recordLengthId] / samplerate;
// A downsampling factor of zero will result in the maximum rate
if(this->settings.samplerate.downsampling)
this->settings.samplerate.current = this->settings.samplerate.limits->base / this->specification.bufferDividers[this->settings.recordLengthId] / this->settings.samplerate.downsampling;
else
this->settings.samplerate.current = this->settings.samplerate.limits->max / this->specification.bufferDividers[this->settings.recordLengthId];
// Maybe normal mode would be sufficient or even better than fast rate mode
if(fastRate) {
// Don't set the downsampling factor to zero (maximum rate) if we could use fast rate mode anyway
unsigned long int slowDownsampling = qMax(this->specification.samplerate.single.base / this->specification.bufferDividers[this->settings.recordLengthId] / samplerate, (long unsigned int) 1);
// Use normal mode if we need valueSlow or it would meet the rate at least as exactly as fast rate mode
if(this->settings.samplerate.downsampling > 4 || (qAbs((double) this->specification.samplerate.single.base / this->specification.bufferDividers[this->settings.recordLengthId] / slowDownsampling - samplerate) <= qAbs((double) this->settings.samplerate.current - samplerate))) {
fastRate = false;
this->settings.samplerate.limits = &(this->specification.samplerate.single);
this->settings.samplerate.downsampling = slowDownsampling;
this->settings.samplerate.current = this->specification.samplerate.single.base / this->specification.bufferDividers[this->settings.recordLengthId] / this->settings.samplerate.downsampling;
}
}
switch(this->specification.command.bulk.setSamplerate) {
case BULK_SETTRIGGERANDSAMPLERATE: {
// Split the resulting divider into the values understood by the device
// The fast value is kept at 4 (or 3) for slow sample rates
long int valueSlow = qMax(((long int) this->settings.samplerate.downsampling - 3) / 2, (long int) 0);
unsigned char valueFast = this->settings.samplerate.downsampling - valueSlow * 2;
// Pointers to needed commands
BulkSetTriggerAndSamplerate *commandSetTriggerAndSamplerate = static_cast<BulkSetTriggerAndSamplerate *>(this->command[BULK_SETTRIGGERANDSAMPLERATE]);
// Store samplerate fast value
commandSetTriggerAndSamplerate->setSamplerateFast(valueFast);
// Store samplerate slow value (two's complement)
commandSetTriggerAndSamplerate->setSamplerateSlow(valueSlow == 0 ? 0 : 0xffff - valueSlow);
// Set fast rate when used
commandSetTriggerAndSamplerate->setFastRate(fastRate);
this->commandPending[BULK_SETTRIGGERANDSAMPLERATE] = true;
break;
}
case BULK_CSETTRIGGERORSAMPLERATE: {
// Split the resulting divider into the values understood by the device
// The fast value is kept at 4 (or 3) for slow sample rates
long int valueSlow = qMax(((long int) this->settings.samplerate.downsampling - 3) / 2, (long int) 0);
unsigned char valueFast = this->settings.samplerate.downsampling - valueSlow * 2;
// Pointers to needed commands
BulkSetSamplerate5200 *commandSetSamplerate5200 = static_cast<BulkSetSamplerate5200 *>(this->command[BULK_CSETTRIGGERORSAMPLERATE]);
BulkSetTrigger5200 *commandSetTrigger5200 = static_cast<BulkSetTrigger5200 *>(this->command[BULK_ESETTRIGGERORSAMPLERATE]);
// Store samplerate fast value
commandSetSamplerate5200->setSamplerateFast(4 - valueFast);
// Store samplerate slow value (two's complement)
commandSetSamplerate5200->setSamplerateSlow(valueSlow == 0 ? 0 : 0xffff - valueSlow);
// Set fast rate when used
commandSetTrigger5200->setFastRate(fastRate);
this->commandPending[BULK_CSETTRIGGERORSAMPLERATE] = true;
this->commandPending[BULK_ESETTRIGGERORSAMPLERATE] = true;
break;
}
case BULK_ESETTRIGGERORSAMPLERATE: {
// Pointers to needed commands
BulkSetSamplerate2250 *commandSetSamplerate2250 = static_cast<BulkSetSamplerate2250 *>(this->command[BULK_ESETTRIGGERORSAMPLERATE]);
bool downsampling = this->settings.samplerate.downsampling > 1;
// Store downsampler state value
commandSetSamplerate2250->setDownsampling(downsampling);
// Store samplerate value
commandSetSamplerate2250->setSamplerate(downsampling ? 0x10001 - this->settings.samplerate.downsampling : 0);
// Set fast rate when used
commandSetSamplerate2250->setFastRate(fastRate);
this->commandPending[BULK_ESETTRIGGERORSAMPLERATE] = true;
break;
}
default:
return 0;
}
this->updateRecordLength(this->specification.recordLengths[this->settings.recordLengthId]);
this->setPretriggerPosition(this->settings.trigger.position);
return this->settings.samplerate.current;
}
/// \brief Enables/disables filtering of the given channel.
/// \param channel The channel that should be set.
/// \param used true if the channel should be sampled.
/// \return See ::Dso::ErrorCode.
int Control::setChannelUsed(unsigned int channel, bool used) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if(channel >= HANTEK_CHANNELS)
return Dso::ERROR_PARAMETER;
// Channel filtering commands
switch(this->specification.command.bulk.setFilter) {
case BULK_SETFILTER: {
// SetFilter bulk command for channel filter (used has to be inverted!)
BulkSetFilter *commandSetFilter = static_cast<BulkSetFilter *>(this->command[BULK_SETFILTER]);
commandSetFilter->setChannel(channel, !used);
this->commandPending[BULK_SETFILTER] = true;
break;
}
case BULK_BSETFILTER: {
// SetFilter2250 bulk command for channel filter (used has to be inverted!)
BulkSetFilter2250 *commandSetFilter2250 = static_cast<BulkSetFilter2250 *>(this->command[BULK_BSETFILTER]);
commandSetFilter2250->setChannel(channel, !used);
this->commandPending[BULK_BSETFILTER] = true;
break;
}
default:
return Dso::ERROR_UNSUPPORTED;
}
// Update settings
this->settings.voltage[channel].used = used;
unsigned int channelCount = 0;
for(int channelCounter = 0; channelCounter < HANTEK_CHANNELS; ++channelCounter) {
if(this->settings.voltage[channelCounter].used)
++channelCount;
}
this->settings.usedChannels = channelCount;
// Additional UsedChannels field for all models except DSO-2250
if(this->specification.command.bulk.setTrigger == BULK_SETTRIGGERANDSAMPLERATE || this->specification.command.bulk.setTrigger == BULK_ESETTRIGGERORSAMPLERATE) {
unsigned char usedChannels = USED_CH1;
if(this->settings.voltage[1].used) {
if(this->settings.voltage[0].used)
usedChannels = USED_CH1CH2;
else
usedChannels = USED_CH2;
}
switch(this->specification.command.bulk.setTrigger) {
case BULK_SETTRIGGERANDSAMPLERATE: {
// SetTriggerAndSamplerate bulk command for trigger source
static_cast<BulkSetTriggerAndSamplerate *>(this->command[BULK_SETTRIGGERANDSAMPLERATE])->setUsedChannels(usedChannels);
this->commandPending[BULK_SETTRIGGERANDSAMPLERATE] = true;
break;
}
case BULK_ESETTRIGGERORSAMPLERATE: {
// SetTrigger5200s bulk command for trigger source
static_cast<BulkSetTrigger5200 *>(this->command[BULK_ESETTRIGGERORSAMPLERATE])->setUsedChannels(usedChannels);
this->commandPending[BULK_ESETTRIGGERORSAMPLERATE] = true;
break;
}
default:
break;
}
}
return Dso::ERROR_NONE;
}
/// \brief Set the coupling for the given channel.
/// \param channel The channel that should be set.
/// \param coupling The new coupling for the channel.
/// \return See ::Dso::ErrorCode.
int Control::setCoupling(unsigned int channel, Dso::Coupling coupling) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if(channel >= HANTEK_CHANNELS)
return Dso::ERROR_PARAMETER;
// SetRelays control command for coupling relays
static_cast<ControlSetRelays *>(this->control[CONTROLINDEX_SETRELAYS])->setCoupling(channel, coupling != Dso::COUPLING_AC);
this->controlPending[CONTROLINDEX_SETRELAYS] = true;
return Dso::ERROR_NONE;
}
/// \brief Sets the gain for the given channel.
/// \param channel The channel that should be set.
/// \param gain The gain that should be met (V/div).
/// \return The gain that has been set, ::Dso::ErrorCode on error.
double Control::setGain(unsigned int channel, double gain) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if(channel >= HANTEK_CHANNELS)
return Dso::ERROR_PARAMETER;
// Find lowest gain voltage thats at least as high as the requested
int gainId;
for(gainId = 0; gainId < this->specification.gainSteps.count() - 1; ++gainId)
if(this->specification.gainSteps[gainId] >= gain)
break;
// SetGain bulk command for gain
static_cast<BulkSetGain *>(this->command[BULK_SETGAIN])->setGain(channel, this->specification.gainIndex[gainId]);
this->commandPending[BULK_SETGAIN] = true;
// SetRelays control command for gain relays
ControlSetRelays *controlSetRelays = static_cast<ControlSetRelays *>(this->control[CONTROLINDEX_SETRELAYS]);
controlSetRelays->setBelow1V(channel, gainId < 3);
controlSetRelays->setBelow100mV(channel, gainId < 6);
this->controlPending[CONTROLINDEX_SETRELAYS] = true;
this->settings.voltage[channel].gain = gainId;
this->setOffset(channel, this->settings.voltage[channel].offset);
return this->specification.gainSteps[gainId];
}
/// \brief Set the offset for the given channel.
/// \param channel The channel that should be set.
/// \param offset The new offset value (0.0 - 1.0).
/// \return The offset that has been set, ::Dso::ErrorCode on error.
double Control::setOffset(unsigned int channel, double offset) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if(channel >= HANTEK_CHANNELS)
return Dso::ERROR_PARAMETER;
// Calculate the offset value
// The range is given by the calibration data (convert from big endian)
unsigned short int minimum = ((unsigned short int) *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_START])) << 8) + *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_START]) + 1);
unsigned short int maximum = ((unsigned short int) *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_END])) << 8) + *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_END]) + 1);
unsigned short int offsetValue = offset * (maximum - minimum) + minimum + 0.5;
double offsetReal = (double) (offsetValue - minimum) / (maximum - minimum);
// SetOffset control command for channel offset
static_cast<ControlSetOffset *>(this->control[CONTROLINDEX_SETOFFSET])->setChannel(channel, offsetValue);
this->controlPending[CONTROLINDEX_SETOFFSET] = true;
this->settings.voltage[channel].offset = offset;
this->settings.voltage[channel].offsetReal = offsetReal;
this->setTriggerLevel(channel, this->settings.trigger.level[channel]);
return offsetReal;
}
/// \brief Set the trigger mode.
/// \return See ::Dso::ErrorCode.
int Control::setTriggerMode(Dso::TriggerMode mode) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if(mode < Dso::TRIGGERMODE_AUTO || mode > Dso::TRIGGERMODE_SINGLE)
return Dso::ERROR_PARAMETER;
this->settings.trigger.mode = mode;
return Dso::ERROR_NONE;
}
/// \brief Set the trigger source.
/// \param special true for a special channel (EXT, ...) as trigger source.
/// \param id The number of the channel, that should be used as trigger.
/// \return See ::Dso::ErrorCode.
int Control::setTriggerSource(bool special, unsigned int id) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if((!special && id >= HANTEK_CHANNELS) || (special && id >= HANTEK_SPECIAL_CHANNELS))
return Dso::ERROR_PARAMETER;
switch(this->specification.command.bulk.setTrigger) {
case BULK_SETTRIGGERANDSAMPLERATE:
// SetTriggerAndSamplerate bulk command for trigger source
static_cast<BulkSetTriggerAndSamplerate *>(this->command[BULK_SETTRIGGERANDSAMPLERATE])->setTriggerSource(special ? 3 + id : 1 - id);
this->commandPending[BULK_SETTRIGGERANDSAMPLERATE] = true;
break;
case BULK_CSETTRIGGERORSAMPLERATE:
// SetTrigger2250 bulk command for trigger source
static_cast<BulkSetTrigger2250 *>(this->command[BULK_CSETTRIGGERORSAMPLERATE])->setTriggerSource(special ? 0 : 2 + id);
this->commandPending[BULK_CSETTRIGGERORSAMPLERATE] = true;
break;
case BULK_ESETTRIGGERORSAMPLERATE:
// SetTrigger5200 bulk command for trigger source
static_cast<BulkSetTrigger5200 *>(this->command[BULK_ESETTRIGGERORSAMPLERATE])->setTriggerSource(special ? 3 + id : 1 - id);
this->commandPending[BULK_ESETTRIGGERORSAMPLERATE] = true;
break;
default:
return Dso::ERROR_UNSUPPORTED;
}
// SetRelays control command for external trigger relay
static_cast<ControlSetRelays *>(this->control[CONTROLINDEX_SETRELAYS])->setTrigger(special);
this->controlPending[CONTROLINDEX_SETRELAYS] = true;
this->settings.trigger.special = special;
this->settings.trigger.source = id;
// Apply trigger level of the new source
if(special) {
// SetOffset control command for changed trigger level
static_cast<ControlSetOffset *>(this->control[CONTROLINDEX_SETOFFSET])->setTrigger(0x7f);
this->controlPending[CONTROLINDEX_SETOFFSET] = true;
}
else
this->setTriggerLevel(id, this->settings.trigger.level[id]);
return Dso::ERROR_NONE;
}
/// \brief Set the trigger level.
/// \param channel The channel that should be set.
/// \param level The new trigger level (V).
/// \return The trigger level that has been set, ::Dso::ErrorCode on error.
double Control::setTriggerLevel(unsigned int channel, double level) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if(channel >= HANTEK_CHANNELS)
return Dso::ERROR_PARAMETER;
// Calculate the trigger level value
unsigned short int minimum, maximum;
switch(this->device->getModel()) {
case MODEL_DSO5200:
case MODEL_DSO5200A:
// The range is the same as used for the offsets for 10 bit models
minimum = ((unsigned short int) *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_START])) << 8) + *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_START]) + 1);
maximum = ((unsigned short int) *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_END])) << 8) + *((unsigned char *) &(this->specification.offsetLimit[channel][this->settings.voltage[channel].gain][OFFSET_END]) + 1);
break;
default:
// It's from 0x00 to 0xfd for the 8 bit models
minimum = 0x00;
maximum = 0xfd;
break;
}
// Never get out of the limits
unsigned short int levelValue = qBound((long int) minimum, (long int) ((this->settings.voltage[channel].offsetReal + level / this->specification.gainSteps[this->settings.voltage[channel].gain]) * (maximum - minimum) + 0.5) + minimum, (long int) maximum);
// Check if the set channel is the trigger source
if(!this->settings.trigger.special && channel == this->settings.trigger.source) {
// SetOffset control command for trigger level
static_cast<ControlSetOffset *>(this->control[CONTROLINDEX_SETOFFSET])->setTrigger(levelValue);
this->controlPending[CONTROLINDEX_SETOFFSET] = true;
}
/// \todo Get alternating trigger in here
this->settings.trigger.level[channel] = level;
return (double) ((levelValue - minimum) / (maximum - minimum) - this->settings.voltage[channel].offsetReal) * this->specification.gainSteps[this->settings.voltage[channel].gain];
}
/// \brief Set the trigger slope.
/// \param slope The Slope that should cause a trigger.
/// \return See ::Dso::ErrorCode.
int Control::setTriggerSlope(Dso::Slope slope) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
if(slope != Dso::SLOPE_NEGATIVE && slope != Dso::SLOPE_POSITIVE)
return Dso::ERROR_PARAMETER;
switch(this->specification.command.bulk.setTrigger) {
case BULK_SETTRIGGERANDSAMPLERATE: {
// SetTriggerAndSamplerate bulk command for trigger slope
static_cast<BulkSetTriggerAndSamplerate *>(this->command[BULK_SETTRIGGERANDSAMPLERATE])->setTriggerSlope(slope);
this->commandPending[BULK_SETTRIGGERANDSAMPLERATE] = true;
break;
}
case BULK_CSETTRIGGERORSAMPLERATE: {
// SetTrigger2250 bulk command for trigger slope
static_cast<BulkSetTrigger2250 *>(this->command[BULK_CSETTRIGGERORSAMPLERATE])->setTriggerSlope(slope);
this->commandPending[BULK_CSETTRIGGERORSAMPLERATE] = true;
break;
}
case BULK_ESETTRIGGERORSAMPLERATE: {
// SetTrigger5200 bulk command for trigger slope
static_cast<BulkSetTrigger5200 *>(this->command[BULK_ESETTRIGGERORSAMPLERATE])->setTriggerSlope(slope);
this->commandPending[BULK_ESETTRIGGERORSAMPLERATE] = true;
break;
}
default:
return Dso::ERROR_UNSUPPORTED;
}
this->settings.trigger.slope = slope;
return Dso::ERROR_NONE;
}
/// \brief Set the trigger position.
/// \param position The new trigger position (in s).
/// \return The trigger position that has been set.
double Control::setPretriggerPosition(double position) {
if(!this->device->isConnected())
return -2;
// All trigger positions are measured in samples
unsigned long int positionSamples = position * this->settings.samplerate.current;
// Fast rate mode uses both channels
if(this->settings.samplerate.limits == &this->specification.samplerate.multi)
positionSamples /= HANTEK_CHANNELS;
switch(this->specification.command.bulk.setPretrigger) {
case BULK_SETTRIGGERANDSAMPLERATE: {
// Calculate the position value (Start point depending on record length)
unsigned long int position = 0x7ffff - this->specification.recordLengths[this->settings.recordLengthId] + positionSamples;
// SetTriggerAndSamplerate bulk command for trigger position
static_cast<BulkSetTriggerAndSamplerate *>(this->command[BULK_SETTRIGGERANDSAMPLERATE])->setTriggerPosition(position);
this->commandPending[BULK_SETTRIGGERANDSAMPLERATE] = true;
break;
}
case BULK_FSETBUFFER: {
// Calculate the position values (Inverse, maximum is 0x7ffff)
unsigned short int positionPre = 0x7ffff - this->specification.recordLengths[this->settings.recordLengthId] + positionSamples;
unsigned short int positionPost = 0x7ffff - positionSamples;
// SetBuffer2250 bulk command for trigger position
BulkSetBuffer2250 *commandSetBuffer2250 = static_cast<BulkSetBuffer2250 *>(this->command[BULK_FSETBUFFER]);
commandSetBuffer2250->setTriggerPositionPre(positionPre);
commandSetBuffer2250->setTriggerPositionPost(positionPost);
this->commandPending[BULK_FSETBUFFER] = true;
break;
}
case BULK_ESETTRIGGERORSAMPLERATE: {
// Calculate the position values (Inverse, maximum is 0xffff)
unsigned short int positionPre = 0xffff - this->specification.recordLengths[this->settings.recordLengthId] + positionSamples;
unsigned short int positionPost = 0xffff - positionSamples;
// SetBuffer5200 bulk command for trigger position
BulkSetBuffer5200 *commandSetBuffer5200 = static_cast<BulkSetBuffer5200 *>(this->command[BULK_DSETBUFFER]);
commandSetBuffer5200->setTriggerPositionPre(positionPre);
commandSetBuffer5200->setTriggerPositionPost(positionPost);
this->commandPending[BULK_DSETBUFFER] = true;
break;
}
default:
return Dso::ERROR_UNSUPPORTED;
}
this->settings.trigger.position = position;
return (double) positionSamples / this->settings.samplerate.current;
}
#ifdef DEBUG
/// \brief Sends bulk/control commands directly.
/// <p>
/// <b>Syntax:</b><br />
/// <br />
/// Bulk command:
/// <pre>send bulk [<em>hex data</em>]</pre>
/// %Control command:
/// <pre>send control [<em>hex code</em>] [<em>hex data</em>]</pre>
/// </p>
/// \param command The command as string (Has to be parsed).
/// \return See ::Dso::ErrorCode.
int Control::stringCommand(QString command) {
if(!this->device->isConnected())
return Dso::ERROR_CONNECTION;
QStringList commandParts = command.split(' ', QString::SkipEmptyParts);
if(commandParts.count() >= 1) {
if(commandParts[0] == "send") {
if(commandParts.count() >= 2) {
if(commandParts[1] == "bulk") {
QString data = command.section(' ', 2, -1, QString::SectionSkipEmpty);
unsigned char commandCode = 0;
// Read command code (First byte)
Helper::hexParse(commandParts[2], &commandCode, 1);
if(commandCode > BULK_COUNT)
return Dso::ERROR_UNSUPPORTED;
// Update bulk command and mark as pending
Helper::hexParse(data, this->command[commandCode]->data(), this->command[commandCode]->getSize());
this->commandPending[commandCode] = true;
return Dso::ERROR_NONE;
}
else if(commandParts[1] == "control") {
unsigned char controlCode = 0;
// Read command code (First byte)
Helper::hexParse(commandParts[2], &controlCode, 1);
int control;
for(control = 0; control < CONTROLINDEX_COUNT; ++control) {
if(this->controlCode[control] == controlCode)
break;
}
if(control >= CONTROLINDEX_COUNT)
return Dso::ERROR_UNSUPPORTED;
QString data = command.section(' ', 3, -1, QString::SectionSkipEmpty);
// Update control command and mark as pending
Helper::hexParse(data, this->control[control]->data(), this->control[control]->getSize());
this->controlPending[control] = true;
return Dso::ERROR_NONE;
}
}
else {
return Dso::ERROR_PARAMETER;
}
}
}
else {
return Dso::ERROR_PARAMETER;
}
return Dso::ERROR_UNSUPPORTED;
}
#endif
}