guilhermesimas / ceu-arduino

Arduino binding for Céu

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Céu-Arduino supports the development of Arduino applications in the programming language Céu:

Arduino Arduino is an open-source project that created microcontroller-based kits for building digital devices and interactive objects that can sense and control physical devices.

Céu is a reactive language that aims to offer a higher-level and safer alternative to C:

Céu-Arduino empowers the development of Arduino applications with the following extensions:

  • Awaiting events in direct/sequential style.
  • Parallel lines of execution with
    • safe abortion;
    • deterministic behavior (in contrast with threads).
  • Asynchronous loops for heavy computations.
  • Interrupt-driven operation mode (optional and experimental).
  • Seamless integration with standard Arduino (e.g., analogRead, random, etc).

Install

Install Arduino

Requires arduino-1.5 or higher:

https://www.arduino.cc/

Install Céu:

https://github.com/fsantanna/ceu/

Clone Céu-Arduino:

$ git clone https://github.com/fsantanna/ceu-arduino
$ cd ceu-arduino/

Compile and Upload

Edit the Makefile to point to your ceu directory and then run make:

$ gedit Makefile
$ make

Certify that your Arduino is connected to the USB. If necessary, configure the variables in the Makefile. The default example blinks the on-board LED every second.

To compile and upload another application, run make and set CEU_SRC:

$ make CEU_SRC=<path-to-ceu-application>

Examples

The samples/ directory contains a number of examples.

Blinking a LED

The example blink-01.ceu assumes that a LED is connected to pin 13.

The program is an infinite loop that intercalates between turning the LED on and off in intervals of 1 second:

#include "arduino/arduino.ceu"  // include standard declarations

output int PIN_13;              // PIN_13 is an output pin

loop do                         // an infinite loop that:
    emit PIN_13(HIGH);          //   - turns the LED on
    await 1s;                   //   - awaits 1 second
    emit PIN_13(LOW);           //   - turns the LED off
    await 1s;                   //   - awaits another 1 second
end                             //   - repeats

Switching a LED

The example button-01.ceu requires a simple circuit with a switch button connected to pin 2.

The program waits for changes on pin 2 (the switch), copying its value to pin 13 (the LED):

#include "arduino/arduino.ceu"

input  int PIN_02;
output int PIN_13;

emit PIN_13(LOW);

loop do
    var int v = await PIN_02;
    emit PIN_13(v);
end

Blinking in Parallel

The example blink-03.ceu requires two additional LEDs connected to pins 11 and 12.

The program blinks the LEDs with different frequencies, in parallel:

#include "arduino/arduino.ceu"

output int PIN_11;
output int PIN_12;
output int PIN_13;

par do
    loop do
        emit PIN_11(HIGH);
        await 1s;
        emit PIN_11(LOW);
        await 1s;
    end
with
    loop do
        emit PIN_12(HIGH);
        await 500ms;
        emit PIN_12(LOW);
        await 500ms;
    end
with
    loop do
        emit PIN_13(HIGH);
        await 250ms;
        emit PIN_13(LOW);
        await 250ms;
    end
end

Fading a LED

The example pwm-01.ceu assumes that a LED is connected to pin 11.

The program fades the LED from 0 to 255 and from 255 to 0 in two consecutive loops:

#include "arduino/arduino.ceu"

output u8 PWM_11;

loop do
    var int i;
    loop i in [0->255] do
        emit PWM_11(i);
        await 5ms;
    end
    loop i in [0<-255] do
        emit PWM_11(i);
        await 5ms;
    end
end

Serial Echo

The example serial-01.ceu reads and write bytes from and to the serial in a continuous loop:

#include "arduino/arduino.ceu"

input byte SERIAL;

loop do
    var byte c = await SERIAL;
    _Serial.write(c);
end

Céu can directly use standard Arduino functionality by prefixing its symbols with an underscore (e.g., _Serial.write(c)).

Switching a LED with Interrupts

The example isr-01.ceu is equivalent to button-01.ceu but uses interrupts instead of polling:

#include "arduino/arduino.ceu"

input  int PIN_02;
output int PIN_13;

spawn async/isr [_digitalPinToInterrupt(2),_CHANGE] do
    emit PIN_02(_digitalRead(2));
end

emit PIN_13(LOW);

loop do
    var int v = await PIN_02;
    emit PIN_13(v);
end

The async/isr is an interrupt service routine written in Céu. It is attached to the interrupt number for pin 2 (_digitalPinToInterrupt(2)) and is triggered whenever the pin changes value (_CHANGE). The routine emits a PIN_02 input to the application.

Examples that use interrupts have to be compiled with CEU_ISR=true:

make CEU_ISR=true CEU_SRC=samples/isr-01.ceu

Interrupts with Drivers

The example isr-08.ceu uses the interrupt drivers timer.ceu and pin-02.ceu from the library.

The program blinks the LED connected to pin 13 until the button connected to pin 02 is clicked. After another click, the blinking restarts.

#include "arduino/isr/timer.ceu"        // timer ISR
#include "arduino/isr/pin-02.ceu"       // pin-02 ISR

output int PIN_13;

loop do
    watching PIN_02 do                  // aborts on click
        var int x = 0;
        every 500ms do                  // blinks every 500ms
            x = 1 - x;
            emit PIN_13(x);
        end
    end
    await 500ms;                        // debouncing
    await PIN_02;                       // restarts
    await 500ms;                        // debouncing
end

Applications

The Game "Ship"

The game ship.ceu is described in a blog post:

About

Arduino binding for Céu


Languages

Language:Makefile 93.3%Language:Shell 6.7%