OpenCores
URL https://opencores.org/ocsvn/modular_oscilloscope/modular_oscilloscope/trunk

Subversion Repositories modular_oscilloscope

[/] [modular_oscilloscope/] [trunk/] [sw/] [src/] [rviboardtools.cpp] - Blame information for rev 60

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 60 budinero
#include <cmath>
2
#include <QTimer>
3
#include <QString>
4
#include <QColorDialog>
5
#include <QInputDialog>
6
#include <qwt_counter.h>
7
#include "rviboardtools.h"
8
#include "ui_rviboardtools.h"
9
 
10
// temp
11
#include <QDir>
12
 
13
 
14
class Suffixer
15
{
16
public:
17
    Suffixer(double number = 0.0)
18
    {
19
        setNumber(number);
20
    }
21
 
22
    void setNumber(double number)
23
    {
24
        number = qAbs(number);
25
        if (number < 1e-9)
26
        {
27
            multiplier = 1e9;
28
            suffix = QString("p");
29
        }
30
        else if (number < 1e-6)
31
        {
32
            multiplier = 1e6;
33
            suffix = QString("n");
34
        }
35
        else if (number < 1e-3)
36
        {
37
            multiplier = 1e6;
38
            suffix = QString("u");
39
        }
40
        else if (number < 1.0)
41
        {
42
            multiplier = 1e3;
43
            suffix = QString("m");
44
        }
45
        else
46
        {
47
            multiplier = 1.0;
48
            suffix = QString("");
49
        }
50
    }
51
 
52
    double multiplier;
53
    QString suffix;
54
};
55
 
56
 
57
RVIBoardTools::RVIBoardTools(QWidget *parent)
58
    : QWidget(parent),
59
    ui(new Ui::RVIBoardTools)
60
{
61
    ui->setupUi(this);
62
 
63
    slidersTimer = new QTimer(this);
64
    slidersTimer->setInterval(550);
65
 
66
    dataTimer = new QTimer(this);
67
    dataTimer->setInterval(30);   // plot update time
68
 
69
    dataAVec = new QVector<double>;
70
    dataBVec = new QVector<double>;
71
    timeVec = new QVector<double>;
72
    board = new RVICommThread(this);
73
 
74
 
75
    // Timer for sliders
76
    connect(slidersTimer, SIGNAL(timeout()),this, SLOT(updateSlidersValues()));
77
 
78
    connect(ui->timeZoomSlider , SIGNAL(sliderPressed()), slidersTimer , SLOT(start()));
79
    connect(ui->timeZoomSlider, SIGNAL(sliderReleased()), slidersTimer, SLOT(stop()));
80
    connect(ui->timePosSlider , SIGNAL(sliderPressed()), slidersTimer , SLOT(start()));
81
    connect(ui->timePosSlider, SIGNAL(sliderReleased()), slidersTimer, SLOT(stop()));
82
 
83
    connect(ui->channelAZoomSlider , SIGNAL(sliderPressed()), slidersTimer , SLOT(start()));
84
    connect(ui->channelAZoomSlider, SIGNAL(sliderReleased()), slidersTimer, SLOT(stop()));
85
    connect(ui->channelAPosSlider, SIGNAL(sliderPressed()), slidersTimer , SLOT(start()));
86
    connect(ui->channelAPosSlider, SIGNAL(sliderReleased()), slidersTimer, SLOT(stop()));
87
 
88
    connect(ui->channelBZoomSlider , SIGNAL(sliderPressed()), slidersTimer , SLOT(start()));
89
    connect(ui->channelBZoomSlider, SIGNAL(sliderReleased()), slidersTimer, SLOT(stop()));
90
    connect(ui->channelBPosSlider, SIGNAL(sliderPressed()), slidersTimer , SLOT(start()));
91
    connect(ui->channelBPosSlider, SIGNAL(sliderReleased()), slidersTimer, SLOT(stop()));
92
 
93
    // Board connections
94
    //connect(board, SIGNAL(chANewData(int )), this, SLOT(chAAppData(int)) );
95
    //connect(board, SIGNAL(chBNewData(int )), this, SLOT(chBAppData(int)));
96
    //connect(board, SIGNAL(finished()),this,SLOT(uncheckRunButtons()));
97
    //connect(board, SIGNAL(endOfBuffer()), this, SLOT(showResults()),Qt::DirectConnection);
98
    connect(board, SIGNAL(statusMessage(QString)), this, SLOT(sendStatusMessage(QString)));
99
    connect(this, SIGNAL(stopAcquistion()), board, SLOT(stopAcquistion()), Qt::DirectConnection);
100
    // Timer for board
101
    connect(dataTimer, SIGNAL(timeout()), this, SLOT(showResults()), Qt::DirectConnection);
102
 
103
 
104
}
105
 
106
RVIBoardTools::~RVIBoardTools()
107
{
108
    delete ui;
109
}
110
 
111
 
112
void RVIBoardTools::setDefaultValues()
113
{
114
    // Fill trigger combo
115
    fillTriggerCombo();
116
 
117
    // Fill trigger Slope
118
    ui->trigSlopeCombo->addItem(QIcon(":/images/rising.png"),"Rising");
119
    ui->trigSlopeCombo->addItem(QIcon(":/images/falling.png"),"Falling");
120
 
121
 
122
    // Set Max-Min values
123
    ui->bufferSpinBox->setMaximum(MAX_BUFF_SIZE);
124
    ui->bufferSpinBox->setMinimum(MIN_BUFF_SIZE);
125
 
126
 
127
 
128
 
129
    fillSampleTimeCombo();
130
 
131
    // Data
132
    chAOffset = 0;
133
    chBOffset = 0;
134
 
135
    confTScal = 1023;
136
    confTScalEn = 1;
137
 
138
    paused = false;
139
 
140
    ui->bufferSpinBox->setValue(1000);
141
    setVoltMultipliers();
142
    updateTimeVec();
143
 
144
 
145
    // Signals
146
    emit timeChangeLimits(timeVec->first(), timeVec->last());
147
    emit channelAColorChanged( ui->channelAColorButton->palette().button().color());
148
    emit channelBColorChanged( ui->channelBColorButton->palette().button().color());
149
 
150
}
151
 
152
 
153
void RVIBoardTools::stop()
154
{
155
    if (!ui->runButton->isChecked())
156
    {
157
        dataTimer->stop();
158
        statusChanged("stop timer");
159
    }
160
   emit stopAcquistion();
161
   //board->terminate();
162
}
163
 
164
void RVIBoardTools::setPaused(const bool &on)
165
{
166
    paused = on;
167
    setEnabled(!on);
168
    if (on)
169
        emit statusChanged(tr("Paused"));
170
}
171
 
172
void RVIBoardTools::startAcquisition(const bool &continuous)
173
{
174
    bool ok;
175
 
176
//    if (board->isRunning())
177
//    {
178
//        //while (!board->isFinished())
179
//       // {
180
//            repaint();
181
//            stop();
182
//            board->quit();
183
//        //}
184
//    }
185
 
186
    emit timeChangeLimits(timeVec->first(), timeVec->last());
187
 
188
    board->setAddress(ui->addressBox->currentText().toShort(&ok, 16));
189
    board->setContinuous(continuous);
190
    board->setTrigger(ui->trigBox->isChecked(),
191
                     ui->trigSlopeCombo->currentText() == "Falling",
192
                     ui->trigSouceCombo->currentText() == "A",
193
                     ui->trigLevelSpinBox->value()* 1023/100,
194
                     ui->trigOffsetSpinBox->value());
195
    board->setChannels(ui->channelABox->isChecked(),
196
                      ui->channelBBox->isChecked());
197
    board->setBuffer(ui->bufferSpinBox->value());
198
    board->setTimeScaler(ui->sampleTimeCombo->currentIndex() > 0,
199
                        ui->sampleTimeCombo->currentIndex() - 1);
200
 
201
    board->start(QThread::LowestPriority);
202
 
203
    if (!dataTimer->isActive())
204
        dataTimer->start();
205
 
206
    disconnect(board, SIGNAL(finished()),this,SLOT(startAcquisition()));
207
 
208
}
209
 
210
 
211
void RVIBoardTools::showResults()
212
{
213
    if (!paused)
214
    {
215
        if ( ui->quickUpdateCheck->isChecked() || board->isBufferFull()  )
216
        {
217
            if (ui->channelABox->isChecked())
218
            {
219
                dataAVec->clear();
220
                data = board->getChannelData(0);
221
                if (data.size() > 5)
222
                {
223
                    for (int i = 0; i < data.size(); i++)
224
                        dataAVec->append((data.at(i) - 512)* chAVoltMultiplier + chAOffset);
225
                    emit channelAData(*timeVec, *dataAVec);
226
                }
227
            }
228
            if (ui->channelBBox->isChecked())
229
            {
230
                dataBVec->clear();
231
                data = board->getChannelData(1);
232
                if (data.size() > 5)
233
                {
234
                    for (int i = 0; i < data.size(); i++)
235
                        dataBVec->append((data.at(i) - 512) * chBVoltMultiplier + chBOffset);
236
                    emit channelBData(*timeVec, *dataBVec);
237
                }
238
            }
239
        }
240
    }
241
}
242
 
243
 
244
void RVIBoardTools::sendStatusMessage(const QString &status)
245
{
246
    emit statusChanged(status);
247
}
248
 
249
 
250
 
251
void RVIBoardTools::updateTimeVec()
252
{
253
    double deltaTime;
254
    deltaTime = pow(2.0,ui->sampleTimeCombo->currentIndex()) / board->getADCSampleRate();
255
 
256
    int size;
257
    size = ui->bufferSpinBox->value();
258
    if (ui->channelABox->isChecked() && ui->channelBBox->isChecked())
259
        size = size/2;
260
 
261
    timeVec->clear();
262
    timeVec->append(deltaTime * ui->trigOffsetSpinBox->value());
263
    for (int i = 1; i < size; i++)
264
        timeVec->append(timeVec->at(i-1) + deltaTime);
265
    //statusChanged(tr("Primero %1 y segundo %2").arg(timeVec->first()).arg(timeVec->last()));
266
}
267
 
268
void RVIBoardTools::setVoltMultipliers()
269
{
270
    if (!(ui->channelAZBtn->isChecked()))
271
    {
272
        if (ui->chASpan2VRadio->isChecked())
273
            chAVoltMultiplier = 2.0/1024;
274
        else
275
            chAVoltMultiplier = 1.0/1024;
276
    }
277
    else
278
        chAVoltMultiplier = 0.0;
279
 
280
    if (!(ui->channelBZBtn->isChecked()))
281
    {
282
        if (ui->chBSpan2VRadio->isChecked())
283
            chBVoltMultiplier = 2.0/1024;
284
        else
285
            chBVoltMultiplier = 1.0/1024;
286
    }
287
    else
288
        chBVoltMultiplier = 0.0;
289
}
290
 
291
 
292
void RVIBoardTools::uncheckRunButtons()
293
{
294
    ui->runButton->setChecked(false);
295
    ui->singleButton->setChecked(false);
296
}
297
 
298
 
299
QVector<double> RVIBoardTools::getData(RVIBoardTools::VariableType var)
300
{
301
    switch(var)
302
    {
303
        case RVIBoardTools::ChannelA:
304
            return *dataAVec;
305
            break;
306
 
307
        case RVIBoardTools::ChannelB:
308
            return *dataBVec;
309
            break;
310
 
311
        default:
312
            return *timeVec;
313
            break;
314
    }
315
 
316
}
317
 
318
 
319
///////////////////////////////////////////////////////////////////////
320
// Trigger
321
 
322
void RVIBoardTools::on_trigLevelSlider_valueChanged(int value)
323
{
324
    double d_value;
325
    if (ui->trigSouceCombo->currentText() == "B")
326
    {
327
        if (ui->chBSpan2VRadio->isChecked())
328
            d_value = (value - 50) * 2.0/50;
329
        else
330
            d_value = (value - 50) * 1.0/50;
331
    }
332
    else
333
    {
334
        if (ui->chASpan2VRadio->isChecked())
335
            d_value = (value - 50) * 2.0/50;
336
        else
337
            d_value = (value - 50) * 1.0/50;
338
    }
339
   emit changeTriggerValue(d_value);
340
 
341
}
342
 
343
 
344
void RVIBoardTools::on_trigLineCheck_toggled(bool checked)
345
{
346
    emit showTriggerLine(checked, ui->trigSouceCombo->currentIndex());
347
}
348
 
349
 
350
void RVIBoardTools::on_trigSouceCombo_currentIndexChanged(int index)
351
{
352
    emit showTriggerLine(ui->trigLineCheck->isChecked(), index);
353
        if (board->isRunning())
354
    {
355
        stop();
356
        connect(board, SIGNAL(finished()),this,SLOT(startAcquisition()));
357
    }
358
 
359
}
360
 
361
void RVIBoardTools::fillTriggerCombo()
362
{
363
    ui->trigSouceCombo->clear();
364
    if (ui->channelABox->isChecked() == true)
365
    {
366
        ui->trigSouceCombo->addItem("A");
367
    }
368
    if (ui->channelBBox->isChecked() == true)
369
    {
370
        ui->trigSouceCombo->addItem("B");
371
    }
372
}
373
 
374
 
375
////////////////////////////
376
// Channel A
377
void RVIBoardTools::on_channelABox_toggled(bool set)
378
{
379
    emit channelAEnabled(set);
380
}
381
 
382
 
383
void RVIBoardTools::on_channelAColorButton_clicked()
384
{
385
    QColorDialog *colorChooser = new QColorDialog();
386
    QColor color = colorChooser->getColor(ui->channelAColorButton->palette().button().color());
387
    ui->channelAColorButton->setPalette(color);
388
    emit channelAColorChanged( color);
389
}
390
 
391
 
392
void RVIBoardTools::setChannelADiv(double div)
393
{
394
    Suffixer mod(div);
395
    ui->channelAZoomSpinBox->setValue(div * mod.multiplier);
396
    ui->channelAZoomSpinBox->setSuffix(" " + mod.suffix + tr("V/Div"));
397
}
398
 
399
 
400
void RVIBoardTools::on_channelAZoomSlider_valueChanged(int value)
401
{
402
    if (value != 0 )
403
    {
404
        ui->channelAAutoButton->setChecked(false);
405
        emit channelAChangeDiv(value);
406
 
407
        if (!slidersTimer->isActive())
408
        {
409
            ui->channelAZoomSlider->setValue(0);
410
        }
411
    }
412
}
413
 
414
 
415
void RVIBoardTools::on_channelAZoomSlider_sliderReleased()
416
{
417
    ui->channelAZoomSlider->setValue(0);
418
}
419
 
420
 
421
void RVIBoardTools::on_channelAAutoButton_toggled(bool checked)
422
{
423
    if (checked)
424
    {
425
        emit channelAChangeDiv(0);
426
    }
427
    else
428
    {
429
        emit channelAChangeDiv(-1);
430
    }
431
}
432
 
433
 
434
void RVIBoardTools::on_channelAPosSlider_valueChanged(int value)
435
{
436
    if (value != 0 )
437
    {
438
        emit channelAMove(value);
439
 
440
        if (!slidersTimer->isActive())
441
        {
442
            ui->channelAPosSlider->setValue(0);
443
        }
444
    }
445
}
446
 
447
void RVIBoardTools::on_channelAPosSlider_sliderReleased()
448
{
449
    ui->channelAPosSlider->setValue(0);
450
}
451
 
452
 
453
void RVIBoardTools::on_channelAResetButton_pressed()
454
{
455
    emit channelAResetPos();
456
}
457
 
458
 
459
////////////////////////////
460
// Channel B
461
void RVIBoardTools::on_channelBBox_toggled(bool set)
462
{
463
    emit channelBEnabled(set);
464
}
465
 
466
 
467
void RVIBoardTools::on_channelBColorButton_clicked()
468
{
469
    QColorDialog *colorChooser = new QColorDialog();
470
    QColor color = colorChooser->getColor(ui->channelBColorButton->palette().button().color());
471
    ui->channelBColorButton->setPalette(color);
472
    emit channelBColorChanged( color);
473
}
474
 
475
 
476
void RVIBoardTools::setChannelBDiv(double div)
477
{
478
    Suffixer mod(div);
479
    ui->channelBZoomSpinBox->setValue(div * mod.multiplier);
480
    ui->channelBZoomSpinBox->setSuffix(" " + mod.suffix + tr("V/Div"));
481
}
482
 
483
 
484
void RVIBoardTools::on_channelBZoomSlider_valueChanged(int value)
485
{
486
    if (value != 0 )
487
    {
488
        ui->channelBAutoButton->setChecked(false);
489
        emit channelBChangeDiv(value);
490
 
491
        if (!slidersTimer->isActive())
492
        {
493
            ui->channelBZoomSlider->setValue(0);
494
        }
495
    }
496
}
497
 
498
 
499
void RVIBoardTools::on_channelBZoomSlider_sliderReleased()
500
{
501
    ui->channelBPosSlider->setValue(0);
502
}
503
 
504
 
505
void RVIBoardTools::on_channelBAutoButton_toggled(bool checked)
506
{
507
    if (checked)
508
    {
509
        emit channelBChangeDiv(0);
510
    }
511
    else
512
    {
513
        emit channelBChangeDiv(-1);
514
    }
515
}
516
 
517
 
518
void RVIBoardTools::on_channelBPosSlider_valueChanged(int value)
519
{
520
    if (value != 0 )
521
    {
522
        emit channelBMove(value);
523
 
524
        if (!slidersTimer->isActive())
525
        {
526
            ui->channelBPosSlider->setValue(0);
527
        }
528
    }
529
}
530
 
531
void RVIBoardTools::on_channelBPosSlider_sliderReleased()
532
{
533
    ui->channelBPosSlider->setValue(0);
534
}
535
 
536
 
537
void RVIBoardTools::on_channelBResetButton_pressed()
538
{
539
    emit channelBResetPos();
540
}
541
 
542
 
543
 
544
////////////////////////////
545
// Time
546
 
547
void RVIBoardTools::on_timeZoomSlider_valueChanged(int value)
548
{
549
    if (value != 0 )
550
    {
551
        ui->timeAutoButton->setChecked(false);
552
        emit timeChangeDiv(value);
553
 
554
        if (!slidersTimer->isActive())
555
        {
556
            ui->timeZoomSlider->setValue(0);
557
        }
558
    }
559
}
560
 
561
 
562
void RVIBoardTools::on_timeAutoButton_toggled(bool checked)
563
{
564
    if (checked)
565
    {
566
        emit channelAChangeDiv(0);
567
    }
568
    else
569
    {
570
        emit channelAChangeDiv(-1);
571
    }
572
}
573
 
574
 
575
void RVIBoardTools::setTimeDiv(double div)
576
{
577
    Suffixer mod(div);
578
    ui->timeZoomSpinBox->setValue(div * mod.multiplier);
579
    ui->timeZoomSpinBox->setSuffix(" " + mod.suffix + tr("s/Div"));
580
}
581
 
582
 
583
void RVIBoardTools::on_bufferSpinBox_valueChanged(int value)
584
{
585
    ui->trigOffsetSlider->setMaximum(value - 1);
586
    ui->trigOffsetSpinBox->setMaximum(value - 1);
587
    ui->trigOffsetSlider->setMinimum(-value);
588
    ui->trigOffsetSpinBox->setMinimum(-value);
589
    ui->trigOffsetSpinBox->setValue(0);
590
 
591
        if (board->isRunning())
592
    {
593
        stop();
594
        connect(board, SIGNAL(finished()),this,SLOT(startAcquisition()));
595
    }
596
 
597
}
598
 
599
 
600
void RVIBoardTools::on_timeZoomSlider_sliderReleased()
601
{
602
    ui->timeZoomSlider->setValue(0);
603
}
604
 
605
 
606
void RVIBoardTools::on_timePosSlider_sliderReleased()
607
{
608
    ui->timePosSlider->setValue(0);
609
}
610
 
611
 
612
void RVIBoardTools::on_timePosSlider_valueChanged(int value)
613
{
614
    if (value != 0 )
615
    {
616
        emit timeMove(value);
617
 
618
        if (!slidersTimer->isActive())
619
        {
620
            ui->timePosSlider->setValue(0);
621
        }
622
    }
623
}
624
 
625
void RVIBoardTools::fillSampleTimeCombo()
626
{
627
    double value;
628
    double freq = board->getADCSampleRate();
629
    for (int i = 0; i <= pow(2.0,SCALE_BITS); i++)
630
    {
631
        value = pow(2.0,i)/freq;
632
        if( value < 1e-3 )
633
            ui->sampleTimeCombo->addItem(QString("%L1 ns").arg(value*1e6));
634
        else if(value < 1)
635
            ui->sampleTimeCombo->addItem(QString("%L1 ms").arg(value*1e3));
636
        else
637
            ui->sampleTimeCombo->addItem(QString("%L1 s").arg(value));
638
    }
639
}
640
////////////////////////////
641
// Common
642
 
643
void  RVIBoardTools::updateSlidersValues()
644
{
645
    on_channelAZoomSlider_valueChanged(ui->channelAZoomSlider->value());
646
    on_channelAPosSlider_valueChanged(ui->channelAPosSlider->value());
647
    on_channelBZoomSlider_valueChanged(ui->channelAZoomSlider->value());
648
    on_channelBPosSlider_valueChanged(ui->channelAPosSlider->value());
649
    on_timeZoomSlider_valueChanged(ui->timeZoomSlider->value());
650
    on_timePosSlider_valueChanged(ui->timePosSlider->value());
651
}
652
 
653
 
654
 
655
void RVIBoardTools::on_runButton_toggled(bool checked)
656
{
657
    if (checked == true)
658
    {
659
        ui->runButton->setText(tr("Stop"));
660
        ui->singleButton->setEnabled(false);
661
        startAcquisition(true); // start continuous
662
    }
663
    else
664
    {
665
        ui->runButton->setText(tr("Start"));
666
        ui->singleButton->setEnabled(true);
667
        stop();
668
    }
669
}
670
 
671
 
672
void RVIBoardTools::on_singleButton_toggled(bool checked)
673
{
674
    if (checked == true)
675
    {
676
        ui->singleButton->setText(tr("Stop"));
677
        ui->runButton->setEnabled(false);
678
        startAcquisition(false); // start single
679
    }
680
    else
681
    {
682
        ui->singleButton->setText(tr("Single"));
683
        ui->runButton->setEnabled(true);
684
        stop();
685
    }
686
}
687
 
688
 
689
 
690
 
691
void RVIBoardTools::on_adcSampleButton_clicked()
692
{
693
     bool ok;
694
     QStringList items;
695
     int index;
696
 
697
    for (int i = 1; i <= pow(2.0, ADC_PS_BITS); i++)
698
    {
699
        items << tr("%1 Hz").arg(BASE_SAMPLE_RATE/i);
700
    }
701
 
702
     QString text = QInputDialog::getItem(this, tr("Set ADC sample rate"),
703
                                          tr("Sample rate:"), items, 0, false, &ok);
704
 
705
    index = items.indexOf(text);
706
    board->setAddress(ui->addressBox->currentText().toShort(&ok, 16));
707
    board->setADCPreScale(index);
708
    board->configADC();
709
    fillSampleTimeCombo();
710
 
711
}
712
 
713
 
714
 
715
void RVIBoardTools::on_trigSlopeCombo_currentIndexChanged(int index)
716
{
717
    if (board->isRunning())
718
    {
719
        stop();
720
        connect(board, SIGNAL(finished()),this,SLOT(startAcquisition()));
721
    }
722
}
723
 
724
void RVIBoardTools::on_trigOffsetSpinBox_valueChanged(int )
725
{
726
    if (board->isRunning())
727
    {
728
        stop();
729
        connect(board, SIGNAL(finished()),this,SLOT(startAcquisition()));
730
    }
731
}
732
 
733
void RVIBoardTools::on_trigLevelSpinBox_valueChanged(int )
734
{
735
    if (board->isRunning())
736
    {
737
        stop();
738
        connect(board, SIGNAL(finished()),this,SLOT(startAcquisition()));
739
    }
740
}
741
 
742
void RVIBoardTools::on_sampleTimeCombo_currentIndexChanged(int index)
743
{
744
    if (board->isRunning())
745
    {
746
        stop();
747
        connect(board, SIGNAL(finished()),this,SLOT(startAcquisition()));
748
    }
749
}
750
 
751
void RVIBoardTools::on_trigBox_toggled(bool )
752
{
753
 
754
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.