LCD keyboard CardKb

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
eeEn_4618901
Level 4
Level 4
5 likes given First like received First like given

Hi,

I2C Full Keyboard Sample (CardKb)

I want to use the project of Moto with character LCD.

I want to use character LCD.

I want to write the letters I write side by side and create a word.

I changed the function as follows. On the LCD screen, 1 letter is written and the other letter is pressed. I want to write a word.

void printc(char c)

{

    UART_PutChar(c) ;

sprintf(yaz, "%c",c);

     lcd_Position(1u,0u);

    lcd_PrintString(yaz);

}

best regards

0 Likes
1 Solution
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Eemuh-san,

So may I assume that now I'm "moto" on your LCD?  😜

Receiving a word from input stream is one of a basic of programming.

So I encourage you to study some c language programming basics.

Having said that following is my ad-lib attempt of receiving word.

Usually, space and tab also terminates a word, but here I only used Enter (0x0D).

When a word is received word_received is set.

Then take care of the word_buf[] before next read will take place.

I also added a simple handling of Backspace (0x08).

Other spacial control chars like, arrow key must be taken care of by you,

if it is required.

And please post a new question when you need to ask a different question.

(This one is done, though)

main.c

=====================

#include "project.h"

#include "stdio.h"

#define CARDKB_I2C_SLAVE_ADDR   0x5F

#define STR_LEN 64

char str[STR_LEN+1] ;

#define KEY_BUF_LEN 16

char key_buf[KEY_BUF_LEN] ;

int key_buf_index = 0 ;

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 CardKb Test") ;

    snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

    splash() ;

   

    I2C_Start() ;

}

uint8_t myI2C_ReadByte(uint8_t reg_addr)

{

    uint8_t status ;

    uint8_t value = 0 ;

   

    I2C_MasterClearStatus();

    status = I2C_MasterSendStart(CARDKB_I2C_SLAVE_ADDR, I2C_WRITE_XFER_MODE) ;

   

    if (I2C_MSTR_NO_ERROR == status) {

        I2C_MasterWriteByte(reg_addr);

        status = I2C_MasterSendRestart(CARDKB_I2C_SLAVE_ADDR, I2C_READ_XFER_MODE); 

    }

   

    if (I2C_MSTR_NO_ERROR == status) {  

        value = I2C_MasterReadByte(I2C_ACK_DATA);

    }

    I2C_MasterSendStop(); 

   

    return(value) ;

}

uint8_t read_cardkb(void)

{

    uint8_t key = 0 ;

    key = myI2C_ReadByte(1) ;

    return( key ) ;

}

int main(void)

{

    uint8_t key ;

    char word_buf[KEY_BUF_LEN+1] ; /* one for null terminator */

    int word_index = 0 ;

    int word_received = 0 ;

   

    init_hardware() ;

       

    for(;;)

    {

        key = read_cardkb() ;

        switch(key) {

        case 0: // no key received, skip!

            break ;

        case 0x0D: // Enter

            printc(key) ;

            word_buf[word_index] = 0 ;

            word_index++ ;

            word_received = 1 ;

            break ;

        case 0x08: // BackSpace

            if (word_index > 0) {

                word_index-- ;

                word_buf[word_index] = 0 ;

            }

            break ;

        case 0xB4: // Left Arrow

            break ;

        case 0xB5: // Up Arrow

            break ;

        case 0xB6: // Down Arrow

            break ;

        case 0xB7: // Right Arrow

            break ;

        case 0x09: // Tab

            break ;

        case 0x20: // Space

        default:

            printc(key) ;

            word_buf[word_index] = key ;

            word_index++ ;

            if (word_index >= KEY_BUF_LEN) {

                word_buf[word_index] = 0 ;

                word_received = 1 ;

            }

            break ;

        }

        if (word_received) {

            print("Word: ") ;

            print(word_buf) ;

            print("\n\r") ;

            word_received = 0 ;

            word_index = 0 ;

        } 

        CyDelay(10) ;

    }

}

=====================

Best Regards,

18-Mar-2020

Motoo Tanaka

View solution in original post

0 Likes
5 Replies
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Hi,

Oh, you have already lcd_PrintString()?

Let's see... I would do

Note: This is an ad-lib ... you are warned.

=========================

#define LCD_WIDTH   8  // specify your LCD width here

#define LCD_HEIGHT 2  // specify your LCD height here

int lcd_x = 0 ;

int lcd_y = 0 ;

void printc(char c)

{

  char buf[2] = { 0, 0 } ;

   buf[0] = c ; /* added later */

   UART_PutChar(c) ;

   lcd_Position(lcd_x, lcd_y) ; /* may be this is (lcd_y, lcd_x) .. it's up to your LCD */

   lcd_PrintString(buf) ; /* As buf[1]=0, this is same with lcd_PutChar() if it exists */

   lcd_x++ ;

   if (lcd_x >= LCD_WIDTH) {

      lcd_x = 0 ;

      lcd_y++ ;

      if (lcd_y >= LCD_HEIGHT) {

        lcd_y = 0 ;

      }

}

=========================

moto

Hi Moto,

for example, I wrote moto. but he writes up and down.

moto.jpg

0 Likes
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Eemuh-san,

This is the case, the position of lcd_x and lcd_y were wrong with my sample.

So I wrote about it in the comment...

Please change the line below

   lcd_Position(lcd_x, lcd_y) ; /* may be this is (lcd_y, lcd_x) .. it's up to your LCD */

to

   lcd_Position(lcd_y, lcd_x) ;

and also

#define LCD_WIDTH   8  // specify your LCD width here

#define LCD_HEIGHT 2  // specify your LCD height here

to

#define LCD_WIDTH  16  // specify your LCD width here

#define LCD_HEIGHT  2  // specify your LCD height here

and try.

Best Regards,

18-Mar-2020

Motoo Tanaka

P.S.

Well, I also like

mt

oo

Hi mt

     oo 

I will add this keyboard to another project. For example, I wrote a word and pressed the enter button. After I press the enter button, I want the word I wrote to be loaded into the  array.

uint8_t yukle(void)

{

    uint8_t key1 = 0 ;

    uint8_t yukle[16] ;

        key1 = myI2C_ReadByte(1) ;

        if(key1!=0x0D){

            for(int i=0;i<16;i++){

            yukle=key1;

            }

        }

    return( yukle ) ;

}

carkb.JPG

0 Likes
lock attach
Attachments are accessible only for community members.
MotooTanaka
Level 9
Level 9
Distributor - Marubun (Japan)
First comment on blog Beta tester First comment on KBA

Dear Eemuh-san,

So may I assume that now I'm "moto" on your LCD?  😜

Receiving a word from input stream is one of a basic of programming.

So I encourage you to study some c language programming basics.

Having said that following is my ad-lib attempt of receiving word.

Usually, space and tab also terminates a word, but here I only used Enter (0x0D).

When a word is received word_received is set.

Then take care of the word_buf[] before next read will take place.

I also added a simple handling of Backspace (0x08).

Other spacial control chars like, arrow key must be taken care of by you,

if it is required.

And please post a new question when you need to ask a different question.

(This one is done, though)

main.c

=====================

#include "project.h"

#include "stdio.h"

#define CARDKB_I2C_SLAVE_ADDR   0x5F

#define STR_LEN 64

char str[STR_LEN+1] ;

#define KEY_BUF_LEN 16

char key_buf[KEY_BUF_LEN] ;

int key_buf_index = 0 ;

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 CardKb Test") ;

    snprintf(str, STR_LEN, "(%s %s)\n", __DATE__, __TIME__) ;

    print(str) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

    UART_Start() ;

    splash() ;

   

    I2C_Start() ;

}

uint8_t myI2C_ReadByte(uint8_t reg_addr)

{

    uint8_t status ;

    uint8_t value = 0 ;

   

    I2C_MasterClearStatus();

    status = I2C_MasterSendStart(CARDKB_I2C_SLAVE_ADDR, I2C_WRITE_XFER_MODE) ;

   

    if (I2C_MSTR_NO_ERROR == status) {

        I2C_MasterWriteByte(reg_addr);

        status = I2C_MasterSendRestart(CARDKB_I2C_SLAVE_ADDR, I2C_READ_XFER_MODE); 

    }

   

    if (I2C_MSTR_NO_ERROR == status) {  

        value = I2C_MasterReadByte(I2C_ACK_DATA);

    }

    I2C_MasterSendStop(); 

   

    return(value) ;

}

uint8_t read_cardkb(void)

{

    uint8_t key = 0 ;

    key = myI2C_ReadByte(1) ;

    return( key ) ;

}

int main(void)

{

    uint8_t key ;

    char word_buf[KEY_BUF_LEN+1] ; /* one for null terminator */

    int word_index = 0 ;

    int word_received = 0 ;

   

    init_hardware() ;

       

    for(;;)

    {

        key = read_cardkb() ;

        switch(key) {

        case 0: // no key received, skip!

            break ;

        case 0x0D: // Enter

            printc(key) ;

            word_buf[word_index] = 0 ;

            word_index++ ;

            word_received = 1 ;

            break ;

        case 0x08: // BackSpace

            if (word_index > 0) {

                word_index-- ;

                word_buf[word_index] = 0 ;

            }

            break ;

        case 0xB4: // Left Arrow

            break ;

        case 0xB5: // Up Arrow

            break ;

        case 0xB6: // Down Arrow

            break ;

        case 0xB7: // Right Arrow

            break ;

        case 0x09: // Tab

            break ;

        case 0x20: // Space

        default:

            printc(key) ;

            word_buf[word_index] = key ;

            word_index++ ;

            if (word_index >= KEY_BUF_LEN) {

                word_buf[word_index] = 0 ;

                word_received = 1 ;

            }

            break ;

        }

        if (word_received) {

            print("Word: ") ;

            print(word_buf) ;

            print("\n\r") ;

            word_received = 0 ;

            word_index = 0 ;

        } 

        CyDelay(10) ;

    }

}

=====================

Best Regards,

18-Mar-2020

Motoo Tanaka

0 Likes