sholler's プログラムとメモ帳

日々プログラムをしていて出てきたエラーの処理、技術関連の気になった記事などを題材に書いていくブログ。

【Android】電卓を作る1

Androidで電卓を作る課題が出たので、
自分がやった事、学んだ事をメモ。

まずは電卓内で起こる「イベント」「状態」「動作」をまとめる。

[イベント]
・0〜9ボタンが押される
・四則演算ボタンが押される
・=ボタンが押される
・Cボタンが押される
・ACボタンが押される

[状態]
・数値A入力中
演算子選択
・数値B入力中
・結果表示

[動作]
・入力数値をAに反映
・数値Aを保持
・数値Aクリア
・入力数値をBに反映
・数値Bを保持
・数値Bクリア
・数値AとBクリア
・演算結果を保持
・ディスプレイ出力
・四則演算を保持
・ステータスチェック


必要な変数を定義

//変数定義
private String inputStr = "";  //入力値
private String aNum = "";      //数値A
private String bNum = "";      //数値B
private String op = "";        //演算子
private String result = "";    //演算結果
private int state = 0;         //ステータス (0:A入力中  1:演算子選択  2:B入力中  3:結果表示)


buttonMethod用意。
処理は全てbuttonMethodに記述していく

public void buttonMethod(View b) {
	//ココに処理を書いていく
}


起きるイベントと電卓の状態で管理する様に作っていくと
拡張性があって良いらしい。

次回に電卓のソースを記述するので、先にレイアウトだけ。

activity_main.xml内容

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#777"
    android:orientation="vertical"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

    <TextView
        android:id="@+id/answerView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:background="#fff"
        android:gravity="center_vertical|right"
        android:padding="5dp"
        android:textSize="30sp" />

    <TableLayout
        android:layout_width="match_parent"
        android:layout_height="337dp"
        android:layout_weight="1" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button19"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="C" />

            <Button
                android:id="@+id/Button03"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="BS" />

            <Button
                android:id="@+id/Button02"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="+/-" />

            <Button
                android:id="@+id/Button01"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="AC" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button07"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="7" />

            <Button
                android:id="@+id/Button06"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="8" />

            <Button
                android:id="@+id/Button05"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="9" />

            <Button
                android:id="@+id/Button04"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="÷" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button11"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="4" />

            <Button
                android:id="@+id/Button10"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="5" />

            <Button
                android:id="@+id/Button09"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="6" />

            <Button
                android:id="@+id/Button08"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="×" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button15"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="1" />

            <Button
                android:id="@+id/Button14"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="2" />

            <Button
                android:id="@+id/Button13"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="3" />

            <Button
                android:id="@+id/Button12"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="-" />

        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1" >

            <Button
                android:id="@+id/Button18"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="0" />

            <Button
                android:id="@+id/Button17"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="00" />

            <Button
                android:id="@+id/Button16"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="=" />

            <Button
                android:id="@+id/button1"
                android:layout_width="0dp"
                android:layout_height="match_parent"
                android:layout_weight="1"
                android:onClick="buttonMethod"
                android:text="+" />

        </TableRow>

    </TableLayout>

</LinearLayout>

今はデザインは気にせず、簡単に。
後でデザインもこっていきたいな

Raspberry pi Set Up(失敗ログ) 2013-12-22

[Command 01]
必要なものインストール祭り

$ sudo apt-get install dnsmasq ruby build-essential openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev ncurses-dev automake libtool bison subversion

[Command 02]
dnsmasqの設定ファイル編集

$ sudo vi /etc/dnsmasq.conf

内容

# Add domains which you want to force to an IP address here.
# The example below send any host in double-click.net to a local
# web-server.
#address=/double-click.net/127.0.0.1
address=/guzzoni.apple.com/10.0.1.46  #編集箇所

[Command 03]
dnsmasq再起動

$ sudo /etc/init.d/dnsmasq restart

[Command 04]

$ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

[Command 05]

$ source /etc/profile.d/rvm.sh

[Command 06]

$ [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm”

[Command 07]

$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" #Load RVM function' >> ~/.bash_profile

できなかった。

[Command 08]
Ruby インストール

$ rvm install 1.9.3

[Command 09]
デフォルトに設定

$ rvm use 1.9.3 --default

[Command 10]
SiriProxyをgithubからコピー

$ git clone git://github.com/plamoni/SiriProxy.git

[Command 11]

$ cd Siriproxy

[Command 12]

$ rake install

できなかった。

解決策

$ sudo chown -R pi ~/SiriProxy

結果

siriproxy 0.5.4 built to pkg/siriproxy-0.5.4.gem.
rake aborted!
Couldn't install gem, run `gem install /home/pi/SiriProxy/pkg/siriproxy-0.5.4.gem' for more detailed output

言われた通りコマンド実行

$ gem install /home/pi/SiriProxy/pkg/siriproxy-0.5.4.gem

結果

gem
Fetching: CFPropertyList-2.1.2.gem (100%)
Successfully installed CFPropertyList-2.1.2
Fetching: eventmachine-1.0.3.gem (100%)
Building native extensions.  This could take a while...
Successfully installed eventmachine-1.0.3
Fetching: uuidtools-2.1.4.gem (100%)
Successfully installed uuidtools-2.1.4
Fetching: geocoder-1.1.9.gem (100%)
Successfully installed geocoder-1.1.9
Fetching: cora-0.0.4.gem (100%)
Successfully installed cora-0.0.4
Fetching: rainbow-1.1.4.gem (100%)
Successfully installed rainbow-1.1.4
Fetching: rexec-1.5.2.gem (100%)
Successfully installed rexec-1.5.2
Fetching: rubydns-0.6.7.gem (100%)
Successfully installed rubydns-0.6.7
Successfully installed siriproxy-0.5.4
Installing ri documentation for CFPropertyList-2.1.2
Installing ri documentation for cora-0.0.4
Installing ri documentation for eventmachine-1.0.3
Installing ri documentation for geocoder-1.1.9
Installing ri documentation for rainbow-1.1.4
Installing ri documentation for rexec-1.5.2
Installing ri documentation for rubydns-0.6.7
Installing ri documentation for siriproxy-0.5.4
Installing ri documentation for uuidtools-2.1.4
9 gems installed

再び

$ rake install

結果

siriproxy 0.5.4 built to pkg/siriproxy-0.5.4.gem.
siriproxy (0.5.4) installed.

[Command 13]
隠しディレクトリ作成

$ mkdir ~/.siriproxy

[Command 14]
設定ファイルコピー

$ cp ./config.example.yml ~/.siriproxy/config.yml

[Command 15]
ca.pemファイル発行

$ siriproxy gencerts

[Command 16]
コンソール画面をもう一つ新規で出して、ca.pemファイルをローカルにコピー

$ scp -r pi@10.0.1.46:~/.siriproxy/ca.pem /Users/user/Desktop/

[Command 17]
RaspberryPiに戻り

$ siriproxy bundle

[Command 18]
サーバー起動

$ rvmsudo siriproxy server

結果(一部抜粋)

'/root/.siriproxy/config.yml' not found.

rvmsudoで実行している為、rootのホームディレクトリを見てしまう。

解決策
.siriproxyをrootのホームディレクトリにコピー

$ sudo cp -r ~/.siriproxy /root/

再起動

$ rvmsudo siriproxy server


ここまでやったけど、結果失敗に終わりました。

Raspberry pi Ruby インストールまで

必要なパッケージインストール

sudo apt-get install curl gcc make git zliblg-dev g++ libssl-dev

RVMダウンロード

bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)

結果一部抜粋
(※piの部分はユーザ名の為、違う人もいる。piはデフォルト)

Installation of RVM in /home/pi/.rvm/ is almost complete:
	* To start using RVM you need to run 
	'source /home/pi/.rvm/scripts/rvm'
 in all your open shell windows, in rare cases you need to reopen all shell windows.

メッセージ通りにコマンド実行

$ source /home/pi/.rvm/scripts/rvm

パス確認

$ which rvm

結果

/home/pi/.rvm/bin/rvm

Rubyインストール

$ rvm install 1.9.3

パス確認

$ which ruby

結果

/home/pi/.rvm/rubies/ruby-1.9.3-p484/bin/ruby

バージョン確認

$ ruby -v

結果

ruby 1.9.3p484 (2013-11-22 revision 43786)[armv6l-linux-eabi]

Raspberry Piをインターネットに繋げる。

普通はshhで繋げる為、staticなaddressを設定するらしいけど、
自分はDNSなどの設定がうまくいかないので、DHCPで設定。
(RaspberryPiだとDHCPがデフォルトみたいだった)


ネットワーク設定ファイルを編集

pi@raspberrypi ~ $ sudo vi /etc/network/interface


編集

auto lo
 
iface lo inet loopback
iface eth0 inet dhcp
 
allow-hotplug wlan0
iface wlan0 inet manual
wpa-roam /etc/wpa_supplicant/wpa_supplicant.conf
iface default inet dhcp


保存したらおk。


再起動。

pi@raspberrypi ~ $ sudo reboot


一応これでインターネットには繋がってて、
sshで接続する為にip address調べなきゃならなくて、
nmapコマンドを使った。

nmap -sT 10.0.1.*  (←自分のipが 10.0.1.xxx の場合の例)


結果(一部抜粋)

PORT   STATE SERVICE
22/tcp open  ssh


と書いてあるとこがおそらくRaspberryPiのip
後はssh接続。

sudo ssh pi@10.0.1.xxx

始まり

二十歳になったので何か新しい事を始めようと

ブログを設立しました。 

 

書いていく事としては、

趣味がプログラムだったりするので、そのメモとかかな(適当だけど)

以外とエラー処理に、人のメモは役にたったりするので^^

 

今は、Siriproxyに興味があるんでそれを動かすまでにした事とかを、

メモ程度に書いていこうと思う。

案外Rubyのインストールでこけたりしまくりなんで(笑)

 

PCはMacメインなんでMacユーザー向けになるかも知れないけど

WinとかLinuxもたまに書きたい

 

もちろんそれ以外の事も書こうと思う。

あと、勉強の為に、英語も書くかも

よろしくお願いします