Pārlūkot izejas kodu

add a button and multiline output to lcd

Jake Kalstad 3 gadi atpakaļ
vecāks
revīzija
c5be627a20
3 mainītis faili ar 431 papildinājumiem un 10 dzēšanām
  1. 345 1
      Cargo.lock
  2. 2 2
      Cargo.toml
  3. 84 7
      src/main.rs

+ 345 - 1
Cargo.lock

@@ -11,6 +11,15 @@ dependencies = [
  "embedded-hal",
 ]
 
+[[package]]
+name = "aho-corasick"
+version = "0.7.19"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b4f55bd91a0978cbfd91c457a164bab8b4001c833b7f323132c0a4e1922dd44e"
+dependencies = [
+ "memchr",
+]
+
 [[package]]
 name = "arduino-hal"
 version = "0.1.0"
@@ -25,6 +34,12 @@ dependencies = [
  "void",
 ]
 
+[[package]]
+name = "arrayvec"
+version = "0.7.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8da52d66c7071e2e3fa2a1e5c6d088fec47b593032b254f5e980de8ea54454d6"
+
 [[package]]
 name = "atmega-hal"
 version = "0.1.0"
@@ -34,6 +49,12 @@ dependencies = [
  "avr-hal-generic",
 ]
 
+[[package]]
+name = "autocfg"
+version = "1.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
+
 [[package]]
 name = "avr-device"
 version = "0.3.4"
@@ -41,7 +62,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "edb5cf74147f0a7ef4b7b2b5cdd6bc04e749d050cf48ed2a40048db290165db4"
 dependencies = [
  "avr-device-macros",
- "bare-metal",
+ "bare-metal 0.2.5",
  "cfg-if 0.1.10",
  "rustversion",
  "vcell",
@@ -82,6 +103,24 @@ dependencies = [
  "rustc_version",
 ]
 
+[[package]]
+name = "bare-metal"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f8fe8f5a8a398345e52358e18ff07cc17a568fbca5c6f73873d3a62056309603"
+
+[[package]]
+name = "bit_field"
+version = "0.10.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "dcb6dd1c2376d2e096796e234a70e17e94cc2d5d54ff8ce42b28cef1d0d359a4"
+
+[[package]]
+name = "bitfield"
+version = "0.13.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "46afbd2983a5d5a7bd740ccb198caf5b82f45c40c09c0eed36052d91cb92e719"
+
 [[package]]
 name = "cfg-if"
 version = "0.1.10"
@@ -94,6 +133,62 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
 
+[[package]]
+name = "cortex-m"
+version = "0.7.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "70858629a458fdfd39f9675c4dc309411f2a3f83bede76988d81bf1a0ecee9e0"
+dependencies = [
+ "bare-metal 0.2.5",
+ "bitfield",
+ "embedded-hal",
+ "volatile-register",
+]
+
+[[package]]
+name = "cortex-m-rt"
+version = "0.7.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d6d3328b8b5534f0c90acd66b68950f2763b37e0173cac4d8b4937c4a80761f9"
+dependencies = [
+ "cortex-m-rt-macros",
+]
+
+[[package]]
+name = "cortex-m-rt-macros"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f0f6f3e36f203cfedbc78b357fb28730aa2c6dc1ab060ee5c2405e843988d3c7"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "critical-section"
+version = "0.2.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "95da181745b56d4bd339530ec393508910c909c784e8962d15d722bacf0bcbcd"
+dependencies = [
+ "bare-metal 1.0.0",
+ "cfg-if 1.0.0",
+ "cortex-m",
+ "riscv",
+]
+
+[[package]]
+name = "critical-section"
+version = "1.1.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6548a0ad5d2549e111e1f6a11a6c2e2d00ce6a3dafe22948d67c2b443f775e52"
+
+[[package]]
+name = "either"
+version = "1.8.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "90e5c1c8368803113bf0c9584fc495a58b86dc8a29edbf8fe877d21d9507e797"
+
 [[package]]
 name = "embedded-hal"
 version = "0.2.7"
@@ -104,6 +199,48 @@ dependencies = [
  "void",
 ]
 
+[[package]]
+name = "embedded-time"
+version = "0.12.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "d7a4b4d10ac48d08bfe3db7688c402baadb244721f30a77ce360bd24c3dffe58"
+dependencies = [
+ "num",
+]
+
+[[package]]
+name = "fugit"
+version = "0.3.6"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7ab17bb279def6720d058cb6c052249938e7f99260ab534879281a95367a87e5"
+dependencies = [
+ "gcd",
+]
+
+[[package]]
+name = "gcd"
+version = "2.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "f37978dab2ca789938a83b2f8bc1ef32db6633af9051a6cd409eff72cbaaa79a"
+dependencies = [
+ "paste",
+]
+
+[[package]]
+name = "itertools"
+version = "0.10.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "b0fd2260e829bddf4cb6ea802289de2f86d6a7a690192fbe91b3f46e0f2c8473"
+dependencies = [
+ "either",
+]
+
+[[package]]
+name = "lazy_static"
+version = "1.4.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646"
+
 [[package]]
 name = "mega2560"
 version = "0.1.0"
@@ -111,9 +248,17 @@ dependencies = [
  "ag-lcd",
  "arduino-hal",
  "embedded-hal",
+ "embedded-time",
  "panic-halt",
+ "rp2040-hal",
 ]
 
+[[package]]
+name = "memchr"
+version = "2.5.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2dffe52ecf27772e601905b7522cb4ef790d2cc203488bbd0e2fe85fcb74566d"
+
 [[package]]
 name = "nb"
 version = "0.1.3"
@@ -129,6 +274,89 @@ version = "1.0.0"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "546c37ac5d9e56f55e73b677106873d9d9f5190605e41a856503623648488cae"
 
+[[package]]
+name = "num"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "8b7a8e9be5e039e2ff869df49155f1c06bd01ade2117ec783e56ab0932b67a8f"
+dependencies = [
+ "num-complex",
+ "num-integer",
+ "num-iter",
+ "num-rational",
+ "num-traits",
+]
+
+[[package]]
+name = "num-complex"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "747d632c0c558b87dbabbe6a82f3b4ae03720d0646ac5b7b4dae89394be5f2c5"
+dependencies = [
+ "num-traits",
+]
+
+[[package]]
+name = "num-integer"
+version = "0.1.45"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "225d3389fb3509a24c93f5c29eb6bde2586b98d9f016636dff58d7c6f7569cd9"
+dependencies = [
+ "autocfg",
+ "num-traits",
+]
+
+[[package]]
+name = "num-iter"
+version = "0.1.43"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "7d03e6c028c5dc5cac6e2dec0efda81fc887605bb3d884578bb6d6bf7514e252"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
+name = "num-rational"
+version = "0.3.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "12ac428b1cb17fce6f731001d307d351ec70a6d202fc2e60f7d4c5e42d8f4f07"
+dependencies = [
+ "autocfg",
+ "num-integer",
+ "num-traits",
+]
+
+[[package]]
+name = "num-traits"
+version = "0.2.15"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "578ede34cf02f8924ab9447f50c28075b4d3e5b269972345e7e0372b38c6cdcd"
+dependencies = [
+ "autocfg",
+]
+
+[[package]]
+name = "num_enum"
+version = "0.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "cf5395665662ef45796a4ff5486c5d41d29e0c09640af4c5f17fd94ee2c119c9"
+dependencies = [
+ "num_enum_derive",
+]
+
+[[package]]
+name = "num_enum_derive"
+version = "0.5.7"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "3b0498641e53dd6ac1a4f22547548caa6864cc4933784319cd1775271c5a46ce"
+dependencies = [
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
 [[package]]
 name = "panic-halt"
 version = "0.2.0"
@@ -141,6 +369,17 @@ version = "1.0.9"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "b1de2e551fb905ac83f73f7aedf2f0cb4a0da7e35efa24a202a936269f1f18e1"
 
+[[package]]
+name = "pio"
+version = "0.2.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "0fd642eada42517e6589cd7f5e830630324aec628fb4aa719000f8801e40c0a2"
+dependencies = [
+ "arrayvec",
+ "num_enum",
+ "paste",
+]
+
 [[package]]
 name = "proc-macro-hack"
 version = "0.5.19"
@@ -165,6 +404,96 @@ dependencies = [
  "proc-macro2",
 ]
 
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+
+[[package]]
+name = "regex"
+version = "1.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e076559ef8e241f2ae3479e36f97bd5741c0330689e217ad51ce2c76808b868a"
+dependencies = [
+ "aho-corasick",
+ "memchr",
+ "regex-syntax",
+]
+
+[[package]]
+name = "regex-syntax"
+version = "0.6.28"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "456c603be3e8d448b072f410900c09faf164fbce2d480456f50eea6e25f9c848"
+
+[[package]]
+name = "riscv"
+version = "0.7.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "6907ccdd7a31012b70faf2af85cd9e5ba97657cc3987c4f13f8e4d2c2a088aba"
+dependencies = [
+ "bare-metal 1.0.0",
+ "bit_field",
+ "riscv-target",
+]
+
+[[package]]
+name = "riscv-target"
+version = "0.1.2"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "88aa938cda42a0cf62a20cfe8d139ff1af20c2e681212b5b34adb5a58333f222"
+dependencies = [
+ "lazy_static",
+ "regex",
+]
+
+[[package]]
+name = "rp2040-hal"
+version = "0.6.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "2f919d0d3c64fd9c7642dac2267a55d6aa1667e45329e8605a3858727acc7711"
+dependencies = [
+ "cortex-m",
+ "cortex-m-rt",
+ "critical-section 0.2.7",
+ "critical-section 1.1.1",
+ "embedded-hal",
+ "fugit",
+ "itertools",
+ "nb 1.0.0",
+ "paste",
+ "pio",
+ "rand_core",
+ "rp2040-hal-macros",
+ "rp2040-pac",
+ "usb-device",
+ "vcell",
+ "void",
+]
+
+[[package]]
+name = "rp2040-hal-macros"
+version = "0.1.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "86479063e497efe1ae81995ef9071f54fd1c7427e04d6c5b84cde545ff672a5e"
+dependencies = [
+ "cortex-m-rt",
+ "proc-macro2",
+ "quote",
+ "syn",
+]
+
+[[package]]
+name = "rp2040-pac"
+version = "0.3.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "13a6106d5db01c7171a39c1f7696780912db9b42fe7ac722db60069c8904ea7c"
+dependencies = [
+ "cortex-m",
+ "vcell",
+]
+
 [[package]]
 name = "rustc_version"
 version = "0.2.3"
@@ -241,6 +570,12 @@ version = "1.0.5"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6ceab39d59e4c9499d4e5a8ee0e2735b891bb7308ac83dfb4e80cad195c9f6f3"
 
+[[package]]
+name = "usb-device"
+version = "0.2.9"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "1f6cc3adc849b5292b4075fc0d5fdcf2f24866e88e336dd27a8943090a520508"
+
 [[package]]
 name = "vcell"
 version = "0.1.3"
@@ -252,3 +587,12 @@ name = "void"
 version = "1.0.2"
 source = "registry+https://github.com/rust-lang/crates.io-index"
 checksum = "6a02e4885ed3bc0f2de90ea6dd45ebcbb66dacffe03547fadbb0eeae2770887d"
+
+[[package]]
+name = "volatile-register"
+version = "0.2.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9ee8f19f9d74293faf70901bc20ad067dc1ad390d2cbf1e3f75f721ffee908b6"
+dependencies = [
+ "vcell",
+]

+ 2 - 2
Cargo.toml

@@ -14,12 +14,12 @@ bench = false
 panic-halt = "0.2.0"
 ag-lcd = "*"
 embedded-hal = "0.2.7"
-
+embedded-time = "*"
+rp2040-hal="*"
 [dependencies.arduino-hal]
 git = "https://github.com/rahix/avr-hal"
 rev = "3b8f39fa2ec5e3359c7bedc33d982e75e8cc3700"
 features = ["arduino-mega2560"]
-
 # Configure the build for minimal size - AVRs have very little program memory
 [profile.dev]
 panic = "abort"

+ 84 - 7
src/main.rs

@@ -1,14 +1,39 @@
 #![no_std]
 #![no_main] 
-use ag_lcd::{Display, LcdDisplay};
+use core::pin::Pin;
+use core::time::Duration;
 
+use ag_lcd::{Display, LcdDisplay};
 use panic_halt as _;
+use embedded_time;
+ 
+fn calibration_calc(temp: f32) -> (f32, Duration) {
+    /// Speed of sound at 0C in m/s.
+    const SOUND_SPEED_0C: f32 = 331.3;
+    /// Increase speed of sound over temperature factor m/[sC].
+    const SOUND_SPEED_INC_OVER_TEMP: f32 = 0.606;
+    /// Maximum measuring range for HC-SR04 sensor in m.
+    const MAX_RANGE: f32 = 4.0;
+
+    // Speed of sound, depending on ambient temperature (if `temp` is `None`, default to 20C).
+    let sound_speed = SOUND_SPEED_0C + (SOUND_SPEED_INC_OVER_TEMP * temp);
+
+    // Polling timeout for **ECHO** pin: since max range for HC-SR04 is 4m, it doesn't make
+    // sense to wait longer than the time required to the ultrasonic sound wave to cover the
+    // max range distance. In other words, if the timeout is reached, the measurement was not
+    // successfull or the object is located too far away from the sensor in order to be
+    // detected.
+    let timeout = Duration::from_secs_f32(MAX_RANGE / sound_speed);
+
+    (sound_speed, timeout)
+}
+
 #[arduino_hal::entry]
 fn main() -> ! {
-
+    // grab all peripherals - pins included
     let peripherals = arduino_hal::Peripherals::take().unwrap();
     let pins = arduino_hal::pins!(peripherals);
-
+    // set the rs/en and data pins for the LCD 
     let rs = pins.d2.into_output().downgrade();
     let en = pins.d3.into_output().downgrade();
     let d4 = pins.d4.into_output().downgrade();
@@ -16,12 +41,64 @@ fn main() -> ! {
     let d6 = pins.d6.into_output().downgrade();
     let d7 = pins.d7.into_output().downgrade();
 
-    let delay = arduino_hal::Delay::new();
-    let mut lcd: LcdDisplay<_, _> = LcdDisplay::new(rs, en, delay)
+    // set the button pins
+    let button_io_pin = pins.d12.into_pull_up_input();
+    
+    // set the led pin
+    let mut led_io_pin = pins.d13.into_output().downgrade();
+    
+    // sonar sensor pins
+    let mut trigger_pin = pins.d9.into_output();
+    let echo_pin = pins.d8.into_pull_up_input();
+
+    // turn on the led
+    led_io_pin.set_high();
+    
+    // Create the LcdDisplay with pins and options
+    let mut lcd: LcdDisplay<_, _> = LcdDisplay::new(rs, en, arduino_hal::Delay::new())
         .with_half_bus(d4, d5, d6, d7)
+        .with_lines(ag_lcd::Lines::TwoLines)
         .with_display(Display::On)
         .build();
+    
+    // clear lcd in case of lingering characters or the arduino didn't restart
     lcd.clear();
-    lcd.print("Hola San Juan!");
-    loop {}
+    
+    // Print to first row of pixels
+    lcd.print("Emigrate.. or..");
+    // move to second one
+    lcd.set_position(0,1);
+    // print again
+    lcd.print("...Degenerate!");
+    // endless loop (this makes return type `!` )
+    let triggered = false;
+    let timer = Timer::new(hal::pac::TIMER, &mut pac.RESETS);
+
+    let mut first_time: bool = true;
+    loop {
+        if first_time {
+            arduino_hal::delay_ms(1000);   // Initial delay to let transducer settle.
+            first_time = false;
+        }
+        // check if the button input pin is high (pressed)
+        if button_io_pin.is_high(){
+            lcd.clear();
+            lcd.print("Saith my heart!");
+            // toggle the led
+            led_io_pin.toggle();
+        }
+
+        // ultrasonic
+        if !triggered {
+            trigger_pin.set_low();
+            arduino_hal::delay_us(2);
+            trigger_pin.set_high();
+            arduino_hal::delay_us(10);
+            trigger_pin.set_low();
+            let ticks = start.elapsed();
+                let hz = self.timer.frequency().0;
+                let distance_mm = (ticks * 171_605) / hz;
+        }
+        
+    }
 }