embedded-hal icon indicating copy to clipboard operation
embedded-hal copied to clipboard

Some questions regarding this project.

Open Raj2032 opened this issue 3 years ago • 3 comments
trafficstars

Hi I have some questions regarding this project.

Is this a "write once run everywhere" project? That means that if I write code for the Arduino, the exact same code would work on a Raspberry Pi, for example if I wanted to make an LED blink I can use the exact same code, the only difference is that the binary output after compiling would be very different between the two devices?

If I had to different Arduino models, would the code be exactly the same?

Raj2032 avatar Jul 31 '22 14:07 Raj2032

The code will not be exactly the same, you have different device initialization on each devices. This crate provide common traits to write generic code. It's mainly useful when writing drivers. For example, if you want to make a driver "Led", you need one pin, this pin will be "PA1" on stm32, "D13" for arduino, ..., but your driver can be generic on "OutputPin".

For example this driver is compatible with all crates that provide the traits from embedded-hal :

struct Led<P: OutputPin> {
    pin: P
}

impl<P: OutputPin> Led<P> {
    fn new(pin: P) -> Self {
        Self {
            pin,
        }
    }

    fn on(&mut self) {
        self.set_high();
    }

    fn off(&mut self) {
        self.set_low();
    }
}```

Fomys avatar Jul 31 '22 17:07 Fomys

struct Led<P: OutputPin> {
    pin: P
}

impl<P: OutputPin> Led<P> {
    fn new(pin: P) -> Self {
        Self {
            pin,
        }
    }

    fn on(&mut self) {
        self.set_high();
    }

    fn off(&mut self) {
        self.set_low();
    }
}

So if I wrote this for both the Arduino and Raspberry Pi, this code can remain exactly the same to control an LED, am I understanding you correctly?

Raj2032 avatar Aug 01 '22 13:08 Raj2032

This part of code yes, but the way you have to initialize the GPIO no

Fomys avatar Aug 01 '22 17:08 Fomys

@Fomys Hey man sorry for the late reply.

So in regards to a blinking led program for both a raspbierry pi pico and arduino.

Here is an example of avr/arduino

    loop {
        led.toggle();
        arduino_hal::delay_ms(100);
        led.toggle();
        arduino_hal::delay_ms(100);
        led.toggle();
        arduino_hal::delay_ms(100);
        led.toggle();
        arduino_hal::delay_ms(800);
    }

And an example for the raspberry pi pico

    loop {
        led_pin.set_high().unwrap();
        // TODO: Replace with proper 1s delays once we have clocks working
        delay.delay_ms(500);
        led_pin.set_low().unwrap();
        delay.delay_ms(500);
    }

as you can see they are different. Is there a way they can be written generically?

Raj2032 avatar Aug 19 '22 11:08 Raj2032

Your snippets don't do exactly the same thing.

Both of the crate you linked already implements traits from embedded-hal, but not the same version, so it is not possible to make generic code. You can try to bump the version in avr-hal to make have the same version.

If the both crates uses the same embedded-hal version you can create this kind of generic function:

fn blink_loop<P: OutputPin>(led: P) {
    loop {
        led.setHigh().unwrap();
        for _ in 0..1000000 {}
        led.setLow().unwrap();
        for _ in 0..1000000 {}
    }
}

And in your executable:

fn main() {
    // ... specific code for your target board
    let led = /* specific code for your target board */ .into_push_pull_output();
    
    blink_loop(led);
}

The first method is generic, so you can use it with anything that implement the traits from embedded-hal crate.

Fomys avatar Aug 19 '22 13:08 Fomys

@Fomys

Your snippets don't do exactly the same thing.

Right, so like what does toggle() do compared to set_high()/set_low() don't they both turn on and off the pin?

but not the same version

Ah right didn't realise that.

If the both crates uses the same embedded-hal version you can create this kind of generic function:

Right that's good then :)


Another question, does embedded-hal support as many various gpio sensors/devices (such as LED, ultrasonic sound distance sensor etc) as the Arduino IDE code/library does?

Raj2032 avatar Aug 19 '22 14:08 Raj2032

Right, so like what does toggle() do compared to set_high()/set_low() don't they both turn on and off the pin?

They both blink the leds, but not at the same speed. toggle() means "change the output state to the oposite one"

Another question, does embedded-hal support as many various gpio sensors/devices (such as LED, ultrasonic sound distance sensor etc) as the Arduino IDE code/library does?

I think not, but you can search on crates.io if someone already write a driver for your device.

You may be able to find even more HAL implementation crates and driver crates by searching for the keywords embedded-hal-impl, embedded-hal-driver and embedded-hal on crates.io.

Fomys avatar Aug 24 '22 13:08 Fomys

Right i see thanks mate :)

Raj2032 avatar Aug 25 '22 10:08 Raj2032

The original question seems solved. Thanks @Fomys! Closing.

eldruin avatar Aug 31 '22 09:08 eldruin