Treehopper C++ API
TreehopperUsb.h
Go to the documentation of this file.
1 #pragma once
2 
3 #include "Treehopper.h"
4 #include "UsbConnection.h"
5 #include <memory>
6 #include <vector>
7 #include "Pin.h"
8 #include "HardwarePwmManager.h"
9 #include "HardwarePwm.h"
10 #include "HardwareI2c.h"
11 #include "HardwareSpi.h"
12 #include "HardwareUart.h"
13 #include <thread>
14 
15 using namespace std;
16 
17 namespace Treehopper {
18  class Pwm;
19 
20  class I2c;
21 
60  friend Pin;
61  friend HardwareI2c;
62  friend UsbConnection;
63  friend HardwarePwmManager;
64  friend HardwareSpi;
65  friend HardwareUart;
66 
67  public:
68  TreehopperUsb(UsbConnection &connection);
69 
70  ~TreehopperUsb();
71 
73  connection = rhs.connection;
74  isConnected = rhs.isConnected;
75 
76  return *this;
77  }
78 
79  TreehopperUsb(const TreehopperUsb &rhs) : TreehopperUsb(rhs.connection) {
80  connection = rhs.connection;
81  isConnected = rhs.isConnected;
82  }
83 
87  void reinitialize();
88 
91 
93  bool connect();
94 
96  void disconnect();
97 
99  wstring serialNumber();
100 
102  wstring name();
103 
105  void led(bool value);
106 
108  bool led();
109 
111  wstring toString() {
112  wstring output = name() + L" (" + serialNumber() + L")";
113  return output;
114  }
115 
117  vector<Pin> pins;
118 
120  const int numberOfPins = 20;
121 
124 
127 
130 
133 
136 
139  private:
140  void sendPinConfigPacket(uint8_t *data, size_t len);
141 
142  void sendPeripheralConfigPacket(uint8_t *data, size_t len);
143 
144  void receivePeripheralConfigPacket(uint8_t *data, size_t numBytesToRead);
145 
146  enum class DeviceCommands {
147  Reserved = 0, // Not implemented
148  ConfigureDevice, // Sent upon board connect/disconnect
149  PwmConfig,
150  UartConfig,
151  I2cConfig,
152  SpiConfig,
153  I2cTransaction,
154  SpiTransaction,
155  UartTransaction,
156  SoftPwmConfig,
157  FirmwareUpdateSerial,
158  FirmwareUpdateName,
159  Reboot,
160  EnterBootloader,
161  LedConfig,
162  ParallelConfig,
163  ParallelTransaction
164  };
165  UsbConnection &connection;
166  thread pinListenerThread;
167 
168  void pinStateListener();
169 
170  uint8_t buffer[41];
171  bool _led;
172  bool _isDestroyed = false;
173  };
174 }
Built-in SPI peripheral.
Definition: HardwareSpi.h:65
HardwarePwmManager pwmManager
Gets the HardwarePwmManager module.
Definition: TreehopperUsb.h:138
Built-in UART peripheral.
Definition: HardwareUart.h:53
HardwarePwm pwm2
Gets the HardwarePwm PWM2 pin.
Definition: TreehopperUsb.h:132
HardwarePwm pwm1
Gets the HardwarePwm PWM1 pin.
Definition: TreehopperUsb.h:129
Built-in I2C module.
Definition: HardwareI2c.h:101
Built-in hardware PWM channels.
Definition: HardwarePwm.h:19
HardwareI2c i2c
Gets the HardwareI2c interface.
Definition: TreehopperUsb.h:123
bool isConnected
Gets whether or not the board is connected.
Definition: TreehopperUsb.h:90
wstring toString()
Gets a string representation of the board.
Definition: TreehopperUsb.h:111
HardwareSpi spi
Gets the HardwareSpi interface.
Definition: TreehopperUsb.h:126
TreehopperUsb(const TreehopperUsb &rhs)
Definition: TreehopperUsb.h:79
vector< Pin > pins
Gets a collection of pins on the board.
Definition: TreehopperUsb.h:117
The core class for communicating with Treehopper USB boards.
Definition: TreehopperUsb.h:59
Definition: AdcPin.h:3
Hardware PWM manager
Definition: HardwarePwmManager.h:19
#define TREEHOPPER_API
Definition: Treehopper.h:17
Definition: UsbConnection.h:9
TreehopperUsb & operator=(const TreehopperUsb &rhs)
Definition: TreehopperUsb.h:72
Built-in I/O pins.
Definition: Pin.h:159
HardwarePwm pwm3
Gets the HardwarePwm PWM3 pin.
Definition: TreehopperUsb.h:135