C language style guides


I don’t have a particular style that I like, I do have a style that I use, frequently without being aware of it. Often, the style of the surrounding code determines my style. I tend to use as references links returned by Google for “c style guide”. I have found the following useful:

I don’t like using hungarian notation for primitive types, I prefer descriptive names and grep, or a good IDE.

Advertisements

FreeRTOS


FreeRTOS is a cross-platform, real-time operating system that has been ported to several architectures. It has a simple but powerful API to deal with tasks, scheduler, queues, semaphores and timers. Learning it is an enriching experience and a knowledge that can be reused widely due to its portability. Its source code is available under the MIT license and allows commercial use without having to pay royalties.

Tasks

The code snippet below demonstrates how a task can be created. Task creation may fail if there is not enough heap space available to allocate task structures and stack.

#include "FreeRTOS.h"
#include "task.h"

// a task
static void run(void *pvParameters)
{
  for (;;)
  {
    // do something useful, then block
  }
}

// create task
xTaskHandle h;
portBASE_TYPE p;
p = xTaskCreate(run,
	(const signed portCHAR *)"task",
	configMINIMAL_STACK_SIZE,
	NULL,
	tskIDLE_PRIORITY,
	&h);
if ( p != pdPASS )
{
  // handle error
}

FreeRTOS divides processor time equally among tasks with the same priority. Tasks with higher priority always preempt lower priority tasks, no other lower priority task will run until the higher priority task blocks. Tasks can block on calls such as vTaskDelay, vTaskDelayUntil, xQueueSend, xQueuePeek, xQueueReceive, and xSemaphoreTake.

To delete a task when done

// delete task
vTaskDelete(h);

FreeRTOS has three different memory allocation implementations, available in separate code files called heap_1.c, heap_2.c, and heap_3.c. You have to choose one of the three. If you choose heap_3.c, the C compiler’s implementation of malloc and free are used, the heap space is determined based on the linker configuration. If interested in AVR32, see Using the GNU Linker Scripts on AVR UC3 Devices.

Queues

A task needs to perform activities. Most tasks are implemented as endless loops that wake up on some event, perform some activity and go back to waiting. An advantage of using queues is that they are multitask safe and thus an effective synchronization mechanism between tasks. You can use them to queue activities that a task must perform, serializing their execution. This is useful if multiple tasks must have access to a shared resource controlled by a single task.

The code snippet below demonstrates how to create a queue

xQueueHandle queue = 0;
queue = xQueueCreate(10, sizeof(unsigned int));

To queue a message (from any task)

unsigned int id = 0;
xQueueSend(xQueueIO, (void *)&id, (portTickType)0);

To retrieve a message from the queue and process it (usually in a dedicated task)

portBASE_TYPE p;
p = xQueueReceive(queue, &id, (portTickType)0);
if (p == pdPASS)
{

}

To delete the queue when done

vQueueDelete(queue);

Timers

Timers are a useful mechanism to stop long running activities and to start activities at periodic intervals.

The code snippet below demonstrates how to use the tick counter maintained by FreeRTOS to implement timers. The portTICK_RATE_MS macro is used to convert milliseconds to ticks. If the port of FreeRTOS for your board is not configured properly the value of portTICK_RATE_MS may not be correct.

The following code sets up some declarations and variables required later

#include "FreeRTOS.h"

static portTickType tickUntil;
static portTickType startTick;

// assign tick count when timer elapses i.e. after 1000 ms
startTick = xTaskGetTickCount();
tickUntil = startTick + (portTickType)(1000 / portTICK_RATE_MS);

In the task function, you’ll have code similar to

currentTick = xTaskGetTickCount();
if (tickUntil > startTick
  && (currentTick > tickUntil
  || (currentTick < startTick
  && currentTick < tickUntil))) // no overflow of tickUntil
{
  // timer elapsed, do something
}
else if (currentTick < startTick && currentTick > tickUntil) // overflow of tickUntil
{
  // timer elapsed, do something
}

In the ticklines below, when tick count enters the red region after tickUntil, we consider the timer to have elapsed. The second tickline is for the case when tickUntil overflows.

WebKit/Gtk+ 1.6.1 with WebGL on PandaBoard


In WebKit/Gtk+ with WebGL on PandaBoard, I show how to build WebKit/Gtk+ with WebGL support. In this post I share the results obtained after building the recently released stable version 1.6.1. This build has been tested with Ubuntu 11.04 and Ubuntu 11.10.

Version 1.6.1 builds flawlessly following the build procedure from the earlier post. The source code is complete, no additional files are required from the WebKit svn. A note regarding package requirements, the package libgtkglext1-dev is not available on Ubuntu 11.10 and it is not required to build version 1.6.1.

WebGL works on PandaBoard, but FPS is painfully low due to usage of software rendering. Please comment below if you know how to enable hardware rendering at build or run time. This is the message that WebKit/Gtk+ shows on the console

** Message: console message: http://mrdoob.github.com/three.js/build/Three.js @225: Mozilla/5.0 (X11; Linux armv7l) AppleWebKit/535.4+ (KHTML, like Gecko) Version/5.0 Safari/535.4+ | WebGL 1.0 (2.1 Mesa 7.11) | Webkit (Mesa Project) | Software Rasterizer | WebGL GLSL ES 1.0 (1.20)

Here’s a screenshot of a three.js Context 3D demo.

Play WebM streamed over HTTP using GStreamer’s souphttpsrc


The pipeline below receives WebM video using souphttpsrc and plays it

gst-launch souphttpsrc location=http://127.0.0.1:9001 ! matroskademux ! vp8dec ! ffmpegcolorspace ! ximagesink

Check the manual page for souphttpsrc or the gst-inspect output for the element, for further details.

Send output to X clipboard from the command line


On Ubuntu, install the xclip package. Pipe output to xclip -selection clipboard.

ls | xclip -selection clipboard

OpenCV with Ubuntu on PandaBoard


This post is for those unfamiliar with OpenCV to obtain and build OpenCV with Ubuntu on PandaBoard.

Source code

Obtain the bzipped source package for Unix. Extract it with tar xvjf so that the source folder is created, e.g. OpenCV-2.3.1.

Additional packages

You’ll need to have the following packages to configure the build. I’ll assume you have the GNU compiler collection installed already.

  • cmake
  • cmake-qt-gui

Configure build

Execute cmake-gui from the command line. Set the source and binaries folders (see figure below for reference). Choose the Unix Makefiles as the build generation mechanism. Configure as appropriate and generate the files required by make. You can build static libraries by unchecking the BUILD_SHARED_LIBS option.

Build

Run make in the binaries folder. If all goes well, the binaries should be available in the bin folder and the libraries in the lib folder.

Run samples

Try and run the drawing sample from the bin folder. Here’s a screenshot of it running for reference.

Travel visa


How much bureaucracy can there be in getting a visa you ask? Here’s a list of requirements to apply for a Schengen visa at the Spanish consulate, so you get a feel for how much our countries mire us in endless paperwork.

  • Schengen visa form
  • 2 3×4 photos
  • Bank statement for the last 4 months
  • Salary receipt for the last 4 months
  • Copy of home ownership contract
  • Copy of IT statement
  • Copy of country ID
  • Copy of country of residence ID
  • Confirmed travel tickets
  • Confirmed hotel reservation
  • Invitation letters
  • Letter from company with signature verified/authenticated
  • Travel insurance
  • Payment receipt for travel insurance
  • Copy of all the pages of the passport
  • Passport
  • Visa fees (BRL 160.75, about USD 92 or 67 Euros)

All that for a visa that will last exactly eleven (the duration of my travel) days.