Skip to content

Commit

Permalink
Static allocator, fix first temp after started
Browse files Browse the repository at this point in the history
  • Loading branch information
Eraden committed Jun 26, 2021
1 parent c26e619 commit ece0840
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 16 deletions.
18 changes: 17 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "argonfand"
version = "1.0.2"
version = "1.0.3"
authors = ["Adrian Woźniak <[email protected]>"]
edition = "2018"
license = "MIT OR Apache-2.0"
Expand All @@ -12,3 +12,4 @@ gpio = { version = "0.4.1" }
gumdrop = { version = "0.8.0" }
serde = { version = "1.0.125", features = ["derive"] }
toml = { version = "0.5.8" }
static-alloc = { version = "0.2.3" }
13 changes: 5 additions & 8 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,11 @@ impl FromStr for Temp {
#[serde(transparent)]
pub struct Speed(pub u8);

impl Speed {
pub fn into_inner(&self) -> u8 {
self.0
impl std::ops::Deref for Speed {
type Target = u8;

fn deref(&self) -> &u8 {
&self.0
}
}

Expand Down Expand Up @@ -89,11 +91,6 @@ impl Config {
Some(1000)
}

pub fn push(&mut self, speed: SpeedConfig) {
self.values.push(speed);
self.values.sort_by(|a, b| a.temp.cmp(&b.temp))
}

// for config
// 20'C = 30
// 40'C = 50
Expand Down
20 changes: 14 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
use crate::config::*;
use gumdrop::Options;
use static_alloc::Bump;
use std::process::Stdio;
use std::thread::sleep;
use std::process::exit;

mod config;

#[global_allocator]
static A: Bump<[u8; 1 << 14]> = Bump::uninit();

static CONFIG_PATH: &str = "/etc/argonfand.toml";

#[derive(Options)]
Expand Down Expand Up @@ -72,7 +76,7 @@ fn main() -> std::io::Result<()> {
config.delay = Some(delay);
}
config.force_speed = opts.force_speed;
eprintln!("Loaded config: {:?}", config);
eprintln!("Loaded config: {:#?}", config);
let mut bus = match rppal::i2c::I2c::with_bus(1) {
Ok(bus) => bus,
Err(e) => {
Expand All @@ -89,7 +93,7 @@ fn main() -> std::io::Result<()> {

fn set_speed(bus: &mut rppal::i2c::I2c, config: &Config) {
let duration = std::time::Duration::from_secs(config.delay.unwrap_or(30));
let mut prev_block = Speed(0);
let mut prev_block = Speed(255);

if let Some(speed) = config.force_speed {
if let Err(e) = bus.write(&[speed]) {
Expand Down Expand Up @@ -119,11 +123,15 @@ fn set_speed(bus: &mut rppal::i2c::I2c, config: &Config) {
if config.verbose {
eprintln!("SPEED: {:?}", block)
};
if block != prev_block {
if *block != *prev_block {
prev_block = block;
if let Err(e) = bus.write(&[block.into_inner()]) {
eprintln!(" bus out {}", e);
}
match bus.write(&[*block]) {
Err(e) => eprintln!(" bus out {}", e),
Ok(n) if config.verbose => {
println!("write new speed result {} bytes written", n);
}
_ => (),
};
}
sleep(duration);
}
Expand Down

0 comments on commit ece0840

Please sign in to comment.