Commit 0636ab1cbd6a0eded7cd38162535e5f2dcba87b1

Authored by oliverhaag
1 parent 35299cfe

Fixed DSO-2090 pretrigger bug and improved timing of sampling routine

openhantek/ChangeLog
... ... @@ -106,3 +106,11 @@
106 106 2010-12-13 Oliver Haag <oliver.haag@gmail.com>
107 107 * Triggermode Auto working correctly now
108 108 * Improved frequency detection routine
  109 +
  110 +2011-01-22 Oliver Haag <oliver.haag@gmail.com>
  111 +* Storing positions of docking windows and toolbars in settings now
  112 +* Added submenu to show/hide docking windows and toolbars to the View menu
  113 +
  114 +2011-01-22 Oliver Haag <oliver.haag@gmail.com>
  115 +* Fixed bug with wrong pretrigger position when using a DSO-2090
  116 +* Improved timing of the Hantek DSO sampling routine
... ...
openhantek/src/hantek/control.cpp
... ... @@ -115,7 +115,7 @@ namespace Hantek {
115 115  
116 116 /// \brief Handles all USB things until the device gets disconnected.
117 117 void Control::run() {
118   - int errorCode, waitCounter = 0;
  118 + int errorCode, cycleCounter = 0, startCycle = 0;
119 119  
120 120 // The control loop is running until the device is disconnected
121 121 int captureState = CAPTURE_WAITING;
... ... @@ -171,9 +171,10 @@ namespace Hantek {
171 171 if(captureState == LIBUSB_ERROR_NO_DEVICE)
172 172 break;
173 173  
174   - // Check the current oscilloscope state every 50 ms
175   - /// \todo Maybe the time interval could be improved...
176   - this->msleep(50);
  174 + // Check the current oscilloscope state everytime 25% of the buffer should be refilled
  175 + // Not more often than every 10 ms though
  176 + int cycleTime = qMax(this->samplerateDivider * this->bufferSize * 250 / this->samplerateMax, (long unsigned int) 10);
  177 + this->msleep(cycleTime);
177 178  
178 179 if(!this->sampling) {
179 180 samplingStarted = false;
... ... @@ -209,21 +210,31 @@ namespace Hantek {
209 210  
210 211 case CAPTURE_WAITING:
211 212 if(samplingStarted && lastTriggerMode == this->triggerMode) {
212   - waitCounter++;
  213 + cycleCounter++;
213 214  
214   - if(this->triggerMode == Dso::TRIGGERMODE_AUTO) {
215   - if(waitCounter > (double) this->samplerateDivider / this->samplerateMax * this->bufferSize * 20 + 2) {
216   - // Force triggering
217   - errorCode = this->device->bulkCommand(this->command[COMMAND_FORCETRIGGER]);
  215 + if(cycleCounter == startCycle) {
  216 + // Buffer refilled completely since start of sampling, enable the trigger now
  217 + errorCode = this->device->bulkCommand(this->command[COMMAND_ENABLETRIGGER]);
  218 + if(errorCode < 0) {
218 219 if(errorCode == LIBUSB_ERROR_NO_DEVICE)
219 220 captureState = LIBUSB_ERROR_NO_DEVICE;
  221 + break;
  222 + }
  223 + #ifdef DEBUG
  224 + qDebug("Enabling trigger");
  225 + #endif
  226 + }
  227 + else if(cycleCounter >= 8 + startCycle && this->triggerMode == Dso::TRIGGERMODE_AUTO) {
  228 + // Force triggering
  229 + errorCode = this->device->bulkCommand(this->command[COMMAND_FORCETRIGGER]);
  230 + if(errorCode == LIBUSB_ERROR_NO_DEVICE)
  231 + captureState = LIBUSB_ERROR_NO_DEVICE;
220 232 #ifdef DEBUG
221 233 qDebug("Forcing trigger");
222 234 #endif
223   - }
224 235 }
225 236  
226   - if(waitCounter < 80)
  237 + if(cycleCounter < 50 || cycleCounter < 4000 / cycleTime)
227 238 break;
228 239 }
229 240  
... ... @@ -238,19 +249,9 @@ namespace Hantek {
238 249 qDebug("Starting to capture");
239 250 #endif
240 251  
241   - // Enable trigger
242   - errorCode = this->device->bulkCommand(this->command[COMMAND_ENABLETRIGGER]);
243   - if(errorCode < 0) {
244   - if(errorCode == LIBUSB_ERROR_NO_DEVICE)
245   - captureState = LIBUSB_ERROR_NO_DEVICE;
246   - break;
247   - }
248   -#ifdef DEBUG
249   - qDebug("Enabling trigger");
250   -#endif
251   -
252 252 samplingStarted = true;
253   - waitCounter = 0;
  253 + cycleCounter = 0;
  254 + startCycle = this->triggerPosition * 1000 / cycleTime + 1;
254 255 lastTriggerMode = this->triggerMode;
255 256 break;
256 257  
... ... @@ -373,7 +374,7 @@ namespace Hantek {
373 374 }
374 375  
375 376 // Convert data from the oscilloscope and write it into the sample buffer
376   - unsigned int bufferPosition = this->triggerPoint * 2;
  377 + unsigned int bufferPosition = (this->triggerPoint + 1) * 2;
377 378 if(using10Bits) {
378 379 // Additional 2 most significant bits after the normal data
379 380 unsigned int extraBitsPosition; // Track the position of the extra bits in the additional byte
... ... @@ -412,7 +413,7 @@ namespace Hantek {
412 413 }
413 414  
414 415 // Convert data from the oscilloscope and write it into the sample buffer
415   - unsigned int bufferPosition = this->triggerPoint * 2;
  416 + unsigned int bufferPosition = (this->triggerPoint + 1) * 2;
416 417 if(using10Bits) {
417 418 // Additional 2 most significant bits after the normal data
418 419 for(unsigned int realPosition = 0; realPosition < channelDataCount; realPosition++, bufferPosition += 2) {
... ... @@ -665,7 +666,6 @@ namespace Hantek {
665 666  
666 667 this->updateBufferSize(this->bufferSize);
667 668 this->setTriggerPosition(this->triggerPosition);
668   - this->setTriggerSlope(this->triggerSlope);
669 669 return this->samplerateMax / this->samplerateDivider;
670 670 }
671 671  
... ... @@ -955,7 +955,7 @@ namespace Hantek {
955 955 // SetTriggerAndSamplerate bulk command for trigger slope
956 956 CommandSetTriggerAndSamplerate *commandSetTriggerAndSamplerate = (CommandSetTriggerAndSamplerate *) this->command[COMMAND_SETTRIGGERANDSAMPLERATE];
957 957  
958   - commandSetTriggerAndSamplerate->setTriggerSlope((/*this->bufferSize != BUFFER_SMALL ||*/ commandSetTriggerAndSamplerate->getSamplerateFast() % 2 == 0) ? slope : Dso::SLOPE_NEGATIVE - slope);
  958 + commandSetTriggerAndSamplerate->setTriggerSlope(slope);
959 959 this->commandPending[COMMAND_SETTRIGGERANDSAMPLERATE] = true;
960 960 break;
961 961 }
... ... @@ -988,10 +988,10 @@ namespace Hantek {
988 988 positionSamples /= HANTEK_CHANNELS;
989 989  
990 990 // Calculate the position value (Start point depending on buffer size)
991   - unsigned long int positionStart = (this->bufferSize == BUFFER_SMALL) ? 0x77660 : 0x78000;
  991 + unsigned long int position = 0x7ffff - this->bufferSize + positionSamples;
992 992  
993 993 // SetTriggerAndSamplerate bulk command for trigger position
994   - ((CommandSetTriggerAndSamplerate *) this->command[COMMAND_SETTRIGGERANDSAMPLERATE])->setTriggerPosition(positionStart + positionSamples);
  994 + ((CommandSetTriggerAndSamplerate *) this->command[COMMAND_SETTRIGGERANDSAMPLERATE])->setTriggerPosition(position);
995 995 this->commandPending[COMMAND_SETTRIGGERANDSAMPLERATE] = true;
996 996  
997 997 break;
... ...