Browse Source

fixed usbFunctionWrite called multiple times

master
Robin Thoni 4 years ago
parent
commit
6233c52dda
2 changed files with 26 additions and 4 deletions
  1. 16
    4
      UsbRaw.cpp
  2. 10
    0
      UsbRaw.h

+ 16
- 4
UsbRaw.cpp View File

@@ -9,7 +9,9 @@ UsbRawDevice UsbRaw = UsbRawDevice();
9 9
 UsbRawDevice::UsbRawDevice()
10 10
     : _dataSend(0),
11 11
       _dataSendLen(0),
12
-      _callback(0)
12
+      _callback(0),
13
+      _dataReceivePos(0),
14
+      _dataReceiveLen(0)
13 15
 {
14 16
 }
15 17
 
@@ -49,6 +51,8 @@ void UsbRawDevice::setDataUsbNoMsg()
49 51
 usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
50 52
 {
51 53
     _rq = *rq;
54
+    _dataReceiveLen = min((uchar)rq->wLength.word, USB_RAW_DEVICE_BUFFER_SIZE);
55
+    _dataReceivePos = 0;
52 56
     if (_callback)
53 57
     {
54 58
         _callback(rq, this, 0, 0);
@@ -61,11 +65,19 @@ usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
61 65
 
62 66
 uchar UsbRawDevice::_usbFunctionWrite(uchar *data, uchar len)
63 67
 {
64
-    if (_callback)
68
+    for (uchar i = 0; i < len && _dataReceivePos < _dataReceiveLen; ++i, ++_dataReceivePos)
69
+    {
70
+        _dataReceive[_dataReceivePos] = data[i];
71
+    }
72
+    if (_dataReceiveLen == _dataReceivePos)
65 73
     {
66
-        _callback(&_rq, this, data, len);
74
+        if (_callback)
75
+        {
76
+            _callback(&_rq, this, _dataReceive, _dataReceiveLen);
77
+        }
78
+        return 1;
67 79
     }
68
-    return 1;
80
+    return 0;
69 81
 }
70 82
 
71 83
 void UsbRawDevice::setCallback(void (*callback)(CALLBACK_ARGS))

+ 10
- 0
UsbRaw.h View File

@@ -13,6 +13,10 @@
13 13
 
14 14
 #define CALLBACK_ARGS usbRequest_t* rq, UsbRawDevice* dev, uchar* data, uchar len
15 15
 
16
+#ifndef USB_RAW_DEVICE_BUFFER_SIZE
17
+#define USB_RAW_DEVICE_BUFFER_SIZE 32
18
+#endif
19
+
16 20
 class UsbRawDevice {
17 21
 public:
18 22
     UsbRawDevice();
@@ -42,6 +46,12 @@ private:
42 46
     void (*_callback)(CALLBACK_ARGS);
43 47
 
44 48
     usbRequest_t _rq;
49
+
50
+    uchar _dataReceive[USB_RAW_DEVICE_BUFFER_SIZE];
51
+
52
+    uchar _dataReceivePos;
53
+
54
+    uchar _dataReceiveLen;
45 55
 };
46 56
 
47 57
 extern UsbRawDevice UsbRaw;

Loading…
Cancel
Save