From c942da0bf354acd7911cf43301c0c0d62a04c55c Mon Sep 17 00:00:00 2001 From: Amir Saeid Date: Sat, 28 Mar 2020 01:14:58 +0000 Subject: Implement transition progress --- Cargo.lock | 3 +-- Cargo.toml | 4 +--- src/bin/main.rs | 46 +++++++++++++++++++++++++--------------------- 3 files changed, 27 insertions(+), 26 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4c438e8..61f29b6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -117,8 +117,7 @@ dependencies = [ [[package]] name = "spa" version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a2cb0b73b1ea9c2b5bbb17576ff522c9a956b414e820e91e04fd86b32769e68a" +source = "git+https://github.com/amir/spa-rs?branch=pub-SolarPos#fa80d0ce99d675674cf1a7cc9cf2ee2e33f501ea" dependencies = [ "chrono", ] diff --git a/Cargo.toml b/Cargo.toml index 8e8fca4..6e3909e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -4,11 +4,9 @@ version = "0.1.0" authors = ["Amir Saeid "] edition = "2018" -# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html - [dependencies] chrono = "0.4.11" -spa = "0.2.0" +spa = { git = "https://github.com/amir/spa-rs", branch = "pub-SolarPos" } clap = "2.33.0" [dependencies.x11] diff --git a/src/bin/main.rs b/src/bin/main.rs index 832320a..9266238 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -4,7 +4,7 @@ extern crate spa; use chrono::prelude::*; use clap::{value_t_or_exit, App, Arg}; -use spa::{calc_sunrise_and_set, SunriseAndSet}; +use spa::{calc_solar_position, calc_sunrise_and_set, SolarPos, SunriseAndSet}; use std::os::raw::{c_ushort, c_void}; use std::ptr; use std::thread; @@ -41,26 +41,28 @@ fn set_temp(temp: u32) { } } -fn get_temp(utc: DateTime, ss: &SunriseAndSet) -> u32 { - let low_temp = 2500; - let high_temp = 5500; +fn get_transition_progress_from_elevation(elevation: f64) -> f64 { + if elevation < -6.0 { + return 0.0 + } else if elevation < 3.0 { + (-6.0 - elevation) / (-6.0 - 3.0) + } else { + return 1.0 + } +} - match *ss { - SunriseAndSet::Daylight(sunrise, sunset) => { - let since_sunrise = utc.signed_duration_since(sunrise); - let since_sunset = utc.signed_duration_since(sunset); +fn get_temp(utc: DateTime, ss: &SunriseAndSet, lat: f64, lon: f64) -> f64 { + let low_temp = 3500f64; + let high_temp = 5500f64; - // this is where gradual increase/decrease should happen - if since_sunrise.num_seconds() < 0 { - return low_temp; - } else if since_sunset.num_seconds() < 0 { - return high_temp; - } else { - return low_temp; - } + match *ss { + SunriseAndSet::Daylight(_, _) => { + let elevation = 90f64 - calc_solar_position(utc, lat, lon).unwrap().zenith_angle; + let progress = get_transition_progress_from_elevation(elevation); + low_temp + (progress * (high_temp - low_temp)) } - SunriseAndSet::PolarDay => return high_temp, - SunriseAndSet::PolarNight => return low_temp, + SunriseAndSet::PolarDay => high_temp, + SunriseAndSet::PolarNight => low_temp, } } @@ -70,12 +72,14 @@ fn main() { .arg( Arg::with_name("latitude") .long("latitude") - .takes_value(true), + .takes_value(true) + .allow_hyphen_values(true), ) .arg( Arg::with_name("longitude") .long("longitude") - .takes_value(true), + .takes_value(true) + .allow_hyphen_values(true), ) .arg(Arg::with_name("reset").long("reset")) .get_matches(); @@ -89,7 +93,7 @@ fn main() { loop { let utc: DateTime = Utc::now(); match calc_sunrise_and_set(utc, latitude, longitude) { - Ok(ss) => set_temp(get_temp(utc, &ss)), + Ok(ss) => set_temp(get_temp(utc, &ss, latitude, longitude) as u32), Err(e) => println!("Error calculating sunrise and sunset: {:?}", e), } thread::sleep(Duration::from_secs(300)); -- cgit v1.2.3