Monday, January 27, 2020

Use Haskell to program Arduino microcontrollers #Haskell #Arduino #Programming

Joey Hess posts about arduino-copilot – use Haskell to program an Arduino. It’s a FRP style system, and uses the Copilot DSL to generate embedded C code.

To make your arduino blink its LED, you only need 4 lines of Haskell:

import Copilot.Arduino
main = arduino $ do
    led =: blinking
    delay =: constant 100

Running that Haskell program generates an Arduino sketch in an .ino file, which can be loaded into the Arduino IDE and uploaded to the Arduino the same as any other sketch. It’s also easy to use things like Arduino-Makefile to build and upload sketches generated by arduino-copilot.

Joey says:

I’m not the first person to use Copilot to program an Arduino. Anthony Cowley showed how to do it in Abstractions for the Functional Roboticist back in 2013. But he had to write a skeleton of C code around the C generated by Copilot. Among other features, arduino-copilot automates generating that C skeleton. So you don’t need to remember to enable GPIO pin 13 for output in the setup function; arduino-copilot sees you’re using the LED and does that for you.

Also: simulating an Arduino

One of Copilot’s features is it can interpret code, without needing to run it on the target platform. So the Arduino’s behavior can be simulated, without ever generating C code, right at the console!

See the details in this blog post and on haskell.org.

No comments:

Post a Comment