BPLightContraption
 All Classes Namespaces Files Functions Variables Macros Pages
microcont.h
Go to the documentation of this file.
1 /*! \file
2  * \brief Class for controlling the microcontroller
3  * \author Benjamin Pritchard (ben@bennyp.org)
4  * \copyright 2013 Benjamin Pritchard. Released under the MIT License
5  */
6 
7 #ifndef MICROCONT_H
8 #define MICROCONT_H
9 
10 #include <QSharedPointer>
11 #include <QtSerialPort/QSerialPort>
12 #include <QtSerialPort/QSerialPortInfo>
13 
14 #define MICROCONTROLLER_FCPU 16000000ul
15 
16 //! This class represents a microcontroller
17 /*!
18  * This command is mostly used to connect and send commands.
19  */
20 class MCInterface : public QObject
21 {
22 public:
23 
24  //! Initializes the interface
25  MCInterface();
26 
27  //! Opens the specified port
28  /*!
29  * After opening the port, it waits for the identification string.
30  * If something goes wrong, it throws a MCInterfaceException (through ThrowException())
31  */
32  void OpenPort(const QString &port);
33 
34  //! Closes the serial connection with the microcontroller
35  void ClosePort(void);
36 
37  //! Resets the port (closes and then reopens the port)
38  void ResetPort(void);
39 
40  //! Returns true if the port is opened
41  bool IsOpen(void);
42 
43 
44  //! Sends a command to the microcontroller
45  /*!
46  * Commands are stored in an array of bytes. The reason for this
47  * is that it is a bit cleaner to write the commands, ie
48  *
49  * \code
50  * uint8_t infocmd[2] = {'\\',COM_INFO};
51  * \endcode
52  *
53  * \param command Array of bytes to send
54  * \param len The length of the command to send
55  * \param expectedreslen The length of the result expected (not including the 3 header bytes)
56  * \param timeout The amount of time to wait for a response from the microcontroller (in ms)
57  * \throw MCInterfaceException An error occurred during sending
58  * the command or receiving the response
59  */
60  QByteArray SendCommand(const quint8 * command, int len, unsigned int expectedreslen, int timeout = 500);
61 
62 
63  //! Returns the current error state of the microcontroller
64  /*!
65  * See ConvertMCError()
66  */
67  int GetMCError(void) const;
68 
69 
70  //! Returns the id returned from the microcontroller during an error
71  /*!
72  * The ID should represent which PowerUnit had a problem
73  */
74  int GetMCErrorID(void) const;
75 
76  //! Returns the command returned from the microcontroller during an error
77  /*!
78  * The ID should represent which command had a problem
79  */
80  int GetMCErrorCMD(void) const;
81 
82  //! Returns the error code from the serial port
83  /*!
84  * See documentation for QSterialPort
85  */
86  QSerialPort::SerialPortError GetSPError(void) const;
87 
88 
89  //! Gets state info from the microcontroller
90  /*!
91  * Information, including dimmer levels, timestamps, etc, are
92  * stored in a specific way in a QByteArray
93  *
94  * \throw MCInterfaceException An error occurred during sending
95  * the command or receiving the response
96  */
97  QByteArray RetrieveInfo(void);
98 
99 private:
100 
101  //! Disables copying of this class
103 
104  //! Serial port object
105  QSerialPort _sp;
106 
107  //! Microcontroller error flag
108  int _mcerror;
109 
110  //! Command returned by the microcontroller during an error
112 
113  //! ID of the power unit, etc, causing the error
115 
116 
117  //! Throws an exception using the current error numbers
118  /*!
119  * This also taks a description
120  */
121  void ThrowException(const QString & desc) const;
122 };
123 
124 
125 
126 #endif // MICROCONT_H