PSoC3 PS/2キーボードハンドラ main.c

投稿日 2012/06/03

//main.c
#include <device.h>
#include <stdio.h>
#include <scancodes.h>

 

void I2C_LCD_Init(void);
void I2C_LCD_WriteString(uint8, uint8, uint8*);
void I2C_LCD_SetPosition(uint8, uint8);
void I2C_LCD_WriteChar(char8);
void I2C_LCD_ClearLine2(void);

 

void main(){
 uint8 scan_code;
 uint16 post_code;
 unsigned char key_up = 0, shift_key = 0;
 unsigned char i;
 unsigned char buffer_pos = 0;
 unsigned char caps_lock = 0;
 char kb_data[16];
 char str[16];


 I2C_Start();
 I2C_LCD_Init();
 I2C_LCD_WriteString(0, 0, "PS2 Kbd" );
 SPIS_Start();

 

 while(1){
  SPIS_ClearRxBuffer();
  while(SPIS_ReadRxStatus() & SPIS_STS_RX_FIFO_EMPTY);
  scan_code = (SPIS_ReadRxData() >> 1) & 0x00FF; /* Data only - Parity bit is ignored */
  if(!key_up){ // to avoid break code
   switch(scan_code){
    case 0xE0: break; //Ignore
    case 0xF0: key_up = 1; break; //Break code
    case 0x05: I2C_LCD_WriteString(11, 0, "F1 " ); break; //F1
    case 0x06: I2C_LCD_WriteString(11, 0, "F2 " ); break; //F2
    case 0x04: I2C_LCD_WriteString(11, 0, "F3 " ); break; //F3
    case 0x0C: I2C_LCD_WriteString(11, 0, "F4 " ); break; //F4
    case 0x03: I2C_LCD_WriteString(11, 0, "F5 " ); break; //F5
    case 0x0B: I2C_LCD_WriteString(11, 0, "F6 " ); break; //F6
    case 0x83: I2C_LCD_WriteString(11, 0, "F7 " ); break; //F7
    case 0x0A: I2C_LCD_WriteString(11, 0, "F8 " ); break; //F8
    case 0x01: I2C_LCD_WriteString(11, 0, "F9 " ); break; //F9
    case 0x09: I2C_LCD_WriteString(11, 0, "F10 " ); break; //F10
    case 0x78: I2C_LCD_WriteString(11, 0, "F11 " ); break; //F11
    case 0x07: I2C_LCD_WriteString(11, 0, "F12 " ); break; //F12
    case 0x5A: I2C_LCD_WriteString(11, 0, "Enter" ); break; //Enter
    case 0x0D: I2C_LCD_WriteString(11, 0, "TAB " ); break; //TAB
    case 0x12: shift_key = 1; I2C_LCD_WriteString(11, 0, "LSIFT" ); break; //Left shift
    case 0x14: I2C_LCD_WriteString(11, 0, "CTRL " ); break; //Left CTRL
    case 0x11: I2C_LCD_WriteString(11, 0, "ALT " ); break; //Left ALT
    case 0x59: shift_key = 1; I2C_LCD_WriteString(11, 0, "RSIFT" ); break; //Right shift
    case 0x75: I2C_LCD_WriteString(11, 0, "UP " ); break; //UP ALLOW
    case 0x72: I2C_LCD_WriteString(11, 0, "DOWN " ); break; //DOWN ALLOW
    case 0x6B: I2C_LCD_WriteString(11, 0, "LEFT " ); break; //LEFT ALLOW
    case 0x74: I2C_LCD_WriteString(11, 0, "RIGHT" ); break; //RIGHT ALLOW
    case 0x7D: I2C_LCD_WriteString(11, 0, "PgUp " ); break; //Page Up
    case 0x7A: I2C_LCD_WriteString(11, 0, "PgDn " ); break; //Page Down
    case 0x6C: I2C_LCD_WriteString(11, 0, "Home " ); break; //Home
    case 0x69: I2C_LCD_WriteString(11, 0, "End " ); break; //End
    case 0x71: I2C_LCD_WriteString(11, 0, "Del " ); break; //Delete
    case 0x70: I2C_LCD_WriteString(11, 0, "Ins " ); break; //Insert
    case 0x1F: I2C_LCD_WriteString(11, 0, "L-Win" ); break; //Left Win
    case 0x27: I2C_LCD_WriteString(11, 0, "R-Win" ); break; //Righ Win
    case 0x2F: I2C_LCD_WriteString(11, 0, "APPLI" ); break; //Application
    case 0x58: //CAPS
     if(caps_lock) caps_lock = 0; else caps_lock = 1; I2C_LCD_WriteString(11, 0, "CAPS " ); break;
    case 0x76: //ESC key to clear the display/
     I2C_LCD_WriteString(11, 0, "ESC " ); 
     buffer_pos = 0;
     kb_data[0] = 0;
     I2C_LCD_ClearLine2();
     break;
    case 0x66: //Backspace - delete last character from buffer
     I2C_LCD_WriteString(11, 0, "BS " ); 
     if(buffer_pos != 0) buffer_pos--;
     kb_data[buffer_pos] = 0;
     I2C_LCD_SetPosition(buffer_pos, 1);
     I2C_LCD_WriteChar(' ');
     break;
    default:
     if(buffer_pos < 16){
      for(i = 0; codes[i][0] != scan_code && codes[i][0]; i++);
      if(caps_lock == 0 && shift_key == 1) kb_data[buffer_pos] = codes[i][1];
      else if(caps_lock == 1 && shift_key == 0 && i < 26) kb_data[buffer_pos] = codes[i][1];
      else if(caps_lock == 1 && shift_key == 1 && i < 26) kb_data[buffer_pos] = codes[i][2];
      else kb_data[buffer_pos] = codes[i][2];

 

      I2C_LCD_SetPosition(buffer_pos, 1);
      I2C_LCD_WriteChar(kb_data[buffer_pos]);
      buffer_pos++;
      break;
     }
    } /* switch */
    if(scan_code != 0xF0 && scan_code != 0xE0){
    post_code = (uint16)(scan_code);
    sprintf(str, "%02X", post_code);
    I2C_LCD_WriteString(8, 0, str);
   }
  } else {
   key_up = 0;
   switch(scan_code) {
    case 0x12: shift_key = 0; break;
    case 0x59: shift_key = 0; break;
    default: break;
   }
  }
 } /* while */

 

(JF1VRR)