initial commit

This commit is contained in:
Agatha Lovelace 2022-03-18 12:53:53 +01:00
commit 0d035f6e94
Signed by: sorceress
GPG Key ID: 11BBCFC65FC9F401
7 changed files with 173 additions and 0 deletions

33
.cargo/config.toml Normal file
View File

@ -0,0 +1,33 @@
[build]
# Uncomment the relevant target for your chip here (ESP32, ESP32-S2, ESP32-S3 or ESP32-C3)
target = "xtensa-esp32-espidf"
#target = "xtensa-esp32s2-espidf"
#target = "xtensa-esp32s3-espidf"
#target = "riscv32imc-esp-espidf"
[target.xtensa-esp32-espidf]
linker = "ldproxy"
[target.xtensa-esp32s2-espidf]
linker = "ldproxy"
[target.xtensa-esp32s3-espidf]
linker = "ldproxy"
[target.riscv32imc-esp-espidf]
linker = "ldproxy"
# Future - necessary for the experimental "native build" of esp-idf-sys with ESP32C3
# See also https://github.com/ivmarkov/embuild/issues/16
rustflags = ["-C", "default-linker-libraries"]
[unstable]
build-std = ["std", "panic_abort"]
build-std-features = ["panic_immediate_abort"]
[env]
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF stable (v4.4)
ESP_IDF_VERSION = { value = "branch:release/v4.4" }
# Enables the esp-idf-sys "native" build feature (`cargo build --features native`) to build against ESP-IDF master (v5.0)
#ESP_IDF_VERSION = { value = "master" }

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
/.vscode
/.embuild
/target
/Cargo.lock

28
Cargo.toml Normal file
View File

@ -0,0 +1,28 @@
[package]
name = "beep"
version = "0.1.0"
authors = ["Agatha Lovelace <agatha@technogothic.net>"]
edition = "2018"
resolver = "2"
[profile.release]
opt-level = "s"
[profile.dev]
debug = true # Symbols are nice and they don't increase the size on Flash
opt-level = "z"
[features]
default = ["native"]
native = ["esp-idf-sys/native"]
[dependencies]
esp-idf-sys = { version = "0.30.6", features = ["binstart"] }
esp-idf-hal = "0.33.2"
embedded-hal = "0.2.7"
anyhow = "1.0.56"
[build-dependencies]
embuild = "0.28"
anyhow = "1.0.56"

5
build.rs Normal file
View File

@ -0,0 +1,5 @@
// Necessary because of this issue: https://github.com/rust-lang/cargo/issues/9641
fn main() -> anyhow::Result<()> {
embuild::build::CfgArgs::output_propagated("ESP_IDF")?;
embuild::build::LinkArgs::output_propagated("ESP_IDF")
}

4
rust-toolchain.toml Normal file
View File

@ -0,0 +1,4 @@
[toolchain]
channel = "esp"
components = ["rustfmt", "rustc-dev"]
targets = ["xtensa-esp32-none-elf"]

10
sdkconfig.defaults Normal file
View File

@ -0,0 +1,10 @@
# Rust often needs a bit of an extra main task stack size compared to C (the default is 3K)
CONFIG_ESP_MAIN_TASK_STACK_SIZE=7000
# Use this to set FreeRTOS kernel tick frequency to 1000 Hz (100 Hz by default).
# This allows to use 1 ms granuality for thread sleeps (10 ms by default).
#CONFIG_FREERTOS_HZ=1000
# Workaround for https://github.com/espressif/esp-idf/issues/7631
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE=n
CONFIG_MBEDTLS_CERTIFICATE_BUNDLE_DEFAULT_FULL=n

89
src/main.rs Normal file
View File

@ -0,0 +1,89 @@
use esp_idf_sys as _;
// If using the `binstart` feature of `esp-idf-sys`, always keep this module imported
use std::thread;
use std::time::{Duration, Instant};
use embedded_hal::digital::v2::OutputPin;
use esp_idf_hal::gpio::{Gpio17, Output};
use esp_idf_hal::peripherals::Peripherals;
enum NoteDuration {
DottedQuarter,
Quarter,
Eighth,
TripletEighth,
Sixteenth,
}
fn main() -> anyhow::Result<()> {
// Temporary. Will disappear once ESP-IDF 4.4 is released, but for now it is necessary to call this function once,
// or else some patches to the runtime implemented by esp-idf-sys might not link properly.
esp_idf_sys::link_patches();
println!("Hello, world!");
let peripherals = Peripherals::take().unwrap();
let mut speaker = peripherals.pins.gpio17.into_output()?;
loop {
// kinda sus
thread::sleep(Duration::from_millis(600));
play_note(&mut speaker, 523, NoteDuration::Eighth)?;
play_note(&mut speaker, 622, NoteDuration::Eighth)?;
play_note(&mut speaker, 698, NoteDuration::Eighth)?;
play_note(&mut speaker, 739, NoteDuration::Eighth)?;
play_note(&mut speaker, 698, NoteDuration::Eighth)?;
play_note(&mut speaker, 622, NoteDuration::Eighth)?;
play_note(&mut speaker, 523, NoteDuration::DottedQuarter)?;
play_note(&mut speaker, 466, NoteDuration::Sixteenth)?;
play_note(&mut speaker, 587, NoteDuration::Sixteenth)?;
play_note(&mut speaker, 523, NoteDuration::Quarter)?;
thread::sleep(Duration::from_millis(1200));
play_note(&mut speaker, 523, NoteDuration::Eighth)?;
play_note(&mut speaker, 622, NoteDuration::Eighth)?;
play_note(&mut speaker, 698, NoteDuration::Eighth)?;
play_note(&mut speaker, 739, NoteDuration::Eighth)?;
play_note(&mut speaker, 698, NoteDuration::Eighth)?;
play_note(&mut speaker, 622, NoteDuration::Eighth)?;
play_note(&mut speaker, 739, NoteDuration::DottedQuarter)?;
thread::sleep(Duration::from_millis(300));
play_note(&mut speaker, 739, NoteDuration::TripletEighth)?;
play_note(&mut speaker, 698, NoteDuration::TripletEighth)?;
play_note(&mut speaker, 622, NoteDuration::TripletEighth)?;
play_note(&mut speaker, 739, NoteDuration::TripletEighth)?;
play_note(&mut speaker, 698, NoteDuration::TripletEighth)?;
play_note(&mut speaker, 622, NoteDuration::TripletEighth)?;
play_note(&mut speaker, 523, NoteDuration::Quarter)?;
}
loop {
thread::sleep(Duration::from_millis(1000))
}
}
fn play_note(pin: &mut Gpio17<Output>, hz: u64, duration: NoteDuration) -> anyhow::Result<()> {
let duration = match duration {
NoteDuration::DottedQuarter => 900,
NoteDuration::Quarter => 600,
NoteDuration::Eighth => 300,
NoteDuration::TripletEighth => 200,
NoteDuration::Sixteenth => 150,
};
let now = Instant::now();
loop {
if now.elapsed().as_millis() >= duration {
break;
}
pin.set_high()?;
thread::sleep(Duration::from_millis(1000 / hz));
pin.set_low()?;
}
Ok(())
}