Help us understand the problem. What is going on with this article?

TI SensorTagのボタンとGUIの暗黒面 "SendKeys" とを組み合わせてプレゼンリモコンをつくる

More than 3 years have passed since last update.

まずはこれを見みやがれです (30秒動画)

TI sensotTagをプレゼン用クリッカーに仕立て上げる (simpleKeyのみ)

本当はこんな感じのX'masをやりたかったお。。。

GUIスクリプティングで面倒なのがAPI操作。Officeやブラウザを操作しようとすると、COM等のAPIを叩く必要がありますが、正直めんどうです
しかし、GUIには "SendKeys"系のダークフォースがあります。即ち「キーを押したことにする」仕組みです

今回はおなじみ TI SensorTagのボタンをキー入力に割り当てて、プレゼン用リモコンに仕立ててみます

環境

  • Ubuntu 15.10
  • Node.js 5.3.0

コード

run.js
/*
 * Presentation Clicker by TI SensorTag
 * @ma2shita
 * License: MIT
 *
 * Usage:
 * $ npm install sensortag
 * # node run.js
 */
var myUUID = process.env["TI_UUID"] || "5c313ebfec2a";

var execSync = require('child_process').execSync;
function xdotool(keyscan) {
    var c = 'xdotool search --onlyvisible --class libreoffice windowfocus key "' + keyscan + '"';
    console.log(c);
    try {
        var r = "" + execSync(c);
    } catch(e) {
        //Nothing Todo
    }
}

function key_event(keyscan) {
    xdotool(keyscan);
}

function ti_simple_key(tag) {
    tag.notifySimpleKey(function() {
        console.info("READY!: notifySimpleKey");
        tag.on("simpleKeyChange", function(left, right) { /* run per pushed button */
             if (!left &&  right) { key_event("Right"); }
             if ( left && !right) { key_event("Left"); }
        });
    });
}

/* MAIN */
var SensorTag = require('sensortag');
console.info(">> STOP: Ctrl+C or SensorTag power off");
console.info("start");
console.info("waiting for connect from " + myUUID);
SensorTag.discoverByUuid(myUUID, function(sensorTag) {
    console.info("FOUND!: connect and setup ... (waiting 5~10 seconds)");
    sensorTag.connectAndSetup(function() {
        sensorTag.readDeviceName(function(error, deviceName) {
            console.info("connect: " + deviceName);
            ti_simple_key(sensorTag);
        });
    });
    /* In case of SensorTag PowerOff or out of range when fired `onDisconnect` */
    sensorTag.on("disconnect", function() {
        sensorTag.disableAccelerometer(function(e){});
        console.info("disconnect and exit");
        process.exit(0);
    });
});

解説

TI sensorTag の simpleKey のイベントから child_process で xdotool を起動してるだけです

えーっと、node.js成分が足りず、asyncとか使えません。cb地獄をお楽しみください

実行

$ npm install sensortag
$ sudo node run.js

改造ポイント

各OSへのポーティング

  • Ubuntu(Linux): xdotool を使いましょう
  • OS X(Mac): osascript を使いましょう。具体的な使い方はこちらが詳しいです
  • Windows: Windows Host Scriptingの SendKeys メソッドを使うことになります。cscript.exe はインライン実行のオプションがないのでファイルに保存したものを実行するなり、工夫してください

加速度センサーを使ってみる

振ると "Escape" の押下が発火する機能を入れてみます
こんな感じで動きます (30秒動画)

TI sensotTagをプレゼン用クリッカーに仕立て上げる (振るとEscape)

コード

--- run.js  2015-12-24 13:22:56.416042611 +0900
+++ run_w_accel.js  2015-12-24 13:22:35.079839374 +0900
@@ -34,6 +34,27 @@
    });
 }

+function ti_accelerometer(tag) {
+   var borderOverCounter = 0;
+   var accelerometerPeriod = 300; // ms
+   tag.enableAccelerometer(function(e) {
+       tag.setAccelerometerPeriod(accelerometerPeriod, function(e) {
+           tag.notifyAccelerometer(function(e){
+               console.info("READY!: notifyAccelerometer");
+               tag.on('accelerometerChange', function(x, y, z) {
+                   var max = Math.max(Math.abs(x), Math.abs(y), Math.abs(z));
+                   if (max > 7.0) {
+                       borderOverCounter++;
+                   } else {
+                       borderOverCounter = 0; // reset
+                   }
+                   if (borderOverCounter > 1) { key_event("Escape"); }
+               });
+           });
+       });
+   });
+}
+
 /* MAIN */
 var SensorTag = require('sensortag');
 console.info(">> STOP: Ctrl+C or SensorTag power off");
@@ -45,6 +66,7 @@
        sensorTag.readDeviceName(function(error, deviceName) {
            console.info("connect: " + deviceName);
            ti_simple_key(sensorTag);
+           ti_accelerometer(sensorTag);
        });
    });
    /* In case of SensorTag PowerOff or out of range when fired `onDisconnect` */

あとがき

よい年末を!

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
No comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
ユーザーは見つかりませんでした