Archive for May, 2006

Hardware hacking the MacBook movement sensor

Developers keep finding new and unexpected uses for hardware and software – seemingly never more so than on the Mac and OS X.

Whether they’re using the iSight camera to scan bar codes into your Delicious Library or turning the AppleRemote into a alarm key-fob in TheftSensor there’s always some novel hack around the corner for the latest bit of kit.

Current attention is focused on the motion sensor added to MacBook and MacBook Pro’s that is used to park the hard-drive should sudden movement be detected.

In true Apple fashion rather than add a simple yes/no movement sensor the device can detect tilts and both the X and Y axis as well as the current level of force.

With a little imagination those enterprising third-party developers have come up with…

Sudden Motion Sensor

Amit Singh is a man with a mission – which is just as well because he doesn’t have a MacBook or a MacBook Pro. So quite how he’s written a bunch of fun apps and a useful command line motion sensor tool is another matter.

Amit includes some technical coverage of the sensor and how it works over the I2C bus Apple also use for temperature sensing, fan control etc.

He also has a useful command-line tool and some fun hacks such as showing a MacBook Pro in 3D oriented as yours currently is and a window that self-adjusts itself to always be horizontal. Alas, at least on my MacBook Pro, the X axis seems to be inverted and the invert X axis didn’t work so the effect fails to impress.

More interesting is his page on using the motion sensor as an input device. While not as advanced as the Nintendo Wand – it doesn’t support detection of position – it still holds some promise as he illustrates.

MacSaber

With the sensor sporting a force level it was only a matter of time before somebody used that sensor magic to come up with something like MacSaber.

The sole purpose of this app is to make your $2,000 laptop hum like a StarWars lightsaber as you swing it dangerously around the room with a satisfying tzzzzum and sheuw.

Just don’t rely on the midichorians to stop your MacBook slipping through your fingers…

SmackBook Pro

You might be thinking this sensor is nothing but a curiosity but SmackBook Pro illustrates a rather novel use to desktop switching.

Desktop switching is one of those problem areas for short cuts. You might be using any one of tens of apps and finding a good shortcut that’s not taken by any of those applications could really be a problem.

SmackBook Pro’s solution is to tap the side of your machine to switch desktops and the way the new desktop flies in from the side is a great visual indicator of what has just happened as the video illustrates.

I’m not sure constantly tapping the side of an LCD is good for it’s health.

Many more hacks to be found at Raul’s blog.

[)amien

IEnumerable<T> as IEnumerable<BaseClassOfT>

A few weeks ago I touched on the substitutability of generic types and how Collection<BaseClass> is never substitutable for Collection<SubClass>. This is because while the read facilities would be substitutable the write ones would not be. A Collection<SubClass> can’t contain non-SubClass classes.

One approach for dealing with collections in general recommended in .NET Framework Design Guidelines is to expose IEnumerator or IEnumerable interfaces rather than the collection itself – especially if you intend on it being read-only.

But even IEnumerator<T> and IEnumerable<T> can’t help you if you need to return not <T> but a base-class or interface that <T> should be substitutable for. Unless the base-class you want is object in which case seeing IEnumerable<T> inherits from the non-generic IEnumerable solves your problem.

C#’s generics aren’t clever enough to realise that an instantiated generic class might be substitutable for another generic class although. Wilco points out that the .NET CLR supports “generic contravariance” but C# doesn’t yet expose it.

In the mean time trying to passing back IEnumerator<T> to something expecting IEnumerator<BaseClassOfT> will result in a compiler error. Try cast ing it and a runtime error awaits.

One option would be to create a whole new Collection<BaseClass> and copy each SubClass element into it but this is hardly efficient or elegant.

What would be really cool is if you could somehow wrap it like;

public IEnumerable<BaseClass> BaseClasses {
   return new BaseEnumerable<BaseClass, SubClass>(subClassCollection);
}

Of course you’d need BaseEnumerator and BaseEnumerable generic classes to do this and preferably they’d be able to enforce that SubClass is of BaseClass at compile time.

So here are two classes to do just that for your enumerable pleasure.

using System;
using System.Collections;
using System.Collections.Generic;

public class BaseEnumerable<TBase, TSub> : IEnumerable<TBase> where TSub : TBase {
    private IEnumerable<TSub> subEnumerable;

    public BaseEnumerable(IEnumerable<TSub> subEnumerable) {
        this.subEnumerable = subEnumerable;
    }

    public IEnumerator<TBase> GetEnumerator() {
        return new BaseEnumerator<TBase, TSub>(subEnumerable);
    }

    IEnumerator IEnumerable.GetEnumerator() {
        return subEnumerable.GetEnumerator();
    }
}

public class BaseEnumerator<TBase, TSub> : IEnumerable<TBase> where TSub : TBase {
    private IEnumerator<TSub> subEnumerator;

    public BaseEnumerator(IEnumerable<TSub> subEnumerable) {
       subEnumerator = subEnumerable.GetEnumerator();
    }

    public BaseEnumerator(IEnumerable<TSub> subEnumerator) {
       this.subEnumerator = subEnumerator;
    }

    public TBase Current {
       get { return subEnumerator.Current; }
    }

    public bool MoveNext() {
       return subEnumerator.MoveNext();
    }

    public void Reset() {
       subEnumerator.Reset();
    }
}

They work clean and fast for me but I’m no expert on the responsibilities of implementing the Dispose pattern. Warranty is not included and your mileage may vary.

[)amien

Piecing together Microsoft’s XNA gaming platform

I’ve briefly covered Microsoft’s XNA gaming platform before but have been trying to piece together what it is actually going to mean to developers.

Major update!

Sorry, had to clarify the whole commercial/non-commercial parts. XNA Framework is just a portion of XNA… that won’t apply to the big guys.

Home-brew/public

Microsoft have made it clear they want to open up XNA to interested parties which should mean home-brew software.

These guys will be using C# in conjunction with XNA Build, XNA Studio, XNA Framework and managed libraries.

Licensed developers

Licensed 360 developers get a whole set of tools and technologies that are under tight NDA just like those Sony and Nintendo insist on for their licensed developers.

These guys will be using C++ in conjunction with XNA Build, XNA Studio and no doubt 360-optimized unmanaged libraries based on Direct3D, Xinput and XACL.

Whether XNA lets Microsoft get the 360 and Windows XNA gaming platforms a little closer is anyone’s guess. Being under NDA I doubt the general public would even know.

Language

C# for home-brew developers, C++ for licensed studios.

There’s no way the licensed studio’s are going to change and C# lets the new guys get up to speed quicker.

Licensed studios will have full control, full speed and continue to use their existing libraries and middle ware.

Core framework – .Net 2.0 BCL

.NET 2.0 Base Class Library – well a modified version of it for those using the XNA Framework and C#.

Seeing as C# is the only managed language on the menu and games are rather performance sensitive expect CLS compliance to disappear and plenty of under-the-hood optimizations now that they don’t have to support other languages.

Graphics framework – MDX2

The graphics framework is based on the graphic assemblies of Managed Direct X 2.0 (MDX2) which currently ships as part of the DirectX SDK.

MDX2 is currently beta and will never hit a final release now all attention is now focused on the "XNA Graphics API" version that will see the weight drop and aim to be more cross-platform – no doubt hiding away all the platform-specific optimizations inside the libraries themselves.

There will be no System.Drawing and no System.Windows.Form name spaces so forget about them.

Audio framework – XACT

Microsoft Cross-Platform Audio Creation Tool (XACT). Grab it from the latest DirectX SDK and forget about DirectSound and DirectMusic. They won’t make it.

Input framework – Xinput

XInput kicks DirectInput off the scene with it’s easier-to-use API and support for vibration and varied controller types and voice headset.

Grab the Xinput Driver for Microsoft Common Controller, plug in the 360 controller on your PC and start messing round.

Build tools – XNA Build

XNA Build is based on MS Build but adds asset (textures, samples, music etc) tracking to the mix to ensure you don’t do like Microsoft and ship almost an entire CD’s worth of unknowingly unused assets with your Mech Commander. It also lists incremental and distributed builds on the feature list.

A March 2006 technology preview is available that integrates with Visual Studio 2005.

IDE – XNA Studio

Once again Microsoft takes from it’s existing .NET toolset and makes an XNA version – in this case Visual Studio 2005 Team System..

Whether the cheaper home-brew/XNA Framework/C# version will have all the C++ features stripped is anyone’s guess.

Live Anywhere!

Microsoft have already announced their plan to take online gaming from the Xbox 360 (gamertags, achievements, friends, chat) and make it cross-platform across to Windows and mobile gaming.

It would make of sense then that the Live Anywhere! system is .NET framework for use with XNA – even if it doesn’t officially make up part of the package.

Have to sit and wait for this one to pan out.

Platforms

Microsoft will be supporting:

  • Xbox 360
  • Windows XP 32-bit/64-bit*
  • Windows Vista 32-bit/64-bit*

Mac OS X?

Apple should really be negotiating to get XNA on Mac OS X. It already internally supports PowerPC (360, G3, G4, G5) and Intel (Windows, IntelMac) as what they offer developers seems rather weak – about the only reusable element is OpenGL.

This would allow existing XNA games to be ported with ease although would be of little use to existing Apple developers who use Objective-C/C++ and wouldn’t want to interop with C#.

Linux?

Linux already has Mono & DotGnu which could provide the basis for a .NET based gaming platform but I can’t see Microsoft wanting to open up the source for a successful Linux port.

The only obvious route here would be for Linux developers to either implement it themselves or wrap around existing technologies – Wine/OpenGL etc.

Sources & info

[)amien

Apple MacBook released – cheaper than comparable Dell

Apple have released their MacBook to compete on the low-end with similar PC laptops. I thought I’d compare it to a very similar Dell offering by using the Apple and Dell web sites…

Dell Inspiron 640m Apple MacBook
Processor Intel Core Duo Intel Core Duo
Processor speed GHz 1.83 1.83
Operating system Win XP Pro Mac OS X
Warranty 1 year 1 year
RAM size MB 512 512
RAM speed MHz 553 DDR2 667 DDR2
HD size GB 60 60
HD speed RPM 5400 5400
Optical 24xCD-RW
8xDVD
16xCD-RW
8xDVD
Screen size 14.1″ 13.3″
Screen res 1280×800 1280×800
Modem Yes No
Bluetooth No Yes
Wireless Mb 54 54
Battery Whr 53 55
Ethernet Mb 100 1,000
Height mm 38.6 27.5
Width mm 333 325
Depth mm 243 227
Weight kg 2.5 2.36
Audio-out Stereo jack Optical + stereo jack
Audio-in Mono jack Optical + mono jack
Video-out S-Video Mini DVI
Camera No Yes
Extras Works 7
5-in-1-media
iLife 06
Apple Remote
MagSafe power
Price – VAT £642.34 £637.45
Price + VAT £754.75 £749.00

That’s right, the Apple comes out at £5 cheaper.

In Dell’s favour is a .7″ bigger screen, modem and a card reader.

In Apple’s favour is Bluetooth, iLife 06, media center features, remote control, web camera, DVI and optical outputs, faster networking, bigger battery, faster RAM and a smaller and lighter package.

No doubt the anti-Apple brigade will find something to whine about but it certainly can’t be that it doesn’t run Windows.

[)amien