part3 update

Dependencies:   mbed

main.cpp

Committer:
yifeng021
Date:
2015-02-12
Revision:
0:7c6b15d40e00
Child:
1:1260dba917b0

File content as of revision 0:7c6b15d40e00:

#include "mbed.h"
#include "string.h"
// speaker sound effect demo using PWM hardware output
PwmOut speaker(p21);
Serial usb(USBTX, USBRX);
DigitalOut row8(p8);
DigitalOut row1(p5);
DigitalOut row2(p6);
DigitalOut row4(p7);
InterruptIn col7(p27);
InterruptIn col6(p28);
InterruptIn col5(p29);
InterruptIn col3(p30);

unsigned short global_timer;//global variable
unsigned short global_key_index;

extern "C" void TIMER0_IRQHandler (void);
void timer0_init(void);
void play_tone(int freq, int dur);
bool check_buffer_format(char *buffer);
//void scan_all_rows();
void key_1();
void key_2();
void key_3();
void key_A();



int main()
{
    //printf("Gello world!\n");
    
    //en8.rise(&check_row8);
    //en1.rise(&check_row1);
    //en2.rise(&check_row2);
    //en4.rise(&check_row4);
    global_timer = 0;
    row8 = 0;
    row1 = 0;
    row2 = 0;
    row4 = 0;
    
    while (1) {
        if (global_timer < 4) {
            row8 = 1;
            col3.rise(&key_1);
            col5.rise(&key_2);
            col6.rise(&key_3);
            col7.rise(&key_A);
        }
        else if (global_timer < 8){
            row8 = 0;
            row1 = 1;
        }
        else if (global_timer < 12) {
            row1 = 0;
            row2 = 1;
        }
        else if (global_timer < 16) {
            row2 = 0;
            row4 = 1;
        }
        else {
            row4 = 0;
            global_timer = 0;
        }
    }
            

    
    /*
    while (1) {   
        speaker = 0.0;
        int freq = 0; 
        int dur = 0;
        int i;
        const int buffer_length = 7; //this is the actual buffer length, excluding the '\0' in the end
        bool flag = true;
        char buffer[128];
        usb.gets(buffer, buffer_length+1);
        //usb.puts(buffer);
        //check if input is valid
        if (check_buffer_format(buffer) == false){
            printf("Invalid input format, please re-enter:\n");
            continue;
            }
            
            
        
        //convert input string to two numbers, freq & dur
        for (i=0; i < buffer_length; i++) {
            //flag indicades whether the string represents frequency or duration
            if (buffer[i] == ' ') {
                flag = false;
                continue;
            }
            if (flag) {
                freq *= 10;
                freq += buffer[i] - '0';
            } else {
                dur *= 10;
                dur += buffer[i] - '0';
            }
        }
        //usb.printf("freq: %d; dur: %d\n", freq, dur);
        play_tone(freq, dur);
    }
    */
}

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
    }
}

void timer0_init(void)
{
    LPC_SC->PCONP |=1<1;            //timer0 power on
    LPC_TIM0->MR0 = 23980;          //1 msec(needs to be verified)
    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");
}

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
}

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;
}

/*
void scan_all_rows()
{
    timer0_init();
    global_timer = 0;
    while(1)
    {
        en8.fall(&check_row8);
        if (global_timer > 4) {
            en1.fall(&check_row1);
            //scan next row
            if (global_timer > 8) {
                en2.fall(&check_row2);
                //scan next row
                if (global_timer > 12) {
                    en4.fall(&check_row4);
                    if (global_timer > 16) {
                        break;
                    }
                }
            }
        }
    }
    usb.printf("row %d is pressed!\n",global_row_index);
    global_timer = 0;
}
*/
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");
}