Archive

Archive for the ‘Distraction’ Category

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

May 1st, 2009 John Van Enk 4 comments

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, 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.

  1. At least, I haven’t seen a reason to yet–perhaps this should not be exposed?
  2. These includes are the kinds that use quotes, not angle brackets. We’ll see #include "blink.h" rather than #include <blink.h>.
Categories: Distraction Tags: ,

"Shelle of prayers and Ceremonies"

February 18th, 2009 John Van Enk No comments

“Right,” said Om. “Now…listen. Do you know how gods get power?”

“By people believing in them,” said Brutha. “Millions of people believe in you.”

Om hesitated.

All right, all right. We are here and it is now. Sooner or later he’ll find out for himself…

“They don’t believe,” said Om.

“But-”

“It’s happened before,” said the tortoise. “Dozens of times. D’you know Abraxas found the lost city of Ee? Very strange carvings, he says. Belief, he says. Belief shifts. People start out believing in the god and end up believing in the structure.”

“I don’t understand,” said Brutha.

“Let me put it another way,” said the tortoise. “I am your God, right?”

“Yes.”

“And you’ll obey me.”

“Yes.”

“Good. Now take a rock and go and kill Vorbis.”

Brutha didn’t move.

“I’m sure you heard me,” said Om.

“But he’ll…he’s…the Quisition would-”

“Now you know what I mean,” said the tortoise. “You’re more afraid of him than you are of me, now. Abraxas says here: `Around the Godde there forms a Shelle of prayers and Ceremonies and Buildings and Priestes and Authority, until at Last the Godde Dies. Ande this maye notte be noticed.’ “

Terry Pratchett – Small Gods

Categories: Distraction Tags: ,

Distraction: Fun with diagrams!

February 11th, 2009 John Van Enk No comments

I’ve finally sat down and played with Brent Yorgey’s diagrams package. Every once in a while, I just want to write something fun–some feel good code. Well, I had so much fun, I want to describe what I made.

It’s nothing complex or strange, it’s just a few shapes with some colors! (But the colors are so shiny and happy!)

Lets start from the top:

numCols, numRows :: Int
numCols = 23
numRows = 5

Here, we just defined how many shapes wide and how many shapes tall we want our resulting diagram to be. Lets define main next:

main :: IO ()
main = renderAs SVG "out2.svg" (Width width) dgram
    where width = (fromIntegral numCols) * 30

Alright, we know what numCols is, and we see width defined in the where clause, but what is dgram?

dgram :: Diagram
dgram = vcat $ take numRows rows
    where rows = map hcat $ plines crps

dgram is the top level diagram we’re rendering. vcat is a vertical concatenation of several other diagrams. We take a specific number of rows from rows–the horizontal concatenation (hcat) of whatever is generated by plines crps–and concatenate them together vertically.

Alright, we seem to be almost there now. What is plines?

plines :: [Diagram] -> [[Diagram]]
plines [] = []
plines pls = let (l,ls) = splitAt numCols pls
             in l : (plines ls)

As it turns out, plines is just a cheap chunking function with a fixed chunk size. All this does is turn a list into a list of lists with the same length. If the sublists were concatenated back together again, you’d have your original list.

We’ve seen everything plines has to offer, what about crps?

crps :: [Diagram]
crps = zipWith fillColor cs rps
    where rps = zipWith rotate rs ps
          rs = cycle . map (/ 4) $ [1..4]                 -- 4 rotations
          ps = cycle . map (flip regPoly 1) $ [3..7]      -- 5 polygons
          cs = cycle [red,black,green,blue,yellow,violet] -- 6 colors

Hey! This is more interesting! Lets look a little closer at what’s happening here.

The ps symbol defines an infinite list of cycling polygons. We use the regular polygon (regPoly) function to generate polygons with 3, 4, 5, 6, and 7 sides. The cs symbol is just defining an infinite list of colors–nothing too complex here. The rs symbol is a cycling list of 1/4, 2/4, 3/4, and 4/4. These are used as rotations in terms of turns.

Now, what is that rps symbol? Lets see, we are zipping rs and ps together using the function rotate which accepts a Double representing a fraction of a turn and a Diagram to rotate. So, rps is an infinite list of cycling polygons where each successive polygon is rotated one quarter turn more than the previous.

This means that crps is an infinite list of cycling polygons each rotated one quarter turn more than the previous polygon but and each polygon has a cycling fill color! Neat!

So what does this look like?

Colorful cycling rotating polygons!

I don’t know about you, but that takes me back to grade school. Sometimes a little bit of happy code can brighten your day!

Links to source files and SVG images:
Original, not quite as cool, attempt and its SVG rendering.
Finished colorful one and its SVG rendering.

Categories: Distraction Tags: ,