2018/01/27

Raspberry Pi 開發環境建置

raspberry pi 可以直接使用 debian linux 發行套件,安裝 makefile 跟 gcc 就可以拿 source code 去編譯,但是編譯速度絕對不比高速 x86 桌機,遇到需要除錯的程式,還是在 x86 linux 上面編譯好再給 raspberry pi 跑會是一個比較省時間的做法。想法很簡單,就是在 x86 linux 上安裝 raspberry pi cross tools,然後透過 nfs 讓 raspberry pi 直接 mount 去執行。

我目前使用的 x86 linux 是 debian 9 amd64,所以以下內容將以此為基準,使用 debian based 的發行套件應該都能直接套用,其他發行套件可能要做些修改。

安裝 cross tools


編譯環境不該使用 root,所以請用一般 user 登入,把 toolchain 放在 home

aimwang@debian:~$ mkdir rpi
aimwang$debian:~$ cd rpi
aimwang@debian:~/rpi$ git clone https://github.com/raspberrypi/tools.git

安裝並設定 nfs server


aimwang@debian:~$ sudo apt-get install nfs-kernel-server nfs-common
aimwang@debian:~$ sudo mkdir /srv/nfs
aimwang@debian:~$ sudo chmod 777 /srv/nfs

/etc/exports 最後增加 (IP 根據自己的環境改)
/srv/nfs 192.168.22.*(rw,sync,no_subtree_check,no_root_squash)

重新啟動 nfs server
aimwang@debian:~$ sudo /etc/init.d/nfs-kernel-server restart

準備編譯 hello world 等一下要透過 nfs 餵給 raspberry pi 執行
Makefile
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
ROOT_PATH := $(PWD)
RPI_BASE := $(HOME)/rpi
NFS_PATH := /srv/nfs
CROSS := $(RPI_BASE)/tools/arm-bcm2708/gcc-linaro-arm-linux-gnueabihf-raspbian-x64/bin/arm-linux-gnueabihf-
CC := $(CROSS)gcc

all: hello nfs

nfs:
 @cp -f hello $(NFS_PATH)/.

hello:
 @$(CC) $(CFLAGS) -o hello hello.c

clean:
 @rm -f hello
hello.c
1
2
3
4
5
6
7
8
#include <stdio.h>
#include <stdlib.h>

void main (void)
{
 printf ("Hello world!\n");
 exit (0);
}

編譯啦
aimwang@debian:~/project/rpi/hello$ make

Raspberry Pi mount nfs 及執行

pi@raspberrypi:~ $ sudo mount -t nfs 192.168.22.200:/srv/nfs /mnt/nfs
pi@raspberrypi:~ $ /mnt/nfs/hello
Hello world!

完成

2016/08/10

LeetCode 65. Valid Number

Validate if a given string is numeric.
Some examples:
"0" => true
" 0.1 " => true
"abc" => false
"1 a" => false
"2e10" => true

解法真是非常呆,不過直覺也不是壞事
 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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
bool isNumber(char* s) {
    int len=strlen(s);
    int i;
    bool havedot=false;
    bool havee=false;
    bool havespace=false;
    bool havenum=false;
    bool havesymbol=false;

    for(i=0; i<len; i++)
    {
        if(s[i]<0x30 || s[i]>0x39)
        {
            if(' ' == s[i])
            {
                if(havenum || havee || havedot || havesymbol)
                {
                    havespace = true;
                }
            }
            else if('.' == s[i])
            {
                if(havee || havedot || havespace)
                {
                    return false;
                }
                else
                {
                    havedot = true;
                }
            }
            else if('e' == s[i])
            {
                if(havee || !havenum)
                {
                    return false;
                }
                else
                {
                    havee = true;
                    havenum = false;
                    havesymbol = false;
                }
            }
            else if('+' == s[i] || '-' == s[i])
            {
                if (havee)
                {
                    if (havesymbol || havenum)
                    {
                        return false;
                    }
                }
                else
                {
                    if (havesymbol || havenum || havedot)
                    {
                        return false;
                    }
                }
                havesymbol = true;
            }
            else
            {
                return false;
            }
        }
        else
        {
            if(havespace)
            {
                return false;
            }
            havenum = true;
        }
    }
    return havenum;
}

2016/07/14

leetcode Counting Bits

leetcode Counting Bits

Given a non negative integer number num. For every numbers i in the range 0 ≤ i ≤ num calculate the number of 1's in their binary representation and return them as an array.
Example:
For num = 5 you should return [0,1,1,2,1,2].
Follow up:
  • It is very easy to come up with a solution with run time O(n*sizeof(integer)). But can you do it in linear time O(n) /possibly in a single pass?
  • Space complexity should be O(n).
  • Can you do it like a boss? Do it without using any builtin function like __builtin_popcount in c++ or in any other language.
給一個正整數,輸出 0 ≤ i ≤ num 的二進制數值 1 的數量,明明很簡單的題目。但是後面的條件說要在 O(n) 解決掉,而不能用到 O(n * sizeof(integer)),所以直覺的寫個 function 用 shift 去計算個別數值 1 的數量並不能滿足條件。只好回歸二進制的特性找線索。
二進制每次+1的時候,就是最後一位 0 變 1 或 1 變 0,每個位數從 1 變成 0 的時候,就往前進一位。
所以 1,2,4,8,16,32,64...在進位之後,低位數就開始循環。列個清單來驗證一下這樣的推論

十進制二進制1 的數量
00000 00000
10000 00011
20000 00101
30000 00112
40000 01001
50000 01012
60000 01102
70000 01113
80000 10001
90000 10012
100000 10102
110000 10113
120000 11002
130000 11013
140000 11103
150000 11114
從表格已經可以看出 1 的數量在 1, 2, 4, 8 的時候,就是複製前面 1, 2, 4, 8 個結果,並將新複製的結果 + 1,也就是重複前面的循環但是要再加上本身進位所增加的那一個。

十進制複製輸出結果
0
0
10 00 1
201 0101 12
40112 01120112 1223
801121223 0112122301121223 12232334
確定這個方法可行,開始動手寫程式啦
 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
49
50
51
52
53
54
55
56
57
58
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int* countBits(int num, int* returnSize) {
    int s, ss, i;
    *returnSize = num+1;
    int * retvals = malloc(sizeof(int) * (num+1));
    memset(retvals, 0, sizeof(int));

    s=1;
    retvals[0] = 0;
    while(s <= num)
    {
 if(s*2 > num)
 {
     ss = num - s + 1;
 }
 else
 {
     ss = s;
 }
 memcpy(retvals + s, retvals, sizeof(int) * ss);
 for(i=0;i<ss;i++)
 {
     retvals[s+i]++;
 }
 s = s<<1;
   }
    return retvals;
}

void main(int argc, char *argv[])
{
 int * resarr;
 int rescnt, i;

 if(2 != argc)
 {
  printf("What?\n");
  return;
 }
 
 if(atoi(argv[1]))
 {
  resarr = countBits(atoi(argv[1]), &rescnt);
  printf("[%d", resarr[0]);
  for(i=1; i<rescnt; i++)
  {
   printf(",%d", resarr[i]);
  }
  printf("]\n");
 }
 else
 {
  printf("Oh! Oh!\n");
 }
}

2016/05/19

利用 ffmpeg 將 truehd 抽出 ac3 或是轉成 dts

擴大機還停留在上一個世代,無法解 truehd,只好祭出 ffmpeg

如果 truehd 有 ac3 core
        ffmpeg -i "src.thd" -c:a copy "dst.ac3"

如果沒有,乾脆轉成 dts
        ffmpeg -i "src.thd" -c:a dts -strict -2 "dst.dts"

最後再用 mkvtoolnix 或是 tsMuxeR 打包回去

話說上一世代的擴大機解 truehd 到現在還沒有成功過,而 DTS-MA 卻沒有失敗過,擴大機 RIP 換新的之前,還是只挑有 DTS-MA 的就好。

2015/11/23

C, Read text file and process line by line.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
#define _BUFFER_SIZE_   2048
 
FILE    *fd;
int     line;
char    buf[_BUFFER_SIZE_];
 
fd = fopen("filename", "r");
     
if (fd) {
    line = 0;
    while(fgets(buf, _BUFFER_SIZE_, fd) != NULL) {
        /* process line by line */
        line++;
    }
    fclose(fd);
} else {
    /* open file error handle */
}