Thursday, August 21, 2008

Socket in python

server:
#!/usr/bin/env python
import socket
if __name__ == '__main__':
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.bind(('localhost', 8001))
sock.listen(5)
while True:
connection,address = sock.accept()
try:
connection.settimeout(5)
buf = connection.recv(1024)
if buf == '1':
connection.send('this is one')
else:
connection.send('this is not one')
except socket.timeout:
print 'time out'
connection.close()

client:
#!/usr/bin/env python
def send():
import socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect(('localhost', 8001))
import time
time.sleep(1)
time.sleep(1)
sock.send('1')
print sock.recv(1024)
sock.close()

if __name__ == '__main__':
while(True):
send()

Monday, August 18, 2008

这个谜语有意思。

清代的谜学家张起南在《橐园春灯话》有个谜语真是bt到极点了。

杜甫诗句“无边落木萧萧下“打一个字:

谜底是"日"

因为萧是齐、梁朝的国姓,故萧->陳, 陳无边->東,東落木->日。

BQ27200芯片的编程


BQ27200 is a commonly used chip in mobile phone like HP IPAQ 6828's battery. It is possible to make a program that interact with the chip in windows ce platform.

First, you will need PXA27X's DDK for windows CE 5.0 or bulverde board development files.

Then, you can start programming. The most important thing is to allocate virtual memory for addressing the chip.

void InitializeBulverde(){
vg_pGPIORegs = NULL;
vg_pI2CRegs = NULL;
vg_pClkRegs = NULL;
vg_pOSTRegs = NULL;

PUCHAR pVMem; // for virtual memory address
DWORD VMemSize; // Total size needed for virtual memory

// Total size needed for virtual memory :
// Page size is 4Kbytes
// Driver Globals is also 4Kbytes
VMemSize = (PAGE_SIZE*5);
////////////////////////////////////////
// Size needed for register space.
pVMem = (PUCHAR)VirtualAlloc(0, VMemSize, MEM_RESERVE, PAGE_NOACCESS);
if(!pVMem) {
MessageBox(NULL, TEXT("Virtual Allocate failed!"), TEXT("InitBatteryDriver"), MB_OK);
}

// Map register space to virtual memory.
// 1. GPIO register
if(!VirtualCopy(pVMem, (LPVOID)(BULVERDE_BASE_REG_PA_GPIO >> 8), PAGE_SIZE,
PAGE_READWRITE|PAGE_NOCACHE|PAGE_PHYSICAL)) {
NKDbgPrintfW(_T("InitBatteryDriver: Virtual Copy: GPIO space failed\r\n"), GetLastError());
MessageBox(NULL, TEXT("GPIO space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pGPIORegs = (DWORD *)pVMem;
pVMem += PAGE_SIZE;

// 2. I2C register
if(!VirtualCopy(pVMem,(LPVOID)(BULVERDE_BASE_REG_PA_I2C >> 8), PAGE_SIZE,
PAGE_READWRITE | PAGE_PHYSICAL | PAGE_NOCACHE)) {
MessageBox(NULL, TEXT("I2C space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pI2CRegs = (DWORD *)pVMem;
pVMem += PAGE_SIZE;

// 3. CLKMGR register
if(!VirtualCopy(pVMem,(LPVOID)(BULVERDE_BASE_REG_PA_CLKMGR >> 8), PAGE_SIZE,
PAGE_READWRITE | PAGE_PHYSICAL | PAGE_NOCACHE)) {
MessageBox(NULL, TEXT("CLKMGR space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pClkRegs = (DWORD *)pVMem;
pVMem += PAGE_SIZE;

// 4. OST register
if(!VirtualCopy(pVMem,(LPVOID)(BULVERDE_BASE_REG_PA_OST >> 8), PAGE_SIZE,
PAGE_READWRITE | PAGE_PHYSICAL | PAGE_NOCACHE)) {
MessageBox(NULL, TEXT("OST space failed"), TEXT("Virtual Copy"), MB_OK);
}
vg_pOSTRegs = (DWORD *)pVMem;

XllpI2cInit((P_XLLP_I2C_T)vg_pI2CRegs, (P_XLLP_GPIO_T)vg_pGPIORegs, (P_XLLP_CLKMGR_T)vg_pClkRegs, (XLLP_UINT32_T) 0x1);

Then you can you can read the registry like this:

if (XllpI2CReadS((P_XLLP_I2C_T)vg_pI2CRegs, (XLLP_OST_T *)vg_pOSTRegs, BQ27200_SLAVE, (XLLP_UINT8_T *)&buff, 1, bSENDSTOP) == XLLP_TRUE) {
return L" Battdrvr: XllpI2CReadS() is failed \r\n";
}
and write like this:

if (XllpI2CWriteS((P_XLLP_I2C_T)vg_pI2CRegs, (XLLP_OST_T *)vg_pOSTRegs, BQ27200_SLAVE, (XLLP_UINT8_T *)&buff, 1, bSENDSTOP) == XLLP_TRUE) {
MessageBox(NULL, TEXT("XllpI2CWriteS() is failed"), TEXT("Battdrvr"), MB_OK);
}

Note, in driver programming, one should use NKDbgPrintfW to print out the debug information instead of fprintr(stderr ...). This is because, this is a real-time function and will be used only in WinCE programs that interact with the kernel. So it is better.

Ok. here I will publish the the battery calibration for BQ27200 called BQ27200Expert. It can display various information about the BQ27200 and also calibrate the battery.

Download the program from XDA!

http://forum.xda-developers.com/showpost.php?p=2527251&postcount=1525


Tuesday, August 5, 2008

Python, a viper that bite me!

I should have learnt it before. It is soooo convenient to process strings and the language is beautiful. My previous programs have taken me 7 or 8 days. But in python, it takes me only 1 day. The debuging time is sharply reduced.

Also I like the philosophy of python.

wchar_t , wcs[insert function in string.h], and w[function in stdio.h]

These functions appears as an addition in C++98 standard and c99. I thought it is windows-only before.

Sunday, August 3, 2008

Mr Angry and Mrs Calm(生气先生和平静女士)


This is an interesting image.

Look at two persons closely. You can find that the left one is angry and the right one is calm. But When looking far away, the left one is calm and the right one is angry, just the opposite!

There are even more:

This is a catdog image:

Note: these images are from Philippe G. Schyns and Aude Oliva of the University of Glasgow.