0 Replies Latest reply on Mar 24, 2020 3:12 AM by MoTa_728816

    A dumb typewriter sample (I2C keyboard and SPI TFT)

    MoTa_728816

      IMG_4161.JPG

      While ago, I wrote a sample for using the I2C keyboard (CardKb)

      少し前に、I2C経由のフルキーボード (CardKb)のサンプルを書く機会がありました。

      I2C Full Keyboard Sample (CardKb)

       

      And a long ago, I ported the UniGraph library from mbed and converted it from C++ to C.

      (So I screamed that I want C++ !!!)

      大分前に、mbed で使っていた UniGraph ライブラリを C++ から C に移植しました。

      (だから C++ 欲しいっていったのに~ (T^T) )

      Re: What LCD screen to use with a psoc 5lp-059 to create an LCD oscilliscope?

       

      So I decided that it's a time to connect both of them.

      で、この機会に両方をつないでみることにしました。

       

      This time I implemented the minimum type-write like functions.

      And also it supports cursor key (up, down, right, left), backspace.

      ESC key to clear screen.

      And the letters entered are also output to UART.

      今回は最低限のタイプライタもどきな動きができるのと

      カーソルキーの移動 (上、下、右、左)、バックスペースを

      付けてみました。 ESC を押すと画面クリアになります。

       

      As usual only the minimum test has been done.

      But anyway you get the source code ;-P

      例によって、テストは最低限しか行っていませんが、

      まぁ、ソースコードあるので・・・ということで (^ ^;;

       

      Now we have keyboard input and display output,

      what shall we do next?

      さてキーボード入力とディスプレイ出力が付いたわけですが、

      次は何をしましょう?

       

      schematic

      000-schematic.JPG

      pins

      001-pins.JPG

      main.c

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

      #include "project.h"

      #include <stdio.h>

       

      #define CARDKB_I2C_SLAVE_ADDR   0x5F

       

      #include "TFT.h"

      #include "Terminal6x8.h"

      volatile int spi_done_flag = 0 ;

       

      #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_PutString(str) ;

      }

       

      CY_ISR(spi_done_isr)

      {

          SPI_ReadTxStatus() ;

          spi_done_flag =  1;

      }

       

      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_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 ) ;

      }

       

      void init_hardware(void)

      {

          CyGlobalIntEnable; /* Enable global interrupts. */

         

          UART_Start() ;

         

          TFT_CS_Write(1) ;

          TFT_DC_Write(0) ;

          TFT_BL_Write(1) ;

         

          isr_tx_StartEx(spi_done_isr) ;

       

       

          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_PutChar(key) ;

      }

       

      void do_key(uint8_t key)

      {

              switch(key) {

              case 0: // no key received, skip!

                  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_PutString("\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_PutString(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 Development Board STE…

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

       

      moto