2010年5月31日 星期一

使用shell script計算CPU使用率

PROGRAM_NAME --程式名稱
DELAY ---------每隔多久看一次CPU usage
NUMBER --------總共看幾次


取得系統CPU使用率:
top -b -n NUMBER -d DELAY | tee top.log
cat top.log | grep CPU: | awk '{print $8}' | sed s/%//g

若要查看某程式的cpu使用率
cat top.log | grep -v grep | grep PROGRAM_NAME | awk '{print $7}'| sed s/%//g







example:
假設要看hello程式, 一分鐘內每隔一秒的CPU使用率:

top -b -n 60 -d 1 | grep -v grep | grep hello | awk '{print $7}'| sed s/%//g
輸出結果:
0
9
5
2
1
2
2
2
4
2
2
3
3
1
2
3
2
3
2
0
.....

2010年5月30日 星期日

腳踏車變速器/剎車調整

我是用:
SHIMANO TY-22中變速器
SHIMANO TOURNEY TX-31 FOR 21SPEED 後變速器
SHIMANO EF50上下按鍵式變把
SHIMANO MFTZ21 7速飛輪

中變:
1.在把手上有微調旋鈕,先調整至在中間那一檔能夠順利
往上下檔位切換
2.調整high/low螺絲限制最高與最低檔位的位置
至適當距離


後變:
1.微調旋鈕在後變上,調整至後變中間幾個檔位切換順暢
2.調整high/low螺絲限制後變最高與最低檔位的
位置至適當距離


剎車:
1.調整變速器上的微調旋鈕使剎車按壓的距離至適當位置
2.調整V型剎車組的彈簧微調螺絲使得兩邊彈力差不多,
剎車皮與輪圈之間距離相同

在linux下使用LFTP多點下載

參考 http://yurinfore.blogspot.com/2009/10/linux.html

改成可以控制下載thread數量
function pget()
{
thread=5

case $# in
1)
addr=$1
;;
2)
thread=$1
addr=$2
;;
*)
echo "usage:"
echo "pget [URL] (default thread:5)"
echo "or"
echo "pget [thread] [URL]"
exit 1
;;
esac


echo thread : $thread
echo address : $addr
cmd="pget -n $thread $addr; exit"
lftp -e "$cmd"
}

2010年5月27日 星期四

linux 上的dnw程式

由於工作機是linux,每次要燒image都要切到windows不方便,
在網路上有找到別人寫的dnw程式,不過沒有作checksum的部份,就把他補上去了

ps.
1.系統需要先安裝libusb
2.使用時vid/pid可能需要依據裝置修改

/*
* cy add checksum
* use: gcc dnw2.c -o dnw2 -lusb -g
*/

/* dnw2 linux main file. This depends on libusb.
*
* Author: Fox
* License: GPL
*
*/


#include
#include
#include
#include
#include
#include

#define QQ2440_SECBULK_IDVENDOR 0x04e8
#define QQ2440_SECBULK_IDPRODUCT 0x1234


struct usb_dev_handle * open_port()
{
struct usb_bus *busses, *bus;

usb_init();
usb_find_busses();
usb_find_devices();

busses = usb_get_busses();
for(bus=busses;bus;bus=bus->next)
{
struct usb_device *dev;
for(dev=bus->devices;dev;dev=dev->next)
{
if( QQ2440_SECBULK_IDVENDOR==dev->descriptor.idVendor
&& QQ2440_SECBULK_IDPRODUCT==dev->descriptor.idProduct)
{
printf("Target usb device found!\n");
struct usb_dev_handle *hdev = usb_open(dev);
if(!hdev)
{
perror("Cannot open device");
}
else
{
int ret;
if(ret = usb_claim_interface(hdev, 0) != 0)
{
perror("Cannot claim interface");
printf("ret=%d\n", ret);
usb_close(hdev);
hdev = NULL;
}
}
return hdev;
}
}
}
printf("Target usb device not found!\n");

return NULL;
}

void usage()
{
printf("Usage: dnw2 \n\n");
}

unsigned char* prepare_write_buf(char *filename, unsigned int *len)
{
unsigned char *write_buf = NULL;
struct stat fs;

int fd = open(filename, O_RDONLY);
if(-1==fd)
{
perror("Cannot open file");
return NULL;
}
if(-1==fstat(fd, &fs))
{
perror("Cannot get file size");
goto error;
}
write_buf = (unsigned char*)malloc(fs.st_size+10);
if(NULL==write_buf)
{
perror("malloc failed");
goto error;
}

if(fs.st_size != read(fd, write_buf+8, fs.st_size))
{
perror("Reading file failed");
goto error;
}

printf("Filename : %s\n", filename);
printf("Filesize : %x bytes\n", (int)fs.st_size);

*((u_int32_t*)write_buf) = 0xC0000000; //download address
*((u_int32_t*)write_buf+1) = fs.st_size + 10; //download size;


//cy: compute checksum
int k;
u_int16_t csum=0;
for(k=0; k<(int)fs.st_size; k++){
csum +=write_buf[8+k];
}
printf("CheckSum=%d\n",(int)csum);

write_buf[fs.st_size+ 8] = csum; //checksum;
write_buf[fs.st_size+ 9] = csum>>8; //checksum;
//printf("write_buf[%d]=%.2x\n", (int)fs.st_size + 8, write_buf[(int)fs.st_size + 8]);
//printf("write_buf[%d]=%.2x\n", (int)fs.st_size + 9, write_buf[(int)fs.st_size + 9]);


*len = fs.st_size + 10;

return write_buf;

error:
if(fd!=-1) close(fd);
if(NULL!=write_buf) free(write_buf);
fs.st_size = 0;
return NULL;
}

int main(int argc, char *argv[])
{
if(2!=argc)
{
usage();
return 1;
}

struct usb_dev_handle *hdev = open_port();
if(!hdev)
{
return 1;
}

unsigned int len = 0;
unsigned char* write_buf = prepare_write_buf(argv[1], &len);
if(NULL==write_buf) return 1;

unsigned int remain = len;
unsigned int towrite;
printf("Writing data ...\n");
while(remain)
{
towrite = remain>512 ? 512 : remain;
if(towrite != usb_bulk_write(hdev, 0x2, write_buf+(len-remain), towrite, 3000))
{
perror("usb_bulk_write failed");
break;
}
remain-=towrite;
printf("\r%d%%\t %d bytes ", (len-remain)*100/len, len-remain);
fflush(stdout);
}
if(0==remain) printf("Done!\n");
return 0;
}

2010年5月24日 星期一

wpa_supplicant 連線 wpa-psk-ccmp 的ap

ps. 有些ap上面是寫AES而不是CCMP

ctrl_interface=/var/run/wpa_supplicant

network={
ssid="xxxxxx"
key_mgmt=WPA-PSK
proto=WPA
pairwise=CCMP
group=CCMP
psk="xxxxxxxxxx"
}

N1 泡水機拆解

螢幕進水


鏡頭進水&金屬生鏽


生鏽...


燒焦?


主電路板


螢幕背面與電容試觸控


軌跡球

超薄的oled

2010年5月20日 星期四

android 螢幕旋轉演算法

eclair只支援兩種方向:rotation 0 and rotation 90
CyanogenMod有支援四個方向
最新的froyo應該是支援三個方向(等froyo release再把他的演算法po上來)

source code 在 framework/base/core/java/android/view/WindowOrientationListenetr.java這裡支援的方向指的是android送給上層依據目前g-sensor算出來的螢幕旋轉方向,
程式部份自己要去對旋轉的方向作處理,預設的setting程式就有支援四個方向,
ui(Launcher)部份,把AndroidManifest.xml的android:screenOrientation="nosensor"改成sensor就可以了



以下是詳細的演算法







CyanogenMod 有支援四個方向,基本上是從原本eclair的演算法算出orientation之後的部份作修改:








android 繪圖程式範例




拿這個範例改的(修正原本要畫完放開滑鼠之後才會出現線段)





source code:
package com.cyl.android.draw;


import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class draw extends Activity {
private ArrayList _graphics = new ArrayList();
private Paint mPaint;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new DrawingPanel(this));
mPaint = new Paint();
mPaint.setDither(true);
mPaint.setColor(0xFFFF0000);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeJoin(Paint.Join.ROUND);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mPaint.setStrokeWidth(3);

}



class DrawingPanel extends SurfaceView implements SurfaceHolder.Callback {
private DrawingThread _thread;
private Path path;

public DrawingPanel(Context context) {
super(context);
getHolder().addCallback(this);
_thread = new DrawingThread(getHolder(), this);
}

@Override
public boolean onTouchEvent (MotionEvent event) {
synchronized (_thread.getSurfaceHolder()) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
path = new Path();
path.moveTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_MOVE){
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
path = new Path();
path.moveTo(event.getX(), event.getY());
}else if(event.getAction() == MotionEvent.ACTION_UP){
path.lineTo(event.getX(), event.getY());
_graphics.add(path);
}

return true;
}
}

@Override
public void onDraw(Canvas canvas) {
for (Path path : _graphics) {
canvas.drawPath(path, mPaint);
}
}



public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
_thread.setRunning(true);
_thread.start();
}

public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
boolean retry = true;
_thread.setRunning(false);
while (retry) {
try {
_thread.join();
retry = false;
} catch (InterruptedException e) {
// we will try it again and again...
}
}
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
// TODO Auto-generated method stub

}
}

class DrawingThread extends Thread {
private SurfaceHolder _surfaceHolder;
private DrawingPanel _panel;
private boolean _run = false;

public DrawingThread(SurfaceHolder surfaceHolder, DrawingPanel panel) {
_surfaceHolder = surfaceHolder;
_panel = panel;
}

public void setRunning(boolean run) {
_run = run;
}

public SurfaceHolder getSurfaceHolder() {
return _surfaceHolder;
}

@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
}
}