BPLightContraption
 All Classes Namespaces Files Functions Variables Macros Pages
triaclight.cpp
Go to the documentation of this file.
1 /*! \file
2  * \brief Main Qt GUI class
3  * \author Benjamin Pritchard (ben@bennyp.org)
4  * \copyright 2013 Benjamin Pritchard. Released under the MIT License
5  */
6 
7 #include "triaclight.h"
8 #include "commands-text.h"
9 #include "microcont.h"
10 #include "ui_triaclight.h"
11 
12 #include <QMessageBox>
13 #include <QString>
14 #include <QTextStream>
15 #include <QDebug>
16 #include <QTimer>
17 #include <QStandardItemModel>
18 
19 #include <QtSerialPort/QSerialPort>
20 #include <QtSerialPort/QSerialPortInfo>
21 //QT_USE_NAMESPACE_SERIALPORT
22 
23 
25  QMainWindow(parent),ui(new Ui::BPLightContraption),mc(new MCInterface)
26 {
27  ui->setupUi(this);
28 
29  int count = 0;
30  foreach (const QSerialPortInfo &info, QSerialPortInfo::availablePorts())
31  {
32  ui->serialPortCombo->addItem(info.portName());
33  QString tooltip("Manufacturer: ");
34  if(info.manufacturer() != "")
35  tooltip.append(info.manufacturer());
36  else
37  tooltip.append("None");
38 
39  tooltip.append("\nDescription: ");
40 
41  if(info.description() != "")
42  tooltip.append(info.description());
43  else
44  tooltip.append("None");
45  ui->serialPortCombo->setItemData(count++, tooltip, Qt::ToolTipRole);
46  }
47 
48 
49  pus.push_back(QSharedPointer<PUInterfaceGUI>(new PUInterfaceGUI(PU_LIGHT1, ConvertPUID(PU_LIGHT1), mc, this)));
50  pus.push_back(QSharedPointer<PUInterfaceGUI>(new PUInterfaceGUI(PU_LIGHT2, ConvertPUID(PU_LIGHT2), mc, this)));
51  pus.push_back(QSharedPointer<PUInterfaceGUI>(new PUInterfaceGUI(PU_RECEPTACLE, ConvertPUID(PU_RECEPTACLE), mc, this)));
52 
53  pus[0]->AttachToGui(ui->l1label, ui->buttonl1_on, ui->buttonl1_off, ui->l1levelslider);
54  pus[1]->AttachToGui(ui->l2label, ui->buttonl2_on, ui->buttonl2_off, ui->l2levelslider);
55  pus[2]->AttachToGui(ui->relabel, ui->buttonre_on, ui->buttonre_off, ui->relevelslider);
56 
57  connect(ui->serialPortOpenButton, SIGNAL(clicked()), this, SLOT(OpenPort()));
58  connect(ui->serialPortCloseButton, SIGNAL(clicked()), this, SLOT(ClosePort()));
59  connect(ui->serialPortReset, SIGNAL(clicked()), this, SLOT(ResetPort()));
60 
61  connect(ui->updateButton, SIGNAL(clicked()), this, SLOT(UpdateInfo()));
62  ui->updateButton->setEnabled(false);
63 
64  connect(ui->actionExit, SIGNAL(triggered()), this, SLOT(close()));
65 
66 
67  dimmerData = new QStandardItemModel(DIMMER_COUNT,3,this);
68  dimmerData->setHorizontalHeaderItem(0, new QStandardItem(QString("Power Unit")));
69  dimmerData->setHorizontalHeaderItem(1, new QStandardItem(QString("Level")));
70  dimmerData->setHorizontalHeaderItem(2, new QStandardItem(QString("Compare Val")));
71 
72  ui->dimmerTable->setModel(dimmerData);
73 
74  for(int i = 0; i < DIMMER_COUNT; i++)
75  {
76  ui->dimmerTable->setRowHeight(i,20);
77  for(int j = 0; j < 3; j++)
78  dimmerData->setItem(i,j, new QStandardItem());
79  }
80 
81 
82  ZeroDisplays();
83 
84  ui->statusBar->showMessage("Disconnected");
85 
86  // Start a timer
87  updatetimer = new QTimer();
88  connect(updatetimer, SIGNAL(timeout()), this, SLOT(UpdateInfo()));
89 }
90 
92 {
93  try {
94  ClosePort();
95  delete dimmerData;
96  delete ui;
97  delete updatetimer;
98  }
99  catch(...)
100  {
101  //eat exceptions
102  }
103 }
104 
106 {
107  try {
108  QString port = ui->serialPortCombo->currentText();
109  ui->statusBar->showMessage(QString("Connecting to %1").arg(port));
110 
111  mc->OpenPort(port);
112 
113  ui->statusBar->showMessage(QString("Connected to %1").arg(port));
114  ui->updateButton->setEnabled(true);
115 
116  for_each(pus.begin(), pus.end(), [](QSharedPointer<PUInterfaceGUI> & spu) { spu->SyncGUI(); });
117 
118  UpdateInfo();
119 
120  updatetimer->start(1000);
121  }
122  catch(const MCInterfaceException & ex)
123  {
124  ExceptionBox(ex);
125  ui->statusBar->showMessage("Disconnected");
126  }
127 }
128 
130 {
131  ui->freqrawRisingDisplay->display(0);
132  ui->freqrawFallingDisplay->display(0);
133  ui->freqrawAvgDisplay->display(0);
134 
135  ui->freqRisingDisplay->display(0);
136  ui->freqFallingDisplay->display(0);
137  ui->freqAvgDisplay->display(0);
138 
139  for(int i = 0; i < DIMMER_COUNT; i++)
140  {
141  for(int j = 0; j < 3; j++)
142  dimmerData->item(i,j)->setText("");
143  }
144 }
145 
147 {
148  mc->ClosePort();
149 
150  ui->updateButton->setEnabled(false);
151  ui->statusBar->showMessage("Disconnected");
152 
153  ZeroDisplays();
154 
155  for_each(pus.begin(), pus.end(), [](QSharedPointer<PUInterfaceGUI> & spu) { spu->Reset(); });
156 
157  updatetimer->stop();
158 }
159 
161 {
162  if(mc->IsOpen())
163  {
164  ClosePort();
165  OpenPort();
166  }
167 }
168 
169 double BPLightContraption::Convert16BitValue(quint8 low, quint8 high)
170 {
171  return low + (high << 8);
172 }
173 
174 double BPLightContraption::Display16BitValue(QLCDNumber * display, quint8 low, quint8 high)
175 {
176  double v = Convert16BitValue(low, high);
177  display->display(v);
178  return v;
179 }
180 
182 {
183  try {
184 
185  // stop the timer to prevent backups
186  updatetimer->stop();
187 
188  if(!mc->IsOpen())
189  return;
190 
191  QByteArray info = mc->RetrieveInfo();
192 
193  //qDebug() << "Byte Array:\n";
194  //for_each(info.begin(), info.end(), [](quint8 v) { qDebug() << v << "\n";});
195  //qDebug() << "\n";
196 
197  double fallingfreq = Display16BitValue(ui->freqrawFallingDisplay, info[0], info[1]);
198  double risingfreq = Display16BitValue(ui->freqrawRisingDisplay, info[2], info[3]);
199  double averagefreq = (fallingfreq + risingfreq)/2.0;
200  ui->freqrawAvgDisplay->display(averagefreq);
201 
202  fallingfreq = MICROCONTROLLER_FCPU/(fallingfreq*16.0);
203  risingfreq = MICROCONTROLLER_FCPU/(risingfreq*16.0);
204  averagefreq = MICROCONTROLLER_FCPU/(averagefreq*16.0);
205  ui->freqFallingDisplay->display(fallingfreq);
206  ui->freqRisingDisplay->display(risingfreq);
207  ui->freqAvgDisplay->display(averagefreq);
208 
209  int off = 4;
210 
211  for(int i = 0; i < DIMMER_COUNT; i++)
212  {
213  if((qint8)info[off+4*i] == 0u)
214  {
215  dimmerData->item(i,0)->setText("None");
216  dimmerData->item(i,1)->setText("-");
217  dimmerData->item(i,2)->setText("-");
218  }
219  else
220  {
221  dimmerData->item(i,0)->setText(ConvertPUID(info[off+4*i]));
222  dimmerData->item(i,1)->setText(QString("%1%").arg(quint16(info[off+1+4*i])));
223  dimmerData->item(i,2)->setText(QString("%1").arg(Convert16BitValue(info[off+2+4*i],info[off+3+4*i])));
224  }
225  }
226 
227 
228  off = 4+4*DIMMER_COUNT;
229  // Now the power units
230  /*for(int i = 0; i < PU_COUNT; i++)
231  {
232  // ignore the id at off+3*i
233  pus[i]->SyncState(info[off+1+3*i], info[off+2+3*i]);
234  }*/
235 
236  //restart the timer
237  updatetimer->start(1000);
238 
239  }
240  catch (const MCInterfaceException & ex)
241  {
242  ExceptionBox(ex);
243  }
244 }
245 
247 {
248  QString errstr;
249  QTextStream err(&errstr);
250  err << e.what() << "\n";
251  err << "\nuC Error: " << e.GetMCError() << " (" << ConvertMCError(e.GetMCError()) << ")";
252  err << "\nuC Error Cmd: " << e.GetMCErrorCMD();
253  err << "\nuC Error ID: " << e.GetMCErrorID();
254  err << "\nSP Error: " << e.GetSPError() << " (" << e.GetSPErrorText() << ")";
255 
256  QMessageBox::information(this, "BPLightContraption Error", errstr);
257 }
258