package hardware import ( "context" _ "embed" "io" "unsafe" "github.com/pkg/errors" "github.com/rs/zerolog/log" ) /* #cgo LDFLAGS: -L${SRCDIR}/../build/bin -ldmxSender #include "cpp/include/dmxSenderBridge.h" */ import "C" // FTDIPeripheral contains the data of an FTDI peripheral type FTDIPeripheral struct { info PeripheralInfo // The peripheral basic data settings map[string]interface{} // The settings of the peripheral dmxDevice unsafe.Pointer // The command object for piloting the DMX ouptut stdin io.WriteCloser // For writing in the DMX sender stdout io.ReadCloser // For reading from the DMX sender stderr io.ReadCloser // For reading the errors disconnectChan chan struct{} // Channel to cancel the connection errorsChan chan error // Channel to get the errors } // NewFTDIPeripheral creates a new FTDI peripheral func NewFTDIPeripheral(info PeripheralInfo) (*FTDIPeripheral, error) { log.Info().Str("file", "FTDIPeripheral").Str("name", info.Name).Str("s/n", info.SerialNumber).Msg("FTDI peripheral created") settings := make(map[string]interface{}) return &FTDIPeripheral{ info: info, dmxDevice: C.dmx_create(), settings: settings, disconnectChan: make(chan struct{}), errorsChan: make(chan error, 1), }, nil } // Connect connects the FTDI peripheral func (p *FTDIPeripheral) Connect(ctx context.Context, location int) error { // Check if the device has already been created if p.dmxDevice == nil { return errors.Errorf("the DMX device has not been created!") } // Connect the peripheral log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("connecting FTDI peripheral...") err := C.dmx_connect(p.dmxDevice) if err { log.Error().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to connect the DMX device") return errors.Errorf("Unable to connect the DMX Device on the specified port") } //TODO: Destroy the object when context is done to avoid memory loss log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device connected successfully") return nil } // Disconnect disconnects the FTDI peripheral func (p *FTDIPeripheral) Disconnect() error { // Check if the device has already been created if p.dmxDevice == nil { return errors.Errorf("the DMX device has not been created!") } //TODO: What actions for disconnecting the DMX device? return nil } // Activate activates the FTDI peripheral func (p *FTDIPeripheral) Activate(ctx context.Context) error { // Check if the device has already been created if p.dmxDevice == nil { return errors.Errorf("the DMX device has not been created!") } log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("activating FTDI peripheral...") C.dmx_activate(p.dmxDevice) log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device activated successfully") return nil } // Deactivate deactivates the FTDI peripheral func (p *FTDIPeripheral) Deactivate(ctx context.Context) error { // Check if the device has already been created if p.dmxDevice == nil { return errors.Errorf("the DMX device has not been created!") } log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("deactivating FTDI peripheral...") C.dmx_deactivate(p.dmxDevice) log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device deactivated successfully") return nil } // SetSettings sets a specific setting for this peripheral func (p *FTDIPeripheral) SetSettings(settings map[string]interface{}) error { p.settings = settings return nil } // SetDeviceProperty sends a command to the specified device func (p *FTDIPeripheral) SetDeviceProperty(ctx context.Context, channelNumber uint32, channelValue byte) error { // Check if the device has already been created if p.dmxDevice == nil { return errors.Errorf("the DMX device has not been created!") } log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("setting device property on FTDI peripheral...") C.dmx_setValue(p.dmxDevice, C.int(channelNumber), C.int(channelValue)) log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("device property set on FTDI peripheral successfully") return nil } // GetSettings gets the peripheral settings func (p *FTDIPeripheral) GetSettings() map[string]interface{} { return p.settings } // GetInfo gets all the peripheral information func (p *FTDIPeripheral) GetInfo() PeripheralInfo { return p.info }