Skip to content

Commit

Permalink
8-update-rtc (#9)
Browse files Browse the repository at this point in the history
* Make WifiManager public, so that we can later acess its state

* StateManager comments

* Refactor WifiManager to use StringUtils for string conversions

* add time_mgr, still need to refactor wifi_mgr to enable web requests

* save

* compiles

* save

* save

* clippy

* cleaning a bit

* clean

* we got connection, rettrieve data and have mnaged the strings stuff better

* rename to time_updater

* tidy

* trying

* can parse response

* some cleaning, i do not need the state structs after all

* typo

* save

* reqwless from fixed version

* use p instead of peripherals

* undo inexpert rtc stuff

* set the RTC

* clean up

---------

Co-authored-by: rafael <rafael>
  • Loading branch information
1-rafael-1 committed Jun 26, 2024
1 parent c32d6f7 commit ddd9767
Show file tree
Hide file tree
Showing 12 changed files with 471 additions and 190 deletions.
14 changes: 9 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ embassy-sync = { git = "https://github.com/embassy-rs/embassy", features = [
"defmt",
] }
embassy-executor = { git = "https://github.com/embassy-rs/embassy", features = [
"task-arena-size-32768",
"task-arena-size-98304",
"arch-cortex-m",
"executor-thread",
"executor-interrupt",
Expand All @@ -41,6 +41,7 @@ embassy-net = { git = "https://github.com/embassy-rs/embassy", features = [
"raw",
"dhcpv4",
"medium-ethernet",
"dns",
] }
embassy-net-wiznet = { git = "https://github.com/embassy-rs/embassy", features = [
"defmt",
Expand All @@ -62,7 +63,6 @@ defmt-test = "0.3"
fixed = "1.23.1"
fixed-macro = "1.2"

#cortex-m = { version = "0.7.6", features = ["critical-section-single-core"] }
cortex-m = { version = "0.7.6", features = ["inline-asm"] }
cortex-m-rt = "0.7.0"
critical-section = "1.1"
Expand All @@ -75,7 +75,8 @@ byte-slice-cast = { version = "1.2.0", default-features = false }
smart-leds = "0.4.0"
heapless = "0.8"
usbd-hid = "0.7.0"

serde = { version = "1.0.203", default-features = false, features = ["derive"] }
serde-json-core = "0.5.1"
embedded-hal-1 = { package = "embedded-hal", version = "1.0" }
embedded-hal-async = "1.0"
embedded-hal-bus = { version = "0.2.0", features = ["async"] }
Expand All @@ -88,6 +89,7 @@ pio-proc = "0.2"
pio = "0.2.1"
rand = { version = "0.8.5", default-features = false }
embedded-sdmmc = "0.7.0"
reqwless = { version = "0.12.0", features = ["defmt"] }

[build-dependencies]
serde = "1.0"
Expand All @@ -104,9 +106,11 @@ codegen-units = 1
debug = 2
debug-assertions = true
incremental = false
opt-level = 1
opt-level = "z"
#opt-level = 1
overflow-checks = true
lto = "off"
#lto = "off"
lto = true

# cargo build/run --release
[profile.release]
Expand Down
50 changes: 47 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
//! updating `memory.x` ensures a rebuild of the application with the
//! new memory settings.

use serde_json;
use std::env;
use std::fs;
use std::fs::File;
Expand All @@ -21,6 +20,7 @@ fn main() {
println!("in build.rs");
memory_x();
wifi_secrets().unwrap();
time_api_config().unwrap();
}

fn wifi_secrets() -> io::Result<()> {
Expand All @@ -30,10 +30,13 @@ fn wifi_secrets() -> io::Result<()> {
// Create a new file in the output directory
let out_dir = env::var("OUT_DIR").expect("OUT_DIR environment variable not set");
let dest_path = Path::new(&out_dir).join("wifi_secrets.rs");
let mut f = File::create(&dest_path).expect("Could not create wifi_secrets.rs file");
let mut f = File::create(dest_path).expect("Could not create wifi_secrets.rs file");

// Read the wifi_config.json file, or create it with dummy values if it doesn't exist
let config_path = Path::new("wifi_config.json");
let manifest_dir =
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR environment variable not set");
let config_path = Path::new(&manifest_dir).join("src/config/wifi_config.json");
//let config_path = Path::new("src/config/wifi_config.json");
let config_contents = if config_path.exists() {
fs::read_to_string(config_path).expect("Could not read wifi_config.json file")
} else {
Expand All @@ -60,6 +63,47 @@ fn wifi_secrets() -> io::Result<()> {
Ok(())
}

fn time_api_config() -> io::Result<()> {
println!("in time_api_config");
// Read the time_api.json file and write the URL and timezone to time_api_config.rs

// Create a new file in the output directory
let out_dir = env::var("OUT_DIR").expect("OUT_DIR environment variable not set");
let dest_path = Path::new(&out_dir).join("time_api_config.rs");
let mut f = File::create(dest_path).expect("Could not create time_api_config.rs file");

// Read the time_api.json file, or create it with dummy values if it doesn't exist
let manifest_dir =
env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR environment variable not set");
let config_path = Path::new(&manifest_dir).join("src/config/time_api.json");
//let config_path = Path::new("src/config/time_api.json");
let config_contents = if config_path.exists() {
fs::read_to_string(config_path).expect("Could not read time_api.json file")
} else {
println!("time_api.json not found, creating with dummy values");
let dummy_config = r#"{"time api by zone":{"baseurl":"dummy","timezone":"dummy"}}"#;
fs::write(config_path, dummy_config).expect("Could not write dummy time_api.json file");
dummy_config.to_string()
};

// Parse the JSON and extract the URL and timezone
let config: serde_json::Value =
serde_json::from_str(&config_contents).expect("Could not parse time_api.json file");
let baseurl = config["time api by zone"]["baseurl"]
.as_str()
.expect("baseurl not found in time_api.json file");
let timezone = config["time api by zone"]["timezone"]
.as_str()
.expect("timezone not found in time_api.json file");

// Combine baseurl and timezone into a single string for TIME_SERVER_URL
let combined_url = format!("{}{}", baseurl, timezone);

// Write the baseurl and timezone to time_api_secrets.rs
writeln!(f, "pub const TIME_SERVER_URL: &str = {:?};", combined_url)?;
Ok(())
}

fn memory_x() {
print!("in memory_x");
// Put `memory.x` in our output directory and ensure it's
Expand Down
6 changes: 0 additions & 6 deletions src/classes/irqs.rs

This file was deleted.

3 changes: 1 addition & 2 deletions src/classes/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
pub mod btn_mgr;
pub mod irqs;
pub mod state_mgr;
pub mod wifi_mgr;
pub mod time_updater;
6 changes: 6 additions & 0 deletions src/classes/state_mgr.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
// not sure if this will even be a thing... but it's here for now
// This is a class that will be used to manage the state of the system, I will need something to call functions across the system and that gets messy quickly
// This will be a way to keep things organized
// Actually I guess I need some sort of event system to call functions across the system... I'll have to look into that but Rust does not seem to have a built in event system
// For now the idea is to do a small version of dependenbcy injection and pass this class around to the other classes that need it; none of that yet implemented

use defmt::*;

pub trait StateManagement {
Expand Down
Loading

0 comments on commit ddd9767

Please sign in to comment.