Created
February 14, 2023 13:11
-
-
Save rondreas/05cdd5b2ca7502ba4cb3a4282c791ebf to your computer and use it in GitHub Desktop.
Implemented a channel modifier that accepts a float input and return the value multiplied by two.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Manual implementation of the SDK chanmod double | |
""" | |
import lx | |
import lxifc | |
class Operator(lxifc.ChannelModOperator): | |
def __init__(self, cmod): | |
""" When we are supposed to instantiate this class in cman_Allocate further down, we have access to the | |
Channel Mod Setup interface, which we will pass here so we can access the channels on it. """ | |
self.setup = lx.object.ChannelModSetup(cmod) | |
def cmop_Evaluate(self): | |
""" Accessing Read and Write value objects, we get the value for the input, and set the output to input | |
multiplied by two. """ | |
input_ = lx.object.Value(self.setup.ReadValue("input")) | |
i = input_.GetFlt() | |
output_ = lx.object.Value(self.setup.WriteValue("output")) | |
output_.SetFlt(i * 2.0) | |
class Instance(lxifc.PackageInstance): | |
""" Dummy subclass of Package Instance, as we aren't allowed to just return an instance of Package Instance """ | |
pass | |
class Manager(lxifc.Package, lxifc.ChannelModManager): | |
def pkg_SetupChannels(self, addChan): | |
""" The SetupChannels() method is called once when the package is initialized to define the set of channels | |
common to all item with this package. """ | |
add_channel = lx.object.AddChannel(addChan) | |
add_channel.NewChannel("input", lx.symbol.sTYPE_FLOAT) | |
add_channel.SetDefault(0.0, 0) | |
add_channel.NewChannel("output", lx.symbol.sTYPE_FLOAT) | |
add_channel.SetDefault(0.0, 0) | |
def pkg_TestInterface(self, guid): | |
return guid == lx.symbol.u_PACKAGEINSTANCE | |
def pkg_Attach(self): | |
""" The Attach() method is called for each item to assign as the package is being attached, and should return | |
an object implementing ILxPackageInstance.""" | |
return Instance() | |
def cman_Define(self, cmod: lx.object.Unknown): | |
""" The Define() method is passed a ChannelModSetup interface and is expected to call AddChannel() or AddTime() | |
methods on that object to add the channels it wants for reading and writing. The names are all for channels on | |
items of this type, and may be set as inputs or outputs.""" | |
setup = lx.object.ChannelModSetup(cmod) | |
# Lookup the channel names and flag them as input and outputs, | |
setup.AddChannel("input", lx.symbol.fCHMOD_INPUT) | |
setup.AddChannel("output", lx.symbol.fCHMOD_OUTPUT) | |
def cman_Allocate(self, cmod: lx.object.Unknown): | |
return Operator(cmod) | |
lx.bless(Manager, "py.cmod.double", {lx.symbol.sPKG_SUPERTYPE: lx.symbol.sITYPE_CHANMODIFY}) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment