<?xml version="1.0"?><rss version="2.0">
<channel>
  <title>Alan&#039;s Ramblings - Tech category</title>
  <link>http://bleaklow.com:80/categories/tech/</link>
  <description>My opinions may be incorrect, but they are my own</description>
  <language>en</language>
  <copyright>Alan Burlison</copyright>
  <lastBuildDate>Wed, 29 Feb 2012 20:50:00 GMT</lastBuildDate>
  <generator>Pebble (http://pebble.sourceforge.net)</generator>
  <docs>http://backend.userland.com/rss</docs>
  <image>
    <url>http://bleaklow.com/images/misc/logo.gif</url>
    <title>Alan&#039;s Ramblings (Tech category)</title>
    <link>http://bleaklow.com:80/</link>
  </image>
  <item>
    <title>Why I&#039;m ditching the Arduino software platform</title>
    <link>http://bleaklow.com:80/2012/02/29/why_im_ditching_the_arduino_software_platform.html</link>
    <description>
          &lt;p&gt;
I&#039;m getting set up for my &lt;a href=&#034;http://global-grooves.org/JUNKJAM.aspx&#034;&gt;next project&lt;/a&gt; and decided to update my development environment.  I&#039;ve finally decided to entirely ditch the &lt;a href=&#034;http://arduino.cc/&#034;&gt;Arduino&lt;/a&gt; software environment and just use the boards.  I stopped using the Arduino IDE &lt;a href=&#034;/2010/06/04/a_makefile_for_arduino_sketches.html&#034;&gt;some time ago&lt;/a&gt;, but now I&#039;m going whole hog and ditching the Arduino library as well.  Why?  Well, it&#039;s simple:
&lt;/p&gt;
&lt;p&gt;
&lt;strong&gt;Significant parts of it are pile of junk.&lt;/strong&gt;
&lt;/p&gt;
&lt;p&gt;
I know that&#039;s a pretty strong statement, so I better back it up with evidence.  OK, let&#039;s start with the hardware serial IO code.  Before version 1.0 of the Arduino platform, although reading from the serial ports was interrupt-driven, writing wasn&#039;t.  Rather, the code went into a spin loop, polling the transmit status bit until the USART was idle before sending the next character.  Why was that a problem?  Well if you wrote a 80-character string at 9600 baud it would take (8 bits + 1 start bit + 1 stop bit) * 80 / 9600 = 0.083, i.e. 83 milliseconds.  That&#039;s a &lt;strong&gt;huge&lt;/strong&gt; amount of time for the CPU to be spending just to do some output.  I found a number of posts where people were complaining that doing reasonable amounts of IO screwed up all the other bits of their sketches, and no wonder.  Admittedly the &lt;a href=&#034;http://arduino.cc/en/Main/ReleaseNotes&#034;&gt;Arduino 1.0 release notes&lt;/a&gt; say that&#039;s been changed so that output now uses interrupts as well, but that&#039;s not the end of the problems.
&lt;/p&gt;
&lt;p&gt;
Let&#039;s take a peek at the &lt;a href=&#034;https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/HardwareSerial.cpp&#034;&gt;HardwareSerial.cpp&lt;/a&gt; class.  First thing to note is that two 64-byte buffers are allocated for each USART, even if it isn&#039;t used.  That&#039;s 128 bytes on a Duemilanove and 512 bytes on a Mega, or 6% and 12% of the available SRAM respectively.  On the Duemilanove that&#039;s reasonable as there&#039;s only 1 UART, but on the Mega it represents a significant waste of precious memory when only 1 USART is normally going to be in use.
&lt;/p&gt;
&lt;p&gt;
OK, let&#039;s look at the new &lt;a href=&#034;https://github.com/arduino/Arduino/blob/master/hardware/arduino/cores/arduino/HardwareSerial.cpp#L383&#034;&gt;&lt;code&gt;write()&lt;/code&gt;&lt;/a&gt; function that does interrupt-driven output:
&lt;/p&gt;
&lt;pre&gt;
size_t HardwareSerial::write(uint8_t c)
{
  int i = (_tx_buffer-&amp;gt;head + 1) % SERIAL_BUFFER_SIZE;

  // If the output buffer is full, there&#039;s nothing for it other than to
  // wait for the interrupt handler to empty it a bit
  // ???: return 0 here instead?
  while (i == _tx_buffer-&amp;gt;tail)
    ;

  _tx_buffer-&amp;gt;buffer[_tx_buffer-&amp;gt;head] = c;
  _tx_buffer-&amp;gt;head = i;

  sbi(*_ucsrb, _udrie);
  
  return 1;
}
&lt;/pre&gt;
&lt;p&gt;
Is there a problem?  Let&#039;s look at the definition of &lt;code&gt;_tx_buffer&lt;/code&gt;:
&lt;/p&gt;
&lt;pre&gt;
struct ring_buffer
{
  unsigned char buffer[SERIAL_BUFFER_SIZE];
  volatile unsigned int head;
  volatile unsigned int tail;
};
&lt;/pre&gt;
&lt;p&gt;
Oh dear.  &lt;code&gt;head&lt;/code&gt; and &lt;code&gt;tail&lt;/code&gt; are declared as &lt;code&gt;int&lt;/code&gt;, i.e. 16 bits, 2 bytes.  They are accessed by both the &lt;code&gt;write&lt;/code&gt; routine and the interrupt service routine that actually transmits the data yet there&#039;s no locking in the &lt;code&gt;write&lt;/code&gt; routine so the accesses aren&#039;t atomic.  Why is that an issue?  Well, the &lt;a href=&#034;http://www.nongnu.org/avr-libc/user-manual/group__util__atomic.html&#034;&gt;avr-libc documentation&lt;/a&gt; makes it clear:
&lt;/p&gt;
&lt;blockquote&gt;
A typical example that requires atomic access is a 16 (or more) bit variable that is shared between the main execution path and an ISR. While declaring such a variable as volatile ensures that the compiler will not optimize accesses to it away, it does not guarantee atomic access to it.
&lt;/blockquote&gt;
&lt;p&gt;
The documentation goes on to explain the sorts of symptoms you&#039;ll see if you ignore this, follow the link above if you want the full details.  This is inexcusably shoddy code - the constraints on accessing variables that are shared between ISR and non-ISR code are well-known.  What really concerns me is that people will use the Arduino code as an example of &#039;good&#039; AVR code and it isn&#039;t, in many places it&#039;s frankly awful.
&lt;/p&gt;
&lt;p&gt;
&#034;So what?&#034; you say, &#034;That&#039;s only one chunk of code that&#039;s a bit naff.&#034;  Unfortunately it&#039;s not an isolated instance.  Let&#039;s move on now to look at one of the newer features that has been added to the Arduino platform, the re-implemented &lt;a href=&#034;http://arduino.cc/en/Reference/StringObject&#034;&gt;String class&lt;/a&gt;.  Ok, let&#039;s build a minimal program that uses it:
&lt;/p&gt;
&lt;pre&gt;
#include &#034;WString.h&#034;
int main(void) {
    String bloat = &#034;hello world&#034;;
    return 0;
}
&lt;/pre&gt;
&lt;p&gt;
And let&#039;s build it:
&lt;/p&gt;
&lt;pre&gt;
WString.cpp: In member function ‘int String::lastIndexOf(char, unsigned int) const’:
WString.cpp:503:38: error: comparison of unsigned expression &lt; 0 is always false [-Werror=type-limits]
WString.cpp: In member function ‘int String::lastIndexOf(const String&amp;, unsigned int) const’:
WString.cpp:519:63: error: comparison of unsigned expression &lt; 0 is always false [-Werror=type-limits]
&lt;/pre&gt;
&lt;p&gt;
Sigh.  One would think that the Arduino developers would at least turn on warnings when they are compiling their code, but they don&#039;t.  And in this case, the consequence is a bug.  So, temporarily comment out the offending lines so we get a successful build, and:
&lt;/p&gt;
&lt;pre&gt;
/opt/avr-gcc/bin/avr-size build/test.elf
   text    data     bss     dec     hex filename
  10194      20       5   10219    27eb build/test.elf
&lt;/pre&gt;
&lt;p&gt;
Can that &lt;strong&gt;really&lt;/strong&gt; be right?  10K for a one-line program?  Unfortunately it is.  Any mention of &lt;code&gt;String&lt;/code&gt; pulls in the entirety of the class, as well as all the other avr-libc routines it references.  So on a Duemilanove that only has 32k to start with, a third of the available memory is gone before you start.  At the time the class was being rewritten I expressed my opinion that it was probably a bad idea and that the Arduino developers really needed to target the platform they actually had and not the one they wished they had.  And that&#039;s not the end of the issues with the &lt;code&gt;String&lt;/code&gt; class - on a constrained-memory platform such as the AVR, providing a class like &lt;code&gt;String&lt;/code&gt; that relies on &lt;code&gt;malloc&lt;/code&gt;, creates lots of temporaries, fragments the (tiny) heap and has no real ability to deal with out-of-memory conditions is a recipe for problems, problems that will manifest themselves as random, mysterious and un-diagnosable run-time errors.  And sure enough, a quick google shows that&#039;s exactly what tends to happen - just about the worst possible outcome for a platform that&#039;s targeted at neophytes.
&lt;/p&gt;
&lt;p&gt;
That&#039;s just two examples - there are others as well, such as the well-known performance problems with pin access, which may be up to &lt;a href=&#034;http://jeelabs.org/2010/01/06/pin-io-performance/&#034;&gt;50x slower that direct pin access&lt;/a&gt;.  In fact the only two remaining parts of the Arduino libraries that I still use are the millisecond clock and the serial IO, and they are easy enough to replace, so that&#039;s what I&#039;m doing.
&lt;/p&gt;
&lt;p&gt;
While I applaud the aims of the Arduino project, the realities of the restricted hardware platform &lt;strong&gt;have&lt;/strong&gt; to be taken into consideration.  In addition, one of the &lt;a href=&#034;http://code.google.com/p/arduino/wiki/DevelopmentPolicy&#034;&gt;aims of the project&lt;/a&gt; is to:
&lt;/p&gt;
&lt;blockquote&gt;
provide a well-designed, maintainable, and stable platform for the future
&lt;/blockquote&gt;
and despite its unquestionable success on many other fronts, on that one I feel the Arduino platform is less than entirely successful.  I for one won&#039;t be using any of the software any more, it&#039;s just not what I consider to be acceptable quality.
&lt;/p&gt;</description>
      <category>Arduino</category>
    <category>Tech</category>
    <comments>http://bleaklow.com:80/2012/02/29/why_im_ditching_the_arduino_software_platform.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2012/02/29/why_im_ditching_the_arduino_software_platform.html</guid>
    <pubDate>Wed, 29 Feb 2012 20:50:00 GMT</pubDate>
  </item>
  <item>
    <title>Scala snippet of the day</title>
    <link>http://bleaklow.com:80/2012/02/17/scala_snippet_of_the_day.html</link>
    <description>
          &lt;p&gt;
The following is useful for benchmarking code from inside the REPL:
&lt;/p&gt;
&lt;pre&gt;
def timed(op: =&gt; Unit) = {
  val start = System.nanoTime
  op
  (System.nanoTime - start) / 1e9
}
&lt;/pre&gt;
&lt;p&gt;
Then you can use it like this:
&lt;/p&gt;
&lt;pre&gt;
scala&amp;gt; timed { Thread.sleep(5000) }
res0: Double = 5.010083526
&lt;/pre&gt;
&lt;p&gt;
That&#039;s syntactic sugar for an anonymous function definition and a call of &lt;code&gt;timed&lt;/code&gt;.  If a function takes only a single parameter you can pass it inside &lt;code&gt;{ }&lt;/code&gt;, so if we wrote that out longhand it would be:
&lt;/p&gt;
&lt;pre&gt;
scala&amp;gt; def fn = Thread.sleep(5000)
fn: Unit

scala&amp;gt; timed(fn)
res0: Double = 5.010088248
&lt;/pre&gt;
&lt;p&gt;
The other neat bit is the use of Scala&#039;s pass-by-name mechanism to pass the &lt;code&gt;op&lt;/code&gt; parameter.  That&#039;s the funny-looking &lt;code&gt;op: =&gt; Unit&lt;/code&gt; argument list to &lt;code&gt;timed&lt;/code&gt;.  Normally Scala uses by-value parameters, i.e. the line &lt;code&gt;timed(fn)&lt;/code&gt; would result in the evaluation of fn, and then passing the returned value (in this case, nothing) into &lt;code&gt;timed&lt;/code&gt;.  However by using pass-by-name, the evaluation of &lt;code&gt;fn&lt;/code&gt; takes place inside &lt;code&gt;timed&lt;/code&gt;, and by wrapping it inside timing instrumentation we can time how long &lt;code&gt;op&lt;/code&gt; takes to execute.
&lt;/p&gt;</description>
      <category>Web</category>
    <category>Tech</category>
    <comments>http://bleaklow.com:80/2012/02/17/scala_snippet_of_the_day.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2012/02/17/scala_snippet_of_the_day.html</guid>
    <pubDate>Fri, 17 Feb 2012 14:04:00 GMT</pubDate>
  </item>
  <item>
    <title>Stupid BT router...</title>
    <link>http://bleaklow.com:80/2011/11/23/stupid_bt_router.html</link>
    <description>
          &lt;p&gt;
I have the misfortune to own a BT Business 2Wire router, and bleaklow.com sits behind that.  A while back it threw a fit and reconfigured itself and when I beat it into submission I didn&#039;t notice that it had also lost the port mappings for bleaklow.com.  As a result I&#039;ve been off-air for quite some time.  Bloody BT...
&lt;/p&gt;</description>
      <category>Web</category>
    <category>Tech</category>
    <comments>http://bleaklow.com:80/2011/11/23/stupid_bt_router.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2011/11/23/stupid_bt_router.html</guid>
    <pubDate>Wed, 23 Nov 2011 14:12:00 GMT</pubDate>
  </item>
  <item>
    <title>Hints on creating 256-colour themes for vim</title>
    <link>vim 256 colour</link>
    <description>
          &lt;p&gt;
Most existing &lt;a href=&#034;http://www.vim.org/&#034;&gt;vim&lt;/a&gt; colour themes are designed for the graphical version of vim, gvim.  They don&#039;t work properly at all if you are running vim in a terminal, and there are very few themes available for vim in terminal-mode. You are pretty much obliged to make your own if one of the small number of available ones doesn&#039;t suit.  Here&#039;s a list of hints and tips that I discovered in the process of creating one:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;If your shell&#039;s setting for &lt;code&gt;$TERM&lt;/code&gt; is &lt;code&gt;xterm&lt;/code&gt; you won&#039;t get colour support.  It needs to be &lt;code&gt;xtermc&lt;/code&gt; or you need to add a &lt;code&gt;set term=xtermc&lt;/code&gt; in your &lt;code&gt;.vimrc&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Even with the terminal type set you may not get 256 colours if you are using gnome-terminal.  To fix that, put &lt;code&gt;set t_Co=256&lt;/code&gt; in your &lt;code&gt;.vimrc&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;In vim, type &lt;code&gt;:help highlight&lt;/code&gt; to get instructions on how to specify colours in vim.&lt;/li&gt;
&lt;li&gt;To make the process of authoring a colour theme easier, install the &lt;a href=&#034;http://www.vim.org/scripts/script.php?script_id=85&#034;&gt;Mkcolorscheme.vim&lt;/a&gt; script in your vim plugin directory, uncommenting the four commands at the top of the file.  Read the instructions in the plugin, they give you hints on how to identify the colours currently being used by syntax elements.&lt;/li&gt;
&lt;li&gt;Open a new subwindow in vim and type &lt;code&gt;:so $VIMRUNTIME/syntax/hitest.vim&lt;/code&gt;.  That will display a table of the current settings, which will be updated as you interactively change colours.&lt;/li&gt;
&lt;li&gt;Open this &lt;a href=&#034;http://upload.wikimedia.org/wikipedia/commons/9/95/Xterm_color_chart.png&#034;&gt;colour chart&lt;/a&gt; in your browser, it gives you the colour numbers you&#039;ll need to set the colours.&lt;/li&gt;
&lt;li&gt;Open up a source file of the type that you want to set the syntax colouring for, put the cursor over an element and use the &lt;code&gt;:GetSyntax&lt;/code&gt; command to identify the element type, followed by the appropriate&lt;code&gt;:highlight&lt;/code&gt; command to set the element colour.&lt;/li&gt;
&lt;li&gt;When you are done, use the &lt;code&gt;:Mkcolorscheme&lt;/code&gt; command to generate a set of commands to generate the colour scheme settings.  Delete any lines containing &lt;code&gt;cleared&lt;/code&gt; as they aren&#039;t valid.&lt;/li&gt;
&lt;li&gt;Save the colour scheme to a file in your vim &lt;code&gt;colors&lt;/code&gt; directory and load the scheme with a &lt;code&gt;colorscheme&lt;/code&gt; command in your &lt;code&gt;.vimrc&lt;/code&gt;.&lt;/li&gt;
&lt;/ul&gt;</description>
      <category>Tech</category>
    <comments>http://bleaklow.com:80/2011/09/21/hints_on_creating_256_colour_themes_for_vim.html#comments</comments>
    <guid isPermaLink="true">vim 256 colour</guid>
    <pubDate>Wed, 21 Sep 2011 20:06:00 GMT</pubDate>
  </item>
  <item>
    <title>Beautiful Scala</title>
    <link>http://bleaklow.com:80/2011/08/23/beautiful_scala.html</link>
    <description>
          &lt;p&gt;
This is, of course, beautiful:
&lt;/p&gt;
&lt;pre&gt;
def msort[T](less: (T, T) =&amp;gt; Boolean)
      (xs: List[T]): List[T] = {
  
    def merge(xs: List[T], ys: List[T]): List[T] =
      (xs, ys) match {
        case (Nil, _) =&amp;gt; ys
        case (_, Nil) =&amp;gt; xs
        case (x :: xs1, y :: ys1) =&amp;gt;
          if (less(x, y)) x :: merge(xs1, ys)
          else y :: merge(xs, ys1)
      }
  
    val n = xs.length / 2
    if (n == 0) xs
    else {
      val (ys, zs) = xs splitAt n
      merge(msort(less)(ys), msort(less)(zs))
    }
  }

scala&amp;gt; val intSort = msort((x: Int, y: Int) =&amp;gt; x &amp;lt; y) _
intSort: (List[Int]) =&amp;gt; List[Int] = &amp;lt;function1&amp;gt;
scala&amp;gt; intSort(List(3, 5, 1, 6, 2))
res3: List[Int] = List(1, 2, 3, 5, 6)
&lt;/pre&gt;
&lt;p&gt;
This is an example of a recursive &lt;a href=&#034;http://en.wikipedia.org/wiki/Merge_sort&#034;&gt;merge sort&lt;/a&gt; implemented using a technique known as &lt;a href=&#034;http://en.wikipedia.org/wiki/Currying&#034;&gt;currying&lt;/a&gt;, expressed in the language &lt;a href=&#034;http://www.scala-lang.org/&#034;&gt;Scala&lt;/a&gt;.
&lt;/p&gt;
&lt;p&gt;
Some context:  I was looking for a Java-based scripting language to act as &#039;glue&#039; for my &lt;a href=&#034;http://oziapi-java.sourceforge.net/&#034;&gt;Java interface&lt;/a&gt; to &lt;a href=&#034;http://www.oziexplorer.com/&#034;&gt;OziExplorer&lt;/a&gt; which I&#039;m using for a fenceline survey I&#039;m doing for the &lt;a href=&#034;http://www.moorsforthefuture.org.uk/&#034;&gt;Moors For The Future&lt;/a&gt; project.  I initially looked at &lt;a href=&#034;http://groovy.codehaus.org/&#034;&gt;Groovy&lt;/a&gt; and came across an intriguing comment by James Strachan, the creator of Groovy:
&lt;/p&gt;
&lt;blockquote&gt;
I can honestly say if someone had shown me the Programming in Scala book by by Martin Odersky, Lex Spoon &amp; Bill Venners back in 2003 I&#039;d probably have never created Groovy.
&lt;/blockquote&gt;
&lt;p&gt;
That set me off on an exploration of the &lt;a href=&#034;http://www.scala-lang.org/&#034;&gt;Scala website&lt;/a&gt; which has lots of good tutorials and links, including one to an &lt;a href=&#034;http://www.simplyscala.com/&#034;&gt;online interpreter&lt;/a&gt; you can type examples into.  Scala is a really intriguing language - it sits on top of the JVM so you get &#039;for free&#039; Java&#039;s cross-platform support and access to the huge Java ecosystem, but Scala also blends together the features of some of the most influential languages that preceded it:
&lt;/p&gt;
&lt;ul&gt;
&lt;li&gt;The &#039;everything is an object&#039; nature of Smalltalk or Ruby&lt;/li&gt;
&lt;li&gt;The imperative nature of languages that have C or Java heritage&lt;/li&gt;
&lt;li&gt;Functional programming as per ML or Haskell&lt;/li&gt;
&lt;li&gt;Strongly typed but with sophisticated type inferencing that makes it look &amp; feel similar to Python or Groovy&lt;/li&gt;
&lt;li&gt;Uniform access to methods and fields, like Eiffel
&lt;li&gt;Actor-based concurrency similar to Erlang&lt;/li&gt;
&lt;/ul&gt;
&lt;p&gt;
Scala also adds new features of its own as well, such as abstract types, traits and extractors.  It&#039;s also very easy to write domain-specific languages in Scala.  James Strachan wrote a good &lt;a href=&#034;http://macstrac.blogspot.com/2009/04/scala-as-long-term-replacement-for.html&#034;&gt;summary&lt;/a&gt; of Scala, from which the above quote is taken.
&lt;/p&gt;
&lt;p&gt;
I also ordered a copy of &lt;a href=&#034;http://www.artima.com/shop/programming_in_scala_2ed&#034;&gt;Programming in Scala, Second Edition&lt;/a&gt; which I&#039;m enjoying reading and can thoroughly recommend.  Unlike many programming books it&#039;s well written with a light but not overly-chatty style.  It explicitly assumes you have programming experience, so it picks up pace pretty quickly.  As I&#039;ve been going through the book, most of my &#034;Why did they do that?&#034; questions have had good answers, which gives the impression that Scala has been very carefully thought through - always a good sign.
&lt;/p&gt;
&lt;p&gt;
Now all I have to do is to think of a substantial project I can use Scala on :-)
&lt;/p&gt;</description>
      <category>Tech</category>
    <category>Scala</category>
    <comments>http://bleaklow.com:80/2011/08/23/beautiful_scala.html#comments</comments>
    <guid isPermaLink="true">http://bleaklow.com:80/2011/08/23/beautiful_scala.html</guid>
    <pubDate>Tue, 23 Aug 2011 08:24:23 GMT</pubDate>
  </item>
  </channel>
</rss>

