Type the only important words you will ever need with this simple keyboard. Input key presses by touching the capacitive slider and watch as the words show up on your screen. Requires the FRDM to be connected to a computer using the USB port.

Dependencies:   USBDevice mbed tsi_sensor

Committer:
oehlberg
Date:
Fri Aug 22 17:09:48 2014 +0000
Revision:
1:d8b92f4c7e03
Parent:
0:fb74ddc7b758
Comments and cleaning up the code

Who changed what in which revision?

UserRevisionLine numberNew contents of line
oehlberg 0:fb74ddc7b758 1 /* Created by Mark Oehlberg 8/21/14
oehlberg 0:fb74ddc7b758 2
oehlberg 0:fb74ddc7b758 3 Automatic keyboard app
oehlberg 0:fb74ddc7b758 4
oehlberg 0:fb74ddc7b758 5 You are viewing the code for the future of keyboards. You might as well
oehlberg 1:d8b92f4c7e03 6 smash your current keyboard, because once you use your KL25Z as a keyboard
oehlberg 1:d8b92f4c7e03 7 you will never look back.
oehlberg 1:d8b92f4c7e03 8
oehlberg 1:d8b92f4c7e03 9 The Keyboard uses the touch slider and splits it in to two buttons, a left and a right button.
oehlberg 1:d8b92f4c7e03 10 The left Button types Pirates! and the right button types ROBOTS!
oehlberg 1:d8b92f4c7e03 11
oehlberg 1:d8b92f4c7e03 12 The two USB ports on the board are labeled SDA and USB. Use the port called USB to type.
oehlberg 1:d8b92f4c7e03 13 The LED will light on when pressed as well.
oehlberg 0:fb74ddc7b758 14
oehlberg 0:fb74ddc7b758 15 */
oehlberg 0:fb74ddc7b758 16
oehlberg 0:fb74ddc7b758 17 #include "mbed.h"
oehlberg 0:fb74ddc7b758 18 #include "tsi_sensor.h"
oehlberg 0:fb74ddc7b758 19 #include "USBKeyboard.h"
oehlberg 0:fb74ddc7b758 20
oehlberg 1:d8b92f4c7e03 21 /* A couple of boards have different pins connected */
oehlberg 0:fb74ddc7b758 22 #if defined (TARGET_KL25Z) || defined (TARGET_KL46Z)
oehlberg 0:fb74ddc7b758 23 #define ELEC0 9
oehlberg 0:fb74ddc7b758 24 #define ELEC1 10
oehlberg 0:fb74ddc7b758 25 #elif defined (TARGET_KL05Z)
oehlberg 0:fb74ddc7b758 26 #define ELEC0 9
oehlberg 0:fb74ddc7b758 27 #define ELEC1 8
oehlberg 0:fb74ddc7b758 28 #else
oehlberg 0:fb74ddc7b758 29 #error TARGET NOT DEFINED
oehlberg 0:fb74ddc7b758 30 #endif
oehlberg 0:fb74ddc7b758 31
oehlberg 1:d8b92f4c7e03 32 Serial pc(USBTX, USBRX);//begins a serial console named pc
oehlberg 0:fb74ddc7b758 33 USBKeyboard keyboard;
oehlberg 0:fb74ddc7b758 34
oehlberg 0:fb74ddc7b758 35
oehlberg 0:fb74ddc7b758 36 int main(void) {
oehlberg 1:d8b92f4c7e03 37 bool justpressed = false;//variable used to
oehlberg 1:d8b92f4c7e03 38 float lastpressed = 0.0;
oehlberg 0:fb74ddc7b758 39 pc.baud(115200);//I apparently have to do this in the main loop
oehlberg 1:d8b92f4c7e03 40 DigitalOut led(LED2);//also called LED_GREEN
oehlberg 0:fb74ddc7b758 41 TSIAnalogSlider tsi(ELEC0, ELEC1, 40);
oehlberg 0:fb74ddc7b758 42 float i=0.0;
oehlberg 0:fb74ddc7b758 43 while (true) {
oehlberg 1:d8b92f4c7e03 44 i=1.0 - tsi.readPercentage();//i=1.0000 when not pressed and will be between 0 and 1 when touched
oehlberg 1:d8b92f4c7e03 45 led = (i == 1.0) ? 1 : 0;//turn the light on if the slider is pressed. Outputting Zero on this pin connects the cathode to ground. The annode is connected to 3.3V
oehlberg 0:fb74ddc7b758 46 if (justpressed && (abs(lastpressed-i)>0.1)){
oehlberg 0:fb74ddc7b758 47 justpressed=false;
oehlberg 0:fb74ddc7b758 48 }
oehlberg 0:fb74ddc7b758 49 if ( 0.5 < i && i < 1.0 && not justpressed ){
oehlberg 0:fb74ddc7b758 50 keyboard.printf("Pirates!\r\n");
oehlberg 0:fb74ddc7b758 51 lastpressed = i;
oehlberg 0:fb74ddc7b758 52 justpressed = true;
oehlberg 0:fb74ddc7b758 53 pc.printf(" Pirates - Trigger number=%f\n\r",i);
oehlberg 0:fb74ddc7b758 54 }else if (0.0 < i && i <= 0.5 && not justpressed ){
oehlberg 0:fb74ddc7b758 55 keyboard.printf("ROBOTS!\r\n");
oehlberg 0:fb74ddc7b758 56 lastpressed = i;
oehlberg 0:fb74ddc7b758 57 justpressed = true;
oehlberg 0:fb74ddc7b758 58 pc.printf(" ROBOTS!- Trigger number=%f\n\r",i);
oehlberg 0:fb74ddc7b758 59 }
oehlberg 0:fb74ddc7b758 60 //pc.printf("Reading:%f\tlastpressed:%f Justpressed:%d\n\r", i,lastpressed, justpressed);//debug code
oehlberg 0:fb74ddc7b758 61 }
oehlberg 0:fb74ddc7b758 62 }