Archive for the ‘Computer’ Category

Magic Rescue

Sunday, December 13th, 2009

I needed to recover some photos from a memory card which had been wiped clean. After some basic research, I installed magicrescue. It is a command line program for Linux, and worked very well. It was able to recover 448MB files from a 512MB memory card.

Installing was simple using synaptic package manager in Ubuntu 9.10, then I had to plug in the memory card and find out what the device name was (Note: this is not the same as where the drive is mounted). For this I used the System > Administration > Disk Utility and found that it was /dev/sdc1

A folder was created to house the rescued photos,

mkdir ~/rec

Then the magic program run.

sudo magicrescue -r jpeg-exif -d ~/rec /dev/sdc1

About 4 minutes later, 160 photos were rescued. Now that is magic!

substr versus direct character reference in php

Saturday, December 13th, 2008

In php there are a few ways to get a few characters from the middle of a string. One is to use substr:

string substr ( string $string, int $start [, int $length  ] )

another is by direct character reference:

string $chars = $string[0].$string[1];

When you only require a single character from a string, one would assume that direct character reference is quickest, and lots of characters are required, the overhead of a function call would be offset against the many string lookups.

One of the projects I am working on at the moment required the unpacking of fairly long strings, sometimes over 1024 bytes. Each byte dictated what the next few bytes are, and so every byte has to be analysed. Therefore I setup a simple test case to find out what the fastest way to do this was.

First, a simple timer function was needed. Here is the code I wrote a long time ago, and used many times.

function timer($stime=0,$btime=0){
   $time = explode(' ', microtime());
   $time = $time[1] + $time[0];
   if($stime){
      return round(($time - $stime - $btime)*1000,3);
   }
   return $time;
}

If you call this function with no arguments, it returns the current time in seconds, to many decimal places. If you then give this time value back to the function later, it will tell you the time difference in milliseconds

A single request for each of above methods would not suffice to compare them, and so each method is done many times within a for loop. This for loop has a fair amount of overhead, and so first the base time is calculated by running an empty for loop.

$num = 1000000;
$s = timer();
for($i=0;$i<$num;$i++){}
$base = timer($s);
print "Loop test took ".$base." milliseconds\n";

When executed, we find that the for loop takes around 225 milliseconds to complete 1 million iterations.

Now for the actual tests (this one is for four characters, starting from the third character):

$s = timer();
for($i=0;$i<$num;$i++){
	$var = substr($input,2,5);
}
print "substr() test took ".(timer($s)-$base)." milliseconds\n";

$s = timer();
for($i=0;$i<$num;$i++){
	$var = $input[2].$input[3].$input[4].$input[5].$input[6];
}
print "char ref test took ".(timer($s)-$base)." milliseconds\n";

This outputs something like:

Loop test took 230.988 milliseconds
substr() test took 728.633 milliseconds
char ref test took 1263.388 milliseconds

so we can see that for extracting four characters, using substr is faster. The same is not true for one and two characters, as the following results show (averages of four runs):

Number of chars      substr() time/ms    direct char time/ms
   1                  713                 358
   2                  770                 630
   3                  753                 945
   4                  751                 1280
   5                  733                 1557

It can be seen that on my development system, using direct character reference is faster for two or less characters, and substr() is faster for three or more characters.

My new phone – a Nokia N95

Saturday, February 16th, 2008

It was that time of the year again when you eagerly await the time when you can ring up the phone company that supplies your contract and ask them what they can offer you as an upgrade. Or perhaps not. I’m not a big fan of talking to the phone networks as they tend to have operators which do not have the best command of the English language, and simply want to extract money from you.

Vodafone logoI took a different line with them this year. I had two phone contracts: One with T-Mobile for my laptop datacard at £20 per month, and another with Vodafone for my normal phone at around £18 per month, and wanted to combine the two and pay less overall. I knew exactly what I wanted, and even which phone I wanted: a Nokia N95, so when I rang Vodafone, rather than asking ‘what can you do me’ I stated what I wanted, and asked them if they could provide it. They wanted to charge me £40 per month, and £90 for the mobile phone, as well as bind me into an 18 month contract. Having done my research, I knew that T-Mobile offered the deal I wanted for £32.50 per month, although there was also a one-off fee of £75 for the phone. I also knew that Vodafone offered what they were offering me to new customers for only £35 per month. I was annoyed – is that what they call customer service? Charging a customer who has been with them for 6 years more than a new customer? Apparently so. I told the gentleman from Vodafone that I would be leaving them, and requested my transfer code so I could go to T-Mobile. He said ‘Ok, I’ll transfer you to the leaving department’. There I explained again the predicament that I found myself in, and I was serious about leaving. However, he offered me what I wanted, and even put a cherry on top for me. Enough free mins, enough free text messages and data transfer, and even a free N95! All for £27.50 per month. That’ll do!

Nokia N95I’ve had my N95 a few weeks now, and am pleasantly surprised. It is packed with features, including GPS and Wifi, and although some of the things that used to be easy with earlier Nokia’s were tucked away behind strange menus. it is fairly easy to use and I am pleased. However, it is not all a bed of roses, and the major complaint I have is the battery life: I have to charge it at least every two days, and every day if I use it much. The applications, GPS and camera all take a big chunk out of the battery, and should only be turned on when necessary. Otherwise at the end of a night out taking lots of pictures you won’t have any battery to ring for a taxi home, and won’t be able to rely on the Nokia GPS to guide you either.

Vehicle tracking application

Tuesday, June 12th, 2007

For the past few months I have been working on a project codenamed motortrace which is a piece of server software which allows real time tracking of cars, caravans and other equipment. Previous journies can be viewed and animated on the map, and detailed reports generated. Driver mileage and speeds can be monitored and analysed.

The software is suitable for most tracking applications, from wanting to know the current whereabouts of a single caravan or cherised vehicle to managing a whole fleet of trucks.

An image of motortrace vehicle tracking

An image of motortrace report generation

An image of motortrace tracker list

This is an exciting project to be part of, and because the mapping engine uses the Google maps api, in order to comply with the Google api the website is free to use! It should soon be possible to sign up for a free account and start tracking your car from your PC or mobile phone. Watch this space!

Mobile vehicle tracking

Radiused corners without resorting to images in CSS

Sunday, March 25th, 2007

Most browsers still do not display CSS3 rounded corners (border-radius), although Mozilla Firefox has implemented its own version of corners, -moz-border-radius.

This is an attempt to recreate rounded corners, which work on most browsers without resorting to images.

The theory is that if you nest a few div’s each with differing borders, then you can create a rounded effect. The upside it that most browsers respect border widths, but the downside is that you end up with more div’s in your html than you really need. So much for separating content and style.

Corner_idea

Individually:

A 75×75px div with a 15px left and top border, and a 60px bottom and right border

A 30×30px div with a 15px left and top border, and a 30px bottom and right border

A 0×0px div with a 15px left and top border, and a 15px bottom and right border

When Nested:

Make the colours the same:

A more useable size

Whilst the large one is nice, and we can even make circles by putting 4 of them together, the application in my mind needs to make them somewhat smaller

It is important that the ratio between the div widths/heights and border widths stays the same. Here each value is 2/5 of the one above. (i.e. 30px square)

Or even smaller…(although smaller ones may as well be made with only 1 nested div, this one has 3)

Or even smaller…

One for each corner

Now, all that is needed is to make 4 of these things, and put them in each corner of a page.

This is a heading

This would be some normal text that would be quality…

…quite unlike this waffle that I am typing to fill the space

©2005 handyandy.org.uk

Channel four on demand – 4OD

Tuesday, March 13th, 2007

4OD logoAfter missing “The great climate change swindle” on channel 4 yesterday I decided I was going to watch it on 4OD, hoping that it might help me with an essay that I have to write for tomorrow as part of an energy studies course.

I visited the channel four website and find that they do not support anything other than windows. Helpful, when my desktop doesn’t ‘do’ windows.

4OD requirements

I fire up my laptop, connect it to the internet and begin downloading the executable from the 4OD website. Partway through the download my hard drive decides that it is full, so I delete a load of files that I don’t need. I really must get myself a bigger drive.

installing .net frameworkAfter successfully downloading the 4OD executable, I run it. It decides I need the .net framework, and proceeds to download that (not a small download at nearly 200MB). After taking an age to install the .net framework which I never asked for, I get told that I need to update windows. Something to do with needing the latest media player to play the 4OD stuff. In order to download the latest media player, I needed to install Windows Genuine Advantage (don’t get me started about this, I hate the way micro$oft assume everyone guilty until proven innocent, and can get away with putting spyware on machines under the guise of ‘genunine advantage’).
With all this software installed, my computer decided It needed a reboot, because clearly updating a media player is such an integral part of the system that it needs to reboot. (Another one of the many reasons why I don’t like windoze: “A flea on the other side of the world has just farted, please reboot”)

After the reboot, I was informed that I needed to download some more spyware – DRM software. The dilogue box gave me no explaination of what DRM was, but I decided to let it anyway as I guessed 4OD would not work without such big brother tactics.

Eventually after another reboot, the 4OD service was working, and I could click on the icon and get a full screen of a channel 4 website, with the option to download films. I searched for “The great climate change swindle” and was greeted with a ‘rent for 99p’. After spending 4 hours trying to install the silly software, I think I deserved it free. However, by the time I had installed 4OD I had already managed to find the same film on youtube, and watched it.

drm

So was the effort worth it? No. I now have a computer that is installed with lots of spyware, and software that does not want to remove itself. When I install a bigger hard drive in my laptop (hopefully very soon) 4OD is not on my to-install list – in fact, I will steer well clear of it. Bill Gates will be lucky if I even bother to put windoze on it, even when I have a legal licence!

Internet Explorer on Ubuntu

Saturday, February 17th, 2007

Internet Explorer (wine) icons in a Linux environmentYou might well ask why – well, I design websites for a living, and unfortunately the majority of the world is uninformed and still use the inferior and buggy browser M$ Internet Explorer, on top of the expensive and buggy operating system M$ Windoze.

Often when I design, I get it looking right in firefox, then take a peek in IE to look how bad it can get. On a regular basis, I find that IE decides that it can’t understand the ’standard’ code, and it just does whatever it wants to.

With this in mind, I set about installing IE6 on my linux ubuntu desktop.

I already had wine installed, and I found an excellent little script called ies4linux which I downloaded, ran and it all worked! It seemed to easy to be true, but it works!

There is an excellent tutorial on Ubuntu Geek which I found helpful and would recommend.

Bike Project – A data logger on a budget

Friday, February 9th, 2007

We have been wanting to do some coast-down tests on the recumbent bike for a while in order to find out the drag coefficient, but have been unable to collect the relevant data. What you need is speed against time – on a graph then the gradient would be accelleration, and the area under would be distance.

Bike ComputerWe thought of a number of ingenious ways to do this, but none seemed very simple. Here is a quick summary of ideas we had.

  • Using a cycle computer, shout out the speed at regular intevals to someone writing them down. We decided this was impractial.
  • Put pressure sensors in the road at regular intevals connected to an old computer. The computers that we had data loggers for were old bbc’s – and they don’t transport on a bike very well unless we had 100m of power cable!
  • Using a camcorder, record the speed on the cycle computer over the journey. The problem with this was finding a suitable mouting point on the bike for the camera (I spent ages hunting around at uni and shops for a 1/4 inch witworth thread, before I found one in my junk box). After thinking some more about this, we came to the conclusion that the cycle computer must average the speed over some time, and so the data is not as accurate as it could be.
  • Mount some bright flags on some of the spokes, and then mount a camcorder on the bike. After filiming the motion of the wheel over the run, step through the frames and record the positon of the flags. Aside from the issue of mounting the camera over the wheel, I didn’t fancy stepping through (3 runs x 6 speeds x 30fps x 60seconds) 32400 frames and marking the position of flags. I am sure it would be possible to write a program to do this, but just don’t go there.

It seemed like what should be a simple task was getting needlesly complicated, until I had an idea.

Some codeAt home I found an old wireless mouse, and with the middle finger scroll butted against the wheel and some gaffer tape around the bike, I span the wheel. A few lines of code later on my linux box, I had a lovely list of numbers in front of me!

I plotted the numbers, and found that they didn’t plot well. The mouse was trying to scream ‘I’ve just moved’ about 8000 times a second, and something didn’t like all that screaming.

My bike wheel is 700mm in diameter, and the mouse scroll wheel was about 20mm. At 30mph my bike wheel spins 6.1 times per second, and the poor mouse wheel would be spinning at 214 revs per second. That 12810 rpm, and it is no wonder something couldn’t cope!

Modified mouseGoing back to the drawing board I decided that I must take a mouse apart. Not wanting to destroy my nice wireless mouse, I found an old ps2 mouse which I soon had in peices. I soldered a few more wires and a reed switch onto it, and did a bit of jigery pokery with magnets on my bike and hey presto, I had a data logger!

The s-video port on my laptopIf only life was so simple. I turned my laptop on, transferred the few lines of code I had written to collect the data, and plugged my mouse in – or at least I tried to. Apparently ps2 was not on the menu when my laptop was ordered, and so that port that I had never used on the back of my lappy which I thought was ps2 on closer inspection turned out to be a s-video out.

usb to ps2 converterBack to ebay, and a usb to ps2 converter solved the problem. It now all works well! Now to get this gear on the recumbent.

Now for the next issue – There is nowhere to put a bag on a recument, because your back is against a seat. It is possible to put a small bag on your front, but your knees bang it every cycle. Not really the treatment for a laptop, but hey, anything for science. Lets do it!

Not so fast! Overnight it had snowed. I couldn’t take the recumbent out in the snow – it is unstable enough in the dry, especially at the kinds of speeds I was hoping to reach.

Tomorrow, Tomorrow, Tomorrow. Maybe one day.

Getting rid of U3 malware on my memory stick

Wednesday, January 10th, 2007

Photo of my memory stickEvery time I put my Sandisk Titanium cruzer in a Windows computer, I have to wait about 30 seconds for some U3 software to load. Most tied-down PC’s like the ones at uni do not allow this program to run correctly and so it is completely useless.

If there was a way to turn this off or easily uninstall it, I wouldn’t mind, but there isn’t. As far as I am concerned this U3 software is malware. It installs itself without my consent, and there is no designated way to remove or deactivate it.
The problem stems from the flash drive setting itself up as both a removable flash drive, and an extra fake CD drive. This fake read-only CD drive is a small partition on the memory stick, and contains the autorun files for the U3 system.

U3 Software logoAfter reading a number of forums on the net, I found an exe that reformats the whole drive, steamrollering the CD partition. After downloading and running it, I now have a 6MB larger stick and a memory stick that works – All I wanted in the first place.

Those extra 6MB (and another 300 MB) have been nicely filled by Portable Apps – A clever set of programs that allows me to run Firefox, OpenOffice, Thunderbird etc straight from my memory stick on most windows computers. It even works on the uni computers, despite the removable drive not being fully mounted due to a lack of administrator privileges.

phpSANE – a total rewrite

Friday, January 5th, 2007

I made a number of improvements to phpSANE a few years ago, but forgot to submit them back to the wider community. I was told by a few users that the new preview function was hard to use, so decided to improve it and share the code.

First of though, what is phpSANE? It is a php front end for the SANE (Scanner Access Now Easy), which is a scanner/image protocol a bit like TWAIN, but for Linux. Its massive advantage is it allows a scanner to be used from any networked computer via a web browser.

After looking at the existing code, I decided that it would be quicker to start from scratch, creating an interface that allowed the following:

  • Ability to switch between connected scanners
  • Settings based on the scanner selected
  • Scalable previews
  • Drag and moveable preview selection
  • Help fields for each setting
  • History of images scanned

After plugging in a new scanner and installing sane

$sudo apt-get install sane

apache2 and php5 were already installed, and so there was no need to install them. If you were to install them, it would be something like this:

$sudo apt-get install php5 php5-gd apache2 apache2-common

The SANE program creates .pnm files (a bit like like .tiff’s) and so there is also a small utility called netpbm which will contains the command pnmtojpeg which is needed.

$sudo apt-get install netpbm

Next job to is to test it all works: This should warm up the scanner, and create the image file /tmp/image.pnm

$scanimage > /tmp/image.pnm

Unless logged in as root, this will return an error as only root (or sudo) has the permission to run scanimage. The apache user, (e.g. www-data, apache, nobody) will need permission to run scanimage.

Create a group called saneusers, and add the apache user to the group.

$sudo visudo

At the end of the file, add this line:

%saneusers ALL=NOPASSWD:/usr/bin/scanimage

Running the next command should now work without any errors:

$scanimage > /tmp/image.pnm

Now for the programming to start.