From cd0a52a4ed4e65e7c7dd6689c4fe72f3595cad4d Mon Sep 17 00:00:00 2001 From: tkl Date: Fri, 25 Jun 2021 05:26:27 +0000 Subject: [PATCH] Initial commit --- .gitignore | 16 +++++++++++++ .vscode/launch.json | 45 +++++++++++++++++++++++++++++++++++ .vscode/tasks.json | 57 +++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 9 +++++++ README.md | 17 ++++++++++++++ batman.log | 3 +++ src/main.rs | 32 +++++++++++++++++++++++++ 7 files changed, 179 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/launch.json create mode 100644 .vscode/tasks.json create mode 100644 Cargo.toml create mode 100644 README.md create mode 100644 batman.log create mode 100644 src/main.rs diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3ca43ae --- /dev/null +++ b/.gitignore @@ -0,0 +1,16 @@ +# ---> Rust +# Generated by Cargo +# will have compiled files and executables +debug/ +target/ + +# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries +# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html +Cargo.lock + +# These are backup files generated by rustfmt +**/*.rs.bk + +# MSVC Windows builds of rustc generate these, which store debugging information +*.pdb + diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..86bd820 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,45 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "type": "lldb", + "request": "launch", + "name": "Debug executable 'rustbook'", + "cargo": { + "args": [ + "build", + "--bin=rustbook", + "--package=rustbook" + ], + "filter": { + "name": "rustbook", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + }, + { + "type": "lldb", + "request": "launch", + "name": "Debug unit tests in executable 'rustbook'", + "cargo": { + "args": [ + "test", + "--no-run", + "--bin=rustbook", + "--package=rustbook" + ], + "filter": { + "name": "rustbook", + "kind": "bin" + } + }, + "args": [], + "cwd": "${workspaceFolder}" + } + ] +} \ No newline at end of file diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..102e5a2 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,57 @@ +{ + "version": "2.0.0", + "tasks": [ + { + "label": "Cargo build (debug)", + "type": "process", + "command": "cargo", + "args": ["build"], + "problemMatcher": [ + "$rustc" + ], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Cargo build (release)", + "type": "process", + "command": "cargo", + "args": ["build", "--release"], + "problemMatcher": [ + "$rustc" + ], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Cargo clean", + "type": "process", + "command": "cargo", + "args": ["clean"], + "problemMatcher": [ + "$rustc" + ], + "group": { + "kind": "build", + "isDefault": true + } + }, + { + "label": "Cargo doc", + "type": "process", + "command": "cargo", + "args": ["doc"], + "problemMatcher": [ + "$rustc" + ], + "group": { + "kind": "build", + "isDefault": true + } + }, + ] +} diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..5a00377 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "batman" +version = "0.1.0" +edition = "2018" + +# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html + +[dependencies] +linux_battery = { git = "https://git.blackfinn.de/rust/linux_battery.git" } diff --git a/README.md b/README.md new file mode 100644 index 0000000..8dcc97d --- /dev/null +++ b/README.md @@ -0,0 +1,17 @@ +# batman + +Simple linux battery monitor. + +## Build + +### debug + +```shell +cargo build +``` + +### release + +```shell +cargo build --release +``` diff --git a/batman.log b/batman.log new file mode 100644 index 0000000..0d06ee0 --- /dev/null +++ b/batman.log @@ -0,0 +1,3 @@ +1624598988 Unknown 98 % +1624598990 Unknown 98 % +1624598992 Unknown 98 % diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..7a58410 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,32 @@ +use linux_battery::Battery; + +use std::fs::OpenOptions; +use std::io::Write; +use std::time::{Duration, SystemTime}; +use std::thread::sleep; + +fn main() { + let mut battery = Battery::new("BAT0"); + let mut log_file = OpenOptions::new() + .create(true) + .append(true) + .open("batman.log") + .expect("can't open log file"); + + loop { + let sys_time = SystemTime::now(); + match sys_time.duration_since(SystemTime::UNIX_EPOCH) { + Ok(n) => { + let mut log_entry = format!("{}\t{}", n.as_secs(), battery.state()); + match battery.capacity() { + Some(cap) => log_entry = format!("{}\t{} %", log_entry, cap), + None => {}, + } + log_entry = format!("{}\r\n", log_entry); + log_file.write_all(log_entry.as_bytes()).expect("write to file failed"); + }, + Err(_) => {}, + } + sleep(Duration::new(2, 0)); + } +}