You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

UsbRaw.cpp 1.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. //
  2. // Created by robin on 1/8/16.
  3. //
  4. #include "UsbRaw.h"
  5. UsbRawDevice UsbRaw = UsbRawDevice();
  6. UsbRawDevice::UsbRawDevice()
  7. : _dataSend(0),
  8. _dataSendLen(0),
  9. _callback(0)
  10. {
  11. }
  12. void UsbRawDevice::init()
  13. {
  14. PORTD = 0; // TODO: Only for USB pins?
  15. DDRD |= ~USBMASK;
  16. cli();
  17. usbDeviceDisconnect();
  18. usbDeviceConnect();
  19. usbInit();
  20. sei();
  21. }
  22. void UsbRawDevice::poll()
  23. {
  24. usbPoll();
  25. }
  26. void UsbRawDevice::setData(const uchar* data, uchar len)
  27. {
  28. _dataSend = data;
  29. _dataSendLen = len;
  30. }
  31. void UsbRawDevice::setDataString(const char* data)
  32. {
  33. setData((const uchar*)data, strlen(data) + 1);
  34. }
  35. void UsbRawDevice::setDataUsbNoMsg()
  36. {
  37. setData(0, USB_NO_MSG);
  38. }
  39. usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
  40. {
  41. _rq = *rq;
  42. if (_callback)
  43. {
  44. _callback(rq, this, 0, 0);
  45. }
  46. usbMsgPtr = (uchar*)_dataSend;
  47. uchar len = _dataSendLen;
  48. setData(0, 0);
  49. return len;
  50. }
  51. uchar UsbRawDevice::_usbFunctionWrite(uchar *data, uchar len)
  52. {
  53. if (_callback)
  54. {
  55. _callback(&_rq, this, data, len);
  56. }
  57. return 1;
  58. }
  59. void UsbRawDevice::setCallback(void (*callback)(CALLBACK_ARGS))
  60. {
  61. _callback = callback;
  62. }
  63. uchar usbFunctionWrite(uchar *data, uchar len)
  64. {
  65. return UsbRaw._usbFunctionWrite(data, len);
  66. }
  67. usbMsgLen_t usbFunctionSetup(uchar data[8])
  68. {
  69. return UsbRaw._usbFunctionSetup((usbRequest_t*)(void*)data);
  70. }