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

When Tom Hawkins released the Atom package, I stole my friend’s Arduino Diecimila to see if I could get some Atom-generated code to work. Turns out it wasn’t that hard! I’ll be detailing this in the next few posts. I’m 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 wasn’t 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 don’t actually have to call writeC our selves [1. At least, I haven't seen a reason to yet--perhaps this should not be exposed?], 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.

Here’s 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. These includes are the kinds that use quotes, not angle brackets. We'll see #include "blink.h" rather than #include <blink.h>.] 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, we’ll start building a simple program: blink an LED hooked up to some I/O pins on the Arduino.

I’ve posted a diff of my changes to atom-0.0.2. Any criticisms are welcome.

About these ads
This entry was posted in Distraction and tagged , . Bookmark the permalink.

7 Responses to Atom & Arduino :: Some Hacking (pt. 1)

  1. brian says:

    Cool! Part 2, please!

  2. John Van Enk says:

    @brian
    It’s in progress!

  3. Radamés says:

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

  4. Pingback: Learning After School » Atom & Arduino :: First Program (pt. 2)

  5. Pingback: An Atomic Fibonacci Server: Exploring the Atom (Haskell) DSL « A Critical Systems Blog

  6. Pingback: Atom : a domain specific language for hard realtime applications « Arch Linux and Haskell

  7. Ivan says:

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

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s