Crazyradio.cpp
Go to the documentation of this file.
1 #include "Crazyradio.h"
2 
3 #include <sstream>
4 #include <stdexcept>
5 
6 #include <libusb-1.0/libusb.h>
7 
8 enum
9 {
12  SET_DATA_RATE = 0x03,
14  SET_RADIO_ARD = 0x05,
15  SET_RADIO_ARC = 0x06,
16  ACK_ENABLE = 0x10,
20 };
21 
23  uint32_t devid)
24  : ITransport()
25  , USBDevice(0x1915, 0x7777)
26  , m_channel(0)
27  , m_address(0)
28  , m_datarate(Datarate_250KPS)
29  , m_ackEnable(true)
30 {
31  open(devid);
33  setChannel(2);
34  setContCarrier(false);
35  setAddress(0xE7E7E7E7E7);
37  setArc(3);
38  setArdBytes(32);
39  setAckEnable(true);
40 }
41 
43 {
44 }
45 
47 {
48  return USBDevice::numDevices(0x1915, 0x7777);
49 }
50 
52 {
53  sendVendorSetup(SET_RADIO_CHANNEL, channel, 0, NULL, 0);
55 }
56 
58 {
59  unsigned char a[5];
60  a[4] = (address >> 0) & 0xFF;
61  a[3] = (address >> 8) & 0xFF;
62  a[2] = (address >> 16) & 0xFF;
63  a[1] = (address >> 24) & 0xFF;
64  a[0] = (address >> 32) & 0xFF;
65 
66  // sendVendorSetup(SET_RADIO_ADDRESS, 0, 0, a, 5);
67  // unsigned char a[] = {0xe7, 0xe7, 0xe7, 0xe7, 0x02};
68 
69  /*int status =*/ libusb_control_transfer(
70  m_handle,
71  LIBUSB_REQUEST_TYPE_VENDOR,
73  0,
74  0,
75  a,
76  5,
77  /*timeout*/ 1000);
78  // if (status != LIBUSB_SUCCESS) {
79  // std::cerr << "sendVendorSetup: " << libusb_error_name(status) << std::endl;
80  // }
82 }
83 
85 {
86  sendVendorSetup(SET_DATA_RATE, datarate, 0, NULL, 0);
87  m_datarate = datarate;
88 }
89 
91 {
92  sendVendorSetup(SET_RADIO_POWER, power, 0, NULL, 0);
93 }
94 
95 void Crazyradio::setArc(uint8_t arc)
96 {
97  sendVendorSetup(SET_RADIO_ARC, arc, 0, NULL, 0);
98 }
99 
100 void Crazyradio::setArdTime(uint8_t us)
101 {
102  // Auto Retransmit Delay:
103  // 0000 - Wait 250uS
104  // 0001 - Wait 500uS
105  // 0010 - Wait 750uS
106  // ........
107  // 1111 - Wait 4000uS
108 
109  // Round down, to value representing a multiple of 250uS
110  int t = (us / 250) - 1;
111  if (t < 0) {
112  t = 0;
113  }
114  if (t > 0xF) {
115  t = 0xF;
116  }
117  sendVendorSetup(SET_RADIO_ARD, t, 0, NULL, 0);
118 }
119 
120 void Crazyradio::setArdBytes(uint8_t nbytes)
121 {
122  sendVendorSetup(SET_RADIO_ARD, 0x80 | nbytes, 0, NULL, 0);
123 }
124 
125 void Crazyradio::setAckEnable(bool enable)
126 {
127  sendVendorSetup(ACK_ENABLE, enable, 0, NULL, 0);
128  m_ackEnable = enable;
129 }
130 
131 void Crazyradio::setContCarrier(bool active)
132 {
133  sendVendorSetup(SET_CONT_CARRIER, active, 0, NULL, 0);
134 }
135 
137  const uint8_t* data,
138  uint32_t length,
139  Ack& result)
140 {
141  result.ack = false;
142  result.size = 0;
143 
144  int status;
145  int transferred;
146 
147  if (!m_handle) {
148  throw std::runtime_error("No valid device handle!");
149  }
150 
151  if (m_enableLogging) {
152  logPacket(data, length);
153  }
154 
155  // Send data
156  status = libusb_bulk_transfer(
157  m_handle,
158  /* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
159  (uint8_t*)data,
160  length,
161  &transferred,
162  /*timeout*/ 100);
163  // if (status == LIBUSB_ERROR_TIMEOUT) {
164  // return;
165  // }
166  if (status != LIBUSB_SUCCESS) {
167  throw std::runtime_error(libusb_error_name(status));
168  }
169  if (length != (uint32_t)transferred) {
170  std::stringstream sstr;
171  sstr << "Did transfer " << transferred << " but " << length << " was requested!";
172  throw std::runtime_error(sstr.str());
173  }
174 
175  // Read result
176  status = libusb_bulk_transfer(
177  m_handle,
178  /* endpoint*/ (0x81 | LIBUSB_ENDPOINT_IN),
179  (unsigned char*)&result,
180  sizeof(result) - 1,
181  &transferred,
182  /*timeout*/ 10);
183  if (status == LIBUSB_ERROR_TIMEOUT) {
184  return;
185  }
186  if (status != LIBUSB_SUCCESS) {
187  throw std::runtime_error(libusb_error_name(status));
188  }
189 
190  result.size = transferred - 1;
191 
192  if (m_enableLogging) {
193  logAck(result);
194  }
195 }
196 
198  const uint8_t* data,
199  uint32_t length)
200 {
201  int status;
202  int transferred;
203 
204  if (!m_handle) {
205  throw std::runtime_error("No valid device handle!");
206  }
207 
208  if (m_enableLogging) {
209  logPacket(data, length);
210  }
211 
212  // Send data
213  status = libusb_bulk_transfer(
214  m_handle,
215  /* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
216  (uint8_t*)data,
217  length,
218  &transferred,
219  /*timeout*/ 100);
220  // if (status == LIBUSB_ERROR_TIMEOUT) {
221  // return;
222  // }
223  if (status != LIBUSB_SUCCESS) {
224  throw std::runtime_error(libusb_error_name(status));
225  }
226  if (length != (uint32_t)transferred) {
227  std::stringstream sstr;
228  sstr << "Did transfer " << transferred << " but " << length << " was requested!";
229  throw std::runtime_error(sstr.str());
230  }
231 }
232 
234  const uint8_t* data,
235  uint32_t totalLength)
236 {
237  int status;
238  int transferred;
239 
240  if (!m_handle) {
241  throw std::runtime_error("No valid device handle!");
242  }
243 
244  if (m_enableLogging) {
245  logPacket(data, totalLength);
246  }
247 
248  // Send data
249  status = libusb_bulk_transfer(
250  m_handle,
251  /* endpoint*/ (0x01 | LIBUSB_ENDPOINT_OUT),
252  (uint8_t*)data,
253  totalLength,
254  &transferred,
255  /*timeout*/ 100);
256  // if (status == LIBUSB_ERROR_TIMEOUT) {
257  // return;
258  // }
259  if (status != LIBUSB_SUCCESS) {
260  throw std::runtime_error(libusb_error_name(status));
261  }
262  if (totalLength != (uint32_t)transferred) {
263  std::stringstream sstr;
264  sstr << "Did transfer " << transferred << " but " << totalLength << " was requested!";
265  throw std::runtime_error(sstr.str());
266  }
267 }
uint8_t m_channel
Definition: Crazyradio.h:99
void setArdTime(uint8_t us)
Definition: Crazyradio.cpp:100
static uint32_t numDevices(uint16_t idVendor, uint16_t idProduct)
Definition: USBDevice.cpp:37
uint8_t channel
Definition: crtp.h:33
void sendVendorSetup(uint8_t request, uint16_t value, uint16_t index, const unsigned char *data, uint16_t length)
Definition: USBDevice.cpp:145
void setAckEnable(bool enable)
Definition: Crazyradio.cpp:125
void setPower(Power power)
Definition: Crazyradio.cpp:90
virtual void sendPacket(const uint8_t *data, uint32_t length, ITransport::Ack &result)
Definition: Crazyradio.cpp:136
virtual ~Crazyradio()
Definition: Crazyradio.cpp:42
uint8_t length
Definition: crtp.h:22
void logPacket(const uint8_t *data, uint32_t length)
Definition: ITransport.cpp:14
Crazyradio(uint32_t devid)
Definition: Crazyradio.cpp:22
void setAddress(uint64_t address)
Definition: Crazyradio.cpp:57
bool m_enableLogging
Definition: ITransport.h:54
uint16_t address
uint8_t size
Definition: ITransport.h:21
uint64_t m_address
Definition: Crazyradio.h:100
uint8_t data[29]
Definition: crtp.h:363
uint8_t result
Definition: crtp.h:440
void setContCarrier(bool active)
Definition: Crazyradio.cpp:131
bool m_ackEnable
Definition: Crazyradio.h:102
virtual void sendPacketNoAck(const uint8_t *data, uint32_t length)
Definition: Crazyradio.cpp:197
void open(uint32_t devid)
Definition: USBDevice.cpp:77
void setArc(uint8_t arc)
Definition: Crazyradio.cpp:95
void setArdBytes(uint8_t nbytes)
Definition: Crazyradio.cpp:120
void setDatarate(Datarate datarate)
Definition: Crazyradio.cpp:84
void logAck(const Ack &ack)
Definition: ITransport.cpp:31
Datarate m_datarate
Definition: Crazyradio.h:101
static uint32_t numDevices()
Definition: Crazyradio.cpp:46
virtual void send2PacketsNoAck(const uint8_t *data, uint32_t totalLength)
Definition: Crazyradio.cpp:233
uint8_t status
Definition: crtp.h:440
libusb_device_handle * m_handle
Definition: USBDevice.h:34
void setChannel(uint8_t channel)
Definition: Crazyradio.cpp:51


crazyflie_cpp
Author(s): Wolfgang Hoenig
autogenerated on Mon Sep 28 2020 03:40:10