Saturday 17 August 2013

Why do things feel as if they're going backwards now?

I seem to remember, for a short period in the late nineties, when things seemed to be getting better. Society seemed to be making genuine progress, albeit in baby steps. Tolerance, acceptance, and common sense seemed to be gradually pushing bigotry, intolerance and ignorance into the past where they belong.

Or maybe I was just the right age at the right time, and was merely observing the world through my last scraps of youthful optimism and naivety as I entered adulthood.

Wednesday 27 March 2013

Things I Hate #344

Just a quickie, but in the long, long list of things that really irritate me..

SQL Databases that insist on enclosing table/field names with other characters. Usually quotes or square brackets.

SQL is an amazing language. On the face of it, it's incredibly arcane, but it's surprisingly versatile and powerful. However, some implementations really keep it stuck in the 1970s.

I'm using Apache Derby for the first time, and considering it's a modern project, I'm surprised I have to use quotes around table and field names.

Is it really necessary to have to do this:

SELECT
 "project"."proj_name",
 "project"."proj_root"
FROM
 APP."project"
WHERE
 "proj_id" = 5

And let's not even get on to update queries, where you have to offensive things like this:

UPDATE
 APP."project"
SET
 "proj_name" = '',
 "proj_path" = ''
WHERE
 "proj_id" = 5

It's just ugly, and unnecessary when other engines manage to cope perfectly fine without it.  Even Microsoft's SQL Server can cope without those hideous square brackets everywhere that it used to insist upon.

Wednesday 27 February 2013

LedBorg Server, Client and Android app


The new HTML interface
Since the original LedBorg Vala project, I have been working on a total re-write, moving it on from a single source file experiment. It's now been split into libledborg which is a shared library, and ledborg-server which is the new version of the server, which has a cleaner and more consistent protocol (though very similar to the version in the original project), and a vastly improved HTML interface shown here.

I've also used libledborg to not only build the new ledborg-server, but to also build a separate client package, imaginatively titled ledborg-client. This is a simple command-line client that could be called from scripts to communicate with the ledborg-server.


ross@raikkonen:~$ ledborg_client --help
Usage:
  ledborg_client [OPTION...]

LedBorg Client 0.1.0 is a simple command-line client that allows
 network control of an LedBorg add-on for the Raspberry Pi
 by talking to LedBorg Server

Help Options:
  -?, --help                              Show help options

Application Options:
  -p, --port                              Specify which port to connect on
  -h, --host=HOSTNAME or IPADDRESS        Specify the LedBorg server to connect to
  -c, --colour=LEDBORG COLOUR TRIPLET     Specify the colour to send to the server
  -o, --operation=get/set                 Operation to perform
  -V, --version                           Show version information

Both of these new packages have been developed using the GNU build system (autotools), which has been a bigger learning curve than Vala itself! In addition, to complete the learning experience, I've been using the git SCM to manage the code.

Android ledborg client
Finally, I've been firing up the Eclipse IDE to develop a basic Android client app. This has, of course, been in Java rather than Vala.

The source code to all three packages is now available on github.



The MagPi Magazine

This is going to be a quick one. Just to say thanks to the awesome people at The MagPi magazine for publishing my article on programming the Raspberry Pi and LedBorg add-on with Vala (based on my previous blog post).

The article appeared in the Feb 2013 issue, which can be downloaded as a PDF or viewed online here.

Saturday 8 December 2012

Raspberry Pi + LedBorg + Vala = Network controllable mood light

Raspberry Pi

So my first real attempt at a project using my new Raspberry Pi has already made me re-discover my love of coding outside of work. It's been far too long since I last pulled an all night-er, coding a pet project for fun; and I'm therefore eternally grateful to everyone involved with the Raspberry Pi project.

Recently I've been learning the Vala language, which is, as programming languages go, very much a new kid on the block. This isn't the place to explain in detail what Vala is, but in short, it's a C# style language that's built on the GLib object system (and as such, uses all the base GNOME libraries and subsystems). The compiler, valac, turns the Vala code into C, and in turn triggers the system's C compiler to produce native code. That sounds messy-er than it is, so it's worth checking out the project's home page at https://live.gnome.org/Vala

One thing I have noticed in the world of the Raspberry Pi, is that in the plethora of programming languages that are pushed, encouraged and provided, Vala is rarely mentioned.
Personally I think this is a shame, and so I'm sharing my first Vala-coded Raspberry Pi project in the hope that it'll help the language gain some momentum on the platform.

LedBorg

The central part of this project is a small, and very cheap add-on board for the Raspberry Pi called the LedBorg. It sits on the Pi's GPIO pins and has an ultra-bright RGB LED. Each channel - red, green and blue, has three intensities: off, half-brightness, and full-brightness. More about LedBorg can be seen at it's website http://www.piborg.org/LedBorg

The driver causes a device entry in the /dev filesystem to be created - /dev/ledborg
The simplest way to control the LedBorg is to echo colour values to this device - for example:
echo "202" > /dev/ledborg

The three digits correspond to R, G and B and can each have a value of 0, 1 or 2, which correspond to the three intensities as above. So, '202' makes the LedBorg light up bright purple (red+blue). '000' is 'black' - basically, off; and '222' is bright white.

Network Control

So after a short time playing around with the different colours, I wondered how easy it would be to control it over the network; and this project was created.

I decided that an easy way to achieve this was to use the well-known HTTP protocol. Using LibSoup in Vala, it's easy to set up a light-weight HTTP server that can respond to requests. I figured this would make it easy to test, as you could use any old web browser on the network to control it, so there was no need to write a client app too - at this stage, at least.

The server takes GET requests in the following URL format:
/?action=SetColour&red=x&green=y&blue=z
Where x, y, and z are integers between 0 and 2.

For the ease of getting started, the program also responds to all requests with a very minimal HTML form containing three drop-down selectors for the three colours, and a submit button.


The Code

To try out this code, you will need to have these packages installed - I'm assuming Raspbian/Debian is the running OS:
valac, libsoup2.4-dev plus any dependencies.

Copy and paste the code below into a new file using your favourite text editor, and save it as
LedBorgSimpleServer.vala

It can then be compiled with the following command:
valac --pkg libsoup-2.4 --pkg gio-2.0 --pkg posix --thread -v LedBorgSimpleServer.vala

I've included the -v flag so that it's more verbose and gives you an idea of what it's doing. If you want to see the C code that it generates, add the -C flag and it'll generate a new file LedBorgSimpleServer.c

So.. it's pretty rough-and-ready, but here's the code..


  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
/* LedBorgSimpleServer.vala
 * 
 * Author:
 *      Ross Taylor <ross@utonium.com>
 */


using GLib;
using Soup;


public class LedBorgSimpleServer : GLib.Object {

 // define the port number to listen on
 static const int LISTEN_PORT = 9999;

 // define the device file to write to
 static const string DEVICE = "/dev/ledborg";



 // main - this is the method that gets executed when the program is run
 public static int main (string[] args) {
  // set up http server
  var server = new Soup.Server(Soup.SERVER_PORT, LISTEN_PORT);
  
  // any requests from the client will be handled by our default_handler method
  server.add_handler("/", default_handler);
  
  // get the http server running and accepting requests
  server.run();

  return 0;
 }


 // default http handler
 public static void default_handler(Soup.Server server, Soup.Message msg, string path, GLib.HashTable<string, string>? query, Soup.ClientContext client)
 {
  // see if we're being asked to action a request
  if(query != null)
  {
   // check the 'action' url parameter just to make sure
   if(query["action"] == "SetColour")
   {
    // get red, green, blue values from url params
    string red = query["red"];
    string green = query["green"];
    string blue = query["blue"];

    // build our colour string
    /*
    The colour string should be three digits long,
    each digit representing red, green and blue respectively.
    
    Each digit's value should be either 0, 1 or 2
    corresponding to 'off', 'half brightness' or 'full brightness'
    */
    string colour = red + green + blue;

    // do colour change
    do_colour_change(colour);
   }
  }


  // build the html to send to the client
  string html = """
   <html>
    <head><title>LedBorgSimpleServer</title></head>
    <body>
     <form method="get" action="/">
      Red:
      <select name="red">
       <option value="0">Off</option>
       <option value="1">1/2 brightness</option>
       <option value="2">Full brightness</option>
      </select>
      Green:
      <select name="green">
       <option value="0">Off</option>
       <option value="1">1/2 brightness</option>
       <option value="2">Full brightness</option>
      </select>
      Blue:
      <select name="blue">
       <option value="0">Off</option>
       <option value="1">1/2 brightness</option>
       <option value="2">Full brightness</option>
      </select>
      <input type="submit" name="action" value="SetColour" />
     </form>
    </body>
   </html> 
  """;

  // send the html back to the client
  msg.set_status_full(Soup.KnownStatusCode.OK, "OK");
  msg.set_response("text/html", Soup.MemoryUse.COPY, html.data);
 }


 // do the colour change
 public static void do_colour_change(string colour)
 {

  // check file exists (if not, then either device or driver not present)
  File file = File.new_for_path(DEVICE);
  if(file.query_exists())
  {
   /*
   Here we use posix file handling to write to the file instead of
   vala's gio file handling, as we don't want the safety of
   GVFS getting in the way when operating in /dev
   */
   // open the file for writing
   Posix.FILE f = Posix.FILE.open(DEVICE, "w");
   
   // write the colour string to the file
   f.puts(colour);
  }
 }

}

Monday 23 July 2012

Pushing jpeg compression

Well my first post was going to be about g4s and the jubilympics, but in setting up this blog, I have found a more immediate thing to rant about.
In choosing to upload a custom background image, blogger gives you a rather insane restriction. Suggesting that you upload an image of at least 1800x1600 but with a max file size of 300kB. That's asking a bit much of jpeg compression really, assuming you want the image displayed to be more than just blocky compression artefacts.