Initial commit

This commit is contained in:
tkl 2021-06-25 05:26:27 +00:00 committed by Thomas Klaehn
commit cd0a52a4ed
7 changed files with 179 additions and 0 deletions

16
.gitignore vendored Normal file
View File

@ -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

45
.vscode/launch.json vendored Normal file
View File

@ -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}"
}
]
}

57
.vscode/tasks.json vendored Normal file
View File

@ -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
}
},
]
}

9
Cargo.toml Normal file
View File

@ -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" }

17
README.md Normal file
View File

@ -0,0 +1,17 @@
# batman
Simple linux battery monitor.
## Build
### debug
```shell
cargo build
```
### release
```shell
cargo build --release
```

3
batman.log Normal file
View File

@ -0,0 +1,3 @@
1624598988 Unknown 98 %
1624598990 Unknown 98 %
1624598992 Unknown 98 %

32
src/main.rs Normal file
View File

@ -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));
}
}