Initial code
This commit is contained in:
71
src/batteryinfo.zig
Normal file
71
src/batteryinfo.zig
Normal file
@@ -0,0 +1,71 @@
|
||||
const std = @import("std");
|
||||
|
||||
const _maxRead = 4096;
|
||||
|
||||
pub const BatteryInfo = struct {
|
||||
Manufacturer: []u8,
|
||||
Model: []u8,
|
||||
Technology: []u8,
|
||||
CellSeriesCount: u32 = 0,
|
||||
Capacity: u32 = 0,
|
||||
CapacityDesign: u32 = 0,
|
||||
Vnow: f32 = 0,
|
||||
Vmin: f32 = 0,
|
||||
ChargeNow: u32 = 0,
|
||||
ChargeFull: u32 = 0,
|
||||
ChargeFullPercentageDesign: u32 = 0,
|
||||
ChargeFullDesign: u32 = 0,
|
||||
_allocator: std.mem.Allocator,
|
||||
|
||||
pub fn deinit(self: *const BatteryInfo) void {
|
||||
self._allocator.free(self.Manufacturer);
|
||||
self._allocator.free(self.Model);
|
||||
self._allocator.free(self.Technology);
|
||||
}
|
||||
};
|
||||
|
||||
fn _readInfo(allocator: std.mem.Allocator, dir: std.fs.Dir, path: []const u8) ![]u8 {
|
||||
const start = try dir.readFileAlloc(allocator, path, _maxRead);
|
||||
defer allocator.free(start);
|
||||
const trimmed = std.mem.trim(u8, start, "\r\n\t ");
|
||||
return allocator.dupe(u8, trimmed);
|
||||
}
|
||||
|
||||
fn _readInfoU32(allocator: std.mem.Allocator, dir: std.fs.Dir, path: []const u8) !u32 {
|
||||
const info = try _readInfo(allocator, dir, path);
|
||||
defer allocator.free(info);
|
||||
return std.fmt.parseInt(u32, info, 10);
|
||||
}
|
||||
|
||||
pub fn GetBatteryInfoAlloc(allocator: std.mem.Allocator, dir: std.fs.Dir) !BatteryInfo {
|
||||
const manufacturer = try _readInfo(allocator, dir, "manufacturer");
|
||||
const model = try _readInfo(allocator, dir, "model_name");
|
||||
const technology = try _readInfo(allocator, dir, "technology");
|
||||
|
||||
var info = BatteryInfo{
|
||||
.Manufacturer = manufacturer,
|
||||
.Model = model,
|
||||
.Technology = technology,
|
||||
._allocator = allocator,
|
||||
};
|
||||
|
||||
info.Capacity = try _readInfoU32(allocator, dir, "capacity");
|
||||
info.ChargeNow = try _readInfoU32(allocator, dir, "energy_now") / 1000;
|
||||
info.ChargeFull = try _readInfoU32(allocator, dir, "energy_full") / 1000;
|
||||
info.ChargeFullDesign = try _readInfoU32(allocator, dir, "energy_full_design") / 1000;
|
||||
|
||||
if(info.ChargeFullDesign != 0) {
|
||||
info.ChargeFullPercentageDesign = 100 * info.ChargeFull / info.ChargeFullDesign;
|
||||
info.CapacityDesign = 100 * info.ChargeNow / info.ChargeFullDesign;
|
||||
}
|
||||
|
||||
const Vnow = try _readInfoU32(allocator, dir, "voltage_now") / 1000;
|
||||
const Vmin = try _readInfoU32(allocator, dir, "voltage_min_design") / 1000;
|
||||
|
||||
info.Vnow = @as(f32, @floatFromInt(Vnow)) / 1000;
|
||||
info.Vmin = @as(f32, @floatFromInt(Vmin)) / 1000;
|
||||
|
||||
info.CellSeriesCount = @intFromFloat(@ceil(info.Vmin / 3.7));
|
||||
|
||||
return info;
|
||||
}
|
36
src/main.zig
Normal file
36
src/main.zig
Normal file
@@ -0,0 +1,36 @@
|
||||
const std = @import("std");
|
||||
const batinfo = @import("batteryinfo.zig");
|
||||
|
||||
pub fn main() !void {
|
||||
var buffer: [65535]u8 = undefined;
|
||||
var fba = std.heap.FixedBufferAllocator.init(&buffer);
|
||||
const allocator = fba.allocator();
|
||||
|
||||
const powerSupply = try std.fs.cwd().openDir("/sys/class/power_supply", .{});
|
||||
var i:u32 = 0;
|
||||
while(true) : (i += 1) {
|
||||
const name = try std.fmt.allocPrint(allocator, "BAT{}", .{i});
|
||||
defer allocator.free(name);
|
||||
const batDir = powerSupply.openDir(name, .{}) catch |err| switch(err) {
|
||||
error.FileNotFound => {return;},
|
||||
else => {return err;},
|
||||
};
|
||||
const battery = try batinfo.GetBatteryInfoAlloc(allocator, batDir);
|
||||
defer battery.deinit();
|
||||
try printBattery(battery);
|
||||
}
|
||||
}
|
||||
|
||||
fn printBattery(battery: batinfo.BatteryInfo) !void {
|
||||
const stdout = std.io.getStdOut().writer();
|
||||
try stdout.print("Battery {s} {s}\n", .{battery.Manufacturer, battery.Model});
|
||||
try stdout.print("\tTechnology: {s}\n", .{battery.Technology});
|
||||
try stdout.print("\tCell series count: {}s\n\n", .{battery.CellSeriesCount});
|
||||
try stdout.print("\tCapacity: {}%\n", .{battery.Capacity});
|
||||
try stdout.print("\tCapacity (design): {}%\n", .{battery.CapacityDesign});
|
||||
try stdout.print("\tVmin: {d:.2}V Per cell: {d:.2}V\n", .{battery.Vmin, battery.Vmin/@as(f32, @floatFromInt(battery.CellSeriesCount))});
|
||||
try stdout.print("\tVnow: {d:.2}V Per cell: {d:.2}V\n", .{battery.Vnow, battery.Vnow/@as(f32, @floatFromInt(battery.CellSeriesCount))});
|
||||
try stdout.print("\tCharge now: {}mAh\n", .{battery.ChargeNow});
|
||||
try stdout.print("\tCharge full: {}mAh\n", .{battery.ChargeFull});
|
||||
try stdout.print("\tCharge full (design): {}mAh\n\n", .{battery.ChargeFullDesign});
|
||||
}
|
Reference in New Issue
Block a user