keyboard

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "USBKeyboard.h"
00003  
00004 //LED1: NUM_LOCK, LED2: CAPS_LOCK, LED3: SCROLL_LOCK
00005 BusOut leds(LED1, LED2, LED3);            //keyboard on/off set
00006 DigitalIn start(PTC1);               // Configure SW2 pin as input function is start
00007 DigitalIn stop(PTB17);               // Configure SW3 pin as input function is stop
00008 USBKeyboard keyboard;
00009 Serial pc(USBTX, USBRX);
00010 
00011 #define amount 5                     
00012 #define delay_MAX_bit 3
00013 
00014 void user_button_send(char [],int []);        //send keyboard key
00015 void user_button_input(char [],int []);       //client software inupt keyboard key and delay
00016 
00017 long int timer = 0;
00018 
00019 int main() {
00020     char user_button_key[amount];                  //user input key
00021     int user_button_delay[amount];                //user input key delay time 
00022 
00023     while (!keyboard.configured()) {    // wait until keyboard is configured
00024     }
00025     
00026     while (1) {
00027         leds = keyboard.lockStatus();         //open keyboard 
00028         
00029         if(pc.readable()){
00030             user_button_input(user_button_key,user_button_delay);
00031         }
00032           
00033         if(start.read() == 0){                    //wait SW2 button press   
00034             while(stop.read() != 0){                    //wait SW3 button press
00035                 user_button_send(user_button_key,user_button_delay);
00036                 timer++;
00037                 for(double i=0;i<1;i=i+0.1){                    //count 1 second time and wait SW3 button press
00038                     if(stop.read() == 0) break;
00039                     wait(0.1);
00040                 }
00041             }
00042             timer = 0;
00043         }   
00044     }
00045 }
00046 
00047 void user_button_send(char key[],int delay[]){
00048     for(int i=0;i<amount;i++){
00049         if((timer%delay[i]) == 0)
00050             keyboard._putc(key[i]);   
00051     }
00052 }
00053 
00054 void user_button_input(char input_key[],int input_delay[]){
00055     char client_input_char[delay_MAX_bit];
00056     int client_input_char_count = 0 , bit_count = 1 , bit_set;
00057     
00058     for(int i =0;i<delay_MAX_bit;i++){
00059         if(i > 0)
00060             bit_count = bit_count * 10;
00061     }
00062     
00063     bit_set = bit_count;
00064     
00065     for(int i=0;i<amount;i++){                           //receive key value
00066             input_key[i] = pc.getc();
00067             if(input_key[i] == '0')
00068                 input_key[i] = NULL;
00069     }
00070     
00071     for(int i=0;i<amount;i++){                           //receive delay value
00072         for(int j=0;j<delay_MAX_bit;j++){
00073             client_input_char[j] = pc.getc();
00074             client_input_char_count = client_input_char_count + (client_input_char[j] - '0') * bit_count;
00075             bit_count = bit_count / 10;
00076         }
00077         
00078         input_delay[i] = client_input_char_count;
00079         client_input_char_count = 0;
00080         bit_count = bit_set;
00081     }
00082 }