Dooble
dooble_charts_iodevice.h
1 /*
2 ** Copyright (c) 2008 - present, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Dooble without specific prior written permission.
15 **
16 ** DOOBLE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** DOOBLE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef dooble_charts_iodevice_h
29 #define dooble_charts_iodevice_h
30 
31 #include <QIODevice>
32 #include <QReadWriteLock>
33 #include <QTimer>
34 #include <QtDebug>
35 
36 class dooble_charts_iodevice: public QIODevice
37 {
38  Q_OBJECT
39 
40  public:
41  dooble_charts_iodevice(QObject *parent, const int index):QIODevice(parent)
42  {
43 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
44  m_finished.store(0);
45  m_index.store(index);
46 #else
47  m_finished.storeRelaxed(0);
48  m_index.storeRelaxed(index);
49 #endif
50  m_read_interval = 256;
51  m_read_size = 1024;
52  m_read_timer.setInterval(m_read_interval);
53  }
54 
55  virtual ~dooble_charts_iodevice()
56  {
57  m_read_timer.stop();
58  }
59 
60  virtual void pause(void)
61  {
62  m_read_timer.stop();
63  }
64 
65  virtual void play(void)
66  {
67 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
68  m_finished.store(0);
69 #else
70  m_finished.storeRelaxed(0);
71 #endif
72  m_read_timer.start();
73  }
74 
75  virtual void rewind(void)
76  {
77  }
78 
79  virtual void stop(void)
80  {
81 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
82  m_finished.store(1);
83 #else
84  m_finished.storeRelaxed(1);
85 #endif
86  m_read_timer.stop();
87  }
88 
89  void set_address(const QString &address)
90  {
91  QWriteLocker locker(&m_address_mutex);
92 
93  m_address = address;
94  }
95 
96  void set_data_extraction_script(const QString &program)
97  {
98  m_program = program.trimmed();
99  }
100 
101  void set_read_interval(const int interval)
102  {
103  m_read_interval = qMax(1, interval);
104  m_read_timer.setInterval(m_read_interval);
105  }
106 
107  void set_read_rate(const QString &rate)
108  {
109  auto list(rate.split('/'));
110 
111  set_read_interval(list.value(1).trimmed().toInt());
112  set_read_size(list.value(0).trimmed().toInt());
113  }
114 
115  void set_read_size(const int size)
116  {
117 #if (QT_VERSION < QT_VERSION_CHECK(5, 14, 0))
118  m_read_size.store(qMax(1, size));
119 #else
120  m_read_size.storeRelaxed(qMax(1, size));
121 #endif
122  }
123 
124  void set_type(const QString &type)
125  {
126  m_type = type.trimmed();
127  }
128 
129  protected:
130  QAtomicInteger<short> m_finished;
131  QAtomicInteger<int> m_index;
132  QAtomicInteger<int> m_read_size;
133  QReadWriteLock m_address_mutex;
134  QString m_address;
135  QString m_program;
136  QString m_type;
137  QTimer m_read_timer;
138  int m_read_interval;
139 
140  signals:
141  void data_ready(const QVector<double> &vector, const int index);
142 };
143 
144 #endif
Definition: dooble_charts_iodevice.h:37