cancel
Showing results for 
Search instead for 
Did you mean: 

Code Examples

MotooTanaka
Esteemed Contributor

So I posted "A dumb typewriter sample (I2C keyboard and SPI TFT)" yesterday,

and I was expecting the porting to CY8CKIT-044 was a matter of re-target and re-compile.

昨日、5LP版のこのサンプルをアップしたのですが、CY8CKIT-044 への移植は

デバイスとピン配置を変えて、ほぼ再コンパイルだけで済むかと思ってたのですが。

A dumb typewriter sample (I2C keyboard and SPI TFT)

But as CY8CKIT-044 has Arduino Connector, I wanted to mount the TFT on it.

Then SPI uses SCB[3], which was also used by the UART to the KitProg.

CY8CKIT-044 には Arduino のコネクタがついてるので、TFT を直接マウントすると

Arduino タイプでは SPI は SCB[3] を使用することになります。

しかし SCB[3] は KitProg へ接続されている UART とかぶっていました。

So I had to change the UART to SCB[1] and connect an external USB-Serial adapter.

Anyway, so I decided to post this as another sample.

(Well I2C functions and isr were also modified)

UART を SCB[1] に変更して、外付けの USB-Serial を接続することにしました。

といった感じでバタバタしたので、別サンプルとして上げさせていただきます。

(I2C関連の関数も全変更、isr も外したなどもありましたが・・・)

IMG_4166.JPG

Tera Term log

Note: The cursor move keys are not reflected.

注:カーソル移動矢印キーはTera Term には反映されていません。

022-TeraTerm-log.JPG

schematic

020-schematic.JPG

pins

021-pins.JPG

main.c

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

#include "project.h"

#include <stdio.h>

#define TIMEOUT_MS 500

#define CARDKB_I2C_SLAVE_ADDR   0x5F

#include "TFT.h"

#include "Terminal6x8.h"

#define LOOP_INTERVAL  10  /* ms */

#define CURSOR_INTERVAL 50 /* LOOP_INTERVAL * CURSOR_INTERVAL */

int pos_x = 0 ;

int pos_y = 0 ;

char str[128] ; /* print buffer */

void print(char *str)

{

    UART_UartPutString(str) ;

}

void cls(void)

{

    print("\033c") ; /* reset */

    CyDelay(100) ;

    print("\033[2J") ; /* clear screen */

    CyDelay(100) ;

}

uint8_t myI2C_ReadByte(uint8_t reg_addr)

{

    uint8_t status ;

    uint8_t value = 0 ;

   

    I2C_I2CMasterClearStatus();

    status = I2C_I2CMasterSendStart(CARDKB_I2C_SLAVE_ADDR, I2C_I2C_WRITE_XFER_MODE, TIMEOUT_MS) ;

   

    if (I2C_I2C_MSTR_NO_ERROR == status) {

        I2C_I2CMasterWriteByte(reg_addr, TIMEOUT_MS);

        status = I2C_I2CMasterSendRestart(CARDKB_I2C_SLAVE_ADDR, I2C_I2C_READ_XFER_MODE, TIMEOUT_MS); 

    }

   

    if (I2C_I2C_MSTR_NO_ERROR == status) {  

        status = I2C_I2CMasterReadByte(I2C_I2C_ACK_DATA, &value, TIMEOUT_MS);

    }

    I2C_I2CMasterSendStop(TIMEOUT_MS); 

   

    return(value) ;

}

uint8_t read_cardkb(void)

{

    uint8_t key = 0 ;

    key = myI2C_ReadByte(1) ;

    return( key ) ;

}

void init_hardware(void)

{

    CyGlobalIntEnable; /* Enable global interrupts. */

   

    UART_Start() ;

   

    TFT_CS_Write(1) ;

    TFT_DC_Write(0) ;

    TFT_BL_Write(1) ;

   

    SPI_Start() ;

    SPI_Enable() ;

   

    TFT_Init() ;

    TFT_BusEnable(1) ;

    TFT_foreground(White) ;

    TFT_background(Black) ;

    TFT_set_orientation(0) ;

    TFT_cls() ;

    TFT_set_font(Terminal6x8) ;

    TFT_BL_Write(1) ;

    TFT_BusEnable(0) ;

   

    I2C_Start() ;

}

void font_cursor_up(void)

{

    pos_y -= TFT_font_height() ;

    if (pos_y < 0) {

        pos_y = TFT_height() - TFT_font_height() - 1 ;

    }

}

void font_cursor_down(void)

{

    pos_y += TFT_font_height() ;

    if (pos_y >= (TFT_height() - TFT_font_height() - 1)) {

        pos_y = 0 ;

    }

}

void font_cursor_forward(void)

{

    pos_x += TFT_font_width() ;

    if (pos_x >= (TFT_width() - TFT_font_width() - 1)) {

        pos_x = 0 ;

        font_cursor_down() ;

    }

}

void font_cursor_backward(void)

{

    pos_x -= TFT_font_width() ;

    if (pos_x < 0) {

        pos_x = TFT_width() - TFT_font_width() - 1 ;

        font_cursor_up() ;

    }

}

void printc(uint8_t key)

{

    TFT_locate(pos_x, pos_y) ;

    TFT_putc(key) ;

    font_cursor_forward() ;

    UART_UartPutChar(key) ;

}

void do_key(uint8_t key)

{

        switch(key) {

        case 0: // no key received, skip!

        case 0xFF: // also not a letter <- new

            break ;

        case 0x1B: // ESC (clear screen for now)

            pos_x = 0 ;

            pos_y = 0 ;

            TFT_cls() ;

            break ;

        case 0x0D: // Enter

            pos_x = 0 ;

            font_cursor_down() ;

            UART_UartPutString("\r\n") ;

            break ;

        case 0x08: // BackSpace

            font_cursor_backward() ;

            printc(' ') ;

            font_cursor_backward() ;

            break ;

        case 0xB4: // Left Arrow

            font_cursor_backward() ;

            break ;

        case 0xB5: // Up Arrow

            font_cursor_up() ;

            break ;

        case 0xB6: // Down Arrow

            font_cursor_down() ;

            break ;

        case 0xB7: // Right Arrow

            font_cursor_forward() ;

            break ;

        case 0x09: // Tab

            // currently no support

            break ;

        case 0x20: // Space

        default:

            printc(key) ;

            break ;

        }

}

void print_cursor(int color)

{

    int x0, x1, y0, y1 ;

    x0 = pos_x ;

    x1 = pos_x + TFT_font_width() ;

    y0 = pos_y ;

    y1 = pos_y + TFT_font_height() ;

    TFT_line(x0, y1, x1, y1, color) ;

}

void blink_cursor(void)

{

    static int num_count = 0 ;

    static int mode = 0 ;

    int16_t color ;

    if (num_count >= CURSOR_INTERVAL) {

        if (mode == 0) {

            mode = 1 ;

            color = White ;

        } else {

            mode = 0 ;

            color = Black ;

        }

        print_cursor(color) ;

        num_count = 0 ;

    } else {

        num_count++ ;

    }

}

int main(void)

{

    uint8_t key = 0 ;

   

    init_hardware() ;

   

    cls() ;

    sprintf(str, "TFT Test Program Started (%s %s)\n", __DATE__, __TIME__) ;

    UART_UartPutString(str) ;

   

    TFT_BusEnable(1) ;

    TFT_set_orientation(0) ;

   

    for(;;)

    {

        blink_cursor() ;

        key = read_cardkb() ;

        if (key) {

            print_cursor(Black) ;

            do_key(key) ;

        }

        CyDelay(LOOP_INTERVAL) ;

    }

}

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

Keybaord:

M5Stack Official CardKB Mini Keyboard Unit MEGA328P GROVE I2C USB ISP Programmer for ESP32 Arduino D...

M5Stack用カード型キーボードユニット - スイッチサイエンス

TFT Display:

Adafruit 2.8" TFT Touch Shield.

https://www.adafruit.com/product/1651

But I hope that this will work with SPI driven ILI9341 TFTs...

この TFT.[ch] は他の SPI  接続の ILI9341 TFT も動くとは思います・・・

moto

0 Likes
0 Replies