Arduino IDE 1.0.1がリリースされた。目玉はLeonardoの正式対応とマルチリンガル対応。MacOSXにインストールするとメニューが日本語化されていた。
 Leonardo正式対応ということで手持ちのDavinciを使ってUSB HID Keyboardのテストをやってみたが、コンパイルは通っても動作しない。これはDavinciのLeonardo相当ファームが古いのでは?と思い更新してみた。
 ファーム更新はAVR-ISP mkIIを使用する。Davinciにハンダ付けした6ピンのISP端子に写真の様に接続する。また、AVR-ISP mkIIでの書き込みでは別に電源が必要なため、DavinciにUSBケーブルを接続した。 また、Devinci上のショートプラグはショートしている。(写真右下にちょろっと見える)
2012isp
 
 書き込みはArduino-IDE1.0.1の[ツール]→[マイコンボード]でArduino Leonardoを選択。次に[ツール]→[書込装置]でAVRISPmkIIを選択。そして[ツール]→[ブートローダを書き込む]を実行。AVR-ISP mkII 装置のLEDがオレンジ色に光り、一分程で終了。
なぜか1回ではうまくいかず、DavinciをUSBケーブルでMac本体に接続してもシリアルポートを認識しなかった。2回目の書き込みでうまくいった。
ついでながら従来DavinciはWindows環境ではデバイスドライバをインストールできなかったのが、ファーム書き換えでArduino IDE1.0.1付属のデバイスドライバで認識できるようになった。まあLeonardoで書き換えられたんだからそうだろう。
 現在、発売されたばかりのArduino LeonardoとATmega32U4搭載マイコンボードを注文している。LeonardoのハードはほぼATmega32U4そのものなので、マイコンボードの方も書き換えられないかなあと思っている。

 HID-USB Keyboardのテストは以下。https://gist.github.com/2775713

/* Button for Keyboard (Arduino Leonardo only) */

/*
  Button
 
 Turns on and off a light emitting diode(LED) connected to digital  
 pin 13, when pressing a pushbutton attached to pin 2. 
 
 
 The circuit:
 * LED attached from pin 13 to ground 
 * pushbutton attached to pin 2 from +5V
 * 10K resistor attached to pin 2 from ground
 
 * Note: on most Arduinos there is already an LED on the board
 attached to pin 13.
 
 
 created 2005
 by DojoDave 
 modified 30 Aug 2011
 by Tom Igoe
 
 This example code is in the public domain.
 
 http://www.arduino.cc/en/Tutorial/Button
 */

// constants won't change. They're used here to 
// set pin numbers:
const int buttonPin = 2;     // the number of the pushbutton pin
const int keymacroPin = 3;
const int ledPin =  13;      // the number of the LED pin

// variables will change:
int buttonState = 0;         // variable for reading the pushbutton status

void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
  pinMode(keymacroPin, INPUT);
  digitalWrite(buttonPin, HIGH);
  digitalWrite(keymacroPin, HIGH);

  Keyboard.begin();
}

void loop(){
  if (digitalRead(keymacroPin)==LOW) {
    // not implemented
  }

  // read the state of the pushbutton value:
  buttonState = digitalRead(buttonPin);

  // check if the pushbutton is pressed.
  // if it is, the buttonState is HIGH:
  if (buttonState == LOW) {     
    // turn LED on:    
    digitalWrite(ledPin, HIGH); 
    delay(30);
    while(buttonState == LOW){
      buttonState = digitalRead(buttonPin);
      // Keyboard.press(ctrlKey);
      Keyboard.press(' ');
    }
  } 
  else {
    // turn LED off:
    digitalWrite(ledPin, LOW); 
    Keyboard.release(' ');
  }
  delay(100);
}

2012mouse

 これは以前作ったマウスクリックと同じようなもので、今回はボタンを押すとスペースキーを押しっぱなしにしたのと同じような動作になる。Arduino-IDE 1.0非公式のやり方ではKeyboard.write()でUSBキーボードの文字を押した動作になるが、押しっぱなしにする機能はなかった。Arduino1.0.1からはKeyboard.press()とKeyboard.release()で指示できる。このため前回はGoogle日本語モールス入力がスペースキーでは出来なかったのでマウスクリックにしたが、今回はスペースキーでOKになった。また、モディファイア(CTRLキー、SHIFTキー)も指示できるようだ。

 Mouse&Keyboardライブラリの使い方。Mouse & Keyboard libraries[arduino.cc]

モディファイアの定義などは以下にある。
hardware/arduino/cores/arduino/USBAPI.h

ちょっと抜き出すと:

#define KEY_LEFT_CTRL           0x80
#define KEY_LEFT_SHIFT          0x81
#define KEY_LEFT_ALT            0x82
#define KEY_LEFT_GUI            0x83
#define KEY_RIGHT_CTRL          0x84
#define KEY_RIGHT_SHIFT         0x85
#define KEY_RIGHT_ALT           0x86
#define KEY_RIGHT_GUI           0x87

#define KEY_UP_ARROW            0xDA
#define KEY_DOWN_ARROW          0xD9
#define KEY_LEFT_ARROW          0xD8
#define KEY_RIGHT_ARROW         0xD7
#define KEY_BACKSPACE           0xB2
#define KEY_TAB                         0xB3
#define KEY_RETURN                      0xB0
#define KEY_ESC                         0xB1
#define KEY_INSERT                      0xD1
#define KEY_DELETE                      0xD4
#define KEY_PAGE_UP                     0xD3
#define KEY_PAGE_DOWN           0xD6
#define KEY_HOME                        0xD2
#define KEY_END                         0xD5
#define KEY_CAPS_LOCK           0xC1
#define KEY_F1                          0xC2
#define KEY_F2                          0xC3
#define KEY_F3                          0xC4
#define KEY_F4                          0xC5
#define KEY_F5                          0xC6
#define KEY_F6                          0xC7
#define KEY_F7                          0xC8
#define KEY_F8                          0xC9
#define KEY_F9                          0xCA
#define KEY_F10                         0xCB
#define KEY_F11                         0xCC
#define KEY_F12                         0xCD




 うれしかったのは(1)Windowsでのデバイス認識、(2)USB-HIDキーボードの正式サポート。これであれがやっとできる!

参考:
2012/5/22 Arduino-1.0.1リリース[Arduino Wiki]
Arduino Leonardoへのガイド[スイッチサイエンス]

追記:モディファイアの設定も効いた。
      Keyboard.press(ctrlKey);
      Keyboard.press('a');
で全選択ができる(WindowsでCTRL+A)。
なお、ArduinoIDE1.0.1のMacOSX版では、Arduino(Leonardo)のシリアルポートがUSBケーブルの挿し直しなどで番号が変わってしまった場合にエラーとなる。ArduinoIDE1.0では接続すべきポートをサジェストしてくれていた。

ctrlaltdel.ino WindowsXPでこれを接続してボタンを押すとタスクマネージャが起動します。 

追記:
      Keyboard.press(shiftKey);
      Keyboard.press('2');
で表示されたのは'@'でした。つまりUSキーボード相当の扱いだと思われます。