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
Int32is always defined assigned int. Unfortunately, the ATmega168 CPU and the avr-gcc compiler define asigned intas anInt16. I needed a way to customize these types. - Even though an Atom
actioncan refer to an external function, I wasn’t able to see a nice way to have Atom plop a#includein 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, 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 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.
