From fb5791a313b1ad37e915b94539bcb7b04a82dcd0 Mon Sep 17 00:00:00 2001 From: Jorik Jonker Date: Wed, 24 Nov 2021 16:15:44 +0100 Subject: [PATCH] feat: create function to retrieve channel value --- attrs.go | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/attrs.go b/attrs.go index e0f6df8..ecc0759 100644 --- a/attrs.go +++ b/attrs.go @@ -16,6 +16,20 @@ type CompareChannel func(*Channel) bool // implementing some kind of comparison function type CompareChannelAttribute func(*ChannelAttribute) bool +type attributeOperation func(float64, float64) float64 + +type attributeOp struct { + name string + op attributeOperation +} + +var attributeOps []attributeOp = []attributeOp{ + {name: "raw", op: func(c, v float64) float64 { return v }}, + {name: "input", op: func(c, v float64) float64 { return v }}, + {name: "offset", op: func(c, v float64) float64 { return c + v }}, + {name: "scale", op: func(c, v float64) float64 { return c * v }}, +} + // GetDevice fetches the first device from the context satisfying the comparison // function func (h *Context) GetDevice(comp CompareDevice) *Device { @@ -104,3 +118,16 @@ func (c *Channel) GetAttribute(comp CompareChannelAttribute) *ChannelAttribute { func (c *Channel) GetAttrByName(name string) *ChannelAttribute { return c.GetAttribute(func(ca *ChannelAttribute) bool { return ca.Name == name }) } + +func (c *Channel) GetValue() float64 { + var current float64 = 0 + + for _, attrOp := range attributeOps { + a := c.GetAttrByName(attrOp.name) + if a != nil { + current = attrOp.op(current, a.Value) + } + } + + return current +}