add getInput/Output
This commit is contained in:
parent
6e79d57296
commit
1e1033a118
1 changed files with 17 additions and 9 deletions
26
src/main.zig
26
src/main.zig
|
@ -324,6 +324,14 @@ pub const Component = struct {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn getInput(self: *Component, idx: usize) ?*Signal {
|
||||||
|
return self.inputs.items[idx].signal;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn getOutput(self: *Component, idx: usize) *Signal {
|
||||||
|
return &self.outputs.items[idx].signal;
|
||||||
|
}
|
||||||
|
|
||||||
fn connect(self: *Component, allocator: std.mem.Allocator, self_idx: usize, to: *Component, to_idx: usize) !void {
|
fn connect(self: *Component, allocator: std.mem.Allocator, self_idx: usize, to: *Component, to_idx: usize) !void {
|
||||||
const input = &to.inputs.items[to_idx];
|
const input = &to.inputs.items[to_idx];
|
||||||
input.signal = &self.outputs.items[self_idx].signal;
|
input.signal = &self.outputs.items[self_idx].signal;
|
||||||
|
@ -424,7 +432,7 @@ pub const Battery = struct {
|
||||||
|
|
||||||
pub fn update(component: *Component) AllocatorError!void {
|
pub fn update(component: *Component) AllocatorError!void {
|
||||||
const self: *Battery = @fieldParentPtr("component", component);
|
const self: *Battery = @fieldParentPtr("component", component);
|
||||||
const output = &component.outputs.items[0].signal;
|
const output = component.getOutput(0);
|
||||||
output.digital = @intFromFloat(std.math.sign(self.value));
|
output.digital = @intFromFloat(std.math.sign(self.value));
|
||||||
output.analog = self.value;
|
output.analog = self.value;
|
||||||
}
|
}
|
||||||
|
@ -450,8 +458,8 @@ pub const Not = struct {
|
||||||
|
|
||||||
pub fn update(component: *Component) AllocatorError!void {
|
pub fn update(component: *Component) AllocatorError!void {
|
||||||
const self: *Not = @fieldParentPtr("component", component);
|
const self: *Not = @fieldParentPtr("component", component);
|
||||||
const output = &component.outputs.items[0].signal;
|
const output = component.getOutput(0);
|
||||||
const input = if (component.inputs.items[0].signal) |s| s else &Signal{};
|
const input = if (component.getInput(0)) |s| s else &Signal{};
|
||||||
if (self.invert_output) {
|
if (self.invert_output) {
|
||||||
output.digital = 1 - @as(i2, @intCast(@abs(input.digital)));
|
output.digital = 1 - @as(i2, @intCast(@abs(input.digital)));
|
||||||
output.analog = 1.0 - @abs(input.analog);
|
output.analog = 1.0 - @abs(input.analog);
|
||||||
|
@ -486,8 +494,8 @@ pub const And = struct {
|
||||||
// TODO check implementation
|
// TODO check implementation
|
||||||
pub fn update(component: *Component) AllocatorError!void {
|
pub fn update(component: *Component) AllocatorError!void {
|
||||||
const self: *And = @fieldParentPtr("component", component);
|
const self: *And = @fieldParentPtr("component", component);
|
||||||
const output = &component.outputs.items[0].signal;
|
const output = component.getOutput(0);
|
||||||
const input0 = if (component.inputs.items[0].signal) |s| s else &Signal{};
|
const input0 = if (component.getInput(0)) |s| s else &Signal{};
|
||||||
if (self.arithmetic_mode) {
|
if (self.arithmetic_mode) {
|
||||||
output.digital = input0.digital;
|
output.digital = input0.digital;
|
||||||
output.analog = input0.analog;
|
output.analog = input0.analog;
|
||||||
|
@ -554,8 +562,8 @@ pub const Or = struct {
|
||||||
// TODO check implementation
|
// TODO check implementation
|
||||||
pub fn update(component: *Component) AllocatorError!void {
|
pub fn update(component: *Component) AllocatorError!void {
|
||||||
const self: *Or = @fieldParentPtr("component", component);
|
const self: *Or = @fieldParentPtr("component", component);
|
||||||
const output = &component.outputs.items[0].signal;
|
const output = component.getOutput(0);
|
||||||
const input0 = if (component.inputs.items[0].signal) |s| s else &Signal{};
|
const input0 = if (component.getInput(0)) |s| s else &Signal{};
|
||||||
if (self.arithmetic_mode) {
|
if (self.arithmetic_mode) {
|
||||||
output.digital = input0.digital;
|
output.digital = input0.digital;
|
||||||
output.analog = input0.analog;
|
output.analog = input0.analog;
|
||||||
|
@ -591,12 +599,12 @@ test "max" {
|
||||||
or1.component.inputs.items[1].signal = &b;
|
or1.component.inputs.items[1].signal = &b;
|
||||||
|
|
||||||
try or1.component.update();
|
try or1.component.update();
|
||||||
try std.testing.expectEqual(1.0, or1.component.outputs.items[0].signal.analog);
|
try std.testing.expectEqual(1.0, or1.component.getOutput(0).analog);
|
||||||
|
|
||||||
a.analog = -0.5;
|
a.analog = -0.5;
|
||||||
b.analog = -0.2;
|
b.analog = -0.2;
|
||||||
try or1.component.update();
|
try or1.component.update();
|
||||||
try std.testing.expectEqual(-0.5, or1.component.outputs.items[0].signal.analog);
|
try std.testing.expectEqual(-0.5, or1.component.getOutput(0).analog);
|
||||||
}
|
}
|
||||||
|
|
||||||
const AllocatorError = std.mem.Allocator.Error;
|
const AllocatorError = std.mem.Allocator.Error;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue