Browse Source

mostly done

master
Robin Thoni 8 years ago
parent
commit
debf1a65d8
3 changed files with 32 additions and 23 deletions
  1. 9
    8
      UsbRaw.cpp
  2. 3
    5
      UsbRaw.h
  3. 20
    10
      examples/UsbRawDemo1/main.ino

+ 9
- 8
UsbRaw.cpp View File

@@ -9,9 +9,7 @@ UsbRawDevice UsbRaw = UsbRawDevice();
9 9
 UsbRawDevice::UsbRawDevice()
10 10
     : _dataSend(0),
11 11
       _dataSendLen(0),
12
-      _callback(0),
13
-      _dataReceived(0),
14
-      _dataReceivedLen(0)
12
+      _callback(0)
15 13
 {
16 14
 }
17 15
 
@@ -40,12 +38,17 @@ void UsbRawDevice::setData(const uchar* data, uchar len)
40 38
 
41 39
 void UsbRawDevice::setDataString(const char* data)
42 40
 {
43
-    setData((const uchar*)data, strlen(data));
41
+    setData((const uchar*)data, strlen(data) + 1);
42
+}
43
+
44
+void UsbRawDevice::setDataUsbNoMsg()
45
+{
46
+    setData(0, USB_NO_MSG);
44 47
 }
45 48
 
46 49
 usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
47 50
 {
48
-    _rq = rq;
51
+    _rq = *rq;
49 52
     if (_callback)
50 53
     {
51 54
         _callback(rq, this, 0, 0);
@@ -58,11 +61,9 @@ usbMsgLen_t UsbRawDevice::_usbFunctionSetup(usbRequest_t* rq)
58 61
 
59 62
 uchar UsbRawDevice::_usbFunctionWrite(uchar *data, uchar len)
60 63
 {
61
-    free(_dataReceived);
62
-    _dataReceived = (uchar*)memcpy(malloc(len), data, _dataReceivedLen = len);
63 64
     if (_callback)
64 65
     {
65
-        _callback(_rq, this, _dataReceived, len);
66
+        _callback(&_rq, this, data, len);
66 67
     }
67 68
     return 1;
68 69
 }

+ 3
- 5
UsbRaw.h View File

@@ -25,6 +25,8 @@ public:
25 25
 
26 26
     void setDataString(const char* data);
27 27
 
28
+    void setDataUsbNoMsg();
29
+
28 30
     usbMsgLen_t _usbFunctionSetup(usbRequest_t* rq);
29 31
 
30 32
     uchar _usbFunctionWrite(uchar *data, uchar len);
@@ -39,11 +41,7 @@ private:
39 41
 
40 42
     void (*_callback)(CALLBACK_ARGS);
41 43
 
42
-    uchar* _dataReceived;
43
-
44
-    uchar _dataReceivedLen;
45
-
46
-    usbRequest_t* _rq;
44
+    usbRequest_t _rq;
47 45
 };
48 46
 
49 47
 extern UsbRawDevice UsbRaw;

+ 20
- 10
examples/UsbRawDemo1/main.ino View File

@@ -9,9 +9,21 @@ uchar buffer[50];
9 9
 
10 10
 void callback(CALLBACK_ARGS)
11 11
 {
12
-    if (rq->bRequest == 0)
12
+    /*digitalWrite(ledPin, !digitalRead(ledPin));
13
+    delay(100);*/
14
+
15
+    /*if (data)
13 16
     {
17
+        strcpy((char *) buffer, (const char *) data);
18
+
14 19
         digitalWrite(ledPin, !digitalRead(ledPin));
20
+        delay(200);
21
+        digitalWrite(ledPin, !digitalRead(ledPin));
22
+    }*/
23
+
24
+    if (rq->bRequest == 0)
25
+    {
26
+        digitalWrite(ledPin, 0);
15 27
     }
16 28
     else if (rq->bRequest == 1)
17 29
     {
@@ -19,23 +31,21 @@ void callback(CALLBACK_ARGS)
19 31
     }
20 32
     else if (rq->bRequest == 4)
21 33
     {
22
-        digitalWrite(ledPin, !digitalRead(ledPin));
23
-        /*if (!data)
34
+        if (data)
24 35
         {
25
-            dev->setData(0, USB_NO_MSG);
36
+            digitalWrite(ledPin, !digitalRead(ledPin));
37
+            delay(200);
38
+            digitalWrite(ledPin, !digitalRead(ledPin));
39
+            strcpy((char *) buffer, (const char *) data);
26 40
         }
27 41
         else
28 42
         {
29
-            dev->setData(data, sizeof(data));
30
-        }*/
31
-        if (data)
32
-        {
33
-            dev->setDataString("response");
43
+            dev->setDataUsbNoMsg();
34 44
         }
35 45
     }
36 46
     else
37 47
     {
38
-        dev->setData(data, strlen((const char*)data) + 1);
48
+        dev->setData(buffer, strlen((const char*)buffer) + 1);
39 49
     }
40 50
 }
41 51
 

Loading…
Cancel
Save