Testing USBKeyboard

Dependencies:   DebounceIn PinDetect_KL25Z USBDevice mbed

Fork of idd_fall15_hw2_chiller_morse by IDD Fall 2015

Committer:
adithya_murali
Date:
Sun Sep 20 05:35:34 2015 +0000
Revision:
1:11e89d389dd4
Parent:
0:4b4ae443bf49
Testing USBKeyboard with basic Pindetect buttons

Who changed what in which revision?

UserRevisionLine numberNew contents of line
chiller 0:4b4ae443bf49 1 #include "mbed.h"
chiller 0:4b4ae443bf49 2 #include "USBKeyboard.h"
chiller 0:4b4ae443bf49 3 #include "PinDetect.h"
chiller 0:4b4ae443bf49 4
adithya_murali 1:11e89d389dd4 5 USBKeyboard keyboard;
chiller 0:4b4ae443bf49 6
adithya_murali 1:11e89d389dd4 7 PinDetect button1(D4); // Left
adithya_murali 1:11e89d389dd4 8 PinDetect button2(D5); // Right
adithya_murali 1:11e89d389dd4 9 PinDetect button3(D6); // Space
adithya_murali 1:11e89d389dd4 10 PinDetect button4(D7); // Shift
adithya_murali 1:11e89d389dd4 11
adithya_murali 1:11e89d389dd4 12 DigitalOut myled(LED1);
adithya_murali 1:11e89d389dd4 13
adithya_murali 1:11e89d389dd4 14 int state[4];
chiller 0:4b4ae443bf49 15
adithya_murali 1:11e89d389dd4 16 void button1_pressed() {
adithya_murali 1:11e89d389dd4 17 state[0] = 1;
adithya_murali 1:11e89d389dd4 18 }
adithya_murali 1:11e89d389dd4 19 void button2_pressed() {
adithya_murali 1:11e89d389dd4 20 state[1] = 1;
adithya_murali 1:11e89d389dd4 21 }
adithya_murali 1:11e89d389dd4 22 void button3_pressed() {
adithya_murali 1:11e89d389dd4 23 state[2] = 1;
adithya_murali 1:11e89d389dd4 24 }
adithya_murali 1:11e89d389dd4 25 void button4_pressed() {
adithya_murali 1:11e89d389dd4 26 state[3] = 1;
adithya_murali 1:11e89d389dd4 27 }
chiller 0:4b4ae443bf49 28
adithya_murali 1:11e89d389dd4 29 void button1_released() {
adithya_murali 1:11e89d389dd4 30 state[0] = 0;
adithya_murali 1:11e89d389dd4 31 }
adithya_murali 1:11e89d389dd4 32 void button2_released() {
adithya_murali 1:11e89d389dd4 33 state[1] = 0;
adithya_murali 1:11e89d389dd4 34 }
adithya_murali 1:11e89d389dd4 35 void button3_released() {
adithya_murali 1:11e89d389dd4 36 state[2] = 0;
adithya_murali 1:11e89d389dd4 37 }
adithya_murali 1:11e89d389dd4 38 void button4_released() {
adithya_murali 1:11e89d389dd4 39 state[3] = 0;
adithya_murali 1:11e89d389dd4 40 }
chiller 0:4b4ae443bf49 41
adithya_murali 1:11e89d389dd4 42 void exec_keycode(int state[], USBKeyboard keyboard) {
adithya_murali 1:11e89d389dd4 43 if (state[0] == 1) {
adithya_murali 1:11e89d389dd4 44 keyboard.keyCode('a');
adithya_murali 1:11e89d389dd4 45 }
adithya_murali 1:11e89d389dd4 46 if (state[1] == 1) {
adithya_murali 1:11e89d389dd4 47 keyboard.keyCode('d');
adithya_murali 1:11e89d389dd4 48 }
adithya_murali 1:11e89d389dd4 49 if (state[2] == 1) {
adithya_murali 1:11e89d389dd4 50 keyboard.keyCode(32);
adithya_murali 1:11e89d389dd4 51 }
adithya_murali 1:11e89d389dd4 52 if (state[3] == 1) {
adithya_murali 1:11e89d389dd4 53 keyboard.keyCode(16);
adithya_murali 1:11e89d389dd4 54 }
adithya_murali 1:11e89d389dd4 55 }
chiller 0:4b4ae443bf49 56
chiller 0:4b4ae443bf49 57 int main() {
adithya_murali 1:11e89d389dd4 58
adithya_murali 1:11e89d389dd4 59 /*Add callback functions for events when buttons are pressed. */
adithya_murali 1:11e89d389dd4 60 button1.attach_asserted(&button1_pressed);
adithya_murali 1:11e89d389dd4 61 button2.attach_asserted(&button2_pressed);
adithya_murali 1:11e89d389dd4 62 button3.attach_asserted(&button3_pressed);
adithya_murali 1:11e89d389dd4 63 button4.attach_asserted(&button4_pressed);
adithya_murali 1:11e89d389dd4 64
adithya_murali 1:11e89d389dd4 65 /*Add callback functions for events when buttons are released. */
adithya_murali 1:11e89d389dd4 66 button1.attach_deasserted(&button1_released);
adithya_murali 1:11e89d389dd4 67 button2.attach_deasserted(&button2_released);
adithya_murali 1:11e89d389dd4 68 button3.attach_deasserted(&button3_released);
adithya_murali 1:11e89d389dd4 69 button4.attach_deasserted(&button4_released);
adithya_murali 1:11e89d389dd4 70
adithya_murali 1:11e89d389dd4 71 button1.setAssertValue(0);
adithya_murali 1:11e89d389dd4 72 button2.setAssertValue(0);
adithya_murali 1:11e89d389dd4 73 button3.setAssertValue(0);
adithya_murali 1:11e89d389dd4 74 button4.setAssertValue(0);
chiller 0:4b4ae443bf49 75
adithya_murali 1:11e89d389dd4 76 button1.setSampleFrequency();
adithya_murali 1:11e89d389dd4 77 button2.setSampleFrequency();
adithya_murali 1:11e89d389dd4 78 button3.setSampleFrequency();
adithya_murali 1:11e89d389dd4 79 button4.setSampleFrequency();
chiller 0:4b4ae443bf49 80
adithya_murali 1:11e89d389dd4 81 for (;;) {
adithya_murali 1:11e89d389dd4 82 if (state[0] == 1) {
adithya_murali 1:11e89d389dd4 83 keyboard.keyCode('a');
chiller 0:4b4ae443bf49 84 }
adithya_murali 1:11e89d389dd4 85 if (state[1] == 1) {
adithya_murali 1:11e89d389dd4 86 keyboard.keyCode('d');
chiller 0:4b4ae443bf49 87 }
adithya_murali 1:11e89d389dd4 88 if (state[2] == 1) {
adithya_murali 1:11e89d389dd4 89 keyboard.keyCode(32);
chiller 0:4b4ae443bf49 90 }
adithya_murali 1:11e89d389dd4 91 if (state[3] == 1) {
adithya_murali 1:11e89d389dd4 92 keyboard.keyCode(16);
chiller 0:4b4ae443bf49 93 }
adithya_murali 1:11e89d389dd4 94 wait(0.1);
chiller 0:4b4ae443bf49 95 }
adithya_murali 1:11e89d389dd4 96 }