PINE64
RockPro64 programing GPIO by Rust - Printable Version

+- PINE64 (https://forum.pine64.org)
+-- Forum: ROCKPRO64 (https://forum.pine64.org/forumdisplay.php?fid=98)
+--- Forum: RockPro64 Projects, Ideas and Tutorials (https://forum.pine64.org/forumdisplay.php?fid=104)
+--- Thread: RockPro64 programing GPIO by Rust (/showthread.php?tid=15407)



RockPro64 programing GPIO by Rust - yanagawa3 - 11-24-2021

Hello,
For the Rust developer, I show the gpio led blinking program code for RockPro64.
You can use GPIO pin number or BCM number.

In project
cargo build --release
sudo target/release/gpio

LED connected Pin 16 blinks.

cargo.toml

------------------------------------
[dependencies]
sysfs_gpio = "0.6"

------------------------------------
main.rs
-----------------------------------------------------------------------------------
use sysfs_gpio::{Direction, Pin};
use std::thread:Confusedleep;
use std::time:Big Grinuration;

// Define GPIO arrays
static BOARD_TO_ROCK: &'static [u8] = &[0, 0, 0, 52, 0, 53, 0, 152, 148, 0, 147, 54, 120, 50, 0, 33, 36, 0, 149, 40, 0, 39, 153, 41, 42, 0, 45, 43, 44, 155, 0, 156, 124, 125, 0, 122, 126, 121, 123, 0, 127];
static ROCK_VALID_CHANNELS: &'static [u8] = &[52, 53, 152, 54, 50, 33, 40, 39, 41, 43, 155, 156, 125, 122, 121, 148, 147, 120, 36, 149, 153, 42, 45, 44, 124, 126, 123, 127];
static BCM_TO_ROCK: &'static [u8] = &[43, 44, 52, 53, 152, 155, 156, 45, 42, 39, 40, 41, 124, 125, 148, 147, 124, 54, 120, 122, 123, 127, 33, 36, 149, 153, 121, 50];

fn main() {
    let my_pin_num = 16;//gpio number: 36
    let my_led = Pin::new(BOARD_TO_ROCK[my_pin_num].into()); // Pin 16 GPIO1_A4 gpio number 36 depends on chip, etc.
    //let my_bcm_num = 23;//gpio number: 36
    //let my_led = Pin::new(BCM_TO_ROCK[my_bcm_num].into()); // Pin 16 GPIO1_A4 gpio number 36 depends on chip, etc.
    my_led.with_exported(|| {
        my_led.set_direction(Direction::Out).unwrap();
        loop {
            my_led.set_value(0).unwrap();
            sleep(Duration::from_millis(100));
            my_led.set_value(1).unwrap();
            sleep(Duration::from_millis(100));
            println!("Blinking GPIO1_A4(36) Pin 16");
        }
    }).unwrap();
}

-------------------------------------------------------------------------------------------------