Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Diff: main.cpp
- Revision:
- 1:0c3b6cc480b6
- Parent:
- 0:1a3aa2e25db9
--- 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