#![doc = include_str!("./lcd_doc.md")]
use arduino_hal::port::{mode, Pin};
const MAX_LENGTH: u8 = 16;
const MS_DELAY: u16 = 3_000;
const LED_PIN: usize = 0;
const D7_PIN: usize = 1;
const D6_PIN: usize = 2;
const D5_PIN: usize = 3;
const D4_PIN: usize = 4;
const D3_PIN: usize = 5;
const D2_PIN: usize = 6;
const D1_PIN: usize = 7;
const D0_PIN: usize = 8;
const ENABLE_PIN: usize = 9;
const READ_WRITE_PIN: usize = 10;
const REGISTER_SEL: usize = 11;
const BIT_0: usize = 0x01;
const BIT_1: usize = 0x02;
const BIT_2: usize = 0x04;
const BIT_3: usize = 0x08;
const BIT_4: usize = 0x10;
const BIT_5: usize = 0x20;
const BIT_6: usize = 0x40;
const BIT_7: usize = 0x80;
const ASCII_OFFSET: u8 = 0x30;
pub fn float_to_array(float: f32, arr: &mut [char]) {
let float_hundredths: u8 = ( ( (float) * 100.0) as u8 ) % 10;
let float_tenths: u8 = ( ( (float) * 10.0) as u8 ) % 10;
let float_ones: u8 = float as u8;
arr[0] = (float_ones + ASCII_OFFSET) as char;
arr[2] = (float_tenths + ASCII_OFFSET) as char;
arr[3] = (float_hundredths + ASCII_OFFSET) as char;
}
pub fn clear_data_bits( gpio: &mut [Pin<mode::Output>]) {
gpio[D7_PIN].set_low();
gpio[D6_PIN].set_low();
gpio[D5_PIN].set_low();
gpio[D4_PIN].set_low();
gpio[D3_PIN].set_low();
gpio[D2_PIN].set_low();
gpio[D1_PIN].set_low();
gpio[D0_PIN].set_low();
}
pub fn write_static_string(gpio: &mut [Pin<mode::Output>], s: &str) {
let mut str_length: u8 = 0;
for _c in s.chars() {
str_length += 1; }
if str_length > MAX_LENGTH { return;
}
gpio[LED_PIN].set_high(); gpio[REGISTER_SEL].set_high(); for c in s.chars() {
clear_data_bits(gpio);
let d7: u8 = (c as u8) & (BIT_7 as u8);
let d6: u8 = (c as u8) & (BIT_6 as u8);
let d5: u8 = (c as u8) & (BIT_5 as u8);
let d4: u8 = (c as u8) & (BIT_4 as u8);
let d3: u8 = (c as u8) & (BIT_3 as u8);
let d2: u8 = (c as u8) & (BIT_2 as u8);
let d1: u8 = (c as u8) & (BIT_1 as u8);
let d0: u8 = (c as u8) & (BIT_0 as u8);
if d7 > 0 {
gpio[D7_PIN].set_high();
}
if d6 > 0 {
gpio[D6_PIN].set_high();
}
if d5 > 0 {
gpio[D5_PIN].set_high();
}
if d4 > 0 {
gpio[D4_PIN].set_high();
}
if d3 > 0 {
gpio[D3_PIN].set_high();
}
if d2 > 0 {
gpio[D2_PIN].set_high();
}
if d1 > 0 {
gpio[D1_PIN].set_high();
}
if d0 > 0 {
gpio[D0_PIN].set_high();
}
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(10);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(20);
}
gpio[REGISTER_SEL].set_low(); gpio[LED_PIN].set_low(); }
pub fn write_char_array(gpio: &mut [Pin<mode::Output>], arr: &mut [char]) {
let arr_length: usize = arr.len();
gpio[LED_PIN].set_high(); gpio[REGISTER_SEL].set_high(); for i in 0..arr_length {
let c: char = arr[i];
clear_data_bits(gpio);
let d7: u8 = (c as u8) & (BIT_7 as u8);
let d6: u8 = (c as u8) & (BIT_6 as u8);
let d5: u8 = (c as u8) & (BIT_5 as u8);
let d4: u8 = (c as u8) & (BIT_4 as u8);
let d3: u8 = (c as u8) & (BIT_3 as u8);
let d2: u8 = (c as u8) & (BIT_2 as u8);
let d1: u8 = (c as u8) & (BIT_1 as u8);
let d0: u8 = (c as u8) & (BIT_0 as u8);
if d7 > 0 {
gpio[D7_PIN].set_high();
}
if d6 > 0 {
gpio[D6_PIN].set_high();
}
if d5 > 0 {
gpio[D5_PIN].set_high();
}
if d4 > 0 {
gpio[D4_PIN].set_high();
}
if d3 > 0 {
gpio[D3_PIN].set_high();
}
if d2 > 0 {
gpio[D2_PIN].set_high();
}
if d1 > 0 {
gpio[D1_PIN].set_high();
}
if d0 > 0 {
gpio[D0_PIN].set_high();
}
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(10);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(20);
}
gpio[REGISTER_SEL].set_low(); gpio[LED_PIN].set_low(); }
pub fn test_write_char( gpio: &mut [Pin<mode::Output>] ) {
gpio[REGISTER_SEL].set_high(); for _i in 0..16 {
gpio[D7_PIN].set_low(); gpio[D6_PIN].set_high();
gpio[D5_PIN].set_low();
gpio[D4_PIN].set_low();
gpio[D3_PIN].set_high();
gpio[D2_PIN].set_low();
gpio[D1_PIN].set_low();
gpio[D0_PIN].set_low();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(10);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(20);
}
gpio[REGISTER_SEL].set_low(); gpio[D7_PIN].set_high();
gpio[D6_PIN].set_high();
gpio[D5_PIN].set_low();
gpio[D4_PIN].set_low();
gpio[D3_PIN].set_low();
gpio[D2_PIN].set_low();
gpio[D1_PIN].set_low();
gpio[D0_PIN].set_low();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(10);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(20);
gpio[REGISTER_SEL].set_high(); for _i in 0..16 {
gpio[D7_PIN].set_low(); gpio[D6_PIN].set_high();
gpio[D5_PIN].set_low();
gpio[D4_PIN].set_low();
gpio[D3_PIN].set_high();
gpio[D2_PIN].set_low();
gpio[D1_PIN].set_low();
gpio[D0_PIN].set_low();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(10);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(20);
}
gpio[REGISTER_SEL].set_low(); gpio[D7_PIN].set_low();
gpio[D6_PIN].set_low();
gpio[D5_PIN].set_low();
gpio[D4_PIN].set_low();
gpio[D3_PIN].set_low(); gpio[D2_PIN].set_low();
gpio[D1_PIN].set_high();
gpio[D0_PIN].set_low();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(10);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(20);
}
pub fn reset_lcd( gpio: &mut [Pin<mode::Output>] ) {
arduino_hal::delay_ms(1);
gpio[REGISTER_SEL].set_low();
gpio[READ_WRITE_PIN].set_low();
gpio[LED_PIN].set_high(); gpio[D7_PIN].set_low();
gpio[D6_PIN].set_low();
gpio[D5_PIN].set_high();
gpio[D4_PIN].set_high(); gpio[D3_PIN].set_high(); gpio[D2_PIN].set_high(); gpio[D1_PIN].set_low();
gpio[D0_PIN].set_low();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(1);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(2);
gpio[D7_PIN].set_low();
gpio[D6_PIN].set_low();
gpio[D5_PIN].set_low();
gpio[D4_PIN].set_low(); gpio[D3_PIN].set_low(); gpio[D2_PIN].set_low();
gpio[D1_PIN].set_low();
gpio[D0_PIN].set_high();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(1);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(2);
gpio[D3_PIN].set_low(); gpio[D2_PIN].set_low();
gpio[D1_PIN].set_high();
gpio[D0_PIN].set_low();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(1);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(2);
gpio[D3_PIN].set_high(); gpio[D2_PIN].set_high();
gpio[D1_PIN].set_high();
gpio[D0_PIN].set_high();
gpio[ENABLE_PIN].set_high(); arduino_hal::delay_ms(1);
gpio[ENABLE_PIN].set_low(); arduino_hal::delay_ms(2);
gpio[LED_PIN].set_low(); }