Working with LCD glyphs

Reading a diff today I found this piece of code for defining a font for a matrix LCD display. The code is interesting, it lets the developer see what the font looks like, so fixing your alphabet is really easy.

unsigned char font5x7[][8] = 
/* z */

/* s */

But something is fishy here, how do the compiler understand ________ as being 0x00, or 0xFF? So I went on to see the included header… and ouch, this is what I found.

#define	_XX_____	0x60
#define	_XX____X	0x61
#define	_XX___X_	0x62
#define	_XX___XX	0x63
#define	_XX__X__	0x64
#define	_XX__X_X	0x65
#define	_XX__XX_	0x66
#define	_XX__XXX	0x67
#define	_XX_X___	0x68
#define	_XX_X__X	0x69
#define	_XX_X_X_	0x6a
#define	_XX_X_XX	0x6b
#define	_XX_XX__	0x6c
#define	_XX_XX_X	0x6d
#define	_XX_XXX_	0x6e
#define	_XX_XXXX	0x6f

This is ugly as code and pretty as ASCII art. When we are coding we want beautiful code, but not pretty ASCII art. Let that to all the artists, they do better art than we do. So, how do we fix the code? Simple, macros to the rescue!

#define _	0
#define X	1
#define b(a,b,c,d,e,f,g,h)	(a << 7| b << 6 | c << 5 | d << 4 | e << 3 | f << 2 | g << 1 | h)

With this we let the compiler do the dirty job of creating all those values. Using the macros above, the code becomes easier to maintain and read. Just remember to undef the macros after using it, as you don't want all your X's, _'s and b's being changed!

&#91;sourcecode lang=c&#93;
unsigned char font5x7&#91;&#93;&#91;8&#93; = {
	/* z */ {

	/* s */ {

By the way, you can apply this idea for creating small graphics on code. It's easy and self-documenting. Happy hacking!

I just remembered the section <em>Making a Glyph from Bit Patterns</em> from <strong>Expert C Programming</strong> (<a href="">buy</a> this book if you don't have it yet!), it gives a solution similar to mine. The macros defined there are:

#define _ )*2
#define X )*2 + 1
#define s ((((((((0

So the code looks like this:

unsigned char font5x7[][8] = {
	/* z */ {
	s _ _ _ _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	s X X X X X _ _ _,
	s _ _ _ X _ _ _ _,
	s _ _ X _ _ _ _ _,
	s _ X _ _ _ _ _ _,
	s X X X X X _ _ _,
	s _ _ _ _ _ _ _ _,
	/* s */ {
	s _ _ _ _ _ _ _ _,
	s _ _ _ _ _ _ _ _,
	s _ X X X _ _ _ _,
	s X _ _ _ _ _ _ _,
	s _ X X _ _ _ _ _,
	s _ _ _ X _ _ _ _,
	s X X X _ _ _ _ _,
	s _ _ _ _ _ _ _ _,

From CVS to Git to Gitorious!

Migrating from CVS to Git

Last week I’ve offered myself to migrate some ~300 repositories to git. Not an easy task at first, but with the right tools at hand the task becomes manageable. Installing cvs2git, and following its documentation will get you started. In Ubuntu that is as simples as:

sudo apt-get install cvs2svn

I know it’s weird, but cvs2git is bundled in cvs2svn… go figure.

But migrating hundreds of repositories isn’t a task to do manually, so I created a script for automating the process. As I had access to the server files, migrating was easier then I expected. My directory structure was something like:

  • cvs_project_1
    • repo_1
    • repo_2
    • repo_3
  • cvs_project_2

I’ve decided to migrate one project at a time, making it straightforward to verify each repo. My script is the following, bare in mind that it my have some flaws, it worked for me. Test it before erasing your old CVS data.

# Copyright (C) Pedro Kiefer

for f in `cat repo_list`;
	echo "===== Creating git repository for ${f/\//\-/}/";
	sed -e "s/__REPO__/${f/\//\\/}/g" my-default.options > $FOP.options;
	cvs2git --options=$FOP.options
	rm $FOP.options
	mkdir $FOP.git
	cd $FOP.git
	git init --bare
	cat ../cvs2svn-tmp/git-blob.dat ../cvs2svn-tmp/git-dump.dat | git fast-import
	cd ..

The script takes a repo_list file with a list of paths to the CVS repositories. Creating this list is quite easy, something like this should work. Be sure to remove CVSROOT and the root directory.

find cvs_project_1/ -maxdepth 1 -type d | sort > repo_list
vim repo_list

The other file the script need is my-default.options, which is the configuration file used by cvs2git. Most of the default values are good, but you really want to add a list of cvs commiters – so you can map the cvs login to a name + email. The other change need is on the line that sets the repository path. For the script to work you need to have it set as __REPO__. Like this:

    # The filesystem path to the part of the CVS repository (*not* a
    # CVS working copy) that should be converted.  This may be a
    # subdirectory (i.e., a module) within a larger CVS repository.

That’s it, just run the script, and voilà, git repositories for all your cvs modules.

From Git to Gitorious

The second part of my task was importing all of those git repositories to my local Gitorious install. Again, doing it manually is not the right way to do it. After asking about it on gitorious mailing list and learning some ruby, I’ve created this little script. It creates all the repositories for a given project. The projects were created manually on gitorious, as I had only 6 projects – extending the tool to support the creating of projects should be easy.

After using the script above, I had the following directory structure:

  • project_1/
    • repo_1.git
    • repo_2.git
    • repo_3.git

      The scripts takes as argument the project name, which should be equal to the one you created on gitorious web interface. The script scan the project directory and creates the matching gitorious repositories, copying the data to the newly created repository. Some magic regexp was added to remove version numbers and set uniform names to the new repositories. You might want to edit this to your taste.

      By the way, this is my very first ruby programming, don’t expect it to be pretty!

      #!/usr/bin/env ruby
      # encoding: utf-8
      # Copyright (C) Pedro Kiefer
      # Mass migrate git repositories to gitorious
      require "/path/to/gitorious/config/environment.rb"
      require "optparse"
      def new_repos(opts={}){
          :name => "foo"
      current_proj = ARGV[0]
      @project = Project.find_by_slug(current_proj)
      puts Dir.pwd
      files = Dir.glob("*.git")
      files.each do |f|
        orig_repo = f
        f = f.gsub(/\.git$/, "")
        f = f.gsub(/_/,"-")
        # has version?
        version = f.match(/-([0-9](.[0-9][0-9]*)+)(-)?/)
        f = f.gsub(/-([0-9](.[0-9][0-9]*)+)(-)?/, "")
        desc = "Repository for package #{f.downcase}\n"
        desc << "Package version #{version&#91;1&#93;}\n" if version
        print "Creating repository for package #{f} ... " 
        @repo = new_repos(:name => f.downcase, :project => @project, :owner => @project.owner, :user => @project.user, :description => desc)
        path = @repo.full_repository_path
        @repo.ready = true 
        FileUtils.cp_r(["#{orig_repo}/branches", "#{orig_repo}/info", "#{orig_repo}/objects", "#{orig_repo}/refs"], @repo.full_repository_path)
        puts "Ok!"

Asterisk and FreePBX on Ubuntu Server 10.10

This is just a small gathering of commands and best practices for installing Asterisk and FreePBX on Ubuntu. This worked for me, it has some shortcomings but should work on most of the cases. Feel free to add some comments on better ways of installing it.

The following packages will be installed:

  • Asterisk
  • FreePBX 2.8.1

I started with a fresh install of Ubuntu Server 10.10, but if you already have it installed, results should be similar. While installing I selected the LAMP and SSH services, those are pretty basic services which you will need. If you have finished a fresh install, or haven’t updated your system in a while, I suggest running the following lines before continuing with this guide.

sudo apt-get update
sudo apt-get upgrade


Although not necessary for running Asterisk and FreePBX, I suggest that you install a MTA agent. If you think this is unnecessary on your setup skip to the next section. Postfix is my MTA of choice, so we are going to install it. When prompt about which configuration should be done to it, select Internet with smarthost, just confirm the other options.

sudo apt-get install postfix

Okey, postfix installed, time to edit the basic configuration, add or change the following lines to /etc/postfix/

relayhost = []:587
smtp_sasl_auth_enable = yes
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_sasl_security_options = noanonymous
smtp_tls_CAfile = /etc/postfix/cacert.pem
smtp_use_tls = yes

The password for accessing your external relay must be saved to
/etc/postfix/sasl_passwd, add the following this file:


Fix the permissions on this file:

sudo chmod 400 /etc/postfix/sasl_passwd
sudo postmap /etc/postfix/sasl_passwd

Add the appropriate ca-certificate to /etc/postfix/cacert.pem. For gmail, that’s Thawte Consulting, so add their ca-certificate.

cat /etc/ssl/certs/Thawte_Premium_Server_CA.pem | sudo tee -a /etc/postfix/cacert.pem

Restart postfix:

sudo /etc/init.d/postfix restart

Avoid sending mail as root

Edit /etc/aliases, add the following:

root: server@domain.tld

Run the new alias command:


Create a /etc/postfix/sender_canonical file mapping user -> email such as:

root            server@domain.tld

Run the following lines:

sudo postmap hash:/etc/postfix/sender_canonical

Add the following line to /etc/postfix/


Restart postfix:

sudo /etc/init.d/postfix restart


When you selected the LAMP service on your Ubuntu install, you automatically got PHP5 installed. Now you just have to install some additional packages that didn’t get installed. So run the following line to install them.

sudo apt-get install php5-gd php-pear php-db sox curl


One might find useful to have phpMyAdmin installed for managing the MySQL database used by FrePBX and Asterisk. If you don’t know what phpMyAdmin is, you can skip to the next section.


Ubuntu 10.10 provides pre-compiled asterisk packages, using that is way more easier than backing your own asterisk. Run the following to install it, and all of its dependencies.

sudo apt-get install asterisk asterisk-mysql asterisk-mp3 asterisk-sounds-extra


This is a really short how-to for configuring Dahdi, it just covers the bare minimum, but it works ok. First of all, load the necessary kernel modules, in my case for a TDM400P it was the following line:

sudo modprobe wctdm

You might wanna check if the module was loaded and configured your hardware properly, so run a dmesg. If everything is alright, you have to create the dadhi configuration file. That’s really easy, just run:

sudo dahdi_genconf -vvvv

Warning: Be careful when you run this on a production system, it will override the current dahdi configuration file.

Edit /etc/dahdi/system.conf and set the correct loadzone and defaultzone for your country code. I like to use vim to edit configuration files, but you can use any text editor.

sudo vim /etc/dahdi/system.conf

Now check if channels are up an running, run dahdi_cfg:

sudo dahdi_cfg -vvv

Next you have to edit /etc/asterisk/chan_dahdi.conf to configure the channels, this is what asterisk will see and use to send and receive calls.


Before running the install command, you have to configure your apache server. I prefer to use virtual host, and as of lately I have adopted the following layout for my server:

  • /var/www/address/conf
  • /var/www/address/public
  • /var/www/address/log

In the conf I store the necessary vhost configuration, in public lives the public accessible files, and log hosts the logging files. Feel free to use your own personal taste on installing webapps. For those who want to stick with the how-to, create the needed directories:

sudo mkdir /var/www/pabx.domain/
sudo mkdir /var/www/pabx.domain/conf
sudo mkdir /var/www/pabx.domain/log
sudo mkdir /var/www/pabx.domain/public

Now create a /var/www/pabx.domain/conf/vhost.conf file:

sudo vim /var/www/pabx.domain/conf/vhost.conf

And paste the following lines, change it accordingly to your domain.

<VirtualHost *:80>
   ServerName pabx.domain
   ServerAlias pabx.domain

   ServerAdmin admin@domain.tld
   ErrorLog /var/www/pabx.domain/log/error.log
   CustomLog /var/www/pabx.domain/log/access.log combined

   DocumentRoot /var/www/pabx.domain/public
   <Directory /var/www/pabx.domain/public>
       Options Indexes FollowSymLinks MultiViews
       Order allow,deny
       AllowOverride All
       Allow from all

   <Directory /var/www/pabx.domain/public/admin>
       AuthType Basic
       AuthName "Restricted Area"
       AuthUserFile freepbx-passwd
       Require user admin

With the file created, add the vhost to the sites-enabled directory, with:

sudo ln -s /var/www/pabx.domain/conf/vhost.conf /etc/apache2/sites-available/pabx.domain
cd /etc/apache2/sites-enabled/
sudo ln -s ../sites-available/pabx.domain

For now, create an htpasswd file to protect the access to freepbx.

sudo htpasswd -c /etc/apache2/freepbx-passwd admin

And finally, restart apache.

sudo /etc/init.d/apache2 restart


Your Asterisk install should be working by now, so it’s time to install a nice web user interface. Ubuntu doesn’t provide a package for FreePBX, so grab the latest stable source code from FreePBX site.

cd /tmp
cd /usr/src
sudo tar xvzf /tmp/freepbx-2.8.1.tar.gz
cd freepbx-2.8.1/

You can equally extract the tarball on your home directory. It doesn’t make any difference. Now it’s time to create the database, the user used to access it, and populate the basic tables. This will create and import the basic tables to asterisk and asterisk cdr database, run this from the recently extracted directory.

mysqladmin create asterisk -u root -p
mysqladmin create asteriskcdrdb -u root -p
mysql -u root -p asterisk < SQL/newinstall.sql
mysql -u root -p asteriskcdrdb < SQL/cdr_mysql_table.sql

With the tables in-place, it's time to create the user with privileges to access and edit those tables. Open a mysql prompt with:

mysql -u root -p

On the prompt run the following queries:

GRANT ALL PRIVILEGES ON asterisk.* TO asterisk@localhost IDENTIFIED BY 'badasspassword';
GRANT ALL PRIVILEGES ON asteriskcdrdb.* TO  asterisk@localhost IDENTIFIED BY 'badasspassword';
flush privileges;

Don't forget to change the password!

Before running the install command, make a copy of /etc/asterisk/modules.conf. FreePBX rewrites the file and trashes Asterisk installation. If you restart Asterisk after installing FreePBX Asterisk dies with no message.

sudo cp /etc/asterisk/modules.conf ~/asterisk-modules.conf

Ok, we are ready to install freepbx to /var/www/pabx.domain/public:

sudo ./install_amp

The install script will ask for some configuration data, eg. were to install freepbx (/var/www/pabx.domain/public), sql password, asterisk password, etc. Take note of the passwords you used, you might need them later.

The output from the install script is somewhat like this:

Enter your USERNAME to connect to the 'asterisk' database:
 [asteriskuser] asterisk
Enter your PASSWORD to connect to the 'asterisk' database:
 [amp109] badasspassword
Enter the hostname of the 'asterisk' database:
Enter a USERNAME to connect to the Asterisk Manager interface:
Enter a PASSWORD to connect to the Asterisk Manager interface:
Enter the path to use for your AMP web root:
Enter the IP ADDRESS or hostname used to access the AMP web-admin:
 [xx.xx.xx.xx] pabx.domain
Enter a PASSWORD to perform call transfers with the Flash Operator Panel:
 [passw0rd] password
Use simple Extensions [extensions] admin or separate Devices and Users [deviceanduser]?
Enter directory in which to store AMP executable scripts:

Restore asterisk-modules.conf file, which you backed up before installing FreePBX:

sudo cp ~/asterisk-modules.conf /etc/asterisk/modules.conf

Apache runs as www-data, Asterisk as user asterisk, so we have to change some permission to make both programs work together. First, add www-data to asterisk group:

sudo adduser www-data asterisk

Fix the permissions from amportal, add these lines to the end of /etc/amportal.conf:


Everything in place, time to start amportal:

sudo amportal start

Open your web browser and go to http://pabx.domain/ and you will be greeted with FreePBX site. I strongly suggest you to upgrade and install the FreePBX modules you will need, so go to Modules Admin and click on Check for online updates.

Start asterisk with amportal

Before we finish, lets make amportal script to manage asterisk and run it through the safe_asterisk script, for that, we have to remove asterisk from rc.d:

sudo update-rc.d -f asterisk remove

Now edit safe_asterisk, to make sure it runs on background, edit the variable BACKGROUND to zero:

sudo sed -e s/BACKGROUND=0/BACKGROUND=1/ -i /usr/sbin/safe_asterisk

We have to start amportal after booting, so call amportal start in /etc/rc.local. Edit your /etc/rc.local and add the following line before the exit 0 line.

/usr/local/sbin/amportal start

Reboot your machine, and check that everything is still working. Have fun!

Histórias de ninar de um engenheiro

Hoje no trabalho, enquanto escrevia um driver novo pra kernel do linux, tivemos uma discussão sobre histórias de ninar de engenheiros, físicos, computólogos, matemáticos, etc… esse povo todo das exatas. A primeira história que lembramos foi a versão ‘exatas’ para os três porquinhos que começa assim:

… Era uma vez três porquinhos ( P1, P2 e P3) e um Lobo Mau, por definição, LM, que vivia os atormentando. P1 era sabido e fazia Engenharia Elétrica e já era formado em Engenharia Civil.
P2 era arquiteto e vivia em fúteis devaneios estéticos absolutamente desprovidos de cálculos rigorosos. P3 fazia Comunicação e Expressão Visual na ECA. LM, na Escala Oficial da ABNT, para medição da Maldade (EOMM) era Mau nível 8,75 (arredondando a partir da 3ª casa decimal para cima)…

Obviamente começamos a pensar em outras histórias, contando as coisas divertidas que fazemos diariamente. Falando de Linux, transistores, FPGAs, spinlock’s, threads, BUG_ON()… não sei se daria uma história muito engraçada, afinal, que criança iria entender! Mas com certeza demos muitas risadas. Algo nessa linha representa as bobagens que inventamos:

Era uma vez um Driver feliz, que funcionava perfeitamente na sua kernel 2.6.18… mas os tempos passaram, e melhorias foram feitas no código da kernel. O Driver feliz estava ficando triste por estar desatualizado, então seu fiel programador resolveu atualizar suas funcionalidades para ele ser ainda mais feliz.

O seu fiel programador não sabia muito sobre as grandes novidades do kernel, e também tinha dormindo nas aulas sobre kernel threads e spinlock’s. Não sabendo dessas novidades, o programador achou melhor não incluir todos aqueles spinlock’s que o Driver feliz tinha.

O Driver feliz então ficou muito triste, pois só fazia kernel panic’s, não conseguia mais ser feliz. Era agora um Driver infeliz… Mas eis que então um superprogramador surge para salvar o Driver feliz da sua infelicidade. O superprogramador não tinha dormido nas aulas de spinlock’s, e em pouco tempo fez o Driver feliz voltar a ser o mais feliz de todos os drivers.

Espero nunca contar algo tipo isso pros meus futuros filhos! Mas, talvez seja inevitável… vou ter que esperar para descobrir.

LCD Bring Up

Uma das vantagens de se trabalhar desenvolvendo hardware é o prazer de ver alguma componente novo funcionando em uma placa. Essa semana tive a felicidade de fazer um LCD funcionar. Depois de bater cabeça com drivers que mereciam um refactor e constantes estranhas – que deveriam virar máscaras com um monte de OR’s – descobri o problema: um dos sinais que devia estar sempre em 1 ou 0 estava alternando de valores enlouquecidamente 0101010101…

Arrumado isso e voilá! LCD funcionando.

LCD com um pinguim!

LCD com um pinguim!

Pena que essa placa não tem um módulo de GPRS… se tivesse o PedroFone tava pronto, mas quem sabe em breve… por enquanto dá pra fazer um PedroTablet, mas acho que não teria muito mercado.