Saturday, August 3, 2013

So you think you can program?

A big mistake that I did when I began my internship a long time ago was to think that knowing a programming language, its syntax, and its standard libraries were suffice. How ingenious I was!  As soon as start talking to experienced programmers that something else was needed to grasp the "The Art of Computer Programming" (after all those years I cannot help to agree more with Donald Knuth that programming is an art).

A graduation course on Computing is focused in teaching on how to solve problems in  effectively and efficiently way.  It is going to teach you one or more programming languages in order to illustrate  the concepts and only for that reason. In my case Pascal was used to introduce Algorithms, C++ for OOP and Operating System Concepts, SQL for Databases, and Lisp and Prolog for functional and logic programming respectively.

But Programming in way that is readable to others, maintainable, efficiently and less prone to bugs is something that you learn in your  daily practice by failing  or by listening to more experienced programmers. Here is a list of books that I recommend reading. Most of them are on my pending list, but I hope to mend it soon as I finish my Masters. 

Programming Style and Attitude

Code Complete by Steve Mcconnell 

During graduation I was presented to the Software Engineering text books written by Pressman and Somerville. That make thought  that doing good software was a simple task. It was just a matter of having a good, well documented and dogmatic process and everything would be fine. This book showed me the light.  The success for a software project depend on the quality and attitude of the developer team. This books teaches about good code style and practices, truths about software development and programmer ethics.
This a mandatory reading  for sure!

Clean Code by Robert C Martin

It is on my queue. I've just read until the beginning of the 2nd chapter. But it is clearly a good book about coding style. It comes  with tons of code to be read. The author presents why it is wrong  and how would it be better rewritten. 

Object Oriented Programming

If you think OOP is just about inheritance, polymorphism and composition, you are very mistaken.  Design Patterns are clever solutions to common problems that appear during design. However, they are more than just recipes or how-to's. Learning the patterns really teaches you to efficiently use the power of OOP.

Refactoring by Martin Fowler

You create some good design. Then you code. It is wonderful. Then you see a bug, and fix it. Then you add one more feature. Then another bug fix. You code start deteriorating, "smelling"  Martin Fowler would say.  This book is about reverting the game.

Design of Algorithms

Introduction to Algorithms by Cormen et al.

This is the book about algorithms. If you had done any serious CS course you have already heard of it.

By the way,  Professor Leiserson's lectures, one of authors of the book are available on the MIT OpenCourseWare 

Effective Series

You must be able to solve a problem independently of the programming language. You must also be able to choose the language according to the problem. If you are doing kernel programming you are probably gonna use C. But you are gonna use Python or  Java for an user application.  Anyway, once you choose the language you ought to know how to use it efficiently 

Saturday, April 13, 2013

Simple way to access GPIO on Raspberry PI

Got yourself a Raspberry Pi and you are using it as a cheap Linux box or Multimedia Center? Wanna play with electronics and interface your Pi with Arduino shields? So you have to learn how to control the GPIO (General Purpose Input Output) port of your Pi.

If you programming in C/C++ there is WiringPI. It is a library that provides an Arduino-like API. If you are using Python you can use Adafruit's library.

But if you do not want to install any library or if you are using another language besides C or Python? Fortunately the RPi kernel exports the GPIO pins to sysfs.

Sysfs, like the procfs, is a Linux pseudo-filesystem. It looks like regular files and directories. You can read them, write to them... But if you do a ls -l  you are going to see that they have zero size. When you read or write to a file, the calls are intercepted by the Linux Virtual File System layer which delivers the calls to the proper implementation of file system. So if the file is in a ext3 partition, the ext3 file system drive is called. In our case the calls are passed to the sysfs implementation which will configure the GPIO ports.

Before telling you how to use  the GPIO pins from sysfs, let me show you a simple schematic to test the GPIO:

The pin 24 (GPIO8) is connect to the LED. The LED is connect to an 1 kilo ohms pull down resistor that will protect our LED and Pi. The resistor is connected to pin 25 (GND). I want to control whether the LED will be lit. So I need the GPIO8 to output 1 ( or 5 volts) to lit the LED.

First we need to set GPIO8 to be a output pin. I am going to use shell script for that.

#We need to be the super user to use the gpio
sudo -i

#Set GPIO 8 to output 
echo "8" > /sys/class/gpio/export
echo "out" > /sys/class/gpio/gpio8/direction

To blink the LED we just need to write ones and zeros to the GPIO8:
while :
    echo "1" >  /sys/class/gpio/gpio8/value
    sleep 1
    echo "0" > /sys/class/gpio/gpio8/value
    sleep 1

One you are done you must free the pin

echo "8" > /sys/class/gpio/unexport

Here is a video of how it works on real life: