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.
Dependencies: mbed DebounceIn USBDevice PinDetect
Revision 8:a6ff9fbacf85, committed 2014-09-29
- Comitter:
- emilychen55
- Date:
- Mon Sep 29 03:03:54 2014 +0000
- Parent:
- 7:7abb32ab30b6
- Commit message:
- Final Code with HID Reporting Mechanisms
Changed in this revision
| main.cpp | Show annotated file Show diff for this revision Revisions of this file |
diff -r 7abb32ab30b6 -r a6ff9fbacf85 main.cpp
--- a/main.cpp Sun Sep 28 22:02:27 2014 +0000
+++ b/main.cpp Mon Sep 29 03:03:54 2014 +0000
@@ -1,13 +1,21 @@
#include "mbed.h"
+#include "PinDetect.h"
#include "USBKeyboard.h"
#include "DebounceIn.h"
-BusOut leds(LED1, LED2, LED3);
+#define PINDETECT_SAMPLE_FREQUENCY 10000
+
+DigitalOut led(LED_GREEN);
+DebounceIn mode(D12);
DebounceIn accelerate(D11);
DebounceIn powerUp(D10);
DebounceIn breakz(D13);
+DebounceIn up(D9);
+DebounceIn down(D7);
+DebounceIn left(D6);
+DebounceIn right(D8);
-Serial pc(USBTX, USBRX);
+HID_REPORT report;
AnalogIn accelZ(A0);
AnalogIn accelY(A1);
@@ -22,51 +30,93 @@
float CENTER_RIGHT = 0.51;
float CENTER_UP = 0.47;
float CENTER_DOWN = 0.57;
+int flag = 1; // to change modes between directions via 1.buttons or 2.accelerometer
int main(void) {
- pc.baud(115200);
- while (1) {
- if (!accelerate) {
- controller.keyCode('x');
+ report.data[0] = 1;
+ report.length = 9;
+ while (true) {
+ if (!mode) {
+ flag = !flag;
+ wait(0.5);
+ }
+
+ //KL25Z Debugging Purposes
+ led=!led;
+ wait(0.05);
+
+ //Mode 1: Accelerometer
+ if (flag) {
+ if (!accelerate) {
+ report.data[3] = 0x1B; //x
+ } else {
+ report.data[3] = 0;
+ }
+ if (!powerUp) {
+ report.data[8] = 0x16; //s
+ } else {
+ report.data[8] = 0;
+ }
if (accelX.read() <= CENTER_LEFT) {
- //controller.move(-1, 0);
- controller.keyCode(LEFT_ARROW);
- controller.keyCode('x');
+ report.data[4] = 0x50;
+ } else {
+ report.data[4] = 0;
}
if (accelX.read() >= CENTER_RIGHT) {
- //controller.move(1, 0);
- controller.keyCode(RIGHT_ARROW);
- controller.keyCode('x');
+ report.data[5] = 0x4F;
+ } else {
+ report.data[5] = 0;
}
- }
- if (!powerUp) {
- controller.keyCode('x');
- controller.keyCode('s');
- controller.keyCode('x');
- }
- if (!breakz) {
- controller.keyCode('z');
+ if (accelZ.read() <= CENTER_UP) {
+ report.data[6] = 0x52;
+ } else if (!breakz) {
+ report.data[6] = 0x1D; //z
+ } else {
+ report.data[6] = 0;
+ }
+ if (accelZ.read() >= CENTER_DOWN) {
+ report.data[7] = 0x51;
+ } else {
+ report.data[7] = 0;
+ }
+ controller.send(&report);
}
- // Navigation in Game Menu
- if (accelX.read() <= CENTER_LEFT) {
- controller.keyCode(LEFT_ARROW);
- pc.printf("left\n");
- wait(0.1);
- }
- if (accelX.read() >= CENTER_RIGHT) {
- controller.keyCode(RIGHT_ARROW);
- pc.printf("right\n");
- wait(0.1);
- }
- if (accelZ.read() <= CENTER_UP) {
- controller.keyCode(UP_ARROW);
- pc.printf("up\n");
- wait(0.1);
- }
- if (accelZ.read() >= CENTER_DOWN) {
- controller.keyCode(DOWN_ARROW);
- pc.printf("down\n");
- wait(0.1);
+
+ //Mode 2: Buttons
+ if (!flag) {
+ if (!accelerate) {
+ report.data[3] = 0x1B; //x
+ } else {
+ report.data[3] = 0;
+ }
+ if (!powerUp) {
+ report.data[8] = 0x16; //s
+ } else {
+ report.data[8] = 0;
+ }
+ if (!up) {
+ report.data[4] = 0x52;
+ } else if (!breakz) {
+ report.data[4] = 0x1D; //z
+ } else {
+ report.data[4] = 0;
+ }
+ if (!down) {
+ report.data[5] = 0x51;
+ } else {
+ report.data[5] = 0;
+ }
+ if (!left) {
+ report.data[6] = 0x50;
+ } else {
+ report.data[6] = 0;
+ }
+ if (!right) {
+ report.data[7] = 0x4F;
+ } else {
+ report.data[7] = 0;
+ }
+ controller.send(&report);
}
}
}
\ No newline at end of file
