the random bit

linux, electro-coisas

Using Subversion (SVN) revision control system

Lately I’ve been working on some big and colaborative projects where the code size grows big and fat easily and there is a huge amount of logs, binaries, object files (etc.) generated by compiler toolchains which we really don’t care about. Tracking and sharing everything can be quite difficult and if you’re like me you have a few different versions of the same set of files scattered around a few folders and when it’s time to cleanup you’ll probably end up deleting the wrong folder. The solution? Revision Control Systems!!

The aim of a revision control system is to track code (mainly code, but it can tack other type of files), today I’ll be talking about Subversion (abreviated SVN) because my university had a Subversion repository and that’s what I learned first though there are a lot of others out there you should check out (comparison of revision control systems, in a future post I’ll be discussing Git).

OK, so you have been working on some code on your computer and have a few files lying around (let’s say: main.c lib1.c lib1.h lib2.c lib2.h makefile), you’ve worked for a while on them, now what? Go ahead and create an online repository (Google Code seems nice, choose Subversion under Version Control Systems), once it’s created you need to set up the repository on your computer, on the command line navigate to the folder where you want to set it up and do:

$ svn checkout https://wordpress-svn-tutorial.googlecode.com/svn/trunk/ wordpress-svn-tutorial –username tiago.gala@gmail.com
Checked out revision 1.

This will set up a folder called “wordpress-svn-tutorial” on your working directory, usually this command is given by the website hosting the repository as it changes from site to site. Next we will add some files to the repository, to do so copy your existing files into “wordpress-svn-tutorial” or create new ones:

$ cp *.c *.h makefile wordpress-svn-tutorial
$ cd wordpress-svn-tutorial
$ mkdir lib
$ mv lib*.* lib
$ ls -R
.:
lib   main.c   makefile
./lib:
lib1.c lib1.h lib2.c lib2.h

We’ve successfully set up our repository tree and we’re ready to upload this code to the server but first we need to check the status of our repository:
$ svn stat
?   main.c
?   lib
?   makefile

The ‘?’ means that SVN doesn’t know what to do with those files, they are “untracked“, to have SVN track them do:
$ svn add main.c
A   main.c

Now “main.c” was added to the database and is being “tracked” (as in looked at) by Subversion, this means that if you modify the file and run $ svn stat, something will happen (more on that later). It can be painfull to add one file at a time, luckily SVN supports bash wildcards, to add everything do:
$ svn add *
A   lib
A   lib/lib1.h
A   lib/lib2.h
A   lib/lib1.c
A   lib/lib2.c

At this point, if you run the last two commands you’ll get a warning saying “main.c” is already under version control (as you previously put it). Now you decide it would be a good time to upload your files to the server, but it may be the case someone else have modified some other files while you were doing your stuff (SVN allows for several people to be working on the same file at the same time, but it can be a tricky business), to fetch the last snapshot of work you do:
$ svn up
Updating ‘.’:
At revision 1.

This will ensure you have the latest snapshot of everyone’s work! You should run this command before “uploading” your work to the server (we call it doing a commit). Doing a commit is not just uploading everything you have to a server, if a file hasn’t been changed there’s no need to reupload it, when committing every file uploaded is given a timestamp, an author label and even a commit message, to do so:
$ svn commit –message “Created the repository, added a working copy of stuff”
Authentication realm: <https://wordpress-svn-tutorial.googlecode.com:443&gt; Google Code Subversion Repository
Password for ‘root’:
Authentication realm: <https://wordpress-svn-tutorial.googlecode.com:443&gt; Google Code Subversion Repository
Username: tiago.gala@gmail.com
Password for ‘tiago.gala@gmail.com’:
Adding         lib
Adding         lib/lib1.c
Adding         lib/lib1.h
Adding         lib/lib2.c
Adding         lib/lib2.h
Adding         main.c
Adding         makefile
Transmitting file data ……
Committed revision 2.

Congratulations you have just done your first commit! It’s true, you can check it on your repository’s web interface. Now everytime you do some changes and want to upload them to the webserver do:
$svn up
$svn stat
$svn commit –message “Something to say here”

For example, suppose you want to remove makefile as you don’t longer need it and you also did some changes in main.c, there’s s a special command to remove files, use it or you’ll risk breaking something in the repository!
$svn up
Updating ‘.’:
At revision 2.
$ svn del makefile
D     makefile
$ svn stat
D     makefile
M    main.c

This will automatically delete makefile and as you can see by the ‘D’ and SVN also knows main.c was modified as stated by the ‘M’ on the stat command. Finlly you’re ready to commit your changes:
$ svn commit –message “Changed something here”
Sending     main.c
Deleting     makefile
Transmitting file data.
Comitted revision 3.

Repositories are very useful as a back up system, as a sharing point for collaborative projects and, of course, as a way to track your code, with a few simple commands you’ll be able to do pretty much all the basic stuff a revision control system allows you to do. Use it for your presentations at school, for your code snippets for your hobby, for your hardware layouts and specially use it for sharing!

Anúncios

Speeding up ArchLinux boot

A few months ago I decided to give Arch a try, I was a bit fed up with the latest Ubuntu releases and I was in need for something new and challenging. The installation process might be a bit scary but it’s quite straight forward as long as you have a hint of what you’re doing. After installing the OS and setting up my basic needs (X, Gnome3, Firefox, internet connections) I quickly noticed the boot process was rather slow for such a light system.

Attention: if you’re going to follow this guide you’ll be changing essential OS files, although unlikely, you might render your machine unusable, do this at your own risk. If you stick to this guide you should be fine :)

After searching in our good ol’ friend Google, I found that some modules were delaying the boot-up (“Waiting for UDev uevents to be processed… [BUSY]”), udev was giving me timeouts, end shouting something along this line:

udevd[132]: seq 1077 '/devices/pci0000:00/0000:00:1c.0/0000:03:00.0' killed

To troubleshoot this, write this in the terminal (with the appropriate replacements):

lspci -vin -s "0000:03:00.0"

There should be a line saying “Kernel driver in use: brcmsmac”, brcmsmac is my wireless card driver and for some reason is delaying udev. To solve this, edit the file /etc/rc.conf and look for a line saying something like “MODULES=(…)” and add your kernel module (driver) to the modules list (if it’s empty just leave it like this: MODULES=(brcmsmac), if there’s some module already on the list add your module to the end of the list: MODULES=(modA modB brcmsmac)), save the file and reboot, you should now notice a improvement in the boot time.

Again, if during boot-up the computer hangs after “Starting Network… [BUSY]”, edit /etc/rc.conf and on the line that goes DAEMONS=(syslog-ng network crond dbus) place an “@” in front of “network” so it reads “@network”, this will start the process on the background and won’t delay the boot up.

Check it out on this links: http://igordcard.blogspot.com/2012/01/waiting-for-udev-events-to-be-processed.html

Editing rc.conf

Arch Linux related forum post

Xmas music!

It’s Christmas time! It’s time for every geek out there who’s into electronics make their microcontrolers shout out, obviously: Star Wars theme songs!! Yeah, I know you got it right away :)
It all started a few months ago, I started tinkering around with the Arduino but this time I was looking for code/space efficiency instead of plain ease of use so I installed Eclipse. At first I played around with the hardware available: good old Arduino but then I found some cheap USBASP AVR ISP programmers on ebay and bought one (actually 2) so I could get started messing around with standalone microcontrolers. After many hours around the maths and constantly switching back and forth between an ATMega8 and an ATtiny2313 (since it’s Christmas I was planning on adding some light decoration as well, fading LED’s etc.) I ended up with this:

The setup seems messy but it is actually quite simple: from the USBASP programmer we get the MOSI, RST, SCK and MISO. VCC and GND should come from and external Power Supply Unit but right now this signals are coming from the programmer. To wire everything up, you just need to check the datasheet for the appropriate microcontroler (in this case an ATMega8) and wire everything to the correct place (MOSI to MOSI, GND to GND, SCK to SCK and so on), you should pay special attention to AREF and AVCC (Analog Reference and Analog Vcc) which should be connected to VCC, there also should be a 10KΩ pull-up resistor connecting RST to VCC and a decoupling capacitor between GND and VCC something bigger than 10nF (ceramic), to filter electrical noise. Since I don’t own an oscilloscope, I had my multimeter measuring the frequency of the output wave. Besides the standard setup, there was a piezoelectric buzzer connected between PB1 and GND.

Now for the maths: I decided to use the ATMega8’s internal oscilator which runs at 1MHz, so I have a resolution of 0.000001s, I googled the piano key frequencies, we know that T = 1/F so in a neat Excel table (ok, it wasn’t Excel, it was LibreOffice’s Calc) I got the period for each note rounded to the 6th decimal place so it would be a natural multiple of our base frequency. I just did the calculations for 3 octaves because I’m lazy, you’re free to do the whole piano range. For the rythm I figured it would be easier to simply make a direct relationship between the note’s time length and it’s numeric value (in western regular music notation it’s exactly the opposit, a quarter is twice as long as an eighth), so I set my time base as a 16th note and everything is written based on the amount of 16th notes that fit inside the desired note. For the note pitch, it goes as 1 is a C3, 2 a C3#, 3 is a D, and so on.
All the tables are on the “music.h” header file:

/*
 * music.h
 *
 *  Created on: Dec 21, 2011
 *      Author: tiago
 *
 *		pitch = controlo de frequências - periodo de uma nota em µs (clock a 1MHz)
 *		music = ordem das notas segundo o array pitch
 *		tempo = duração de cada nota
 *
 *		music e tempo têm que começar SEMPRE com 0 para facilitar as contas no programa.
 */

#ifndef MUSIC_H_
#define MUSIC_H_

volatile unsigned int pitch[] = {0,	//pausa
								1911,	//c2
								1804,
								1703,	//d
								1607,
								1517,	//e
								1432,	//f
								1352,
								1276,	//g
								1204,
								1136,	//a
								1073,
								1012,	//b
								956,	//c3
								902,
								852,
								804,
								759,
								716,
								676,
								638,
								602,
								568,	//a3
								537,
								506,
								478,
								451,
								426,
								402,
								379,
								358,
								338,
								319,
								301,
								284,
								268,
								253,
								239};

volatile unsigned char music[] = {0, 8, 8, 8, 4, 11,
								8, 4, 11, 8,
								15, 15, 15, 16, 11,
								7, 4, 11, 8,
								20, 8, 8, 20, 19, 18,
								17, 16, 17, 0, 9, 14, 13, 12,
								11, 10, 11, 0, 4, 7, 4, 7,
								11, 8, 11, 15,
								20, 8, 8, 20, 19, 18,
								17, 16, 17, 0, 9, 14, 13, 12,
								11, 10, 11, 0, 4, 7, 4, 11,
								8, 4, 11, 8};

volatile unsigned char tempo[] = {0, 4, 4, 4, 3, 1,
								4, 3, 1, 8,
								4, 4, 4, 3, 1,
								4, 3, 1, 8,
								4, 3, 1, 4, 3, 1,
								1, 1, 2, 2, 2, 4, 3, 1,
								1, 1, 2, 2, 2, 4, 3, 1,
								4, 3, 1, 8,
								4, 3, 1, 4, 3, 1,
								1, 1, 2, 2, 2, 4, 3, 1,
								1, 1, 2, 2, 2, 4, 3, 1,
								4, 3, 1, 8};


#endif /* MUSIC_H_ */

And finally: the main program is all based on interrupts from Timer0 (controls how long a note has been playing) and Timer1 (it’s a 16bit timer, it generates a square wave with the desired frequency):

/*
 * buzzer.c
 *
 *  Created on: Dec 9, 2011
 */

#include <avr/io.h>
#define F_CPU 1000000UL
#include <util/delay.h>
#include "music.h"
#include <avr/interrupt.h>

#define DELAY_LINE 10

unsigned int counter = 0;
unsigned int i = 0;


int main(void) {


	DDRB |= (1<<PB1);										//port as output
	//timer 1
	TCCR1A |= (1<<COM1A0)|(1<<WGM11)|(1<<WGM10);			//toggle on compare match
	TCCR1B |= (1<<CS10)|(1<<WGM13)|(1<<WGM12);				//prescaler = :1

	//timer 0
	TCCR0 |= ((1<<CS01)|(1<<CS00));									//prescaler = :1
	TIMSK |= (1<<TOIE0);									//interrupts on overflow

	sei();													//enable interrupts


	while(1) {

	}

	return 1;
}

ISR(TIMER0_OVF_vect) {
	if (counter >= tempo[i]*DELAY_LINE) { //avança de nota
		counter = 0;
		i++;
		OCR1A = pitch[music[i]];
	}

	if (counter >= tempo[i]*DELAY_LINE-1) { //pausa para separar as notas
		OCR1A = 0;
	}

	counter++;
}

Sometime soon I’ll add a pushbutton that will alow you to start the song all over again, then I’ll put all this on a standalone cardboard box, batteries included, then some kind of support for printed transparency paper. With a mechanical encoding I can sense wich transparency is on the tray and play the corresponding track and shine my LED’s in all their glory, projecting awesome pictures with soundtrack on the walls (of course, a christmas tree and Darthvader, what else?)!
As soon as I can I’ll try to add the source for some real Christmas music :)

Microsoft fonts in Ubuntu

Ubuntu comes with a pretty decent font collection, well suited for the common user needs (thanks Mark) but sometimes you’ll get through websites and documents who call for those Microsoft fonts and when your computer lacks them the document might lose it’s desired impact or, god forbids, it’s layout! So if you’re using linux/Ubuntu you probably miss the Microsoft fonts, to fix it, just install this software package:

$ sudo apt-get install msttcorefonts

Along the installation some End User License Agreement‘s (EULA) will pop up, answer them positively and after a while the fonts will be installed. All the fonts installed are true type and are listed here:

  • Andale Mono
  • Arial Black
  • Arial (Bold, Italic, Bold Italic)
  • Comic Sans MS (Bold)
  • Courier New (Bold, Italic, Bold Italic)
  • Georgia (Bold, Italic, Bold Italic)
  • Impact
  • Times New Roman (Bold, Italic, Bold Italic)
  • Trebuchet (Bold, Italic, Bold Italic)
  • Verdana (Bold, Italic, Bold Italic)
  • Webdings

To start using the fonts right away you can either log-out (the dumb version) or do it the awesome way:

$ sudo fc-cache -fv

This will update the font database  to the newest version. To install fonts you downloaded from the internet or created yourself (you are awesome!) simply put them in ~/.fonts/ and update the font cache by running the above command. Note that when you launch most programs they will read the font cache and keep that list in memory, this means that if you install a new font after a program is running that program will only know about that font after you close it and open it again.