Thursday, May 8, 2014

Switches and Buttons and Dials and A Haskell DSL for Embedded Systems Programming

  In my ongoing journey towards hobbyist electronics, I've connected a button, a switch, and a dial (potentiometer) to my Arduino. It is simple stuff, but I still did it wrong the first time.

  The code is done using the Haskell DSL Ivory, which is pretty easy to get set up with the Arduino. At some point I would also like to get the other language build by the same team called Tower working on the Arduino, which requires FreeRTOS, but that will be a project in itself.
  I'm using the makefile from and a little shell script to avoid the Arduino IDE and so I can build everything from the Template Haskell to the Haskell to the C to the final binary and upload it to the Arduino all at once.
  No external C code is required when using Ivory (though I have a #define in a header file to configure the generated C code). I even wrapped up some of the Arduino Serial library in C functions to be able to call it from Ivory. The wrapping is necessary to call C++ functions from C, and in this case, to import them as external functions in Ivory.

  Ivory itself is a language for embedded systems as a Haskell DSL that does much more type checking and prevents many common bugs compared to C. I've liked it so far, although it certainly takes getting used to. I like that they make use of some Haskell extensions like automatically lifting data values into types and types into kinds.  The bitdata aspect of the language looks really great. I am interested in trying to specify things like register contents using their bitdata types to see how much better we can do then C (which I use for this kind of thing at work).

  Back to the Arduino, I was using analogRead to get the voltage level, sending the input to the host computer using the wrapped up Serial functions, and then using "cat /dev/ttyACM0" to read from the Arduino, I could see the voltage level in the little circuits for each element. The most fun was the dial, as you could make very fine adjustments to the voltage by just barely turning the dial, and you could go through the range (0-1023) in a nice linear fashion.

  I'm looking into taking the EdX class taught by (among others) Gerald Sussman to get some basic electronics knowledge. I have some highschool education in this, and some physics classes in college, but not enough to do anything interesting. Hopefully next up will be a hall effect sensor or a 1-wire temperature sensor.

No comments:

Post a Comment