Enabling Case Sensitivity in Windows

I recently needed to download a large number of files from a Linux server using SCP, which would take nearly 12 hours. When i checked the next day this transfer had stopped to ask if I wanted to overwrite a file. This was because two files had the same name, differing only in case.

I remembered there was a registry setting that could set the NTFS filesystem to be case sensitive[0], but I’d rather not do that on this workstation. I did a Google search anyway, but then I found there is a newer way to do this which is on a per-folder basis instead of globally[1].

There is a command called “fsutil.exe” that can adjust a lot of filesystem settings, one of them is “SetCaseSensitiveInfo”, which controls if the folder is case sensitive or not.

fsutil.exe file SetCaseSensitiveInfo C:\folder enable

When I restarted the download, it still didn’t work, because that command only applies it to the specified folder, and any new folders created after. It does not apply to existing sub-folders. The following PowerShell command will apply the setting to any existing folder.

Get-ChildItem C:\folder -Recurse | 
	? {$_.PSIsContainer} |
	% { fsutil.exe file SetCaseSensitiveInfo $_.FullName enable }

References

[0] https://superuser.com/questions/266110/how-do-you-make-windows-7-fully-case-sensitive-with-respect-to-the-filesystem

[1] https://docs.microsoft.com/en-us/windows/wsl/case-sensitivity

     

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Ā šŸ˜¢

         

Contolling the CPU Governor in Linux

Recently I wondered how you control CPUĀ throttlingĀ in GNU/Linux systems so after doing a little research this script is what Iā€™ve come up with. Itā€™s based on some others that Iā€™ve comeĀ across. All it does is set the CPU throttling mode to ā€œperformanceā€ then displays the current CPU frequency setting, which should be full speed.

#!/bin/sh
for CPUGOV in /sys/devices/system/cpu/cpu*/cpufreq/scaling_governor
do
    echo -n performance > $CPUGOV;
done
 
# Display Stats about new settings
grep -E '^model name' /proc/cpuinfo | head -n 1
for CPUFREQ in /sys/devices/system/cpu/cpu*/cpufreq/scaling_cur_freq
do
    cat  $CPUFREQ | awk '{ print $1 / 1e6 "GHz"; }'
done
 
exit 0
     

A Switch Debouncing Method

There are a lot of different methods used to debounce switch inputs and this post is just about one method that I read about recently and used a couple of times now. So far it seems pretty good and does the job.

The C code for this method is just below, I defined it as a macro so I can use it for multiple switch inputs. All you need to do is execute this code at a fixed sample rate and it will do the rest.

if( input == OFF ) {
    if( integrator > 0 ) {
        integrator--;
    }
} else if( integrator < PERIOD ) {
    integrator++;
}
 
if( integrator == 0 ) {
    output = OFF;
} else if( integrator >= PERIOD ) {
    output = ON;
    integrator = PERIOD;
}

What this basically does is average samples. When the input is high the ā€œintegratorā€ counts up until it reaches the PERIOD constant. Then it sets the output high and also saturates the integrator to the value of PERIOD. When the input is low the integrator counts down until it reaches 0 when it sets the output low.

I cant remember where I found this method, I believe the link is somewhere on Jack Ganssleā€™s website. He also has a good tutorial on switch debouncing.

       

Creating an IO Assignment Header File

When developing embedded firmware I like to define macros that provide aliases for the IO port registers that I need. The name of each of the macros will correspond to the net name on the schematic to make it easy to check for errors. I put all of these macros into a single header file that I can include, I have sometimes seen these called board support packages. They can sometime be entire libraries that abstract details of the hardware. In my case they are just simple header files as that is all I need for now.

Recently I decided to automate some of the work of creating these files as it can be very time consuming. This is the Awk script I ended up with.

#!/usr/bin/awk -f
{
    if ($0 == "") next;
 
    if ($2 == "PORTA") { port = "A"; }
    if ($2 == "PORTB") { port = "B"; }
    if ($2 == "PORTC") { port = "C"; }
    if ($2 == "PORTD") { port = "D"; }
    if ($2 == "PORTE") { port = "E"; }
    if ($2 == "PORTF") { port = "F"; }
    if ($2 == "PORTG") { port = "G"; }
 
    print "#define " $1 "_PIN " $3;
    print "#define " $1 "_TRIS TRIS" port "bits.TRIS" port $3;
    print "#define " $1 "_LAT LAT" port "bits.LAT" port $3;
    print "#define " $1 "_PORT PORT" port "bits.R" port $3;
    print "";
}

All I need to do is write a space separated file in which each line contains the name I want, the port it is on and the number of the pin that it is attached to. Then this simple script generates the C code. This was for a PIC24F series micro, I have a slightly different script for a project involving a PIC32 which I may post later.

This makes creating BSP header files really easy, especially if you need to modify the pin assignments!

       

Profiling Python Applications

Iā€™ve been working on a project thatā€™s written in Python, it continuously communicates with some external industrial equipment. It will poll the status of this equipment 4 times per second and also sends commands to them when requested to. My job this week was to raise the update rate to 5Hzā€¦. I needed to make sure I had enough time to do this!

I decided before doing anything I should profile the code to find out how much time the main loop needs to run and what methods take the longest time. That way Iā€™d know if the code can support 5Hz and if not what I can do about it.

Once again the Python standard library comes to the rescue, theĀ cProfileĀ module will monitor the execution of your program and generate a report. Below is an example of how to use it.

import cProfile
cProfile.run("main()")

The next thing I did is write a simple bit of code that will print to stdout the current update rate of my application every second. Itā€™s pretty much a Python port of the JavaScript libraryĀ stats.js.

from __future__ import division
import time

class stats(object):
    def __init__(self):
        self.msMin = 1000
        self.msMax = 0
        self.msTime = 0
        self.fpsMin = 1000
        self.fpsMax = 0
        self.fps = 0;
        self.updates = 0
        self.startTime = int((time.time()+0.5)*1000)
        self.prevTime = int((time.time()+0.5)*1000)

    def begin(self):
        """Calling the method signifies the start of a frame
        """
        self.startTime = int((time.time()+0.5)*1000)

    def end(self):
        """Calling this method signifies the end of a frame
        """
        now = int((time.time()+0.5)*1000)

        self.msTime = now-self.startTime
        self.msMax = max(self.msMax, self.msTime)
        self.msMin = min(self.msMin, self.msTime)

        #print "ms: %i (%i - %i)" % (self.msTime, self.msMin, self.msMax)

        self.updates = self.updates + 1

        if now > (self.prevTime + 1000.0):
            self.fps = round((self.updates*1000.0)/float(now-self.prevTime))
            self.fpsMax = max(self.fpsMax, self.fps)
            self.fpsMin = min(self.fpsMin, self.fps)

            print "stats: %i fps (%.i fps - %i fps)" % (self.fps, self.fpsMin, self.fpsMax)

            self.prevTime = now
            self.updates = 0

        return now

        def update(self):
            self.startTime = self.end()

Simply add a call toĀ begin() to the start of your loop and a corresponding call toĀ end()Ā  at the end of your loop. Now I get a nice counter that tells me how the code is performing. As I work on the code I can see how this affects the main loop performance.