Wednesday, June 30, 2010

Getting started with Arduino on Freetronics TwentyTen

Barely a week ago, I was happily unboxing my Freetronics Arduino starter kit (available here in Australia) and the Getting started with Arduino book by Massimo Banzi one of the 5 developers of the Arduino environment. The introductory chapters of the book are well written and you get a good sense for the passion of Massimo for electronics and prototyping. The obligatory what is electricity and the water pump analogy out of the way, it gets stuck in the first sketch - making an LED blink. This is relatively easy to achieve and a nice quick and easy success for the beginner.

The next challenge was to use a push-button switch to add some interactivity to the mix. This is where the lack of an endorsed starter kit makes following the book a challenge. There isn't enough of an introduction to the electronics components required (other than explaining where they can be purchased). The diagram uses a full-size bread board which is different to the tiny breadboard that comes in the Freetronics starter kit. Still, its not impossible to figure out. My circuit is shown below:



The LED is partially obscured but the other connections can be easily figured out. A slightly more challenging tutorial is available here.

Friday, June 25, 2010

SQLCMD CSV Output

SQLServer (both express and full versions) provide a useful command line tool (sqlcmd) which you can use to run batch tasks. See the full list of options here. Here's a quick way to get usable CSV output from this tool.



The main things to watch out for are specifying the correct delimiter.
  • -s: For Comma-Seperated-Values (CSV), the value of -s ","works a joy.
  • -W: The -W option removes the trailing whitespace.
  • -h: Setting this to -1 removes the headers of the fields (if your query is returning a result set).
  • -i: This is the input file (the file with the sql to be executed). For interactive scripts and to make your batch file resuable, set this to %1 to use the first command line argument
  • -o: This specifies the output file
  • -S: The server.
  • -U: The user
  • -P: Password
The complete version of the reusable script with comments and usage:


I'm still working on how to remove the number of rows affected line after the result set.

UPDATE: Setting NoCount On (Set NotCount On) in the SQL file disables this behaviour.

DOS Scripting Text Parsing

I've been most unfortunate to have to be involved in some Windows scripting at work. What is that you say? A colleague of mine recently needed a script that rearranged some images and icons based on some fields in a database. I started off with exporting the database records into a comma separated file.

A few tips about DOS scripting:
  1. www.dostips.com: One of the best resources on the web on DOS.
  2. REM: Useful for commenting your scripts. I cannot emphasize enough how important it is to comment your scripts.
  3. @echo off: Setting this at the beginning of your script stops the script from printing out the commands it is executing. This makes the output nice and tidy, printing only when echo is called explicitly. Not setting it is good for debugging.
Lets start of with some simple examples and pitfalls:

Parsing text and printing out line numbers:
See the script listing below:


Note that the delims option is set to blank (delims=). This is to counter the fact that spaces and tabs are the default delimiters so you need to override this. Another interesting aspect in the script is the use of the "enabledelayedexpansion". This allows variables in for loops to be evaluated correctly.

Splitting a line delimited by commas
Once your lines have been split, you may want to split the individual lines either by comma (for CSV files) or other delimiters.


This will display the first 3 words of each line after line.

Update: I've found a site with interesting tutorials on DOS.

Tuesday, June 22, 2010

Arduino Unboxing

Finally after a few days of waiting, my Arduino (I/O) board has arrived and I can get stuck into some prototyping. For those not in the know, Arduino is an open source physical computing platform based on a simple microcontroller board.

Pics below of my box...




More soon on my exploits and the abilities of this nifty little board. Did I mention its dead easy to program?

Wednesday, June 9, 2010

Perils of a Polyglot - part 2

This is part 2 of my Perils of a Polyglot series. I looked at tokens to mark identifiers ($ vs @) and where to put semicolons (;) to terminate statements. I'll continue this rant looking at other pet hates.

3. if, elseif, else if, elif, end if, then, else:
This is probably a point about scope, whitespace and indentation (I'll whinge about that later). Different programming languages have different ways of doing pretty much the same thing. I must say the most annoying of them all I find is Python. Why? First, 'elif' for 'elseif'. Second, parenthesis outside the condition is optional. That may be convenient but I always pause a second thinking is it right?

if request == "good":
#do something good
elif request == "bad":
#do something bad
else:
#do nothing

You can also do:

if (abooleanVariable):
#do somethihing

VB.NET takes the Pseudo code analogy a bit to serious for me. If ... Then ... ElseIf .. Then .. End If. I always wonder, "If End If is two words, why is ElseIf one word?". Another interesting point is the fact that an If statement spanning a single line does not need an "End If". Example below:

If Not anObject Is Nothing Then anObject.ToString()

VB.NET also brings you niceties such as no parentheses around the conditions and real worlds for the boolean operators (And, Not). Thankfully the Visual Studio IDE should keep you from straying with its auto correct features that include correcting the case of reserved keywords. Nice.

Finally, how the major programming languages handle scope within conditional statements (This could be repeated when b***ching about indentation and whitespace) :

Curly Braces:
  1. Java
  2. C++
  3. C#
  4. PHP
  5. Javascript

The Rest:
  1. Python (Indentation)
  2. Ruby (Keywords: end)
  3. VB.NET (Keywords: Then, EndIf)
Enough about conditionals, I'll try cover the rest of the issues in one post next time.

Tuesday, June 8, 2010

Perils of a Polyglot

A polyglot is linguist - a person who speaks more than one language. Most programmers worth their salt tend to be polyglots in the never ending quest to improve productivity and I'm no different. My day-to-day programming is most in Java and .NET languages VB.NET and C# as I'm mostly doing enterprise development and integration work. My website runs on Joomla! in PHP and I occasionally write some modules for it. The rest of the small projects I work on are some Python (for Google App Engine), Ruby (on Rails) and I'll include Javascript because with node.js and coffee script, I'm doing quite a bit of Javascript.

One of the greatest pains of jumping between different programming languages is the different syntax rules and the obscure error messages you get when you violate these rules. You stare at the code, the error message and nothing makes sense, especially if you are not using an IDE. It was when I was facing such a mistake that I got the idea for this post. So I'll start with that.

1. In PHP variables are denoted with '$' not '@'.
You can guess that I'd been doing some Ruby programming before this. The code I wrote was:

@usr = new StdClass();

The error message was 'Parse error: syntax error, unexpected '=' in on line '. How cryptic is that! I should have typed:

$usr = new StdClass();

However, just @usr; is a valid PHP statement!

2. Where to put those semicolons?
Semicolons are neat statement delimiters. Where to put them and where not to is an error that's easy to make and easy to pick up but worth a mention.

Of the programming languages I use the following use semicolons:
  1. Java
  2. Javascript
  3. C++
  4. C#.NET
  5. PHP
And the languages that don't:
  1. Visual Basic .NET (and VB6)
  2. Python
  3. Ruby
Next post I'll look at the "if, elseif, else if, elif, end if, then" dilemma, Case Sensitivity, Whitespace (tabs vs space), Comments and much more. Share your pain in the comments.

Casein CMS on Heroku

When evaluating some Ruby on Rails CMSes I mentioned Casein (which I wouldn't really classify as a CMS). Its more of a plugin that provides admin authentication and pretty CRUDification of resources. I'm doing my development staging on Heroku and needed to ensure that it runs on Heroku. Here's a couple of Gotchas!

Gems
Gems on Heroku are installed using a .gems manifest file. 2 gems required: calendar_date_select (>=1.15) and will_paginate (>=2.3.11). Adding these two gems to the manifest will ensure they are installed when you push the application.

calendar_date_select --version '>=1.15'
will_paginate --version '>=2.3.11'

You'd be forgiven for thinking this would resolve the issue, however, there is another reference to the gems as part of the plugin that causes the application to fail. In /vendor/plugins/casein/init.rb comment out the two lines that configure the gem.

#config.gem "calendar_date_select", :version => '1.15'
#config.gem 'mislav-will_paginate', :version => '~> 2.3.11', :lib => 'will_paginate', :source => 'http://gems.github.com'

Caching
As part of the magic the Heroku fairy godmother provides (Building upon the AWS and S3 storage service), the clock strikes 12 when you attempt to write to the file system. Simple, Heroku slugs operate in a readonly context. The caching currently written in Casein, primarily for static resources like Stylesheets and Javascript, attempts to write to the file system. The first instance you will encounter this is on the authentication page. The layout file is in /vendor/plugins/casein/app/views/layouts/casein_auth.html.erb. Change the line:

<%= javascript_include_tag :defaults, "/casein/javascripts/login.js", :cache => "casein_auth_javascripts" %>

to:

<%= javascript_include_tag :defaults, "/casein/javascripts/login.js" %>

That should get you up and running. My next stumble was running the casein:init rake task but as I'm probably not sticking with the platform I may or may not investigate a solution for that.

UPDATE: Not sure what's wrong with the rake task, but the task is not doing much. See /vendor/plugins/casein/tasks/casein_tasks.rake to see what the task does. It runs the migration, removes all existing users and then creates the admin. If you've already run this locally, I'd suggest simply using heroku db:push to transfer your local data to Heroku. Sadly I'm not going ahead with Casein so this is it for me.

Monday, June 7, 2010

Ruby on Rails customizable CMS

As mentioned in a previous post, I'm staging my coming-out-of-the-ruby-on-rails-closet party with the rewriting of my scheduling system, Chex. The system was coded from scratch on python/django on the Google App Engine. One of the things I didn't like about the original application was the fact that the static content was tightly coupled to the functionality of the application. My experience with this blog and my website is that it is much simpler to update the content using specialized systems. Especially as I program in many different languages, if I want to quickly change some presentation aspects and my day programming job does not include that language, I don't want to be fiddling with code, sticking semicolons where they don't belong!

My initial googling returned several possible options I could use, including Radiant, BrowserCMS, ... Here's a dated list of the top 20. I found overall that there were a handful that were mature in the classic CMS sense (coming from someone who's website runs on Joomla!) and off course plenty that were experimental and flaky. I'll leave the critique to others.

This evaluation process got me thinking about what a CMS is and what I was really after. I needed to do registration, authentication and quickly managing static content. I also needed something that would drop into my application. After reading an insightful article on how Rails + Plugins effectively equated a CMS, I'm feeling upto the challenge for creating a DIY which hopefully can become reusable in time.