Tim H / Mbed OS UsbKnob

Dependencies:   USBDevice

Files at this revision

API Documentation at this revision

Comitter:
Timmmm
Date:
Mon Feb 13 14:56:00 2017 +0000
Parent:
0:1a3aa2e25db9
Commit message:
Various changes?

Changed in this revision

Readme.md Show annotated file Show diff for this revision Revisions of this file
TSI.lib Show diff for this revision Revisions of this file
USBDevice.lib Show annotated file Show diff for this revision Revisions of this file
WinUSBDevice.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show diff for this revision Revisions of this file
--- a/Readme.md	Fri Nov 04 22:07:07 2016 +0000
+++ b/Readme.md	Mon Feb 13 14:56:00 2017 +0000
@@ -179,6 +179,9 @@
     bool WinUSBDevice::USBCallback_request()
     {
         
+Returning false means 'I didn't handle this; carry on as you were'. Returning true means
+'I've handled this and stored a response in transfer'.
+                
 First we need a reference to the transfer structure which contains both the request information
 and provides a place to store our response. The USBDevice::transfer member is actually private
 but there is a protected function to access it. (Thank you mBed people - I always knew your APIs were the best!
@@ -187,15 +190,10 @@
         // This can never be null.
         CONTROL_TRANSFER& transfer = *getTransferPtr();
         
-Check if the setup is addressed to the device. It can also be addressed to an interface or an endpoint
-but we do not care about these. More advanced devices may do, and in that case you may wish to use
-MSOS Descriptors 2.0 which support treating interfaces separately.        
-        
-        if (transfer.setup.bmRequestType.Recipient != DEVICE_RECIPIENT)
-            return false;
-    
-Note that returning false means 'I didn't handle this; carry on as you were'. Returning true means
-'I've handled this and stored a response in transfer'.
+Requests can be addressed to the device, an interface or an endpoint. Most WinUSB requests are addressed
+to the device, but the Extended Properties request is sent to the interface, so we'll just ignore what
+the recipient is. We only have on interface anyway.
+
 
 Next we see if this is a request for the 0xEE string descriptor.
     
@@ -250,3 +248,5 @@
 And we're done! Next we need a USB class that takes advantage of this WinUSBDevice class and
 responds to our control transfers.
 
+
+...WIP...
--- a/TSI.lib	Fri Nov 04 22:07:07 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://developer.mbed.org/teams/mbed/code/TSI/#1a60ef257879
--- a/USBDevice.lib	Fri Nov 04 22:07:07 2016 +0000
+++ b/USBDevice.lib	Mon Feb 13 14:56:00 2017 +0000
@@ -1,1 +1,1 @@
-http://mbed.org/users/mbed_official/code/USBDevice/#c1549af978d6
+http://mbed.org/users/mbed_official/code/USBDevice/#01321bd6ff89
--- a/WinUSBDevice.cpp	Fri Nov 04 22:07:07 2016 +0000
+++ b/WinUSBDevice.cpp	Mon Feb 13 14:56:00 2017 +0000
@@ -1,5 +1,3 @@
-#pragma once
-
 #include "WinUSBDevice.h"
 
 #include <USBDescriptor.h>
@@ -232,6 +230,7 @@
     compatIdData.header.bcdVersion = 0x0100;
     compatIdData.header.wIndex = COMPAT_ID_INDEX;
     compatIdData.header.bCount = 1;
+    compatIdData.header.reserved1 = 0x01;
 
     compatIdData.data[0].bFirstInterfaceNumber = 0;
     copyString8(compatIdData.data[0].compatibleID, sizeof(compatIdData.data[0].compatibleID), "WINUSB");
--- a/main.cpp	Fri Nov 04 22:07:07 2016 +0000
+++ b/main.cpp	Mon Feb 13 14:56:00 2017 +0000
@@ -1,7 +1,6 @@
 #include <mbed.h>
 
 #include "USBControl.h"
-//#include "TSISensor.h"
 
 #define VENDOR_ID 0x0004
 #define PRODUCT_ID 0x0307
@@ -23,44 +22,62 @@
     }
 };
 
-USBKnob usb;
-//TSISensor touch;
+
+//DigitalOut red(LED_RED);
+//DigitalOut green(LED_GREEN);
+//DigitalOut blue(LED_BLUE);
+
+AnalogIn knob(PTC2);
 
-DigitalOut red(LED_RED);
-DigitalOut green(LED_GREEN);
-DigitalOut blue(LED_BLUE);
-
-Serial pc(USBTX, USBRX);
-
-AnalogIn knob(A0);
+// ReadKnob reads the knob until the value is sufficiently
+// changed from the previousValue. The longer time goes on the
+// more the difference has to be (up to a point).
+//
+// Values are 16 bit.
+int ReadKnob(int previousValue)
+{
+    int centiseconds = 0;
+    
+    for (;;)
+    {
+        int val = knob.read_u16();
+        
+        int difference = abs(val - previousValue);
+        
+        // TODO: Check these.
+        if (centiseconds < 10 && difference > 100)
+            return val;
+        else if (difference > 1000)
+            return val;
+        
+        Thread::wait(10);
+        if (centiseconds < 1000)
+            ++centiseconds;
+    }
+}
 
 int main(int argc, char* argv[])
 {
-    pc.baud(115200);
-    pc.printf("\n\n\nmBed WinUSB Knob Example\n");
-    red = 1;
-    green = 1;
-    blue = 1;
+    // So, for some reason this doesn't work with mBed OS 5.
+    
+    USBKnob usb;
 
     usb.connect();
     
-    // Touch sensor example (for FRDM-KL25Z).
-//    for (;;)
-//    {
-//        float f = touch.readPercentage();
-//        if (f != 0.0)
-//        {
-//            // Note that this blocks if no-one is reading it!
-//            usb.send(0xFFFFFFFF * f);
-//            green = !green;
-//            wait_ms(10);
-//        }
-//    }
+    // We'll use a time-dependent threshold. Basically we take the time
+    // since we sent the last value, and then plug it into some kind of
+    // curve to give a threshold. Then we compare the current value to
+    // the previous value, and if the difference is greater than the threshold
+    // we send it.
+    
+    const int THRESHOLD = 0x0100; // 1/256
+
+    int val = knob.read_u16();
+    
     for (;;)
     {
-        // TODO: Only send when it changes.
-        usb.send(0xFFFFFFFF * knob);
-        wait_ms(100);
+        val = ReadKnob(val);
+        usb.send(static_cast<uint32_t>(val) << 16);
     }
     return 0;
 }
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Mon Feb 13 14:56:00 2017 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#e2617cc0e17f5c3fc2bae6a589c9bcfd3d1a717b
--- a/mbed.bld	Fri Nov 04 22:07:07 2016 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,1 +0,0 @@
-http://mbed.org/users/mbed_official/code/mbed/builds/aae6fcc7d9bb
\ No newline at end of file