Arduino
serial

arduinoでターミナル

概要

arduinoでターミナルやってみる。
20字4行のLCDとps/2キーボードをつないで、シリアル端末を作ってみる。

写真

CIMG2525.JPG

回路図

image

キーボードを送信するサンプルコード

#include "Lcd4567.h"
#include "ps2.h"

Lcd4567 lcd;
PS2 kbd(7, 6);

int kl;
int el;

byte p[] = {
    0x16,
    0x1e,
    0x26,
    0x25,
    0x2e,
    0x36,
    0x3d,
    0x3e,
    0x46,
    0x45,
    0x4e,
    0x55,
    0x6A,
    0x66,
    0x0D,
    0x15,
    0x1D,
    0x24,
    0x2D,
    0x2C,
    0x35,
    0x3C,
    0x43,
    0x44,
    0x4D,
    0x54,
    0x5B,
    0x5D,
    0x58,
    0x1C,
    0x1B,
    0x23,
    0x2B,
    0x34,
    0x33,
    0x3B,
    0x42,
    0x4B,
    0x4C,
    0x52,
    0x5A,
    0x12,
    0x1A,
    0x22,
    0x21,
    0x2A,
    0x32,
    0x31,
    0x3A,
    0x41,
    0x49,
    0x4A,
    0x51,
    0x59,
    0x14,
    0x11,
    0x29,
    0x76,
    0x05,
    0x06,
    0x04,
    0x0C,
    0x03,
    0x0B,
    0x83,
    0x0A,
    0x01,
    0x09,
    0x78,
    0x07,
    0x77,
    0x7B,
    0x71,
    0x7C,
    0x79,
    0x70,
    0x69,
    0x72,
    0x7A,
    0x6B,
    0x73,
    0x74,
    0x6C,
    0x75,
    0x7D,
    0x84,
    0x7E,
    0x0E,
    0x67,
    0x64,
    0x13,
};
byte s[] = {
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    '0',
    '-',
    '^',
    0,
    8,
    9,
    'q',
    'w',
    'e',
    'r',
    't',
    'y',
    'u',
    'i',
    'o',
    'p',
    '@',
    '[',
    ']',
    0,
    'a',
    's',
    'd',
    'f',
    'g',
    'h',
    'j',
    'k',
    'l',
    ';',
    ':',
    10,
    0,
    'z',
    'x',
    'c',
    'v',
    'b',
    'n',
    'm',
    ',',
    '.',
    '/',
    0,
    0,
    0,
    0,
    ' ',
    27,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    '-',
    '.',
    '*',
    '+',
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    0,
    0,
    0,
    0,
    0,
    0,
};
byte p_ex[] = {
    0x4A,
    0x70,
    0x6c,
    0x7d,
    0x71,
    0x69,
    0x7a,
    0x75,
    0x6b,
    0x74,
    0x72,
    0x1F,
    0x27,
};
byte s_s[] = {
    '!',
    '"',
    '#',
    '$',
    '%',
    '&',
    '\'',
    '(',
    ')',
    0,
    '=',
    '~',
    '|',
    0,
    0,
    'Q',
    'W',
    'E',
    'R',
    'T',
    'Y',
    'U',
    'I',
    'O',
    'P',
    '@',
    '[',
    ']',
    0,
    'A',
    'S',
    'D',
    'F',
    'G',
    'H',
    'J',
    'K',
    'L',
    ';',
    ':',
    0,
    0,
    'Z',
    'X',
    'C',
    'V',
    'B',
    'N',
    'M',
    '<',
    '>',
    '?',
    '_',
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    0,
    '-',
    '.',
    '*',
    '+',
    '0',
    '1',
    '2',
    '3',
    '4',
    '5',
    '6',
    '7',
    '8',
    '9',
    0,
    0,
    0,
    0,
    0,
    0,
};
byte s_ex[] = {
    '/',
    155,
    36,
    19,
    127,
    35,
    34,
    82,
    80,
    79,
    81,
    0,
    0,
};
void kbd_init()
{
    char ack;
    kbd.write(0xff);
    ack = kbd.read();
    ack = kbd.read();
    kl = sizeof(p);
    el = sizeof(p_ex);
}
void setup()
{
    Serial.begin(115200);
    Serial.println("start0");
    kbd_init();
    lcd.begin(20, 4);
    lcd.setCursor(0, 0);
    lcd.print("ok");
}
void loop()
{
    unsigned char code;
    boolean release;
    boolean ext;
    boolean shift;
    shift = false;
    for ( ; ; )
    {
        ext = false;
        release = false;
        code = kbd.read();
        if (code == 0xE0)
        {
            ext = true;
            code = kbd.read();
        }
        if (code == 0xF0)
        {
            release = true;
            code = kbd.read();
        }
        if (ext)
        {
            if (code == 0x11 || code == 0x14 || code == 0x4A || code == 0x5A) ext = false;
        }
        if (code == 0x12 || code == 0x59)
        {
            shift = (release) ? false : true;
            continue;
        }
        int i = 0;
        if (!ext && !release)
        {
            for (i = 0; i < kl; i++)
            {
                if (code == p[i]) break;
            }
            if (i < kl)
            {
                if (!shift)
                {
                    Serial.println(s[i]);
                }
                else
                {
                    Serial.println(s_s[i]);
                }
            }
        }
        else if (ext && !release)
        {
            for (i = 0; i < el; i++)
            {
                if (code == p_ex[i]) break;
            }
            if (i < el)
            {
                if (!shift)
                {
                    Serial.println(s_ex[i]);
                }
            }
        }
    }
}




以上。