aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAmir Saeid <amir@glgdgt.com>2020-03-28 01:29:28 +0000
committerAmir Saeid <amir@glgdgt.com>2020-03-28 01:29:28 +0000
commit267bd75ce29d429edba2815994e4babd4711630e (patch)
tree8e5e9d9cc022652aa1b99c9ec2f25d5dd6cb3999
parentfc211b40cf4b54334ce50162b67a0a9c3c57aabf (diff)
Move helper methods to the lib
-rw-r--r--src/bin/main.rs64
-rw-r--r--src/lib.rs70
2 files changed, 73 insertions, 61 deletions
diff --git a/src/bin/main.rs b/src/bin/main.rs
index 9266238..42044bd 100644
--- a/src/bin/main.rs
+++ b/src/bin/main.rs
@@ -4,67 +4,9 @@ extern crate spa;
use chrono::prelude::*;
use clap::{value_t_or_exit, App, Arg};
-use spa::{calc_solar_position, calc_sunrise_and_set, SolarPos, SunriseAndSet};
-use std::os::raw::{c_ushort, c_void};
-use std::ptr;
+use spa::calc_sunrise_and_set;
use std::thread;
use std::time::Duration;
-use x11::xlib::{XDefaultScreen, XFree, XOpenDisplay, XRootWindow};
-use x11::xrandr::{
- XRRAllocGamma, XRRCrtcGamma, XRRGetCrtcGammaSize, XRRGetScreenResourcesCurrent, XRRSetCrtcGamma,
-};
-
-fn set_temp(temp: u32) {
- let ratio: f64 = (temp % 500) as f64 / 500f64;
- unsafe {
- let display = XOpenDisplay(ptr::null_mut());
- let screen = XDefaultScreen(display);
- let root = XRootWindow(display, screen);
- let resource = XRRGetScreenResourcesCurrent(display, root);
-
- for x in 0..(*resource).ncrtc {
- let crtcxid = (*resource).crtcs.offset(x as isize);
- let size = XRRGetCrtcGammaSize(display, *crtcxid);
- let crtc_gamma: *mut XRRCrtcGamma = XRRAllocGamma(size);
- let gamma = sctd::avg(temp, ratio);
-
- for i in 0..size {
- let g = (65535f64 * i as f64) / size as f64;
- *((*crtc_gamma).red as *mut c_ushort).offset(i as isize) = (g * gamma.red) as u16;
- *((*crtc_gamma).green as *mut c_ushort).offset(i as isize) =
- (g * gamma.green) as u16;
- *((*crtc_gamma).blue as *mut c_ushort).offset(i as isize) = (g * gamma.blue) as u16;
- }
- XRRSetCrtcGamma(display, *crtcxid, crtc_gamma);
- XFree(crtc_gamma as *mut c_void);
- }
- }
-}
-
-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
- }
-}
-
-fn get_temp(utc: DateTime<Utc>, ss: &SunriseAndSet, lat: f64, lon: f64) -> f64 {
- let low_temp = 3500f64;
- let high_temp = 5500f64;
-
- 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 => high_temp,
- SunriseAndSet::PolarNight => low_temp,
- }
-}
fn main() {
let matches = App::new("sctd")
@@ -85,7 +27,7 @@ fn main() {
.get_matches();
if matches.is_present("reset") {
- set_temp(5500);
+ sctd::reset_temp();
} else {
let latitude = value_t_or_exit!(matches, "latitude", f64);
let longitude = value_t_or_exit!(matches, "longitude", f64);
@@ -93,7 +35,7 @@ fn main() {
loop {
let utc: DateTime<Utc> = Utc::now();
match calc_sunrise_and_set(utc, latitude, longitude) {
- Ok(ss) => set_temp(get_temp(utc, &ss, latitude, longitude) as u32),
+ Ok(ss) => sctd::set_temp(sctd::get_temp(utc, &ss, latitude, longitude) as u32),
Err(e) => println!("Error calculating sunrise and sunset: {:?}", e),
}
thread::sleep(Duration::from_secs(300));
diff --git a/src/lib.rs b/src/lib.rs
index d3af757..cc4bb92 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,3 +1,20 @@
+extern crate spa;
+
+use chrono::prelude::*;
+use spa::{calc_solar_position, SunriseAndSet};
+use std::os::raw::{c_ushort, c_void};
+use std::ptr;
+use x11::xlib::{XDefaultScreen, XFree, XOpenDisplay, XRootWindow};
+use x11::xrandr::{
+ XRRAllocGamma, XRRCrtcGamma, XRRGetCrtcGammaSize, XRRGetScreenResourcesCurrent, XRRSetCrtcGamma,
+};
+
+const LOW_TEMP: f64 = 3500f64;
+const HIGH_TEMP: f64 = 5500f64;
+
+const TRANSITION_LOW: f64 = -6.0;
+const TRANSITION_HIGH: f64 = 3.0;
+
#[derive(Debug)]
pub struct WhitePoint {
pub red: f64,
@@ -5,6 +22,59 @@ pub struct WhitePoint {
pub blue: f64,
}
+pub fn set_temp(temp: u32) {
+ let ratio: f64 = (temp % 500) as f64 / 500f64;
+ unsafe {
+ let display = XOpenDisplay(ptr::null_mut());
+ let screen = XDefaultScreen(display);
+ let root = XRootWindow(display, screen);
+ let resource = XRRGetScreenResourcesCurrent(display, root);
+
+ for x in 0..(*resource).ncrtc {
+ let crtcxid = (*resource).crtcs.offset(x as isize);
+ let size = XRRGetCrtcGammaSize(display, *crtcxid);
+ let crtc_gamma: *mut XRRCrtcGamma = XRRAllocGamma(size);
+ let gamma = avg(temp, ratio);
+
+ for i in 0..size {
+ let g = (65535f64 * i as f64) / size as f64;
+ *((*crtc_gamma).red as *mut c_ushort).offset(i as isize) = (g * gamma.red) as u16;
+ *((*crtc_gamma).green as *mut c_ushort).offset(i as isize) =
+ (g * gamma.green) as u16;
+ *((*crtc_gamma).blue as *mut c_ushort).offset(i as isize) = (g * gamma.blue) as u16;
+ }
+ XRRSetCrtcGamma(display, *crtcxid, crtc_gamma);
+ XFree(crtc_gamma as *mut c_void);
+ }
+ }
+}
+
+pub fn reset_temp() {
+ set_temp(HIGH_TEMP as u32);
+}
+
+fn get_transition_progress_from_elevation(elevation: f64) -> f64 {
+ if elevation < TRANSITION_LOW {
+ return 0.0;
+ } else if elevation < TRANSITION_HIGH {
+ (-TRANSITION_LOW - elevation) / (-TRANSITION_LOW - TRANSITION_HIGH)
+ } else {
+ return 1.0;
+ }
+}
+
+pub fn get_temp(utc: DateTime<Utc>, ss: &SunriseAndSet, lat: f64, lon: f64) -> f64 {
+ 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 => HIGH_TEMP,
+ SunriseAndSet::PolarNight => LOW_TEMP,
+ }
+}
+
pub fn avg(temp: u32, ratio: f64) -> WhitePoint {
const WPS: [WhitePoint; 20] = [
WhitePoint {