part3 update

Dependencies:   mbed

main.cpp

Committer:
yifeng021
Date:
2015-02-13
Revision:
1:1260dba917b0
Parent:
0:7c6b15d40e00
Child:
2:75fb721a5b86

File content as of revision 1:1260dba917b0:

#include "mbed.h"
#include "string.h"
// speaker sound effect demo using PWM hardware output
PwmOut speaker(p21);//pwm output
Serial usb(USBTX, USBRX);//serial comm
DigitalOut row8(p8);//pin8 on keypad board->row1(top)
DigitalOut row1(p5);//pin1 ...->row2
DigitalOut row2(p6);//pin2->row3
DigitalOut row4(p7);//pin4->row4
InterruptIn col7(p27);//pin7->column 1(right-most column)
InterruptIn col6(p28);//pin6->column 2
InterruptIn col5(p29);//pin5->column 3
InterruptIn col3(p30);//pin3->column 4

unsigned short global_timer;//global variable
unsigned short global_key_index;//global variable, value range:0 - 15, represents for 16 keys

extern "C" void TIMER0_IRQHandler (void);
void timer0_init(void);//initialize timer0
void play_tone(int freq, int dur);//takes in frequency & duration and plays the tone accodingly
bool check_buffer_format(char *buffer);//check input string format
// The following functions determine which key is pressed by changing global variable 'global_key_index'
void key_1();
void key_2();
void key_3();
void key_A();
void key_4();
void key_5();
void key_6();
void key_B();
void key_7();
void key_8();
void key_9();
void key_C();
void key_str();
void key_0();
void key_pd();
void key_D();

int main()
{
    timer0_init();
    global_timer = 0;
    row8 = 0;
    row1 = 0;
    row2 = 0;
    row4 = 0;
    
    while (1) {
        if (global_timer < 4) {
            //set row1 to high for 4ms
            row8 = 1;
            //scan 4 columns
            col3.fall(&key_1);
            col5.fall(&key_2);
            col6.fall(&key_3);
            col7.fall(&key_A);    
        }
        if (global_timer >= 4 && global_timer < 8){
            //set row2 to high for 4ms
            row8 = 0;
            row2 = 0;
            row4 = 0;
            row1 = 1;
            //scan 4 columns
            col3.fall(&key_4);
            col5.fall(&key_5);
            col6.fall(&key_6);
            col7.fall(&key_B); 
        }
        if (global_timer >= 8 && global_timer < 12) {
            //set row3 high for 4ms
            row8 = 0;
            row1 = 0;
            row4 = 0;
            row2 = 1;
            //scan 4 columns
            col3.fall(&key_7);
            col5.fall(&key_8);
            col6.fall(&key_9);
            col7.fall(&key_C); 
        }
        if (global_timer >= 12 && global_timer < 16) {
            //set row4 high for 4ms
            row8 = 0;
            row1 = 0;
            row2 = 0;
            row4 = 1;
            //scan 4 columns
            col3.fall(&key_str);
            col5.fall(&key_0);
            col6.fall(&key_pd);
            col7.fall(&key_D); 
        }
        if (global_timer >= 16) {
            row8 = 0;
            row1 = 0;
            row2 = 0;
            row4 = 0;
            global_timer = 0;
        }
    }
}

//source: https://developer.mbed.org/users/microguy/notebook/timer0-example-code/
extern "C" void TIMER0_IRQHandler (void)
{
if((LPC_TIM0->IR & 0x01) == 0x01)   // if MR0 interrupt, proceed
    {
    LPC_TIM0->IR |= 1 << 0;         // Clear MR0 interrupt flag
    global_timer++;                  //increment timer_count
    }
}

//source: https://developer.mbed.org/users/microguy/notebook/timer0-example-code/
// This function initializes timer0
void timer0_init(void)
{
    LPC_SC->PCONP |=1<1;            //timer0 power on
    LPC_TIM0->MR0 = 23980;          //1 msec(verified with oscilloscope)
    LPC_TIM0->MCR = 3;              //interrupt and reset control
                                    //3 = Interrupt & reset timer0 on match
                                    //1 = Interrupt only, no reset of timer0
    NVIC_EnableIRQ(TIMER0_IRQn);    //enable timer0 interrupt
    LPC_TIM0->TCR = 1;              //enable Timer0
    printf("Done timer_init\n\r");
}

// This function plays a tone for a certain duration
void play_tone(int freq, int dur)
{
    speaker.period(1.0/freq); // 500hz period
    speaker = 0.2; //50% duty cycle - max volume
    wait(dur/1000.0);
    speaker=0.0; // turn off audio
}

// This function checks the user input format of frequency & duration
bool check_buffer_format(char *buffer)
{
    bool flag = true;//true - valid input; false - invalid input
    int buffer_length = strlen(buffer);
    int i = 0;
    for (i=0; i<buffer_length; i++) {
        //check if buffer[0:2] & buffer[4:6] are #s
        if (i==0 || i==1 || i==2 || i==4 || i==5 || i==6){
            if (buffer[i] < '0' || buffer[i] > '9'){
                flag = false;
                }
            }
        //check if buffer[3] is a 'space'
        else if (i==3){
            if (buffer[i] != ' '){
            flag = false;
            }
        }
    }
    return flag;
}

// The following functions determine which key is pressed by changing global variable 'global_key_index'
//indice:
//key-[index]
//1-[0],2-[1],3-[2],A-[3],4-[4],5-[5],6-[6],B-[7],7-[8],8-[9],9-[10],C-[11],*-[12],0-[13],#-[14],D-[15] 
void key_1(){
    global_key_index = 0;
    printf("1 is pressed!\n");
}
void key_2(){
    global_key_index = 1;
    printf("2 is pressed!\n");
}
void key_3(){
    global_key_index = 2;
    printf("3 is pressed!\n");
}
void key_A(){
    global_key_index = 3;
    printf("A is pressed!\n");
}
void key_4(){
    global_key_index = 4;
    printf("4 is pressed!\n");
}
void key_5(){
    global_key_index = 5;
    printf("5 is pressed!\n");
}
void key_6(){
    global_key_index = 6;
    printf("6 is pressed!\n");
}
void key_B(){
    global_key_index = 7;
    printf("B is pressed!\n");
}
void key_7(){
    global_key_index = 8;
    printf("7 is pressed!\n");
}
void key_8(){
    global_key_index = 9;
    printf("8 is pressed!\n");
}
void key_9(){
    global_key_index = 10;
    printf("9 is pressed!\n");
}
void key_C(){
    global_key_index = 11;
    printf("C is pressed!\n");
}
void key_str(){
    global_key_index = 12;
    printf("* is pressed!\n");
}
void key_0(){
    global_key_index = 13;
    printf("0 is pressed!\n");
}
void key_pd(){
    global_key_index = 14;
    printf("# is pressed!\n");
}
void key_D(){
    global_key_index = 15;
    printf("D is pressed!\n");
}