cancel
Showing results for 
Search instead for 
Did you mean: 

PSoC™ 5, 3 & 1

Yash_r
New Contributor II

Hello @MotooTanaka san,

Thanks a lot for your help on many previous issues of mine.

Based on the last project that you helped with me earlier, I wanted to know if it is possible to store the keypad input to a buffer and then print the buffer (instead of individual characters), if '#' is detected as input or at some interval over UART?

I did try some ways in the print_key_table() function,  but I am not able to store the data properly.

Hoping someone could provide some way for me to get started with the above method.

Regards,
Yash

0 Likes
1 Solution
MotooTanaka
Esteemed Contributor

Dear Yash-san,

The most difficult part of this discussion for me was to remember what I've done...

It would have been nice, if we could hear what you tried, 

but anyway here is my minimal sketch.

I added the third option to the top menu so what we don't have to destroy the previous project,

and in the 3rd option.

001-top-menu.JPG

The the program enters "string entering mode"

002-string_test.JPG

and each letter pushed will be echoed back to the serial terminal

003-entering-letters.JPG

and when '#' is pushed,  the buffered string will be written.

004-pound-entered.JPG

Or if you push 'D' 

005-again-entering-letters.JPG

The program returns to the top menu.

006-d-to-return.JPG

As usual, there are millions of way to implement something like this,

and I am positive that mine is not the most elegant way to do.

So please take my code as a "SAMPLE".

main.c

#include "project.h"
#include "stdio.h"

#define NUM_ROW 4
#define NUM_COL 4

volatile uint8_t key[NUM_ROW][NUM_COL] = { 0u } ;
uint8_t letter[NUM_ROW][NUM_COL] = {
    { '1', '2', '3', 'A' },
    { '4', '5', '6', 'B' },
    { '7', '8', '9', 'C' },
    { '*', '0', '#', 'D' }
} ;
    

volatile int row_no = 0 ;
volatile int col_no = 0 ;

void scan_key_matrix(void)
{
    switch(row_no) {
    case  0: ROW0_Write(1) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
    case  1: ROW0_Write(0) ; ROW1_Write(1) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
    case  2: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(1) ; ROW3_Write(0) ; break ;
    case  3: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(1) ; break ;
    default: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
    }

    switch(col_no) {
    case 0: key[row_no][col_no] = COL0_Read() ; break ;
    case 1: key[row_no][col_no] = COL1_Read() ; break ;
    case 2: key[row_no][col_no] = COL2_Read() ; break ;
    case 3: key[row_no][col_no] = COL3_Read() ; break ;
    }
    
    col_no++ ;
    if (col_no >= NUM_COL) {
        col_no = 0 ;
        row_no = (row_no + 1) % NUM_ROW ;
    } 
    ROW0_Write(0) ; 
    ROW1_Write(0) ; 
    ROW2_Write(0) ; 
    ROW3_Write(0) ; 
}

CY_ISR(my_tick_isr)
{
    scan_key_matrix() ;
}

#define STR_LEN 64
char str[STR_LEN+1] ;
void print(char *str)
{
    UART_PutString(str) ;
}

void printc(char c)
{
    UART_PutChar(c) ;
}

void cls(void)
{
    print("\033c") ; /* reset */
    CyDelay(20) ;
    print("\033[2J") ; /* clear screen */
    CyDelay(20) ;
}

void splash(void)
{
    cls() ;
    print("5LP 4x4 Matrix Key Test") ;
    snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;
    print(str) ;
}

void init_hardware(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    UART_Start() ;
    splash() ;
    CySysTickStart() ;
    CySysTickSetCallback(0, my_tick_isr) ;
}

uint16_t get_key_matrix(void)
{
    int col, row ;
    uint16_t value = 0 ;
    for (row = 0 ; row < NUM_ROW ; row++) {
        for (col = 0 ; col < NUM_COL ; col++) {
            value <<= 1 ;
            if (key[row][col]) {
                value |= 0x01 ;
            }
        }
    }   
    return( value ) ;
}



void print_key_table(uint16_t key_table)
{
    int row, col ;
    uint16_t mask = 0x8000 ; /* bin: 1000_0000_0000_0000 */
    snprintf(str, STR_LEN, "0x%04X: ", key_table) ;
    print(str) ;
    
    if (key_table & 0x8000) {
        print("1 is pushed ") ;
    }
    if (key_table & 0x4000) {
        print("2 is pushed ") ;
    }
    print("\n\r") ;
    
    for (row = 0 ; row < NUM_ROW ; row++) {
        for (col = 0 ; col < NUM_COL ; col++) {
            if (key_table & mask) {
                snprintf(str, STR_LEN, "%c ", letter[row][col]) ;
                print(str) ;
            } else {
                print("_ ") ;
            }
            mask >>= 1 ;
        }
        print("\n\r") ;
    }
}

#define BUTTON_1     0x8000
#define BUTTON_2     0x4000
#define BUTTON_3     0x2000
#define BUTTON_A     0x1000
#define BUTTON_4     0x0800
#define BUTTON_5     0x0400
#define BUTTON_6     0x0200
#define BUTTON_B     0x0100
#define BUTTON_7     0x0080
#define BUTTON_8     0x0040
#define BUTTON_9     0x0020
#define BUTTON_C     0x0010
#define BUTTON_STAR  0x0008
#define BUTTON_0     0x0004
#define BUTTON_POUND 0x0002
#define BUTTON_D     0x0001


#define MENU_TOP 0u
#define MENU_10  10
#define MENU_11  11
#define MENU_12  12
#define MENU_20  20
#define MENU_21  21
#define MENU_22  22
#define STR_TEST 99
int menu_mode = MENU_TOP ;

uint8_t key_table_to_letter(uint16_t key_table)
{
    uint8_t c = 0 ;
    
    switch(key_table) {
    case BUTTON_0:     c = '0' ; break ;
    case BUTTON_1:     c = '1' ; break ;
    case BUTTON_2:     c = '2' ; break ;
    case BUTTON_3:     c = '3' ; break ;
    case BUTTON_4:     c = '4' ; break ;
    case BUTTON_5:     c = '5' ; break ;
    case BUTTON_6:     c = '6' ; break ;
    case BUTTON_7:     c = '7' ; break ;
    case BUTTON_8:     c = '8' ; break ;
    case BUTTON_9:     c = '9' ; break ;
    case BUTTON_A:     c = 'A' ; break ;
    case BUTTON_B:     c = 'B' ; break ;
    case BUTTON_C:     c = 'C' ; break ;
    case BUTTON_D:     c = 'D' ; break ;
    case BUTTON_STAR:  c = '*' ; break ;
    case BUTTON_POUND: c = '#' ; break ;
    default: break ;
    }
    
    return( c ) ;
}

void greeting(void)
{
    cls() ;
    print("WELCOME TO THE") ; print("\n\r") ;
    print("EMP/COMP DBS")   ; print("\n\r") ;
}

void go_menu_top(void)
{
    cls() ;
    print("P1-Emp Details"); print("\n\r") ;
    print("P2-Comp Details"); print("\n\r") ;
    print("P3-String test") ; print("\n\r") ;
    menu_mode = MENU_TOP ;
}

void go_menu_10(void)
{
    cls() ;
    print("PRESS 1: NAME"); print("\n\r") ;
    print("PRESS 2: AGE"); print("\n\r") ;
    menu_mode = MENU_10 ;
}

void go_menu_11(void) 
{
    cls() ;
    print("Employee Name"); print("\n\r") ;
    print("Yash"); print("\n\r") ;
    menu_mode = MENU_11 ;   
}

void go_menu_12(void)
{
    cls() ;
    print("Employee Age"); print("\n\r") ;
    print("24"); print("\n\r") ;
    menu_mode = MENU_12 ;   
}

void go_menu_20(void)
{
    cls() ;
    print("Press1:Comp Name"); print("\n\r") ;
    print("Press2:Comp Addr"); print("\n\r") ;
    menu_mode = MENU_20 ;  
}

void go_menu_21(void)
{
    cls() ;
    print("Company Name:"); print("\n\r") ;
    print("ATPL"); print("\n\r") ;
    menu_mode = MENU_21 ;   
}

void go_menu_22(void)
{
    cls() ;
    print("Company Addr:"); print("\n\r") ;
    print("Hyd"); print("\n\r") ;
    menu_mode = MENU_22 ;   
}

void go_str_test(void)
{
    cls() ;
    print("String Entering Test: '#' to end line, 'D' to return to top menu") ; print("\n\r") ;
    menu_mode = STR_TEST ;
}

void do_menu_top(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_10() ; break ;
    case BUTTON_2: go_menu_20() ; break ;
    case BUTTON_3: go_str_test() ; break ;
    default:       go_menu_top() ; break ;
    }
}

void do_menu_10(uint16_t key_table)
{

    switch(key_table) {
    case BUTTON_1: go_menu_11() ; break ;
    case BUTTON_2: go_menu_12() ; break ;
    case BUTTON_D: go_menu_top() ; break ;
    default:       go_menu_10() ; break ;
    }
}

void do_menu_11(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_11() ; break ;
    case BUTTON_2: go_menu_11() ; break ;
    case BUTTON_D: go_menu_10() ; break ;
    default:       go_menu_10() ; break ;
    }
}

void do_menu_12(uint16_t key_table)
{

    switch(key_table) {
    case BUTTON_1: go_menu_12() ; break ;
    case BUTTON_2: go_menu_12() ; break ;
    case BUTTON_D: go_menu_10() ; break ;        
    default:       go_menu_12() ; break ;
    }
}

void do_menu_20(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_21() ; ; break ;
    case BUTTON_2: go_menu_22() ; break ;
    case BUTTON_D: go_menu_top() ; break ;  
    default:       go_menu_20() ; break ;
    }
}

void do_menu_21(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_21() ; break ;
    case BUTTON_2: go_menu_21() ; break ;
    case BUTTON_D: go_menu_20() ; break ;
    default:       go_menu_21() ; break ;
    }
}

void do_menu_22(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_22() ; break ;
    case BUTTON_2: go_menu_22() ; break ;
    case BUTTON_D: go_menu_20() ; break ;
    default:       go_menu_22() ; break ;
    }
}

#define KEY_BUF_LEN 128
char key_buf[KEY_BUF_LEN+1] ;
int key_buf_index = 0 ;

void flash_key_buf(void)
{
    print("\n\r") ; /* new line */
    key_buf[key_buf_index] = 0 ; /* terminate the string */
    print(key_buf) ;
    print("\n\r") ; /* new line */
    key_buf_index = 0 ;
    key_buf[key_buf_index] = 0 ; /* clear string */
}

void do_str_test(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_POUND: 
        flash_key_buf() ;
        break ;
    case BUTTON_D:
        flash_key_buf() ;
        go_menu_top() ;
        break ;
    case BUTTON_0:
    case BUTTON_1:
    case BUTTON_2:
    case BUTTON_3:
    case BUTTON_4:
    case BUTTON_5:
    case BUTTON_6:
    case BUTTON_7:
    case BUTTON_8:
    case BUTTON_9:
    case BUTTON_A:
    case BUTTON_B:
    case BUTTON_C:
    case BUTTON_STAR:
        key_buf[key_buf_index] = key_table_to_letter(key_table) ;
        printc(key_buf[key_buf_index]) ; /* echo typed letter */
        key_buf_index++ ;
        if (key_buf_index >= KEY_BUF_LEN) { /* buffer full flash! */
            key_buf_index = KEY_BUF_LEN ;
            flash_key_buf() ;
        }
        break ;    
    default: /* invalid value for a letter, ignore it */
        break ;
    }
}

int main(void)
{
    uint16_t key_table = 0 ;
    uint16_t prev_key = 0 ;
    
    init_hardware() ;
    
    greeting() ;
    CyDelay(3000) ;
    
    go_menu_top() ;

    for(;;)
    {
        key_table = get_key_matrix() ;
        
        if ((key_table != 0) && (key_table != prev_key)) {  
            switch(menu_mode) {
            case MENU_TOP: do_menu_top(key_table) ; break ;
            case MENU_10:  do_menu_10(key_table)  ; break ;
            case MENU_11:  do_menu_11(key_table)  ; break ;
            case MENU_12:  do_menu_12(key_table)  ; break ;
            case MENU_20:  do_menu_20(key_table)  ; break ;
            case MENU_21:  do_menu_21(key_table)  ; break ;
            case MENU_22:  do_menu_22(key_table)  ; break ;
            case STR_TEST: do_str_test(key_table) ; break ;
            default:       menu_mode = MENU_TOP   ; break ;
            }
            CyDelay(500) ;
        }
        prev_key = key_table ;
    }
}

 

Best Regards,

29-Nov-2021

Motoo Tanaka

 

View solution in original post

3 Replies
MotooTanaka
Esteemed Contributor

Dear Yash-san,

The most difficult part of this discussion for me was to remember what I've done...

It would have been nice, if we could hear what you tried, 

but anyway here is my minimal sketch.

I added the third option to the top menu so what we don't have to destroy the previous project,

and in the 3rd option.

001-top-menu.JPG

The the program enters "string entering mode"

002-string_test.JPG

and each letter pushed will be echoed back to the serial terminal

003-entering-letters.JPG

and when '#' is pushed,  the buffered string will be written.

004-pound-entered.JPG

Or if you push 'D' 

005-again-entering-letters.JPG

The program returns to the top menu.

006-d-to-return.JPG

As usual, there are millions of way to implement something like this,

and I am positive that mine is not the most elegant way to do.

So please take my code as a "SAMPLE".

main.c

#include "project.h"
#include "stdio.h"

#define NUM_ROW 4
#define NUM_COL 4

volatile uint8_t key[NUM_ROW][NUM_COL] = { 0u } ;
uint8_t letter[NUM_ROW][NUM_COL] = {
    { '1', '2', '3', 'A' },
    { '4', '5', '6', 'B' },
    { '7', '8', '9', 'C' },
    { '*', '0', '#', 'D' }
} ;
    

volatile int row_no = 0 ;
volatile int col_no = 0 ;

void scan_key_matrix(void)
{
    switch(row_no) {
    case  0: ROW0_Write(1) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
    case  1: ROW0_Write(0) ; ROW1_Write(1) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
    case  2: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(1) ; ROW3_Write(0) ; break ;
    case  3: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(1) ; break ;
    default: ROW0_Write(0) ; ROW1_Write(0) ; ROW2_Write(0) ; ROW3_Write(0) ; break ;
    }

    switch(col_no) {
    case 0: key[row_no][col_no] = COL0_Read() ; break ;
    case 1: key[row_no][col_no] = COL1_Read() ; break ;
    case 2: key[row_no][col_no] = COL2_Read() ; break ;
    case 3: key[row_no][col_no] = COL3_Read() ; break ;
    }
    
    col_no++ ;
    if (col_no >= NUM_COL) {
        col_no = 0 ;
        row_no = (row_no + 1) % NUM_ROW ;
    } 
    ROW0_Write(0) ; 
    ROW1_Write(0) ; 
    ROW2_Write(0) ; 
    ROW3_Write(0) ; 
}

CY_ISR(my_tick_isr)
{
    scan_key_matrix() ;
}

#define STR_LEN 64
char str[STR_LEN+1] ;
void print(char *str)
{
    UART_PutString(str) ;
}

void printc(char c)
{
    UART_PutChar(c) ;
}

void cls(void)
{
    print("\033c") ; /* reset */
    CyDelay(20) ;
    print("\033[2J") ; /* clear screen */
    CyDelay(20) ;
}

void splash(void)
{
    cls() ;
    print("5LP 4x4 Matrix Key Test") ;
    snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;
    print(str) ;
}

void init_hardware(void)
{
    CyGlobalIntEnable; /* Enable global interrupts. */
    UART_Start() ;
    splash() ;
    CySysTickStart() ;
    CySysTickSetCallback(0, my_tick_isr) ;
}

uint16_t get_key_matrix(void)
{
    int col, row ;
    uint16_t value = 0 ;
    for (row = 0 ; row < NUM_ROW ; row++) {
        for (col = 0 ; col < NUM_COL ; col++) {
            value <<= 1 ;
            if (key[row][col]) {
                value |= 0x01 ;
            }
        }
    }   
    return( value ) ;
}



void print_key_table(uint16_t key_table)
{
    int row, col ;
    uint16_t mask = 0x8000 ; /* bin: 1000_0000_0000_0000 */
    snprintf(str, STR_LEN, "0x%04X: ", key_table) ;
    print(str) ;
    
    if (key_table & 0x8000) {
        print("1 is pushed ") ;
    }
    if (key_table & 0x4000) {
        print("2 is pushed ") ;
    }
    print("\n\r") ;
    
    for (row = 0 ; row < NUM_ROW ; row++) {
        for (col = 0 ; col < NUM_COL ; col++) {
            if (key_table & mask) {
                snprintf(str, STR_LEN, "%c ", letter[row][col]) ;
                print(str) ;
            } else {
                print("_ ") ;
            }
            mask >>= 1 ;
        }
        print("\n\r") ;
    }
}

#define BUTTON_1     0x8000
#define BUTTON_2     0x4000
#define BUTTON_3     0x2000
#define BUTTON_A     0x1000
#define BUTTON_4     0x0800
#define BUTTON_5     0x0400
#define BUTTON_6     0x0200
#define BUTTON_B     0x0100
#define BUTTON_7     0x0080
#define BUTTON_8     0x0040
#define BUTTON_9     0x0020
#define BUTTON_C     0x0010
#define BUTTON_STAR  0x0008
#define BUTTON_0     0x0004
#define BUTTON_POUND 0x0002
#define BUTTON_D     0x0001


#define MENU_TOP 0u
#define MENU_10  10
#define MENU_11  11
#define MENU_12  12
#define MENU_20  20
#define MENU_21  21
#define MENU_22  22
#define STR_TEST 99
int menu_mode = MENU_TOP ;

uint8_t key_table_to_letter(uint16_t key_table)
{
    uint8_t c = 0 ;
    
    switch(key_table) {
    case BUTTON_0:     c = '0' ; break ;
    case BUTTON_1:     c = '1' ; break ;
    case BUTTON_2:     c = '2' ; break ;
    case BUTTON_3:     c = '3' ; break ;
    case BUTTON_4:     c = '4' ; break ;
    case BUTTON_5:     c = '5' ; break ;
    case BUTTON_6:     c = '6' ; break ;
    case BUTTON_7:     c = '7' ; break ;
    case BUTTON_8:     c = '8' ; break ;
    case BUTTON_9:     c = '9' ; break ;
    case BUTTON_A:     c = 'A' ; break ;
    case BUTTON_B:     c = 'B' ; break ;
    case BUTTON_C:     c = 'C' ; break ;
    case BUTTON_D:     c = 'D' ; break ;
    case BUTTON_STAR:  c = '*' ; break ;
    case BUTTON_POUND: c = '#' ; break ;
    default: break ;
    }
    
    return( c ) ;
}

void greeting(void)
{
    cls() ;
    print("WELCOME TO THE") ; print("\n\r") ;
    print("EMP/COMP DBS")   ; print("\n\r") ;
}

void go_menu_top(void)
{
    cls() ;
    print("P1-Emp Details"); print("\n\r") ;
    print("P2-Comp Details"); print("\n\r") ;
    print("P3-String test") ; print("\n\r") ;
    menu_mode = MENU_TOP ;
}

void go_menu_10(void)
{
    cls() ;
    print("PRESS 1: NAME"); print("\n\r") ;
    print("PRESS 2: AGE"); print("\n\r") ;
    menu_mode = MENU_10 ;
}

void go_menu_11(void) 
{
    cls() ;
    print("Employee Name"); print("\n\r") ;
    print("Yash"); print("\n\r") ;
    menu_mode = MENU_11 ;   
}

void go_menu_12(void)
{
    cls() ;
    print("Employee Age"); print("\n\r") ;
    print("24"); print("\n\r") ;
    menu_mode = MENU_12 ;   
}

void go_menu_20(void)
{
    cls() ;
    print("Press1:Comp Name"); print("\n\r") ;
    print("Press2:Comp Addr"); print("\n\r") ;
    menu_mode = MENU_20 ;  
}

void go_menu_21(void)
{
    cls() ;
    print("Company Name:"); print("\n\r") ;
    print("ATPL"); print("\n\r") ;
    menu_mode = MENU_21 ;   
}

void go_menu_22(void)
{
    cls() ;
    print("Company Addr:"); print("\n\r") ;
    print("Hyd"); print("\n\r") ;
    menu_mode = MENU_22 ;   
}

void go_str_test(void)
{
    cls() ;
    print("String Entering Test: '#' to end line, 'D' to return to top menu") ; print("\n\r") ;
    menu_mode = STR_TEST ;
}

void do_menu_top(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_10() ; break ;
    case BUTTON_2: go_menu_20() ; break ;
    case BUTTON_3: go_str_test() ; break ;
    default:       go_menu_top() ; break ;
    }
}

void do_menu_10(uint16_t key_table)
{

    switch(key_table) {
    case BUTTON_1: go_menu_11() ; break ;
    case BUTTON_2: go_menu_12() ; break ;
    case BUTTON_D: go_menu_top() ; break ;
    default:       go_menu_10() ; break ;
    }
}

void do_menu_11(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_11() ; break ;
    case BUTTON_2: go_menu_11() ; break ;
    case BUTTON_D: go_menu_10() ; break ;
    default:       go_menu_10() ; break ;
    }
}

void do_menu_12(uint16_t key_table)
{

    switch(key_table) {
    case BUTTON_1: go_menu_12() ; break ;
    case BUTTON_2: go_menu_12() ; break ;
    case BUTTON_D: go_menu_10() ; break ;        
    default:       go_menu_12() ; break ;
    }
}

void do_menu_20(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_21() ; ; break ;
    case BUTTON_2: go_menu_22() ; break ;
    case BUTTON_D: go_menu_top() ; break ;  
    default:       go_menu_20() ; break ;
    }
}

void do_menu_21(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_21() ; break ;
    case BUTTON_2: go_menu_21() ; break ;
    case BUTTON_D: go_menu_20() ; break ;
    default:       go_menu_21() ; break ;
    }
}

void do_menu_22(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_1: go_menu_22() ; break ;
    case BUTTON_2: go_menu_22() ; break ;
    case BUTTON_D: go_menu_20() ; break ;
    default:       go_menu_22() ; break ;
    }
}

#define KEY_BUF_LEN 128
char key_buf[KEY_BUF_LEN+1] ;
int key_buf_index = 0 ;

void flash_key_buf(void)
{
    print("\n\r") ; /* new line */
    key_buf[key_buf_index] = 0 ; /* terminate the string */
    print(key_buf) ;
    print("\n\r") ; /* new line */
    key_buf_index = 0 ;
    key_buf[key_buf_index] = 0 ; /* clear string */
}

void do_str_test(uint16_t key_table)
{
    switch(key_table) {
    case BUTTON_POUND: 
        flash_key_buf() ;
        break ;
    case BUTTON_D:
        flash_key_buf() ;
        go_menu_top() ;
        break ;
    case BUTTON_0:
    case BUTTON_1:
    case BUTTON_2:
    case BUTTON_3:
    case BUTTON_4:
    case BUTTON_5:
    case BUTTON_6:
    case BUTTON_7:
    case BUTTON_8:
    case BUTTON_9:
    case BUTTON_A:
    case BUTTON_B:
    case BUTTON_C:
    case BUTTON_STAR:
        key_buf[key_buf_index] = key_table_to_letter(key_table) ;
        printc(key_buf[key_buf_index]) ; /* echo typed letter */
        key_buf_index++ ;
        if (key_buf_index >= KEY_BUF_LEN) { /* buffer full flash! */
            key_buf_index = KEY_BUF_LEN ;
            flash_key_buf() ;
        }
        break ;    
    default: /* invalid value for a letter, ignore it */
        break ;
    }
}

int main(void)
{
    uint16_t key_table = 0 ;
    uint16_t prev_key = 0 ;
    
    init_hardware() ;
    
    greeting() ;
    CyDelay(3000) ;
    
    go_menu_top() ;

    for(;;)
    {
        key_table = get_key_matrix() ;
        
        if ((key_table != 0) && (key_table != prev_key)) {  
            switch(menu_mode) {
            case MENU_TOP: do_menu_top(key_table) ; break ;
            case MENU_10:  do_menu_10(key_table)  ; break ;
            case MENU_11:  do_menu_11(key_table)  ; break ;
            case MENU_12:  do_menu_12(key_table)  ; break ;
            case MENU_20:  do_menu_20(key_table)  ; break ;
            case MENU_21:  do_menu_21(key_table)  ; break ;
            case MENU_22:  do_menu_22(key_table)  ; break ;
            case STR_TEST: do_str_test(key_table) ; break ;
            default:       menu_mode = MENU_TOP   ; break ;
            }
            CyDelay(500) ;
        }
        prev_key = key_table ;
    }
}

 

Best Regards,

29-Nov-2021

Motoo Tanaka

 

View solution in original post

Yash_r
New Contributor II

Hello, @MotooTanaka san,

Thank you for your sample. I will take it as an example method and build upon it.

I was trying out a stupid way, where I was trying to copy the letter[row][col] (directly instead of a char) and copy it to a buffer. I knew I was missing some point in the implementation and your sample helps me understand it.

Thank you so much.

Regards,
Yash

MotooTanaka
Esteemed Contributor

Hi,

Don't worry, at first I did the same 😜

Then I had to read my code again (lol)

moto