lcd1602rs 0.1.1

A tiny package to write to a LCD1602 via I2C (e.g. on a Raspberry Pi)
Documentation
extern "C" {
    // Opens a file system handle to the I2C device
    // Turns on the LCD, sets the 4-bit mode and clears the memory
    // Returns 0 for success, 1 for failure
    fn lcd1602Init(iChannel: i32, iAddr: i32) -> i32;

    // Set the cursor position on the LCD
    fn lcd1602SetCursor(x: i32, y: i32) -> i32;

    // Control the backlight, cursor, and blink
    fn lcd1602Control(bBacklight: i32, bCursor: i32, bBlink: i32) -> i32;

    // Print a zero-terminated character string
    // Only 1 line at a time can be accessed
    fn lcd1602WriteString(szText: *const std::ffi::c_char) -> i32;

    // Clear the characters from the LCD
    fn lcd1602Clear() -> i32;

    // Turn off the LCD and backlight
    // close the I2C file handle
    fn lcd1602Shutdown();
}

pub struct Lcd1602 {}

impl Lcd1602 {
    pub fn new(channel: i32, addr: i32) -> Option<Lcd1602> {
        let result = unsafe {
            lcd1602Init(channel, addr)
        };
        match result {
            0 => Some(Lcd1602 {}),
            _ => None
        }
    }

    pub fn set_cursor(&self, x: i32, y: i32) {
        unsafe {
            lcd1602SetCursor(x, y);
        }
    }

    pub fn control(&self, backlight: bool, cursor: bool, blink: bool) {
        unsafe {
            lcd1602Control(if backlight {1} else {0}, if cursor {1} else {0}, if blink {1} else {0});
        }
    }

    pub fn write(&self, text: &str) {
        let cstr = std::ffi::CString::new(text).expect("CString::new failed");
        let ptr = cstr.into_raw();

        unsafe {
            lcd1602WriteString(ptr);

            // retake pointer to free memory
            let _ = std::ffi::CString::from_raw(ptr);
        }
    }

    pub fn clear(&self) {
        unsafe {
            lcd1602Clear();
        }
    }
}

impl Drop for Lcd1602 {
    fn drop(&mut self) {
        unsafe {
            lcd1602Shutdown();
        }
    }
}