BPLightContraption
 All Classes Namespaces Files Functions Variables Macros Pages
powerunit.cpp
Go to the documentation of this file.
1 /*! \file
2  * \brief A class describing individual, controllable power units
3  * \author Benjamin Pritchard (ben@bennyp.org)
4  * \copyright 2013 Benjamin Pritchard. Released under the MIT License
5  */
6 
7 #include <iostream>
8 #include <string>
9 #include <stdexcept>
10 #include <exception>
11 
12 #include <QApplication>
13 #include <QMessageBox>
14 #include <QString>
15 #include <QtSerialPort/QSerialPort>
16 #include <QtSerialPort/QSerialPortInfo>
17 
18 #include "powerunit.h"
19 #include "microcont.h"
20 #include "microcontexception.h"
21 #include "commands.h"
22 
23 using namespace std;
24 
25 PUInterface::PUInterface(char id, const QString &desc, QSharedPointer<MCInterface> mc)
26  : _id(id),_desc(desc),_mc(mc)
27 {
28  Reset();
29 }
30 
32 {
33 }
34 
36 {
37  return _level;
38 }
39 
41 {
42  return _desc;
43 }
44 
46 {
47  return _id;
48 }
49 
50 
52 {
53  if(_state == PUSTATE_ON)
54  TurnOff();
55  else if(_state == PUSTATE_OFF)
56  TurnOn();
57 
58  return _state;
59 }
60 
61 quint8 PUInterface::SetLevel(quint8 level)
62 {
63  quint8 command[4];
64  command[0] = '\\';
65  command[1] = COM_LEVEL;
66  command[2] = _id;
67  command[3] = level;
68 
69  _mc->SendCommand(command, 4, 0);
70 
71  _level = level;
72 
73 
74  if(level >= 100)
76  else if(level == 0)
78  else
80 
81 
82  return _level; // for now. Maybe some more complex stuff in the
83  // future with what comes back from the microcontroller
84 }
85 
86 
88 {
89  quint8 command[3];
90  command[0] = '\\';
91  command[1] = COM_OFF;
92  command[2] = _id;
93 
94  _mc->SendCommand(command, 3, 0);
95 
96  _level = 0;
98 }
99 
101 {
102  quint8 command[3];
103  command[0] = '\\';
104  command[1] = COM_ON;
105  command[2] = _id;
106 
107  _mc->SendCommand(command, 3, 0);
108 
109  _level = 100;
110  _state = PUSTATE_ON;
111 }
112 
114 {
115  _level = 0;
117 }
118 
120 {
121  return _mc->IsOpen();
122 }
123 
125 {
126  return _state;
127 }
128 
129 void PUInterface::SyncState(char state, quint8 level)
130 {
131  _state = state;
132  _level = level;
133 }
134