Modules.zig (1261B)
1 const std = @import("std"); 2 const mem = std.mem; 3 const ArrayList = std.ArrayList; 4 5 const Modules = @This(); 6 7 const Backlight = @import("modules/Backlight.zig"); 8 const Battery = @import("modules/Battery.zig"); 9 const Pulse = @import("modules/Pulse.zig"); 10 11 var state = &@import("root").state; 12 13 const Tag = enum { backlight, battery, pulse }; 14 15 backlight: ?Backlight = null, 16 battery: ?Battery = null, 17 pulse: ?Pulse = null, 18 order: ArrayList(Tag), 19 20 pub fn init() Modules { 21 return Modules{ .order = ArrayList(Tag).init(state.gpa) }; 22 } 23 24 pub fn deinit(self: *Modules) void { 25 if (self.backlight) |*mod| mod.deinit(); 26 if (self.battery) |*mod| mod.deinit(); 27 if (self.pulse) |*mod| mod.deinit(); 28 self.order.deinit(); 29 } 30 31 pub fn register(self: *Modules, name: []const u8) !void { 32 if (mem.eql(u8, name, "backlight")) { 33 self.backlight = try Backlight.init(); 34 try self.order.append(.backlight); 35 } else if (mem.eql(u8, name, "battery")) { 36 self.battery = try Battery.init(); 37 try self.order.append(.battery); 38 } else if (mem.eql(u8, name, "pulse")) { 39 self.pulse = try Pulse.init(); 40 try self.pulse.?.start(); 41 try self.order.append(.pulse); 42 } else { 43 return error.UnknownModule; 44 } 45 }