Sunday, April 21, 2013

Ambient Lighting Monitor with Android and Java

Fascinated by Arduino and Raspberry Pi recently. Found a cool application called "Ambient Lighting" which calculates the light on your monitor and changes the color of the LED accordingly. The effect is real time cinema-like movie watching experience.

Note that this video is stolen from http://brunql.github.io/ambilight/


Most of them are using LED with Arduino controller. None of the people think of using the screen of the mobile phone or tablet as a light source (they are probably too dim for the desired effect which I found out later).

This is a quick hack during the weekend, firstly take a screenshot of the current screen. It was easier than I thought using the Java built-in function of java.awt.Robot, createScreenCapture(). After that get all the Red, Green and Blue of each pixels and calculate the average color for the current screen. Ratios of the color according to the different positions such as main color on the top right or bottom left portion of the screen are ignored as my light source is limited (1 or 2 android phone) and could not display different color on each spot.

After that, I need a simple way to send the color data to the android phone. I got lazy and just use Simple Framework to serve HTTP response on configured port. For example, every time the phone send request to http://my_ip:7788, it would return the calculated average color for current screen. To make it easier, I return a simple HTML code to set the background color of the whole page with the calculated RGB value. The page will reload every 1 second using HTTP "META refresh" tag. Reload the page more frequently with javascript should result in a more realistic ambient lighting.

The result is less satisfactory than had been anticipated. The screen of the mobile phone is not as bright as expected even after manually tuning it to the maximum brightness. Probably can improve the effect after tuning the calculated color to make it look brighter. Position information should make multiple phones and tablets act as the "LED", albeit much more expensive. Just for the sake of fun as I have a couple of Android devices hanging around.

I have published the code on bitbucket and feel free to have a look if you are looking to waste some time to enhance your movie watching experience. =)

My code on bitbucket
Ambient light

Reference:
http://brunql.github.io/ambilight/
https://code.google.com/p/boblight/
https://code.google.com/p/light-pack/
http://learn.adafruit.com/adalight-diy-ambient-tv-lighting/download-and-install

Sunday, April 22, 2012

注音轉成拼音



時常看不懂Facebook上朋友寫的注音文。昨晚心血來潮用Javascript寫了一個轉換的程式。之後發現轉成拼音了仍然看不懂。驚!有待改進。

用javascript寫的原因是以後有時間可以將之變成chrome extension。Chrome extension主要是由javascript與網頁包裝而成的。那樣就可以隨時翻譯網頁上的注音文了。=)

可以把注音文貼在這裡然後按轉換的按鈕




解釋
要來喝一杯ㄇ
ㄇ念成m或ma。所以這里可以翻譯為"嗎"。(純屬猜測)

程式的流程
主要是將textarea裡的文字,呼叫javscript的string.replace()。

例:string.replace(new RegExp('ㄦ', 'g'),'er') - 將所有'ㄦ'換成拼音的'er'。

'g' 為global的意思。不那麼寫的話只有第一個matching的字體會被轉換。之後就呼叫alert()將結果顯示出來。

相關連結
注音符號列表 (維基百科)

Monday, April 9, 2012

讀電腦的沒有前途?

"現在電腦領域改變太快了。"
雖然這領域日新月異,不過舊式的語言與機器都還是存在的。目前的電腦跟1940-1950時Von Neumann所提出的規格沒什麼兩樣。Python、Ruby等新語言當然可以減輕寫程式的負擔,不過主要的邏輯是沒變的。舉個例子,表達能力沒有問題的話,用英文、中文、日文都沒差,只是需要去瞭解它們的語法與用詞。

C++目前還是很普遍的語言。主要是它的效能比較好,firefox、chrome都是C或C++的啊。就算是Java的VM也是吧。我所知道的,馬來西亞金融機構後端server主要還是使用舊式的電腦(Mainframe)和語言(RPG),主要是因為牽涉太廣,沒必要去更換。可能你看到硬體、手機不斷推陳出新,所謂的雲端,背後還是一堆平凡的電腦。Web 2.0、Web 3.0雖然也會有些不同,不過感覺都是行銷的人想出來的用語。

"需要一直學習新東西,老了之後跟不上"
對,年紀大了記憶力分析力也許會減退,不過隨著資歷增長,所做的層面也有說不同。不需要凡事親力親為,有經驗的人看事情比較全面,可能可以從事策劃之類的。其他的領域也是大同小異吧。當然這也讓我們時時警惕自己,30歲前要賺取第一桶金!

從Apple的個人電腦、Microsoft的作業系統、Netscape遊覽器、人工分類網頁的Yahoo、利用演算法分類網頁的Google、讓人們互動的Facebook、智能手機、平板電腦、App Store…… 每一波新的潮流出現勢必讓一群人暴富,而且頻率與速度也越來越快。

Apple發明了圖形介面(也可以說是Xerox),電腦螢幕不再只是沉悶的文字。Microsoft向IBM提出了軟體授權的概念,軟體低成本的reproduce費用(只是光碟的費用),讓它的盈利飆高。隨著遊覽器的改進(AJAX等),遊覽網頁不再是單方向的行為,人們可以透過網頁跟朋友互動--所謂的社交網站。可想而知人們平均停留在Facebook的時間比在Google上搜尋的時間長。對大部份的盈利都是來自于網頁上的廣告的Google來說這是一項威脅。從這裡可以看到科技行業後浪推前浪的景象。

App Store建立了手機使用者與軟體開發者之間的橋樑,開發者不再被手機廠商、不同國家的電訊業者與關稅條款束縛。只要是有品質的軟體就會被全世界的使用者認同。Angry Birds熱潮后誰會意料到Draw Something的崛起。Draw Something早前已被Zynga收購,謠穿約180 million USD。

這還是一個充滿機會的領域。有能力的人就能突圍而出,創造 "A dent in the universe"。讀著Steve Jobs的自傳,心中燃起了一團火,希望可以維持下去。

以上是小弟的拙見,如有說錯的地方敬請高人賜教。我確信行行都能出狀元。做自己喜歡的事情,不管是不是熱門的一定可以闖出一片天。

Friday, November 26, 2010

Bit and Byte in Java and C++

There are a lot of bitwise operations involved in the C++ code.

signed and unsigned
C++ has "unsigned" type for each of the primitive types, for example char, int, etc.
What it means is to assume all values stored are positive. Bytes that were used to represent positive or negative can be used to stored additional values. Thus, unsigned data type can store up to double the range of the signed data type.

For example,
char in C:
signed: -128 to 127
unsigned: 0 to 255

Java has no "unsigned" keyword. Programmer cannot define signed or unsigned data type. Most of the data types are signed. int, short, long, etc.

The C++ program uses unsigned char, which can stored values between 0-255.

char in C++ and Java
char in Java is Unicode(2 bytes) - support chinese, japanese, etc.
char in C(1 byte) - only supports ASCII character(A-Z, 0-9, etc).

As the char in C++ is 1 byte, and it provides more freedom to play with memory. The C++ code directly manipulate byte and char with XOR operations.

Mapping char to the byte value
The alphabets and numbers are mapped to the same value in ASCII and Unicode. As long as only ASCII characters are used we can be sure that the values returned will fall in the range of 0-127, thus can be stored in byte of Java (-128 to 127). Value of 'A' in Unicode is same as the value in ASCII.

In Java, we just need to call .getBytes() method on the String to get a byte[] array for each of the char in the String. By default it would use the system default charset. We should defined the character encoding to make the result consistent across different platform. For example,getBytes("US-ASCII");

Endian
Endian is the way computer store the data in memory. It is fairly low level and normally we don't need to know/care about how the computer arrange the bytes.

12345678
Big Endian - 12-34-56-78
Little Endian - 78-56-34-12

Everything in Java is Big Endian while in C++ it depends on the implementation and CPU architecture. On x86 machine, it is Little Endian. I suspect it might affect us as we deal with bitwise operation in C++ and Java.

Luckily we didn't deal with more than 1 byte at a time. Each of the character in the String is mapped to 1 byte value and stored to array. Thus, endianness does not affect the output. '1' is 1, '2' is 2 no matter it is read backward or forward. =)

Zero and 0
Character '0' is not the same as the value 0. Character '0' is represented by value 48 in ASCII.

In the C++ program, 0x00 (0 in hex format) is appended to the char[] array. In this case, value 0 is being appended to the array, not the character '0', which would result in appending value 48 into the array.

In conclusion
Documentation matters. Standard matters.

The XOR part of the code(assume it is some kind of hashing) gives us a hard time compare to the AES encryption. AES encryption produces the same output if confirms to the standard. In Java, it is just a matter of calling the method in the library.

That's what I understand so far. Feel free to correct me if there's any error.

Assumption:
the C++ code is compiled and run on 32-bit machine.

Reference:

Sunday, May 17, 2009

SMS Jinx

When you were waiting for bus, commuting in the LRT, snorkeling at Redang island(=.=), do you have the urge to share the joy/funny/sad/grief/pain moment with your friends on the spot?

That's why I try to write this application. Reuse some codes from my final year project, polish up my rusty python skill, playing with facebook API, experimenting with Google App Engine.. That's kind of conclude what I have been doing these days.

Go to SMS Jinx and try it out. And remember to give me comment/suggestion/problems etc. =)

SMS is the most basic communication method in GSM 2.0. All of the current mobile phones support SMS. And it is cheap. Problem? Most of the foreign website such as blogger, facebook and twitter do not provide SMS service to Malaysia users. Well, even if it is provided, I wouldn't want to spend 25sen to send an international SMS to update my twitter status...

Programming is fun, when I am trying to solve my own problems. Webpage designing still sucks to the max though. I doubt that I will be a good webpage designer, ever. Anyhow, I come up with a Step 1 -> Step 3 approach. Should be simple enough to follow. Idea came from Blogger welcome page.

Users management is a troublesome issue. I try to avoid having users to register before they can use the system. Anyway, it is not possible. I need to have a userid to store the phone number and other account information. Thanks to Google SDK, it's easy to outsource the user login system. I only have to get and store the userid and Google would manage the password, cookie and authentication for me. Great. I wonder whether people would hesitate to the Google account login prompt though. It's 100% safe, HTTPS and check the verified certificate if you know what you are doing.

I realize there are a lot of API(s) made available to developers. And there are countless things you can do from generate a random friend to post facebook status message as the user credential.

Another interesting problem come when I have to find a way to make sure the messages are coming from valid user instead of attacker. Researched for 1-2 days about this. What complicates the matter is that my system consists of Java client and Python server. Great! It's difficult to find a secure implementation of encryption or hashing algorithm on both platform. In the end I settled with HMAC hashing algorithm and both the server and client understand that.

I realize that facebook uses MD5 for authentication. Duh.. Then my application would be safer than facebook theoretically. I'm using SHA256 with HMAC algorithm to makes the secret key harder to be determined. Not to forget those nonce and random numbers. Uh huh..

I'm starting to see the beauty of python.
Case insensitive? abc.lower(). done!
Substring? cde(3:)

Never miss the brackets and messy complicated declarations in java and other languages.

Similar services at Malaysia (I've never try them):
Pacmee (look expensive.)
Twitter Malaysia gateway (018 though)

Saturday, September 13, 2008

How to lock your computer

Windows
Win+L
Or
ctrl-alt-del then choose the [lock computer] button.

Mac
Set screensaver option to ask for password
And then use one of your screen corner as a way to activate screensaver
That's all.

Every time you need to leave your mac, just move your mouse to that particular corner.