2015年6月14日日曜日

[Raspberry Pi]おすすめの本

Raspberry Piでおすすめの本をご紹介いたします。


こんな人におすすめです。
・プログラミングの経験はあるけれども、40本のピンをどう使えばよいのかわからずにもてあましていて、LEDをチカチカさせてみたい人。プログラミングの知識は入門書程度の内容で大丈夫です。その他本書ではタクトスイッチ、温度センサー、モーター、WebIOPiが取り上げられています。
 ・Python (PiはPythonに由来するそうです)
 ・Ruby
 ・Java
 ・C / C++
 ・Scratch 等
・Raspberry Piに興味があってはじめてみようとおもっているけれども、まわりにやっている人がいなくて何を買いそろえればよいのかわからない人。
・予備知識はないけどとにかくRaspberry Piをはじめてみようというやる気のある人。プログラムはダウンロードができるのではじめはそれをそのまま使うことでよいとおもいます。Pythonはドットインストール様で学ぶことができます。
・サポートページがありまして、最新内容を反映してくださっている点もおすすめポイントです。

楽しみましょう!


[android]Android Design Support Libraryをつかってみる

Android Design Support Libraryはこちらで紹介されています。

日本語では「Android Design Support Libraryを使う」で紹介されています。
サンプルコードも紹介いただいています。
ありがとうございます。

自分が作っているアプリにFloatingActionButtonを組み込もうとおもってレイアウトに、追加してみたのですが実行しようとするとレイアウトのインフレートで失敗しました。

こんなエラー内容でした。

android.view.InflateException: Binary XML file line #19: Error inflating class android.support.design.widget.FloatingActionButton
(略)
Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class android.support.design.widget.FloatingActionButton

インフレートに失敗した原因は下記のようです。
* Activityはandroid.support.v7.app.AppCompatActivityを継承していないといけないようです。Support Libraryですからね。
* (言わずもがなかもしれませんが……)ということで、マニフェストで指定するandroid:themeのparentはTheme.AppCompat.Light.DarkActionBar等にしないといけないようです。


上記とは関係ありませんが、Android Devlopersみてて気づいたのでついでに書いておきます。Data Bindingについて書いておきます。
レイアウトのxmlにデータクラス名を書いておくとたとえばTextViewなんかで、下記みたいに書けるようになるそうです。

<TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>



Google I/O 2015 - What's new in Androidで紹介されていたData Bindingのガイドがありました。こういう書き方が今後の主流になるんじゃなかろうかと個人的にはおもいます。


2015年6月12日金曜日

[Raspbery Pi]天気予報をしゃべる目覚まし時計

Raspberry Piで天気予報をしゃべる目覚まし時計を作ってみました。


天気予報はWeather Hacks様で調べることにしました。
音声ファイルはdocomo Developer support様の音声合成で作ります。
Open JTalkを使う方法もあるようです。詳しくは第20回「ラズベリーパイで手作り目覚まし時計!(2)音声アラーム編」。とても詳しく書かれています。

Rubyで作りました。
$ ruby -v
ruby 2.0.0p645 (2015-04-13 revision 50299) [armv7l-linux-eabihf]

ソースコードはこんな感じです。
docomo Developer supportの音声合成API Keyは取得したものを記載してください。

あとはこれを$ crontab -e とかで
45 6 * * * <command>
みたいなのを書いておけば6:45に天気予報をしゃべってくれます。
cronも第19回「ラズベリーパイで手作り目覚まし時計!ストップボタン編」の説明をご紹介しておきます。
参考にさせていただきました。ありがとうございます。


require 'open-uri'
require 'json'
require 'uri'

# http://weather.livedoor.com/weather_hacks/
class Weather
  attr_accessor :id

  BASE_URL = 'http://weather.livedoor.com/forecast/webservice/json/v1?city='

  def initialize(id)
    @id = id
  end

  def get
    json = nil
    open("#{BASE_URL}#{id}") do |f|
      json = f.inject('') do |memo, line|
        memo + line
      end
    end
    parse(json)
  end

  private
  def parse(json_text)
    json = JSON.parse(json_text)
    "今日の" + json['location']['prefecture'] + json['location']['city'] + "の天気は" + json['forecasts'][0]['telop'] + 'です'
  end
end

# https://dev.smt.docomo.ne.jp/
class TextToFile
  attr_accessor :msg, :speaker, :file_path

  # https://dev.smt.docomo.ne.jp/?p=docs.api.page&api_name=text_to_speech&p_name=api_hoya#tag01
  API_KEY = 'Your API Key'
  URL = "https://api.apigw.smt.docomo.ne.jp/voiceText/v1/textToSpeech?APIKEY=#{API_KEY}"

  def initialize(opts)
    @msg = opts[:msg]
    @speaker = opts[:speaker]
    @speaker ||= %w(show haruka hikari takeru santa bear).sample(1)[0]
    @file_path = opts[:file_path]
  end

  def to_file
    uri = URI.parse(URL)
    https = Net::HTTP.new(uri.host, uri.port)

    https.use_ssl = true
    req = Net::HTTP::Post.new(uri.request_uri)

    req["Content-Type"] = "application/x-www-form-urlencoded"
    req.body = "text=#{URI.encode(msg)}&speaker=#{speaker}&speed=50&format=wav"

    res = https.request(req)

    File.open(file_path, 'wb') do |file|
      file.write(res.body)
    end
  end
end

# 調べたい地域のIDを指定してください。
# http://weather.livedoor.com/forecast/rss/primary_area.xml
w = Weather.new('130010')
TextToFile.new(msg: w.get, speaker: 'haruka', file_path: 'speech.wav').to_file

`aplay speech.wav`




[Raspberry Pi]無線LANアダプターでつなぐ

簡単でした。
1. I-O DATA WN-G150UをUSBポートに挿して電源投入
2. この時点で無線LANアダプターが青くピカピカ光っています
3. 右上のインターネットのアイコンをクリックするとSSIDが表示されるのでパスフレーズ等入力してください(お使いのWi-Fiルートの裏とかに書いてあるあれです)


以前は設定ファイルみたいなのを編集する必要があったようですが、挿すだけで使えてしまいました。

【環境】
* Raspberry Pi 2 Model B
* OS Raspbian: NOOBS 1.4.1(2015-05-11)でインストールしたやつです。
Linux raspberrypi 3.18.14-v7+ #793 SMP PREEMPT Sat May 30 14:04:35 BST 2015 armv7l GNU/Linux

バージョンの調べ方はこちらを参考にさせていただきました。
ありがとうございます。
$ uname -a

2015年6月9日火曜日

[android]ActiveNotification

Android M Developer Previewのサンプルを読んでみます。
ActiveNotification
・Activityが2つ、Fragmentが1つのサンプルです。
 ・ActivityはActiveNotificationActivityがMainActivityを継承しています。
  ManifestにはActiveNotificationActivityだけが書かれていますので、このActiveNotificationActivityだけが使われています。
・起動すると、ActiveNotificationFragmentインスタンスをFragmentTransaction#replaceしています
・ActiveNotificationFragmentは画面やボタンの初期化をしています
・「ADD A NOTIFICATION」ボタンを押すと、ActiveNotificationFragment#addNotificationAndReadNumber()が呼ばれて、NotificationManager#notifyでNotificationが画面上部に追加されます
・このNotificationは消えるときに、Intent("com.example.android.activenotifications.delete")がBroadcastされます
・このIntentはActiveNotificationActivity#onResumeで登録されたBroadcastReceiverで受け取ります。
・BroadcastReceiverがIntentを受け取ると、ActiveNotificationFragment #updateNumberOfNotificationsを呼び出して、Notification数の更新をしています。
・このサンプルのポイントです。android.app.NotificationManager#getActiveNotifications()というAPIがMから追加されています。このAPIで現在表示されているNotificationの数を数えられます!




2015年6月8日月曜日

[Raspberry Pi]イヤホンジャックに差し込んでも音が鳴らない そんなときは

画面右上のスピーカーのアイコンを右クリックして、HDMIをAnalogに変更してみてください。

Raspberry Pi 2 Model B
OS: RASPBIAN Release date:2015-05-05





2015年6月7日日曜日

[android]Fingerprint Dialog

Android M Developer Previewの話です。
Fingerprint Dialog Sample を読んでみます。

・このサンプルはInjectを使って変数の初期化をしています
・com.example.android.fingerprintdialog.MainActivity#onCreateではまず、requestPermissions(new String[]{Manifest.permission.USE_FINGERPRINT}, 0);を使って指紋認証パーミッションの許可を取得しています
・com.example.android.fingerprintdialog.MainActivity#onRequestPermissionsResult
では指紋認証の許可が取れていた場合、android.app.KeyguardManager#isKeyguardSecureで設定がされているかどうかをチェックしています。設定されていなければ「"Secure lock screen hasn't set up.\nGo to 'Settings -> Security -> Fingerprint' to set up a fingerprint"」をToast表示して終了です。
・まず、Settings -> Security -> Fingerprintで設定をしています。指示通り進めていくと、指紋センサーに触るように言われるのでエミュレータの場合は、「$ adb -e emu finger touch <finger_id>」で指紋センサーにさわったことにします。具体的には、「$ adb -e emu finger touch 1234」みたいな感じです。
・再びFingerprint アプリを起動します
・PURCHASEボタンを押すと下図のようなダイアログが表示されるので、指紋センサーに触ります。エミュレータの場合は「$ adb -e emu finger touch 1234」みたいな感じです。
 ・ダイアログはcom.example.android.fingerprintdialog.FingerprintAuthenticationDialogFragmentです
 ・com.example.android.fingerprintdialog.FingerprintUiHelperで指紋認証のコールバックを受け取っています
 ・指紋認証の成功コールバックは、com.example.android.fingerprintdialog.FingerprintUiHelper#onAuthenticationSucceededです。
 ・com.example.android.fingerprintdialog.FingerprintAuthenticationDialogFragment#onAuthenticated -> com.example.android.fingerprintdialog.MainActivity#onPurchased -> com.example.android.fingerprintdialog.MainActivity#tryEncryptcom.example.android.fingerprintdialog.MainActivity#tryEncrypt へと連鎖します。私のエミュレータでは、この最後のtryEncryptメソッドで例外発生のルートへ突入します。そもそもこのタイミングでjavax.crypto.Cipher#doFinal(byte[])をしている理由がいまいちよくわからないです。
・指紋認証が使えないときの代替手段として、パスワード認証が用意されています。こちらはサンプルですので何を入力しても成功となっています。
・指紋認証関係のAPIをメモしておきます。
 ・android.app.Activity#requestPermissions : 指紋認証のパーミッションを要求します
 ・android.app.Activity#onRequestPermissionsResult: 指紋認証のパーミッションの結果がコールバックされます
 ・android.hardware.fingerprint.FingerprintManager#isHardwareDetected : 指紋認証のセンサーがあるかどうか
 ・android.hardware.fingerprint.FingerprintManager#hasEnrolledFingerprints : 指紋が登録されているかどうか
 ・android.hardware.fingerprint.FingerprintManager#authenticate(FingerprintManager.CryptoObject crypto, CancellationSignal cancel, FingerprintManager.AuthenticationCallback callback, int flags) で指紋センサーからの入力をコールバックしてもらうオブジェクトを登録しておきます
・FingerprintManager.AuthenticationCallbackはabstractクラスで4つのコールバックがあります。指紋認証に成功したときのコールバックは、onAuthenticationSucceededです。