Initial
Dependencies: mbed USBDevice USBJoystick_SIM
Diff: main.cpp
- Revision:
- 6:d3042649530d
- Parent:
- 5:64b1b58873f6
- Child:
- 7:6e1b826a62b6
--- a/main.cpp Thu Dec 13 09:34:49 2018 +0000 +++ b/main.cpp Wed Mar 25 14:20:37 2020 +0000 @@ -30,10 +30,7 @@ USBJoystick joystick; // Variables for Heartbeat and Status monitoring -DigitalOut heartbeatLED(LED1); // -//DigitalOut myled2(LED2); -//DigitalOut myled3(LED3); -//DigitalOut myled1(LED4); +DigitalOut heartbeatLED(PA_5); // AnalogIn inX(A0); // X AnalogIn inY(A1); // Y @@ -44,6 +41,14 @@ Ticker heartbeat; Serial pc(USBTX, USBRX); // tx, rx + +// Variables for Encoder +DigitalIn a(PC_13); +DigitalIn b(PD_2); + +int8_t oldAB = 0; // Remember old encoder values A and B +int8_t stepTab[16] = {0,0,1,0,0,0,0,-1,0,0,0,1,0,0,-1,0}; // Lookup table for encoder steps 1/2 +int32_t step = 0; // number of elements in an array #define countof(x) (sizeof(x)/sizeof((x)[0])) @@ -62,7 +67,7 @@ void heartbeat_start() { - heartbeatLED=1; + heartbeatLED = 1; heartbeat.attach(&pulse, 0.5); } @@ -83,6 +88,8 @@ // until the debounce time elapses. int bt; } buttonState[NUM_OF_BUTTONS]; + + // timer for button reports static Timer buttonTimer; @@ -103,6 +110,13 @@ buttonTimer.start(); } +int8_t readEncoder() +{ + oldAB <<= 2; + oldAB &= 0x0C; + oldAB |= (a.read() << 1) | b.read(); + return stepTab[oldAB]; +} // read the button input state uint32_t readButtons() @@ -110,6 +124,9 @@ // start with all buttons off uint32_t buttons = 0; + // start encoder result with 0 + int8_t encoder = 0; + // figure the time elapsed since the last scan int dt = buttonTimer.read_ms(); @@ -158,9 +175,20 @@ if (bs->pressed) buttons |= bit; //pc.printf("Buttons: %d\n", buttons); + } } + encoder = readEncoder(); + if (encoder == -1) + { + buttons |= 0x40; + } + else if (encoder == 1) + { + buttons |= 0x80; + } + // return the new button list return buttons; }