Stopping ā€œNo Diskā€ Error Pop-up In Windows

While working on a project that used the Microchip C18 compiler I got this error message:-

If I clicked ā€œContinueā€ it would appear again instantly, and this kept going dozens of times, the compile worked fine but the error message was very annoying. The drive in question was a partition on my main hard disk, it was not a removable drive!

After a bit of research I found out how to suppress the error message by modifying the system registry. Below is an example of the .reg file I created.

Windows Registry Editor Version 5.00

[HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Windows]
"ErrorMode"=dword:00000002

This will disable all application and system error pop-ups, which may not be what you want so what I did is create two reg files, on disables the errors, the other enables the errors. This means I can temporarily disable the errors when I want.

For more information on this see this Microsoft support page.

WARNING! Modifying your system registry could be dangerous, proceed with caution. I am not responsible for any damage you cause to your system!

I recommend creating a system restore point before making any modifications to the system registry.

   

Changing File Permissions in Cygwin (Windows 8)

I tried to change the permissions of my ssh private/public keys in the .ssh directory under Cygwin on Windows 8 and it didnā€™t work. This seems to be a bug in Cygwin but there is a work around that I found throughĀ SuperUser.

chgrp -R Users ~/.ssh

Now all you need to do is run the chmod commands as usual and all should work.

 chmod 644 id_rsa.pub
 chmod 600 id_rsa
     

Handling SIGUSR1 in GDB

I was debugging a project that used SIGUSR1 heavily, GDB stops on SIGUSR1 by default and it was making debugging a pain when I didnā€™t care when the signal was being generated. So here is now to set how GDB interprets signals.

By entering the following into the GDB prompt you can instruct it to not print, or stop when the signal happens and to pass it to the program.

handle SIGUSR1 nostop noprint pass

     

Extracting Audio From MP4 Videos

Iā€™ve been asked to help someone create a device that can play steam train whistle sounds when a button is pressed. I was given a load of MP4 video files with recordings of the whistles.

FirstĀ I wanted to rename them because they all had a ā€œPrj ā€ prefix on them. The code snippet below is what I used to remove the prefix from the file names.

for file in *.mp4 ;
do
    mv "$file" $(echo "$file" | sed 's/Prj //')
done

The next thing to do is extract the audio from all the videos. FFMPEG is perfect for this. The Bash code below will extract the audio fromĀ all of the MP4 files in a directory.

for file in *.mp4 ;
do
    ffmpeg -i $file -map 0:1 -acodec pcm_s16le -ac 2 ${file/.mp4/.wav}
done

Now that Iā€™ve got the audio files I need to open them in Audacity and edit them, that is something I canā€™t automate unfortunatelyĀ šŸ˜¢

         

Enabling I2C on Raspbian

This is a quick guide to enabling I2C support on the Raspberry PI operating system Raspbian. This may work on the other Linux based distros available for the Pi but I have not checked.

sudo apt-get update
sudo apt-get install python-smbus

Next we need to enable the kernel drivers forĀ Ā I2C, by default the drivers are blacklisted so we must un-comment the lines that include ā€œi2c-bcm2708ā€

sudo nano /etc/modprobe.d/raspi-blacklist.conf

Then add the kernel module ā€œi2c-devā€ to /etc/modules

sudo nano /etc/modules

Then finally reboot the Pi

sudo reboot

Using the I2C Command Line Tools

The i2c-tools package provide a number tools for operating on the I2C bus using the command line. The first is i2cdetect which scans the bus for devices and then prints an address map.

i2cdetect -y 1

The next is i2cset, this is a tool that allows you to set register values on an I2C device. The first argument after the bus is the chip address, then the register address and finally the value to write. All of the arguments are in decimal.

i2cset -y 1 <chip> <addr> <value>

The opposite to the set command is i2cget, this reads a register on an I2C device.

i2cget -y 1 <chip> <addr>

Another tool is i2cdump which reads out all of the registers of a I2C device.

i2cdump -y 1 <chip>
         

An Analogue Clock in Processing

I came across some code I wrote in Processing a while ago so I thought Iā€™d post bits of it here. I made a really simple analogue clock. Below is s screenshot of what I came up with.

I wrote a small function that handles drawing a hand of the clock. This function is called for each hand of the code by the draw loop.

void draw_hand(long count, long modulo, long len)
{
    float nx, ny;
    a = (2*PI/modulo) * count - (2*PI/4);
    nx = (width/2) + (Clock_Size/2-len) * cos(a);
    ny = (height/2) + (Clock_Size/2-len) * sin(a);
    strokeWeight(7);
    line(width/2, height/2, nx, ny);
}

Then all we need to do in the draw loop is call the function for each of the hands of the clock. Here is an example of drawing the hours hand.

To put the ticks around the outside of the clock I used the code below.

  fill(0,0,0);
  for( int i = 0; i < 12; i ++ ) {
    float angle = radians(i*30) - (2*PI/4);
    nx = (width/2) + (Clock_Size/2-35) * cos(angle);
    ny = (height/2) + (Clock_Size/2-35) * sin(angle);
    text(i == 0 ? 12 : i, nx, ny);
  }

Processing.js Version

I have got this code working fine using Processing.js. See it here

     

Creating a Virtual Serial Port in Linux

When developing software that uses a serial port for communication with the outside world it is really helpful to be able to use a virtual serial port and write test code to aid in debugging. Well it turns out there is a program that does that in Linux, itā€™s calledĀ socat. Using this utility you can create a pair of virtual serial ports and connect them together. To install it in Ubuntu run the following commands.

sudo apt-get update
sudo apt-get install socat

Once you have the socat program installed you just need to run the following command to create a pair of virtual tty devices. When you run the program it will print out which ports you need to use.

socat -d -d PTY,b57600 PTY,link=ttyVS1,b57600

This is an example of the output that socat produces. The two ports it has created are /dev/pts/5 and /dev/pts/6. These can vary from time to time so keep an eye on that.

2013/11/15 07:47:13 socat[2970] N PTY is /dev/pts/5
2013/11/15 07:47:13 socat[2970] N PTY is /dev/pts/6
2013/11/15 07:47:13 socat[2970] N starting data transfer loop with FDs [3,3] and [5,5]
   

Notes on Implementing a Timer

Iā€™m currently working on a project at work that includes a timer that will turn relays on during certain time periods and off outside them. The timer wasnā€™t nearly as easy as I thought it would be so I decided to prototype the code on the PC as itā€™s a lot easier to debug than on a tiny little PIC18F. All of the code in this post will be in C# which I what I used to prototype with.

The weird thing about this timer is that the day is assumed change at 3am and not midnight. So a day starts at 3am and ends at 2:59am, this is apparently so that the relay can be on across midnight. This caused massive confusion and I struggled to implement it.

The idea is that the timer will store and work with an internal representation of time and then convert to hours/minutes only when displaying settings to the user and when getting the time from the RTC. This internal representation is a single integer number of minutes from midnight offset in such a way that times less than 03:00am are offset by a day. This means that 03:00am is 180 minutes as you would expect but 02:55 is actually 1615 minutes. Using this representation means that comparing time is really easy and that makes the whole timer and user interface much easier to implement. Below are some constants

const int MinutesPerDay = 1440;
const int MinInternalTime = 180;
const int MaxInternalTime = 1619;

Below is a function that converts hours and minutes to the internal representation. If the time is less than the minimum internal time then itā€™ll add a whole day to the time.

int TimeToInternal(int hrs, int min)
{
    int time = hrs*60 + min;
    if( time < MinInternalTime) {
        time += MinutesPerDay;
    }
    return time;
}

Next I needed a function to convert the internal representation to actual time, in hours and minutes. Here the time is returned as a tuple of two integers, in the C version I used two pointer parameters instead.

Tuple<int, int> InternalToTime(int time)
{
    if( time > MinutesPerDay ) {
        time -= MinutesPerDay;
    }
    return new Tuple<int, int>(time/60, time%60);
}

I have also implemented a similar timer elsewhere in the project, but it was much simpler and the day changed at midnight as you would expect. This is the code I used to implement the timer that turns on a feature during a time period, then turns off. This period can span across midnight. The time is really simple, just multiply hours by 100 then add the minutes so 11:32 is 1132.

bool IsInPeriod(int time, Period period)
{
    if( period.Start < period.Stop ) {
        return (time >= period.Start) && (time <= period.Stop);
    } else if ( period.Start > period.Stop ) {
        return (time >= period.Start) || (time <= period.Stop);
    } else {
        return false;
    }
}

Here is the Period class as I implemented it, nothing special or interesting here

public class Period {
    public Period() {
    }
     
    public Period(int start, int stop) {
        this.Start = start;
        this.Stop = stop;
    }
     
    public static int HrsMinToInt(int hrs, int min) {
        return hrs*100 + min;
    }
     
    public int Start { get; set; }
    public int Stop { get; set; }
}

Sorry if none of this make any sense, this post has mostly just been a brain dump so that if in the future I work on this again, or something similar I can remind myself of all this.

       

Creating a really simple Biconvex-like Lens in POVRay

I created a macro that will generate a really simple Biconvex lens. The macro take in three arguments, Location, Radius and Overlap. It works by taking the intersection of two spheres. The resulting object is then translated to wherever you want it to go in your scene.

#macro Make_BiconvexLens(Location, Radius, Overlap)
    intersection {
        sphere { <0,0,0>, Radius translate <0,0, -R/Overlap>}
        sphere { <0,0,0>, Radius translate <0,0, R/Overlap>}
        texture { T_Glass3 }
        interior { I_Glass }
        photons { reflection on refraction on }
        translate Location
    }
#end
       
1 2 3 4