code for the left controller (Niimote)

Dependencies:   PinDetect_hw3 USBDevice mbed

Committer:
franklu
Date:
Mon Sep 21 15:19:03 2015 +0000
Revision:
1:8252a6052ebe
Parent:
0:120c46c83388
added main code for the right controller

Who changed what in which revision?

UserRevisionLine numberNew contents of line
franklu 0:120c46c83388 1 #include "mbed.h"
franklu 0:120c46c83388 2 #include "USBKeyboard.h"
franklu 0:120c46c83388 3 #include "PinDetect.h"
franklu 0:120c46c83388 4
franklu 0:120c46c83388 5 AnalogIn potx(A0);
franklu 0:120c46c83388 6 AnalogIn poty(A1);
franklu 0:120c46c83388 7 USBKeyboard keyboard;
franklu 0:120c46c83388 8 PinDetect button(D3);
franklu 0:120c46c83388 9 PinDetect button1(D4);
franklu 0:120c46c83388 10 PinDetect button2(D5);
franklu 0:120c46c83388 11 int state[3];
franklu 0:120c46c83388 12
franklu 0:120c46c83388 13 void button_pressed() {
franklu 0:120c46c83388 14 state[0] = 1;
franklu 0:120c46c83388 15 }
franklu 0:120c46c83388 16
franklu 0:120c46c83388 17 void button_released() {
franklu 0:120c46c83388 18 state[0] = 0;
franklu 0:120c46c83388 19 }
franklu 0:120c46c83388 20
franklu 0:120c46c83388 21 void button1_pressed() {
franklu 0:120c46c83388 22 state[1] = 1;
franklu 0:120c46c83388 23 }
franklu 0:120c46c83388 24
franklu 0:120c46c83388 25 void button1_released() {
franklu 0:120c46c83388 26 state[1] = 0;
franklu 0:120c46c83388 27 }
franklu 0:120c46c83388 28
franklu 0:120c46c83388 29 void button2_pressed() {
franklu 0:120c46c83388 30 state[2] = 1;
franklu 0:120c46c83388 31 }
franklu 0:120c46c83388 32
franklu 0:120c46c83388 33 void button2_released() {
franklu 0:120c46c83388 34 state[2] = 0;
franklu 0:120c46c83388 35 }
franklu 0:120c46c83388 36
franklu 0:120c46c83388 37 int main() {
franklu 0:120c46c83388 38 button.attach_asserted(&button_pressed);
franklu 0:120c46c83388 39 button.attach_deasserted(&button_released);
franklu 0:120c46c83388 40 button.setAssertValue(0);
franklu 0:120c46c83388 41 button.setSampleFrequency();
franklu 0:120c46c83388 42
franklu 0:120c46c83388 43 button1.attach_asserted(&button1_pressed);
franklu 0:120c46c83388 44 button1.attach_deasserted(&button1_released);
franklu 0:120c46c83388 45 button1.setAssertValue(0);
franklu 0:120c46c83388 46 button1.setSampleFrequency();
franklu 0:120c46c83388 47
franklu 0:120c46c83388 48 button2.attach_asserted(&button2_pressed);
franklu 0:120c46c83388 49 button2.attach_deasserted(&button2_released);
franklu 0:120c46c83388 50 button2.setAssertValue(0);
franklu 0:120c46c83388 51 button2.setSampleFrequency();
franklu 0:120c46c83388 52
franklu 0:120c46c83388 53 while(1) {
franklu 0:120c46c83388 54 if (state[0] == 1) {
franklu 0:120c46c83388 55 keyboard.keyCode('e');
franklu 0:120c46c83388 56 }
franklu 0:120c46c83388 57 if (state[1] == 1) {
franklu 0:120c46c83388 58 keyboard.keyCode('m');
franklu 0:120c46c83388 59 }
franklu 0:120c46c83388 60 if (state[2] == 1) {
franklu 0:120c46c83388 61 keyboard.keyCode('n');
franklu 0:120c46c83388 62 }
franklu 0:120c46c83388 63 int potx_read = (int)(potx.read() * 100.0f);
franklu 0:120c46c83388 64 int poty_read = (int)(poty.read() * 100.0f);
franklu 0:120c46c83388 65 if (potx_read > 70) {
franklu 0:120c46c83388 66 keyboard.keyCode('s');
franklu 0:120c46c83388 67 }else if (potx_read < 30) {
franklu 0:120c46c83388 68 keyboard.keyCode('w');
franklu 0:120c46c83388 69 } else if (poty_read > 80) {
franklu 0:120c46c83388 70 keyboard.keyCode('d');
franklu 0:120c46c83388 71 } else if (poty_read < 20) {
franklu 0:120c46c83388 72 keyboard.keyCode('a');
franklu 0:120c46c83388 73 }
franklu 0:120c46c83388 74 wait_ms(1);
franklu 0:120c46c83388 75 }
franklu 0:120c46c83388 76 }