Home > Distraction > Atom & Arduino :: Some Hacking (pt. 1)

Atom & Arduino :: Some Hacking (pt. 1)

When Tom Hawkins released the Atom package, I stole my friends Arduino Diecimila to see if I could get some Atom-generated code to work. Turns out it wasnt that hard! Ill be detailing this in the next few posts. Im going to use this first post to point out two things I changed in atom-0.0.2.

  • By default, Atom hardcodes the C types which correspond to the Atom types. This means that an Int32 is always defined as signed int. Unfortunately, the ATmega168 CPU and the avr-gcc compiler define a signed int as an Int16. I needed a way to customize these types.
  • Even though an Atom action can refer to an external function, I wasnt able to see a nice way to have Atom plop a #include in the generated code. I added this ability as well.

My changes only end up affecting two exposed functions. To facilitate the include files and the C types, I added an AtomConfig data type (this can be found in Language.Atom.Code).

data AtomConfig = AtomConfig 
    cIncludes :: Maybe String,
    cTyper    :: Maybe Type -> String

I also changed the type signature of writeC. It now reads as:

writeC :: Name -> Maybe AtomConfig -> Rule -> UV -> IO 

Now, we dont actually have to call writeC our selves 1, but it does get called by compile from Language.Atom.Compile. The compile function now has this type signature:

compile :: Name -> Maybe AtomConfig -> Atom  -> IO 

Other than the example in Language.Atom.Example, this is the only visible code that I changed.

Heres how you use the AtomConfig type:

cType :: Type -> String
cType t = case t of
  Bool   -> "uint8_t"
  Int8   -> "int8_t"
  Int16  -> "int16_t"
  Int32  -> "int32_t"
  Int64  -> "int64_t"
  Word8  -> "uint8_t"
  Word16 -> "uint16_t"
  Word32 -> "uint32_t"
  Word64 -> "uint64_t"
  Float  -> "float"
  Double -> "double"
cfg = AtomConfig 
    cIncludes = Just "blink.h",
    cTyper    = Just cType

First, we define a function that takes a Type and produces a string representing the C type. We build the config with an optional list of include files 2 and an optional typing function. If Nothing is passed to both fields, then Atom will not use any include files and will use the built in type converter.

In the next post, well start building a simple program: blink an LED hooked up to some I/O pins on the Arduino.

Ive posted a diff of my changes to atom-0.0.2. Any criticisms are welcome.

  1. At least, I havent seen a reason to yetperhaps this should not be exposed?
  2. These includes are the kinds that use quotes, not angle brackets. Well see #include "blink.h" rather than #include <blink.h>.
Categories: Distraction Tags: ,
  1. brian
    May 3rd, 2009 at 13:03 | #1

    Cool! Part 2, please!

  2. John Van Enk
    May 3rd, 2009 at 13:43 | #2

    Its in progress!

  3. Radamés
    May 3rd, 2009 at 23:20 | #3

    Great idea! Ive been thinking about haskell and arduino interacting by some serial communication with System.IO, however Ive never thought about program arduino with haskell!! For a while, I dont know to much Atom package for help you!
    Good job!

  4. Ivan
    April 22nd, 2010 at 23:17 | #4

    Is Pt. 2 still in progress? I have a Duemilanove; maybe I can test some programs you want to post. Im using Haskell in several platforms (Angstrom on Toshiba e750, Debian on Freerunner, and my desktop of course), Id like Arduino to be the next. Contact me if you think I can help.

  1. May 4th, 2009 at 22:58 | #1
  2. May 5th, 2009 at 13:03 | #2
  3. August 1st, 2009 at 13:19 | #3