コンピュータ

...now browsing by category

 

Raspberry Pi 2 のセットアップ

土曜日, 3月 26th, 2016

今更ながら Raspberry Pi 2 を入手しました。

IMG_3201

あちこちのサイトを見ながらセットアップしたのですが、自分向けにまとめてみました。
インストールするOSはRasbian、使用するPCはMac、主な用途はヘッドレスサーバです。

揃えたもの

  • Raspberry Pi 2 B 本体
  • SDカード 16GB
  • ケース
  • Wi-Fiモジュール(GW-USNANO2A)
  • USB ACアタダプタ
  • micro USB ケーブル
  • キーボード
  • マウス
  • HDMI接続のディスプレイ
  • HDIケーブル
  • イーサネットケーブル

ダウンロード

(1) 以下のサイトから Rasbian OS のイメージファイルをダウンロードします。
今回は通常版(LITEでない方)を使いました。

Raspbian

ダウンロードは1時間くらいかかります。

(2) ダウンロードしたらZipファイルを解凍して、imgファイルにしておきます。

SDカードへのインストール

(1) ターミナルアプリを起動します。

(2) ファイルシステム上にあるデバイス名を調べます。
$ df
Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
/dev/disk1 974700800 598106040 376082760 62% 74827253 47010345 61% /
devfs 380 380 0 100% 660 0 100% /dev
map -hosts 0 0 0 100% 0 0 100% /net
map auto_home 0 0 0 100% 0 0 100% /home

(3) SDカードをMacに接続します。

(4) SDカードのデバイス名を調べます。
$ df
Filesystem 512-blocks Used Available Capacity iused ifree %iused Mounted on
/dev/disk1 974700800 598106040 376082760 62% 74827253 47010345 61% /
devfs 380 380 0 100% 660 0 100% /dev
map -hosts 0 0 0 100% 0 0 100% /net
map auto_home 0 0 0 100% 0 0 100% /home
/dev/disk3s1 122768 40800 81968 34% 512 0 100% /Volumes/NO NAME

SDカードをさしこむ前と比較すると、一番下の行が増えてます。これがSDカードです。
ファイルシステム上のデバイス名が「/dev/disk3s1」で、マウントされているボリューム名が「/Volumes/NO NAME」す。

(5) SDカードをアンマウントします。
$ sudo diskutil umount /Volumes/NO\ NAME
Password:
Volume NO NAME on disk3s1 unmounted

(6) SDカードにイメージファイルの内容を書き込みます。
$ sudo dd bs=1m if=./2016-03-18-raspbian-jessie.img of="/dev/rdisk3"
3847+0 records in
3847+0 records out
4033871872 bytes transferred in 267.713917 secs (15067845 bytes/sec)

「of」に書き込み先のデバイス名を指定するのですが、元の名前の先頭に「r」を追加して、末尾の「s1」を削除したものになります。
5分くらいかかります。

起動

(1) SDカードを Raspberry Pi に差し込みます。

(2) キーボード、マウス、ディスプレイをつなげます。

(3) 有線でインターネットに接続します。

(3) 最後に電源をつなげると、Raspberry Pi が起動します。

キーボードの設定

(1) デスクトップ画面から Menu -> Preference -> Raspberry Pi Configuration で設定画面を開きます。

(2) Localisationタブを選び、「Set Keyboard…」ボタンを押して、Keyboard Layout画面を開きます。

(3) 「HHKB Lite 2」の場合は、「Japan – Japanese」を選んで、OKボタンを押します。

OSのアップデート

(1) 「Terminal」を起動します。

(2) OSを最新の状態にします。
$ sudo apt-get update && sudo apt-get -y dist-upgrade

(3) ファームウェアを最新の状態にします。
$ sudo rpi-update

(4) 再起動します。
$ sudo reboot

raspi-config の設定

(1) raspi-config を起動します。
$ sudo raspi-config

スクリーンショット 2016-03-26 12.48.03

(2) ファイルシステムの拡張を行います。
1 Expand Filesystem
選択すると実行されます。これを行わないと/rootがすぐに満杯になります。

(3) パスワードの変更
2 Change User Password
「pi」アカウントのパスワードを変更します。

(4) 起動オプション
3 Boot Options -> Console
CUIで起動するように変更します。

(5) 言語を設定します。
5 Internationalisation Options -> I1 Change Locale
[ ] en_GB.UTF-8 UTF-8
[*] en_US.UTF-8 UTF-8
[*] ja_JP.UTF-8 UTF-8

([Ctrl]+v で1ページ送り)

Default locale for the system environment:
ja_JP.UTF-8

(6) タイムゾーンを設定します。
5 Internationalisation Options -> I2 Change Timezone
Asia / Tokyo

(7) raspi-config終了時に再起動するか聞いてくるので、再起動します。

sshでの接続

(1) Raspberry Pi の IPアドレスを調べます。
Raspberry Pi上でifconfigコマンドを使うか、DHCPサーバから使用されているIPアドレスを見るとか。

(2) Macからsshで接続します。
$ ssh pi@192.168.xx.xx

初期パスワードは「raspberry」です。

(3) この時、以下のようなエラーが出た場合は、Macに前のホストの公開鍵が残っているのが原因なので、ssh-keygenを使って削除してから、再試行します。
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@ WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED! @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that a host key has just been changed.
The fingerprint for the ECDSA key sent by the remote host is
SHA256:8GbcugJQ9+jorhBq5tdIuG3nMriSy7zxxxxxxxxxxxx.
Please contact your system administrator.
Add correct host key in /Users/xxxxx/.ssh/known_hosts to get rid of this message.
Offending ECDSA key in /Users/xxxxx/.ssh/known_hosts:3
ECDSA host key for 192.168.xx.xx has changed and you have requested strict checking.
Host key verification failed.

ssh-keygen -R 192.168.xx.xx
# Host 192.168.xx.xx found: line 3
/Users/xxxxx/.ssh/known_hosts updated.
Original contents retained as /Users/xxxxx/.ssh/known_hosts.old

Wi-Fi の設定

(1) 無線LANモジュールを接続します。

(2) 無線LANモジュールが認識されていることを確認します。
$ lsusb
...
Bus 001 Device 004: ID 2019:ab2a PLANEX GW-USNano2 802.11n Wireless Adapter [Realtek RTL8188CUS]
...

(3) Wi-Fiの設定を書き込みます。
$ sudo sh -c 'wpa_passphrase SSID PASSPHRASE >> /etc/wpa_supplicant/wpa_supplicant.conf'

「SSID」にはWi-FiのSSID、「PASSPHRASE」には同じくパスフレーズ(パスワード)を入力します。

(4) コメントのパスフレーズを削除
$ sudo vi /etc/wpa_supplicant/wpa_supplicant.conf

先ほど入力したパスフレーズがコメントアウトされて書き込まれている(#psk=の行)ので削除しておきます。

(5) ネットワーク機能を再起動します。
$ sudo /etc/init.d/networking restart

(6) IPアドレスが取得できたことを確認します。
$ ifconfig
...
wlan0 Link encap:Ethernet HWaddr 00:22:cf:xx:xx:xx
inet addr:192.168.xx.yy Bcast:192.168.xx.255 Mask:255.255.255.0
...

(7) IPアドレスを固定にします。
$ sudo vim /etc/network/interfaces
...
allow-hotplug wlan0
iface wlan0 inet static
address 192.168.xx.zzz
netmask 255.255.255.0
gateway 192.168.xx.1
wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf
...

(8) 再起動します。
$ sudo reboot

(9) 無線LANのIPアドレスが指定したものになっていることを確認します。
$ ifconfig
...
wlan0 Link encap:Ethernet HWaddr 00:22:cf:xx:xx:xx
inet addr:192.168.xx.zzz Bcast:192.168.xx.255 Mask:255.255.255.0
...

Xcode 7.1 Build Phase でドラッグ&ドロップが効かない

金曜日, 11月 13th, 2015

Xcode 7.1 にて Build Phase に項目を追加し、適切な位置にドラッグ&ドロップで移動しようとしたのですが、任意の位置に入らず戻ってしまいます。

スクリーンショット 2015-11-13 8.55.54

何度試しても上手くいかず、Xcodeを再起動しても、Macを再起動してもダメ。

同じような問題が起きている人はいないかと探してみたら、いました。

XcodeのRun Scriptのドラッグ&ドロップ移動のしにくさを緩和する

途中に入れるのは出来ませんでしたが、一番下に移動するのはできたので、入れたい位置の下から全ての項目を、一個ずつ最下行に移動することで、対処しました。

この人の場合は「非常に効きにくい」と言っていますが、私の場合、何度やっても成功しませんでした。

なんかコツとかあるんでしょうか?

UniversalアプリでUITableViewのセルの背景がiPadだと透明にならない

木曜日, 10月 1st, 2015

Universalアプリにて、storyboardでUITableViewのセルの背景を透明に設定したのですが、iPhoneだと透明になるのに、iPadだと透明にならず白いままでした。

調べてみたところ、コードにて背景色に透明を設定することで、iPadでも透明になることがわかりました。

let cell = tableView.dequeueReusableCellWithIdentifier("contentCell", forIndexPath: indexPath)

// セルの背景を透明に設定
cell.backgroundColor = UIColor.clearColor()

return cell

iOS7かららしいですが、バグなのか仕様なのか?

【参考】
UITableView / UITableViewCell challenge with transparent background on iPad with iOS7

XcodeでSwiftのリファクタリングができないから、renameの代わりにターミナルのコマンドで一括置換してみた

木曜日, 9月 17th, 2015

Xcode7の正式版が出たというのに、未だにSwiftのリファクタリング機能が実装されていません。特にrenameはよく使うので困りものです。さすがにちまちま置換していくのはとても面倒なので、ターミナルのコマンドで一括置換しました。

find -E . -regex ".*\.(swift|storyboard)" | xargs perl -i -pe 's/\bOldName\b/NewName/g'

これでカレントディレクトリ配下の *.swift ファイルおよび *.storyboard ファイル内の、「OldName」を「NewName」に置換できます。

「.*\.(swift|storyboard)」の部分で対象とするファイルを指定しています。「-E」「-regex」 は拡張正規表現を使うためのオプションで、両方必要なようです。

「OldName」の前後の「\b」は単語の区切りを示します。これがあると「HogeOldName」や「OldNameMoge」などは対象外になります。

最初、perlの代わりにsedを使おうと思っていたのですが、正規表現で「\b」が使えなかったり、バックアップファイルが残ってしまったりするので、perlに変えました。

Frameworkのテストコードで”Library not loaded: @rpath/libswiftCoreAudio.dylib”

木曜日, 8月 27th, 2015

iOSでFrameworkのテストコードを書いたのですが、テストを実行しようとすると、ビルドは成功するのですが、テスト実行時に以下のエラーが出てテストが行われませんでした。

2015-08-27 08:35:04.489 xctest[1176:62057] The test bundle at /Users/who/Library/Developer/Xcode/DerivedData/XxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest could not be loaded because an unanticipated error occurred.
2015-08-27 08:35:04.490 xctest[1176:62057] Detailed error information: Error Domain=NSCocoaErrorDomain Code=3587 “The bundle “xxxTests” couldn’t be loaded because it is damaged or missing necessary resources.” (dlopen_preflight(/Users/who/Library/Developer/Xcode/DerivedData/XxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest/xxxTests): Library not loaded: @rpath/libswiftCoreAudio.dylib
Referenced from: /Users/who/Library/Developer/Xcode/DerivedData/XxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxx.framework/xxx
Reason: image not found) UserInfo=0x7f9a39422c50 {NSLocalizedFailureReason=The bundle is damaged or missing necessary resources., NSLocalizedRecoverySuggestion=Try reinstalling the bundle., NSFilePath=/Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest/xxxTests, NSDebugDescription=dlopen_preflight(/Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest/xxxTests): Library not loaded: @rpath/libswiftCoreAudio.dylib
Referenced from: /Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxx.framework/xxx
Reason: image not found, NSBundlePath=/Users/who/Library/Developer/Xcode/DerivedData/XxxxFramework-gpqmrnxhtltpenbdidiiwacuonzd/Build/Products/Debug-iphonesimulator/xxxTests.xctest, NSLocalizedDescription=The bundle “xxxTests” couldn’t be loaded because it is damaged or missing necessary resources.}

*** Test session exited(1) without checking in. If you believe this error represents a bug, please attach the log file at /var/folders/nt/x60j01ps3r3dds9ldvppd5xm0000gn/T/com.apple.dt.XCTest-status/Session-2015-08-27_08:35:01-Z4Gt8p.log

  • framework内でAVFoundationにリンクしています。
  • CoreLocationを使用しています。
  • サンプルアプリに組み込んだframeworkは正常に動作していました。

cleanしたり、再起動したりしたのですが、改善しませんでした。色々試した結果、以下のBuild Settingsを変更したところ、正常に動くようになりました。

Embedded Content Contains Swift Code = NO -> YES

ですが、これで動くようになった理由がわかりません。
新しくframeworkを作るとデフォルトでNOになっているのは確認しました。