2015年3月10日 星期二

Linux tty raw mode

The TTY demystified
http://www.linusakesson.net/programming/tty/

use stty set tty to raw mode
http://linux.about.com/od/commands/l/blcmdl1_stty.htm

#!/bin/bash

# Port setting
stty -F /dev/ttyS0 raw speed 1200

# Loop
while [ 1 ]; 
do
    echo 'LOADING...'
    READ=`dd if=/dev/ttyS0 count=1`
    echo $READ


echo '[PRESS Ctrl + C TO EXIT]'
done

per
http://man7.org/linux/man-pages/man3/termios.3.html

use cfmakeraw() in C program

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include <termios.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/syslog.h>
#include <sys/types.h>
#include <sys/un.h>

#define SERIALPORT_IS_CONSOLE

main()
{
    struct termios  tty;
    struct termios  savetty;
    speed_t     spd;
    unsigned int    sfd;
    unsigned char   buf[80];
    int     reqlen = 79;
    int     rc;
    int     rdlen;
    int     pau = 0;

#ifdef SERIALPORT_IS_CONSOLE
    sfd = STDIN_FILENO;
#else
    sfd = open("/dev/ttyS1", O_RDWR | O_NOCTTY | O_NONBLOCK);
#endif
    if (sfd < 0) {
        syslog(LOG_DEBUG, "failed to open: %d, %s", sfd, strerror(errno));
        exit (-1);
    }
    syslog(LOG_DEBUG, "opened sfd=%d for reading", sfd);

    rc = tcgetattr(sfd, &tty);
    if (rc < 0) {
        syslog(LOG_DEBUG, "failed to get attr: %d, %s", rc, strerror(errno));
        exit (-2);
    }
    savetty = tty;    /* preserve original settings for restoration */

    spd = B115200;
    cfsetospeed(&tty, (speed_t)spd);
    cfsetispeed(&tty, (speed_t)spd);

    cfmakeraw(&tty);

    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 10;

    tty.c_cflag &= ~CSTOPB;
    tty.c_cflag &= ~CRTSCTS;    /* no HW flow control? */
    tty.c_cflag |= CLOCAL | CREAD;
    rc = tcsetattr(sfd, TCSANOW, &tty);
    if (rc < 0) {
        syslog(LOG_DEBUG, "failed to set attr: %d, %s", rc, strerror(errno));
        exit (-3);
    }

    do {
        unsigned char   *p = buf;

        rdlen = read(sfd, buf, reqlen);
        if (rdlen > 0) {
            if (*p == '\r')
                pau = 1;
            syslog(LOG_DEBUG, "read: %d, 0x%x 0x%x 0x%x", \
                     rdlen, *p, *(p + 1), *(p + 2));
        } else {
            syslog(LOG_DEBUG, "failed to read: %d, %s", rdlen, strerror(errno));
        }
    } while (!pau);

    tcsetattr(sfd, TCSANOW, &savetty);
    close(sfd);
    exit (0);
}
 more example
http://www.cs.uleth.ca/~holzmann/C/system/ttyraw.c




2015年3月1日 星期日

AndroLua = Luajava + Luajit for android


step 1

https://github.com/mkottman/AndroLua


git clone git://github.com/mkottman/AndroLua.git
cd AndroLua
$NDK/ndk-build
$SDK/tools/android update project -p .
ant debug
ant install

step 2
http://luajit.org/download.html

use one of these
# Android/ARM, armeabi (ARMv5TE soft-float), Android 2.2+ (Froyo)
NDK=/opt/android/ndk
NDKABI=8
NDKVER=$NDK/toolchains/arm-linux-androideabi-4.6
NDKP=$NDKVER/prebuilt/linux-x86/bin/arm-linux-androideabi-
NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF"

# Android/ARM, armeabi-v7a (ARMv7 VFP), Android 4.0+ (ICS)
NDK=/opt/android/ndk
NDKABI=14
NDKVER=$NDK/toolchains/arm-linux-androideabi-4.6
NDKP=$NDKVER/prebuilt/linux-x86/bin/arm-linux-androideabi-
NDKF="--sysroot $NDK/platforms/android-$NDKABI/arch-arm"
NDKARCH="-march=armv7-a -mfloat-abi=softfp -Wl,--fix-cortex-a8"
make HOST_CC="gcc -m32" CROSS=$NDKP TARGET_FLAGS="$NDKF $NDKARCH"
copy result file src/libluajit.a to androlua/jni/luajava
 copy src/lua.h, src/luajit.h,src/luaconf.h, src/lualib.h, src/lauxlib.h to androlua/jni/luajava
remove androlua/jni/lua
step 3
modify androlua/jni/luajava/android.mk
LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_LDLIBS := $(LOCAL_PATH)/libluajit.a
LOCAL_ARM_MODE := arm
TARGET_PLATFORM := armeabi
#TARGET_PLATFORM := armeabi-v7a
TARGET_ABI := android-8-armeabi
LOCAL_MODULE     := luajava
LOCAL_SRC_FILES  := luajava.c

include $(BUILD_SHARED_LIBRARY)
ndk-build clean
ndk-build
ant clean
ant debug

2014年10月7日 星期二

解決 virtualbox ubuntu guest 無法存取共用資料夾

在 virtual box 建立  ubuntu guest OS,  當設定主機共用資料夾, 會出現在 ubuntu 的 /media/sf_filename

但是這個目錄的屬性為 root vboxsf
我們無法直接存取, 是很麻煩的, 又不想改成 root login

google 一下, 只要將共用資料夾設定為自動掛載, 使用者加入 vboxsf 群組既可

sudo adduser <username>  vboxsf
sudo usermod -aG  vboxsf   <username>
然後 reboot, 重新進 ubuntu



2014年6月27日 星期五

ubuntu 12.04 用 nolapic 時, 只認到 CPU 0

灌完 ubuntu 12.04, 進OS 時會當機, 有時當在 gdm login, 有時是當在 input password 之後. 然後 keyboard 燈狂閃

加 kernel 參數 nolapic 可以進OS. 但是 /proc/cpuinfo 只見到一核. SMP 失效

google 有人這樣解

I finally get both core working. I added these options to the kernel line in /boot/grub/menu.lst 

noapic pci=assign-busses apicmaintimer idle=poll reboot=cold,hard 

And two cores were brought up. 

但我只用 pci=assign-busses apicmaintimer idle=poll,  對我的OS就夠好了

2014年5月29日 星期四

undefined reference to 'WebPDecodeRGBAInto' when build SDL_image for Android

使用 Android NDK 來build  SDL2_image 時, 出現如下錯誤訊息

 SDL2_image/IMG_webp.o: in function IMG_InitWEBP:jni/SDL_image/IMG_webp.c:123: error: undefined reference to 'WebPDecodeRGBAInto'

這時在 SDL_image 的 Android.mk 最後面加上這幾行

#
# 假設環境變數 NDK_ROOT 指到 NDK
#
#  設定 NDK_MODULE_PATH
#
$(call import-add-path,$(NDK_ROOT)/sources)
$(call import-add-path,./$(LOCAL_PATH))

#  叫 ndk-build 編譯 libwebp
$(call import-module,$(WEBP_LIBRARY_PATH))

2014年4月15日 星期二

多線程 tar 壓縮與解壓縮

TAR 是單線程的, 就算是多核 CPU, 只有單核 CPU 使用率 100%, 其他的在睡覺.

有人寫了多線程壓縮器來充分使用多核心優勢
http://www.zlib.net/pigz/
http://lbzip2.org/
http://www.nongnu.org/lzip/plzip.html
http://jnovy.fedorapeople.org/pxz/
http://sourceforge.net/projects/pixz/files/
https://wiki.archlinux.org/index.php/Lrzip



ubuntu 直接安裝
sudo apt-get install pigz
sudo apt-get install lbzip2
sudo apt-get install plzip
sudo apt-get install lrzip

pxz & pixz 目前不在 ubuntu 套件內, 需要自己拉 source code rebuild


Compress
tar -I pigz -cf test.tar.gz *

Decompress

tar -I pigz -xf test.tar.gz

剩下的問題就看 Harddisk & memory 速度有多快


也可以把 pigz rename 成 gzip,  來省去 "-I pigz"

另外有人拿 tar 的 source code 重新編譯, 在 configure 加參數  
"--with-gzip=pigz"  "--with-bzip2=lbzip2" "--with-lzip=plzip" 

這樣就不需要使用 "-I  xxx" 參數  

tar --help | grep "lbzip2\|7x\|plzip\|pigz"
  -j, --bzip2                filter the archive through lbzip2
  -J, --xz                   filter the archive through 7x
      --lzip                 filter the archive through plzip
  -z, --gzip, --gunzip, --ungzip   filter the archive through pigz

追蹤者