Compare version strings with InnoSetup

I recently had to implement version string comparison for InnoSetup, because the solutions that I found on the internet did not suit my needs. Hopefully this will be useful for somebody else.

// Procedure to split a string into an array of integers 
procedure Explode(var Dest: TArrayOfInteger; Text: String; Separator: String);
var
  i, p: Integer;
begin
  i := 0;
  repeat
    SetArrayLength(Dest, i+1);
    p := Pos(Separator,Text);
    if p > 0 then begin
      Dest[i] := StrToInt(Copy(Text, 1, p-1));
      Text := Copy(Text, p + Length(Separator), Length(Text));
      i := i + 1;
    end else begin
      Dest[i] := StrToInt(Text);
      Text := '';
    end;
  until Length(Text)=0;
end;

// Function compares version strings numerically:
//     * when v1 = v2, result = 0  
//     * when v1 < v2, result = -1  
//     * when v1 > v2, result = 1
//
// Supports version numbers with trailing zeroes, for example 1.02.05.
// Supports comparison of two version number of different lengths, for example
//     CompareVersions('1.2', '2.0.3')
// When any of the parameters is '' (empty string) it considers version number as 0
function CompareVersions(v1: String; v2: String): Integer;
var
  v1parts: TArrayOfInteger;
  v2parts: TArrayOfInteger;
  i: Integer;
begin
  if v1 = '' then
  begin
    v1 := '0';
  end;

  if v2 = '' then
  begin
    v2 := '0';
  end;

  Explode(v1parts, v1, '.');
  Explode(v2parts, v2, '.');
  
  if (GetArrayLength(v1parts) > GetArrayLength(v2parts)) then
  begin
    SetArrayLength(v2parts, GetArrayLength(v1parts)) 
  end else if (GetArrayLength(v2parts) > GetArrayLength(v1parts)) then
  begin
    SetArrayLength(v1parts, GetArrayLength(v2parts)) 
  end; 
  
  for i := 0 to GetArrayLength(v1parts) - 1 do 
  begin
    if v1parts[i] > v2parts[i] then
    begin
      { v1 is greater }
      Result := 1;
      exit;
    end else if v1parts[i] < v2parts[i] then
    begin
      { v2 is greater }
      Result := -1;
      exit;
    end;
  end;
  
  { Are Equal }
  Result := 0;
end;

To test the code in InnoSetup, you can use the following function:

procedure TestVersions(v1: String; v2: String);
begin
  Log(v1 + ' : ' + v2 + ' = ' + IntToStr(CompareVersions(v1, v2)));
end;

And call it with some parameters:

  TestVersions('1', '2');
  TestVersions('2', '1');
  TestVersions('3', '3');
  
  TestVersions('1.1', '1');
  TestVersions('2.1', '1');
  TestVersions('1.1', '2');

  TestVersions('2.12.11', '2.12.25');
  TestVersions('', '2.12.25');
  TestVersions('2.12.25', '');
  TestVersions('', '');
  TestVersions('2.12.11', '2.012.11');

A practical example can be used as follows:

if CompareVersion(currentVersion, newVersion) = -1 then
begin
  //do the upgrade
end

Windows 7 on Acer E1-530

My wife’s Acer E1-530 is a nice laptop which comes with Windows 8 – rather a big misunderstanding by Microsoft of how proper OS should work. Windows 8 is OK for tablets, but it’s absolute rubbish for laptops without touchscreen. For no reason whatsoever I had to completely reinstall it twice, because after some time it failed to boot due to some mysterious error. The only option was to reset it which fixed it for a few weeks. After the boot problem happened for the third time it was time to load Windows 7 on the laptop.

The process is fairly simple, you just need to go to BIOS (keep pressing F2 during boot screen) and enable legacy boot mode instead of UEFI. Acer does not provide Windows 7 drivers on their website for download, but all drivers are in the C:\OEM\Preload\Autorun\DRV hidden folder. Just stumbled on this blog post which turned out to be very handy by providing the tip on where to find drivers for the Acer E1-530! The only missing driver is the “USB eXtensible Host Controller” which you can download from my blog:

USB_Intel_1.0.10.255_W7_April_2014

I replaced the HDD with an SSD and Windows 7 on the laptop and now it’s become a very fast and nice piece of kit.

Visual Studio 2013 hangs after each debug session

Stumbled upon this issue when Visual Studio 2013 Update 4 would hang after each debug session. Very annoying especially when you want to quickly run the application test something, fix code and back to the beginning of this cycle. The hanging would occur for about 15-30s every time I would close the application and debug session ends. To resolve this I tried a lot of solutions suggested by Google which didn’t help:

  • Resetting Visual Studio 2013 settings via “devenv.exe /ResetSettings”
  • Deleted *.suo file
  • Rebuilt new solution file with all projects
  • Even disabled internet as I was suspecting some sort of network communication issue was causing this
  • Attempted to disable git integration with Visual Studio by renaming .git folder to _git as a temporary solution
  • Tried to disable IntelliSense with no luck

The only solution that worked for me was to disable Visual Studio host process for the project. To do this on the Project menu, click Properties, then click the Debug tab and uncheck the Enable the Visual Studio hosting process check box.

Hope this post saves a few hours messing around with settings for somebody trying to resolve this issue.

Cheap USB MIDI cable: some self assembly may be required

USB-MIDI-Cable

I had recently bought a Rock Band 3 Wii keyboard which has come down in price to around £10 with an intention to connect it to a cheap Chinese USB to MIDI interface cable for around £3 pictured above to do some MIDI experiments on my computer. Videos online demonstrated that connecting the IN connector to the keyboard and USB cable to the PC was all that was needed to use it, but I just couldn’t get it working. The Rock Band keyboard detected a MIDI cable and switched to MIDI mode, PC properly detected USB MIDI device, but there were no messages in the MIDI-OX application. That’s when I decided to open up the plastic casing to find a few surprises…

Continue reading Cheap USB MIDI cable: some self assembly may be required

Problems with Samsung Series 5 Ultrabook and USBTinyISP

Well, this is somewhat disappointing… I recently acquired a Samsung Series 5 530U3B Ultrabook which is really nice and was going to make my main laptop for on the work on the go. Unfortunately it supplies insufficient power to my USBTinyISP programmer. The effect is that when I try to program ATTiny45, I get this error:

Binary sketch size: 2,752 bytes (of a 4,096 byte maximum)
avrdude: verification error, first mismatch at byte 0x0040
 0x02 != 0x36
avrdude: verification error; content mismatch

The verification fails on random memory addresses. This means that I will need to add an external power supply to my custom programmer or find a USB hub with external power supply. I think that the latter will be the easiest.

Repairing Syma S031G helicopter

A few weeks ago I found this boxed Syma S031G helicopter in a ModelZone store on a special shelf where they are selling broken or otherwise non working gadgets. There were more of those broken helicopters for sale, but they were in a range of £30 and were listed as having RC transmitter problems or other faulty parts. It wouldn’t have made sense buying one without investigating if repairs wouldn’t even cost more than a new heli retailing for £59.99 at the same store (or 30% cheaper on eBay). Mine only had a handwritten sticker “broken” stuck to it and a price of £9.99. A quick inspection through the box didn’t show any signs of significant damage and shop assistant was kind enough to let me open the box and check if it had all parts. Transmitter, charger, heli, instructions and even spare rear propeller was in the box so I decided to take the risk and bough it.

Continue reading Repairing Syma S031G helicopter

A world of free IC samples

The world is full of free integrated circuit samples, you just need to know where to look for them. Today I got a very nicely packaged batch of samples from Maxim Integrated. All I had to do was ask nicely :)

I filled all relevant information four days ago and today I have 2 x MAX7219CNG and 2 x MAX7221CNG LED display drivers. They retail at about £8 and £13 without shipping so it’s about £42 of free stuff! Will be using them to control 8×8 LED matrices I got a while back from Rapid Online.

Thanks Maxim Integrated!

Repairing Alba CR305IP FM clock radio alarm

I bought this Alba FM clock radio a very long time ago at a car boot sale for £2. Seller was selling a bunch of them. It had everything in the box: power supply, cables, various iPod dock plastic bits and even AV cable. Took it home, plugged it in and… it was working, but no audio at all. I could control the iPod, set the clock and etc, but it just did not make any sound. Well, you know… it was just £2 :) So it was left in a cupboard for a very long time until today I decided to take it out and have a look at what’s inside.

Continue reading Repairing Alba CR305IP FM clock radio alarm

Using USBTinyISP to program ATTiny45 and ATTiny85

There are a few different ways how to program ATTiny microcontrollers – lovely little chip with so much potential. I tried programming with Arduino Uno, but the solution was too clumsy with lots of wires connected to the breadboard from Arduino. Then I remembered that I have USBTinyISP and apparently it is one of the best ways to program these chips.

Continue reading Using USBTinyISP to program ATTiny45 and ATTiny85