2015/4/9

【樹莓派】查看版本、規格與製造商

在第一代不同的Model在GPIO腳位的設計上有些許的不同,板子上的記憶體大小也不一樣,要知道自己手上的RPi是屬於哪個版本、製造商是誰可以透過查看CPU資訊來得知
cat /proc/cpuinfo
顯示結果會有像下面的訊息
Hardware        : BCM2708
Revision        : 000e
Serial          : 00000000bf257857
依據Revision再去查表即可知道詳細的相關資訊
像手上這塊板子是000e,則可以知道是Model B 2.0、記憶體是512MB,由Sony製造。
表格資訊會隨時間持續增加項目,最新資料可到elinux.org查詢。

2015/3/30

【樹莓派】編譯一個Hello World程式在RPi上執行

要在PC上編譯一個可以在Raspberry Pi上執行的程式必須透過Cross-Compile才能達成。Cross-Compile的意思就是在A架構電腦(比如你用的一般電腦)上編譯B架構電腦(比如樹莓派)能執行的二進位程式,為了讓電腦能夠Cross-Compile出樹莓派能接受的執行檔,須要先在電腦上安裝Cross-Compiler:
1
sudo apt-get install gcc-arm-linux-gnueabi
這裡寫了一個簡單的Hello World程式做為示範
1
2
3
4
5
6
#include <stdio.h>

int main(int argc, char *argv[])
{
    printf("Hello Raspberry Pi!\n");
}
透過剛安裝的Cross-Compiler進行編譯:
1
arm-linux-gnueabi-gcc -o hello hello.c
產生的執行檔可以使用file指令查看執行檔格式是否為ARM架構
若正確則可將檔案放到樹莓派上執行,應該可以得到下列結果:

2015/3/18

解決呼叫某些系統函數發生'無法解析的外部符號'問題的兩種方法

Windows有些系統函數被存放在像是Shlwapi.lib函數庫中而非常用到的kernel32.lib,在Visual Studio編譯環境中,Linker預設是不會去找Shlwapi.lib等較不常用的Library進行鏈結,所以當呼叫到如PathFileExists等系統函數,編譯過程中就會收到類似錯誤:
1>Source.obj : error LNK2001: 無法解析的外部符號 __imp__PathFileExistsA@4
1>E:\Tmp\ConsoleApplication13\Release\ConsoleApplication13.exe : fatal error LNK1120: 1 個無法解析的外部符號
1>
1>建置失敗。

解決方法有兩種:
其一是透過設定專案屬性→連結器→輸入→其他相依性,加入*.lib(如Shlwapi.lib)
其二是透過假指令#pragma告知編譯環境將調用到*.lib
#pragma comment(lib, "Shlwapi.lib")

2015/2/7

【C#】客製化外觀的三態按鈕(Three State CheckBox)

.NET提供的CheckBox元件本身就支援三態按鈕(三種選取狀態),只要把屬性ThreeState設定為True即可
但有時候預設的外觀顯示與操作體驗可能無法準確呈現使用情境,這時可以新建一個類別繼承CheckBox並重載OnPaint與OnClick方法來客製化符合使用情境的三態按鈕
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
class CheckBoxTriState : CheckBox
{
    public CheckBoxTriState()
    {
        ThreeState = true;
        CheckState = System.Windows.Forms.CheckState.Indeterminate;
        Text = String.Empty;
        Size = new Size(20, 40);
    }

    protected override void OnPaint(PaintEventArgs pevent)
    {
        Graphics g = pevent.Graphics;
        g.Clear(Color.LightGray);
        Brush b = new SolidBrush(Color.DarkGray);
        g.DrawRectangle(new Pen(b), 0, 0, Width, Height);

        float radius = (float)(Width * 0.9);
        float margin = (float)(Width * 0.05);
        if (CheckState == System.Windows.Forms.CheckState.Indeterminate)
            g.FillEllipse(b, margin, (float)(Height / 4.0) + margin, radius, radius);
        else if (CheckState == System.Windows.Forms.CheckState.Checked)
            g.FillEllipse(b, margin, margin, radius, radius);
        else
            g.FillEllipse(b, margin, (float)(Height / 2.0 - margin), radius, radius);
    }

    protected override void OnClick(EventArgs e)
    {
        Point pos = this.PointToClient(Cursor.Position);
        if (pos.Y < Height / 3)
            CheckState = System.Windows.Forms.CheckState.Checked;
        else if (pos.Y > Height * 2 / 3)
            CheckState = System.Windows.Forms.CheckState.Unchecked;
        else
            CheckState = System.Windows.Forms.CheckState.Indeterminate;
    }
}
使用方始與一般的CheckBox相同
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
        CheckBoxTriState check = new CheckBoxTriState();
        check.Location = new Point(10, 10);
        Controls.Add(check);

        CheckBoxTriState check2 = new CheckBoxTriState();
        check2.Location = new Point(40, 10);
        check2.CheckState = CheckState.Checked;
        Controls.Add(check2);

        CheckBoxTriState check3 = new CheckBoxTriState();
        check3.Location = new Point(70, 10);
        check3.CheckState = CheckState.Unchecked;
        Controls.Add(check3);
    }
}
Keyword:How to customize a tri-state checkbox / button in C#

2015/2/2

【C#】客制化可調大小的勾選元件(CheckBox)

CheckBox的方框大小是以Hard Coded方式寫死的,因此沒辦法藉由設定CheckBox任何參數來改變勾選框框的大小,但可以透過override方是依自己需求重新繪製方框,直接看程式碼:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class CheckBoxEx : CheckBox
{
    public CheckBoxEx()
    {
    }

    public override string Text
    {
        get
        {
            return base.Text;
        }
        set
        {
            base.Text = value;
            Size size = TextRenderer.MeasureText(value, Font);
            if (Width < size.Width + ClientSize.Height)
                Width = size.Width + ClientSize.Height;
        }
    }

    public override Font Font
    {
        get
        {
            return base.Font;
        }
        set
        {
            base.Font = value;
            Size size = TextRenderer.MeasureText(Text, value);
            if (Width < size.Width + ClientSize.Height)
                Width = size.Width + ClientSize.Height;
        }
    }

    protected override void OnPaint(PaintEventArgs e)
    {
        int h = ClientSize.Height;
        Rectangle rc = new Rectangle(new Point(0, 0), new Size(h, h));
        e.Graphics.Clear(Parent.BackColor);
        ControlPaint.DrawCheckBox(e.Graphics, rc,
            this.Checked ? ButtonState.Checked : ButtonState.Normal);
        SizeF size = e.Graphics.MeasureString(Text, Font);
        e.Graphics.DrawString(Text, this.Font,
            new SolidBrush(Color.Blue), new PointF(h, size.Height < h ? (h - size.Height) / 2 : 0));
    }
}
重點在於重載OnPaint這個繪圖函數,在函數內使用ControlPaint.DrawCheckBox重新繪製方框外形,因重載了原本OnPaint,因此要自己把文字補畫上去(參考44 ~ 46行)。
  因重設字型大小或顯示文字內容會影響CheckBox的呈現寬度,因此一併重載TextFont屬性,當使用者更改這兩個屬性值,則要重新計算CheckBox的寬度,否則文字呈現可能會被截斷。
  使用方法跟一般CheckBox一樣:
1
2
3
4
5
6
CheckBoxEx check = new CheckBoxEx();
check.Location = new Point(40, 40);
check.ClientSize = new Size(30, 30);
check.Text = "Hello CheckBox";
check.Font = new System.Drawing.Font("新細明體", 18);
Controls.Add(check);

Keyword:Customize checkbox size

2015/1/30

【PetaLinux】設定自動登入

PetaLinux預設要由使用者輸入帳號密碼登入後才能進行系統操作,但很多時候在嵌入式裝置應用上會希望能夠開機完成後能自動登入並執行後續指令功能。要設定自動登入主要就是修改/etc/inittab這個檔案,只要把修改後的inittab檔案塞進Root File System中即可完成。

首先使用開發套件產生一個安裝開發樣版
1
petalinux-create -t apps --template install --name autostart
這會在開發目錄components/apps下產生一個名為autostart的樣板資料夾
inittabautologin.sh檔案放到autostart資料夾中

接著修改Makefile
1
2
3
4
5
6
7
8
install:
 # Please add commands below the comments to install data to target file system.
 # Use $(TARGETINST) to copy data into the target
 # E.g. there is data/acos_install in the current directory, and I want to
 # copy it into the target "/" directory:
 #$(TARGETINST) -d data/acos_install /acos_install
 $(TARGETINST) -d -p 0755 autologin.sh /home/autologin.sh
 $(TARGETINST) -d -p 0755 inittab /etc/inittab

回到開發環境最上層資料夾
1
petalinux-config -c rootfs
進入Root File System編譯選單鉤選autostart安裝選項並存檔離開
執行編譯
1
petalinux-build

最後將在images/linux目錄下的image.ub複製到SD Card中取代原本檔案即可。

Keyword: How to set auto login for PetaLinux, Push files into root file system

2015/1/28

【Ubuntu】切換預設shell為bash

  Ubuntu基於效能等因素從6.10版以後將預設執行的shell由bash改為dash,最近要編譯PetaLinux在很前面步驟就錯誤了,從Log看不出點蛛絲馬跡,後來將預設的shell改為bash後一切就沒問題了!
  修改指令:
1
sudo dpkg-reconfigure dash
跳出選單選擇no即可完成修改。

參考文件:DashAsBinSh