idd hw3

Dependencies:   mbed DebounceIn USBDevice PinDetect

Committer:
emilychen55
Date:
Mon Sep 29 03:03:54 2014 +0000
Revision:
8:a6ff9fbacf85
Parent:
7:7abb32ab30b6
Final Code with HID Reporting Mechanisms

Who changed what in which revision?

UserRevisionLine numberNew contents of line
pgao 0:84cb4cb4f860 1 #include "mbed.h"
emilychen55 8:a6ff9fbacf85 2 #include "PinDetect.h"
pgao 6:adec77323ab2 3 #include "USBKeyboard.h"
emilychen55 1:85b6b5d1528e 4 #include "DebounceIn.h"
pgao 4:c90291fd6cc9 5
emilychen55 8:a6ff9fbacf85 6 #define PINDETECT_SAMPLE_FREQUENCY 10000
emilychen55 8:a6ff9fbacf85 7
emilychen55 8:a6ff9fbacf85 8 DigitalOut led(LED_GREEN);
emilychen55 8:a6ff9fbacf85 9 DebounceIn mode(D12);
pgao 7:7abb32ab30b6 10 DebounceIn accelerate(D11);
pgao 7:7abb32ab30b6 11 DebounceIn powerUp(D10);
pgao 7:7abb32ab30b6 12 DebounceIn breakz(D13);
emilychen55 8:a6ff9fbacf85 13 DebounceIn up(D9);
emilychen55 8:a6ff9fbacf85 14 DebounceIn down(D7);
emilychen55 8:a6ff9fbacf85 15 DebounceIn left(D6);
emilychen55 8:a6ff9fbacf85 16 DebounceIn right(D8);
pgao 0:84cb4cb4f860 17
emilychen55 8:a6ff9fbacf85 18 HID_REPORT report;
pgao 0:84cb4cb4f860 19
pgao 0:84cb4cb4f860 20 AnalogIn accelZ(A0);
pgao 0:84cb4cb4f860 21 AnalogIn accelY(A1);
pgao 0:84cb4cb4f860 22 AnalogIn accelX(A2);
emilychen55 1:85b6b5d1528e 23
emilychen55 1:85b6b5d1528e 24 //USBMouseKeyboard
pgao 7:7abb32ab30b6 25 USBKeyboard controller;
pgao 0:84cb4cb4f860 26
pgao 7:7abb32ab30b6 27 //X: 0.48 - 0.51
pgao 7:7abb32ab30b6 28 //Z: 0.47 - 0.57
emilychen55 1:85b6b5d1528e 29 float CENTER_LEFT = 0.48;
emilychen55 1:85b6b5d1528e 30 float CENTER_RIGHT = 0.51;
pgao 6:adec77323ab2 31 float CENTER_UP = 0.47;
pgao 6:adec77323ab2 32 float CENTER_DOWN = 0.57;
emilychen55 8:a6ff9fbacf85 33 int flag = 1; // to change modes between directions via 1.buttons or 2.accelerometer
emilychen55 1:85b6b5d1528e 34
emilychen55 1:85b6b5d1528e 35 int main(void) {
emilychen55 8:a6ff9fbacf85 36 report.data[0] = 1;
emilychen55 8:a6ff9fbacf85 37 report.length = 9;
emilychen55 8:a6ff9fbacf85 38 while (true) {
emilychen55 8:a6ff9fbacf85 39 if (!mode) {
emilychen55 8:a6ff9fbacf85 40 flag = !flag;
emilychen55 8:a6ff9fbacf85 41 wait(0.5);
emilychen55 8:a6ff9fbacf85 42 }
emilychen55 8:a6ff9fbacf85 43
emilychen55 8:a6ff9fbacf85 44 //KL25Z Debugging Purposes
emilychen55 8:a6ff9fbacf85 45 led=!led;
emilychen55 8:a6ff9fbacf85 46 wait(0.05);
emilychen55 8:a6ff9fbacf85 47
emilychen55 8:a6ff9fbacf85 48 //Mode 1: Accelerometer
emilychen55 8:a6ff9fbacf85 49 if (flag) {
emilychen55 8:a6ff9fbacf85 50 if (!accelerate) {
emilychen55 8:a6ff9fbacf85 51 report.data[3] = 0x1B; //x
emilychen55 8:a6ff9fbacf85 52 } else {
emilychen55 8:a6ff9fbacf85 53 report.data[3] = 0;
emilychen55 8:a6ff9fbacf85 54 }
emilychen55 8:a6ff9fbacf85 55 if (!powerUp) {
emilychen55 8:a6ff9fbacf85 56 report.data[8] = 0x16; //s
emilychen55 8:a6ff9fbacf85 57 } else {
emilychen55 8:a6ff9fbacf85 58 report.data[8] = 0;
emilychen55 8:a6ff9fbacf85 59 }
pgao 6:adec77323ab2 60 if (accelX.read() <= CENTER_LEFT) {
emilychen55 8:a6ff9fbacf85 61 report.data[4] = 0x50;
emilychen55 8:a6ff9fbacf85 62 } else {
emilychen55 8:a6ff9fbacf85 63 report.data[4] = 0;
emilychen55 2:4b2e3ca87deb 64 }
pgao 6:adec77323ab2 65 if (accelX.read() >= CENTER_RIGHT) {
emilychen55 8:a6ff9fbacf85 66 report.data[5] = 0x4F;
emilychen55 8:a6ff9fbacf85 67 } else {
emilychen55 8:a6ff9fbacf85 68 report.data[5] = 0;
emilychen55 2:4b2e3ca87deb 69 }
emilychen55 8:a6ff9fbacf85 70 if (accelZ.read() <= CENTER_UP) {
emilychen55 8:a6ff9fbacf85 71 report.data[6] = 0x52;
emilychen55 8:a6ff9fbacf85 72 } else if (!breakz) {
emilychen55 8:a6ff9fbacf85 73 report.data[6] = 0x1D; //z
emilychen55 8:a6ff9fbacf85 74 } else {
emilychen55 8:a6ff9fbacf85 75 report.data[6] = 0;
emilychen55 8:a6ff9fbacf85 76 }
emilychen55 8:a6ff9fbacf85 77 if (accelZ.read() >= CENTER_DOWN) {
emilychen55 8:a6ff9fbacf85 78 report.data[7] = 0x51;
emilychen55 8:a6ff9fbacf85 79 } else {
emilychen55 8:a6ff9fbacf85 80 report.data[7] = 0;
emilychen55 8:a6ff9fbacf85 81 }
emilychen55 8:a6ff9fbacf85 82 controller.send(&report);
emilychen55 2:4b2e3ca87deb 83 }
emilychen55 8:a6ff9fbacf85 84
emilychen55 8:a6ff9fbacf85 85 //Mode 2: Buttons
emilychen55 8:a6ff9fbacf85 86 if (!flag) {
emilychen55 8:a6ff9fbacf85 87 if (!accelerate) {
emilychen55 8:a6ff9fbacf85 88 report.data[3] = 0x1B; //x
emilychen55 8:a6ff9fbacf85 89 } else {
emilychen55 8:a6ff9fbacf85 90 report.data[3] = 0;
emilychen55 8:a6ff9fbacf85 91 }
emilychen55 8:a6ff9fbacf85 92 if (!powerUp) {
emilychen55 8:a6ff9fbacf85 93 report.data[8] = 0x16; //s
emilychen55 8:a6ff9fbacf85 94 } else {
emilychen55 8:a6ff9fbacf85 95 report.data[8] = 0;
emilychen55 8:a6ff9fbacf85 96 }
emilychen55 8:a6ff9fbacf85 97 if (!up) {
emilychen55 8:a6ff9fbacf85 98 report.data[4] = 0x52;
emilychen55 8:a6ff9fbacf85 99 } else if (!breakz) {
emilychen55 8:a6ff9fbacf85 100 report.data[4] = 0x1D; //z
emilychen55 8:a6ff9fbacf85 101 } else {
emilychen55 8:a6ff9fbacf85 102 report.data[4] = 0;
emilychen55 8:a6ff9fbacf85 103 }
emilychen55 8:a6ff9fbacf85 104 if (!down) {
emilychen55 8:a6ff9fbacf85 105 report.data[5] = 0x51;
emilychen55 8:a6ff9fbacf85 106 } else {
emilychen55 8:a6ff9fbacf85 107 report.data[5] = 0;
emilychen55 8:a6ff9fbacf85 108 }
emilychen55 8:a6ff9fbacf85 109 if (!left) {
emilychen55 8:a6ff9fbacf85 110 report.data[6] = 0x50;
emilychen55 8:a6ff9fbacf85 111 } else {
emilychen55 8:a6ff9fbacf85 112 report.data[6] = 0;
emilychen55 8:a6ff9fbacf85 113 }
emilychen55 8:a6ff9fbacf85 114 if (!right) {
emilychen55 8:a6ff9fbacf85 115 report.data[7] = 0x4F;
emilychen55 8:a6ff9fbacf85 116 } else {
emilychen55 8:a6ff9fbacf85 117 report.data[7] = 0;
emilychen55 8:a6ff9fbacf85 118 }
emilychen55 8:a6ff9fbacf85 119 controller.send(&report);
emilychen55 1:85b6b5d1528e 120 }
pgao 0:84cb4cb4f860 121 }
emilychen55 1:85b6b5d1528e 122 }