Skip to content

Commit

Permalink
feat: create function to retrieve channel value
Browse files Browse the repository at this point in the history
  • Loading branch information
jonkerj committed Nov 24, 2021
1 parent 0e9c7ee commit fb5791a
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions attrs.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
}

0 comments on commit fb5791a

Please sign in to comment.