Tuesday, May 25, 2010

DLL wrapper for Ruby 1.9 in Windows

I found myself in a situation where I had to access a Windows DLL to control some external PC peripheral. Hey I thought, this is a good opportunity to hone my Ruby skills.

At first I used Ruby's Win32API. It works fine, provided that the dll function does not return a pointer. For example, if the DLL function is

char * controlx (int a, int b, some_struct * c)

In ruby you can process arguments a, b, and even c, but I almost vomited blood trying to process the return pointer. The thing is, Ruby do not use pointers ! This makes it very tricky to process return pointers from functions.

I was about to use plain old C to do the job, or maybe even Python (I got a Python friend - or fiend - who is telling me that this is no prob in Python, even though Python doesn't use pointers too). Then I googled upon the SWIG project (www.swig.org).

It do me a while to get SWIG working for Ruby. SWIG is pretty well documented, but the below gives the juice of the process and some gotchas. SWIG essentially generates a Ruby wrapper module (in c) for the DLL.

To save hassles, get the same c compiler and environment that created the Ruby windows version you are using. Ruby 1.9 uses MINGW compiler (basically an open source Windows C compiler that uses the Windows libraries, unlike CYGWIN that uses the POSIX libraries). You also need the make program. So I installed MINGW and MSYS, which gives a POSIX-like shell with the essential make program and other usual shell goodies. I think it may be possible to use Microsoft Visual C and nmake.exe (same Windows library calls right?), but I didn't to bother to try that.

You have to create a interface.i file that lists down the DLL functions. It looks like this.

%module your_desired_ruby_name
%{
/* Put header files here or struct declarations that are needed by the functions below */
%}

%feature("autodoc", "1"); /* this is for Ruby's rdoc */
extern char * DLL_function1(void);

%feature("autodoc", "1");
extern bool DLL_function2(void);
...............

Then just execute

> swig -ruby interface.i

or for c++

> swig -c++ -ruby interface.i

SWIG is supposed to be able to wrap both C and C++ functions for Ruby. However when I tried it with the c++ option, the generated c++ wrapper code is not compilable. No problem, I just changed all the bool types in my interface to char, and I had switched to a nice C interface. Anyway why would anyone export C++ functions in a DLL? DLLs are meant to be called from any language.

SWIG generates a c program named interface_wrapper.c. This is just a Ruby c module. So you can now use the standard Ruby ways to compile the module. You can either go the gcc way, or use Ruby built-in mkmf class. mkmf is recommended because it handles all the gcc settings for your Ruby installation (mostly the include dirs etc).

To use mkmf, create a extconf.rb file that looks like this

require 'mkmf'
$libs = append_library($libs, "your_dll_name")
create_makefile('your_ruby_module_name')

Now execute

>ruby extconf.rb

This creates a Makefile in the local directory

>make

This compiles the wrapper Ruby module. Put the DLL in the same directory as the wrapper c code to avoid any linking problems. Then just

>make install

Viola! Your Ruby wrapper module had been compiled and copied to your Ruby installation. In my case it is C:\Ruby19\lib\ruby\site_ruby\1.9.1\i386-msvcrt

To use your DLL functions, just fire up irb or type in a ruby program

irb> require 'your_module_name'
irb> YOUR_MODULE_NAME_IN_CAPITAL.dll_function_xxxx

Oh yeah one last thing. Use rdoc to generate some nice documentation of what the DLL input/output parameters are.

rdoc -f html interface_wrap.c







Sunday, November 23, 2008

Removing Nokia E71's camera

Many companies do not allow their employees to use camera phones. Unfortunately my company was one of them. But the nokia E71 is such a great phone - I like the full QWERTY keyboard very much, SMS will no longer a chore like my old phones - that I decided to get one and just remove the camera myself.

One needs the T5 and T6 special screwdrivers, and some sharp plastic edge to dissassemble the E71. No metal tools for prying open some of the parts! I learnt too late from a forum that this is VERY unwise and will just damage your cover (which it did a little to mine)



1. First remove the back covers and battery and SIM card.



2. Use T5 or T6 tool to unscrew (RED)

3. Just depress the latches (BLUE) to push out the front cover

4. As there are metal latches at the head of the phone, you got to clear the latches a bit from the body first, then just slide it towards the head (maintain the angle shown in the next pic). Make sure that the latches do not hit the flexible PCB strip that goes to the volume controls. I was trying to slide the cover out so hard that I didn't notice until quite late that the latches were damaging flex PCB. Luckily the volume controls are still working in the end (I think).



You will get this:



There are two more screws at the upper left and right of the LCD that you need to unscrew using T5 or T6 before you can remove the back cover.

By the way, I am not sure if you need to take out the antenna cover at the base of the device in order to remove the front cover, but I did anyway.

5. Pry the PCB assembly off the back cover. Camera is highlighted in RED.



6. The 3.2 Megapixel camera is attached to the underside of the PCB by a simple flex connector, so its easy to remove. You do have to open up the two pieces of the PCB assembly a bit to remove the connector

See how slim the thing is :



Split to access the camera connector:



The camera, with the mechanical auto focus:



7. If you look closely, there is a pin hole camera (RED) at the head of the phone for 3G video calls. You can either desolder this or just seal it up with opaque tape or something.



8. Now assemble back. This is probably the most DIFFICULT part. You need to snap the PCB assembly into the cover, and you got to do it carefully so that you don't irrevocably damage some plastic. I find it easier to disassemble the PCB assembly. Put in the green piece first into the back cover then the LCD piece. When you put the front cover back, do remember to slide in the front cover from the top a bit.

Ta ta! Assembled back. The cool sticker at the back is from a recent Japan trip.




Just for curiosity, I disassembled everything in the process to see how the E71 is constructed. Surprisingly, most of the PCB is bare. Most of the electronics is at the lower half, shielded behind EMI enclosures.



You can see three antennas. The bigger one is for WCDMA (or 3G). The two smaller white ceramic-like antennas are for Bluetooth and WiFi. I believe the design is called folded dipole?



The back cover is carefully designed too. You can see a speaker, vibrator (the rectangular metal box - I know there's a magnet inside because it attracts the screws), the camera flash (PCB). The parallel strips thingy is the headset jack. No idea whats the circular object though.

Saturday, July 26, 2008

Internet Radio Device

I decided to use the pico-ITX computer mainly as an internet radio device. Ok, so this would be an under-utilization of the 1 GHz CPU, 512 MB, Win XP appliance, but I can't think of any other more interesting applications that I would need at the moment. Of course I can use it to watch streaming video, or play my mp3 collections, but internet radio (SHOUTCAST) would be its main purpose.

I googled and installed the following software
- Winamp : mp3, internet radio player
- CLamp : command line control application to Winamp's API
- AutoHotkey : a key mapping tool with surprisingly powerful scripting capabilties.

I also got myself a nice little numeric keyboard. With a simple script, I am now able to select radio stations, control volume, and shutdown the device with a single keypress. I have also configured XP so that it will autoplay the first internet radio station upon boot up. No user input required. On the power switch, wait 30s, you got the music. The audio-out goes straight to my hi-fi system auxilary input, so the sound is great.


The device doesn't need a monitor to boot up, but I linked the VGA out to my HDTV. I usually off the HDTV when I turned on the device, but if I wanted to find out the name of a particularly good song, I can check it out on screen.

Small-Form Factor PC and Win CE

When I got the VIA Artigo, it comes bundled with full (but non-commerical) versions of Windows CE 6.0 and Visual Studio 2005. I guess its part of Microsoft's marketing to get more people hooked on their embedded OS.

I tried playing with Windows CE 6.0 R2. Its a bit irritating at first because I had to install a couple of service packs before Win CE 6.0 R2 can be installed. But once I got Windows CE running, I am impressed (I am normally not a fan of Microsoft). Win CE is good for quickly building embedded applications. With the Platform Builder in Visual Studio, one can click and add components such as web servers, drivers, OS applications from a fairly comprehensive library into an image. This image (a bit like vmlinuz) is compiled and downloaded into the target embedded device. Downloading and debugging is done via ethernet, so its very convenient (once you managed to install and run eboot.bin onto the target so that it can initiate the ethernet connection)

Provided
that a Win CE board support package (BSP) is available from the embedded platform vendor, embedded linux development should eat the dust compared to Windows CE where it comes to time-to-market. A BSP contains the customised driver code needed to interface with the platform devices and peripherals.


Unfortunately, fast doesnt mean no work. Tinkering is still needed to make CE work on the Artigo. Artigo just refuses to recognize my USB thumbdrives. Since I was not really keen to spend many hours doing embedded development/debugging at this particular moment, I decided to just use good old XP instead. Actually I was deciding between XP and Linux, but because of the application that was forming in my mind, I chose XP. Linux is stable and fast, but when it comes to multimedia and graphics, Windows is still better, in my opinion.

Friday, July 25, 2008

Small-Form PC

I was lucky enough to get my hands on a VIA Artigo computer recently. It is probably one of the smallest small-form PC ever. Sleek and small.


The VIA Artigo uses the pico-ITX motherboard, which is the smallest motherboard from VIA. The pico-ITX board is primarily designed for embedded applications that need more-than-the-usual horsepower. For example, industrial control, set-top boxes such as singtel's mio TV, car computers, ATMs, even thin clients.

VIA sells the Artigo computer as a hobbyist kit, without RAM or hard disk. My Artigo has the following.
- CPU : VIA C7 (This is a 1 GHz x86 CPU. It also apparently has built-in AES/SHA cryptographic cores!)
- Chipset: VIA VX700 (northbridge, southbridge, integrated graphics, HD audio all in one package). Supports SATA, IDE, USB, DDR2 RAM etc
- RAM: 512 MB
- 3.5" HD: 80 GB
- 4 USB ports
- 1 VGA port
- audio out and in ports

The VX700 chipset diagram


Lets see whats inside


The heat sink is for the C7 CPU and the VX700. I like the low profile heat disspation design.


Harddisk is mounted to the custom chasis mounting.


Removing the hard disk reveals the notebook RAM

Monday, July 14, 2008

Gaming Rig More Pics

I decided to upload some day pics of the rig as well.






I placed the RF transceiver for the mouse and keyboard inside the case, and held it down to the DVD bay with some tape.


The joints of the transparent case are made of acrylic too.

Gaming Rig Components Pics

EN9600GT graphics card with the 'gladiator' quiet fan


The red butterfly is the BTF90 fanless CPU cooler!


You can see the copper heat sink fins here. Note the height of the cooler. There are also four heat pipes. A casing fan and the power supply fan are just next to the cooler to ensure a good air flow.


And this must be the northbridge heat sink ....


And the smaller southbridge heat sink.


Back IO port panel


And the 625W Enermax Mod82+ power supply. Its pretty quiet and the modular connectors, while not essential, make the case a lot tidier. The casing fans turn out to be the main noise generator here.