Skip to content

Commit a54a826

Browse files
authored
4-add-wifi-connection (#5)
* save * Update rust-toolchain.toml to use stable channel * safely use wifi credentials via build.rs and include! in wifi_mgr * better orga and errors when reading wifi secrets * wifi connects * build fails: missing wifi config file, need a dummy file for ci --------- Co-authored-by: rafael <rafael>
1 parent a1cf965 commit a54a826

13 files changed

+259
-278
lines changed

.gitignore

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
.idea/
22
target
33
Cargo.lock
4-
*.orig
4+
*.orig
5+
6+
#secrets
7+
wifi_config.json

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,10 @@ pio = "0.2.1"
8989
rand = { version = "0.8.5", default-features = false }
9090
embedded-sdmmc = "0.7.0"
9191

92+
[build-dependencies]
93+
serde = "1.0"
94+
serde_json = "1.0"
95+
9296
# for each crate in the `tests` directory
9397
[[test]]
9498
name = "validate_ci" # tests/test-name.rs

build.rs

+48
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,60 @@
88
//! updating `memory.x` ensures a rebuild of the application with the
99
//! new memory settings.
1010
11+
use serde_json;
1112
use std::env;
13+
use std::fs;
1214
use std::fs::File;
15+
use std::io;
1316
use std::io::Write;
17+
use std::path::Path;
1418
use std::path::PathBuf;
1519

1620
fn main() {
21+
println!("in build.rs");
22+
memory_x();
23+
wifi_secrets().unwrap();
24+
}
25+
26+
fn wifi_secrets() -> io::Result<()> {
27+
println!("in wifi_secrets");
28+
// Read the wifi_config.json file and write the SSID and password to wifi_secrets.rs
29+
30+
// Create a new file in the output directory
31+
let out_dir = env::var("OUT_DIR").expect("OUT_DIR environment variable not set");
32+
let dest_path = Path::new(&out_dir).join("wifi_secrets.rs");
33+
let mut f = File::create(&dest_path).expect("Could not create wifi_secrets.rs file");
34+
35+
// Read the wifi_config.json file, or create it with dummy values if it doesn't exist
36+
let config_path = Path::new("wifi_config.json");
37+
let config_contents = if config_path.exists() {
38+
fs::read_to_string(config_path).expect("Could not read wifi_config.json file")
39+
} else {
40+
println!("wifi_config.json not found, creating with dummy values");
41+
let dummy_config = r#"{"ssid":"dummy","password":"dummy"}"#;
42+
fs::write(config_path, dummy_config).expect("Could not write dummy wifi_config.json file");
43+
dummy_config.to_string()
44+
};
45+
46+
// Parse the JSON and extract the SSID and password
47+
let config: serde_json::Value =
48+
serde_json::from_str(&config_contents).expect("Could not parse wifi_config.json file");
49+
let ssid = config["ssid"]
50+
.as_str()
51+
.expect("ssid not found in wifi_config.json file");
52+
let password = config["password"]
53+
.as_str()
54+
.expect("password not found in wifi_config.json file");
55+
56+
// Write the SSID and password to wifi_secrets.rs
57+
println!("in wifi_secrets, before writing ssid and password to output file");
58+
writeln!(f, "pub const SSID: &str = {:?};", ssid)?;
59+
writeln!(f, "pub const PASSWORD: &str = {:?};", password)?;
60+
Ok(())
61+
}
62+
63+
fn memory_x() {
64+
print!("in memory_x");
1765
// Put `memory.x` in our output directory and ensure it's
1866
// on the linker search path.
1967
let out = &PathBuf::from(env::var_os("OUT_DIR").unwrap());

cargo-generate/Cargo.toml

-103
This file was deleted.

cargo-generate/launch.json

-78
This file was deleted.

cargo-generate/pre-hook.rhai

-8
This file was deleted.

cargo-generate/wifi-blinky.rs

-76
This file was deleted.

flash_wifi_firmware.ps1

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Navigate to the cyw43-firmware directory
2+
Set-Location -Path "C:\Users\rafae\git\pi-pico-alarmclock-rust\cargo-generate\cyw43-firmware"
3+
4+
# Download firmware using probe-rs
5+
Write-Host "Downloading firmware..."
6+
probe-rs download 43439A0.bin --binary-format bin --chip RP2040 --base-address 0x10100000
7+
probe-rs download 43439A0_clm.bin --binary-format bin --chip RP2040 --base-address 0x10140000
8+
Write-Host "Firmware download completed."

rust-toolchain.toml

+3-5
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
# Before upgrading check that everything is available on all tier1 targets here:
22
# https://rust-lang.github.io/rustup-components-history
33
[toolchain]
4-
channel = "1.75"
5-
components = [ "rust-src", "rustfmt", "llvm-tools" ]
6-
targets = [
7-
"thumbv6m-none-eabi",
8-
]
4+
channel = "stable"
5+
components = ["rust-src", "rustfmt", "llvm-tools"]
6+
targets = ["thumbv6m-none-eabi"]

src/classes/irqs.rs

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
use embassy_rp::peripherals::PIO0;
2+
use embassy_rp::pio::InterruptHandler;
3+
4+
pub struct Irqs {
5+
pub pio0_irq_0: InterruptHandler<PIO0>,
6+
}

src/classes/mod.rs

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
11
pub mod btn_mgr;
2+
pub mod irqs;
23
pub mod state_mgr;
4+
pub mod wifi_mgr;

0 commit comments

Comments
 (0)