misc
This commit is contained in:
parent
84965904b3
commit
1dedf1eedf
1 changed files with 59 additions and 24 deletions
83
src/main.zig
83
src/main.zig
|
@ -5,8 +5,17 @@ pub fn main() !void {
|
|||
defer _ = gpa.deinit();
|
||||
// const allocator = gpa.allocator();
|
||||
|
||||
// var input = Wire{ .digital = false, .analog = 1.0 };
|
||||
// var input_buffer = Not{ .invert_output = false, .input = &input };
|
||||
var input = Signal{ .digital = 1, .analog = 1.0 };
|
||||
|
||||
var not1 = Not{ .input = &input };
|
||||
var not2 = Not{ .input = ¬1.output, .invert_output = false };
|
||||
var not3 = Not{ .input = ¬2.output };
|
||||
|
||||
not1.process();
|
||||
not2.process();
|
||||
not3.process();
|
||||
|
||||
std.debug.print("{}\n{}\n{}\n{}\n", .{ input, not1.output, not2.output, not3.output });
|
||||
|
||||
// // analog in
|
||||
// var wire1 = Wire{};
|
||||
|
@ -78,13 +87,44 @@ pub const Component = struct {
|
|||
x: u16 = 0,
|
||||
y: u16 = 0,
|
||||
|
||||
processFn: *const fn (*Component) void,
|
||||
processFn: *const fn (*Component, []*Signal) []Signal,
|
||||
};
|
||||
|
||||
pub const Signal = struct {
|
||||
digital: i2 = 0,
|
||||
analog: f32 = 0.0,
|
||||
color: u24 = 0,
|
||||
|
||||
pub fn format(
|
||||
self: Signal,
|
||||
comptime fmt: []const u8,
|
||||
options: std.fmt.FormatOptions,
|
||||
writer: anytype,
|
||||
) !void {
|
||||
_ = .{ fmt, options };
|
||||
try writer.writeAll("Signal(");
|
||||
if (self.digital < 0) try writer.writeByte('-') else try writer.writeByte('+');
|
||||
try writer.print("{d} / {d:0>1.4})", .{
|
||||
@abs(self.digital),
|
||||
self.analog,
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
pub const Circuit = struct {
|
||||
width: u16 = 8,
|
||||
height: u16 = 8,
|
||||
components: std.ArrayList(Component),
|
||||
|
||||
pub fn init(allocator: std.mem.Allocator) Circuit {
|
||||
return .{
|
||||
.components = std.ArrayList(Component).init(allocator),
|
||||
};
|
||||
}
|
||||
|
||||
pub fn deinit(self: *Circuit) void {
|
||||
self.components.deinit();
|
||||
}
|
||||
};
|
||||
|
||||
pub const Battery = struct {
|
||||
|
@ -101,12 +141,6 @@ pub const Battery = struct {
|
|||
}
|
||||
};
|
||||
|
||||
pub const Signal = struct {
|
||||
digital: i2 = 0,
|
||||
analog: f32 = 0.0,
|
||||
color: u24 = 0,
|
||||
};
|
||||
|
||||
// pub const Wire = struct {
|
||||
// digital: bool = false,
|
||||
// analog: f32 = 0.0,
|
||||
|
@ -143,24 +177,25 @@ pub const Signal = struct {
|
|||
// }
|
||||
// };
|
||||
|
||||
// pub const Not = struct {
|
||||
// // component: Component = .{},
|
||||
pub const Not = struct {
|
||||
// component: Component = .{},
|
||||
|
||||
// input: *Wire,
|
||||
// output: *Wire,
|
||||
input: *Signal,
|
||||
output: Signal = .{},
|
||||
|
||||
// invert_output: bool = true,
|
||||
invert_output: bool = true,
|
||||
|
||||
// pub fn process(self: *Not) void {
|
||||
// if (self.invert_output) {
|
||||
// self.output.digital = !self.input.digital;
|
||||
// self.output.analogSet(1.0 - @abs(self.input.analog));
|
||||
// } else {
|
||||
// self.output.digital = self.input.digital;
|
||||
// self.output.analogSet(self.input.analog); // TODO does the output get clamped here?
|
||||
// }
|
||||
// }
|
||||
// };
|
||||
pub fn process(self: *Not) void {
|
||||
if (self.invert_output) {
|
||||
self.output.digital = -self.input.digital;
|
||||
self.output.analog = 1.0 - @abs(self.input.analog);
|
||||
} else {
|
||||
self.output.digital = self.input.digital;
|
||||
self.output.analog = self.input.analog;
|
||||
}
|
||||
self.output.analog = std.math.clamp(self.output.analog, 0.0, 1.0);
|
||||
}
|
||||
};
|
||||
|
||||
// pub const And = struct {
|
||||
// // component: Component,
|
||||
|
|
Loading…
Add table
Reference in a new issue