Showing posts with label .net. Show all posts
Showing posts with label .net. Show all posts

12 November 2015

Using the Xbox 360 Scene it controllers in Unity3d

Today, as a proof of concept I adapted the Xbox 360 Scene it controller code that I had previously written to act as an input controller for my recently completed Unity3d Space Shooter game.

The Space Shooter game is nothing special, it is the result of going through the beginner tutorial on the Unity website.

It took me a good while to figure out how to hack the code I wrote into the Unity system. Keep in mind that this is just a hack and a more robust solution is needed for a production ready system.

Here is the link to the original project. I have updated it so that it is now fully compatible with Unity3d.


For some strange reason the Unity system did not like me using asynchronous reading from the USB device. The code just hung there on BeginRead and never got anything back from the device. I had to change the code to do a direct synchronous read from the USB device to get the code working properly in Unity.

I had to make the following changes to my PlayerController class to read the Xbox 360 Big Button Controller successfully


The input code is dependent on the Update loop which makes it vulnerable to frame-rate drops and performance issues. Also it is likely that any issues with the controller code will significantly impact the performance of the game.

I ended up using the Loom technique to ensure that all updates coming from the USB device would be marshaled correctly to the main Unity thread.

For performance and speed reasons I ended up using a simple array to hold the state of the pressed buttons in each loop and using simple bit operations to read and unset the values (private Buttons[] _state = new Buttons[4];). This makes the code a little hard to read for beginners but don't worry the techniques really are quite naive.

There is no easing when dealing with the Axis values (movement). This means that all movements coming in from the Big Button Controllers will be in discreet steps and not show a nice smoothed motion like the keyboard buttons and other controllers will.

I will have to incorporate this code into a custom Input Manager to actually make this code robust enough to use in non-trivial situations. Currently I have no idea how to achieve this though.

The full Unity script

I simply attached the script to the GameController object like so

11 July 2015

Updating the ESA Small & Medium Sized Enterprises map (#3)

Since I created the ESA contractor map in November I've been surprised to see that I seem to get regular traffic to it.

I must admit that when I built this first I was not concerned with tracking how often the underlying SME database changes on the ESA side and the project doesn't really have any built in stats tracking that (yet).

Neither have I found time to fully automate the extraction and parsing of the data from the SME database. This code to produce and prepare the background data all lives in a C# application that currently I need to run manually to get the updated lists of companies and fields.

Out of curiosity I decided to run an update on the underlying data today and was surprised at the number of new and updated entries that were located.

View Latest

The Changes

All in all there where 39 major changes to the registry including new, deleted and updated companies.

I've updated the map markers and fixed a few minor bugs relating to mobile clients.

I also introduced a change list section. This section is shown in the lower right hand corner of the map and lists the changes that I have made to the map source data.

I should do this to all of my applications actually, it is really handy to track what has changed and when.

A version of this article is also available on LinkedIn

22 June 2015

Timeouts and TimeSpans

Huh? We’re waiting how long?

You don't have to work for a long time in software development to come across code similar to the following

void DoSomething()
   var data = GetData(5000);
Data GetData(int timeout)
   Thread.Sleep(timeout); // or something that takes a while

There is one major issue with this code that specifically relates to its readability and consequently its long term maintainability.

Can you spot it?

Don’t worry if you couldn’t. This is a very easy flaw to miss and is usually not uncovered until someone is tasked with maintaining or changing a block of code like this.

What is a “timeout”

The issue with the code block above is that the unit of the “timeout” value is unclear at the point of use.

5000 of what? Seconds, micro-, milli-, nano-, cpu ticks? *gasp* maybe it’s minutes! You just can’t be sure unless you check.

This one coding issue has in the past caused me to loose countless minutes in extreme code deep-diving and wading through disassembled code just to figure out exactly what unit is being implied for “timeout”.

Issues such as these can also cause more subtle and hard to track down issues related to unit mismatches as the code is extended or used by other programmers. What if one programmer is expecting milliseconds but others supply microseconds? Sometimes these issues cause multi-million dollar losses as was the case with the Mars Climate Orbiter in 1999.

For every problem there is a solution

Here are three simple alternatives that can help you avoid making or mitigate this kind of an issue and significantly improve your and your colleagues life’s in the long run.
The code examples below are presented in C# but apply to most modern programming languages.

Solution 1: Working under tight constraints

By simply tagging the relative function and parameter names with the explicit timeout unit a lot of uncertainty has been removed. If changing the function name is impossible due to legacy constraints, just changing the parameter name to reflect the unit will help immensely and can usually be done without sacrificing any backwards compatibility (exceptions to this are when relying on named parameters).

void CoolAndClear()
   var data = GetDataWithTimeoutMSec(5000);

Data GetDataWithTimeoutMSec(int timeoutInMilliseconds)

Solution 2: Having full freedom to change

The absolute best case scenario is when you’re able to modify the parameter type to be a non-ambiguous type. An example of this would be to change the timeout variable’s type from int to a System.TimeSpan struct instead.

void CoolAndClear()
   var data = GetData(TimeSpan.FromMilliSeconds(5000));

Data GetData(TimeSpan timeout)

In this case the exact unit is explicitly stated at the point of use and all ambiguity is removed. This also provides solutions for special cases such as when specifying an Infinite timeout value or special hard-coded custom values.

Note that it would not be advisable to mix both Solutions 1 and 2. Their combination would introduce even more ambiguity than before. Such as this statement

GetDataWithTimeoutMsec( TimeSpan.FromMinutes(10) )

Solution 2: Extra credit

When using the System.TimeSpan or other custom object there exists the possibility in C# to create extension methods to make the code even more concise and simpler to read

void CoolAndClear()
   var data = GetData(5000.MilliSeconds());
   // or
   var data = GetData(20.Seconds());
   // or
   var data = GetData(3.Minutes());

If you like this approach then feel free to download the skeleton code for these extension methods from my OneDrive.

Solution 3: Working under impossible constraints

In some cases you might find yourself working under almost impossible constraints where modifying any part of the code is not permitted. In those cases adding clear documentation would still be a huge improvement.

void CoolStuff()
   var data = GetData(5000);

/// <summary>
/// Gets data with a user supplied
/// <see cref=”timeout”/> in milliseconds.
/// </summary>
/// <param name=”timeout”>The timeout value in milliseconds. 
/// Value of 0 will cause the function to return immediately. 
/// Value of -1 will specify an infinite waiting period.</param>
Data GetData(int timeout)
   Thread.Sleep(timeout); // or something that takes a while

Actually come to think of it, even if you end up choosing to do either solution 1 or 2, always do Solution 3 as well.

.     .     .

This entry can also be found on Medium:

15 January 2015

Filtering the ESA Small & Medium Sized Enterprises on a map (#2)

The project discussed in this article has now been updated with newer data see post 3.

As a follow up on the work I did in an earlier post I decided to go ahead and expand my initial implementation quite a bit in an attempt to make it more useful for people looking for ESA vetoed contracting firms.

Try it

So I implemented two major enhancements:

1. Richer company info

Now when you click on a marker it will show you a much richer details about each company. Specifically its primary fields of expertise and contact information (in case you're interested in working there).

2. Filtering of companies

You can now hover over a little blue icon in the lower right-hand corner to open up a filter menu. By selecting individual filters in this menu you can limit the markers shown on the map to only companies that specialize in these fields.

Super useful. Wow

24 November 2014

ESA Small & Medium Sized Enterprises on a map

This article has been expanded and updated in post 2 and post 3.

Recently I had some business on the European Space Agency (ESA) SME Database. This database lists small and medium sized companies that have been cleared or granted privileges to work as contractors for the ESA.

The primary public access to this database is a long tabular list of company names and addresses. This data can be better presented today by overlaying it on an interactive map instead of a list. Using such a format can also make it easier for people to browse and investigate opportunities in specific areas.

So, in the spirit of being the change you want to see, I set out to create an offline parser (.NET) to source the list of companies from the SME database and then a simple PHP website to render the results onto a nice modern map (in this case Google Maps).

The change

First version is done and you are welcome to use it:

Try it

This first version is very basic. Currently the following improvements are planned

  1. More details about companies shown when the markers are clicked
         2015-01-11: Page now shows company fields of expertise as well as contact information 
  2. Filter companies by their sector and project types
  3. Select companies and print a list for future reference (in case you're job hunting)

3 June 2014

Programming Garmin Nüvi 1690 SatNav using C#

Update Oct 2015
I've now written a web-tool that automagically converts
Google Maps directions to GPX format. Its pretty awesome!

Take me to the new article

Oh dear, this is going to be one of these things that make you go d'oh.

If you just want to know how the tech bits and skip the narrative you can jump to the Solution section below.

I have a now rather ancient Garmin SatNav, a Nüvi 1690. I bought it a few years back when I realized that renting a GPS device with my rental car would be more expensive after the second time than buying a new device.

This little device has paid for itself many times over by now. However the process of getting data points to the device prior to a trip has always been a very complicated and round-about way one which I've vowed to make smoother every time I go through it.

My Process

I like to plan my medium to long trips using either Google Maps or Google Earth. These are excellent tools to find hotels, parking, places of interest and planning which roads to take. However neither of these two tools can handle the necessary GPS data formats (GPX being the most common) that the Garmin tools do. The Google tools (understandably if you remember their original acquisition source) only deal with KML and KMZ data formats.

This has required me to take the following steps:
  1. Plan points and routes in Google Earth/Maps
  2. Save waypoint data from Google Earth to KML file
  3. Convert KML file to GPX file (excellent tool for this is GPS Visualizer)
  4. Handle any data format errors
  5. Use any of the many Garmin tools to import the data to the device
    1. Sometimes I've used Google Map's "Send to GPS" feature for single points. This feature seems to have been removed from the new Google Maps.
    2. BaseCamp has very good support for importing waypoints and routes. It regretfully lacks support to delete said points from the Nüvi device though.
    3. myGarmin web page used to have an import feature with their GarminConnector plugin (but this is gone now)
    4. MapSource had some support for this (but this tool is discontinued now and cannot be downloaded)
    5. Garmin Express is simply useless when it comes to uploading waypoints (it is atrociously simple)
This time the only tool available to me was the rather nice BaseCamp tool. However I ran into some problems as I was unable to delete some previously loaded waypoints off my device. BaseCamp could not delete anything and even after deleting everything I could from the device menus there were still some points that lingered in there.

Deleting Everything

When using Windows 7 and newer the Garmin device shows up as an external USB storage drive. So after spending a very unsuccessful hour trying to clean my previous data from the device I finally decided to open up Windows Explorer and go hunting through the mounted drive.

I finally found that the GPX folder actually held an archive of my waypoints and trips that were somehow read by the device. Deleting every gpx file in this folder removed all custom points from the device (I also removed the Archive folder for good measure).

The Garmin USB Protocol

My perception of the device communication standard has always been the thing that has scared me away from actually diving in and creating a helper application. I decided to embark on trying to understand and leverage this protocol to automate my process as much as I could.

Cutting a very long story short, after numerous attempts using their Garmin SDK library and reverse engineering their .NET libraries I had made no real progress. The SDK samples I had, strangely just indicated that there seemed to be no compatible GPS device connected to my machine. This was weird as I could easily verify that the device was there by using their BaseCamp tool and the web-based GarminConnector (showed it connected and accessible).

Solution: USB Mass Storage Mode

Although embarrassing I, in desperation, went back to Windows Explorer and scoured through the device that was mounted as my G:\ drive trying to find any hints to how to access it.

I found a rather interesting file under the Garmin\ folder which was named GarminDevice.xml. Looking through this file it wasn't long until the shear stupidity of what I had been trying to do hit me.

I was already accessing the device! I had previously removed waypoints by deleting the files from the GPX directory, I could just as well have added a file in there with my new data!

And sure enough, this is indeed the case for a large range of Garmin devices. Below is an explanation of the relevant GarminDevice.xml section:
The GpsData DataType element also contains a File element with TransferDirection=InputToUnit which specifies a file type, extension and file path. The application places a GPS Exchange (.gpx) file containing routes, tracks and waypoints in the directory path specified. This file will be processed by the GPS device upon exit from mass storage mode.
This has reduced the problem to a simple FileIO and XML parsing exercise which is trivial to implement using any modern programming language (e.g. C# or Python).

Stay tuned for my new app :)

24 February 2012

Why I Think C# and Java Trump C and C++

Well the title is a bit sensationalist. As a disclaimer I strongly believe that you need to pick the correct tool that best suits the job at hand and that you believe you will be the most productive in.

However when faced with the agonizing task of printing out Trace messages with variable formatting in a bit of C code that I had lying around recently, I realised just how focused C# is on getting things done rather than faffing about with boilerplate code.

As an example, here are two functions that fundamentally do the same thing, print a formatted string to the system Trace listeners using a single format string and then a variable number of arguments:

First the C# function:
void odprintf(string format, params object[] args)
    Trace.WriteLine(string.Format(format, args));

Compared to the C equivalent:
void __cdecl odprintf(const char *format, ...)
    char buf[4096], *p = buf;
    va_list args;
    int n;

    va_start(args, format);
                             // buf-3 is room for CR/LF/NUL
    n = _vsnprintf(p, sizeof buf - 3, format, args);

    p += (n < 0) ? sizeof buf - 3 : n;
    while ( p > buf  &&  isspace(p[-1]) )
            *--p = '\0';

    *p++ = '\r';
    *p++ = '\n';
    *p   = '\0';

Really, all this magic is necessary to just print to Trace?

I reserve the right to seriously doubt that "lower" level languages have any place but in highly specialised or those extremely few cases where throughput and latency are more important than developer productivity and deadlines are lax and forgiving.

Here is someone that is more eloquent than I am on this issue.
There is no reason to use C++ for new projects. However, there are existing projects in C++ which might be worth working on. Weighting the positive aspects of such a project against the sad fact that C++ is involved is a matter of personal judgement.

If you end up working with C++, don't try to "fix" it (or "boost" it). You'll just add more layers of complexity. The most productive approach is to accept the problems and try to write simple code which people can easily follow.

If you are an expert in the intricacies of C++, please consider this knowledge a kind of martial art - something a real master never uses.

17 February 2012

Bit manipulation gasp, gasp

Call me an elitist if you want, but it still surprises me how reluctant programmers are to use basic bit operations.

As an example, let's say that you have a series of calls to functions that might return true or false and you want to call every single one of them but if one of them returns true you want to preserve that value at the expense of the other false values.

For such a problem I am more likely to encounter code written similar to this:
bool anyreturntrue = false;
for( a few calls )
    bool result = SomeFunction();
    if( !anyreturntrue && result)
        anyreturntrue = result;
return anyreturntrue;

rather than the more elegant:
bool anyreturntrue = false;
for( a few calls )
    anyreturntrue |= SomeFunction();
return anyreturntrue;

Should this really be considered wizardry?
I hope not.

16 February 2012

Calculating x^y

I stumbled onto this blog (Antonio Gulli) the other day and read through a couple of his suggested programming problems.

Kind of interesting to tackle that power function. After coming up with a rather naive solution to begin with, I attempted to improve on it and ended up with the following implementation for X^Y that works when X and Y  are integers. As I wanted to keep it rooted in the .NET framework implementation I needed to use the BigInteger class so the function will not return a value for negative Y values just yet :)

What do you think?

using System;
using System.Numerics;

internal class Program
    private static void Main(string[] args)
        int x = 2;
        int y = 2000;

        BigInteger pow = Pow(x, y);

        Console.WriteLine("{0}^{1}={2}", x, y, pow);

    private static BigInteger Pow(int x, int y)
        if (y == 0) return 1; // Zero power
        int ay = Math.Abs(y);
        BigInteger pow = 1;
        BigInteger sum = x;
        if (ay > 0)
            var iterations = (int) Math.Ceiling(Math.Log(ay, 2));
            for (int i = 0; i <= iterations; ++i)
                if (ay == 0) break;
                if (ay%2 != 0) pow *= sum;
                sum *= sum;
                ay = ay/2;

        // Handle negative powers (Well this will work when we have BigDecimal in System.Numerics)
        return y < 0 ? 1/pow : pow;