keyboard

Dependencies:   mbed

main.cpp

Committer:
bruce_0205
Date:
2017-12-15
Revision:
0:6e42a1ce0351

File content as of revision 0:6e42a1ce0351:

#include "mbed.h"
#include "USBKeyboard.h"
 
//LED1: NUM_LOCK, LED2: CAPS_LOCK, LED3: SCROLL_LOCK
BusOut leds(LED1, LED2, LED3);            //keyboard on/off set
DigitalIn start(PTC1);               // Configure SW2 pin as input function is start
DigitalIn stop(PTB17);               // Configure SW3 pin as input function is stop
USBKeyboard keyboard;
Serial pc(USBTX, USBRX);

#define amount 5                     
#define delay_MAX_bit 3

void user_button_send(char [],int []);        //send keyboard key
void user_button_input(char [],int []);       //client software inupt keyboard key and delay

long int timer = 0;

int main() {
    char user_button_key[amount];                  //user input key
    int user_button_delay[amount];                //user input key delay time 

    while (!keyboard.configured()) {    // wait until keyboard is configured
    }
    
    while (1) {
        leds = keyboard.lockStatus();         //open keyboard 
        
        if(pc.readable()){
            user_button_input(user_button_key,user_button_delay);
        }
          
        if(start.read() == 0){                    //wait SW2 button press   
            while(stop.read() != 0){                    //wait SW3 button press
                user_button_send(user_button_key,user_button_delay);
                timer++;
                for(double i=0;i<1;i=i+0.1){                    //count 1 second time and wait SW3 button press
                    if(stop.read() == 0) break;
                    wait(0.1);
                }
            }
            timer = 0;
        }   
    }
}

void user_button_send(char key[],int delay[]){
    for(int i=0;i<amount;i++){
        if((timer%delay[i]) == 0)
            keyboard._putc(key[i]);   
    }
}

void user_button_input(char input_key[],int input_delay[]){
    char client_input_char[delay_MAX_bit];
    int client_input_char_count = 0 , bit_count = 1 , bit_set;
    
    for(int i =0;i<delay_MAX_bit;i++){
        if(i > 0)
            bit_count = bit_count * 10;
    }
    
    bit_set = bit_count;
    
    for(int i=0;i<amount;i++){                           //receive key value
            input_key[i] = pc.getc();
            if(input_key[i] == '0')
                input_key[i] = NULL;
    }
    
    for(int i=0;i<amount;i++){                           //receive delay value
        for(int j=0;j<delay_MAX_bit;j++){
            client_input_char[j] = pc.getc();
            client_input_char_count = client_input_char_count + (client_input_char[j] - '0') * bit_count;
            bit_count = bit_count / 10;
        }
        
        input_delay[i] = client_input_char_count;
        client_input_char_count = 0;
        bit_count = bit_set;
    }
}