2014年12月20日 星期六

use cdrom in docker container.



  1. make sure you never mount your cdrom.
  2. run you container using the command like ``` docker run -i -t -v /dev/cdrom:/dev/cdrom dockerfiles/centos-lamp /bin/bash```
  3. type ```mkdir /mnt/cdrom``` in your container bash.
  4. type ```mount -t iso9660 -o ro /dev/cdrom /mnt/cdrom``` in your container bash.
  5. see you cdrom using ```ls /mnt/cdrom``` in your container bash.

2014年12月9日 星期二

read .bashrc via ssh.

如果你透過 ssh 登入 linux 主機,預設是不會讀取 .bashrc 。如果你希望登入 ssh 後,自動 source .bashrc ,你需要加入下面的內容到 .bash_profile

if [ -f ~/.bashrc ]; then
  . ~/.bashrc
fi


ref:
http://stackoverflow.com/questions/820517/bashrc-at-ssh-login

2014年11月17日 星期一

在 linux 下,如何顯示按鍵所對應的碼(scan code or key code 等)

當你想知道按鍵的 scan code 或 key code,
可以使用 xev 這隻程式

他可以告訴你鍵盤或滑鼠事件所對應的一些資訊,如  scan code 或 key code

2014年11月14日 星期五

論生活中常見成本概念

這篇文章探討生活中的常見成本,讓你在思考成本時,有個方向。以下的名詞並非所有都是經濟學術語。成本不一定是金錢。



  1. 擁有成本:這是指當你從一個狀態轉變到另一個狀態(注:後者狀態的價值比前者狀態還高),所付出的成本。例如你從沒有車到有一台車,購買車的成本。或是從有第二台車到有第三台車的購車成本。
  2. 維持成本:維持狀態,使其狀態的價值避免嚴重減損。例如保養車的成本,避免車損壞。
  3. 機會成本:當你做一個選擇而需要放棄其他選擇,則其他選擇的最高價值者,為機會成本。在思考時間相關的問題時,應注意機會成本的問題。
  4. 沉沒成本:當你做了某件事,然後發現做下去沒意義(或價值)。但是因為過去花費的成本,又讓你繼續做沒意義(或低價值或非完全理性)的事。則過去花費的成本則為沉沒成本。例如你不小心買了一張很難看的電影票,你覺得去看只是又浪費時間。但最後又去看,而浪費了時間。該張電影票則為沉沒成本。


了解了上述成本的意義,則以情境來描述上面成本概念。

1. 情境一:
當你花錢買書,則買書錢為擁有成本(你必須付出錢,才能擁有該本書)。當你買了很多書,因而需要額外租空間來放書,則租額外空間的錢則是書的維持成本(為了讓你繼續擁有書,你需要付出額外的錢,讓你維持擁有)。當你不想付出額外租空間的成本時,而選擇丟棄一本書來放新進的書,則被丟棄的書的價值為你決定保留新進的書的機會成本。當你買一本新書,而決定租額外空間放書,而不丟棄最低價值的書,而對低價值的書已經對你沒意義。則該本最低價值的書的價值為你的沉沒成本(你為了一本沒意義的書,而租額外空間)。

1. 情境二:
當你打算寫一個網站,你在思考要學習組合語言或是 python 語言。最後你選擇了組合語言,此時 python 語言就是你的機會成本(你損失 python 語言所帶來的好處)。你花了很多時間,終於學會怎麼用組合語言寫網站,那些時間則是你的擁有成本。當你學會了用組合語言寫網站,你必須花時間去進修你的能力,或確保你不會忘記技能。此時你的付出為維護成本。後來發現用 python 寫網站比較好,但是你卻不換成更好的工具。此時過去在組合語言的付出為沉沒成本,他讓繼續沉沒下去。

2014年11月11日 星期二

run play 2.3 in cloud9

For play 2.3 or above, run the below command in terminal:

 activator run -Dhttp.address=$IP -Dhttp.port=$PORT run

2014年11月9日 星期日

scala shell(console) with vim key bind.



If you want to use scala shell with vim key bind,

you can press `esc + enter`.

Now you enter to vim mode.




When you enter vim mode, you will in Insert mode.

In Insert mode, it will display anything you type.




If you want to switch to Normal mode, just type `esc` or `ctrl + [`.

It's similar as vim.

Now you can move your cursor using `h`,`l`.

Or you can delete line using `dd`, etc.


Enjoy it.


How does it work?
https://github.com/jline/jline2/wiki/JLine-2.x-Wiki

2014年11月8日 星期六

How to get the play version for scala.

type below in your terminal

activator console

or

play console


In scala console,

type

play.core.PlayVersion.current

It will display the play version you use.

2014年11月1日 星期六

用 scala akka 寫一個簡單的 grep 程式

akka 是一個 actor model 。因此他的思維和一般寫程式的思維很不一樣。akka 可以讓我們很容易寫出 concurrent 的程式。我用 akka  寫了兩個 grep 程式,用不一樣的方式。

我會介紹的兩個程式碼在此


  是推薦寫法。 akka 鼓勵你大量建立 actor 。在這隻程式裡,每個資料夾或檔案都會對應到一個 actor ,一個 actor 只會負責一個檔案或目錄。



  是一個特別的寫法。這隻程式會重複利用 FolderExplorer (這個例子中,最多會有15 個 actor )和 FileReader (這個例子中,最多會有25 個 actor )。這個例子是為了示範 RoundRobinRouter 的使用方法。

scala 中,將 list 根據條件分成兩個,一個是滿足條件的 list,一個是不滿足條件的 list。

Scala 中, list 有許多內建好用的函數。partition 是其中一個。

來看個簡單的例子

List(1, 2, 3, 1, 2).partition(_ == 2)

換產生

res0: (List[Int], List[Int]) = (List(2, 2),List(1, 3, 1))

第一個 tuple 的元素是滿足條件的 list,第二個 tuple 的元素是不滿足條件的 list。

有了 partition ,就可以很容易作到一些事情,如建立字元出現頻率的對照表


  def makeMap(data: List[Char], output: Map[Char, Int]): Map[Char, Int] = data match {
    case x :: xs => {
      val (expected, rest) = data.partition(_ == x)
      makeMap(rest, output + (x -> expected.length))
    }   
    case Nil => output
  }


2014年8月2日 星期六

configure ssd for linux


add noatime,nodiratime,discard for your ssd device in /etc/fstab.

Like below
noatime,nodiratime,discard,errors=remount-ro


add below settings in /etc/fstab

tmpfs   /tmp       tmpfs   defaults,noatime,mode=1777   0  0

tmpfs   /var/spool tmpfs   defaults,noatime,mode=1777   0  0

tmpfs   /var/tmp   tmpfs   defaults,noatime,mode=1777   0  0

tmpfs   /var/log   tmpfs   defaults,noatime,mode=0755   0  0


In /etc/default/grub,
add "elevator=noop" for GRUB_CMDLINE_LINUX_DEFAULT

2014年7月26日 星期六

linux 下 chrome 界面亂碼解決方法

修改 /etc/fonts/conf.d/49-sansserif.conf倒數第四行,改成下面這樣或是換成其他你喜歡的字型。


ubuntu


參考資料:
http://blog.csdn.net/loveaborn/article/details/29579787


如果上面這個方法無效,試試看下面指令
$ sudo apt-get install ttf-wqy-microhei ttf-wqy-zenhei xfonts-wqy

2014年7月14日 星期一

一些黑底白字的瀏覽器外掛或 pdf reader

chrome:

High Contrast
Night Reading Mode  (推薦)

pdf reader:
evince  (到 view -> inverted color 將白底換成黑底)

2014年7月12日 星期六

How to build x86_64 image for qemu using buildroot

Download buildroot and change into buldroot folder. And then type below command.
make qemu_x86_64_defconfig
make -j8
qemu-system-x86_64 -M pc -kernel output/images/bzImage -drive file=output/images/rootfs.ext2 -append root=/dev/sda -vga std

Enjoy it.

scala 中 for ...yield 與 map, flatMap, withFilter 之互換


-------------------------------
for(x <- expr1) yield expr2
expr1.map(x => expr2)

-------------------------------

for(x <- expr1 if expr2) expr3
expr1.withFilter(x => expr2).map(x => expr3)

-------------------------------

for(x <- expr1 if expr2; seq) yield expr3
for(x <- expr1 withFilter expr2; seq) yield expr3

-------------------------------

for(x <- expr1; y <- expr2; seq) yield expr3
expr1.flatMap(x => for (y <- expr2; seq) yield expr3)

-------------------------------


for ((x1, x2) <- expr1) yield expr2
expr1.map{case(x1, x2) => expr2}

-------------------------------

for(pat <- expr1) yield2

expr1 withFilter {
  case pat = > true
  case _ => false
} map {
  case pat => expr2
}

2014年6月26日 星期四

How to attach to running container?



If you want to attach to running container, it's possible. First, use `sudo docker ps--no-trunc` to find your container full id. And use `sudo lxc-attach --name your_id` to attach. The way seems to be dangerous. Look before you leap.

2014年6月22日 星期日

introduce to call_seq.

    A tool to help you trace code efficiently. call_seq record the program how to run and save the result to file with json format.


Quick start to try it.
                $ pip install pyside pyqode.core pyqode.python
                $ git clone https://github.com/ya790206/call_seq
                $ python demo.py
                $ python -m call_seq.browser output.json        

How to record the program how to run.
    if __name__ == '__main__':
        with CallSeq(name='output.json'):
            test() #  the code you want to trace

After the file `output.json` created, then you run the below command.
python -m call_seq.browser output.json
Now you can trace code using call_seq.browser. Enjoy it.





2014年5月4日 星期日

scala 檔案目錄操作

如果你要對目錄下作一下操作,如列出所有的檔案之類的。可以考慮用 reflect.io 這個 package。簡單的用法如下。

import reflect.io._

Directory(".").list


iscala

iscala ,一個類似 ipython 的東西,不過他是給 scala 用的。用這個 shell 最大的好處是,你可以按 tab 健來觀察該物件有哪些屬性或方法。

iscala 的官方網站在這。https://github.com/mattpap/IScala。比較簡單的安裝方法就到這個頁面下載 iscala ,然後用下面 bash 執行。(注意:你可能要修改你的 JAR_PATH 路徑)

#!/bin/bash
JAR_PATH="$(dirname $(dirname $(readlink -f $0)))/lib/IScala.jar"
KERNEL_CMD="[\"java\", \"-jar\", \"$JAR_PATH\", \"--profile\", \"{connection_file}\", \"--   parent\", \"$@\"]"
ipython console --profile scala --KernelManager.kernel_cmd="$KERNEL_CMD" --no-banner

2014年5月3日 星期六

在 sbt 下使用 scala 內建的 actor



在 build.sbt 裡新增
libraryDependencies +=
      "org.scala-lang" % "scala-actors" % "2.10.0"

scala-actors 是 library name
2.10.0 是版本號

他有提供什麼 library 以及版本可從下面列表去找

scala 的 library 列表
https://oss.sonatype.org/content/groups/public/org/scala-lang/


2014年4月6日 星期日

scala 中對於不接受參數的函數定義方式與使用方法

    scala 對於不接受參數的函數定義方式有兩種。如下面程式碼所示。

def test1 = { // 1. without ()
  println("SS")
}

def test2() = { //2. with ()
  println("GG")
}


test1()

    第一種寫法,沒有 () 。使用時,不可以加上 () 否則會出錯。因此呼叫時只有一種寫法,test1。

    第二種寫法,有 () 。使用時,可以加上 (),也可以不要加上 () 。因此呼叫時有兩種寫法, test2 或 test2()。

scala 語言特性優缺點分析

這篇文章闡述哪些 scala 特性"我認為"是好的,哪些是不好的。因為是"我認為",所以這是一篇充滿主觀意見的文章。


優點:

  1. type inference: 這個特性可以減少程式設計師輸入重複的資訊。
  2. lazy keyword: 可以做到延遲計算的功能 
  3. currying: 讓人更容易產生 partial function。
  4. 對於接受參數型別為 => Unit(Int等),要傳參數時,可以簡單的用 { } 代替。詳見範例 1
  5. actor: 多執行緒的函式庫,用 message queue share memory 的問題(當然,還是要看你的寫法。沒寫好還是有 share memory 的問題)



範例1
def repeat(times: Int) (func: => Unit) {
  for (n <- 1 to times) func
}

repeat (5) {
  println("SSS")
}


缺點:

  1. implicit conversion: 這會影響可讀性。因為你在閱讀時,無法立刻知道他在執行時,是以何種物件在執行。不過這個特性應該是為了能夠和 jvm 整合所產生的特性。
  2. 探討 scala 的函數呼叫與自動推論型別所產生的模糊情形


結論: scala 是一個很好的語言。不過他是 function programming language 的關係,可能不會紅起來。但是下一個如果能夠吸收 scala 的優點,改進 scala 的缺點,我猜能夠成為下一個主流語言。

2014年4月5日 星期六

scala play framework 使用方式





要建立專案,輸入

$ play new myFirstApp



進入 play 的 shell (用來輸入指令的)
$ cd myFirstApp
$ play shell


如果要啟用 dev server 在 play shell 輸入

run

或是

$ play run


進入 play 的 console (用來輸入程式碼的)

$ play debug console

如果要使用 stand-alone server

$ play start

你也可以把你的程式打包起來,複製到其他地方執行

$ play dist

他會產生一個 zip 檔,包含所有執行時必要的 jar。



其他可能有用指令

因為 neo4j 只支援 openjdk-7 ,所以用下面指令檢查 java version。不然 play start 未必能正常運作。

$javac -version
$java -version


2014年3月17日 星期一

2014年3月16日 星期日

chroot: failed to run command : No such file or directory

   If you use chroot and it said "chroot: failed to run command  : No such file or directory",  how to slove it? Ok, follow me.


   For example, if you type "chroot /tmp/root /bin/bash" and get error. First of all, check whether the file "/tmp/root/bin/bash" exists. If not, create it.

   But if the file exists and got "No such file or directory", use "file /tmp/root /bin/bash" to check the file. If the file is "statically linked", I don't know what happen to your system. If the file is "dynamically linked (uses shared libs)", you have two way to go. The one is that you can compile the file to "statically linked". The another is that copy the shared libs which your program use to the right path(here is /tmp/root/lib).
 
    You can use "ldd to find out all shared libs your prcess use. The output is like below.

    linux-vdso.so.1 =>  (0x00007fff747fe000)
libtinfo.so.5 => /lib/x86_64-linux-gnu/libtinfo.so.5 (0x00007fd6544bc000)
libdl.so.2 => /lib/x86_64-linux-gnu/libdl.so.2 (0x00007fd6542b8000)
libc.so.6 => /lib/x86_64-linux-gnu/libc.so.6 (0x00007fd653eef000)
/lib64/ld-linux-x86-64.so.2 (0x00007fd6546ff000)

2014年3月9日 星期日

golang 的 revel web framework 安裝與使用



要在 linux 下安裝 revel,要先設定環境變數,讓 go get 能夠將檔案下載到你指定的路徑

export GOPATH="/home/user/go"
export PATH="$PATH:$GOPATH/bin"

下載 revel
go get github.com/robfig/revel

編譯 revel

go get github.com/robfig/revel/revel

你可以用這個指令確定是否安裝成功

$ revel help

建立新應用程式

revel new myapp

注意:他是建立在 $GOPATH 的路徑上,而非當前目錄。輸出訊息會告訴你專案建在哪。

執行你的第一個程式

revel run myapp


有關 route 的資訊放在 conf/routes。內容依序為 method, path, function

GET     /                                       App.Index

App.Index 的程式碼大概長這樣。每個 Action 都會去找對應檔名的 view 來當 template。如下面程式碼會找 view 目錄下的 app/index.html 來當 template

package controllers

import "github.com/robfig/revel"

type App struct {
 *revel.Controller
}

func (c App) Index() revel.Result {
 return c.Render()
}

template 大概長這樣,這個 template 從 action 接受兩個參數,一個 name,一個 sum。

Hello, {{ .name }} {{ .sum }}

我們要傳資料到 template ,只要在 c.Render 裡放我們要的參數即可

func (c App) Index(name string, a int, b int) revel.Result {    sum := a + b return c.Render(name, sum)}

get/post method 的參數會被放在 action 相對應的參數裡。如 name=world&a=1&b=2,到 action 的話,變數 name是字串 world ,a 是整數1,b 是 整數2


如果你程式都寫完了,想要 deploy。用 

revel build import/path/to/app /tmp/app

之後你就可以到 /tmp/app ,用 ./run.sh 來跑 server 了。






2014年3月8日 星期六

web load balance

Nginx 要作到 load balance 很簡單,只要幾個設定就好了。以下面的 Nginx 設定檔為例(來源)。


worker_processes 2;
 
error_log /var/log/nginx/error.log;
pid /var/run/nginx.pid;
 
events {
    worker_connections 1024;
    use epoll;
}
 
http {
    charset utf-8;
 
    # Enumerate all the Tornado servers here
    upstream frontends {
        server 127.0.0.1:8000;
        server 127.0.0.1:8001;
        server 127.0.0.1:8002;
        server 127.0.0.1:8003;
    }
 
    include /path/to/nginx.mime.types;
    default_type application/octet-stream;
 
    access_log /var/log/nginx/access.log;
 
    keepalive_timeout 65;
    proxy_read_timeout 200;
    sendfile on;
    tcp_nopush on;
    tcp_nodelay on;
    gzip on;
    gzip_min_length 1000;
    gzip_proxied any;
    gzip_types text/plain text/css text/xml
               application/x-javascript application/xml
               application/atom+xml text/javascript;
 
    # Only retry if there was a communication error, not a timeout
    # on the Tornado server (to avoid propagating "queries of death"
    # to all frontends)
    proxy_next_upstream error;
 
    server {
        listen 80;
 
        location ^~ /static/ {
            root /path/to/app;
            if ($query_string) {
                expires max;
            }
        }
 
        location / {
            proxy_pass_header Server;
            proxy_set_header Host $http_host;
            proxy_redirect off;
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Scheme $scheme;
            proxy_pass http://frontends;
        }
    }
}
只要簡單的設定 upstream frontends(frontends名稱可自訂) 裡的 server ,即可作到 loadbalance。

下面連結有 lighttpd 的 load balance 例子。
http://www.playframework.com/documentation/2.2.x/HTTPServer
設定檔大概就長這樣



server.modules = (
      "mod_access",
      "mod_proxy",
      "mod_accesslog"
)

$HTTP["host"] =~ "www.myapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" =>
        ( ( "host" => "127.0.0.1", "port" => 9000 ) ) )
}

$HTTP["host"] =~ "www.loadbalancedapp.com" {
    proxy.balance = "round-robin" proxy.server = ( "/" => (
          ( "host" => "127.0.0.1", "port" => 9001 ),
          ( "host" => "127.0.0.1", "port" => 9002 ) )
    )
}



apache 的 load balancer 的資料在這
http://httpd.apache.org/docs/2.2/mod/mod_proxy_balancer.html

2014年2月22日 星期六

2014年1月22日 星期三

ubuntu acer notebook brightness not work

    If you use ubuntu with acer notebook, and you fint that you can't change brightness using hotkey. Hot to solve it?

First edit the file, "/etc/default/grub". Add "acpi_osi=Linux acpi_backlight=vendor" to GRUB_CMDLINE_LINUX_DEFAULT. So the setting should look like below.

GRUB_CMDLINE_LINUX_DEFAULT="quiet splash acpi_osi=Linux               acpi_backlight=vendor"

After saving the file, you should type the below command. And reboot.
sudo update-grub
Now you should change brightness using hotkey.


2014年1月20日 星期一

how to compile i3 window manager (i3 wm) 4.6

1.  Install the below packages.

sudo apt-get install -y libxcb1-dev libxcb-keysyms1-dev libxcb-util0-dev libxcb-icccm4-dev libyajl-dev libstartup-notification0-dev libxcb-randr0-dev libev-dev libxcb-xinerama0-dev libpango1.0-dev libxcursor-dev libxcb-keysyms1-dev libpango1.0-dev libxcb-util0-dev libxcb-icccm4-dev libyajl-dev libstartup-notification0-dev libev-dev libxcb-xkb-dev libxcb-cursor-dev libxcb-xinerama0-dev libxkbcommon-x11-dev libxkbcommon-dev

sudo apt-get install -y libxcb1-dev libxcb-keysyms1-dev libxcb-util0-dev libxcb-icccm4-dev libyajl-dev libstartup-notification0-dev libxcb-randr0-dev libev-dev libxcb-xinerama0-dev build-essential freeglut3-dev libfreeimage-dev libgl1-mesa-dev libopenal-dev bpango1.0-dev libsdl-ttf2.0-dev libsndfile-dev libxinerama-dev libxcursor-dev libxcb-cursor-dev


2. download i3-4.8.tar.bz2

3. uncompress i3-4.8.tar.bz2

4. cd i3-4.8

5. make

6. make isntall


Congratulations. You make it. I test the way in ubuntu 14.04 and mint 16.


If you want to compile i3-4.7, you need to compile http://cgit.freedesktop.org/xcb/util-cursor . But I don't know how to compile it.


How to compile i3status

1.  Install the below packages.

sudo apt-get install libpango1.0-dev libxcursor-dev libconfuse-dev libasound2-dev  libiw-dev


3. uncompress

4. cd to folder

5. make

6. make isntall

替 cubietruck 加上 bonding 模組

    如果你是根據 http://blog.blackwhite.tw/2014/01/cubie-truck-sd.html 這篇文章建立 lubuntu 的作業系統的話,你會發現他預設並沒有編譯 bonding 模組。因此如果我們想要使用 bonding 的話,必須自己編譯 kernel。

    編譯 kernel 是一件很麻煩的事。不過有人幫我們簡化了很多步驟了。https://github.com/davidandreoletti/cubietruck 這裡已經有人提供腳本幫我們完成 cross compile。不過你需要注意的是你要使用 Ubuntu 12.04 或是 arm-linux-gnueabihf-gcc 4.6.3 版本。不然可能會編譯失敗。我在 mint 16 (with arm-linux-gnueabihf-gcc 4.8.1 版本會編譯失敗。

    編譯完後,他會在 output 的資料夾產生很多檔案。你需要把 uImage 複製到你 sd 卡裡的 bootfs,覆蓋他。然後在複製 output/lib 裏面的內容到 sd 卡裡的 rootfs 的 lib 資料夾裡。etc 資料夾也一樣。複製完後,記得去改 sd 卡裡的 /etc/network/interface。他有寫好 bonding 的設定,記得去修改相關設定。



2014年1月13日 星期一

2014年1月12日 星期日

Ethernet And Wireless Bonding(在 linux 下合併兩張網卡,變成一張)


為什麼要把兩張網卡合併成一張呢?
理由:

  1. 當一張網卡壞掉時,還有另一張,因此不會引起斷線
  2. 網路速度提昇(因為可以走兩條路)
  3. load balance

下面的例子是如何合併筆電的有線網卡和無線網卡。

先安裝所軟體

sudo apt-get install ifenslave

首先先編輯 /etc/NetworkManager/NetworkManager.conf 這個檔案。改成像下面那樣。你可以簡單的把 managed 設成 false,或是在 unmanaged-devices 設定哪些裝置不要被 NetworkManager 管。基本上要被合併的網卡不能被 NetworkManager 控制。


[main]
plugins=ifupdown,keyfile
dns=dnsmasq
[ifupdown]
managed=false
[keyfile]
unmanaged-devices=mac:your_mac



在 /etc/network/interfaces 新增下面的設定,你需要修改無線網卡怎麼連線以及 bond0 怎麼連線,bond0 就是合併後的新界面

auto bond0
iface bond0 inet dhcp
  bond_mode 0
  bond_primary eth0
  bond_miimon 100
  bond_downdelay 200
  bond_updelay 200
  wpa-ssid ssid
  wpa-ap-scan 1
  wpa-psk password
  wpa-scan-ssid 1
  wpa-iface wlan0                                                                
  dns-nameserver 8.8.8.8                                                          
  slaves eth0 wlan0 



重新啟動網路

sudo /etc/init.d/networking restart


用下面指令檢查設定是否成功

cat /proc/net/bonding/bond0


如果你的電腦變成只能用 ip 連但是不能用網址連
如 ping 8.8.8.8 可以通,但是透過網址不行

則在編輯  /etc/resolv.conf 這個檔案
加上

nameserver 8.8.8.8
nameserver 8.8.4.4


mode 0 和6的選擇:
如果你希望能夠平衡網卡的流量,或是你的網卡無法取得速度
則用 mode 0
否則 mode 6 會是較好的選擇


注意參考連結2的 mode 0 的說明。
 "如果一个连接或者会话的数据包从不同的接口发出的话,中途再经过不同的链路,在客户端很有可能会出现数据包无序到达的问题,而无序到达的数据包需要重新要求被发送,这样网络的吞吐量就会下降"

參考資料:

  1. http://techpatterns.com/forums/about1327.html
  2. http://www.huawei.com/ecommunity/bbs/10155553.html



註:之前範例有些問題,因此今天有作更新。 

2014年1月9日 星期四

在 linux 下讓 sdk 可以正確偵測 g2


  1. 將手機連線方式改成"數據連線"
  2. 開啟 debug 模式
  3. 跑下面指令
  4. sudo ./adb kill-server
    sudo ./adb start-server
    sudo ./adb devices
    這樣 sdk 就可以正確偵測到 g2 手機了

參考連結

2014年1月6日 星期一

Arduino LCD KeyPad Shield 的測試程式碼

Arduino LCD KeyPad Shield 的測試程式碼

//Sample using LiquidCrystal library
#include <LiquidCrystal.h>
 
/*******************************************************
 
This program will test the LCD panel and the buttons
Mark Bramwell, July 2010
 
********************************************************/
 
// select the pins used on the LCD panel
LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
 
// define some values used by the panel and buttons
int lcd_key     = 0;
int adc_key_in  = 0;
#define btnRIGHT  0
#define btnUP     1
#define btnDOWN   2
#define btnLEFT   3
#define btnSELECT 4
#define btnNONE   5
 
// read the buttons
int read_LCD_buttons()
{
 adc_key_in = analogRead(0);      // read the value from the sensor 
 // my buttons when read are centered at these valies: 0, 144, 329, 504, 741
 // we add approx 50 to those values and check to see if we are close
 if (adc_key_in > 1000) return btnNONE; // We make this the 1st option for speed reasons since it will be the most likely result
 // For V1.1 us this threshold
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 250)  return btnUP; 
 if (adc_key_in < 450)  return btnDOWN; 
 if (adc_key_in < 650)  return btnLEFT; 
 if (adc_key_in < 850)  return btnSELECT;  
 
 // For V1.0 comment the other threshold and use the one below:
/*
 if (adc_key_in < 50)   return btnRIGHT;  
 if (adc_key_in < 195)  return btnUP; 
 if (adc_key_in < 380)  return btnDOWN; 
 if (adc_key_in < 555)  return btnLEFT; 
 if (adc_key_in < 790)  return btnSELECT;   
*/
 
 
 return btnNONE;  // when all others fail, return this...
}
 
void setup()
{
 lcd.begin(16, 2);              // start the library
 lcd.setCursor(0,0);
 lcd.print("Push the buttons"); // print a simple message
}
  
void loop()
{
 lcd.setCursor(9,1);            // move cursor to second line "1" and 9 spaces over
 lcd.print(millis()/1000);      // display seconds elapsed since power-up
 
 
 lcd.setCursor(0,1);            // move to the begining of the second line
 lcd_key = read_LCD_buttons();  // read the buttons
 
 switch (lcd_key)               // depending on which button was pushed, we perform an action
 {
   case btnRIGHT:
     {
     lcd.print("RIGHT ");
     break;
     }
   case btnLEFT:
     {
     lcd.print("LEFT   ");
     break;
     }
   case btnUP:
     {
     lcd.print("UP    ");
     break;
     }
   case btnDOWN:
     {
     lcd.print("DOWN  ");
     break;
     }
   case btnSELECT:
     {
     lcd.print("SELECT");
     break;
     }
     case btnNONE:
     {
     lcd.print("NONE  ");
     break;
     }
 }
 
}


出處:
http://www.dfrobot.com/wiki/index.php/Arduino_LCD_KeyPad_Shield_(SKU:_DFR0009)

2014年1月5日 星期日

cross compile python for arm

這裡是編譯 Python 3.3.3 的方法

1. 建立 config.site,檔案內容如下

ac_cv_file__dev_ptmx=no
ac_cv_file__dev_ptc=no 


2.  在 python 3.3.3 的目錄下依序輸入以下指令。cross compiler 的相關變數你改成你系統的。



CONFIG_SITE=config.site CC=arm-linux-gcc CXX=arm-linux-g++ AR=arm-linux-ar RANLIB=arm-linux-ranlib ./configure --host=arm-linux --build=x86_64-linux-gnu --prefix=/python LDFLAGS="-static -static-libgcc" CPPFLAGS="-static -march=armv4t" --disable-ipv6
make HOSTPYTHON=./hostpython HOSTPGEN=./Parser/hostpgen BLDSHARED="arm-linux-gcc -static" CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes HOSTARCH=arm-linux BUILDARCH=x86_64-linux-gnu
 make install HOSTPYTHON=./hostpython BLDSHARED="arm-linux-gcc -static" CROSS_COMPILE=arm-linux- CROSS_COMPILE_TARGET=yes prefix=~/mypython

參考資料:

http://randomsplat.com/id5-cross-compiling-python-for-embedded-linux.html
http://bugs.python.org/msg136962

2014年1月4日 星期六

x86 qemu 使用方式

讓 qemu 指執行 kenel
sudo qemu-system-x86_64 -kernel /boot/vmlinuz-3.8.0-19-generic -m 256
這段指令執行到最後會發生 kernel panic 。因為找不到 root
sudo qemu-system-x86_64 -kernel /boot/vmlinuz-3.8.0-19-generic -initrd /boot/initrd.img-3.8.0-19-generic -append "root=/ram/" -m 256
上面這個指令複雜一些,kenel 會讀取 initrd (用來讀取其他檔案系統的玩意,細節) 。他會執行這個執行檔在 initrd 裡的 /init 這個執行檔,最後會失敗。進入 terminal 模式。你可以打 cat /init 看他做了什麼。


讓 cubie truck 從 sd 卡開機


這裡只是簡單的描述,細節請看 http://docs.cubieboard.org/zh/tutorials/ct1/installation/install_lubuntu_desktop_server_to_sd_card#make_bootable_sd_card

  1. 下載 u-boot,bootfs,rootfs
  2. 清除 sd 卡
    • sudo dd if=/dev/zero of=${card} bs=1024 seek=544 count=128
  3. 把 u-boot 寫進 sd 卡。
    • sudo dd if=u-boot-sunxi-with-spl-ct-20131102.bin of=$card bs=1024 seek=8
  4. 再做磁碟分割,bootfs(第一個磁區) 要 ext2,第二個磁區要 ext4,像這樣(sdd 會隨每個電腦環境不同而有所不同)
    1. Device Boot      Start         End      Blocks   Id  System
      /dev/sdd1            2048      133119       65536   83  Linux
      /dev/sdd2          133120    15278079     7572480   83  Linux
  5. 建立檔案系統並把 bootfs 和rootfs 寫進你剛剛新建的磁區
    1. $mkdir /tmp/sdd1 /tmp/sdd2
      $sudo mount -t ext2 ${card}1 /tmp/sdd1
      $sudo mount -t ext4 ${card}2 /tmp/sdd2
      $sudo tar -C /tmp/sdd1 -xvf bootfs-part1.tar.gz
      $sudo tar -C /tmp/sdd2 -xvf rootfs-part2.tar.gz
      $sync
      $sudo umount /tmp/sdd1
      $sudo umount /tmp/sdd2

2014年1月1日 星期三

在 qemu 底下模擬 Raspberry Pi


1. 安裝 qemu-system-arm

2. 下載 http://xecdesign.com/downloads/linux-qemu/kernel-qemu

3. 執行下面程式碼

qemu-system-arm -kernel kernel-qemu -cpu arm1176 -m 256 -M versatilepb -no-reboot -serial stdio -append "root=/dev/sda2 panic=1 rootfstype=ext4 rw" -hda 2013-09-25-wheezy-raspbian.img

重點就那個 kernel image 和 qemu 的參數,詳情請看參考連結。

參考連結:
http://xecdesign.com/qemu-emulating-raspberry-pi-the-easy-way/