Archive for October, 2010

Handling PNG images in C++

October 5, 2010

This blog post is about png++, one of my favourite C++ libraries.

It’s a wrapper of libpng in C++ that allows to handle PNG images so easily that you’ll never want to use bare libpng again.

All you need to start is to add these two lines in you source files:

#include "png++/png.hpp"
using namespace png;

This is the code to open an existing image:

image img("image.png");

The code to make a new image from scratch differs only in the parameter passed to image’s constructor:

image img(1024,768);

The image class has the get_pixel() and set_pixel() memeber functions to access the individual pixels:

rgb_pixel pixel=img.get_pixel(0,0);
pixel.red=min(255,pixel.red+10); //Increase red color of pixel
img.set_pixel(0,0,pixel);

The image height and width can be obtained with intuitive get_height() and get_width() memeber functions:

for(int i=0;i<img.get_width();i++)
{
    for(int j=0;j<img.get_height();j++)
    {
        //Do something
    }
}

The image can be written to file with the write() memebr function:

img.write("new_image.png");

Also, this library is header-only. What does it means? That you do not have to compile the library to start playing with it, just exptract the library in the folder where you have your source code, and include the header file “png.hpp”, just that.

Lastly, here is an example code including CMake scripts to show the capabilities of the library. png++.tar.gz

Miosix 1.54 released

October 3, 2010

After a lot of time spent coding, here’s the new release of Miosix, my OS kernel for microcontrollers.

New features include:

  • Porting for ST’s Cortex M3 microcontrollers
  • Preliminary implementation of the POSIX thread API (pthreads)
  • Improved statistics on memory usage and debugging messages
  • Bug fixes and other enhancements

If you’re interested, download the new release here: http://www.webalice.it/fede.tft/miosix/index.html

Digital voltmeter for power supply

October 2, 2010

This summer I finally found some time to fix my power supply.

It’s a rather old but relieable unit, and I have no intention of replacing it. Basically, it still works except for the voltage meter on the front panel. Over time, the pointer developed an offset of around 1V, which is visible in this image where the power supply is turned off. Instead of indicating exactly zero volt, the pointer is below the beginning of the scale.

This, together with the fact that today’s microcontroller require 3.3V to operate (while the voltmeter only has an 1V resolution), forced me to always use a multimeter when using it, to be able to precisely set the output voltage.

The solution I found was to simply replace the analog voltmeter with a digital one. Instead of using a voltmeter chip like the ICL7107 that usually require the measurement ground to be separated from the supply ground, these days it is easier to build a voltmeter using a microcontroller.

That’s because even the cheap and simple micros now have at least a 10 bit ADC which is more than enough for a voltage meter in the rage 3..15V (which is the range of my power supply). Since the task is easy there was no need to use an ARM microcontroller as I usually do, but instead an ATtiny26 proved more than enough, despite only having 2KBytes of FLASH and 128Bytes of RAM.

This is the result:

The circiut is simple, a 78L05 is used to reduce the 20V found in the power supply to 5V to power the microcontroller. A voltage divider made with 1% precision resistors is connected from the power supply input to an ADC capable GPIO on the microcontroller, and three LED displays show the voltage with 0.1V resolution.

The LED displays are ofcourse multiplexed so that the ATtiny, despite its low number of GPIOs, can drive the display with no other glue logic except for current limiting resistors.

Around 100 lines of C++ code keep the whole thing working.