Category Archives: Technology

Posts about technology, interesting things I find out as I develop technical products.

Dead Easy Pure CSS Button Class

Here’s a really simple way to make a styled button using only CSS. I wrote this because a lot of the “Pure CSS Button” scripts I found on the interwebs use a PNG image for the button which is not pure enough for me dammit!

This one does not use any image at all which is good for saving space if you are, say, working on an embedded web server with only a few kB of storage. It is also quick to set up. Simply cut & paste the following lines into your CSS file and add class=”button” to your tag in the HTML.
.button {
font-size: smaller;
background: #CCCCCC;
padding: 3px;
border-top: 1px solid #EEEEEE;
border-left: 1px solid #EEEEEE;
border-right: 1px solid #666666;
border-bottom: 1px solid #666666;
text-decoration: none;
}

.button:active {
border-top: 1px solid #666666;
border-left: 1px solid #666666;
border-right: 1px solid #EEEEEE;
border-bottom: 1px solid #EEEEEE;
}

And here is an example of a link using this style:
Go to Other Page

Here is what it looks like: Test Button – how easy was that!

Fastest Board Ever

I’ve just got to have a little boast about my latest PCB. I managed to draw up the schematic and lay out the PCB in just 3 hours start to finish. And anyone who knows me will tell you that I’m quite thorough when designing boards, double-checking measurements and footprints and so on.

Admittedly, I was doing a lot of recycling, about 75% of it was cut & pasted from other boards I have designed in the past but hey, if it gets the board out the door quicker…

In case you’re wondering what it does, it’s a custom RS422 to RS232 Bit Rate Converter I did for a client and of course, the board works perfectly with no design errors.

How to FFT with NXP’s DSP library

What a mouthful of acronyms in that title! This is going to be a very nerdy post.

Today I have been having fun processing audio with the NXP1758 Arm Cortex-M3 microprocessor. It’s really quite easy with the DSP library from NXP. Above is a screen shot of it showing the spectrum of a single note from a synthesizer.

The DSP Library

To get the library, download AN10913 from NXP’s website. There is no source code for the FFT, you need to link the static library NXP_M3_DSPLibFft.a into your project. The documentation doesn’t say much about the FFT function which is why I’m writing this blog post. Here’s some things they don’t mention:

  1. The FFT functions use complex numbers for both the input and output data
  2. It won’t do an in-place FFT – you need two separate data buffers
  3. The number of output data points is always equal to the number of input data points

Processing the data

I’m doing a 1024 point FFT so I’ll need a 4096 byte input buffer and a 4096 byte output buffer for a total RAM usage of 8k. The buffers are twice as large as you’d think because they need to store complex numbers. Each complex number consists of two 16-bit signed values, the first one being the real component and the second one the imaginary component which can be safely set to zero for audio data.

Interpreting the result

The result data contains a bunch of complex numbers which represent the amplitude and phase of each “frequency bin” in the spectrum. A frequency bin is a slice out of the total spectrum; being a digital process, FFT cannot deliver a continuous spectrum.

The bandwidth of each frequency bin is the sampling frequency divided by the number of FFT points. So for example if you are sampling at 22050Hz and using 1024 FFT points, each bin will cover a 21.53Hz slice of the total spectrum. Each bin has a centre frequency of (N*SAMPLERATE)/FFTSIZE where N is the bin number. So in my example above, bin 1 will be centred at 21.53Hz, bin 2 will be centred at 43.06Hz and so on. Bin 0 is centred at 0Hz and represents the DC component of the signal.

The results are complex numbers so you can work out not only the amplitude of each bin but also the phase. I am not interested in the phase so my example code below only computes the amplitude.

The Code

#include "dsp/cr_dsplib.h"
#include
#include

uint16_t fft_in[2048];
uint16_t fft_out[2048];
int16_t spectrum[512];

// Returns an array of 512 values containing the amplitude of each frequency
// bin from DC up to the sampling frequency / 2.
int16_t *processaudio(uint16_t audiobuffer)
{
int i;

// First copy the audio data to the real component of the FFT input buffer.
// Set the imaginary component to zero for all samples.
memset(fft_in, 0, sizeof(fft_in));
for(i = 0; i < 1024; i++)
fft_in[i*2] = audiobuffer[i];

// Now I can run the FFT.
vF_dspl_fftR4b16N1024(fft_out, fft_in);

// Convert the output data back into real numbers because I am not interested
// in the phase component.
// Also note that the second half of the FFT output mirrors the first half so it is
// only necessary to process 512 data points.
for(i = 0; i < 512; i++)
{
a = fft_out[i*2];
b = fft_out[i*2+1];
spectrum[i] = sqrt((a*a)+(b*b));
}

return spectrum;
}

First Board from OurPCB

A few people in my circle of acquaintances have been recommending OurPCB for prototype printed circuit boards so I thought I’d give them a try with my latest board. This board is a work-for-hire job so I’m sorry I can’t tell you what it’s for but I can tell you it’s a 4-layer board.

With 4 layer boards, you can’t see everything from visual inspection. The proof will be whether the board works after I populate it. But apart from the solder mask registration being slightly out, the board looks pretty nice. They even threw in gold coating for free!

OurPCB are a little slower than other board fabs I have used – it was almost three weeks before I got my boards – but they are cheaper. They seem pretty good.

Talking Metronome – part 4

This is the final part of a 4-part series. See also part 1, part 2 and part 3.

I’m really running out of time now. I need to get this thing into a box and looking nice ASAP.

At this stage I’m not 100% sure it will fit. I chose the box on the basis of “Oh, that a pretty box and seems to be about the right size”. As an emergency plan B, I bought another slightly larger one in case it didn’t fit but I’m going with the smaller box because it looks prettier than the plain boring square black larger box.

Drilling and cutting all the holes for the controls took some considerable time. Mechanical work is not really my forté but I managed to pull it off without spilling any blood, destroying the boards, the box or any tools. I had to be really quite precise because it’s such a tight squeeze, I didn’t have much choice where to place the controls. The rectangular hole for the LEDs was also a challenge for my mediocre workshop skills – but I like a good challenge!

I used some PCB standoffs to bolt the ARM board to the PIC board. I had to drill some holes and hack a couple of chunks out of the PIC board to make it fit. The DAC board was glued on to the ARM board (I love hot-melt glue!) and the amp board is just kind of crammed into the end. The speaker goes underneath where it almost fits but a little brute-force got it in there. I had to re-do a lot of the interconnecting wires to make them shorter or longer as neccessary and phew! it just fits!

The lid goes on easily and I even have easy access to the programming connector so I can do software updates! The PIC is socketed so if I want to update it’s software, I can pull it out and reprogram it.

And I’ve still got a few hours spare so I resampled the audio. The 8kHz samples sounded very Speak & Spell so I re-did them at 22kHz and it’s good now.

My Dad should be pleased.

Now I can go and spend some time with my family. I’ve been cocooned in my office for the last couple of days working on this and haven’t seen them much.

Merry Christmas everyone.

Talking Metronome – part 3

This is part 3 of a 4-part series. See also part 1, part 2 and part 4.

I’ve finished all the wiring. It looks a mess but I’m confident that it will work. Of course it won’t do anything until I write the software. There are four circuit boards here:

  • The PIC Board – Contains a PIC 8-bit computer. Handles user input, LED display and generates all the timing (you can’t see the PIC in this photo because it’s on the other side of the board).
  • The AMP Board – 0.5W amplifier to drive the speaker.
  • The DAC Board – Converts digital data from the ARM into analogue sound waves.
  • The ARM Board – Contains a powerful 32-bit ARM computer which is kind of wasted because all it does is shovel audio data into the DAC.

I recorded my audio samples using Audacity and downsampled them all to 8kHz so they will fit in the ARM’s memory. I only had a 64k ARM in my parts box, both the program and the audio samples need to fit into that space. I won’t have time to write an MP3 decoder or anything so it’s going to be 8-bit uncompressed PCM samples. I didn’t need to worry though because I’m only recording the numbers 1, 2, 3, 4 and as it turned out, they all fitted into 4k.

Writing the software had it’s ups and downs. About 80% of the code was recycled from other projects, stuff to boot up the ARM, timer interrupt handlers, RS232 drivers and that sort of thing. The only bit I had to write from scratch was the DAC driver and audio replay code which ran to only about 150 lines of code but for some reason took hours to get working. I had to make a few modifications to the PIC code also to send RS232 commands to the ARM, this was written in assembler which I generally avoid like the plague these days as it always takes longer to get things done in assembler. Anyway it is done and after whacking a couple of bugs, it works reasonably well.

Now to shove it all into a box. The software took most of today and it’s getting late, I’ll leave the box mounting for tomorrow.

Talking Metronome – part 2

This is part 2 of a 4-part series. See also part 1, part 3 and part 4.

I made it down to Jaycar today and bought all the bits I was short of. These guys aren’t all that cheap but time is short and I really need to get the parts today. They also had a neat little 0.5 watt amplifier kit called “The Champ” which can run off 4 – 12V. Perfect.

I assembled the amplifier, speaker and volume control in about 15 minutes. The sound is a little buzzy but what do you expect for $8. It will do just fine.

I also wired up the MIDI Sync board. I have decided to completely finish the circuitry before starting on the software to save time but it’s a little risky to my schedule. There are still a lot of unknowns in this project – will the ARM be able to stream the audio to the DAC fast enough? Will it all fit in the box? Will the sound quality be OK? Will I be able to write the software in time? There are many things which could go wrong and I don’t have the time to build this carefully, testing each stage as I would normally do. I am just blasting ahead with confidence in myself that I have the skill to make this work.

So next is the DAC. I found a few different DAC chips in my parts box and after reading the data sheets for a few of them, I have decided on the AD558 which is a very simple 8-bit DAC with a built-in data latch. I need the latch because I will be sharing the data bus with the buttons and LEDs. I just tossed this onto a piece of veroboard. The only external component is a 7805 voltage regulator because the DAC runs off 5V and cannot share the 3.3V supply from the ARM. I won’t need a level converter for the data bus because the ARM can pull the data lines up to about 3.2V which is high enough for the DAC.

After wiring the ARM up to the MIDI Sync board, I have realised there is a fatal flaw in my plan! After checking the electrical specifications for the ARM7, I discover that the output pins can only supply a maximum of 2 milliamps each. I need more like 25mA to drive the LEDs. So I guess I’ll need a driver chip of some sort. I have a 74HC244 in my parts box which would be perfect but now I have to wire that up too. This thing is getting more complicated than I had planned.

Time for a re-think. After a little time-out and thought I have devised a new plan. I’ll fit the 8-bit PIC microcomputer to the MIDI Sync board and use it to drive the LEDs and read the user input. This will actually make the job easier as I already have PIC software written to do this from the MIDI Sync Box project. The PIC can send data to the ARM via RS232 and all the ARM has to do now is play back the audio samples when instructed to do so by the PIC. I like this new design even though there are now two computers in the box! Talk about overkill, I am going for speed rather than elegance here.

It took me a little while to find my original code for the MIDI Sync Box, set up all the compilers and tools and get it running but I now have the LED display, all the timing, tempo control and start/stop control done.

Talking Metronome – part 1

This is part 1 of a 4-part series. See also part 2, part 3 and part 4.

So I asked my Dad what he wanted for Christmas, and he said a Talking Metronome. He’s learning to play the piano you see and is dissatisfied with regular tick-tock metronomes. So I had a look around. Talking metronomes are not all that easy to buy. The only product I found was the Boss DB90 which costs $250 and is kinda complicated. So I have decided to build my own. How hard could it be?

I will be attempting to build this out of parts and software that I have already developed for other projects plus whatever I can find in my parts box. I’ll start with one of my RS422/485 Bit Rate Converter boards which has a really nice 32-bit ARM7 computer on it, wire it up to some buttons, an LED display and a DAC to produce the audio output.. I have only placed the parts for the CPU and switchmode power regulator, I won’t be needing the serial ports. That pin header is the programming port for the CPU.

I also have my MIDI Sync Box circuit board which I designed years ago when I thought I could launch a MIDI clock source device – I never launched the product but I have a few old prototype boards lying about. It’s perfect for mounting the LEDs and rotary encoder for the tempo knob. It also has all the multiplexing circuitry for the LEDs, rotary encoder and additional buttons. I won’t place the PIC microcontroller, I’ll just wire the ARM straight up to it.

I’ll have to change the resistor values for the LEDs because the board will now be running at 3.3 volts instead of 5 volts. I guesstimate that 100Ω should just about do it. Only problem is that I’m completely out of 100Ω through-hole resistors.

So today I have assembled the ARM board and tomorrow I will need to go shopping. The parts I will need to buy are:

  • Some 100Ω resistors
  • A small amplifier module which can run off 9-12V
  • A small loudspeaker
  • A suitable box to put it all in

Not that much stuff. I could probably have built an amplifier out of bits I have lying around but a pre-built module will save me a lot of time.

Random page background image

Just for something different, here is a bit of PHP which will cause a random background image to be loaded every time you refresh your page. The image is chosen randomly from a directory making it easy to add or remove new images without needing to update the HTML.

This code should go in place of the <body> tag in your HTML.
// Set this to the path where your images are stored.
$bgdir = '/images/backgrounds';

// Test the extension of a file to see if it is an image file.
function isImageFile($src) { return preg_match('/^.+.(gif|png|jpe?g|bmp|tif)$/i', $src); }

// Get the list of all files.
$dirlist = scandir($_SERVER['DOCUMENT_ROOT'] . $bgdir);

// Put all the file names into an array.
$imgs = array();
$preload = '';
foreach($dirlist as $file)
{
if(isImageFile($file))
{
$preload .= "   rn";
$imgs[] = $file;
}
}

// Generate a tag with a random background image URL.
$url = $bgdir . '/' . $imgs[array_rand($imgs)];
print "rn";

// Pre-load all the other images so subsequent page loads will be fast.
print $preload;
?>;

Unsoldering Equipment with Hot Air

I just bought myself a new SMD rework station, it’s a fine tool but I want to share with you a few choice excerpts from the extremely poorly translated manual. I think the Chinese manufacturers put no effort at all into the English translation these days:

“Intelligent system that could alarm, Fault detection automatically”

“Green lines and high-quality silicone body heat good for extend life and save power”

“increased gun handpiece would be insertion and unplug”

and my favourite…

“LED manifestation of tiny electricity computer figures”