Advertisement
arum

try1

Jan 24th, 2017
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 12.44 KB | None | 0 0
  1. //#include MAINWINDOW_H
  2.  
  3.  
  4. #include <qwidget.h>
  5. #include <QComboBox>
  6. #include <QPushButton>
  7. #include <QLabel>
  8. #include <QScrollArea>
  9. #include <QSpinBox>
  10. #include <QSignalMapper>
  11. #include <QModelIndex>
  12. #include <QtGlobal>
  13. #include <QMainWindow>
  14. #include <QLCDNumber>
  15.  
  16. #include <QStatusBar>
  17.  
  18. #include "qfirmata.h"
  19.  
  20. class Plot;
  21. class Knob;
  22. class WheelBox;
  23. class SamplingThread;
  24.  
  25.  
  26. class MainWindow : public QWidget
  27. {
  28.     Q_OBJECT
  29.  
  30. public:
  31.     MainWindow( QWidget * = NULL );
  32.  
  33.     void start();
  34.  
  35.     double amplitude() const;
  36.     double frequency() const;
  37.     double signalInterval() const;
  38.     int analoginp;
  39.  
  40. //private Q_SLOTS:
  41. public Q_SLOTS:
  42.     void onFirmwareVersionReceived(const int majorVersion, const int minorVersion);
  43.     void onFirmwareNameReceived(QString firmwareName);
  44.     void onInitialized(const int majorVersion, const int minorVersion, QString firmwareName);
  45.     void onAnalogPinChanged(int pin);
  46.     void onAnalogPinReportinChange(unsigned int pin, bool value);
  47.     void onDigitalPinChanged(int pin);
  48.     void on_pushButtonConnect_clicked();
  49.     void on_pushButtonDisconnect_clicked();
  50.     void on_pushButtonConnect_2_clicked();
  51.     void on_pushButtonDisconnect_2_clicked();
  52.     void samplesValueThis();
  53.    // void setStatusBar(QStatusBar *statusbar);
  54. Q_SIGNALS:
  55.     void amplitudeChanged( double );
  56.     void frequencyChanged( double );
  57.     void signalIntervalChanged( double );
  58.     void transferAnalog(int);
  59.  
  60.  
  61. private:
  62.     QFirmata *_firmata;
  63.     Knob *d_frequencyKnob;
  64.     Knob *d_amplitudeKnob;
  65.     WheelBox *d_timerWheel;
  66.     WheelBox *d_intervalWheel;
  67.     QComboBox *_portComboBox;
  68.     QPushButton *_connectButton;
  69.     QPushButton *_disconnectButton;
  70.     QLabel *_firmwareLabel;
  71.     QLCDNumber *_lcddisplay;
  72.     SamplingThread *d_samplingThread;
  73.  
  74.  
  75.     Plot *d_plot;
  76.  
  77. };
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. #include "mainwindow.h"
  85. #include "plot.h"
  86. #include "knob.h"
  87. #include "wheelbox.h"
  88. #include "samplingthread.h"
  89. #include <qwt_scale_engine.h>
  90. #include <qlabel.h>
  91. #include <qlayout.h>
  92. #include <QComboBox>
  93. #include <QDebug>
  94. #include <QDesktopWidget>
  95. #include <QScreen>
  96. #include <QMessageBox>
  97. #include <QMetaEnum>
  98. #include <QSerialPort>
  99. #include <QSerialPortInfo>
  100. #include <QtGlobal>
  101. #include <QMainWindow>
  102.  
  103. MainWindow::MainWindow( QWidget *parent ):
  104.     QWidget( parent ),
  105.     _firmata(new QFirmata(this))
  106.  
  107.  
  108. {
  109.  
  110.     const double intervalLength = 10.0; // seconds
  111.  
  112.     d_plot = new Plot( this );
  113.     d_plot->setIntervalLength( intervalLength );
  114.  
  115.     d_amplitudeKnob = new Knob( "Amplitude", 0.0, 5000.0, this );
  116.     d_amplitudeKnob->setValue( 160.0 );
  117.  
  118.     d_frequencyKnob = new Knob( "Frequency [Hz]", 0.1, 20.0, this );
  119.     d_frequencyKnob->setValue( 17.8 );
  120.  
  121.     d_intervalWheel = new WheelBox( "Displayed [s]", 1.0, 100.0, 1.0, this );
  122.     d_intervalWheel->setValue( intervalLength );
  123.  
  124.     d_timerWheel = new WheelBox( "Sample Interval [ms]", 0.0, 20.0, 0.1, this );
  125.     d_timerWheel->setValue( 10.0 );
  126.  
  127.     d_samplingThread = new SamplingThread (this);
  128.  
  129.  
  130.  
  131.  
  132.  
  133.     _portComboBox = new QComboBox(this);
  134.    Q_FOREACH (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
  135.     {
  136.         _portComboBox->addItem(info.portName());
  137.    }
  138.  
  139.     _connectButton = new QPushButton(tr("connect"),this);
  140.     _disconnectButton = new QPushButton(tr("disconnect"),this);
  141.     _firmwareLabel = new QLabel();
  142.       QLabel *serialLabel = new QLabel("Serial settings");
  143.       QFont font = serialLabel->font();
  144.       font.setBold(true);
  145.       font.setPointSize(10);
  146.       serialLabel->setFont(font);
  147.       QLabel *serialLabel2 = new QLabel("LCD Display");
  148.       QFont font2 = serialLabel2->font();
  149.       font2.setBold(true);
  150.       font2.setPointSize(10);
  151.       serialLabel2->setFont(font2);
  152.  
  153.     _lcddisplay = new QLCDNumber(this);
  154.     _lcddisplay ->setGeometry(QRect(30,70,10,41));
  155.  
  156.  
  157.  QVBoxLayout* vLayout1 = new QVBoxLayout();
  158.     vLayout1->addWidget( d_intervalWheel );
  159.     vLayout1->addWidget( d_timerWheel );
  160.     vLayout1->addStretch( 10 );
  161.     vLayout1->addWidget( d_amplitudeKnob );
  162.     vLayout1->addWidget( d_frequencyKnob );
  163.  
  164.  
  165.    //  vLayout1->addWidget(serialLabel);
  166.  
  167. //    QHBoxLayout *layout2 = new QHBoxLayout( this );
  168. //    layout2->addWidget(_connectButton);
  169. //    layout2->addWidget(_disconnectButton);
  170.  
  171.      QVBoxLayout* vLayout2 = new QVBoxLayout();
  172.      vLayout2 ->addWidget(serialLabel);
  173.      vLayout2->addWidget(_portComboBox);
  174.      //vLayout2->addLayout(layout2);
  175.      vLayout2->addWidget(_connectButton);
  176.      vLayout2->addWidget(_disconnectButton);
  177.      vLayout2->addWidget(_firmwareLabel);
  178.      vLayout2->addSpacerItem(new QSpacerItem(3,3,QSizePolicy::Expanding));
  179.      vLayout2 ->addWidget(serialLabel2);
  180.      vLayout2->addWidget(_lcddisplay);
  181.  
  182.  
  183.  
  184.     QHBoxLayout *layout = new QHBoxLayout( this );
  185.     layout->addWidget( d_plot, 10 );
  186.     layout->addLayout( vLayout1 );
  187.     layout->addSpacing(30);
  188.     layout->addLayout( vLayout2 );
  189.  
  190.  
  191.  
  192. //    _connectButton = new QPushButton(tr("connect"),this);
  193. //    _disconnectButton = new QPushButton(tr("disconnect"),this);
  194. //    _firmwareLabel = new QLabel();
  195.  
  196.  
  197. //   QWidget *widget = new QWidget(this);
  198.  
  199. //   QWidget *serialWidget = new QWidget(this);
  200. //   QVBoxLayout *serialWidgetLayout = new QVBoxLayout(serialWidget);
  201. //   serialWidget->setLayout(serialWidgetLayout);
  202. //   serialWidgetLayout->addWidget(_portComboBox);
  203. //  serialWidgetLayout->addWidget(_connectButton);
  204. //   serialWidgetLayout->addWidget(_disconnectButton);
  205. //   serialWidgetLayout->addWidget(_firmwareLabel);
  206. //
  207.  
  208.  
  209.  
  210.  
  211.  
  212.   //layout->addWidget(serialLabel);
  213.    //layout->addLayout(serialWidgetLayout);
  214.  
  215. //    d_samplingThread.setFrequency( frequency() );
  216. //    d_samplingThread.setAmplitude( amplitude() );
  217. //    d_samplingThread.setInterval( signalInterval() );
  218.  
  219. //    connect( d_amplitudeKnob, SIGNAL( valueChanged( double ) ),
  220. //        SLOT( amplitudeChanged( double ) ) );
  221. //    connect( d_frequencyKnob, SIGNAL( valueChanged( double ) ),
  222. //        SLOT( frequencyChanged( double ) ) );
  223. //    connect( d_timerWheel, SIGNAL( valueChanged( double ) ),
  224. //        SLOT( signalIntervalChanged( double ) ) );
  225.  
  226. //    connect( d_intervalWheel, SIGNAL( valueChanged( double ) ),
  227. //        d_plot, SLOT( setIntervalLength( double ) ) );
  228.  
  229.     connect(_firmata, SIGNAL(firmwareVersionReceived(int, int)), this, SLOT(onFirmwareVersionReceived(int,int)));
  230.     connect(_firmata, SIGNAL(firmwareNameReceived(QString)), this, SLOT(onFirmwareNameReceived(QString)));
  231.     connect(_firmata, SIGNAL(initialized(int,int,QString)), this, SLOT(onInitialized(int,int,QString)));
  232.     connect(_firmata, SIGNAL(digitalPinChanged(int)), this, SLOT(onDigitalPinChanged(int)));
  233.     connect(_firmata, SIGNAL(analogPinChanged(int)), this, SLOT(onAnalogPinChanged(int)));
  234.  
  235.  
  236.  
  237.  
  238.     connect(_connectButton, SIGNAL(clicked()), this, SLOT(on_pushButtonConnect_clicked()));
  239.      connect(_disconnectButton, SIGNAL(clicked()), this, SLOT(on_pushButtonDisconnect_clicked()));
  240.     connect(d_samplingThread, SIGNAL(transfeAnalog(int)), this, SLOT(samplesValueThis()));
  241.     // connect(_firmata, SIGNAL(tranferAnalog(int) ),this, SLOT( samplesValueThis() )  );
  242. }
  243.  
  244.  
  245. void MainWindow::start()
  246. {
  247.     d_plot->start();
  248.  
  249. }
  250.  
  251. double MainWindow::frequency() const
  252. {
  253.     return d_frequencyKnob->value();
  254.     qDebug()<<"frequency";
  255. }
  256.  
  257. double MainWindow::amplitude() const
  258. {
  259.     return d_amplitudeKnob->value();
  260.     qDebug()<<"amplitude";
  261. }
  262.  
  263. double MainWindow::signalInterval() const
  264. {
  265.     return d_timerWheel->value();
  266. }
  267.  
  268.  
  269. void MainWindow::onFirmwareVersionReceived(const int majorVersion, const int minorVersion)
  270. {
  271.     qDebug()<<"major"<<majorVersion<<"minor"<<minorVersion;
  272. }
  273.  
  274. void MainWindow::onFirmwareNameReceived(QString firmwareName)
  275. {
  276.     qDebug()<<"firmware name"<<firmwareName;
  277. }
  278.  
  279. void MainWindow::onInitialized(const int majorVersion, const int minorVersion, QString firmwareName)
  280. {
  281.     qDebug()<<"firmate initialized"<<majorVersion<<minorVersion<<firmwareName;
  282.    //statusBar()->showMessage(tr("initialized"), 5000);
  283.  
  284.   // QString statusText = QString::number(majorVersion)+"."+QString::number(minorVersion);
  285.  // _firmata->send;
  286.  
  287. }
  288.  
  289. void MainWindow::onAnalogPinChanged(int pin)
  290. {
  291.  
  292.  
  293. analoginp = _firmata->getAnalog(pin);
  294. qDebug()<<"analog pin change"<<pin<<"changed. new value"<<analoginp;
  295. _lcddisplay->display(analoginp);
  296.  
  297.  Q_EMIT transferAnalog(analoginp);
  298.  
  299.  
  300.  
  301.  
  302. }
  303.  
  304.  
  305. void MainWindow::onAnalogPinReportinChange(unsigned int pin, bool value)
  306. {
  307.     qDebug()<<"analog pin reporting"<<pin<<"changed. new value"<<value;
  308.  
  309.     if(value)
  310.     {
  311.         _firmata->sendAnalogPinReporting(pin, ARD_ANALOG);
  312.     }
  313.     else
  314.     {
  315.         _firmata->sendAnalogPinReporting(pin, ARD_OFF);
  316.     }
  317. }
  318.  
  319. void MainWindow::onDigitalPinChanged(int pin)
  320. {
  321.     qDebug()<<"digital pin"<<pin<<"changed. new value"<<_firmata->getDigital(pin);
  322.  
  323. }
  324.  
  325. void MainWindow::on_pushButtonConnect_clicked()
  326. {
  327.     _firmata->open(_portComboBox->currentText());
  328.   //statusBar()->showMessage(tr("[Notice] Successfully open connection"), 5000);
  329.  
  330.     _firmata->sendAnalogPinReporting(3, ARD_ANALOG);
  331.  
  332.    _firmata->sendDigitalPinMode(13,ARD_OUTPUT);
  333. }
  334.  
  335.  
  336. void MainWindow::on_pushButtonDisconnect_clicked()
  337. {
  338.     _firmata->close();
  339.   //   _connectButton->show();
  340.   //  _disconnectButton->hide();
  341.   // _firmwareLabel->setText("");
  342.   //  statusBar()->showMessage(tr("[Notice] Successfully closed connection"), 5000);
  343.  
  344. }
  345.  
  346.  
  347. void MainWindow::on_pushButtonConnect_2_clicked()
  348. {
  349.    _firmata->sendDigital(13,ARD_ON);
  350.  
  351. }
  352.  
  353. void MainWindow::on_pushButtonDisconnect_2_clicked()
  354. {
  355.     _firmata->sendDigital(13,ARD_OFF);
  356. }
  357.  
  358.  
  359. void MainWindow::samplesValueThis()
  360. {
  361.     d_samplingThread -> setSampleValue(analoginp);
  362. }
  363.  
  364.  
  365.  
  366.  
  367.  
  368.  
  369. ----------------------------------------------------------------------------------------------------
  370.  
  371.  
  372.  
  373. #include <qwt_sampling_thread.h>
  374. #include <qfirmata.h>
  375.  
  376. class SamplingThread: public QwtSamplingThread
  377. {
  378.     Q_OBJECT
  379.  
  380. public:
  381.     SamplingThread( QObject *parent = NULL );
  382.  
  383.     double frequency() const;
  384.     double amplitude() const;
  385.     int analoginput;
  386.     int pin;
  387.     static int analoginp;
  388.  
  389.  
  390. public Q_SLOTS:
  391.     void setAmplitude( double );
  392.     void setFrequency( double );
  393.     void setSampleValue(int);
  394. //    void onFirmwareVersionReceived(const int majorVersion, const int minorVersion);
  395. //    void onFirmwareNameReceived(QString firmwareName);
  396. //    void onInitialized(const int majorVersion, const int minorVersion, QString firmwareName);
  397. //    void onAnalogPinChanged(int pin);
  398. //    void onAnalogPinReportinChange(unsigned int pin, bool value);
  399. //    void onDigitalPinChanged(int pin);
  400.  
  401. protected:
  402.     virtual void sample( double elapsed );
  403.  
  404. private:
  405.     virtual double value( double timeStamp ) const;
  406.  
  407.     double d_frequency;
  408.     double d_amplitude;
  409.     QFirmata *_firmata2;
  410.     double sampleValue;
  411. };
  412.  
  413.  
  414.  
  415.  
  416.  
  417.  
  418.  
  419. #include "samplingthread.h"
  420. #include "signaldata.h"
  421. #include <qwt_math.h>
  422. #include <math.h>
  423. #include "qfirmata.h"
  424. #include "mainwindow.h"
  425.  
  426. #if QT_VERSION < 0x040600
  427. #define qFastSin(x) ::sin(x)
  428. #endif
  429.  
  430. SamplingThread::SamplingThread( QObject *parent ):
  431.     QwtSamplingThread( parent ),
  432.     d_frequency( 5.0 ),
  433.     d_amplitude( 20.0 ),
  434.   sampleValue (0)
  435.  
  436.  
  437.  
  438. {
  439.  
  440. }
  441.  
  442. void SamplingThread::setFrequency( double frequency )
  443. {
  444.     d_frequency = frequency;
  445.     qDebug()<<"display frequency"<<d_frequency;
  446. }
  447.  
  448. double SamplingThread::frequency() const
  449. {
  450.     return d_frequency;
  451. }
  452.  
  453. void SamplingThread::setAmplitude( double amplitude )
  454. {
  455.     d_amplitude = amplitude;
  456. }
  457.  
  458. double SamplingThread::amplitude() const
  459. {
  460.     return d_amplitude;
  461. }
  462.  
  463. void SamplingThread::sample( double elapsed )
  464. {
  465.     if ( d_frequency > 0.0 )
  466.     {
  467.         const QPointF s( elapsed, value( elapsed ) );
  468.         SignalData::instance().append( s );
  469.     }
  470. }
  471.  
  472. double SamplingThread::value( double timeStamp ) const
  473. {
  474.    //analoginput = MainWindow::analoginp;
  475.     const double period = 1.0 / d_frequency;
  476.  
  477.     const double x = ::fmod( timeStamp, period );
  478.     //const double v = d_amplitude * analoginput;
  479.    //const double v = d_amplitude * qFastSin( x / period * 2 * M_PI );
  480.     //const double v = d_amplitude;
  481.    const double v = d_amplitude * sampleValue * 0.001;
  482.    qDebug()<<"value V"<< v;
  483.    qDebug()<<"amplitude"<< d_amplitude;
  484.    qDebug()<<"SV"<< sampleValue;
  485.     return v;
  486.  
  487. }
  488.  
  489. void SamplingThread::setSampleValue(int samples)
  490. {
  491.   analoginp = (double)samples;
  492.   qDebug()<<"sample value"<< analoginp << "nilai sample" << samples;
  493.  
  494.  
  495. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement