Browse Source

fixed usbFunctionWrite called multiple times

master
Robin Thoni 8 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
 UsbRawDevice::UsbRawDevice()
9
 UsbRawDevice::UsbRawDevice()
10
     : _dataSend(0),
10
     : _dataSend(0),
11
       _dataSendLen(0),
11
       _dataSendLen(0),
12
-      _callback(0)
12
+      _callback(0),
13
+      _dataReceivePos(0),
14
+      _dataReceiveLen(0)
13
 {
15
 {
14
 }
16
 }
15
 
17
 
49
 usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
51
 usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
50
 {
52
 {
51
     _rq = *rq;
53
     _rq = *rq;
54
+    _dataReceiveLen = min((uchar)rq->wLength.word, USB_RAW_DEVICE_BUFFER_SIZE);
55
+    _dataReceivePos = 0;
52
     if (_callback)
56
     if (_callback)
53
     {
57
     {
54
         _callback(rq, this, 0, 0);
58
         _callback(rq, this, 0, 0);
61
 
65
 
62
 uchar UsbRawDevice::_usbFunctionWrite(uchar *data, uchar len)
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
 void UsbRawDevice::setCallback(void (*callback)(CALLBACK_ARGS))
83
 void UsbRawDevice::setCallback(void (*callback)(CALLBACK_ARGS))

+ 10
- 0
UsbRaw.h View File

13
 
13
 
14
 #define CALLBACK_ARGS usbRequest_t* rq, UsbRawDevice* dev, uchar* data, uchar len
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
 class UsbRawDevice {
20
 class UsbRawDevice {
17
 public:
21
 public:
18
     UsbRawDevice();
22
     UsbRawDevice();
42
     void (*_callback)(CALLBACK_ARGS);
46
     void (*_callback)(CALLBACK_ARGS);
43
 
47
 
44
     usbRequest_t _rq;
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
 extern UsbRawDevice UsbRaw;
57
 extern UsbRawDevice UsbRaw;

Loading…
Cancel
Save