#include "DigiKeyboard.h" #define CLOCK 2 #define DATA 0 #define BUFFER_SIZE 16 static volatile uint8_t keyBuffer[BUFFER_SIZE]; static volatile uint8_t head, tail, code, counter; uint8_t keyno; //英語キーボードとして扱われるので一部の記号を変換しておく char keydef[] = {'0', ',', '.', '\n', '1', '2', '3', '4', '5', '6', ':', '7', '8', '9', '-', '\b', '_', '/', '\"' }; void setup() { pinMode(CLOCK, INPUT_PULLUP); pinMode(DATA, INPUT); head = 0; tail = 0; code = 0; counter = 0; attachInterrupt(0, receiveData, FALLING); } void loop() { DigiKeyboard.sendKeyPress(0); keyno = getCode(); if(keyno != 0) { switch(keyno) { case 20: //undo DigiKeyboard.sendKeyStroke(KEY_Z, MOD_CONTROL_LEFT); break; case 21: //cut DigiKeyboard.sendKeyStroke(KEY_X, MOD_CONTROL_LEFT); break; case 22: //copy DigiKeyboard.sendKeyStroke(KEY_C, MOD_CONTROL_LEFT); break; case 23: //paste DigiKeyboard.sendKeyStroke(KEY_V, MOD_CONTROL_LEFT); break; default: DigiKeyboard.print(keydef[keyno - 1]); } } else DigiKeyboard.delay(100); } uint8_t getCode() { uint8_t retcode; if(head == tail) return 0; retcode = keyBuffer[head]; if(++head == BUFFER_SIZE) head = 0; return retcode; } void receiveData() { uint8_t p; code |= ((uint8_t)digitalRead(DATA) << counter); if(++counter > 4) { //キーバッファにコードを入れる p = tail; keyBuffer[p++] = code; if(p == BUFFER_SIZE) p = 0; if(p != head) tail = p; code = 0; counter = 0; } }