part3 update

Dependencies:   mbed

main.cpp

Committer:
yifeng021
Date:
2015-02-17
Revision:
2:75fb721a5b86
Parent:
1:1260dba917b0

File content as of revision 2:75fb721a5b86:

// This version does not work.
// The issue is: if I only scan first row, it works. However, when I essentially copy
// the first case to the rest of the cases(or rows), program does not recognize any
// input anymore.

#include "mbed.h"
#include "string.h"
// speaker sound effect demo using PWM hardware output
PwmOut keytone_row(p21);//pwm output for key tone table row freq
PwmOut keytone_col(p22);//pwm output for key tone table column freq
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
Ticker t;

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

// 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();

void scan_row();
void print_key();
void key_release();

int main()
{
    //usb.printf("hello world!");
    float dur_row = 4000.0; //time to scan each row in us
    global_count = 0;
    global_flag_row = 0;
    global_key_index = 16;
    
    row8 = 1;
    row1 = 1;
    row2 = 1;
    row4 = 1;
    
    t.attach_us(&scan_row, dur_row);
    
    while (1) {
        usb.printf("%d",global_key_index);
        //print_key();
        //wait(0.2);
    }
}

// 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;}
void key_2(){global_key_index = 1;}
void key_3(){global_key_index = 2;}
void key_A(){global_key_index = 3;}
void key_4(){global_key_index = 4;}
void key_5(){global_key_index = 5;}
void key_6(){global_key_index = 6;}
void key_B(){global_key_index = 7;}
void key_7(){global_key_index = 8;}
void key_8(){global_key_index = 9;}
void key_9(){global_key_index = 10;}
void key_C(){global_key_index = 11;}
void key_str(){global_key_index = 12;}
void key_0(){global_key_index = 13;}
void key_pd(){global_key_index = 14;}
void key_D(){global_key_index = 15;}

void scan_row()
{
    global_flag_row = global_count % 4;
    switch(global_flag_row) {
        case 0:
            //printf("case0");
            row8 = 0;
            row1 = 1;
            row2 = 1;
            row4 = 1;
            col3.rise(&key_1);
            col5.rise(&key_2);
            col6.rise(&key_3);
            col7.rise(&key_A);
            col3.fall(&key_release);
            col5.fall(&key_release);
            col6.fall(&key_release);
            col7.fall(&key_release);
            break;
        
        case 1:
            //printf("case1");
            row8 = 1;
            row1 = 0;
            row2 = 1;
            row4 = 1;
            /*
            col3.rise(&key_4);
            col5.rise(&key_5);
            col6.rise(&key_6);
            col7.rise(&key_B);
            col3.fall(&key_release);
            col5.fall(&key_release);
            col6.fall(&key_release);
            col7.fall(&key_release);
            */
            break;
        case 2:
            //printf("case2");
            row8 = 1;
            row1 = 1;
            row2 = 0;
            row4 = 1;
            /*
            col3.rise(&key_7);
            col5.rise(&key_8);
            col6.rise(&key_9);
            col7.rise(&key_C);
            col3.fall(&key_release);
            col5.fall(&key_release);
            col6.fall(&key_release);
            col7.fall(&key_release);
            */
            break;
        case 3:
            //printf("case3");
            row8 = 1;
            row1 = 1;
            row2 = 1;
            row4 = 0;
            /*
            col3.rise(&key_str);
            col5.rise(&key_0);
            col6.rise(&key_pd);
            col7.rise(&key_D);
            col3.fall(&key_release);
            col5.fall(&key_release);
            col6.fall(&key_release);
            col7.fall(&key_release);
            */
            break;
            
    }
    global_count += 1;
}

void print_key()
{
    switch(global_key_index) {
        case 0:
            usb.printf("1");
            break;
        case 1:
            usb.printf("2");
            break;
        case 2:
            usb.printf("3");
            break;
        case 3:
            usb.printf("A");
            break;
        case 4:
            usb.printf("4");
            break;
        case 5:
            usb.printf("5");
            break;
        case 6:
            usb.printf("6");
            break;
        case 7:
            usb.printf("B");
            break;
        case 8:
            usb.printf("7");
            break;
        case 9:
            usb.printf("8");
            break;
        case 10:
            usb.printf("9");
            break;
        case 11:
            usb.printf("C");
            break;
        case 12:
            usb.printf("*");
            break;
        case 13:
            usb.printf("0");
            break;
        case 14:
            usb.printf("#");
            break;
        case 15:
            usb.printf("D");
            break;
        case 16:
            //usb.printf("default!");
            break;
    }
}

void key_release()
{
    global_key_index = 16;
}