ctypes typecasting in perl

Shantanu Bhadoria shantanu at cpan.org
Tue Sep 10 06:14:59 BST 2013

Thanks Abigail,
That works perfectly. I had a inkling that the solution lies in pack
unpack. This is it.

> I think you want pack/unpack:
>   $ perl -E 'say unpack s => pack S => 64256'
>   -1280
>   $
> Abigail
> From: Yitzchak Scott-Thoennes <sthoenna at gmail.com>

> Abigail's pack/unpack suggestion will convert it after the fact, but I
> am wondering what you do to "read[ing] the register" to get an
> unsigned int in the first place.
> Hi Yitzchak,
I am working with I2C/SMBus devices but there weren't many modules offering
a easy interface for that on cpan that I was particularly pleased with. I
had to write my own based on libi2c-dev library called Device::SMBus
It doesn't expose all functions provided by smbus section of libi2c-dev but
it does provide most of the usable parts.
I pushed it to CPAN a few days ago. I am planning to push
Device::LSM303DLHC and Device::L3GD30 public too since the last bit off
handicap I had is sorted out now.

In case you are curious, all this is to access altIMU Inertial measurement
unit which includes a LSM303 chip for magnetometer/accelerometer L3GD20
chip for gyroscope and a LPS331AP chip for barometer/altimeter for my
personal robotics project. Its a cheap piece of hardware for doing stuff by
yourself if you can't afford the more expensive IMU solutions in the
market. Only trouble being that you have to write the whole code, filtering
algorithms interfaces/API etc. yourself from the absolute raw data provided
by this chipset, which makes it a fun project for me.
-Shantanu Bhadoria

More information about the london.pm mailing list