From 4e0829e821de441d79a85a90d91257210b7fb056 Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sun, 26 Jan 2025 12:01:31 +0100 Subject: [PATCH 1/7] rework on the peripherals and finders --- .../Settings/InputsOutputsContent.svelte | 72 +------ hardware/FTDIFinder.go | 106 ++++++---- hardware/FTDIPeripheral.go | 80 ++++--- hardware/MIDIFinder.go | 70 +++++-- hardware/MIDIPeripheral.go | 30 +-- hardware/OS2LFinder.go | 69 ++++-- hardware/OS2LPeripheral.go | 30 ++- hardware/hardware.go | 32 +-- hardware/interfaces.go | 19 +- peripherals.go | 196 +++++++++--------- 10 files changed, 360 insertions(+), 344 deletions(-) diff --git a/frontend/src/components/Settings/InputsOutputsContent.svelte b/frontend/src/components/Settings/InputsOutputsContent.svelte index 415e380..c629c44 100644 --- a/frontend/src/components/Settings/InputsOutputsContent.svelte +++ b/frontend/src/components/Settings/InputsOutputsContent.svelte @@ -5,59 +5,20 @@ import { t, _ } from 'svelte-i18n' import { generateToast, needProjectSave, peripherals } from "../../stores"; import { get } from "svelte/store" - import { UpdatePeripheralSettings, GetPeripheralSettings, AddOS2LPeripheral, RemovePeripheral, ConnectFTDI, ActivateFTDI, DeactivateFTDI, DisconnectFTDI, SetDeviceFTDI, AddPeripheral } from "../../../wailsjs/go/main/App"; + import { UpdatePeripheralSettings, GetPeripheralSettings, RemovePeripheral, AddPeripheral } from "../../../wailsjs/go/main/App"; import RoundedButton from "../General/RoundedButton.svelte"; - function ftdiConnect(){ - ConnectFTDI().then(() => - console.log("FTDI connected")) - .catch((error) => { - console.log("Error when trying to connect: " + error) - }) - } - - function ftdiActivate(){ - ActivateFTDI().then(() => - console.log("FTDI activated")) - .catch((error) => { - console.log("Error when trying to activate: " + error) - }) - } - - function ftdiDeactivate(){ - DeactivateFTDI().then(() => - console.log("FTDI deactivated")) - .catch((error) => { - console.log("Error when trying to deactivate: " + error) - }) - } - - let sliderValue = 0 - function ftdiSetDevice(value){ - console.log("value is " + value) - SetDeviceFTDI(value).then(() => - console.log("FTDI device set up")) - .catch((error) => { - console.log("Error when trying to set the device: " + error) - }) - } - - function ftdiDisconnect(){ - DisconnectFTDI().then(() => - console.log("FTDI disconnected")) - .catch((error) => { - console.log("Error when trying to disconnect: " + error) - }) - } - // Add the peripheral to the project function addPeripheral(peripheral){ // Add the peripheral to the project (backend) - AddPeripheral(peripheral.ProtocolName, peripheral.SerialNumber).then(() => { + AddPeripheral(peripheral).then((serialNumber) => { + peripheral.SerialNumber = serialNumber peripherals.update((value) => { - if (value[peripheral.SerialNumber]) { - value[peripheral.SerialNumber].isSaved = true; + // If the peripheral doesn't exists yet, create it + if (!(peripheral.SerialNumber in value)) { + value[peripheral.SerialNumber] = peripheral } + value[peripheral.SerialNumber].isSaved = true; return {...value} }) $needProjectSave = true @@ -96,23 +57,6 @@ }) } - // Create the OS2L peripheral - function createOS2L(){ - AddOS2LPeripheral().then(os2lDevice => { - peripherals.update(currentPeriph => { - os2lDevice.isSaved = true - os2lDevice.isDetected = true - currentPeriph[os2lDevice.SerialNumber] = os2lDevice - return {...currentPeriph} - }) - $needProjectSave = true - generateToast('info', 'bx-signal-5', $_("os2lPeripheralCreatedToast")) - }).catch(error => { - console.log("Unable to add the OS2L peripheral: " + error) - generateToast('danger', 'bx-error', $_("os2lPeripheralCreateErrorToast")) - }) - } - // Select the peripheral to edit its settings let selectedPeripheralSN = null let selectedPeripheralSettings = {} @@ -179,7 +123,7 @@ {/if} {/each}

{$_("projectHardwareOthersLabel")}

- + addPeripheral({Name: "OS2L connection", ProtocolName: "OS2L"})} text="Add an OS2L peripheral" icon="bx-plus-circle" tooltip="Configure an OS2L connection"/> diff --git a/hardware/FTDIFinder.go b/hardware/FTDIFinder.go index 4d1fc29..075fa92 100644 --- a/hardware/FTDIFinder.go +++ b/hardware/FTDIFinder.go @@ -24,22 +24,48 @@ const ( // FTDIFinder represents how the protocol is defined type FTDIFinder struct { - findTicker time.Ticker // Peripherals find ticker - peripherals map[string]Peripheral // The list of peripherals handled by this finder - scanChannel chan struct{} // The channel to trigger a scan event - goWait sync.WaitGroup // Check goroutines execution + findTicker time.Ticker // Peripherals find ticker + foundPeripherals map[string]PeripheralInfo // The list of peripherals handled by this finder + registeredPeripherals map[string]FTDIPeripheral // The list of found peripherals + scanChannel chan struct{} // The channel to trigger a scan event + goWait sync.WaitGroup // Check goroutines execution } // NewFTDIFinder creates a new FTDI finder func NewFTDIFinder(findPeriod time.Duration) *FTDIFinder { log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder created") return &FTDIFinder{ - findTicker: *time.NewTicker(findPeriod), - peripherals: make(map[string]Peripheral), - scanChannel: make(chan struct{}), + findTicker: *time.NewTicker(findPeriod), + foundPeripherals: make(map[string]PeripheralInfo), + registeredPeripherals: make(map[string]FTDIPeripheral), + scanChannel: make(chan struct{}), } } +// RegisterPeripheral registers a new peripheral +func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) { + ftdiPeripheral, err := NewFTDIPeripheral(peripheralData) + if err != nil { + return "", fmt.Errorf("unable to create the FTDI peripheral: %v", err) + } + f.registeredPeripherals[peripheralData.SerialNumber] = *ftdiPeripheral + log.Trace().Any("periph", &ftdiPeripheral).Str("file", "FTDIFinder").Str("peripheralName", peripheralData.Name).Msg("FTDI peripheral has been created") + return peripheralData.SerialNumber, nil +} + +// UnregisterPeripheral unregisters an existing peripheral +func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { + peripheral, registered := f.registeredPeripherals[peripheralID] + if registered { + err := peripheral.Disconnect(ctx) + if err != nil { + return err + } + } + delete(f.registeredPeripherals, peripheralID) + return nil +} + //go:embed third-party/ftdi/detectFTDI.exe var finderExe []byte @@ -58,7 +84,7 @@ func (f *FTDIFinder) Initialize() error { if err != nil { return err } - createExecutable(ftdiSenderExecutableName, senderExe) + err = createExecutable(ftdiSenderExecutableName, senderExe) if err != nil { return err } @@ -144,16 +170,28 @@ func (f *FTDIFinder) GetName() string { return "FTDI" } -// GetPeripheral gets the peripheral that correspond to the specified ID -func (f *FTDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) { +// GetPeripheralSettings gets the peripheral settings +func (f *FTDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) { // Return the specified peripheral - peripheral := f.peripherals[peripheralID] - if peripheral == nil { + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") - return nil, false + return nil, fmt.Errorf("unable to found the peripheral") } log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") - return peripheral, true + return peripheral.GetSettings(), nil +} + +// SetPeripheralSettings sets the peripheral settings +func (f *FTDIFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error { + // Return the specified peripheral + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { + log.Error().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return fmt.Errorf("unable to found the peripheral") + } + log.Debug().Str("file", "FTDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.SetSettings(settings) } // scanPeripherals scans the FTDI peripherals @@ -163,8 +201,6 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { log.Trace().Str("file", "FTDIFinder").Msg("FTDI scan triggered") - ftdiPeripherals := make(map[string]Peripheral) - finder := exec.CommandContext(detectionCtx, filepath.Join(os.TempDir(), ftdiFinderExecutableName)) log.Trace().Str("file", "FTDIFinder").Msg("has executed the FIND executable") @@ -190,6 +226,8 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { return fmt.Errorf("unable to find FTDI peripherals: %s", scannerErr.Text()) } + temporaryPeripherals := make(map[string]PeripheralInfo) + scanner := bufio.NewScanner(stdout) for scanner.Scan() { peripheralString := scanner.Text() @@ -203,29 +241,27 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { log.Warn().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("no location provided for this FTDI peripheral") location = -1 } - // Add the peripheral to the temporary list - peripheral, err := NewFTDIPeripheral(peripheralInfo[2], peripheralInfo[1], location) - if err != nil { - return fmt.Errorf("unable to create the FTDI peripheral: %v", err) + // Add the peripheral info to the found list + temporaryPeripherals[peripheralInfo[1]] = PeripheralInfo{ + Name: peripheralInfo[2], + SerialNumber: peripheralInfo[1], + ProtocolName: "FTDI", } - log.Trace().Any("periph", &peripheral).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("has been created") - ftdiPeripherals[peripheralInfo[1]] = peripheral - log.Trace().Any("periph", ftdiPeripherals).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder") + // If this peripheral is already registered, connect it + peripheral, registered := f.registeredPeripherals[peripheralInfo[1]] + if registered { + err := peripheral.Connect(ctx, location) + if err != nil { + log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralInfo[1]).Msg("unable to connect the peripheral") + } + } + + log.Trace().Any("periph", temporaryPeripherals).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder") } // Emit the peripherals changes to the front - emitPeripheralsChanges(ctx, f.peripherals, ftdiPeripherals) + emitPeripheralsChanges(ctx, f.foundPeripherals, temporaryPeripherals) // Store the new peripherals list - f.peripherals = ftdiPeripherals - return nil -} - -// CreatePeripheral is not implemented here -func (f *FTDIFinder) CreatePeripheral(context.Context) (Peripheral, error) { - return nil, nil -} - -// DeletePeripheral is not implemented here -func (f *FTDIFinder) DeletePeripheral(serialNumber string) error { + f.foundPeripherals = temporaryPeripherals return nil } diff --git a/hardware/FTDIPeripheral.go b/hardware/FTDIPeripheral.go index a005cc9..7e03e57 100644 --- a/hardware/FTDIPeripheral.go +++ b/hardware/FTDIPeripheral.go @@ -21,9 +21,7 @@ const ( // FTDIPeripheral contains the data of an FTDI peripheral type FTDIPeripheral struct { - name string // The name of the peripheral - serialNumber string // The S/N of the FTDI peripheral - location int // The location of the peripheral + info PeripheralInfo // The peripheral basic data programName string // The temp file name of the executable settings map[string]interface{} // The settings of the peripheral dmxSender *exec.Cmd // The command to pilot the DMX sender program @@ -35,14 +33,12 @@ type FTDIPeripheral struct { } // NewFTDIPeripheral creates a new FTDI peripheral -func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPeripheral, error) { - log.Info().Str("file", "FTDIPeripheral").Str("name", name).Str("s/n", serialNumber).Int("location", location).Msg("FTDI peripheral created") +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{ - name: name, + info: info, dmxSender: nil, - serialNumber: serialNumber, - location: location, settings: settings, disconnectChan: make(chan struct{}), errorsChan: make(chan error, 1), @@ -50,32 +46,32 @@ func NewFTDIPeripheral(name string, serialNumber string, location int) (*FTDIPer } // Connect connects the FTDI peripheral -func (p *FTDIPeripheral) Connect(ctx context.Context) error { +func (p *FTDIPeripheral) Connect(ctx context.Context, location int) error { // Connect if no connection is already running - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("connecting FTDI peripheral...") + log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("connecting FTDI peripheral...") // Check if the connection has already been established if p.dmxSender != nil { - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender already initialized") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender already initialized") return nil } // Initialize the exec.Command for running the process - p.dmxSender = exec.Command(p.programName, fmt.Sprintf("%d", p.location)) - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender instance created") + p.dmxSender = exec.Command(p.programName, fmt.Sprintf("%d", location)) + log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender instance created") // Create the pipes for stdin, stdout, and stderr asynchronously without blocking var err error if p.stdout, err = p.dmxSender.StdoutPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stdout pipe") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stdout pipe") return fmt.Errorf("unable to create stdout pipe: %v", err) } if p.stdin, err = p.dmxSender.StdinPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stdin pipe") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stdin pipe") return fmt.Errorf("unable to create stdin pipe: %v", err) } if p.stderr, err = p.dmxSender.StderrPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to create stderr pipe") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stderr pipe") return fmt.Errorf("unable to create stderr pipe: %v", err) } @@ -84,10 +80,10 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error { scanner := bufio.NewScanner(p.stderr) for scanner.Scan() { // Process each line read from stderr - log.Err(fmt.Errorf(scanner.Text())).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error detected in dmx sender") + log.Err(fmt.Errorf(scanner.Text())).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error detected in dmx sender") } if err := scanner.Err(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error reading from stderr") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error reading from stderr") } }() @@ -98,33 +94,33 @@ func (p *FTDIPeripheral) Connect(ctx context.Context) error { select { case <-ctx.Done(): // If the context is canceled, handle it gracefully - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender was canceled by context") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender was canceled by context") return default: // Handle command exit normally if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while execution of dmx sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while execution of dmx sender") if exitError, ok := err.(*exec.ExitError); ok { - log.Warn().Str("file", "FTDIPeripheral").Int("exitCode", exitError.ExitCode()).Str("s/n", p.serialNumber).Msg("dmx sender exited with code") + log.Warn().Str("file", "FTDIPeripheral").Int("exitCode", exitError.ExitCode()).Str("s/n", p.info.SerialNumber).Msg("dmx sender exited with code") } } else { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmx sender exited successfully") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmx sender exited successfully") } } }() - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxSender process started successfully") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender process started successfully") return nil } // Disconnect disconnects the FTDI peripheral func (p *FTDIPeripheral) Disconnect(ctx context.Context) error { - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("disconnecting FTDI peripheral...") + log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("disconnecting FTDI peripheral...") if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") _, err := io.WriteString(p.stdin, string([]byte{0x04, 0x00, 0x00, 0x00})) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to disconnect: %v", err) } p.stdin.Close() @@ -132,47 +128,47 @@ func (p *FTDIPeripheral) Disconnect(ctx context.Context) error { p.dmxSender = nil err = os.Remove(p.programName) if err != nil { - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Str("senderPath", p.programName).Msg("unable to delete the dmx sender temporary file") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Str("senderPath", p.programName).Msg("unable to delete the dmx sender temporary file") return fmt.Errorf("unable to delete the temporary file: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while disconnecting: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while disconnecting: not connected") return fmt.Errorf("unable to disconnect: not connected") } // Activate activates the FTDI peripheral func (p *FTDIPeripheral) Activate(ctx context.Context) error { if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") _, err := io.WriteString(p.stdin, string([]byte{0x01, 0x00, 0x00, 0x00})) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to activate: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while activating: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while activating: not connected") return fmt.Errorf("unable to activate: not connected") } // Deactivate deactivates the FTDI peripheral func (p *FTDIPeripheral) Deactivate(ctx context.Context) error { if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") _, err := io.WriteString(p.stdin, string([]byte{0x02, 0x00, 0x00, 0x00})) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to deactivate: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while deactivating: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while deactivating: not connected") return fmt.Errorf("unable to deactivate: not connected") } -// SetPeripheralSettings sets a specific setting for this peripheral -func (p *FTDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { +// SetSettings sets a specific setting for this peripheral +func (p *FTDIPeripheral) SetSettings(settings map[string]interface{}) error { p.settings = settings return nil } @@ -180,16 +176,16 @@ func (p *FTDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) // SetDeviceProperty sends a command to the specified device func (p *FTDIPeripheral) SetDeviceProperty(ctx context.Context, uint32, channelNumber uint32, channelValue byte) error { if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("dmxsender is defined for this FTDI") + log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") commandString := []byte{0x03, 0x01, 0x00, 0xff, 0x03, 0x02, 0x00, channelValue} _, err := io.WriteString(p.stdin, string(commandString)) if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("unable to write command to sender") + log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") return fmt.Errorf("unable to set device property: %v", err) } return nil } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.serialNumber).Msg("error while setting device property: not connected") + log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while setting device property: not connected") return fmt.Errorf("unable to set device property: not connected") } @@ -200,9 +196,5 @@ func (p *FTDIPeripheral) GetSettings() map[string]interface{} { // GetInfo gets all the peripheral information func (p *FTDIPeripheral) GetInfo() PeripheralInfo { - return PeripheralInfo{ - Name: p.name, - SerialNumber: p.serialNumber, - ProtocolName: "FTDI", - } + return p.info } diff --git a/hardware/MIDIFinder.go b/hardware/MIDIFinder.go index 4d7a65e..ecc4100 100644 --- a/hardware/MIDIFinder.go +++ b/hardware/MIDIFinder.go @@ -15,18 +15,18 @@ import ( // MIDIFinder represents how the protocol is defined type MIDIFinder struct { - findTicker time.Ticker // Peripherals find ticker - peripherals map[string]Peripheral // The list of peripherals - scanChannel chan struct{} // The channel to trigger a scan event - goWait sync.WaitGroup // Check goroutines execution + findTicker time.Ticker // Peripherals find ticker + registeredPeripherals map[string]MIDIPeripheral // The list of peripherals + scanChannel chan struct{} // The channel to trigger a scan event + goWait sync.WaitGroup // Check goroutines execution } // NewMIDIFinder creates a new DMXUSB protocol func NewMIDIFinder(findPeriod time.Duration) *MIDIFinder { log.Trace().Str("file", "MIDIFinder").Msg("MIDI finder created") return &MIDIFinder{ - findTicker: *time.NewTicker(findPeriod), - peripherals: make(map[string]Peripheral), + findTicker: *time.NewTicker(findPeriod), + registeredPeripherals: make(map[string]MIDIPeripheral), } } @@ -36,6 +36,30 @@ func (f *MIDIFinder) Initialize() error { return nil } +// RegisterPeripheral registers a new peripheral +func (f *MIDIFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) { + peripheral, err := NewMIDIPeripheral(peripheralData) + if err != nil { + return "", fmt.Errorf("unable to create the MIDI peripheral: %v", err) + } + f.registeredPeripherals[peripheralData.SerialNumber] = *peripheral + log.Trace().Any("periph", &peripheral).Str("file", "MIDIFinder").Str("peripheralName", peripheralData.Name).Msg("FTDI peripheral has been created") + return peripheralData.SerialNumber, nil +} + +// UnregisterPeripheral unregisters an existing peripheral +func (f *MIDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { + peripheral, registered := f.registeredPeripherals[peripheralID] + if registered { + err := peripheral.Disconnect(ctx) + if err != nil { + return err + } + } + delete(f.registeredPeripherals, peripheralID) + return nil +} + // Start starts the finder and search for peripherals func (f *MIDIFinder) Start(ctx context.Context) error { f.goWait.Add(1) @@ -79,16 +103,28 @@ func (f *MIDIFinder) GetName() string { return "MIDI" } -// GetPeripheral gets the peripheral that correspond to the specified ID -func (f *MIDIFinder) GetPeripheral(peripheralID string) (Peripheral, bool) { +// GetPeripheralSettings gets the peripheral settings +func (f *MIDIFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) { // Return the specified peripheral - peripheral, found := f.peripherals[peripheralID] + peripheral, found := f.registeredPeripherals[peripheralID] if !found { - log.Error().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the MIDI finder") - return nil, false + log.Error().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return nil, fmt.Errorf("unable to found the peripheral") } - log.Trace().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("MIDI peripheral found in the driver") - return peripheral, true + log.Debug().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.GetSettings(), nil +} + +// SetPeripheralSettings sets the peripheral settings +func (f *MIDIFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error { + // Return the specified peripheral + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { + log.Error().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return fmt.Errorf("unable to found the peripheral") + } + log.Debug().Str("file", "MIDIFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.SetSettings(settings) } func splitStringAndNumber(input string) (string, int, error) { @@ -119,7 +155,7 @@ func (f *MIDIFinder) ForceScan() { // scanPeripherals scans the MIDI peripherals func (f *MIDIFinder) scanPeripherals(ctx context.Context) error { - midiPeripherals := make(map[string]Peripheral) + // midiPeripherals := make(map[string]Peripheral) log.Trace().Str("file", "MIDIFinder").Msg("opening MIDI scanner port...") midiScanner, err := rtmidi.NewMIDIInDefault() if err != nil { @@ -148,8 +184,8 @@ func (f *MIDIFinder) scanPeripherals(ctx context.Context) error { } log.Info().Str("file", "MIDIFinder").Str("name", name).Int("location", location).Msg("MIDI peripheral found") // Add the peripheral to the temporary list - sn := strings.ToLower(strings.Replace(name, " ", "_", -1)) - midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn) + // sn := strings.ToLower(strings.Replace(name, " ", "_", -1)) + // midiPeripherals[sn] = NewMIDIPeripheral(name, location, sn) } // Compare with the current peripherals to detect arrivals/removals // removedList, addedList := comparePeripherals(f.peripherals, midiPeripherals) @@ -159,7 +195,7 @@ func (f *MIDIFinder) scanPeripherals(ctx context.Context) error { // emitPeripheralsEvents(ctx, addedList, PeripheralArrival) log.Info().Str("file", "MIDIFinder").Msg("MIDI add list emitted to the front") // Store the new peripherals list - f.peripherals = midiPeripherals + // f.peripherals = midiPeripherals return nil } diff --git a/hardware/MIDIPeripheral.go b/hardware/MIDIPeripheral.go index e3083ba..a143b64 100644 --- a/hardware/MIDIPeripheral.go +++ b/hardware/MIDIPeripheral.go @@ -8,22 +8,18 @@ import ( // MIDIPeripheral contains the data of a MIDI peripheral type MIDIPeripheral struct { - name string // The name of the peripheral - location int // The location of the peripheral - serialNumber string // The S/N of the peripheral - settings map[string]interface{} // The settings of the peripheral + info PeripheralInfo // The peripheral info + location int // The location of the peripheral + settings map[string]interface{} // The settings of the peripheral } // NewMIDIPeripheral creates a new MIDI peripheral -func NewMIDIPeripheral(name string, location int, serialNumber string) *MIDIPeripheral { - log.Trace().Str("file", "MIDIPeripheral").Str("name", name).Str("s/n", serialNumber).Int("location", location).Msg("MIDI peripheral created") - settings := make(map[string]interface{}) +func NewMIDIPeripheral(peripheralData PeripheralInfo) (*MIDIPeripheral, error) { + log.Trace().Str("file", "MIDIPeripheral").Str("name", peripheralData.Name).Str("s/n", peripheralData.SerialNumber).Msg("MIDI peripheral created") return &MIDIPeripheral{ - name: name, - location: location, - serialNumber: serialNumber, - settings: settings, - } + info: peripheralData, + settings: peripheralData.Settings, + }, nil } // Connect connects the MIDI peripheral @@ -46,8 +42,8 @@ func (p *MIDIPeripheral) Deactivate(ctx context.Context) error { return nil } -// SetPeripheralSettings sets a specific setting for this peripheral -func (p *MIDIPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { +// SetSettings sets a specific setting for this peripheral +func (p *MIDIPeripheral) SetSettings(settings map[string]interface{}) error { p.settings = settings return nil } @@ -64,9 +60,5 @@ func (p *MIDIPeripheral) GetSettings() map[string]interface{} { // GetInfo gets the peripheral information func (p *MIDIPeripheral) GetInfo() PeripheralInfo { - return PeripheralInfo{ - Name: p.name, - ProtocolName: "MIDI", - SerialNumber: p.serialNumber, - } + return p.info } diff --git a/hardware/OS2LFinder.go b/hardware/OS2LFinder.go index 33fae6b..2acdb8e 100644 --- a/hardware/OS2LFinder.go +++ b/hardware/OS2LFinder.go @@ -11,14 +11,14 @@ import ( // OS2LFinder represents how the protocol is defined type OS2LFinder struct { - peripherals map[string]Peripheral // The list of peripherals + registeredPeripherals map[string]OS2LPeripheral // The list of found peripherals } // NewOS2LFinder creates a new OS2L finder func NewOS2LFinder() *OS2LFinder { log.Trace().Str("file", "OS2LFinder").Msg("OS2L finder created") return &OS2LFinder{ - peripherals: make(map[string]Peripheral), + registeredPeripherals: make(map[string]OS2LPeripheral), } } @@ -28,21 +28,36 @@ func (f *OS2LFinder) Initialize() error { return nil } -// CreatePeripheral creates a new OS2L peripheral -func (f *OS2LFinder) CreatePeripheral(ctx context.Context) (Peripheral, error) { +// RegisterPeripheral registers a new peripheral +func (f *OS2LFinder) RegisterPeripheral(ctx context.Context, peripheralData PeripheralInfo) (string, error) { // Create a random serial number for this peripheral - randomSerialNumber := strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32))) - log.Trace().Str("file", "OS2LFinder").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created") - peripheral := NewOS2LPeripheral("OS2L", randomSerialNumber) - f.peripherals[randomSerialNumber] = peripheral - log.Info().Str("file", "OS2LFinder").Str("serialNumber", randomSerialNumber).Msg("OS2L peripheral created and registered") - return peripheral, nil + peripheralData.SerialNumber = strings.ToUpper(fmt.Sprintf("%08x", rand.Intn(1<<32))) + log.Trace().Str("file", "OS2LFinder").Str("serialNumber", peripheralData.SerialNumber).Msg("OS2L peripheral created") + + os2lPeripheral, err := NewOS2LPeripheral(peripheralData) + if err != nil { + return "", fmt.Errorf("unable to create the OS2L peripheral: %v", err) + } + + f.registeredPeripherals[peripheralData.SerialNumber] = *os2lPeripheral + log.Trace().Any("periph", &os2lPeripheral).Str("file", "OS2LFinder").Str("peripheralName", peripheralData.Name).Msg("OS2L peripheral has been created") + // Send the change to the front + // runtime.EventsEmit(ctx, string(PeripheralArrival), peripheralData) + return peripheralData.SerialNumber, nil } -// DeletePeripheral removes an OS2L peripheral -func (f *OS2LFinder) DeletePeripheral(serialNumber string) error { - delete(f.peripherals, serialNumber) - log.Info().Str("file", "OS2LFinder").Str("serialNumber", serialNumber).Msg("OS2L peripheral removed") +// UnregisterPeripheral unregisters an existing peripheral +func (f *OS2LFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { + peripheral, registered := f.registeredPeripherals[peripheralID] + if registered { + err := peripheral.Disconnect(ctx) + if err != nil { + return err + } + } + delete(f.registeredPeripherals, peripheralID) + // Send the change to the front + // runtime.EventsEmit(ctx, string(PeripheralRemoval), peripheral.GetInfo()) return nil } @@ -51,16 +66,28 @@ func (f *OS2LFinder) GetName() string { return "OS2L" } -// GetPeripheral gets the peripheral that correspond to the specified ID -func (f *OS2LFinder) GetPeripheral(peripheralID string) (Peripheral, bool) { +// GetPeripheralSettings gets the peripheral settings +func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (map[string]interface{}, error) { // Return the specified peripheral - peripheral, found := f.peripherals[peripheralID] + peripheral, found := f.registeredPeripherals[peripheralID] if !found { - log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral in the OS2L finder") - return nil, false + log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return nil, fmt.Errorf("unable to found the peripheral") } - log.Trace().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("OS2L peripheral found in the finder") - return peripheral, true + log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.GetSettings(), nil +} + +// SetPeripheralSettings sets the peripheral settings +func (f *OS2LFinder) SetPeripheralSettings(peripheralID string, settings map[string]interface{}) error { + // Return the specified peripheral + peripheral, found := f.registeredPeripherals[peripheralID] + if !found { + log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + return fmt.Errorf("unable to found the peripheral") + } + log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + return peripheral.SetSettings(settings) } // Start starts the finder diff --git a/hardware/OS2LPeripheral.go b/hardware/OS2LPeripheral.go index e6845e5..6e12008 100644 --- a/hardware/OS2LPeripheral.go +++ b/hardware/OS2LPeripheral.go @@ -9,21 +9,19 @@ import ( // OS2LPeripheral contains the data of an OS2L peripheral type OS2LPeripheral struct { - name string // The name of the peripheral - serialNumber string // The serial number of the peripheral - serverIP string // OS2L server IP - serverPort int // OS2L server port + info PeripheralInfo // The basic info for this peripheral + serverIP string // OS2L server IP + serverPort int // OS2L server port } // NewOS2LPeripheral creates a new OS2L peripheral -func NewOS2LPeripheral(name string, serialNumber string) *OS2LPeripheral { - log.Trace().Str("file", "OS2LPeripheral").Str("name", name).Str("s/n", serialNumber).Msg("OS2L peripheral created") +func NewOS2LPeripheral(peripheralData PeripheralInfo) (*OS2LPeripheral, error) { + log.Trace().Str("file", "OS2LPeripheral").Str("name", peripheralData.Name).Str("s/n", peripheralData.SerialNumber).Msg("OS2L peripheral created") return &OS2LPeripheral{ - name: name, - serverIP: "127.0.0.1", - serverPort: 9995, - serialNumber: serialNumber, - } + info: peripheralData, + serverIP: "127.0.0.1", + serverPort: 9005, + }, nil } // Connect connects the MIDI peripheral @@ -50,8 +48,8 @@ func (p *OS2LPeripheral) Deactivate(ctx context.Context) error { return nil } -// SetPeripheralSettings sets a specific setting for this peripheral -func (p *OS2LPeripheral) SetPeripheralSettings(settings map[string]interface{}) error { +// SetSettings sets a specific setting for this peripheral +func (p *OS2LPeripheral) SetSettings(settings map[string]interface{}) error { // Check if the IP exists serverIP, found := settings["os2lIp"] if !found { @@ -95,9 +93,5 @@ func (p *OS2LPeripheral) GetSettings() map[string]interface{} { // GetInfo gets the peripheral information func (p *OS2LPeripheral) GetInfo() PeripheralInfo { - return PeripheralInfo{ - Name: p.name, - SerialNumber: p.serialNumber, - ProtocolName: "OS2L", - } + return p.info } diff --git a/hardware/hardware.go b/hardware/hardware.go index f7aeabf..1409567 100644 --- a/hardware/hardware.go +++ b/hardware/hardware.go @@ -93,19 +93,19 @@ func (h *HardwareManager) RegisterFinder(finder PeripheralFinder) { log.Info().Str("file", "hardware").Str("finderName", finder.GetName()).Msg("finder registered") } -// GetPeripheral gets the peripheral object from the parent finder -func (h *HardwareManager) GetPeripheral(finderName string, peripheralID string) (Peripheral, bool) { - // Get the finder - parentFinder, found := h.finders[finderName] - // If no finder found, return false - if !found { - log.Error().Str("file", "hardware").Str("finderName", finderName).Msg("unable to get the finder") - return nil, false - } - log.Trace().Str("file", "hardware").Str("finderName", parentFinder.GetName()).Msg("finder got") - // Contact the finder to get the peripheral - return parentFinder.GetPeripheral(peripheralID) -} +// // GetPeripheral gets the peripheral object from the parent finder +// func (h *HardwareManager) GetPeripheral(finderName string, peripheralID string) (Peripheral, bool) { +// // Get the finder +// parentFinder, found := h.finders[finderName] +// // If no finder found, return false +// if !found { +// log.Error().Str("file", "hardware").Str("finderName", finderName).Msg("unable to get the finder") +// return nil, false +// } +// log.Trace().Str("file", "hardware").Str("finderName", parentFinder.GetName()).Msg("finder got") +// // Contact the finder to get the peripheral +// return parentFinder.GetPeripheral(peripheralID) +// } // Scan scans all the peripherals for the registered finders func (h *HardwareManager) Scan() error { @@ -130,13 +130,13 @@ func (h *HardwareManager) Stop() error { } // emitPeripheralsChanges compares the old and new peripherals to determine which ones have been added or removed. -func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]Peripheral, newPeripherals map[string]Peripheral) { +func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]PeripheralInfo, newPeripherals map[string]PeripheralInfo) { log.Trace().Any("oldList", oldPeripherals).Any("newList", newPeripherals).Msg("emitting peripherals changes to the front") // Identify removed peripherals: present in the old list but not in the new list for oldPeriphName := range oldPeripherals { if _, exists := newPeripherals[oldPeriphName]; !exists { - runtime.EventsEmit(ctx, string(PeripheralRemoval), oldPeripherals[oldPeriphName].GetInfo()) + runtime.EventsEmit(ctx, string(PeripheralRemoval), oldPeripherals[oldPeriphName]) log.Trace().Str("file", "hardware").Str("event", string(PeripheralRemoval)).Msg("emit peripheral removal event") } } @@ -144,7 +144,7 @@ func emitPeripheralsChanges(ctx context.Context, oldPeripherals map[string]Perip // Identify added peripherals: present in the new list but not in the old list for newPeriphName := range newPeripherals { if _, exists := oldPeripherals[newPeriphName]; !exists { - runtime.EventsEmit(ctx, string(PeripheralArrival), newPeripherals[newPeriphName].GetInfo()) + runtime.EventsEmit(ctx, string(PeripheralArrival), newPeripherals[newPeriphName]) log.Trace().Str("file", "hardware").Str("event", string(PeripheralArrival)).Msg("emit peripheral arrival event") } } diff --git a/hardware/interfaces.go b/hardware/interfaces.go index 213c3e3..2041002 100644 --- a/hardware/interfaces.go +++ b/hardware/interfaces.go @@ -8,7 +8,7 @@ type Peripheral interface { Disconnect(context.Context) error // Disconnect the peripheral Activate(context.Context) error // Activate the peripheral Deactivate(context.Context) error // Deactivate the peripheral - SetPeripheralSettings(map[string]interface{}) error // Set a peripheral setting + SetSettings(map[string]interface{}) error // Set a peripheral setting SetDeviceProperty(context.Context, uint32, uint32, byte) error // Update a device property GetInfo() PeripheralInfo // Get the peripheral information @@ -25,12 +25,13 @@ type PeripheralInfo struct { // PeripheralFinder represents how compatible peripheral drivers are implemented type PeripheralFinder interface { - Initialize() error // Initializes the protocol - Start(context.Context) error // Start the detection - Stop() error // Stop the detection - ForceScan() // Explicitly scans for peripherals - CreatePeripheral(ctx context.Context) (Peripheral, error) // Creates a new peripheral - DeletePeripheral(serialNumber string) error // Removes a peripheral - GetName() string // Get the name of the finder - GetPeripheral(string) (Peripheral, bool) // Get the peripheral + Initialize() error // Initializes the protocol + Start(context.Context) error // Start the detection + Stop() error // Stop the detection + ForceScan() // Explicitly scans for peripherals + RegisterPeripheral(context.Context, PeripheralInfo) (string, error) // Registers a new peripheral data + UnregisterPeripheral(context.Context, string) error // Unregisters an existing peripheral + GetPeripheralSettings(string) (map[string]interface{}, error) // Gets the peripheral settings + SetPeripheralSettings(string, map[string]interface{}) error // Sets the peripheral settings + GetName() string // Get the name of the finder } diff --git a/peripherals.go b/peripherals.go index d98ea46..9120603 100644 --- a/peripherals.go +++ b/peripherals.go @@ -8,41 +8,48 @@ import ( ) // AddPeripheral adds a peripheral to the project -func (a *App) AddPeripheral(protocolName string, peripheralID string) error { +func (a *App) AddPeripheral(peripheralData hardware.PeripheralInfo) (string, error) { // Get the peripheral from its finder - p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID) - if !found { - log.Error().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("unable to found the specified peripheral") - return fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) + f, err := a.hardwareManager.GetFinder(peripheralData.ProtocolName) + if err != nil { + log.Error().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Msg("unable to found the specified finder") + return "", fmt.Errorf("unable to found the peripheral ID '%s'", peripheralData.SerialNumber) } + // Register this new peripheral + serialNumber, err := f.RegisterPeripheral(a.ctx, peripheralData) + if err != nil { + log.Trace().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Str("periphID", serialNumber).Msg("device registered to the finder") + return "", fmt.Errorf("unable to register the peripheral '%s'", serialNumber) + } + // Rewrite the serialnumber for virtual devices + peripheralData.SerialNumber = serialNumber + // Add the peripheral ID to the project if a.projectInfo.PeripheralsInfo == nil { a.projectInfo.PeripheralsInfo = make(map[string]hardware.PeripheralInfo) } - a.projectInfo.PeripheralsInfo[peripheralID] = p.GetInfo() - log.Info().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("peripheral added to project") - // TODO: Connect the peripheral - return nil + a.projectInfo.PeripheralsInfo[peripheralData.SerialNumber] = peripheralData + log.Info().Str("file", "peripheral").Str("protocolName", peripheralData.ProtocolName).Str("periphID", peripheralData.SerialNumber).Msg("peripheral added to project") + return peripheralData.SerialNumber, nil } // GetPeripheralSettings gets the peripheral settings func (a *App) GetPeripheralSettings(protocolName, peripheralID string) (map[string]interface{}, error) { // Get the peripheral from its finder - p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID) - if !found { + f, err := a.hardwareManager.GetFinder(protocolName) + if err != nil { log.Error().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("unable to found the specified peripheral") return nil, fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) } - // Return the peripheral settings - return p.GetSettings(), nil + return f.GetPeripheralSettings(peripheralID) } // UpdatePeripheralSettings updates a specific setting of a peripheral func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settings map[string]interface{}) error { - // Get the peripheral from its finder - p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID) - if !found { + // Sets the settings with the finder + f, err := a.hardwareManager.GetFinder(protocolName) + if err != nil { log.Error().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("unable to found the specified peripheral") return fmt.Errorf("unable to found the peripheral ID '%s'", peripheralID) } @@ -54,104 +61,91 @@ func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settin pInfo.Settings = settings a.projectInfo.PeripheralsInfo[peripheralID] = pInfo // Apply changes in the peripheral - return p.SetPeripheralSettings(pInfo.Settings) + return f.SetPeripheralSettings(peripheralID, pInfo.Settings) } // RemovePeripheral adds a peripheral to the project func (a *App) RemovePeripheral(protocolName string, peripheralID string) error { - // TODO: Disconnect the peripheral + // Unregister the peripheral from the finder + f, err := a.hardwareManager.GetFinder(protocolName) + if err != nil { + log.Err(err).Str("file", "peripherals").Str("protocolName", protocolName).Msg("unable to find the finder") + return fmt.Errorf("unable to find the finder") + } + err = f.UnregisterPeripheral(a.ctx, peripheralID) + if err != nil { + log.Err(err).Str("file", "peripherals").Str("peripheralID", peripheralID).Msg("unable to unregister this peripheral") + return fmt.Errorf("unable to unregister this peripheral") + } // Remove the peripheral ID from the project delete(a.projectInfo.PeripheralsInfo, peripheralID) log.Info().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("peripheral removed from project") return nil } -// AddOS2LPeripheral adds a new OS2L peripheral -func (a *App) AddOS2LPeripheral() (hardware.PeripheralInfo, error) { - // Get the OS2L finder - os2lDriver, err := a.hardwareManager.GetFinder("OS2L") - if err != nil { - log.Err(err).Str("file", "peripheral").Msg("unable to found the OS2L driver") - return hardware.PeripheralInfo{}, err - } - log.Trace().Str("file", "peripheral").Msg("OS2L driver got") - - // Create a new OS2L peripheral with this finder - os2lPeripheral, err := os2lDriver.CreatePeripheral(a.ctx) - if err != nil { - log.Err(err).Str("file", "peripheral").Msg("unable to create the OS2L peripheral") - return hardware.PeripheralInfo{}, err - } - - os2lInfo := os2lPeripheral.GetInfo() - log.Info().Str("file", "peripheral").Str("s/n", os2lInfo.SerialNumber).Msg("OS2L peripheral created, adding to project") - // Add this new peripheral to the project - return os2lInfo, a.AddPeripheral(os2lDriver.GetName(), os2lInfo.SerialNumber) -} - // FOR TESTING PURPOSE ONLY -func (a *App) ConnectFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Connect(a.ctx) -} +// func (a *App) ConnectFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Connect(a.ctx) +// } -func (a *App) ActivateFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Activate(a.ctx) -} +// func (a *App) ActivateFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Activate(a.ctx) +// } -func (a *App) SetDeviceFTDI(channelValue byte) error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.SetDeviceProperty(a.ctx, 0, 0, channelValue) -} +// func (a *App) SetDeviceFTDI(channelValue byte) error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.SetDeviceProperty(a.ctx, 0, 0, channelValue) +// } -func (a *App) DeactivateFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Deactivate(a.ctx) -} +// func (a *App) DeactivateFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Deactivate(a.ctx) +// } -func (a *App) DisconnectFTDI() error { - // Connect the FTDI - driver, err := a.hardwareManager.GetFinder("FTDI") - if err != nil { - return err - } - periph, found := driver.GetPeripheral("A50285BI") - if !found { - return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") - } - return periph.Disconnect(a.ctx) -} +// func (a *App) DisconnectFTDI() error { +// // Connect the FTDI +// driver, err := a.hardwareManager.GetFinder("FTDI") +// if err != nil { +// return err +// } +// periph, found := driver.GetPeripheral("A50285BI") +// if !found { +// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") +// } +// return periph.Disconnect(a.ctx) +// } From 0db468bfef0cfb006609d9bfa9e0bbca2ae07d4e Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sun, 26 Jan 2025 15:11:05 +0100 Subject: [PATCH 2/7] add a waiting status on device cards --- .../src/components/Settings/DeviceCard.svelte | 60 ++++++++++++++----- .../Settings/InputsOutputsContent.svelte | 2 +- 2 files changed, 47 insertions(+), 15 deletions(-) diff --git a/frontend/src/components/Settings/DeviceCard.svelte b/frontend/src/components/Settings/DeviceCard.svelte index 85158e0..6a85854 100644 --- a/frontend/src/components/Settings/DeviceCard.svelte +++ b/frontend/src/components/Settings/DeviceCard.svelte @@ -16,6 +16,7 @@ export let signalized = false; export let disconnected = false; export let selected = false; + export let waiting = false; // Emit a delete event when the device is being removed const dispatch = createEventDispatcher(); @@ -37,8 +38,8 @@
-
-
+
+

{#if disconnected} {/if}{title}

{type} {location != '' ? "- " : ""}{location}
{#if disconnected} @@ -77,23 +78,12 @@ text-align: left; cursor: pointer; } - .unselected{ - background-color: var(--third-color); - background: fixed; - margin: 0.2em; - padding-left: 0.3em; - padding-bottom: 0.3em; - border-radius: 0.2em; - display: flex; - justify-content: space-between; - text-align: left; - cursor: pointer; - } .subtitle{ margin-bottom: 0.5em; } .actions { margin-left: 0.2em; + z-index: 2; } p{ margin: 0; @@ -102,4 +92,46 @@ margin: 0; font-weight: 1; } + + .waiting::before{ + content: ''; + position: absolute; + background: linear-gradient(var(--second-color), var(--first-color)); + width: 100%; + height: 60%; + animation: rotate 3s linear infinite; + } + + .waiting::after{ + content: ''; + position: absolute; + background: var(--second-color); + inset: 2px; + border-radius: 0.2em; + } + + .unselected{ + background-color: var(--third-color); + background: fixed; + position: relative; + margin: 0.2em; + padding-left: 0.3em; + padding-bottom: 0.3em; + border-radius: 0.2em; + display: flex; + justify-content: space-between; + text-align: left; + cursor: pointer; + overflow: hidden; + } + + /* Définition de l'animation */ + @keyframes rotate { + from { + transform: rotate(0deg); /* Début de la rotation */ + } + to { + transform: rotate(360deg); /* Fin de la rotation */ + } + } \ No newline at end of file diff --git a/frontend/src/components/Settings/InputsOutputsContent.svelte b/frontend/src/components/Settings/InputsOutputsContent.svelte index c629c44..02341a8 100644 --- a/frontend/src/components/Settings/InputsOutputsContent.svelte +++ b/frontend/src/components/Settings/InputsOutputsContent.svelte @@ -133,7 +133,7 @@ {#if savedPeripheralNumber > 0} {#each Object.entries($peripherals) as [serialNumber, peripheral]} {#if peripheral.isSaved} - removePeripheral(peripheral)} on:dblclick={() => removePeripheral(peripheral)} on:click={() => selectPeripheral(peripheral)} + removePeripheral(peripheral)} on:dblclick={() => removePeripheral(peripheral)} on:click={() => selectPeripheral(peripheral)} disconnected={!peripheral.isDetected} title={peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={peripheral.SerialNumber ? "S/N: " + peripheral.SerialNumber : ""} selected={serialNumber == selectedPeripheralSN} removable signalizable/> {/if} {/each} From bc15407cadd23dd9b85b788b5c775d98cb87ab0f Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sun, 31 Aug 2025 11:15:38 +0200 Subject: [PATCH 3/7] resolved disconnected hardware when it is detected --- app.go | 4 +-- frontend/src/App.svelte | 17 +++++++++- .../src/components/Settings/DeviceCard.svelte | 15 ++++----- .../Settings/InputsOutputsContent.svelte | 33 ++++++++++--------- frontend/src/lang/en.json | 1 + hardware/FTDIFinder.go | 16 +++++++-- hardware/FTDIPeripheral.go | 12 +++++-- hardware/MIDIFinder.go | 4 +-- hardware/MIDIPeripheral.go | 2 +- hardware/OS2LFinder.go | 17 +++++----- hardware/OS2LPeripheral.go | 9 ++++- hardware/hardware.go | 2 ++ hardware/interfaces.go | 14 ++++---- peripherals.go | 30 ++--------------- 14 files changed, 96 insertions(+), 80 deletions(-) diff --git a/app.go b/app.go index 36d1024..f55e03f 100644 --- a/app.go +++ b/app.go @@ -28,9 +28,9 @@ type App struct { func NewApp() *App { // Create a new hadware manager hardwareManager := hardware.NewHardwareManager() - hardwareManager.RegisterFinder(hardware.NewMIDIFinder(5 * time.Second)) + // hardwareManager.RegisterFinder(hardware.NewMIDIFinder(5 * time.Second)) hardwareManager.RegisterFinder(hardware.NewFTDIFinder(5 * time.Second)) - hardwareManager.RegisterFinder(hardware.NewOS2LFinder()) + // hardwareManager.RegisterFinder(hardware.NewOS2LFinder()) return &App{ hardwareManager: hardwareManager, projectSave: "", diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 032ecde..0b54732 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -54,9 +54,24 @@ generateToast('warning', 'bxs-hdd', $_("peripheralRemovalToast") + ' ' + peripheralInfo.Name + '') }) + // Handle the event when a peripheral status is updated + EventsOn('PERIPHERAL_STATUS', function(peripheral, status){ + console.log("Hardware status has been updated to " + status); + // When a peripheral status is updated, update it in the store + peripherals.update((storedPeripherals) => { + return { + ...storedPeripherals, + [peripheral.SerialNumber]: { + ...storedPeripherals[peripheral.SerialNumber], + isSaved: true, + Status: status, + }, + }}) + }) + // Set the window title $: { - WindowSetTitle("DMXConnect - " + $showInformation.Name + ($needProjectSave ? " (unsaved)" : "")) + WindowSetTitle("DMXConnect - " + $showInformation.Name + ($needProjectSave ? " (" + $_("unsavedProjectFlag") + ")" : "")) } let selectedMenu = "settings" diff --git a/frontend/src/components/Settings/DeviceCard.svelte b/frontend/src/components/Settings/DeviceCard.svelte index 6a85854..0c0a46e 100644 --- a/frontend/src/components/Settings/DeviceCard.svelte +++ b/frontend/src/components/Settings/DeviceCard.svelte @@ -14,9 +14,8 @@ export let addable = false; export let signalizable = false; export let signalized = false; - export let disconnected = false; export let selected = false; - export let waiting = false; + export let status = "disconnected"; // Emit a delete event when the device is being removed const dispatch = createEventDispatcher(); @@ -38,11 +37,11 @@
-
+
-

{#if disconnected} {/if}{title}

+

{#if status == "disconnected" } {/if}{title}

{type} {location != '' ? "- " : ""}{location}
- {#if disconnected} + {#if status == "disconnected"}
Disconnected
{:else}
{line1}
@@ -51,9 +50,9 @@
- - - + + +
diff --git a/frontend/src/components/Settings/InputsOutputsContent.svelte b/frontend/src/components/Settings/InputsOutputsContent.svelte index 02341a8..5b4d796 100644 --- a/frontend/src/components/Settings/InputsOutputsContent.svelte +++ b/frontend/src/components/Settings/InputsOutputsContent.svelte @@ -12,15 +12,17 @@ function addPeripheral(peripheral){ // Add the peripheral to the project (backend) AddPeripheral(peripheral).then((serialNumber) => { - peripheral.SerialNumber = serialNumber - peripherals.update((value) => { - // If the peripheral doesn't exists yet, create it - if (!(peripheral.SerialNumber in value)) { - value[peripheral.SerialNumber] = peripheral - } - value[peripheral.SerialNumber].isSaved = true; - return {...value} - }) + peripherals.update((storedPeripherals) => { + return { + ...storedPeripherals, + [serialNumber]: { + ...storedPeripherals[serialNumber], + Name: peripheral.Name, + ProtocolName: peripheral.ProtocolName, + SerialNumber: serialNumber, + isSaved: true, + }, + }}) $needProjectSave = true }).catch((error) => { console.log("Unable to add the peripheral to the project: " + error) @@ -62,15 +64,15 @@ let selectedPeripheralSettings = {} function selectPeripheral(peripheral){ // Load the settings array if the peripheral is detected - if (peripheral.isDetected){ + if (peripheral.isSaved){ GetPeripheralSettings(peripheral.ProtocolName, peripheral.SerialNumber).then((peripheralSettings) => { selectedPeripheralSettings = peripheralSettings + // Select the current peripheral + selectedPeripheralSN = peripheral.SerialNumber }).catch((error) => { console.log("Unable to get the peripheral settings: " + error) generateToast('danger', 'bx-error', $_("getPeripheralSettingsErrorToast")) }) - // Select the current peripheral - selectedPeripheralSN = peripheral.SerialNumber } } @@ -97,7 +99,6 @@ boolean: Boolean, }[typeof(selectedPeripheralSettings[settingName])] || (x => x) selectedPeripheralSettings[settingName] = convert(settingValue) - console.log(typeof(selectedPeripheralSettings[settingName])) let peripheralProtocolName = get(peripherals)[selectedPeripheralSN].ProtocolName UpdatePeripheralSettings(peripheralProtocolName, selectedPeripheralSN, selectedPeripheralSettings).then(()=> { $needProjectSave = true @@ -119,7 +120,7 @@ if(!peripheral.isSaved) addPeripheral(peripheral) }} - title={peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={"S/N: " + peripheral.SerialNumber} addable={!peripheral.isSaved}/> + status="connected" title={peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={"S/N: " + peripheral.SerialNumber} addable={!peripheral.isSaved}/> {/if} {/each}

{$_("projectHardwareOthersLabel")}

@@ -133,8 +134,8 @@ {#if savedPeripheralNumber > 0} {#each Object.entries($peripherals) as [serialNumber, peripheral]} {#if peripheral.isSaved} - removePeripheral(peripheral)} on:dblclick={() => removePeripheral(peripheral)} on:click={() => selectPeripheral(peripheral)} - disconnected={!peripheral.isDetected} title={peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={peripheral.SerialNumber ? "S/N: " + peripheral.SerialNumber : ""} selected={serialNumber == selectedPeripheralSN} removable signalizable/> + removePeripheral(peripheral)} on:dblclick={() => removePeripheral(peripheral)} on:click={() => selectPeripheral(peripheral)} + title={peripheral.Name} type={peripheral.ProtocolName} location={peripheral.Location ? peripheral.Location : ""} line1={peripheral.SerialNumber ? "S/N: " + peripheral.SerialNumber : ""} selected={serialNumber == selectedPeripheralSN} removable signalizable/> {/if} {/each} {:else} diff --git a/frontend/src/lang/en.json b/frontend/src/lang/en.json index 7e7de71..51e7be1 100644 --- a/frontend/src/lang/en.json +++ b/frontend/src/lang/en.json @@ -13,6 +13,7 @@ "newProjectTooltip": "Create a new project", "openProjectString": "Open", "openProjectTooltip": "Open an existing project", + "unsavedProjectFlag": "unsaved", "projectPropertiesTab": "Project properties", "projectPropertiesTooltip": "The project properties", "projectInputOutputTab": "Hardware", diff --git a/hardware/FTDIFinder.go b/hardware/FTDIFinder.go index 075fa92..e7a489c 100644 --- a/hardware/FTDIFinder.go +++ b/hardware/FTDIFinder.go @@ -15,6 +15,7 @@ import ( "time" "github.com/rs/zerolog/log" + "github.com/wailsapp/wails/v2/pkg/runtime" ) const ( @@ -54,10 +55,10 @@ func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData Peri } // UnregisterPeripheral unregisters an existing peripheral -func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { +func (f *FTDIFinder) UnregisterPeripheral(peripheralID string) error { peripheral, registered := f.registeredPeripherals[peripheralID] if registered { - err := peripheral.Disconnect(ctx) + err := peripheral.Disconnect() if err != nil { return err } @@ -248,13 +249,22 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { ProtocolName: "FTDI", } - // If this peripheral is already registered, connect it + // If this peripheral is already registered, connect it and activate it peripheral, registered := f.registeredPeripherals[peripheralInfo[1]] if registered { + runtime.EventsEmit(ctx, string(PeripheralStatus), peripheral.info, "connecting") + time.Sleep(2 * time.Second) err := peripheral.Connect(ctx, location) if err != nil { log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralInfo[1]).Msg("unable to connect the peripheral") } + runtime.EventsEmit(ctx, string(PeripheralStatus), peripheral.info, "deactivated") + time.Sleep(2 * time.Second) + err = peripheral.Activate(ctx) + if err != nil { + log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralInfo[1]).Msg("unable to activate the peripheral") + } + runtime.EventsEmit(ctx, string(PeripheralStatus), peripheral.info, "activated") } log.Trace().Any("periph", temporaryPeripherals).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder") diff --git a/hardware/FTDIPeripheral.go b/hardware/FTDIPeripheral.go index 7e03e57..7d218d8 100644 --- a/hardware/FTDIPeripheral.go +++ b/hardware/FTDIPeripheral.go @@ -6,6 +6,7 @@ import ( _ "embed" "fmt" "io" + "path/filepath" "github.com/rs/zerolog/log" @@ -37,6 +38,7 @@ 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{ + programName: filepath.Join(os.TempDir(), ftdiSenderExecutableName), info: info, dmxSender: nil, settings: settings, @@ -94,6 +96,10 @@ func (p *FTDIPeripheral) Connect(ctx context.Context, location int) error { select { case <-ctx.Done(): // If the context is canceled, handle it gracefully + err = p.Disconnect() + if err != nil { + log.Err(err).Str("file", "FTDIPeripheral").Msg("unable to disconnect the peripheral") + } log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender was canceled by context") return default: @@ -114,7 +120,7 @@ func (p *FTDIPeripheral) Connect(ctx context.Context, location int) error { } // Disconnect disconnects the FTDI peripheral -func (p *FTDIPeripheral) Disconnect(ctx context.Context) error { +func (p *FTDIPeripheral) Disconnect() error { log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("disconnecting FTDI peripheral...") if p.dmxSender != nil { log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") @@ -174,10 +180,10 @@ func (p *FTDIPeripheral) SetSettings(settings map[string]interface{}) error { } // SetDeviceProperty sends a command to the specified device -func (p *FTDIPeripheral) SetDeviceProperty(ctx context.Context, uint32, channelNumber uint32, channelValue byte) error { +func (p *FTDIPeripheral) SetDeviceProperty(ctx context.Context, channelNumber uint32, channelValue byte) error { if p.dmxSender != nil { log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") - commandString := []byte{0x03, 0x01, 0x00, 0xff, 0x03, 0x02, 0x00, channelValue} + commandString := []byte{0x03, byte(channelNumber & 0xFF), byte((channelNumber >> 8) & 0xFF), channelValue} _, err := io.WriteString(p.stdin, string(commandString)) if err != nil { log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") diff --git a/hardware/MIDIFinder.go b/hardware/MIDIFinder.go index ecc4100..4feb2d8 100644 --- a/hardware/MIDIFinder.go +++ b/hardware/MIDIFinder.go @@ -48,10 +48,10 @@ func (f *MIDIFinder) RegisterPeripheral(ctx context.Context, peripheralData Peri } // UnregisterPeripheral unregisters an existing peripheral -func (f *MIDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { +func (f *MIDIFinder) UnregisterPeripheral(peripheralID string) error { peripheral, registered := f.registeredPeripherals[peripheralID] if registered { - err := peripheral.Disconnect(ctx) + err := peripheral.Disconnect() if err != nil { return err } diff --git a/hardware/MIDIPeripheral.go b/hardware/MIDIPeripheral.go index a143b64..16aadef 100644 --- a/hardware/MIDIPeripheral.go +++ b/hardware/MIDIPeripheral.go @@ -28,7 +28,7 @@ func (p *MIDIPeripheral) Connect(ctx context.Context) error { } // Disconnect disconnects the MIDI peripheral -func (p *MIDIPeripheral) Disconnect(ctx context.Context) error { +func (p *MIDIPeripheral) Disconnect() error { return nil } diff --git a/hardware/OS2LFinder.go b/hardware/OS2LFinder.go index 2acdb8e..24a8d42 100644 --- a/hardware/OS2LFinder.go +++ b/hardware/OS2LFinder.go @@ -38,26 +38,27 @@ func (f *OS2LFinder) RegisterPeripheral(ctx context.Context, peripheralData Peri if err != nil { return "", fmt.Errorf("unable to create the OS2L peripheral: %v", err) } + // Connect this peripheral + err = os2lPeripheral.Connect(ctx) + if err != nil { + return "", err + } f.registeredPeripherals[peripheralData.SerialNumber] = *os2lPeripheral log.Trace().Any("periph", &os2lPeripheral).Str("file", "OS2LFinder").Str("peripheralName", peripheralData.Name).Msg("OS2L peripheral has been created") - // Send the change to the front - // runtime.EventsEmit(ctx, string(PeripheralArrival), peripheralData) return peripheralData.SerialNumber, nil } // UnregisterPeripheral unregisters an existing peripheral -func (f *OS2LFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { +func (f *OS2LFinder) UnregisterPeripheral(peripheralID string) error { peripheral, registered := f.registeredPeripherals[peripheralID] if registered { - err := peripheral.Disconnect(ctx) + err := peripheral.Disconnect() if err != nil { return err } } delete(f.registeredPeripherals, peripheralID) - // Send the change to the front - // runtime.EventsEmit(ctx, string(PeripheralRemoval), peripheral.GetInfo()) return nil } @@ -71,10 +72,10 @@ func (f *OS2LFinder) GetPeripheralSettings(peripheralID string) (map[string]inte // Return the specified peripheral peripheral, found := f.registeredPeripherals[peripheralID] if !found { - log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the FTDI finder") + log.Error().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("unable to get this peripheral from the OS2L finder") return nil, fmt.Errorf("unable to found the peripheral") } - log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the FTDI finder") + log.Debug().Str("file", "OS2LFinder").Str("peripheralID", peripheralID).Msg("peripheral found by the OS2L finder") return peripheral.GetSettings(), nil } diff --git a/hardware/OS2LPeripheral.go b/hardware/OS2LPeripheral.go index 6e12008..07e69a0 100644 --- a/hardware/OS2LPeripheral.go +++ b/hardware/OS2LPeripheral.go @@ -3,8 +3,10 @@ package hardware import ( "context" "fmt" + "time" "github.com/rs/zerolog/log" + "github.com/wailsapp/wails/v2/pkg/runtime" ) // OS2LPeripheral contains the data of an OS2L peripheral @@ -27,11 +29,16 @@ func NewOS2LPeripheral(peripheralData PeripheralInfo) (*OS2LPeripheral, error) { // Connect connects the MIDI peripheral func (p *OS2LPeripheral) Connect(ctx context.Context) error { log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral connected") + go func() { + runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "connecting") + time.Sleep(5 * time.Second) + runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "disconnected") + }() return nil } // Disconnect disconnects the MIDI peripheral -func (p *OS2LPeripheral) Disconnect(ctx context.Context) error { +func (p *OS2LPeripheral) Disconnect() error { log.Info().Str("file", "OS2LPeripheral").Msg("OS2L peripheral disconnected") return nil } diff --git a/hardware/hardware.go b/hardware/hardware.go index 1409567..5fc1af0 100644 --- a/hardware/hardware.go +++ b/hardware/hardware.go @@ -19,6 +19,8 @@ const ( PeripheralArrival PeripheralEvent = "PERIPHERAL_ARRIVAL" // PeripheralRemoval is triggered when a peripheral has been disconnected from the system PeripheralRemoval PeripheralEvent = "PERIPHERAL_REMOVAL" + // PeripheralStatus is triggered when a peripheral status has been updated (disconnected - connecting - connected) + PeripheralStatus PeripheralEvent = "PERIPHERAL_STATUS" // debounceDuration = 500 * time.Millisecond ) diff --git a/hardware/interfaces.go b/hardware/interfaces.go index 2041002..629df01 100644 --- a/hardware/interfaces.go +++ b/hardware/interfaces.go @@ -4,12 +4,12 @@ import "context" // Peripheral represents the methods used to manage a peripheral (input or output hardware) type Peripheral interface { - Connect(context.Context) error // Connect the peripheral - Disconnect(context.Context) error // Disconnect the peripheral - Activate(context.Context) error // Activate the peripheral - Deactivate(context.Context) error // Deactivate the peripheral - SetSettings(map[string]interface{}) error // Set a peripheral setting - SetDeviceProperty(context.Context, uint32, uint32, byte) error // Update a device property + Connect(context.Context) error // Connect the peripheral + Disconnect() error // Disconnect the peripheral + Activate(context.Context) error // Activate the peripheral + Deactivate(context.Context) error // Deactivate the peripheral + SetSettings(map[string]interface{}) error // Set a peripheral setting + SetDeviceProperty(context.Context, uint32, byte) error // Update a device property GetInfo() PeripheralInfo // Get the peripheral information GetSettings() map[string]interface{} // Get the peripheral settings @@ -30,7 +30,7 @@ type PeripheralFinder interface { Stop() error // Stop the detection ForceScan() // Explicitly scans for peripherals RegisterPeripheral(context.Context, PeripheralInfo) (string, error) // Registers a new peripheral data - UnregisterPeripheral(context.Context, string) error // Unregisters an existing peripheral + UnregisterPeripheral(string) error // Unregisters an existing peripheral GetPeripheralSettings(string) (map[string]interface{}, error) // Gets the peripheral settings SetPeripheralSettings(string, map[string]interface{}) error // Sets the peripheral settings GetName() string // Get the name of the finder diff --git a/peripherals.go b/peripherals.go index 9120603..d359f97 100644 --- a/peripherals.go +++ b/peripherals.go @@ -64,7 +64,7 @@ func (a *App) UpdatePeripheralSettings(protocolName, peripheralID string, settin return f.SetPeripheralSettings(peripheralID, pInfo.Settings) } -// RemovePeripheral adds a peripheral to the project +// RemovePeripheral removes a peripheral from the project func (a *App) RemovePeripheral(protocolName string, peripheralID string) error { // Unregister the peripheral from the finder f, err := a.hardwareManager.GetFinder(protocolName) @@ -72,7 +72,7 @@ func (a *App) RemovePeripheral(protocolName string, peripheralID string) error { log.Err(err).Str("file", "peripherals").Str("protocolName", protocolName).Msg("unable to find the finder") return fmt.Errorf("unable to find the finder") } - err = f.UnregisterPeripheral(a.ctx, peripheralID) + err = f.UnregisterPeripheral(peripheralID) if err != nil { log.Err(err).Str("file", "peripherals").Str("peripheralID", peripheralID).Msg("unable to unregister this peripheral") return fmt.Errorf("unable to unregister this peripheral") @@ -85,19 +85,6 @@ func (a *App) RemovePeripheral(protocolName string, peripheralID string) error { // FOR TESTING PURPOSE ONLY -// func (a *App) ConnectFTDI() error { -// // Connect the FTDI -// driver, err := a.hardwareManager.GetFinder("FTDI") -// if err != nil { -// return err -// } -// periph, found := driver.GetPeripheral("A50285BI") -// if !found { -// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") -// } -// return periph.Connect(a.ctx) -// } - // func (a *App) ActivateFTDI() error { // // Connect the FTDI // driver, err := a.hardwareManager.GetFinder("FTDI") @@ -136,16 +123,3 @@ func (a *App) RemovePeripheral(protocolName string, peripheralID string) error { // } // return periph.Deactivate(a.ctx) // } - -// func (a *App) DisconnectFTDI() error { -// // Connect the FTDI -// driver, err := a.hardwareManager.GetFinder("FTDI") -// if err != nil { -// return err -// } -// periph, found := driver.GetPeripheral("A50285BI") -// if !found { -// return fmt.Errorf("unable to find the peripheral s/n %s", "A50285BI") -// } -// return periph.Disconnect(a.ctx) -// } From 65e2def501691a61e8ba51b47ffbcc06dd7889a9 Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sun, 19 Oct 2025 19:56:05 +0200 Subject: [PATCH 4/7] added open flag indicator for FTDI --- hardware/FTDIFinder.go | 6 +++--- hardware/interfaces.go | 1 + hardware/third-party/ftdi/detectFTDI.cpp | 11 ++++++++++- 3 files changed, 14 insertions(+), 4 deletions(-) diff --git a/hardware/FTDIFinder.go b/hardware/FTDIFinder.go index e7a489c..3a640a8 100644 --- a/hardware/FTDIFinder.go +++ b/hardware/FTDIFinder.go @@ -232,10 +232,10 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { scanner := bufio.NewScanner(stdout) for scanner.Scan() { peripheralString := scanner.Text() - // The program output is like '0:1:2' where 0 is the location, 1 is the S/N and 2 is the name + // The program output is like '0:1:2:3' where 0 is the location, 1 is the S/N, 2 is the name and 3 is the open flag [O/C] peripheralInfo := strings.Split(peripheralString, ":") - log.Trace().Str("file", "FTDIFinder").Str("scannedString", peripheralString).Str("peripheralName", peripheralInfo[2]).Str("peripheralSN", peripheralInfo[1]).Msg("new FTDI peripheral detected") + log.Trace().Str("file", "FTDIFinder").Str("scannedString", peripheralString).Str("peripheralOpenFlag", peripheralInfo[3]).Str("peripheralName", peripheralInfo[2]).Str("peripheralSN", peripheralInfo[1]).Msg("new FTDI peripheral detected") // Convert the location to an integer location, err := strconv.Atoi(peripheralInfo[0]) if err != nil { @@ -246,6 +246,7 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { temporaryPeripherals[peripheralInfo[1]] = PeripheralInfo{ Name: peripheralInfo[2], SerialNumber: peripheralInfo[1], + IsOpen: peripheralInfo[3] == "O", ProtocolName: "FTDI", } @@ -253,7 +254,6 @@ func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { peripheral, registered := f.registeredPeripherals[peripheralInfo[1]] if registered { runtime.EventsEmit(ctx, string(PeripheralStatus), peripheral.info, "connecting") - time.Sleep(2 * time.Second) err := peripheral.Connect(ctx, location) if err != nil { log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralInfo[1]).Msg("unable to connect the peripheral") diff --git a/hardware/interfaces.go b/hardware/interfaces.go index 629df01..09f568f 100644 --- a/hardware/interfaces.go +++ b/hardware/interfaces.go @@ -20,6 +20,7 @@ type PeripheralInfo struct { Name string `yaml:"name"` // Name of the peripheral SerialNumber string `yaml:"sn"` // S/N of the peripheral ProtocolName string `yaml:"protocol"` // Protocol name of the peripheral + IsOpen bool // Open flag for peripheral connection Settings map[string]interface{} `yaml:"settings"` // Peripheral settings } diff --git a/hardware/third-party/ftdi/detectFTDI.cpp b/hardware/third-party/ftdi/detectFTDI.cpp index c1e8722..d5c271f 100644 --- a/hardware/third-party/ftdi/detectFTDI.cpp +++ b/hardware/third-party/ftdi/detectFTDI.cpp @@ -28,8 +28,17 @@ int main() { for (int i = 0; i < numDevs; i++) { if (devInfo[i].SerialNumber[0] != '\0') { - std::cout << i << ":" << devInfo[i].SerialNumber << ":" << devInfo[i].Description << std::endl; + std::cout << i << ":" << devInfo[i].SerialNumber << ":" << devInfo[i].Description << ":"; + + // Add information about the hardware open state + if (devInfo[i].Flags & FT_FLAGS_OPENED) { + std::cout << "O" << std::endl; + } else { + std::cout << "C" << std::endl; + } } } + + free(devInfo); } } \ No newline at end of file From cb5c5b688ee24c0d7ef8f2dcd0b88f4d51f895f2 Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sat, 25 Oct 2025 12:25:09 +0200 Subject: [PATCH 5/7] Resolve CGO configuration + version in EXE --- .vscode/settings.json | 5 +- build.bat | 55 + go.mod | 2 +- hardware/FTDIFinder.go | 13 - hardware/FTDIPeripheral.go | 190 +- hardware/cpp/generate.bat | 21 + hardware/cpp/include/dmxSenderBridge.h | 15 + hardware/cpp/lib/ftd2xx.lib | Bin 0 -> 20260 bytes hardware/cpp/src/dmxSender.cpp | 127 ++ hardware/cpp/src/dmxSender.h | 49 + hardware/cpp/src/ftd2xx.h | 1667 ++++++++++++++++++ hardware/third-party/ftdi/dmxSender.cpp | 142 -- hardware/third-party/ftdi/dmxSender.manifest | 12 - hardware/third-party/ftdi/generate.bat | 8 - wails.json | 2 +- 15 files changed, 2002 insertions(+), 306 deletions(-) create mode 100644 build.bat create mode 100644 hardware/cpp/generate.bat create mode 100644 hardware/cpp/include/dmxSenderBridge.h create mode 100644 hardware/cpp/lib/ftd2xx.lib create mode 100644 hardware/cpp/src/dmxSender.cpp create mode 100644 hardware/cpp/src/dmxSender.h create mode 100644 hardware/cpp/src/ftd2xx.h delete mode 100644 hardware/third-party/ftdi/dmxSender.cpp delete mode 100644 hardware/third-party/ftdi/dmxSender.manifest delete mode 100644 hardware/third-party/ftdi/generate.bat diff --git a/.vscode/settings.json b/.vscode/settings.json index 4396ea7..3cad51b 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -58,6 +58,9 @@ "streambuf": "cpp", "thread": "cpp", "typeinfo": "cpp", - "variant": "cpp" + "variant": "cpp", + "queue": "cpp", + "ranges": "cpp", + "text_encoding": "cpp" } } \ No newline at end of file diff --git a/build.bat b/build.bat new file mode 100644 index 0000000..b10b859 --- /dev/null +++ b/build.bat @@ -0,0 +1,55 @@ +@echo off +setlocal + +echo ============================================ +echo [INFO] Starting Wails build script +echo ============================================ + +rem Détection du mode (par défaut : build) +set "MODE=build" +if /i "%~1"=="-dev" set "MODE=dev" + +rem 1️⃣ Essayer de récupérer le dernier tag +for /f "tokens=*" %%i in ('git describe --tags --abbrev=0 2^>nul') do set "GIT_TAG=%%i" + +rem 2️⃣ Si pas de tag, utiliser le hash du commit +if "%GIT_TAG%"=="" ( + for /f "tokens=*" %%i in ('git rev-parse --short HEAD 2^>nul') do set "GIT_TAG=%%i" +) + +rem 3️⃣ Si Git n’est pas dispo, mettre "unknown" +if "%GIT_TAG%"=="" set "GIT_TAG=unknown" + +echo [INFO] Git version detected: %GIT_TAG% + + +echo [INFO] Mode selectionne : %MODE% + +echo [INFO] Moving to the C++ folder... +cd /d "%~dp0hardware\cpp" || ( + echo [ERROR] Impossible d'accéder à hardware\cpp + exit /b 1 +) + +echo [INFO] Compiling C++ libraries... +call generate.bat || ( + echo [ERROR] Échec de la compilation C++ + exit /b 1 +) + +echo [INFO] Returning to project root... +cd /d "%~dp0" || exit /b 1 + +if /i "%MODE%"=="dev" ( + echo [INFO] Launching Wails in DEV mode... + wails dev +) else ( + echo [INFO] Building Wails application... + wails build -o "dmxconnect-%GIT_TAG%.exe" +) + +echo ============================================ +echo [SUCCESS] Done! +echo ============================================ + +endlocal \ No newline at end of file diff --git a/go.mod b/go.mod index 0601c5e..f721582 100644 --- a/go.mod +++ b/go.mod @@ -6,6 +6,7 @@ toolchain go1.21.3 require ( github.com/mattrtaylor/go-rtmidi v0.0.0-20220428034745-af795b1c1a79 + github.com/pkg/errors v0.9.1 github.com/rs/zerolog v1.33.0 github.com/wailsapp/wails/v2 v2.9.1 gopkg.in/yaml.v2 v2.4.0 @@ -26,7 +27,6 @@ require ( github.com/mattn/go-colorable v0.1.13 // indirect github.com/mattn/go-isatty v0.0.19 // indirect github.com/pkg/browser v0.0.0-20210911075715-681adbf594b8 // indirect - github.com/pkg/errors v0.9.1 // indirect github.com/rivo/uniseg v0.4.4 // indirect github.com/samber/lo v1.38.1 // indirect github.com/tkrajina/go-reflector v0.5.6 // indirect diff --git a/hardware/FTDIFinder.go b/hardware/FTDIFinder.go index 3a640a8..966ba0e 100644 --- a/hardware/FTDIFinder.go +++ b/hardware/FTDIFinder.go @@ -20,7 +20,6 @@ import ( const ( ftdiFinderExecutableName = "FTDI_finder.exe" - ftdiSenderExecutableName = "FTDI_sender.exe" ) // FTDIFinder represents how the protocol is defined @@ -70,9 +69,6 @@ func (f *FTDIFinder) UnregisterPeripheral(peripheralID string) error { //go:embed third-party/ftdi/detectFTDI.exe var finderExe []byte -//go:embed third-party/ftdi/dmxSender.exe -var senderExe []byte - // Initialize initializes the FTDI finder func (f *FTDIFinder) Initialize() error { // Check platform @@ -85,10 +81,6 @@ func (f *FTDIFinder) Initialize() error { if err != nil { return err } - err = createExecutable(ftdiSenderExecutableName, senderExe) - if err != nil { - return err - } log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder initialized") return nil } @@ -157,11 +149,6 @@ func (f *FTDIFinder) Stop() error { if err != nil { log.Warn().Str("file", "FTDIFinder").Str("fileName", fileToDelete).AnErr("error", err).Msg("unable to remove the executable file") } - fileToDelete = filepath.Join(os.TempDir(), ftdiSenderExecutableName) - err = os.Remove(fileToDelete) - if err != nil { - log.Warn().Str("file", "FTDIFinder").Str("fileName", fileToDelete).AnErr("error", err).Msg("unable to remove the executable file") - } log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped") return nil } diff --git a/hardware/FTDIPeripheral.go b/hardware/FTDIPeripheral.go index 7d218d8..daa7657 100644 --- a/hardware/FTDIPeripheral.go +++ b/hardware/FTDIPeripheral.go @@ -1,31 +1,27 @@ package hardware import ( - "bufio" "context" _ "embed" - "fmt" "io" - "path/filepath" + "unsafe" + + "github.com/pkg/errors" "github.com/rs/zerolog/log" - - "os" - "os/exec" ) -const ( - activateCommandString = 0x01 - deactivateCommandString = 0x02 - setCommandString = 0x03 -) +/* +#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 - programName string // The temp file name of the executable settings map[string]interface{} // The settings of the peripheral - dmxSender *exec.Cmd // The command to pilot the DMX sender program + 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 @@ -38,9 +34,8 @@ 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{ - programName: filepath.Join(os.TempDir(), ftdiSenderExecutableName), info: info, - dmxSender: nil, + dmxDevice: C.dmx_create(), settings: settings, disconnectChan: make(chan struct{}), errorsChan: make(chan error, 1), @@ -49,128 +44,67 @@ func NewFTDIPeripheral(info PeripheralInfo) (*FTDIPeripheral, error) { // Connect connects the FTDI peripheral func (p *FTDIPeripheral) Connect(ctx context.Context, location int) error { - // Connect if no connection is already running + // 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...") - - // Check if the connection has already been established - if p.dmxSender != nil { - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender already initialized") - return nil + 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") } - // Initialize the exec.Command for running the process - p.dmxSender = exec.Command(p.programName, fmt.Sprintf("%d", location)) - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender instance created") + //TODO: Destroy the object when context is done to avoid memory loss - // Create the pipes for stdin, stdout, and stderr asynchronously without blocking - var err error - if p.stdout, err = p.dmxSender.StdoutPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stdout pipe") - return fmt.Errorf("unable to create stdout pipe: %v", err) - } - if p.stdin, err = p.dmxSender.StdinPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stdin pipe") - return fmt.Errorf("unable to create stdin pipe: %v", err) - } - if p.stderr, err = p.dmxSender.StderrPipe(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to create stderr pipe") - return fmt.Errorf("unable to create stderr pipe: %v", err) - } - - // Launch a goroutine to read stderr asynchronously - go func() { - scanner := bufio.NewScanner(p.stderr) - for scanner.Scan() { - // Process each line read from stderr - log.Err(fmt.Errorf(scanner.Text())).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error detected in dmx sender") - } - if err := scanner.Err(); err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error reading from stderr") - } - }() - - // Launch the command asynchronously in another goroutine - go func() { - // Run the command, respecting the context cancellation - err := p.dmxSender.Run() - select { - case <-ctx.Done(): - // If the context is canceled, handle it gracefully - err = p.Disconnect() - if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Msg("unable to disconnect the peripheral") - } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender was canceled by context") - return - default: - // Handle command exit normally - if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while execution of dmx sender") - if exitError, ok := err.(*exec.ExitError); ok { - log.Warn().Str("file", "FTDIPeripheral").Int("exitCode", exitError.ExitCode()).Str("s/n", p.info.SerialNumber).Msg("dmx sender exited with code") - } - } else { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmx sender exited successfully") - } - } - }() - - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxSender process started successfully") + 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 { - log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("disconnecting FTDI peripheral...") - if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") - _, err := io.WriteString(p.stdin, string([]byte{0x04, 0x00, 0x00, 0x00})) - if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") - return fmt.Errorf("unable to disconnect: %v", err) - } - p.stdin.Close() - p.stdout.Close() - p.dmxSender = nil - err = os.Remove(p.programName) - if err != nil { - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Str("senderPath", p.programName).Msg("unable to delete the dmx sender temporary file") - return fmt.Errorf("unable to delete the temporary file: %v", err) - } - return nil + // Check if the device has already been created + if p.dmxDevice == nil { + return errors.Errorf("the DMX device has not been created!") } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while disconnecting: not connected") - return fmt.Errorf("unable to disconnect: not connected") + + //TODO: What actions for disconnecting the DMX device? + + return nil } // Activate activates the FTDI peripheral func (p *FTDIPeripheral) Activate(ctx context.Context) error { - if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") - _, err := io.WriteString(p.stdin, string([]byte{0x01, 0x00, 0x00, 0x00})) - if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") - return fmt.Errorf("unable to activate: %v", err) - } - return nil + // Check if the device has already been created + if p.dmxDevice == nil { + return errors.Errorf("the DMX device has not been created!") } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while activating: not connected") - return fmt.Errorf("unable to activate: not connected") + + 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 { - if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") - _, err := io.WriteString(p.stdin, string([]byte{0x02, 0x00, 0x00, 0x00})) - if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") - return fmt.Errorf("unable to deactivate: %v", err) - } - return nil + // Check if the device has already been created + if p.dmxDevice == nil { + return errors.Errorf("the DMX device has not been created!") } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while deactivating: not connected") - return fmt.Errorf("unable to deactivate: not connected") + + 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 @@ -181,18 +115,18 @@ func (p *FTDIPeripheral) SetSettings(settings map[string]interface{}) error { // SetDeviceProperty sends a command to the specified device func (p *FTDIPeripheral) SetDeviceProperty(ctx context.Context, channelNumber uint32, channelValue byte) error { - if p.dmxSender != nil { - log.Debug().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("dmxsender is defined for this FTDI") - commandString := []byte{0x03, byte(channelNumber & 0xFF), byte((channelNumber >> 8) & 0xFF), channelValue} - _, err := io.WriteString(p.stdin, string(commandString)) - if err != nil { - log.Err(err).Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("unable to write command to sender") - return fmt.Errorf("unable to set device property: %v", err) - } - return nil + // Check if the device has already been created + if p.dmxDevice == nil { + return errors.Errorf("the DMX device has not been created!") } - log.Warn().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("error while setting device property: not connected") - return fmt.Errorf("unable to set device property: not connected") + + 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 diff --git a/hardware/cpp/generate.bat b/hardware/cpp/generate.bat new file mode 100644 index 0000000..a7613fb --- /dev/null +++ b/hardware/cpp/generate.bat @@ -0,0 +1,21 @@ +@REM windres dmxSender.rc dmxSender.o +@REM windres detectFTDI.rc detectFTDI.o + +@REM g++ -o dmxSender.exe dmxSender.cpp dmxSender.o -I"include" -L"lib" -lftd2xx -mwindows +@REM g++ -o detectFTDI.exe detectFTDI.cpp detectFTDI.o -I"include" -L"lib" -lftd2xx -mwindows + +@REM g++ -o dmxSender.exe dmxSender.cpp -I"include" -L"lib" -lftd2xx +@REM g++ -o detectFTDI.exe detectFTDI.cpp -I"include" -L"lib" -lftd2xx + +@REM g++ -c dmxSender.cpp -o dmxSender.o -I"include" -L"lib" -lftd2xx -mwindows + +@REM g++ -c dmxSender_wrapper.cpp -o dmxSender_wrapper.o -I"include" -L"lib" -lftd2xx -mwindows + +@REM g++ -shared -o ../../build/bin/libdmxSender.dll src/dmxSender.cpp -fPIC -Wl,--out-implib,../../build/bin/libdmxSender.dll.a -L"lib" -lftd2xx + +@REM Compiling DMXSENDER library + +g++ -shared -o ../../build/bin/libdmxSender.dll src/dmxSender.cpp -fPIC -L"lib" -lftd2xx + + +@REM g++ -shared -o libdmxSender.so dmxSender.cpp -fPIC -I"include" -L"lib" -lftd2xx -mwindows \ No newline at end of file diff --git a/hardware/cpp/include/dmxSenderBridge.h b/hardware/cpp/include/dmxSenderBridge.h new file mode 100644 index 0000000..08b1991 --- /dev/null +++ b/hardware/cpp/include/dmxSenderBridge.h @@ -0,0 +1,15 @@ +// Declare the C++ function from the shared library + +typedef void DMXDevice; + +extern DMXDevice* dmx_create(); + +extern void* dmx_destroy(DMXDevice* dev); + +extern bool dmx_connect(DMXDevice* dev); + +extern void dmx_activate(DMXDevice* dev); + +extern void dmx_deactivate(DMXDevice* dev); + +extern void dmx_setValue(DMXDevice* dev, int channel, int value); \ No newline at end of file diff --git a/hardware/cpp/lib/ftd2xx.lib b/hardware/cpp/lib/ftd2xx.lib new file mode 100644 index 0000000000000000000000000000000000000000..7b28fab0dbe62abcd38b6588f3702be7decbe218 GIT binary patch literal 20260 zcmdU1OK)677CwX-m!^WxH)BCVt^KBs>PYu{+SB-EFnonF&G+ zLWtFfB}frM2(e(n56ET^LWl)I2q6|MW`S6-idZ0okOt;Eb?R1~y7%^_+wCl-rQ>_+ z)T#RFoO++C>j#e%YV*$@TfeU({O#}Q>dy7{_8jUB`JFbH%jN9r+G%gi$G#+D+A%HE|0@yGj>|&1 zSBRL7ZWik80S;&YIWXvf%k+Ce~a>iI^Px=_#F74eJDFq?{7jAA3=Z6Bfw?a{i@L3ZA6CNz?11`p%Lh2 zXd|AWM_&>ee+}gWjb0KO-b-ZYT|7Y}SA z`V3Fd7;I?>^)Ut--z2pE0uj^DCqjeo!)~C%>xBAIHl{w5k7@U2p*_%@X;)cj=PyJ| zT{A-azem2Hqdy6CeF{D(hcYsqSP(h{+Zsan5NQ8(Lc5^{)4n;OJ;;}-`)Q#^24Q#5 zu5W~P!q$fF#}kyl*%I=|GrcG@gglsfazdSWcIIYhN5`ior)Oq|hEM0GN2g{cr)NiI zhPuw3quJSsGh<`Pc>1RZNc7CfGZUv~hX!T_Xk=z~a;aEB!qVara*+_8`Eqr+=-Cnk zf4W#$@N5$T_iU|HSJDy$f3%vfm%X?IO)ksD`cUygXD|K!~TsOUjXQ_3!y=rCzJ5A|i$6Ta0=w%oAb6h5ol#L6RSBGgVRV%DD8$**WMI}i}Y#63d{8rE12#Y#aL-G zlrwHt@X}Raz=e7wff)`RYnVb-M=ff>5kaRclIEa$*+Z{ivvCoq#J+JY2*? zEDQ1Zi}U4Th~ilsU*mDjo0g|7Z{E~cqzcR)CP_xc<%f#(LP;&xf)q9qX<)e_Xtr3W z*v99Jm5j0&wGzg^$1#NkD;Wj|VnlkZ^lYtAyO<^(i-Now0Fu270NjqFUWRI%FQ_$* z4I`K7V!0Zwy-irkgDkkBCRP@o)e8j4T*W{dt$?c-E8p{_=e%7)w#b=TB1KU1eU8KV zd?GvMNeR{ZQ8zf0FpM$zO-)aZ%Ovf_jNxI=^fD=PzY&Z0khG>54dztM^fI>dr%9?P z$Uk1JEL3ax#RU}K>l|>k6xv*h6mzu{^3s;kxq+`Gkup_M_>df}lcfuu$8n+sr2h&y z>p+{4$9s_b7Wu#3N%T3=f4Pt7H{|sZ@IQQn=+)iu7VRZ^1N1ZKl@X$ipqCycdJXwq z8if~RnCM;574W}4AzqBjC$W1++y=<~06KSy=rd5y81w*bLfQ-ai9Q)3dLLz5hcdi| z@?3(xn}PY!9@t|S(JwoRX1a*JhwM+#^HaQUfQ}2m`TP)`pz9F#1JcZ)>@!IBH1r-s z*}p;hH<12*(9LM=Abj2V)SRanKsaq^aPF4NjgVoX$OtaPC89z=mhPe zM`$N>u4K2MpN`x8m2t0 zr-$fnx{Ef_4Kzmg(M`0OZln$xqFZPaJxuq|AU#EYqo?T^8lWyZKznHqCTw+nnNEx>rq`j-ad2+aJI3rU>!}1ob1@wAhIkJ<8AZQkI zCZ%4Xf`OUCIooW|dIoF=E%Jp)=QIi3EY9_WTpGLEVhb|KH}adTDu`I9BCWl&hZjy2$X!6lirFr3mrDMF=y&;f^XJ>9_ z!>ZzR!9=Ek^mL~A!QdD~G~U1J2~HAw1hJBs?$W>>P`V#wYfC(!Y11ToymgAKsWb6y2Sf zTfgx7wbb|D@8{NEL)hEPZ|_3!*_G!WK5c{W1rYr!3fN~qZ5Z5JTEI#B!|+weZxbG0 zfWDI;U5jTwb$)E+;ERyojd=V9`c8u8Z$EW>ybf=-!k@su^>RCbmD*0XZMYt+uS^7- z9C*I<%*OH3e66}%JzwwGF}<^6Y;*#Cm*kJJxExFb&jr8X`Ud<7{N_PW()sG*5{mlP zGuK@6`|mmhMB#y8iZt#gGNpY!;uzfo$I9eb28qbYbbAi-AK*Xs$GPKPjt%i+9Xu<9 zai;ZUvPLv9R* z$Ee2zC$<^)2zttF5-p?7&ID+@(oz+rUYkT}P-k}mWov{K`3O|*tI+9Sdh56cDET-N zR_txruR$N%44iEat8Lr4d(Yv>$D`|G0g82;D3$zc`}M@jL5#AI>)#v%xQ4!_Jqia` z8Y!rxmNm-gx!WC%JHK!WttHM5g=4RYtsW<%C*c6l+RdN3IH1;RC}S*|c1YTB=54l$ zew)#k9Fo)KJb~X!VaLF zNI{G*ELzquqxN_^E43Foc|FD|+GR$c=9RSIY1eT(Xwe;z_?jJ`H{b?Agcdjn6`X%< zvbAK?-IzxUz1$u|HPq(t-1azSt;Q}2v88sME3nr4T}I7qbZE-)uJmcWon(zFGihk; zuB00DbY5Tc?n`WG4-b?X{cDqTmN70rB;&t2k?^^d`8fv3}>( zAu#meiBiYEHfOg34O-uAK!M|sk?iFJr%dZDyunz=_0Ow!i_vaNi(H?vUdy8&`vrj# zU8-=ZtfjTsM+l7greIC2Wp={qE{IcPO;IKzP3%3BtAjETp_oHCUlXh7kr}<^I)~zp z`V^FZZLYUuc?$*=lD&H44A(MF##*f3;iyAN4}dlp!8WN=hV|Gvk2a0t5O>n)vu_|Twngxcz#_q!WbZjSD6d|V*2~a|y#*SnqAa!woicVfw>YHedX`hD z<&mMgfuDuFIqZWLjnYn~z3p1pF3YPq>QUjXQuq87pnq+@-q^EgwRB(gt=^tlUST-A zBP4ShZuKZxzEhsdMW5thzqecKb(XbG=CeM*)K>`FLCe~6uoqR()@;v_(F<_R+@hu3 zc4$Y(thU1EouXsACkR=q%Ud0aI~i9B`Pb%pS=MgjL5CDx_wX32XptFn8Sgm6e(n;9 zg7UA;X_m_v_m5j7e>27Bz6zZhv_xJxKOso&1`FSJX$z~tZh-sOQ%S6LSL_*@JsCs8 z66C6B_H+{KDr$xgShyVq_lVl5v+f4#0`6bbTp$9;TqMq3#R>qvP+hehp z$9^yn<-fM=^PtDFHx;9=BHF;q7)5&%c=n462iK}4v(|q2=rL+5R=ca)`|wd_3~w!Z zUsk(393Z-HP*!ivj5ClU9xJ?|>Z`2vevr}U@QG?vm*8l(j literal 0 HcmV?d00001 diff --git a/hardware/cpp/src/dmxSender.cpp b/hardware/cpp/src/dmxSender.cpp new file mode 100644 index 0000000..96685f1 --- /dev/null +++ b/hardware/cpp/src/dmxSender.cpp @@ -0,0 +1,127 @@ +//dmxSender.cpp + +#include "dmxSender.h" + +#define DMX_START_CODE 0x00 +#define BREAK_DURATION_US 110 +#define MAB_DURATION_US 16 +#define DMX_CHANNELS 512 +#define FREQUENCY 44 +#define INTERVAL (1000000 / FREQUENCY) + +// Initialize default values for starting the DMX device +DMXDevice::DMXDevice(){ + ftHandle = nullptr; + isOutputActivated = false; +} + +// Properly close the DMX device +DMXDevice::~DMXDevice(){ + deactivate(); + if (ftHandle != nullptr){ + FT_Close(ftHandle); + ftHandle = nullptr; + } +} + +// Connect the device on a specific port +bool DMXDevice::connect(int port){ + ftStatus = FT_Open(port, &ftHandle); + if (ftStatus != FT_OK) { + return true; + } + + ftStatus = FT_SetBaudRate(ftHandle, 250000); + ftStatus |= FT_SetDataCharacteristics(ftHandle, 8, FT_STOP_BITS_2, FT_PARITY_NONE); // 8 bits, no parity, 1 stop bit + ftStatus |= FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0); + if (ftStatus != FT_OK) { + FT_Close(ftHandle); + return true; + } + + // Send the DMX frames + std::thread updateThread([this]() { + this->sendDMX(ftHandle); + }); + return false; +} + +// Activate the DMX flow +void DMXDevice::activate(){ + isOutputActivated.store(true); +} + +// Deactivate the DMX flow +void DMXDevice::deactivate(){ + isOutputActivated.store(false); +} + +// Set the value of a DMX channe +void DMXDevice::setValue(int channel, int value){ + dmxData[channel].store(value); +} + +// Send a break line +void DMXDevice::sendBreak(FT_HANDLE ftHandle) { + FT_SetBreakOn(ftHandle); // Set BREAK ON + std::this_thread::sleep_for(std::chrono::microseconds(BREAK_DURATION_US)); + FT_SetBreakOff(ftHandle); // Set BREAK OFF +} + +// Continuously send the DMX frame +void DMXDevice::sendDMX(FT_HANDLE ftHandle) { + while (true) { + if(isOutputActivated){ + // Send the BREAK + sendBreak(ftHandle); + + // Send the MAB + std::this_thread::sleep_for(std::chrono::microseconds(MAB_DURATION_US)); + + DWORD bytesWritten = 0; + + // Send the DMX frame + FT_STATUS status = FT_Write(ftHandle, dmxData, DMX_CHANNELS, &bytesWritten); + if (status != FT_OK || bytesWritten != DMX_CHANNELS) { // Error detected when trying to send the frame. Deactivate the line. + deactivate(); + continue; + } + + // Wait before sending the next frame + std::this_thread::sleep_for(std::chrono::microseconds(INTERVAL - BREAK_DURATION_US - MAB_DURATION_US)); + } + } +} + +// Linkable functions from Golang +extern "C" { + // Create a new DMX device + DMXDevice* dmx_create() { + return new DMXDevice(); + } + + // Destroy a DMX device + void dmx_destroy(DMXDevice* dev) { + dev->~DMXDevice(); + } + + // Connect a DMX device + bool dmx_connect(DMXDevice* dev, int port) { + return dev->connect(port); + } + + // Activate a DMX device + void dmx_activate(DMXDevice* dev) { + dev->activate(); + } + + // Deactivate a DMX device + void dmx_deactivate(DMXDevice* dev) { + dev->deactivate(); + } + + // Set the channel value of a DMX device + void dmx_setValue(DMXDevice* dev, int channel, int value) { + dev->setValue(channel, value); + } +} \ No newline at end of file diff --git a/hardware/cpp/src/dmxSender.h b/hardware/cpp/src/dmxSender.h new file mode 100644 index 0000000..70b7644 --- /dev/null +++ b/hardware/cpp/src/dmxSender.h @@ -0,0 +1,49 @@ +// dmxSender.h + +#pragma once + +#include +#include +#include +#include +#include "ftd2xx.h" + +#define DMX_START_CODE 0x00 +#define BREAK_DURATION_US 110 +#define MAB_DURATION_US 16 +#define DMX_CHANNELS 512 +#define FREQUENCY 44 +#define INTERVAL (1000000 / FREQUENCY) + +class DMXDevice { +public: + // Initialize default values for starting the DMX device + DMXDevice(); + + // Properly close the DMX device + ~DMXDevice(); + + // Connect the device on a specific port + bool connect(int port); + + // Activate the DMX flow + void activate(); + + // Deactivate the DMX flow + void deactivate(); + + // Set the value of a DMX channel + void setValue(int channel, int value); + +private: + FT_STATUS ftStatus; // FTDI peripheral status + FT_HANDLE ftHandle = nullptr; // FTDI object + std::atomic dmxData[DMX_CHANNELS + 1]; // For storing dynamically the DMX data + std::atomic isOutputActivated = false; // Boolean to start/stop the DMX flow + + // Send a break line + void sendBreak(FT_HANDLE ftHandle); + + // Continuously send the DMX frame + void sendDMX(FT_HANDLE ftHandle); +}; \ No newline at end of file diff --git a/hardware/cpp/src/ftd2xx.h b/hardware/cpp/src/ftd2xx.h new file mode 100644 index 0000000..71bc79b --- /dev/null +++ b/hardware/cpp/src/ftd2xx.h @@ -0,0 +1,1667 @@ +/*++ + +Copyright © 2001-2021 Future Technology Devices International Limited + +THIS SOFTWARE IS PROVIDED BY FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES +OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT +OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) +HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR +TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, +EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +FTDI DRIVERS MAY BE USED ONLY IN CONJUNCTION WITH PRODUCTS BASED ON FTDI PARTS. + +FTDI DRIVERS MAY BE DISTRIBUTED IN ANY FORM AS LONG AS LICENSE INFORMATION IS NOT MODIFIED. + +IF A CUSTOM VENDOR ID AND/OR PRODUCT ID OR DESCRIPTION STRING ARE USED, IT IS THE +RESPONSIBILITY OF THE PRODUCT MANUFACTURER TO MAINTAIN ANY CHANGES AND SUBSEQUENT WHQL +RE-CERTIFICATION AS A RESULT OF MAKING THESE CHANGES. + + +Module Name: + +ftd2xx.h + +Abstract: + +Native USB device driver for FTDI FT232x, FT245x, FT2232x, FT4232x, FT2233H and FT4233H devices +FTD2XX library definitions + +Environment: + +kernel & user mode + + +--*/ + + +#ifndef FTD2XX_H +#define FTD2XX_H + +#ifdef _WIN32 +// Compiling on Windows +#include + +// The following ifdef block is the standard way of creating macros +// which make exporting from a DLL simpler. All files within this DLL +// are compiled with the FTD2XX_EXPORTS symbol defined on the command line. +// This symbol should not be defined on any project that uses this DLL. +// This way any other project whose source files include this file see +// FTD2XX_API functions as being imported from a DLL, whereas this DLL +// sees symbols defined with this macro as being exported. + +#ifdef FTD2XX_EXPORTS +#define FTD2XX_API __declspec(dllexport) +#elif defined(FTD2XX_STATIC) +// Avoid decorations when linking statically to D2XX. +#define FTD2XX_API +// Static D2XX depends on these Windows libs: +#pragma comment(lib, "setupapi.lib") +#pragma comment(lib, "advapi32.lib") +#pragma comment(lib, "user32.lib") +#else +#define FTD2XX_API __declspec(dllimport) +#endif + +#else // _WIN32 +// Compiling on non-Windows platform. +#include "WinTypes.h" +// No decorations needed. +#define FTD2XX_API + +#endif // _WIN32 + +typedef PVOID FT_HANDLE; +typedef ULONG FT_STATUS; + +// +// Device status +// +enum { + FT_OK, + FT_INVALID_HANDLE, + FT_DEVICE_NOT_FOUND, + FT_DEVICE_NOT_OPENED, + FT_IO_ERROR, + FT_INSUFFICIENT_RESOURCES, + FT_INVALID_PARAMETER, + FT_INVALID_BAUD_RATE, + + FT_DEVICE_NOT_OPENED_FOR_ERASE, + FT_DEVICE_NOT_OPENED_FOR_WRITE, + FT_FAILED_TO_WRITE_DEVICE, + FT_EEPROM_READ_FAILED, + FT_EEPROM_WRITE_FAILED, + FT_EEPROM_ERASE_FAILED, + FT_EEPROM_NOT_PRESENT, + FT_EEPROM_NOT_PROGRAMMED, + FT_INVALID_ARGS, + FT_NOT_SUPPORTED, + FT_OTHER_ERROR, + FT_DEVICE_LIST_NOT_READY, +}; + + +#define FT_SUCCESS(status) ((status) == FT_OK) + +// +// FT_OpenEx Flags +// + +#define FT_OPEN_BY_SERIAL_NUMBER 1 +#define FT_OPEN_BY_DESCRIPTION 2 +#define FT_OPEN_BY_LOCATION 4 + +#define FT_OPEN_MASK (FT_OPEN_BY_SERIAL_NUMBER | \ + FT_OPEN_BY_DESCRIPTION | \ + FT_OPEN_BY_LOCATION) + +// +// FT_ListDevices Flags (used in conjunction with FT_OpenEx Flags +// + +#define FT_LIST_NUMBER_ONLY 0x80000000 +#define FT_LIST_BY_INDEX 0x40000000 +#define FT_LIST_ALL 0x20000000 + +#define FT_LIST_MASK (FT_LIST_NUMBER_ONLY|FT_LIST_BY_INDEX|FT_LIST_ALL) + +// +// Baud Rates +// + +#define FT_BAUD_300 300 +#define FT_BAUD_600 600 +#define FT_BAUD_1200 1200 +#define FT_BAUD_2400 2400 +#define FT_BAUD_4800 4800 +#define FT_BAUD_9600 9600 +#define FT_BAUD_14400 14400 +#define FT_BAUD_19200 19200 +#define FT_BAUD_38400 38400 +#define FT_BAUD_57600 57600 +#define FT_BAUD_115200 115200 +#define FT_BAUD_230400 230400 +#define FT_BAUD_460800 460800 +#define FT_BAUD_921600 921600 + +// +// Word Lengths +// + +#define FT_BITS_8 (UCHAR) 8 +#define FT_BITS_7 (UCHAR) 7 + +// +// Stop Bits +// + +#define FT_STOP_BITS_1 (UCHAR) 0 +#define FT_STOP_BITS_2 (UCHAR) 2 + +// +// Parity +// + +#define FT_PARITY_NONE (UCHAR) 0 +#define FT_PARITY_ODD (UCHAR) 1 +#define FT_PARITY_EVEN (UCHAR) 2 +#define FT_PARITY_MARK (UCHAR) 3 +#define FT_PARITY_SPACE (UCHAR) 4 + +// +// Flow Control +// + +#define FT_FLOW_NONE 0x0000 +#define FT_FLOW_RTS_CTS 0x0100 +#define FT_FLOW_DTR_DSR 0x0200 +#define FT_FLOW_XON_XOFF 0x0400 + +// +// Purge rx and tx buffers +// +#define FT_PURGE_RX 1 +#define FT_PURGE_TX 2 + +// +// Events +// + +typedef void(*PFT_EVENT_HANDLER)(DWORD, DWORD); + +#define FT_EVENT_RXCHAR 1 +#define FT_EVENT_MODEM_STATUS 2 +#define FT_EVENT_LINE_STATUS 4 + +// +// Timeouts +// + +#define FT_DEFAULT_RX_TIMEOUT 300 +#define FT_DEFAULT_TX_TIMEOUT 300 + +// +// Device types +// + +typedef ULONG FT_DEVICE; + +enum { + FT_DEVICE_BM, + FT_DEVICE_AM, + FT_DEVICE_100AX, + FT_DEVICE_UNKNOWN, + FT_DEVICE_2232C, + FT_DEVICE_232R, + FT_DEVICE_2232H, + FT_DEVICE_4232H, + FT_DEVICE_232H, + FT_DEVICE_X_SERIES, + FT_DEVICE_4222H_0, + FT_DEVICE_4222H_1_2, + FT_DEVICE_4222H_3, + FT_DEVICE_4222_PROG, + FT_DEVICE_900, + FT_DEVICE_930, + FT_DEVICE_UMFTPD3A, + FT_DEVICE_2233HP, + FT_DEVICE_4233HP, + FT_DEVICE_2232HP, + FT_DEVICE_4232HP, + FT_DEVICE_233HP, + FT_DEVICE_232HP, + FT_DEVICE_2232HA, + FT_DEVICE_4232HA, + FT_DEVICE_232RN, +}; + +// +// Bit Modes +// + +#define FT_BITMODE_RESET 0x00 +#define FT_BITMODE_ASYNC_BITBANG 0x01 +#define FT_BITMODE_MPSSE 0x02 +#define FT_BITMODE_SYNC_BITBANG 0x04 +#define FT_BITMODE_MCU_HOST 0x08 +#define FT_BITMODE_FAST_SERIAL 0x10 +#define FT_BITMODE_CBUS_BITBANG 0x20 +#define FT_BITMODE_SYNC_FIFO 0x40 + +// +// FT232R CBUS Options EEPROM values +// + +#define FT_232R_CBUS_TXDEN 0x00 // Tx Data Enable +#define FT_232R_CBUS_PWRON 0x01 // Power On +#define FT_232R_CBUS_RXLED 0x02 // Rx LED +#define FT_232R_CBUS_TXLED 0x03 // Tx LED +#define FT_232R_CBUS_TXRXLED 0x04 // Tx and Rx LED +#define FT_232R_CBUS_SLEEP 0x05 // Sleep +#define FT_232R_CBUS_CLK48 0x06 // 48MHz clock +#define FT_232R_CBUS_CLK24 0x07 // 24MHz clock +#define FT_232R_CBUS_CLK12 0x08 // 12MHz clock +#define FT_232R_CBUS_CLK6 0x09 // 6MHz clock +#define FT_232R_CBUS_IOMODE 0x0A // IO Mode for CBUS bit-bang +#define FT_232R_CBUS_BITBANG_WR 0x0B // Bit-bang write strobe +#define FT_232R_CBUS_BITBANG_RD 0x0C // Bit-bang read strobe + +// +// FT232H CBUS Options EEPROM values +// + +#define FT_232H_CBUS_TRISTATE 0x00 // Tristate +#define FT_232H_CBUS_TXLED 0x01 // Tx LED +#define FT_232H_CBUS_RXLED 0x02 // Rx LED +#define FT_232H_CBUS_TXRXLED 0x03 // Tx and Rx LED +#define FT_232H_CBUS_PWREN 0x04 // Power Enable +#define FT_232H_CBUS_SLEEP 0x05 // Sleep +#define FT_232H_CBUS_DRIVE_0 0x06 // Drive pin to logic 0 +#define FT_232H_CBUS_DRIVE_1 0x07 // Drive pin to logic 1 +#define FT_232H_CBUS_IOMODE 0x08 // IO Mode for CBUS bit-bang +#define FT_232H_CBUS_TXDEN 0x09 // Tx Data Enable +#define FT_232H_CBUS_CLK30 0x0A // 30MHz clock +#define FT_232H_CBUS_CLK15 0x0B // 15MHz clock +#define FT_232H_CBUS_CLK7_5 0x0C // 7.5MHz clock + +// +// FT X Series CBUS Options EEPROM values +// + +#define FT_X_SERIES_CBUS_TRISTATE 0x00 // Tristate +#define FT_X_SERIES_CBUS_TXLED 0x01 // Tx LED +#define FT_X_SERIES_CBUS_RXLED 0x02 // Rx LED +#define FT_X_SERIES_CBUS_TXRXLED 0x03 // Tx and Rx LED +#define FT_X_SERIES_CBUS_PWREN 0x04 // Power Enable +#define FT_X_SERIES_CBUS_SLEEP 0x05 // Sleep +#define FT_X_SERIES_CBUS_DRIVE_0 0x06 // Drive pin to logic 0 +#define FT_X_SERIES_CBUS_DRIVE_1 0x07 // Drive pin to logic 1 +#define FT_X_SERIES_CBUS_IOMODE 0x08 // IO Mode for CBUS bit-bang +#define FT_X_SERIES_CBUS_TXDEN 0x09 // Tx Data Enable +#define FT_X_SERIES_CBUS_CLK24 0x0A // 24MHz clock +#define FT_X_SERIES_CBUS_CLK12 0x0B // 12MHz clock +#define FT_X_SERIES_CBUS_CLK6 0x0C // 6MHz clock +#define FT_X_SERIES_CBUS_BCD_CHARGER 0x0D // Battery charger detected +#define FT_X_SERIES_CBUS_BCD_CHARGER_N 0x0E // Battery charger detected inverted +#define FT_X_SERIES_CBUS_I2C_TXE 0x0F // I2C Tx empty +#define FT_X_SERIES_CBUS_I2C_RXF 0x10 // I2C Rx full +#define FT_X_SERIES_CBUS_VBUS_SENSE 0x11 // Detect VBUS +#define FT_X_SERIES_CBUS_BITBANG_WR 0x12 // Bit-bang write strobe +#define FT_X_SERIES_CBUS_BITBANG_RD 0x13 // Bit-bang read strobe +#define FT_X_SERIES_CBUS_TIMESTAMP 0x14 // Toggle output when a USB SOF token is received +#define FT_X_SERIES_CBUS_KEEP_AWAKE 0x15 // + + +// Driver types +#define FT_DRIVER_TYPE_D2XX 0 +#define FT_DRIVER_TYPE_VCP 1 + + + +#ifdef __cplusplus +extern "C" { +#endif + + +#ifdef FTD2XX_STATIC + FTD2XX_API + FT_STATUS WINAPI FT_Initialise( + void + ); + + FTD2XX_API + void WINAPI FT_Finalise( + void + ); +#endif // FTD2XX_STATIC + + FTD2XX_API + FT_STATUS WINAPI FT_Open( + int deviceNumber, + FT_HANDLE *pHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_OpenEx( + PVOID pArg1, + DWORD Flags, + FT_HANDLE *pHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ListDevices( + PVOID pArg1, + PVOID pArg2, + DWORD Flags + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Close( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Read( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD dwBytesToRead, + LPDWORD lpBytesReturned + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Write( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD dwBytesToWrite, + LPDWORD lpBytesWritten + ); + + FTD2XX_API + FT_STATUS WINAPI FT_IoCtl( + FT_HANDLE ftHandle, + DWORD dwIoControlCode, + LPVOID lpInBuf, + DWORD nInBufSize, + LPVOID lpOutBuf, + DWORD nOutBufSize, + LPDWORD lpBytesReturned, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBaudRate( + FT_HANDLE ftHandle, + ULONG BaudRate + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDivisor( + FT_HANDLE ftHandle, + USHORT Divisor + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDataCharacteristics( + FT_HANDLE ftHandle, + UCHAR WordLength, + UCHAR StopBits, + UCHAR Parity + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetFlowControl( + FT_HANDLE ftHandle, + USHORT FlowControl, + UCHAR XonChar, + UCHAR XoffChar + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ResetDevice( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDtr( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ClrDtr( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetRts( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ClrRts( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetModemStatus( + FT_HANDLE ftHandle, + ULONG *pModemStatus + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetChars( + FT_HANDLE ftHandle, + UCHAR EventChar, + UCHAR EventCharEnabled, + UCHAR ErrorChar, + UCHAR ErrorCharEnabled + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Purge( + FT_HANDLE ftHandle, + ULONG Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetTimeouts( + FT_HANDLE ftHandle, + ULONG ReadTimeout, + ULONG WriteTimeout + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetQueueStatus( + FT_HANDLE ftHandle, + DWORD *dwRxBytes + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetEventNotification( + FT_HANDLE ftHandle, + DWORD Mask, + PVOID Param + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetStatus( + FT_HANDLE ftHandle, + DWORD *dwRxBytes, + DWORD *dwTxBytes, + DWORD *dwEventDWord + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBreakOn( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBreakOff( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetWaitMask( + FT_HANDLE ftHandle, + DWORD Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_WaitOnMask( + FT_HANDLE ftHandle, + DWORD *Mask + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetEventStatus( + FT_HANDLE ftHandle, + DWORD *dwEventDWord + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ReadEE( + FT_HANDLE ftHandle, + DWORD dwWordOffset, + LPWORD lpwValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_WriteEE( + FT_HANDLE ftHandle, + DWORD dwWordOffset, + WORD wValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EraseEE( + FT_HANDLE ftHandle + ); + + // + // structure to hold program data for FT_EE_Program, FT_EE_ProgramEx, FT_EE_Read + // and FT_EE_ReadEx functions + // + typedef struct ft_program_data { + + DWORD Signature1; // Header - must be 0x00000000 + DWORD Signature2; // Header - must be 0xffffffff + DWORD Version; // Header - FT_PROGRAM_DATA version + // 0 = original + // 1 = FT2232 extensions + // 2 = FT232R extensions + // 3 = FT2232H extensions + // 4 = FT4232H extensions + // 5 = FT232H extensions + + WORD VendorId; // 0x0403 + WORD ProductId; // 0x6001 + char *Manufacturer; // "FTDI" + char *ManufacturerId; // "FT" + char *Description; // "USB HS Serial Converter" + char *SerialNumber; // "FT000001" if fixed, or NULL + WORD MaxPower; // 0 < MaxPower <= 500 + WORD PnP; // 0 = disabled, 1 = enabled + WORD SelfPowered; // 0 = bus powered, 1 = self powered + WORD RemoteWakeup; // 0 = not capable, 1 = capable + // + // Rev4 (FT232B) extensions + // + UCHAR Rev4; // non-zero if Rev4 chip, zero otherwise + UCHAR IsoIn; // non-zero if in endpoint is isochronous + UCHAR IsoOut; // non-zero if out endpoint is isochronous + UCHAR PullDownEnable; // non-zero if pull down enabled + UCHAR SerNumEnable; // non-zero if serial number to be used + UCHAR USBVersionEnable; // non-zero if chip uses USBVersion + WORD USBVersion; // BCD (0x0200 => USB2) + // + // Rev 5 (FT2232) extensions + // + UCHAR Rev5; // non-zero if Rev5 chip, zero otherwise + UCHAR IsoInA; // non-zero if in endpoint is isochronous + UCHAR IsoInB; // non-zero if in endpoint is isochronous + UCHAR IsoOutA; // non-zero if out endpoint is isochronous + UCHAR IsoOutB; // non-zero if out endpoint is isochronous + UCHAR PullDownEnable5; // non-zero if pull down enabled + UCHAR SerNumEnable5; // non-zero if serial number to be used + UCHAR USBVersionEnable5; // non-zero if chip uses USBVersion + WORD USBVersion5; // BCD (0x0200 => USB2) + UCHAR AIsHighCurrent; // non-zero if interface is high current + UCHAR BIsHighCurrent; // non-zero if interface is high current + UCHAR IFAIsFifo; // non-zero if interface is 245 FIFO + UCHAR IFAIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IFAIsFastSer; // non-zero if interface is Fast serial + UCHAR AIsVCP; // non-zero if interface is to use VCP drivers + UCHAR IFBIsFifo; // non-zero if interface is 245 FIFO + UCHAR IFBIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IFBIsFastSer; // non-zero if interface is Fast serial + UCHAR BIsVCP; // non-zero if interface is to use VCP drivers + // + // Rev 6 (FT232R) extensions + // + UCHAR UseExtOsc; // Use External Oscillator + UCHAR HighDriveIOs; // High Drive I/Os + UCHAR EndpointSize; // Endpoint size + UCHAR PullDownEnableR; // non-zero if pull down enabled + UCHAR SerNumEnableR; // non-zero if serial number to be used + UCHAR InvertTXD; // non-zero if invert TXD + UCHAR InvertRXD; // non-zero if invert RXD + UCHAR InvertRTS; // non-zero if invert RTS + UCHAR InvertCTS; // non-zero if invert CTS + UCHAR InvertDTR; // non-zero if invert DTR + UCHAR InvertDSR; // non-zero if invert DSR + UCHAR InvertDCD; // non-zero if invert DCD + UCHAR InvertRI; // non-zero if invert RI + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + UCHAR RIsD2XX; // non-zero if using D2XX driver + // + // Rev 7 (FT2232H) Extensions + // + UCHAR PullDownEnable7; // non-zero if pull down enabled + UCHAR SerNumEnable7; // non-zero if serial number to be used + UCHAR ALSlowSlew; // non-zero if AL pins have slow slew + UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input + UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR AHSlowSlew; // non-zero if AH pins have slow slew + UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input + UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BLSlowSlew; // non-zero if BL pins have slow slew + UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input + UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BHSlowSlew; // non-zero if BH pins have slow slew + UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input + UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR IFAIsFifo7; // non-zero if interface is 245 FIFO + UCHAR IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + UCHAR IFAIsFastSer7; // non-zero if interface is Fast serial + UCHAR AIsVCP7; // non-zero if interface is to use VCP drivers + UCHAR IFBIsFifo7; // non-zero if interface is 245 FIFO + UCHAR IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target + UCHAR IFBIsFastSer7; // non-zero if interface is Fast serial + UCHAR BIsVCP7; // non-zero if interface is to use VCP drivers + UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs + // + // Rev 8 (FT4232H) Extensions + // + UCHAR PullDownEnable8; // non-zero if pull down enabled + UCHAR SerNumEnable8; // non-zero if serial number to be used + UCHAR ASlowSlew; // non-zero if A pins have slow slew + UCHAR ASchmittInput; // non-zero if A pins are Schmitt input + UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BSlowSlew; // non-zero if B pins have slow slew + UCHAR BSchmittInput; // non-zero if B pins are Schmitt input + UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR CSlowSlew; // non-zero if C pins have slow slew + UCHAR CSchmittInput; // non-zero if C pins are Schmitt input + UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR DSlowSlew; // non-zero if D pins have slow slew + UCHAR DSchmittInput; // non-zero if D pins are Schmitt input + UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN + UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN + UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN + UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN + UCHAR AIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR BIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR CIsVCP8; // non-zero if interface is to use VCP drivers + UCHAR DIsVCP8; // non-zero if interface is to use VCP drivers + // + // Rev 9 (FT232H) Extensions + // + UCHAR PullDownEnableH; // non-zero if pull down enabled + UCHAR SerNumEnableH; // non-zero if serial number to be used + UCHAR ACSlowSlewH; // non-zero if AC pins have slow slew + UCHAR ACSchmittInputH; // non-zero if AC pins are Schmitt input + UCHAR ACDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ADSlowSlewH; // non-zero if AD pins have slow slew + UCHAR ADSchmittInputH; // non-zero if AD pins are Schmitt input + UCHAR ADDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR Cbus0H; // Cbus Mux control + UCHAR Cbus1H; // Cbus Mux control + UCHAR Cbus2H; // Cbus Mux control + UCHAR Cbus3H; // Cbus Mux control + UCHAR Cbus4H; // Cbus Mux control + UCHAR Cbus5H; // Cbus Mux control + UCHAR Cbus6H; // Cbus Mux control + UCHAR Cbus7H; // Cbus Mux control + UCHAR Cbus8H; // Cbus Mux control + UCHAR Cbus9H; // Cbus Mux control + UCHAR IsFifoH; // non-zero if interface is 245 FIFO + UCHAR IsFifoTarH; // non-zero if interface is 245 FIFO CPU target + UCHAR IsFastSerH; // non-zero if interface is Fast serial + UCHAR IsFT1248H; // non-zero if interface is FT1248 + UCHAR FT1248CpolH; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) + UCHAR FT1248LsbH; // FT1248 data is LSB (1) or MSB (0) + UCHAR FT1248FlowControlH; // FT1248 flow control enable + UCHAR IsVCPH; // non-zero if interface is to use VCP drivers + UCHAR PowerSaveEnableH; // non-zero if using ACBUS7 to save power for self-powered designs + + } FT_PROGRAM_DATA, *PFT_PROGRAM_DATA; + + FTD2XX_API + FT_STATUS WINAPI FT_EE_Program( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ProgramEx( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_Read( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadEx( + FT_HANDLE ftHandle, + PFT_PROGRAM_DATA pData, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UASize( + FT_HANDLE ftHandle, + LPDWORD lpdwSize + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UAWrite( + FT_HANDLE ftHandle, + PUCHAR pucData, + DWORD dwDataLen + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_UARead( + FT_HANDLE ftHandle, + PUCHAR pucData, + DWORD dwDataLen, + LPDWORD lpdwBytesRead + ); + + + typedef struct ft_eeprom_header { + FT_DEVICE deviceType; // FTxxxx device type to be programmed + // Device descriptor options + WORD VendorId; // 0x0403 + WORD ProductId; // 0x6001 + UCHAR SerNumEnable; // non-zero if serial number to be used + // Config descriptor options + WORD MaxPower; // 0 < MaxPower <= 500 + UCHAR SelfPowered; // 0 = bus powered, 1 = self powered + UCHAR RemoteWakeup; // 0 = not capable, 1 = capable + // Hardware options + UCHAR PullDownEnable; // non-zero if pull down in suspend enabled + } FT_EEPROM_HEADER, *PFT_EEPROM_HEADER; + + + // FT232B EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_232b { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + } FT_EEPROM_232B, *PFT_EEPROM_232B; + + + // FT2232 EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_2232 { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR AIsHighCurrent; // non-zero if interface is high current + UCHAR BIsHighCurrent; // non-zero if interface is high current + // Hardware options + UCHAR AIsFifo; // non-zero if interface is 245 FIFO + UCHAR AIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR AIsFastSer; // non-zero if interface is Fast serial + UCHAR BIsFifo; // non-zero if interface is 245 FIFO + UCHAR BIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR BIsFastSer; // non-zero if interface is Fast serial + // Driver option + UCHAR ADriverType; // non-zero if interface is to use VCP drivers + UCHAR BDriverType; // non-zero if interface is to use VCP drivers + } FT_EEPROM_2232, *PFT_EEPROM_2232; + + + // FT232R EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_232r { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR IsHighCurrent; // non-zero if interface is high current + // Hardware options + UCHAR UseExtOsc; // Use External Oscillator + UCHAR InvertTXD; // non-zero if invert TXD + UCHAR InvertRXD; // non-zero if invert RXD + UCHAR InvertRTS; // non-zero if invert RTS + UCHAR InvertCTS; // non-zero if invert CTS + UCHAR InvertDTR; // non-zero if invert DTR + UCHAR InvertDSR; // non-zero if invert DSR + UCHAR InvertDCD; // non-zero if invert DCD + UCHAR InvertRI; // non-zero if invert RI + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + // Driver option + UCHAR DriverType; // non-zero if using D2XX driver + } FT_EEPROM_232R, *PFT_EEPROM_232R; + + + // FT2232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_2232h { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ALSlowSlew; // non-zero if AL pins have slow slew + UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input + UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR AHSlowSlew; // non-zero if AH pins have slow slew + UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input + UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BLSlowSlew; // non-zero if BL pins have slow slew + UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input + UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BHSlowSlew; // non-zero if BH pins have slow slew + UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input + UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // Hardware options + UCHAR AIsFifo; // non-zero if interface is 245 FIFO + UCHAR AIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR AIsFastSer; // non-zero if interface is Fast serial + UCHAR BIsFifo; // non-zero if interface is 245 FIFO + UCHAR BIsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR BIsFastSer; // non-zero if interface is Fast serial + UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs + // Driver option + UCHAR ADriverType; // non-zero if interface is to use VCP drivers + UCHAR BDriverType; // non-zero if interface is to use VCP drivers + } FT_EEPROM_2232H, *PFT_EEPROM_2232H; + + + // FT4232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_4232h { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ASlowSlew; // non-zero if A pins have slow slew + UCHAR ASchmittInput; // non-zero if A pins are Schmitt input + UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR BSlowSlew; // non-zero if B pins have slow slew + UCHAR BSchmittInput; // non-zero if B pins are Schmitt input + UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR CSlowSlew; // non-zero if C pins have slow slew + UCHAR CSchmittInput; // non-zero if C pins are Schmitt input + UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR DSlowSlew; // non-zero if D pins have slow slew + UCHAR DSchmittInput; // non-zero if D pins are Schmitt input + UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // Hardware options + UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN + UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN + UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN + UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN + // Driver option + UCHAR ADriverType; // non-zero if interface is to use VCP drivers + UCHAR BDriverType; // non-zero if interface is to use VCP drivers + UCHAR CDriverType; // non-zero if interface is to use VCP drivers + UCHAR DDriverType; // non-zero if interface is to use VCP drivers + } FT_EEPROM_4232H, *PFT_EEPROM_4232H; + + + // FT232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_232h { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ACSlowSlew; // non-zero if AC bus pins have slow slew + UCHAR ACSchmittInput; // non-zero if AC bus pins are Schmitt input + UCHAR ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ADSlowSlew; // non-zero if AD bus pins have slow slew + UCHAR ADSchmittInput; // non-zero if AD bus pins are Schmitt input + UCHAR ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // CBUS options + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + UCHAR Cbus5; // Cbus Mux control + UCHAR Cbus6; // Cbus Mux control + UCHAR Cbus7; // Cbus Mux control + UCHAR Cbus8; // Cbus Mux control + UCHAR Cbus9; // Cbus Mux control + // FT1248 options + UCHAR FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) + UCHAR FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) + UCHAR FT1248FlowControl; // FT1248 flow control enable + // Hardware options + UCHAR IsFifo; // non-zero if interface is 245 FIFO + UCHAR IsFifoTar; // non-zero if interface is 245 FIFO CPU target + UCHAR IsFastSer; // non-zero if interface is Fast serial + UCHAR IsFT1248; // non-zero if interface is FT1248 + UCHAR PowerSaveEnable; // + // Driver option + UCHAR DriverType; // non-zero if interface is to use VCP drivers + } FT_EEPROM_232H, *PFT_EEPROM_232H; + + + // FT X Series EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_x_series { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + // Drive options + UCHAR ACSlowSlew; // non-zero if AC bus pins have slow slew + UCHAR ACSchmittInput; // non-zero if AC bus pins are Schmitt input + UCHAR ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR ADSlowSlew; // non-zero if AD bus pins have slow slew + UCHAR ADSchmittInput; // non-zero if AD bus pins are Schmitt input + UCHAR ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA + // CBUS options + UCHAR Cbus0; // Cbus Mux control + UCHAR Cbus1; // Cbus Mux control + UCHAR Cbus2; // Cbus Mux control + UCHAR Cbus3; // Cbus Mux control + UCHAR Cbus4; // Cbus Mux control + UCHAR Cbus5; // Cbus Mux control + UCHAR Cbus6; // Cbus Mux control + // UART signal options + UCHAR InvertTXD; // non-zero if invert TXD + UCHAR InvertRXD; // non-zero if invert RXD + UCHAR InvertRTS; // non-zero if invert RTS + UCHAR InvertCTS; // non-zero if invert CTS + UCHAR InvertDTR; // non-zero if invert DTR + UCHAR InvertDSR; // non-zero if invert DSR + UCHAR InvertDCD; // non-zero if invert DCD + UCHAR InvertRI; // non-zero if invert RI + // Battery Charge Detect options + UCHAR BCDEnable; // Enable Battery Charger Detection + UCHAR BCDForceCbusPWREN; // asserts the power enable signal on CBUS when charging port detected + UCHAR BCDDisableSleep; // forces the device never to go into sleep mode + // I2C options + WORD I2CSlaveAddress; // I2C slave device address + DWORD I2CDeviceId; // I2C device ID + UCHAR I2CDisableSchmitt; // Disable I2C Schmitt trigger + // FT1248 options + UCHAR FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) + UCHAR FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) + UCHAR FT1248FlowControl; // FT1248 flow control enable + // Hardware options + UCHAR RS485EchoSuppress; // + UCHAR PowerSaveEnable; // + // Driver option + UCHAR DriverType; // non-zero if interface is to use VCP drivers + } FT_EEPROM_X_SERIES, *PFT_EEPROM_X_SERIES; + + + // FT4222H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + typedef struct ft_eeprom_4222h { + // Common header + FT_EEPROM_HEADER common; // common elements for all device EEPROMs + CHAR Revision; // 'A', 'B', 'C', or 'D'. + UCHAR I2C_Slave_Address; + // Suspend + UCHAR SPISuspend; // 0 for "Disable SPI, tristate pins", 2 for "Keep SPI pin status", 3 for "Enable SPI pin control" + UCHAR SuspendOutPol; // 0 for negative, 1 for positive (not implemented on Rev A) + UCHAR EnableSuspendOut; // non-zero to enable (not implemented on Rev A) + // QSPI + UCHAR Clock_SlowSlew; // non-zero if clock pin has slow slew + UCHAR Clock_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR IO0_SlowSlew; // non-zero if IO0 pin has slow slew + UCHAR IO1_SlowSlew; // non-zero if IO1 pin has slow slew + UCHAR IO2_SlowSlew; // non-zero if IO2 pin has slow slew + UCHAR IO3_SlowSlew; // non-zero if IO3 pin has slow slew + UCHAR IO_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR SlaveSelect_PullUp; // non-zero to enable pull up + UCHAR SlaveSelect_PullDown; // non-zero to enable pull down + UCHAR SlaveSelect_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR SlaveSelect_SlowSlew; // non-zero if slave select pin has slow slew + UCHAR MISO_Suspend; // 2 for push-low, 3 for push high, 0 and 1 reserved + UCHAR SIMO_Suspend; // 2 for push-low, 3 for push high, 0 and 1 reserved + UCHAR IO2_IO3_Suspend; // 2 for push-low, 3 for push high, 0 and 1 reserved + UCHAR SlaveSelect_Suspend; // 0 for no-change (not implemented on Rev A), 2 for push-low, 3 for push high, 1 reserved + // GPIO + UCHAR GPIO0_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR GPIO1_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR GPIO2_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR GPIO3_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + UCHAR GPIO0_SlowSlew; // non-zero if IO0 pin has slow slew + UCHAR GPIO1_SlowSlew; // non-zero if IO0 pin has slow slew + UCHAR GPIO2_SlowSlew; // non-zero if IO0 pin has slow slew + UCHAR GPIO3_SlowSlew; // non-zero if IO0 pin has slow slew + UCHAR GPIO0_PullDown; // non-zero to enable pull down + UCHAR GPIO1_PullDown; // non-zero to enable pull down + UCHAR GPIO2_PullDown; // non-zero to enable pull down + UCHAR GPIO3_PullDown; // non-zero to enable pull down + UCHAR GPIO0_PullUp; // non-zero to enable pull up + UCHAR GPIO1_PullUp; // non-zero to enable pull up + UCHAR GPIO2_PullUp; // non-zero to enable pull up + UCHAR GPIO3_PullUp; // non-zero to enable pull up + UCHAR GPIO0_OpenDrain; // non-zero to enable open drain + UCHAR GPIO1_OpenDrain; // non-zero to enable open drain + UCHAR GPIO2_OpenDrain; // non-zero to enable open drain + UCHAR GPIO3_OpenDrain; // non-zero to enable open drain + UCHAR GPIO0_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high + UCHAR GPIO1_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high + UCHAR GPIO2_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high + UCHAR GPIO3_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high + UCHAR FallingEdge; // non-zero to change GPIO on falling edge + // BCD + UCHAR BCD_Disable; // non-zero to disable BCD + UCHAR BCD_OutputActiveLow; // non-zero to set BCD output active low + UCHAR BCD_Drive; // valid values are 4mA, 8mA, 12mA, 16mA + } FT_EEPROM_4222H, *PFT_EEPROM_4222H; + + + // Power Delivery structures for use with FT_EEPROM_Read and FT_EEPROM_Program + // PDO Configuration structure, mA supported values 0 to 10230mA, mV supported values 0 to 51100mV + // This is part of the FT_EEPROM_PD structure. + typedef struct ft_eeprom_PD_PDO_mv_ma { + USHORT PDO1ma; // PDO1 mA + USHORT PDO1mv; // PDO1 mV + USHORT PDO2ma; // PDO2 mA + USHORT PDO2mv; // PDO2 mV + USHORT PDO3ma; // PDO3 mA + USHORT PDO3mv; // PDO3 mV + USHORT PDO4ma; // PDO4 mA + USHORT PDO4mv; // PDO4 mV + USHORT PDO5ma; // PDO5 mA (FTx233HP only) + USHORT PDO5mv; // PDO5 mV (FTx233HP only) + USHORT PDO6ma; // PDO6 mA (FTx233HP only) + USHORT PDO6mv; // PDO6 mV (FTx233HP only) + USHORT PDO7ma; // PDO7 mA (FTx233HP only) + USHORT PDO7mv; // PDO7 mV (FTx233HP only) + } FT_EEPROM_PD_PDO_mv_ma; + + // PD EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + // This is appended to the end of the base device structure. e_g. + // struct { + // FT_EEPROM_xxx base; + // FT_EEPROM_PD pd; + // }; + // Device GPIO values are: + // FTx233HP - 0 to 7, 15 for N/A + // FTx232HP - 0 to 3, 15 for N/A + typedef struct ft_eeprom_pd { + // Configuration + UCHAR srprs; // non-zero to enable Sink Request Power Role Swap + UCHAR sraprs; // non-zero to enable Sink Accept PR Swap + UCHAR srrprs; // non-zero to enable Source Request PR SWAP + UCHAR saprs; // non-zero to enable Source Accept PR SWAP + UCHAR vconns; // non-zero to enable vConn Swap + UCHAR passthru; // non-zero to enable Pass Through (FTx233HP only) + UCHAR extmcu; // non-zero to enable External MCU + UCHAR pd2en; // non-zero to enable PD2 (FTx233HP only) + UCHAR pd1autoclk; // non-zero to enable PD1 Auto Clock + UCHAR pd2autoclk; // non-zero to enable PD2 Auto Clock (FTx233HP only) + UCHAR useefuse; // non-zero to Use EFUSE + UCHAR extvconn; // non-zero to enable External vConn + + // GPIO Configuration + UCHAR count; // GPIO Count, supported values are 0 to 7 + UCHAR gpio1; // GPIO Number 1, supports device GPIO values + UCHAR gpio2; // GPIO Number 2, supports device GPIO values + UCHAR gpio3; // GPIO Number 3, supports device GPIO values + UCHAR gpio4; // GPIO Number 4, supports device GPIO values + UCHAR gpio5; // GPIO Number 5, supports device GPIO values (FTx233HP only) + UCHAR gpio6; // GPIO Number 6, supports device GPIO values (FTx233HP only) + UCHAR gpio7; // GPIO Number 7, supports device GPIO values (FTx233HP only) + UCHAR pd1lden; // PD1 Load Enable, supports device GPIO values + UCHAR pd2lden; // PD2 Load Enable, supports device GPIO values (FTx233HP only) + UCHAR dispin; // Discharge Pin, supports device GPIO values + UCHAR disenbm; // Discharge Enable BM, 0 for "Drive Hi", 1 for "Drive Low", 2 for "Input Mode", 3 for "Don't Care" + UCHAR disdisbm; // Discharge Disable BM, 0 for "Drive Hi", 1 for "Drive Low", 2 for "Input Mode", 3 for "Don't Care" + UCHAR ccselect; // CC Select Indicator, supports device GPIO values + + // ISET Configuration + UCHAR iset1; // ISET1, supports device GPIO values + UCHAR iset2; // ISET2, supports device GPIO values + UCHAR iset3; // ISET3, supports device GPIO values + UCHAR extiset; // non-zero to enable EXTEND_ISET + UCHAR isetpd2; // non-zero to enable ISET_PD2 + UCHAR iseten; // non-zero to set ISET_ENABLED + + // BM Configuration, 0 for "Drive Hi", 1 for "Drive Low", 2 for "Input Mode", 3 for "Don't Care" + UCHAR PDO1_GPIO[7]; // PDO1 GPIO1 to GPIO7 + UCHAR PDO2_GPIO[7]; // PDO2 GPIO1 to GPIO7 + UCHAR PDO3_GPIO[7]; // PDO3 GPIO1 to GPIO7 + UCHAR PDO4_GPIO[7]; // PDO4 GPIO1 to GPIO7 + UCHAR PDO5_GPIO[7]; // PDO5 GPIO1 to GPIO7 (FTx233HP only) + UCHAR PDO6_GPIO[7]; // PDO6 GPIO1 to GPIO7 (FTx233HP only) + UCHAR PDO7_GPIO[7]; // PDO7 GPIO1 to GPIO7 (FTx233HP only) + UCHAR VSET0V_GPIO[7]; // PDO7 GPIO1 to GPIO7 + UCHAR VSAFE5V_GPIO[7]; // PDO7 GPIO1 to GPIO7 + + FT_EEPROM_PD_PDO_mv_ma BM_PDO_Sink; + FT_EEPROM_PD_PDO_mv_ma BM_PDO_Source; + FT_EEPROM_PD_PDO_mv_ma BM_PDO_Sink_2; // (FTx233HP only) + + // PD Timers + UCHAR srt; // Sender Response Timer + UCHAR hrt; // Hard Reset Timer + UCHAR sct; // Source Capability Timer + UCHAR dit; // Discover Identity Timer + USHORT srcrt; // Source Recover Timer + USHORT trt; // Transition Timer + USHORT sofft; // Source off timer + USHORT nrt; // No Response Timer + USHORT swct; // Sink Wait Capability Timer + USHORT snkrt; // Sink Request Timer + UCHAR dt; // Discharge Timer + UCHAR cnst; // Chunk not supported timer + USHORT it; // Idle Timer + + // PD Control + UCHAR i2caddr; // I2C Address (hex) + UINT prou; // Power Reserved for OWN use + UINT trim1; // TRIM1 + UINT trim2; // TRIM2 + UCHAR extdc; // non-zero to enable ETERNAL_DC_POWER + } FT_EEPROM_PD, *PFT_EEPROM_PD; + + // FT2233HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + // FT2232H with power delivery + typedef struct _ft_eeprom_2233hp + { + FT_EEPROM_2232H ft2232h; + FT_EEPROM_PD pd; + } FT_EEPROM_2233HP, *PFT_EEPROM_2233HP; + + // FT4233HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + // FT4232H with power delivery + typedef struct _ft_eeprom_4233hp + { + FT_EEPROM_4232H ft4232h; + FT_EEPROM_PD pd; + } FT_EEPROM_4233HP, *PFT_EEPROM_4233HP; + + // FT2232HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + // FT2232H with power delivery + typedef struct _ft_eeprom_2232hp + { + FT_EEPROM_2232H ft2232h; + FT_EEPROM_PD pd; + } FT_EEPROM_2232HP, *PFT_EEPROM_2232HP; + + // FT4232HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + // FT4232H with power delivery + typedef struct _ft_eeprom_4232hp + { + FT_EEPROM_4232H ft4232h; + FT_EEPROM_PD pd; + } FT_EEPROM_4232HP, *PFT_EEPROM_4232HP; + + // FT233HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + // FT233H with power delivery + typedef struct _ft_eeprom_233hp + { + FT_EEPROM_232H ft232h; + FT_EEPROM_PD pd; + } FT_EEPROM_233HP, *PFT_EEPROM_233HP; + + // FT232HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program + // FT232H with power delivery + typedef struct _ft_eeprom_232hp + { + FT_EEPROM_232H ft232h; + FT_EEPROM_PD pd; + } FT_EEPROM_232HP, *PFT_EEPROM_232HP; + + FTD2XX_API + FT_STATUS WINAPI FT_EEPROM_Read( + FT_HANDLE ftHandle, + void *eepromData, + DWORD eepromDataSize, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + + FTD2XX_API + FT_STATUS WINAPI FT_EEPROM_Program( + FT_HANDLE ftHandle, + void *eepromData, + DWORD eepromDataSize, + char *Manufacturer, + char *ManufacturerId, + char *Description, + char *SerialNumber + ); + + + FTD2XX_API + FT_STATUS WINAPI FT_SetLatencyTimer( + FT_HANDLE ftHandle, + UCHAR ucLatency + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetLatencyTimer( + FT_HANDLE ftHandle, + PUCHAR pucLatency + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetBitMode( + FT_HANDLE ftHandle, + UCHAR ucMask, + UCHAR ucEnable + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetBitMode( + FT_HANDLE ftHandle, + PUCHAR pucMode + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetUSBParameters( + FT_HANDLE ftHandle, + ULONG ulInTransferSize, + ULONG ulOutTransferSize + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetDeadmanTimeout( + FT_HANDLE ftHandle, + ULONG ulDeadmanTimeout + ); + +#ifndef _WIN32 + // Extra functions for non-Windows platforms to compensate + // for lack of .INF file to specify Vendor and Product IDs. + + FTD2XX_API + FT_STATUS FT_SetVIDPID( + DWORD dwVID, + DWORD dwPID + ); + + FTD2XX_API + FT_STATUS FT_GetVIDPID( + DWORD * pdwVID, + DWORD * pdwPID + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceLocId( + FT_HANDLE ftHandle, + LPDWORD lpdwLocId + ); +#endif // _WIN32 + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfo( + FT_HANDLE ftHandle, + FT_DEVICE *lpftDevice, + LPDWORD lpdwID, + PCHAR SerialNumber, + PCHAR Description, + LPVOID Dummy + ); + + FTD2XX_API + FT_STATUS WINAPI FT_StopInTask( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_RestartInTask( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_SetResetPipeRetryCount( + FT_HANDLE ftHandle, + DWORD dwCount + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ResetPort( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_CyclePort( + FT_HANDLE ftHandle + ); + + + // + // Win32-type functions + // + + FTD2XX_API + FT_HANDLE WINAPI FT_W32_CreateFile( + LPCTSTR lpszName, + DWORD dwAccess, + DWORD dwShareMode, + LPSECURITY_ATTRIBUTES lpSecurityAttributes, + DWORD dwCreate, + DWORD dwAttrsAndFlags, + HANDLE hTemplate + ); + + FTD2XX_API + BOOL WINAPI FT_W32_CloseHandle( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_ReadFile( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD nBufferSize, + LPDWORD lpBytesReturned, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + BOOL WINAPI FT_W32_WriteFile( + FT_HANDLE ftHandle, + LPVOID lpBuffer, + DWORD nBufferSize, + LPDWORD lpBytesWritten, + LPOVERLAPPED lpOverlapped + ); + + FTD2XX_API + DWORD WINAPI FT_W32_GetLastError( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetOverlappedResult( + FT_HANDLE ftHandle, + LPOVERLAPPED lpOverlapped, + LPDWORD lpdwBytesTransferred, + BOOL bWait + ); + + FTD2XX_API + BOOL WINAPI FT_W32_CancelIo( + FT_HANDLE ftHandle + ); + + + // + // Win32 COMM API type functions + // + typedef struct _FTCOMSTAT { + DWORD fCtsHold : 1; + DWORD fDsrHold : 1; + DWORD fRlsdHold : 1; + DWORD fXoffHold : 1; + DWORD fXoffSent : 1; + DWORD fEof : 1; + DWORD fTxim : 1; + DWORD fReserved : 25; + DWORD cbInQue; + DWORD cbOutQue; + } FTCOMSTAT, *LPFTCOMSTAT; + + typedef struct _FTDCB { + DWORD DCBlength; /* sizeof(FTDCB) */ + DWORD BaudRate; /* Baudrate at which running */ + DWORD fBinary : 1; /* Binary Mode (skip EOF check) */ + DWORD fParity : 1; /* Enable parity checking */ + DWORD fOutxCtsFlow : 1; /* CTS handshaking on output */ + DWORD fOutxDsrFlow : 1; /* DSR handshaking on output */ + DWORD fDtrControl : 2; /* DTR Flow control */ + DWORD fDsrSensitivity : 1; /* DSR Sensitivity */ + DWORD fTXContinueOnXoff : 1; /* Continue TX when Xoff sent */ + DWORD fOutX : 1; /* Enable output X-ON/X-OFF */ + DWORD fInX : 1; /* Enable input X-ON/X-OFF */ + DWORD fErrorChar : 1; /* Enable Err Replacement */ + DWORD fNull : 1; /* Enable Null stripping */ + DWORD fRtsControl : 2; /* Rts Flow control */ + DWORD fAbortOnError : 1; /* Abort all reads and writes on Error */ + DWORD fDummy2 : 17; /* Reserved */ + WORD wReserved; /* Not currently used */ + WORD XonLim; /* Transmit X-ON threshold */ + WORD XoffLim; /* Transmit X-OFF threshold */ + BYTE ByteSize; /* Number of bits/byte, 4-8 */ + BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */ + BYTE StopBits; /* FT_STOP_BITS_1 or FT_STOP_BITS_2 */ + char XonChar; /* Tx and Rx X-ON character */ + char XoffChar; /* Tx and Rx X-OFF character */ + char ErrorChar; /* Error replacement char */ + char EofChar; /* End of Input character */ + char EvtChar; /* Received Event character */ + WORD wReserved1; /* Fill for now. */ + } FTDCB, *LPFTDCB; + + typedef struct _FTTIMEOUTS { + DWORD ReadIntervalTimeout; /* Maximum time between read chars. */ + DWORD ReadTotalTimeoutMultiplier; /* Multiplier of characters. */ + DWORD ReadTotalTimeoutConstant; /* Constant in milliseconds. */ + DWORD WriteTotalTimeoutMultiplier; /* Multiplier of characters. */ + DWORD WriteTotalTimeoutConstant; /* Constant in milliseconds. */ + } FTTIMEOUTS, *LPFTTIMEOUTS; + + + FTD2XX_API + BOOL WINAPI FT_W32_ClearCommBreak( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_ClearCommError( + FT_HANDLE ftHandle, + LPDWORD lpdwErrors, + LPFTCOMSTAT lpftComstat + ); + + FTD2XX_API + BOOL WINAPI FT_W32_EscapeCommFunction( + FT_HANDLE ftHandle, + DWORD dwFunc + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommModemStatus( + FT_HANDLE ftHandle, + LPDWORD lpdwModemStatus + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommState( + FT_HANDLE ftHandle, + LPFTDCB lpftDcb + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommTimeouts( + FT_HANDLE ftHandle, + FTTIMEOUTS *pTimeouts + ); + + FTD2XX_API + BOOL WINAPI FT_W32_PurgeComm( + FT_HANDLE ftHandle, + DWORD dwMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommBreak( + FT_HANDLE ftHandle + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommMask( + FT_HANDLE ftHandle, + ULONG ulEventMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_GetCommMask( + FT_HANDLE ftHandle, + LPDWORD lpdwEventMask + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommState( + FT_HANDLE ftHandle, + LPFTDCB lpftDcb + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetCommTimeouts( + FT_HANDLE ftHandle, + FTTIMEOUTS *pTimeouts + ); + + FTD2XX_API + BOOL WINAPI FT_W32_SetupComm( + FT_HANDLE ftHandle, + DWORD dwReadBufferSize, + DWORD dwWriteBufferSize + ); + + FTD2XX_API + BOOL WINAPI FT_W32_WaitCommEvent( + FT_HANDLE ftHandle, + PULONG pulEvent, + LPOVERLAPPED lpOverlapped + ); + + + // + // Device information + // + + typedef struct _ft_device_list_info_node { + ULONG Flags; + ULONG Type; + ULONG ID; + DWORD LocId; + char SerialNumber[16]; + char Description[64]; + FT_HANDLE ftHandle; + } FT_DEVICE_LIST_INFO_NODE; + + // Device information flags + enum { + FT_FLAGS_OPENED = 1, + FT_FLAGS_HISPEED = 2 + }; + + + FTD2XX_API + FT_STATUS WINAPI FT_CreateDeviceInfoList( + LPDWORD lpdwNumDevs + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfoList( + FT_DEVICE_LIST_INFO_NODE *pDest, + LPDWORD lpdwNumDevs + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetDeviceInfoDetail( + DWORD dwIndex, + LPDWORD lpdwFlags, + LPDWORD lpdwType, + LPDWORD lpdwID, + LPDWORD lpdwLocId, + LPVOID lpSerialNumber, + LPVOID lpDescription, + FT_HANDLE *pftHandle + ); + + + // + // Version information + // + + FTD2XX_API + FT_STATUS WINAPI FT_GetDriverVersion( + FT_HANDLE ftHandle, + LPDWORD lpdwVersion + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetLibraryVersion( + LPDWORD lpdwVersion + ); + + + FTD2XX_API + FT_STATUS WINAPI FT_Rescan( + void + ); + + FTD2XX_API + FT_STATUS WINAPI FT_Reload( + WORD wVid, + WORD wPid + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetComPortNumber( + FT_HANDLE ftHandle, + LPLONG lpdwComPortNumber + ); + + + // + // FT232H additional EEPROM functions + // + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadConfig( + FT_HANDLE ftHandle, + UCHAR ucAddress, + PUCHAR pucValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_WriteConfig( + FT_HANDLE ftHandle, + UCHAR ucAddress, + UCHAR ucValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_EE_ReadECC( + FT_HANDLE ftHandle, + UCHAR ucOption, + LPWORD lpwValue + ); + + FTD2XX_API + FT_STATUS WINAPI FT_GetQueueStatusEx( + FT_HANDLE ftHandle, + DWORD *dwRxBytes + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ComPortIdle( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_ComPortCancelIdle( + FT_HANDLE ftHandle + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdGet( + FT_HANDLE ftHandle, + UCHAR Request, + UCHAR *Buf, + USHORT Len + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdSet( + FT_HANDLE ftHandle, + UCHAR Request, + UCHAR *Buf, + USHORT Len + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdGetEx( + FT_HANDLE ftHandle, + USHORT wValue, + UCHAR *Buf, + USHORT Len + ); + + FTD2XX_API + FT_STATUS WINAPI FT_VendorCmdSetEx( + FT_HANDLE ftHandle, + USHORT wValue, + UCHAR *Buf, + USHORT Len + ); + +#ifdef __cplusplus +} +#endif + + +#endif /* FTD2XX_H */ \ No newline at end of file diff --git a/hardware/third-party/ftdi/dmxSender.cpp b/hardware/third-party/ftdi/dmxSender.cpp deleted file mode 100644 index 20a6995..0000000 --- a/hardware/third-party/ftdi/dmxSender.cpp +++ /dev/null @@ -1,142 +0,0 @@ -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "ftd2xx.h" - -#define DMX_START_CODE 0x00 -#define BREAK_DURATION_US 110 -#define MAB_DURATION_US 16 -#define DMX_CHANNELS 512 -#define FREQUENCY 44 -#define INTERVAL (1000000 / FREQUENCY) - -std::atomic dmxData[DMX_CHANNELS + 1]; -std::atomic isOutputActivated = false; - -using namespace std; - -void sendBreak(FT_HANDLE ftHandle) { - FT_SetBreakOn(ftHandle); // Envoie le signal de BREAK - std::this_thread::sleep_for(std::chrono::microseconds(BREAK_DURATION_US)); - FT_SetBreakOff(ftHandle); // Arrête le signal de BREAK -} - -void sendDMX(FT_HANDLE ftHandle) { - while (true) { - if(isOutputActivated){ - // Envoi du BREAK suivi du MAB - sendBreak(ftHandle); - std::this_thread::sleep_for(std::chrono::microseconds(MAB_DURATION_US)); - - // Envoi de la trame DMX512 - DWORD bytesWritten = 0; - - // Envoyer la trame DMX512 - FT_STATUS status = FT_Write(ftHandle, dmxData, DMX_CHANNELS, &bytesWritten); - if (status != FT_OK || bytesWritten != DMX_CHANNELS) { - std::cerr << "Unable to send the DMX frame" << std::endl; - FT_Close(ftHandle); - return; - } - - // Attendre avant d'envoyer la prochaine trame - std::this_thread::sleep_for(std::chrono::microseconds(INTERVAL - BREAK_DURATION_US - MAB_DURATION_US)); - } - } -} - -void processCommand(const char* buffer) { - if (buffer[0] == 0x01) { - // Activate the DMX512 - isOutputActivated.store(true); - } else if(buffer[0] == 0x02) { - // Deactivate the DMX512 - isOutputActivated.store(false); - } else if(buffer[0] == 0x03) { - // Get the channel number - uint16_t channelNumber = (static_cast(buffer[1]) | - (static_cast(buffer[2]) << 8)); - // Get the channel value - uint8_t channelValue = static_cast(buffer[3]); - // // Update the DMX array - dmxData[channelNumber].store(channelValue); - } else if(buffer[0] == 0x04) { - // Close this sender - exit(0); - } else { - std::cerr << "Unknown command" << endl; - } -} - -// Entry point -int main(int argc, char* argv[]) { - #ifdef _WIN32 - _setmode(_fileno(stdin), _O_BINARY); - #endif - - FT_STATUS ftStatus; - FT_HANDLE ftHandle = nullptr; - - // Check if the serial port is specified - if (argc != 2) { - std::cerr << "Invalid call to DMX sender" << std::endl; - return 1; - } - - // Connect the serial port - int deviceDev; - try { - deviceDev = std::stoi(argv[1]); - }catch(const std::exception& e){ - std::cerr << "Invalid call to DMX sender" << std::endl; - return 1; - } - - ftStatus = FT_Open(deviceDev, &ftHandle); - if (ftStatus != FT_OK) { - std::cerr << "Unable to open the FTDI device" << std::endl; - return 1; - } - - ftStatus = FT_SetBaudRate(ftHandle, 250000); - ftStatus |= FT_SetDataCharacteristics(ftHandle, 8, FT_STOP_BITS_2, FT_PARITY_NONE); // 8 bits, pas de parité, 1 bit de stop - ftStatus |= FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0); - if (ftStatus != FT_OK) { - std::cerr << "Unable to configure the FTDI device" << std::endl; - FT_Close(ftHandle); - return 1; - } - - // Send the DMX frames - std::thread updateThread(sendDMX, ftHandle); - - // Intercept commands from the GO program - char buffer[4]; // Tampon pour stocker les 4 octets d'une commande - - while (true) { - std::cin.read(buffer, 4); // Attente bloquante jusqu'à ce que 4 octets soient lus - if (std::cin.gcount() == 4) { // Vérifier que 4 octets ont été lus - processCommand(buffer); - } else if (std::cin.eof()) { - std::cerr << "Fin de l'entrée standard (EOF)" << std::endl; - break; - } else if (std::cin.fail()) { - std::cerr << "Erreur de lecture sur stdin" << std::endl; - break; - } - } - return 0; -} \ No newline at end of file diff --git a/hardware/third-party/ftdi/dmxSender.manifest b/hardware/third-party/ftdi/dmxSender.manifest deleted file mode 100644 index c87b039..0000000 --- a/hardware/third-party/ftdi/dmxSender.manifest +++ /dev/null @@ -1,12 +0,0 @@ - - - - DMXSender - - - - - - - - \ No newline at end of file diff --git a/hardware/third-party/ftdi/generate.bat b/hardware/third-party/ftdi/generate.bat deleted file mode 100644 index 000979c..0000000 --- a/hardware/third-party/ftdi/generate.bat +++ /dev/null @@ -1,8 +0,0 @@ -windres dmxSender.rc dmxSender.o -windres detectFTDI.rc detectFTDI.o - -g++ -o dmxSender.exe dmxSender.cpp dmxSender.o -I"include" -L"lib" -lftd2xx -mwindows -g++ -o detectFTDI.exe detectFTDI.cpp detectFTDI.o -I"include" -L"lib" -lftd2xx -mwindows - -@REM g++ -o dmxSender.exe dmxSender.cpp -I"include" -L"lib" -lftd2xx -@REM g++ -o detectFTDI.exe detectFTDI.cpp -I"include" -L"lib" -lftd2xx \ No newline at end of file diff --git a/wails.json b/wails.json index b3676fe..279d20c 100644 --- a/wails.json +++ b/wails.json @@ -1,7 +1,7 @@ { "$schema": "https://wails.io/schemas/config.v2.json", "name": "dmxconnect", - "outputfilename": "dmxconnect", + "outputfilename": "dmxconnect.exe", "frontend:install": "npm install", "frontend:build": "npm run build", "frontend:dev:watcher": "npm run dev", From abcc3e0b5e6b67fb14bcb889531ce36d6405c077 Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sat, 1 Nov 2025 12:23:22 +0100 Subject: [PATCH 6/7] resolved: activating/deactivating peripherals --- hardware/FTDIFinder.go | 170 +- hardware/FTDIPeripheral.go | 96 +- hardware/cpp/generate.bat | 7 +- hardware/cpp/include/detectFTDIBridge.h | 19 + hardware/cpp/include/dmxSenderBridge.h | 23 +- hardware/cpp/src/detectFTDI.cpp | 91 + hardware/cpp/src/detectFTDI.h | 14 + hardware/cpp/src/dmxSender.cpp | 149 +- hardware/cpp/src/dmxSender.h | 28 +- hardware/cpp/test/detectFTDI_test.cpp | 14 + hardware/cpp/test/dmxSender_test.cpp | 122 ++ hardware/interfaces.go | 5 +- hardware/third-party/ftdi/detectFTDI.cpp | 44 - hardware/third-party/ftdi/detectFTDI.manifest | 12 - hardware/third-party/ftdi/include/ftd2xx.h | 1667 ----------------- hardware/third-party/ftdi/lib/ftd2xx.lib | Bin 20260 -> 0 bytes peripherals.go | 2 +- 17 files changed, 559 insertions(+), 1904 deletions(-) create mode 100644 hardware/cpp/include/detectFTDIBridge.h create mode 100644 hardware/cpp/src/detectFTDI.cpp create mode 100644 hardware/cpp/src/detectFTDI.h create mode 100644 hardware/cpp/test/detectFTDI_test.cpp create mode 100644 hardware/cpp/test/dmxSender_test.cpp delete mode 100644 hardware/third-party/ftdi/detectFTDI.cpp delete mode 100644 hardware/third-party/ftdi/detectFTDI.manifest delete mode 100644 hardware/third-party/ftdi/include/ftd2xx.h delete mode 100644 hardware/third-party/ftdi/lib/ftd2xx.lib diff --git a/hardware/FTDIFinder.go b/hardware/FTDIFinder.go index 966ba0e..fea6588 100644 --- a/hardware/FTDIFinder.go +++ b/hardware/FTDIFinder.go @@ -1,34 +1,31 @@ package hardware import ( - "bufio" "context" _ "embed" "fmt" - "os" - "os/exec" - "path/filepath" goRuntime "runtime" - "strconv" - "strings" "sync" "time" + "unsafe" "github.com/rs/zerolog/log" - "github.com/wailsapp/wails/v2/pkg/runtime" ) -const ( - ftdiFinderExecutableName = "FTDI_finder.exe" -) +/* +#include +#cgo LDFLAGS: -L${SRCDIR}/../build/bin -ldetectFTDI +#include "cpp/include/detectFTDIBridge.h" +*/ +import "C" // FTDIFinder represents how the protocol is defined type FTDIFinder struct { - findTicker time.Ticker // Peripherals find ticker - foundPeripherals map[string]PeripheralInfo // The list of peripherals handled by this finder - registeredPeripherals map[string]FTDIPeripheral // The list of found peripherals - scanChannel chan struct{} // The channel to trigger a scan event - goWait sync.WaitGroup // Check goroutines execution + findTicker time.Ticker // Peripherals find ticker + foundPeripherals map[string]PeripheralInfo // The list of peripherals handled by this finder + registeredPeripherals map[string]*FTDIPeripheral // The list of found peripherals + scanChannel chan struct{} // The channel to trigger a scan event + goWait sync.WaitGroup // Check goroutines execution } // NewFTDIFinder creates a new FTDI finder @@ -37,7 +34,7 @@ func NewFTDIFinder(findPeriod time.Duration) *FTDIFinder { return &FTDIFinder{ findTicker: *time.NewTicker(findPeriod), foundPeripherals: make(map[string]PeripheralInfo), - registeredPeripherals: make(map[string]FTDIPeripheral), + registeredPeripherals: make(map[string]*FTDIPeripheral), scanChannel: make(chan struct{}), } } @@ -48,16 +45,33 @@ func (f *FTDIFinder) RegisterPeripheral(ctx context.Context, peripheralData Peri if err != nil { return "", fmt.Errorf("unable to create the FTDI peripheral: %v", err) } - f.registeredPeripherals[peripheralData.SerialNumber] = *ftdiPeripheral + f.registeredPeripherals[peripheralData.SerialNumber] = ftdiPeripheral log.Trace().Any("periph", &ftdiPeripheral).Str("file", "FTDIFinder").Str("peripheralName", peripheralData.Name).Msg("FTDI peripheral has been created") + // Peripheral created, connect it + err = ftdiPeripheral.Connect(ctx) + if err != nil { + log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to connect the peripheral") + } + // Peripheral connected, activate it + err = ftdiPeripheral.Activate(ctx) + if err != nil { + log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralData.SerialNumber).Msg("unable to activate the peripheral") + } + // Peripheral activated return peripheralData.SerialNumber, nil } // UnregisterPeripheral unregisters an existing peripheral -func (f *FTDIFinder) UnregisterPeripheral(peripheralID string) error { +func (f *FTDIFinder) UnregisterPeripheral(ctx context.Context, peripheralID string) error { peripheral, registered := f.registeredPeripherals[peripheralID] if registered { - err := peripheral.Disconnect() + // Deactivating peripheral + err := peripheral.Deactivate(ctx) + if err != nil { + return err + } + // Disconnecting peripheral + err = peripheral.Disconnect(ctx) if err != nil { return err } @@ -66,9 +80,6 @@ func (f *FTDIFinder) UnregisterPeripheral(peripheralID string) error { return nil } -//go:embed third-party/ftdi/detectFTDI.exe -var finderExe []byte - // Initialize initializes the FTDI finder func (f *FTDIFinder) Initialize() error { // Check platform @@ -76,34 +87,10 @@ func (f *FTDIFinder) Initialize() error { log.Error().Str("file", "FTDIFinder").Str("platform", goRuntime.GOOS).Msg("FTDI finder not compatible with your platform") return fmt.Errorf("the FTDI finder is not compatible with your platform yet (%s)", goRuntime.GOOS) } - // Create the FTDI executables - err := createExecutable(ftdiFinderExecutableName, finderExe) - if err != nil { - return err - } log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder initialized") return nil } -// createExecutable creates and writes an executable to the temporary directory of the system -func createExecutable(fileName string, storedFile []byte) error { - tempFile, err := os.Create(filepath.Join(os.TempDir(), fileName)) - if err != nil { - log.Err(err).Str("file", "FTDIFinder").Str("fileName", fileName).Msg("unable to create an FTDI executable") - return err - } - log.Trace().Str("file", "FTDIFinder").Str("filePath", tempFile.Name()).Msg("FTDI executable created") - - // Write the embedded executable to the temp file - if _, err := tempFile.Write(storedFile); err != nil { - log.Err(err).Str("file", "FTDIFinder").Str("fileName", fileName).Msg("unable to write the content to an FTDI executable") - return err - } - tempFile.Close() - log.Trace().Str("file", "FTDIPeripheral").Str("fileName", fileName).Msg("FTDI executable written") - return nil -} - // Start starts the finder and search for peripherals func (f *FTDIFinder) Start(ctx context.Context) error { f.goWait.Add(1) @@ -143,12 +130,6 @@ func (f *FTDIFinder) Stop() error { f.goWait.Wait() // Stop the ticker f.findTicker.Stop() - // Delete the FTDI executable files - fileToDelete := filepath.Join(os.TempDir(), ftdiFinderExecutableName) - err := os.Remove(fileToDelete) - if err != nil { - log.Warn().Str("file", "FTDIFinder").Str("fileName", fileToDelete).AnErr("error", err).Msg("unable to remove the executable file") - } log.Trace().Str("file", "FTDIFinder").Msg("FTDI finder stopped") return nil } @@ -184,78 +165,49 @@ func (f *FTDIFinder) SetPeripheralSettings(peripheralID string, settings map[str // scanPeripherals scans the FTDI peripherals func (f *FTDIFinder) scanPeripherals(ctx context.Context) error { - detectionCtx, cancel := context.WithCancel(ctx) - defer cancel() - log.Trace().Str("file", "FTDIFinder").Msg("FTDI scan triggered") - finder := exec.CommandContext(detectionCtx, filepath.Join(os.TempDir(), ftdiFinderExecutableName)) - log.Trace().Str("file", "FTDIFinder").Msg("has executed the FIND executable") + count := int(C.get_peripherals_number()) - stdout, err := finder.StdoutPipe() - if err != nil { - return fmt.Errorf("unable to create the stdout pipe: %s", err) - } - defer stdout.Close() + log.Info().Int("number", count).Msg("number of FTDI devices connected") - stderr, err := finder.StderrPipe() - if err != nil { - return fmt.Errorf("unable to create the stderr pipe: %s", err) - } - defer stderr.Close() + // Alloue un tableau de structures côté C + size := C.size_t(count) * C.size_t(unsafe.Sizeof(C.FTDIPeripheralC{})) + devicesPtr := C.malloc(size) + defer C.free(devicesPtr) - err = finder.Start() - if err != nil { - return fmt.Errorf("unable to find FTDI peripherals: %s", err) + devices := (*[1 << 30]C.FTDIPeripheralC)(devicesPtr)[:count:count] + + type device struct { + SerialNumber string + Description string + IsOpen bool } - scannerErr := bufio.NewScanner(stderr) - for scannerErr.Scan() { - return fmt.Errorf("unable to find FTDI peripherals: %s", scannerErr.Text()) - } + C.get_ftdi_devices((*C.FTDIPeripheralC)(devicesPtr), C.int(count)) temporaryPeripherals := make(map[string]PeripheralInfo) - scanner := bufio.NewScanner(stdout) - for scanner.Scan() { - peripheralString := scanner.Text() - // The program output is like '0:1:2:3' where 0 is the location, 1 is the S/N, 2 is the name and 3 is the open flag [O/C] - peripheralInfo := strings.Split(peripheralString, ":") + for i := 0; i < count; i++ { + d := devices[i] - log.Trace().Str("file", "FTDIFinder").Str("scannedString", peripheralString).Str("peripheralOpenFlag", peripheralInfo[3]).Str("peripheralName", peripheralInfo[2]).Str("peripheralSN", peripheralInfo[1]).Msg("new FTDI peripheral detected") - // Convert the location to an integer - location, err := strconv.Atoi(peripheralInfo[0]) - if err != nil { - log.Warn().Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("no location provided for this FTDI peripheral") - location = -1 - } - // Add the peripheral info to the found list - temporaryPeripherals[peripheralInfo[1]] = PeripheralInfo{ - Name: peripheralInfo[2], - SerialNumber: peripheralInfo[1], - IsOpen: peripheralInfo[3] == "O", + sn := C.GoString(d.serialNumber) + desc := C.GoString(d.description) + isOpen := d.isOpen != 0 + + temporaryPeripherals[sn] = PeripheralInfo{ + SerialNumber: sn, + Name: desc, + IsOpen: isOpen, ProtocolName: "FTDI", } - // If this peripheral is already registered, connect it and activate it - peripheral, registered := f.registeredPeripherals[peripheralInfo[1]] - if registered { - runtime.EventsEmit(ctx, string(PeripheralStatus), peripheral.info, "connecting") - err := peripheral.Connect(ctx, location) - if err != nil { - log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralInfo[1]).Msg("unable to connect the peripheral") - } - runtime.EventsEmit(ctx, string(PeripheralStatus), peripheral.info, "deactivated") - time.Sleep(2 * time.Second) - err = peripheral.Activate(ctx) - if err != nil { - log.Err(err).Str("file", "FTDIFinder").Str("peripheralSN", peripheralInfo[1]).Msg("unable to activate the peripheral") - } - runtime.EventsEmit(ctx, string(PeripheralStatus), peripheral.info, "activated") - } - - log.Trace().Any("periph", temporaryPeripherals).Str("file", "FTDIFinder").Str("peripheralName", peripheralInfo[2]).Msg("successfully added the FTDI peripheral to the finder") + // Libération mémoire allouée côté C + C.free_ftdi_device(&d) } + + log.Info().Any("peripherals", temporaryPeripherals).Msg("available FTDI peripherals") + // Emit the peripherals changes to the front emitPeripheralsChanges(ctx, f.foundPeripherals, temporaryPeripherals) // Store the new peripherals list diff --git a/hardware/FTDIPeripheral.go b/hardware/FTDIPeripheral.go index daa7657..a69e523 100644 --- a/hardware/FTDIPeripheral.go +++ b/hardware/FTDIPeripheral.go @@ -3,12 +3,13 @@ package hardware import ( "context" _ "embed" - "io" + "sync" "unsafe" "github.com/pkg/errors" "github.com/rs/zerolog/log" + "github.com/wailsapp/wails/v2/pkg/runtime" ) /* @@ -19,14 +20,11 @@ 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 + info PeripheralInfo // The peripheral basic data + settings map[string]interface{} // The settings of the peripheral + dmxSender unsafe.Pointer // The command object for piloting the DMX ouptut + waitGroup sync.WaitGroup // Waitgroup to wait goroutines + isConnected bool // If the FTDI is connected or not } // NewFTDIPeripheral creates a new FTDI peripheral @@ -34,57 +32,83 @@ 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), + info: info, + dmxSender: nil, + settings: settings, + isConnected: false, }, nil } // Connect connects the FTDI peripheral -func (p *FTDIPeripheral) Connect(ctx context.Context, location int) error { +func (p *FTDIPeripheral) Connect(ctx context.Context) error { + runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "connecting") + // Check if the device has already been created - if p.dmxDevice == nil { - return errors.Errorf("the DMX device has not been created!") + if p.dmxSender != nil { + return errors.Errorf("the DMX device has already been created!") } + // Create the DMX sender + p.dmxSender = C.dmx_create() + // 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") + err := C.dmx_connect(p.dmxSender, C.CString(p.info.SerialNumber)) + if err != C.DMX_OK { + log.Error().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Any("err", err).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 + p.waitGroup.Add(1) + go func() { + defer p.waitGroup.Done() + <-ctx.Done() + p.Disconnect(ctx) + }() + + p.isConnected = true + runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "deactivated") 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 { +func (p *FTDIPeripheral) Disconnect(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!") + if p.dmxSender == nil { + return errors.Errorf("the DMX device has not been connected!") } - //TODO: What actions for disconnecting the DMX device? - + // Destroy the dmx sender + C.dmx_destroy(p.dmxSender) + p.isConnected = false return nil } +// IsConnected returns if the FTDI is connected or not +func (p *FTDIPeripheral) IsConnected() bool { + return p.isConnected +} + // 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!") + if p.dmxSender == nil { + return errors.Errorf("the DMX sender has not been created!") } log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("activating FTDI peripheral...") - C.dmx_activate(p.dmxDevice) + err := C.dmx_activate(p.dmxSender) + if err != C.DMX_OK { + return errors.Errorf("unable to activate the DMX sender!") + } + + C.dmx_setValue(p.dmxSender, C.uint16_t(1), C.uint8_t(255)) + C.dmx_setValue(p.dmxSender, C.uint16_t(5), C.uint8_t(255)) + + runtime.EventsEmit(ctx, string(PeripheralStatus), p.info, "activated") log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device activated successfully") @@ -94,13 +118,16 @@ func (p *FTDIPeripheral) Activate(ctx context.Context) error { // 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 { + if p.dmxSender == 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) + err := C.dmx_deactivate(p.dmxSender) + if err != C.DMX_OK { + return errors.Errorf("unable to deactivate the DMX sender!") + } log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("DMX device deactivated successfully") @@ -116,13 +143,16 @@ func (p *FTDIPeripheral) SetSettings(settings map[string]interface{}) error { // 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 { + if p.dmxSender == 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)) + err := C.dmx_setValue(p.dmxSender, C.uint16_t(channelNumber), C.uint8_t(channelValue)) + if err != C.DMX_OK { + return errors.Errorf("unable to update the channel value!") + } log.Trace().Str("file", "FTDIPeripheral").Str("s/n", p.info.SerialNumber).Msg("device property set on FTDI peripheral successfully") diff --git a/hardware/cpp/generate.bat b/hardware/cpp/generate.bat index a7613fb..4deba9c 100644 --- a/hardware/cpp/generate.bat +++ b/hardware/cpp/generate.bat @@ -13,9 +13,10 @@ @REM g++ -shared -o ../../build/bin/libdmxSender.dll src/dmxSender.cpp -fPIC -Wl,--out-implib,../../build/bin/libdmxSender.dll.a -L"lib" -lftd2xx +@REM Compiling DETECTFTDI library +g++ -shared -o ../../build/bin/libdetectFTDI.dll src/detectFTDI.cpp -fPIC -Wl,--out-implib,../../build/bin/libdetectFTDI.dll.a -L"lib" -lftd2xx -mwindows + @REM Compiling DMXSENDER library - -g++ -shared -o ../../build/bin/libdmxSender.dll src/dmxSender.cpp -fPIC -L"lib" -lftd2xx - +g++ -shared -o ../../build/bin/libdmxSender.dll src/dmxSender.cpp -fPIC -Wl,--out-implib,../../build/bin/libdmxSender.dll.a -L"lib" -lftd2xx -mwindows @REM g++ -shared -o libdmxSender.so dmxSender.cpp -fPIC -I"include" -L"lib" -lftd2xx -mwindows \ No newline at end of file diff --git a/hardware/cpp/include/detectFTDIBridge.h b/hardware/cpp/include/detectFTDIBridge.h new file mode 100644 index 0000000..8cab84c --- /dev/null +++ b/hardware/cpp/include/detectFTDIBridge.h @@ -0,0 +1,19 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct { + char* serialNumber; + char* description; + int isOpen; +} FTDIPeripheralC; + +int get_peripherals_number(); +void get_ftdi_devices(FTDIPeripheralC* devices, int count); +void free_ftdi_device(FTDIPeripheralC* device); + +#ifdef __cplusplus +} +#endif \ No newline at end of file diff --git a/hardware/cpp/include/dmxSenderBridge.h b/hardware/cpp/include/dmxSenderBridge.h index 08b1991..1cc9ed4 100644 --- a/hardware/cpp/include/dmxSenderBridge.h +++ b/hardware/cpp/include/dmxSenderBridge.h @@ -1,15 +1,30 @@ // Declare the C++ function from the shared library +#include + +typedef enum { + DMX_OK, + DMX_CHANNEL_TOO_LOW_ERROR, + DMX_CHANNEL_TOO_HIGH_ERROR, + DMX_VALUE_TOO_LOW_ERROR, + DMX_VALUE_TOO_HIGH_ERROR, + DMX_OPEN_ERROR, + DMX_SET_BAUDRATE_ERROR, + DMX_SET_DATA_CHARACTERISTICS_ERROR, + DMX_SET_FLOW_ERROR, + DMX_UNKNOWN_ERROR +} DMXError; + typedef void DMXDevice; extern DMXDevice* dmx_create(); extern void* dmx_destroy(DMXDevice* dev); -extern bool dmx_connect(DMXDevice* dev); +extern DMXError dmx_connect(DMXDevice* dev, char* serialNumber); -extern void dmx_activate(DMXDevice* dev); +extern DMXError dmx_activate(DMXDevice* dev); -extern void dmx_deactivate(DMXDevice* dev); +extern DMXError dmx_deactivate(DMXDevice* dev); -extern void dmx_setValue(DMXDevice* dev, int channel, int value); \ No newline at end of file +extern DMXError dmx_setValue(DMXDevice* dev, uint16_t channel, uint8_t value); \ No newline at end of file diff --git a/hardware/cpp/src/detectFTDI.cpp b/hardware/cpp/src/detectFTDI.cpp new file mode 100644 index 0000000..a6266ee --- /dev/null +++ b/hardware/cpp/src/detectFTDI.cpp @@ -0,0 +1,91 @@ +#include "../include/detectFTDIBridge.h" +#include "detectFTDI.h" +#include +#include +#include +#include + +int getFTDIPeripheralsNumber() { + DWORD numDevs = 0; + if (FT_CreateDeviceInfoList(&numDevs) != FT_OK) { + std::cerr << "Unable to get FTDI devices: create list error\n"; + } + return numDevs; +} + +std::vector scanFTDIPeripherals() { + DWORD numDevs = 0; + if (FT_CreateDeviceInfoList(&numDevs) != FT_OK) { + std::cerr << "Unable to get FTDI devices: create list error\n"; + return {}; + } + + if (numDevs == 0) { + return {}; + } + + std::vector devInfo(numDevs); + if (FT_GetDeviceInfoList(devInfo.data(), &numDevs) != FT_OK) { + std::cerr << "Unable to get FTDI devices: get list error\n"; + return {}; + } + + std::vector peripherals; + peripherals.reserve(numDevs); + + for (const auto& info : devInfo) { + if (info.SerialNumber[0] != '\0') { + peripherals.push_back({ + info.SerialNumber, + info.Description, + static_cast(info.Flags & FT_FLAGS_OPENED) + }); + } + } + + return peripherals; +} + +extern "C" { + +int get_peripherals_number() { + return getFTDIPeripheralsNumber(); +} + +void get_ftdi_devices(FTDIPeripheralC* devices, int count) { + if (!devices || count <= 0) { + return; + } + + auto list = scanFTDIPeripherals(); + int n = std::min(count, static_cast(list.size())); + + for (int i = 0; i < n; ++i) { + const auto& src = list[i]; + auto& dst = devices[i]; + + dst.serialNumber = static_cast(std::malloc(src.serialNumber.size() + 1)); + std::strcpy(dst.serialNumber, src.serialNumber.c_str()); + + dst.description = static_cast(std::malloc(src.description.size() + 1)); + std::strcpy(dst.description, src.description.c_str()); + + dst.isOpen = src.isOpen ? 1 : 0; + } +} + +void free_ftdi_device(FTDIPeripheralC* device) { + if (!device) return; + + if (device->serialNumber) { + std::free(device->serialNumber); + device->serialNumber = nullptr; + } + + if (device->description) { + std::free(device->description); + device->description = nullptr; + } +} + +} // extern "C" \ No newline at end of file diff --git a/hardware/cpp/src/detectFTDI.h b/hardware/cpp/src/detectFTDI.h new file mode 100644 index 0000000..d22d4fb --- /dev/null +++ b/hardware/cpp/src/detectFTDI.h @@ -0,0 +1,14 @@ +#pragma once + +#include "ftd2xx.h" +#include +#include + +struct FTDIPeripheral { + std::string serialNumber; + std::string description; + bool isOpen; +}; + +int getFTDIPeripheralsNumber(); +std::vector scanFTDIPeripherals(); \ No newline at end of file diff --git a/hardware/cpp/src/dmxSender.cpp b/hardware/cpp/src/dmxSender.cpp index 96685f1..9f372ca 100644 --- a/hardware/cpp/src/dmxSender.cpp +++ b/hardware/cpp/src/dmxSender.cpp @@ -1,6 +1,7 @@ //dmxSender.cpp #include "dmxSender.h" +#include #define DMX_START_CODE 0x00 #define BREAK_DURATION_US 110 @@ -11,61 +12,132 @@ // Initialize default values for starting the DMX device DMXDevice::DMXDevice(){ + std::cout << " [DMXSENDER] " << "Creating a new DMXDevice..." << std::endl; ftHandle = nullptr; isOutputActivated = false; + resetChannels(); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); + std::cout << " [DMXSENDER] " << "DMXDevice created!" << std::endl; } // Properly close the DMX device DMXDevice::~DMXDevice(){ + std::cout << " [DMXSENDER] " << "Removing the DMXDevice..." << std::endl; + std::cout << " [DMXSENDER] " << "Deactivating the DMXDevice..." << std::endl; deactivate(); + std::cout << " [DMXSENDER] " << "DMXDevice deactivated!" << std::endl; + if (ftHandle != nullptr){ + std::cout << " [DMXSENDER] " << "ftHandle not null, closing it..." << std::endl; FT_Close(ftHandle); + std::cout << " [DMXSENDER] " << "FT_HANDLE closed!" << std::endl; ftHandle = nullptr; } + std::cout << " [DMXSENDER] " << "DMXDevice removed!" << std::endl; } // Connect the device on a specific port -bool DMXDevice::connect(int port){ - ftStatus = FT_Open(port, &ftHandle); +DMXError DMXDevice::connect(char* serialNumber){ + std::cout << " [DMXSENDER] " << "Connecting the DMXDevice..." << std::endl; + ftStatus = FT_OpenEx((PVOID)serialNumber, FT_OPEN_BY_SERIAL_NUMBER, &ftHandle); if (ftStatus != FT_OK) { - return true; + std::cout << " [DMXSENDER] " << "Error when connecting the DMXDevice..." << std::endl; + return DMX_OPEN_ERROR; } + std::cout << " [DMXSENDER] " << "DMXDevice connected, setting up..." << std::endl; + ftStatus = FT_SetBaudRate(ftHandle, 250000); + if (ftStatus != FT_OK) { + std::cout << " [DMXSENDER] " << "Error when setting the baudrate..." << std::endl; + FT_Close(ftHandle); + return DMX_SET_BAUDRATE_ERROR; + } ftStatus |= FT_SetDataCharacteristics(ftHandle, 8, FT_STOP_BITS_2, FT_PARITY_NONE); // 8 bits, no parity, 1 stop bit + if (ftStatus != FT_OK) { + std::cout << " [DMXSENDER] " << "Error when setting the data characteristics..." << std::endl; + FT_Close(ftHandle); + return DMX_SET_DATA_CHARACTERISTICS_ERROR; + } ftStatus |= FT_SetFlowControl(ftHandle, FT_FLOW_NONE, 0, 0); if (ftStatus != FT_OK) { + std::cout << " [DMXSENDER] " << "Error when trying to set up the flow control..." << std::endl; FT_Close(ftHandle); - return true; + return DMX_SET_FLOW_ERROR; } + std::cout << " [DMXSENDER] " << "DMXDevice set up!" << std::endl; + std::cout << " [DMXSENDER] " << "Preparing sending job..." << std::endl; + // Send the DMX frames std::thread updateThread([this]() { this->sendDMX(ftHandle); }); - return false; + + updateThread.detach(); + + std::cout << " [DMXSENDER] " << "Sending job executed!" << std::endl; + std::cout << " [DMXSENDER] " << "DMXDevice connected!" << std::endl; + return DMX_OK; } // Activate the DMX flow -void DMXDevice::activate(){ +DMXError DMXDevice::activate(){ + std::cout << " [DMXSENDER] " << "Activating the DMXDevice..." << std::endl; isOutputActivated.store(true); + std::cout << " [DMXSENDER] " << "DMXDevice activated!" << std::endl; + return DMX_OK; } // Deactivate the DMX flow -void DMXDevice::deactivate(){ +DMXError DMXDevice::deactivate(){ + std::cout << " [DMXSENDER] " << "Deactivating the DMXDevice..." << std::endl; + std::cout << " [DMXSENDER] " << "Resetting channels..." << std::endl; + resetChannels(); + std::cout << " [DMXSENDER] " << "Channels resetted!" << std::endl; isOutputActivated.store(false); + std::cout << " [DMXSENDER] " << "DMXDevice deactivated!" << std::endl; + return DMX_OK; } -// Set the value of a DMX channe -void DMXDevice::setValue(int channel, int value){ +// Set the value of a DMX channel +DMXError DMXDevice::setValue(uint16_t channel, uint8_t value){ + std::cout << " [DMXSENDER] " << "Setting a channel value..." << std::endl; + if (channel < 1) { + std::cout << " [DMXSENDER] " << "Unable to set channel value: channel number too low!" << std::endl; + return DMX_CHANNEL_TOO_LOW_ERROR; + } + if (channel > 512) { + std::cout << " [DMXSENDER] " << "Unable to set channel value: channel number too high!" << std::endl; + return DMX_CHANNEL_TOO_HIGH_ERROR; + } + if(value < 0) { + std::cout << " [DMXSENDER] " << "Unable to set channel value: channel value too low!" << std::endl; + return DMX_VALUE_TOO_LOW_ERROR; + } + if(value > 255) { + std::cout << " [DMXSENDER] " << "Unable to set channel value: channel value too high!" << std::endl; + return DMX_VALUE_TOO_HIGH_ERROR; + } dmxData[channel].store(value); + std::cout << " [DMXSENDER] " << "Channel value set!" << std::endl; + return DMX_OK; } // Send a break line -void DMXDevice::sendBreak(FT_HANDLE ftHandle) { - FT_SetBreakOn(ftHandle); // Set BREAK ON +FT_STATUS DMXDevice::sendBreak(FT_HANDLE ftHandle) { + ftStatus = FT_SetBreakOn(ftHandle); // Set BREAK ON + if (ftStatus != FT_OK) { + std::cout << " [DMXSENDER] " << "Unable to put break signal ON!" << std::endl; + return ftStatus; + } std::this_thread::sleep_for(std::chrono::microseconds(BREAK_DURATION_US)); - FT_SetBreakOff(ftHandle); // Set BREAK OFF + ftStatus = FT_SetBreakOff(ftHandle); // Set BREAK OFF + if (ftStatus != FT_OK) { + std::cout << " [DMXSENDER] " << "Unable to put break signal OFF!" << std::endl; + return ftStatus; + } + return ftStatus; } // Continuously send the DMX frame @@ -73,7 +145,12 @@ void DMXDevice::sendDMX(FT_HANDLE ftHandle) { while (true) { if(isOutputActivated){ // Send the BREAK - sendBreak(ftHandle); + ftStatus = sendBreak(ftHandle); + if (ftStatus != FT_OK) { + std::cout << " [DMXSENDER] " << "Unable to send break signal! Deactivating output..." << std::endl; + deactivate(); + continue; + } // Send the MAB std::this_thread::sleep_for(std::chrono::microseconds(MAB_DURATION_US)); @@ -81,8 +158,9 @@ void DMXDevice::sendDMX(FT_HANDLE ftHandle) { DWORD bytesWritten = 0; // Send the DMX frame - FT_STATUS status = FT_Write(ftHandle, dmxData, DMX_CHANNELS, &bytesWritten); - if (status != FT_OK || bytesWritten != DMX_CHANNELS) { // Error detected when trying to send the frame. Deactivate the line. + ftStatus = FT_Write(ftHandle, dmxData, DMX_CHANNELS, &bytesWritten); + if (ftStatus != FT_OK || bytesWritten != DMX_CHANNELS) { // Error detected when trying to send the frame. Deactivate the line. + std::cout << " [DMXSENDER] " << "Error when trying to send the DMX frame! Deactivating output..." << std::endl; deactivate(); continue; } @@ -93,6 +171,15 @@ void DMXDevice::sendDMX(FT_HANDLE ftHandle) { } } +// Resetting the DMX channels +void DMXDevice::resetChannels(){ + for (auto &v : dmxData) { + v.store(0); + } + dmxData[0].store(DMX_START_CODE); + std::this_thread::sleep_for(std::chrono::milliseconds(100)); +} + // Linkable functions from Golang extern "C" { // Create a new DMX device @@ -106,22 +193,38 @@ extern "C" { } // Connect a DMX device - bool dmx_connect(DMXDevice* dev, int port) { - return dev->connect(port); + DMXError dmx_connect(DMXDevice* dev, char* serialNumber) { + try{ + return dev->connect(serialNumber); + } catch (...) { + return DMX_UNKNOWN_ERROR; + } } // Activate a DMX device - void dmx_activate(DMXDevice* dev) { - dev->activate(); + DMXError dmx_activate(DMXDevice* dev) { + try{ + return dev->activate(); + } catch (...) { + return DMX_UNKNOWN_ERROR; + } } // Deactivate a DMX device - void dmx_deactivate(DMXDevice* dev) { - dev->deactivate(); + DMXError dmx_deactivate(DMXDevice* dev) { + try{ + return dev->activate(); + } catch (...) { + return DMX_UNKNOWN_ERROR; + } } // Set the channel value of a DMX device - void dmx_setValue(DMXDevice* dev, int channel, int value) { - dev->setValue(channel, value); + DMXError dmx_setValue(DMXDevice* dev, int channel, int value) { + try { + return dev->setValue(channel, value); + } catch (...) { + return DMX_UNKNOWN_ERROR; + } } } \ No newline at end of file diff --git a/hardware/cpp/src/dmxSender.h b/hardware/cpp/src/dmxSender.h index 70b7644..f0d3db1 100644 --- a/hardware/cpp/src/dmxSender.h +++ b/hardware/cpp/src/dmxSender.h @@ -15,6 +15,19 @@ #define FREQUENCY 44 #define INTERVAL (1000000 / FREQUENCY) +typedef enum { + DMX_OK, + DMX_CHANNEL_TOO_LOW_ERROR, + DMX_CHANNEL_TOO_HIGH_ERROR, + DMX_VALUE_TOO_LOW_ERROR, + DMX_VALUE_TOO_HIGH_ERROR, + DMX_OPEN_ERROR, + DMX_SET_BAUDRATE_ERROR, + DMX_SET_DATA_CHARACTERISTICS_ERROR, + DMX_SET_FLOW_ERROR, + DMX_UNKNOWN_ERROR +} DMXError; + class DMXDevice { public: // Initialize default values for starting the DMX device @@ -24,25 +37,28 @@ public: ~DMXDevice(); // Connect the device on a specific port - bool connect(int port); + DMXError connect(char* serialNumber); // Activate the DMX flow - void activate(); + DMXError activate(); // Deactivate the DMX flow - void deactivate(); + DMXError deactivate(); // Set the value of a DMX channel - void setValue(int channel, int value); + DMXError setValue(uint16_t channel, uint8_t value); + + // Resetting the DMX channels + void resetChannels(); private: FT_STATUS ftStatus; // FTDI peripheral status FT_HANDLE ftHandle = nullptr; // FTDI object - std::atomic dmxData[DMX_CHANNELS + 1]; // For storing dynamically the DMX data + std::atomic dmxData[DMX_CHANNELS + 1]; // For storing dynamically the DMX data std::atomic isOutputActivated = false; // Boolean to start/stop the DMX flow // Send a break line - void sendBreak(FT_HANDLE ftHandle); + FT_STATUS sendBreak(FT_HANDLE ftHandle); // Continuously send the DMX frame void sendDMX(FT_HANDLE ftHandle); diff --git a/hardware/cpp/test/detectFTDI_test.cpp b/hardware/cpp/test/detectFTDI_test.cpp new file mode 100644 index 0000000..181dded --- /dev/null +++ b/hardware/cpp/test/detectFTDI_test.cpp @@ -0,0 +1,14 @@ +#include "../src/detectFTDI.h" +#include + +int main(){ + int peripheralsNumber = getFTDIPeripheralsNumber(); + + + std::vector peripherals = scanFTDIPeripherals(); + + // for (const auto& peripheral : peripherals) { + // std::cout << peripheral.serialNumber << " (" << peripheral.description << ") -> IS OPEN: " << peripheral.isOpen << std::endl; + // } + +} \ No newline at end of file diff --git a/hardware/cpp/test/dmxSender_test.cpp b/hardware/cpp/test/dmxSender_test.cpp new file mode 100644 index 0000000..12e4e77 --- /dev/null +++ b/hardware/cpp/test/dmxSender_test.cpp @@ -0,0 +1,122 @@ +#include "../src/dmxSender.h" + +#include +#include +#include + +int main(){ + std::cout << "Debugging application DMXSENDER" << std::endl; + + DMXDevice* dev = nullptr; + try { + dev = new DMXDevice(); + } + catch(const std::exception &e){ + std::cout << "Unable to create a DMX device: " << e.what() << std::endl; + } + + if (!dev) { + std::cout << "Device not created, aborting." << std::endl; + return 1; + } + + try { + bool err = dev->connect(0); + + if (err == true) { + delete dev; + return 1; + } + } + catch (const std::exception &e){ + std::cout << "Unable to connect" << e.what() << std::endl; + delete dev; + return 1; + } + + try{ + dev->activate(); + } + catch(const std::exception &e){ + std::cout << "Unable to activate" << e.what() << std::endl; + delete dev; + return 1; + } + + try{ + dev->setValue(1, 100); + dev->setValue(2, 255); + } + catch(const std::exception &e){ + std::cout << "Unable to activate" << e.what() << std::endl; + delete dev; + return 1; + } + + Sleep(500); + + try{ + dev->setValue(2, 127); + dev->setValue(3, 255); + } + catch(const std::exception &e){ + std::cout << "Unable to activate" << e.what() << std::endl; + delete dev; + return 1; + } + + Sleep(500); + + try{ + dev->setValue(3, 127); + dev->setValue(4, 255); + } + catch(const std::exception &e){ + std::cout << "Unable to activate" << e.what() << std::endl; + delete dev; + return 1; + } + + Sleep(500); + + + try{ + dev->setValue(2, 0); + dev->setValue(3, 0); + dev->setValue(4, 0); + dev->setValue(5, 255); + } + catch(const std::exception &e){ + std::cout << "Unable to activate" << e.what() << std::endl; + delete dev; + return 1; + } + + Sleep(5000); + + + // try{ + // dev->setValue(3, 255); + // } + // catch(const std::exception &e){ + // std::cout << "Unable to activate" << e.what() << std::endl; + // delete dev; + // return 1; + // } + + // Sleep(5000); + + // try{ + // dev->setValue(4, 255); + // } + // catch(const std::exception &e){ + // std::cout << "Unable to activate" << e.what() << std::endl; + // delete dev; + // return 1; + // } + + // Sleep(5000); + + delete dev; + return 0; +} \ No newline at end of file diff --git a/hardware/interfaces.go b/hardware/interfaces.go index 09f568f..ec415ba 100644 --- a/hardware/interfaces.go +++ b/hardware/interfaces.go @@ -5,7 +5,8 @@ import "context" // Peripheral represents the methods used to manage a peripheral (input or output hardware) type Peripheral interface { Connect(context.Context) error // Connect the peripheral - Disconnect() error // Disconnect the peripheral + IsConnected() bool // Return if the peripheral is connected or not + Disconnect(context.Context) error // Disconnect the peripheral Activate(context.Context) error // Activate the peripheral Deactivate(context.Context) error // Deactivate the peripheral SetSettings(map[string]interface{}) error // Set a peripheral setting @@ -31,7 +32,7 @@ type PeripheralFinder interface { Stop() error // Stop the detection ForceScan() // Explicitly scans for peripherals RegisterPeripheral(context.Context, PeripheralInfo) (string, error) // Registers a new peripheral data - UnregisterPeripheral(string) error // Unregisters an existing peripheral + UnregisterPeripheral(context.Context, string) error // Unregisters an existing peripheral GetPeripheralSettings(string) (map[string]interface{}, error) // Gets the peripheral settings SetPeripheralSettings(string, map[string]interface{}) error // Sets the peripheral settings GetName() string // Get the name of the finder diff --git a/hardware/third-party/ftdi/detectFTDI.cpp b/hardware/third-party/ftdi/detectFTDI.cpp deleted file mode 100644 index d5c271f..0000000 --- a/hardware/third-party/ftdi/detectFTDI.cpp +++ /dev/null @@ -1,44 +0,0 @@ -#include -#include -#include -#include "ftd2xx.h" - -int main() { - FT_STATUS ftStatus; - FT_HANDLE ftHandle = nullptr; - FT_DEVICE_LIST_INFO_NODE *devInfo; - DWORD numDevs; - - // create the device information list - ftStatus = FT_CreateDeviceInfoList(&numDevs); - if (ftStatus != FT_OK) { - std::cerr << "Unable to get the FTDI devices : create list error" << std::endl; - return 1; - } - if (numDevs > 0) { - // allocate storage for list based on numDevs - devInfo = - (FT_DEVICE_LIST_INFO_NODE*)malloc(sizeof(FT_DEVICE_LIST_INFO_NODE)*numDevs); - // get the device information list - ftStatus = FT_GetDeviceInfoList(devInfo, &numDevs); - if (ftStatus != FT_OK) { - std::cerr << "Unable to get the FTDI devices : get list error" << std::endl; - return 1; - } - - for (int i = 0; i < numDevs; i++) { - if (devInfo[i].SerialNumber[0] != '\0') { - std::cout << i << ":" << devInfo[i].SerialNumber << ":" << devInfo[i].Description << ":"; - - // Add information about the hardware open state - if (devInfo[i].Flags & FT_FLAGS_OPENED) { - std::cout << "O" << std::endl; - } else { - std::cout << "C" << std::endl; - } - } - } - - free(devInfo); - } -} \ No newline at end of file diff --git a/hardware/third-party/ftdi/detectFTDI.manifest b/hardware/third-party/ftdi/detectFTDI.manifest deleted file mode 100644 index a048981..0000000 --- a/hardware/third-party/ftdi/detectFTDI.manifest +++ /dev/null @@ -1,12 +0,0 @@ - - - - Detect FTDI - - - - - - - - \ No newline at end of file diff --git a/hardware/third-party/ftdi/include/ftd2xx.h b/hardware/third-party/ftdi/include/ftd2xx.h deleted file mode 100644 index 71bc79b..0000000 --- a/hardware/third-party/ftdi/include/ftd2xx.h +++ /dev/null @@ -1,1667 +0,0 @@ -/*++ - -Copyright © 2001-2021 Future Technology Devices International Limited - -THIS SOFTWARE IS PROVIDED BY FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES -OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL -FUTURE TECHNOLOGY DEVICES INTERNATIONAL LIMITED BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, -SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT -OF SUBSTITUTE GOODS OR SERVICES LOSS OF USE, DATA, OR PROFITS OR BUSINESS INTERRUPTION) -HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR -TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, -EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - -FTDI DRIVERS MAY BE USED ONLY IN CONJUNCTION WITH PRODUCTS BASED ON FTDI PARTS. - -FTDI DRIVERS MAY BE DISTRIBUTED IN ANY FORM AS LONG AS LICENSE INFORMATION IS NOT MODIFIED. - -IF A CUSTOM VENDOR ID AND/OR PRODUCT ID OR DESCRIPTION STRING ARE USED, IT IS THE -RESPONSIBILITY OF THE PRODUCT MANUFACTURER TO MAINTAIN ANY CHANGES AND SUBSEQUENT WHQL -RE-CERTIFICATION AS A RESULT OF MAKING THESE CHANGES. - - -Module Name: - -ftd2xx.h - -Abstract: - -Native USB device driver for FTDI FT232x, FT245x, FT2232x, FT4232x, FT2233H and FT4233H devices -FTD2XX library definitions - -Environment: - -kernel & user mode - - ---*/ - - -#ifndef FTD2XX_H -#define FTD2XX_H - -#ifdef _WIN32 -// Compiling on Windows -#include - -// The following ifdef block is the standard way of creating macros -// which make exporting from a DLL simpler. All files within this DLL -// are compiled with the FTD2XX_EXPORTS symbol defined on the command line. -// This symbol should not be defined on any project that uses this DLL. -// This way any other project whose source files include this file see -// FTD2XX_API functions as being imported from a DLL, whereas this DLL -// sees symbols defined with this macro as being exported. - -#ifdef FTD2XX_EXPORTS -#define FTD2XX_API __declspec(dllexport) -#elif defined(FTD2XX_STATIC) -// Avoid decorations when linking statically to D2XX. -#define FTD2XX_API -// Static D2XX depends on these Windows libs: -#pragma comment(lib, "setupapi.lib") -#pragma comment(lib, "advapi32.lib") -#pragma comment(lib, "user32.lib") -#else -#define FTD2XX_API __declspec(dllimport) -#endif - -#else // _WIN32 -// Compiling on non-Windows platform. -#include "WinTypes.h" -// No decorations needed. -#define FTD2XX_API - -#endif // _WIN32 - -typedef PVOID FT_HANDLE; -typedef ULONG FT_STATUS; - -// -// Device status -// -enum { - FT_OK, - FT_INVALID_HANDLE, - FT_DEVICE_NOT_FOUND, - FT_DEVICE_NOT_OPENED, - FT_IO_ERROR, - FT_INSUFFICIENT_RESOURCES, - FT_INVALID_PARAMETER, - FT_INVALID_BAUD_RATE, - - FT_DEVICE_NOT_OPENED_FOR_ERASE, - FT_DEVICE_NOT_OPENED_FOR_WRITE, - FT_FAILED_TO_WRITE_DEVICE, - FT_EEPROM_READ_FAILED, - FT_EEPROM_WRITE_FAILED, - FT_EEPROM_ERASE_FAILED, - FT_EEPROM_NOT_PRESENT, - FT_EEPROM_NOT_PROGRAMMED, - FT_INVALID_ARGS, - FT_NOT_SUPPORTED, - FT_OTHER_ERROR, - FT_DEVICE_LIST_NOT_READY, -}; - - -#define FT_SUCCESS(status) ((status) == FT_OK) - -// -// FT_OpenEx Flags -// - -#define FT_OPEN_BY_SERIAL_NUMBER 1 -#define FT_OPEN_BY_DESCRIPTION 2 -#define FT_OPEN_BY_LOCATION 4 - -#define FT_OPEN_MASK (FT_OPEN_BY_SERIAL_NUMBER | \ - FT_OPEN_BY_DESCRIPTION | \ - FT_OPEN_BY_LOCATION) - -// -// FT_ListDevices Flags (used in conjunction with FT_OpenEx Flags -// - -#define FT_LIST_NUMBER_ONLY 0x80000000 -#define FT_LIST_BY_INDEX 0x40000000 -#define FT_LIST_ALL 0x20000000 - -#define FT_LIST_MASK (FT_LIST_NUMBER_ONLY|FT_LIST_BY_INDEX|FT_LIST_ALL) - -// -// Baud Rates -// - -#define FT_BAUD_300 300 -#define FT_BAUD_600 600 -#define FT_BAUD_1200 1200 -#define FT_BAUD_2400 2400 -#define FT_BAUD_4800 4800 -#define FT_BAUD_9600 9600 -#define FT_BAUD_14400 14400 -#define FT_BAUD_19200 19200 -#define FT_BAUD_38400 38400 -#define FT_BAUD_57600 57600 -#define FT_BAUD_115200 115200 -#define FT_BAUD_230400 230400 -#define FT_BAUD_460800 460800 -#define FT_BAUD_921600 921600 - -// -// Word Lengths -// - -#define FT_BITS_8 (UCHAR) 8 -#define FT_BITS_7 (UCHAR) 7 - -// -// Stop Bits -// - -#define FT_STOP_BITS_1 (UCHAR) 0 -#define FT_STOP_BITS_2 (UCHAR) 2 - -// -// Parity -// - -#define FT_PARITY_NONE (UCHAR) 0 -#define FT_PARITY_ODD (UCHAR) 1 -#define FT_PARITY_EVEN (UCHAR) 2 -#define FT_PARITY_MARK (UCHAR) 3 -#define FT_PARITY_SPACE (UCHAR) 4 - -// -// Flow Control -// - -#define FT_FLOW_NONE 0x0000 -#define FT_FLOW_RTS_CTS 0x0100 -#define FT_FLOW_DTR_DSR 0x0200 -#define FT_FLOW_XON_XOFF 0x0400 - -// -// Purge rx and tx buffers -// -#define FT_PURGE_RX 1 -#define FT_PURGE_TX 2 - -// -// Events -// - -typedef void(*PFT_EVENT_HANDLER)(DWORD, DWORD); - -#define FT_EVENT_RXCHAR 1 -#define FT_EVENT_MODEM_STATUS 2 -#define FT_EVENT_LINE_STATUS 4 - -// -// Timeouts -// - -#define FT_DEFAULT_RX_TIMEOUT 300 -#define FT_DEFAULT_TX_TIMEOUT 300 - -// -// Device types -// - -typedef ULONG FT_DEVICE; - -enum { - FT_DEVICE_BM, - FT_DEVICE_AM, - FT_DEVICE_100AX, - FT_DEVICE_UNKNOWN, - FT_DEVICE_2232C, - FT_DEVICE_232R, - FT_DEVICE_2232H, - FT_DEVICE_4232H, - FT_DEVICE_232H, - FT_DEVICE_X_SERIES, - FT_DEVICE_4222H_0, - FT_DEVICE_4222H_1_2, - FT_DEVICE_4222H_3, - FT_DEVICE_4222_PROG, - FT_DEVICE_900, - FT_DEVICE_930, - FT_DEVICE_UMFTPD3A, - FT_DEVICE_2233HP, - FT_DEVICE_4233HP, - FT_DEVICE_2232HP, - FT_DEVICE_4232HP, - FT_DEVICE_233HP, - FT_DEVICE_232HP, - FT_DEVICE_2232HA, - FT_DEVICE_4232HA, - FT_DEVICE_232RN, -}; - -// -// Bit Modes -// - -#define FT_BITMODE_RESET 0x00 -#define FT_BITMODE_ASYNC_BITBANG 0x01 -#define FT_BITMODE_MPSSE 0x02 -#define FT_BITMODE_SYNC_BITBANG 0x04 -#define FT_BITMODE_MCU_HOST 0x08 -#define FT_BITMODE_FAST_SERIAL 0x10 -#define FT_BITMODE_CBUS_BITBANG 0x20 -#define FT_BITMODE_SYNC_FIFO 0x40 - -// -// FT232R CBUS Options EEPROM values -// - -#define FT_232R_CBUS_TXDEN 0x00 // Tx Data Enable -#define FT_232R_CBUS_PWRON 0x01 // Power On -#define FT_232R_CBUS_RXLED 0x02 // Rx LED -#define FT_232R_CBUS_TXLED 0x03 // Tx LED -#define FT_232R_CBUS_TXRXLED 0x04 // Tx and Rx LED -#define FT_232R_CBUS_SLEEP 0x05 // Sleep -#define FT_232R_CBUS_CLK48 0x06 // 48MHz clock -#define FT_232R_CBUS_CLK24 0x07 // 24MHz clock -#define FT_232R_CBUS_CLK12 0x08 // 12MHz clock -#define FT_232R_CBUS_CLK6 0x09 // 6MHz clock -#define FT_232R_CBUS_IOMODE 0x0A // IO Mode for CBUS bit-bang -#define FT_232R_CBUS_BITBANG_WR 0x0B // Bit-bang write strobe -#define FT_232R_CBUS_BITBANG_RD 0x0C // Bit-bang read strobe - -// -// FT232H CBUS Options EEPROM values -// - -#define FT_232H_CBUS_TRISTATE 0x00 // Tristate -#define FT_232H_CBUS_TXLED 0x01 // Tx LED -#define FT_232H_CBUS_RXLED 0x02 // Rx LED -#define FT_232H_CBUS_TXRXLED 0x03 // Tx and Rx LED -#define FT_232H_CBUS_PWREN 0x04 // Power Enable -#define FT_232H_CBUS_SLEEP 0x05 // Sleep -#define FT_232H_CBUS_DRIVE_0 0x06 // Drive pin to logic 0 -#define FT_232H_CBUS_DRIVE_1 0x07 // Drive pin to logic 1 -#define FT_232H_CBUS_IOMODE 0x08 // IO Mode for CBUS bit-bang -#define FT_232H_CBUS_TXDEN 0x09 // Tx Data Enable -#define FT_232H_CBUS_CLK30 0x0A // 30MHz clock -#define FT_232H_CBUS_CLK15 0x0B // 15MHz clock -#define FT_232H_CBUS_CLK7_5 0x0C // 7.5MHz clock - -// -// FT X Series CBUS Options EEPROM values -// - -#define FT_X_SERIES_CBUS_TRISTATE 0x00 // Tristate -#define FT_X_SERIES_CBUS_TXLED 0x01 // Tx LED -#define FT_X_SERIES_CBUS_RXLED 0x02 // Rx LED -#define FT_X_SERIES_CBUS_TXRXLED 0x03 // Tx and Rx LED -#define FT_X_SERIES_CBUS_PWREN 0x04 // Power Enable -#define FT_X_SERIES_CBUS_SLEEP 0x05 // Sleep -#define FT_X_SERIES_CBUS_DRIVE_0 0x06 // Drive pin to logic 0 -#define FT_X_SERIES_CBUS_DRIVE_1 0x07 // Drive pin to logic 1 -#define FT_X_SERIES_CBUS_IOMODE 0x08 // IO Mode for CBUS bit-bang -#define FT_X_SERIES_CBUS_TXDEN 0x09 // Tx Data Enable -#define FT_X_SERIES_CBUS_CLK24 0x0A // 24MHz clock -#define FT_X_SERIES_CBUS_CLK12 0x0B // 12MHz clock -#define FT_X_SERIES_CBUS_CLK6 0x0C // 6MHz clock -#define FT_X_SERIES_CBUS_BCD_CHARGER 0x0D // Battery charger detected -#define FT_X_SERIES_CBUS_BCD_CHARGER_N 0x0E // Battery charger detected inverted -#define FT_X_SERIES_CBUS_I2C_TXE 0x0F // I2C Tx empty -#define FT_X_SERIES_CBUS_I2C_RXF 0x10 // I2C Rx full -#define FT_X_SERIES_CBUS_VBUS_SENSE 0x11 // Detect VBUS -#define FT_X_SERIES_CBUS_BITBANG_WR 0x12 // Bit-bang write strobe -#define FT_X_SERIES_CBUS_BITBANG_RD 0x13 // Bit-bang read strobe -#define FT_X_SERIES_CBUS_TIMESTAMP 0x14 // Toggle output when a USB SOF token is received -#define FT_X_SERIES_CBUS_KEEP_AWAKE 0x15 // - - -// Driver types -#define FT_DRIVER_TYPE_D2XX 0 -#define FT_DRIVER_TYPE_VCP 1 - - - -#ifdef __cplusplus -extern "C" { -#endif - - -#ifdef FTD2XX_STATIC - FTD2XX_API - FT_STATUS WINAPI FT_Initialise( - void - ); - - FTD2XX_API - void WINAPI FT_Finalise( - void - ); -#endif // FTD2XX_STATIC - - FTD2XX_API - FT_STATUS WINAPI FT_Open( - int deviceNumber, - FT_HANDLE *pHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_OpenEx( - PVOID pArg1, - DWORD Flags, - FT_HANDLE *pHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ListDevices( - PVOID pArg1, - PVOID pArg2, - DWORD Flags - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Close( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Read( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD dwBytesToRead, - LPDWORD lpBytesReturned - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Write( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD dwBytesToWrite, - LPDWORD lpBytesWritten - ); - - FTD2XX_API - FT_STATUS WINAPI FT_IoCtl( - FT_HANDLE ftHandle, - DWORD dwIoControlCode, - LPVOID lpInBuf, - DWORD nInBufSize, - LPVOID lpOutBuf, - DWORD nOutBufSize, - LPDWORD lpBytesReturned, - LPOVERLAPPED lpOverlapped - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBaudRate( - FT_HANDLE ftHandle, - ULONG BaudRate - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDivisor( - FT_HANDLE ftHandle, - USHORT Divisor - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDataCharacteristics( - FT_HANDLE ftHandle, - UCHAR WordLength, - UCHAR StopBits, - UCHAR Parity - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetFlowControl( - FT_HANDLE ftHandle, - USHORT FlowControl, - UCHAR XonChar, - UCHAR XoffChar - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ResetDevice( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDtr( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ClrDtr( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetRts( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ClrRts( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetModemStatus( - FT_HANDLE ftHandle, - ULONG *pModemStatus - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetChars( - FT_HANDLE ftHandle, - UCHAR EventChar, - UCHAR EventCharEnabled, - UCHAR ErrorChar, - UCHAR ErrorCharEnabled - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Purge( - FT_HANDLE ftHandle, - ULONG Mask - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetTimeouts( - FT_HANDLE ftHandle, - ULONG ReadTimeout, - ULONG WriteTimeout - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetQueueStatus( - FT_HANDLE ftHandle, - DWORD *dwRxBytes - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetEventNotification( - FT_HANDLE ftHandle, - DWORD Mask, - PVOID Param - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetStatus( - FT_HANDLE ftHandle, - DWORD *dwRxBytes, - DWORD *dwTxBytes, - DWORD *dwEventDWord - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBreakOn( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBreakOff( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetWaitMask( - FT_HANDLE ftHandle, - DWORD Mask - ); - - FTD2XX_API - FT_STATUS WINAPI FT_WaitOnMask( - FT_HANDLE ftHandle, - DWORD *Mask - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetEventStatus( - FT_HANDLE ftHandle, - DWORD *dwEventDWord - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ReadEE( - FT_HANDLE ftHandle, - DWORD dwWordOffset, - LPWORD lpwValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_WriteEE( - FT_HANDLE ftHandle, - DWORD dwWordOffset, - WORD wValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EraseEE( - FT_HANDLE ftHandle - ); - - // - // structure to hold program data for FT_EE_Program, FT_EE_ProgramEx, FT_EE_Read - // and FT_EE_ReadEx functions - // - typedef struct ft_program_data { - - DWORD Signature1; // Header - must be 0x00000000 - DWORD Signature2; // Header - must be 0xffffffff - DWORD Version; // Header - FT_PROGRAM_DATA version - // 0 = original - // 1 = FT2232 extensions - // 2 = FT232R extensions - // 3 = FT2232H extensions - // 4 = FT4232H extensions - // 5 = FT232H extensions - - WORD VendorId; // 0x0403 - WORD ProductId; // 0x6001 - char *Manufacturer; // "FTDI" - char *ManufacturerId; // "FT" - char *Description; // "USB HS Serial Converter" - char *SerialNumber; // "FT000001" if fixed, or NULL - WORD MaxPower; // 0 < MaxPower <= 500 - WORD PnP; // 0 = disabled, 1 = enabled - WORD SelfPowered; // 0 = bus powered, 1 = self powered - WORD RemoteWakeup; // 0 = not capable, 1 = capable - // - // Rev4 (FT232B) extensions - // - UCHAR Rev4; // non-zero if Rev4 chip, zero otherwise - UCHAR IsoIn; // non-zero if in endpoint is isochronous - UCHAR IsoOut; // non-zero if out endpoint is isochronous - UCHAR PullDownEnable; // non-zero if pull down enabled - UCHAR SerNumEnable; // non-zero if serial number to be used - UCHAR USBVersionEnable; // non-zero if chip uses USBVersion - WORD USBVersion; // BCD (0x0200 => USB2) - // - // Rev 5 (FT2232) extensions - // - UCHAR Rev5; // non-zero if Rev5 chip, zero otherwise - UCHAR IsoInA; // non-zero if in endpoint is isochronous - UCHAR IsoInB; // non-zero if in endpoint is isochronous - UCHAR IsoOutA; // non-zero if out endpoint is isochronous - UCHAR IsoOutB; // non-zero if out endpoint is isochronous - UCHAR PullDownEnable5; // non-zero if pull down enabled - UCHAR SerNumEnable5; // non-zero if serial number to be used - UCHAR USBVersionEnable5; // non-zero if chip uses USBVersion - WORD USBVersion5; // BCD (0x0200 => USB2) - UCHAR AIsHighCurrent; // non-zero if interface is high current - UCHAR BIsHighCurrent; // non-zero if interface is high current - UCHAR IFAIsFifo; // non-zero if interface is 245 FIFO - UCHAR IFAIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR IFAIsFastSer; // non-zero if interface is Fast serial - UCHAR AIsVCP; // non-zero if interface is to use VCP drivers - UCHAR IFBIsFifo; // non-zero if interface is 245 FIFO - UCHAR IFBIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR IFBIsFastSer; // non-zero if interface is Fast serial - UCHAR BIsVCP; // non-zero if interface is to use VCP drivers - // - // Rev 6 (FT232R) extensions - // - UCHAR UseExtOsc; // Use External Oscillator - UCHAR HighDriveIOs; // High Drive I/Os - UCHAR EndpointSize; // Endpoint size - UCHAR PullDownEnableR; // non-zero if pull down enabled - UCHAR SerNumEnableR; // non-zero if serial number to be used - UCHAR InvertTXD; // non-zero if invert TXD - UCHAR InvertRXD; // non-zero if invert RXD - UCHAR InvertRTS; // non-zero if invert RTS - UCHAR InvertCTS; // non-zero if invert CTS - UCHAR InvertDTR; // non-zero if invert DTR - UCHAR InvertDSR; // non-zero if invert DSR - UCHAR InvertDCD; // non-zero if invert DCD - UCHAR InvertRI; // non-zero if invert RI - UCHAR Cbus0; // Cbus Mux control - UCHAR Cbus1; // Cbus Mux control - UCHAR Cbus2; // Cbus Mux control - UCHAR Cbus3; // Cbus Mux control - UCHAR Cbus4; // Cbus Mux control - UCHAR RIsD2XX; // non-zero if using D2XX driver - // - // Rev 7 (FT2232H) Extensions - // - UCHAR PullDownEnable7; // non-zero if pull down enabled - UCHAR SerNumEnable7; // non-zero if serial number to be used - UCHAR ALSlowSlew; // non-zero if AL pins have slow slew - UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input - UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR AHSlowSlew; // non-zero if AH pins have slow slew - UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input - UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BLSlowSlew; // non-zero if BL pins have slow slew - UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input - UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BHSlowSlew; // non-zero if BH pins have slow slew - UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input - UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR IFAIsFifo7; // non-zero if interface is 245 FIFO - UCHAR IFAIsFifoTar7; // non-zero if interface is 245 FIFO CPU target - UCHAR IFAIsFastSer7; // non-zero if interface is Fast serial - UCHAR AIsVCP7; // non-zero if interface is to use VCP drivers - UCHAR IFBIsFifo7; // non-zero if interface is 245 FIFO - UCHAR IFBIsFifoTar7; // non-zero if interface is 245 FIFO CPU target - UCHAR IFBIsFastSer7; // non-zero if interface is Fast serial - UCHAR BIsVCP7; // non-zero if interface is to use VCP drivers - UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs - // - // Rev 8 (FT4232H) Extensions - // - UCHAR PullDownEnable8; // non-zero if pull down enabled - UCHAR SerNumEnable8; // non-zero if serial number to be used - UCHAR ASlowSlew; // non-zero if A pins have slow slew - UCHAR ASchmittInput; // non-zero if A pins are Schmitt input - UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BSlowSlew; // non-zero if B pins have slow slew - UCHAR BSchmittInput; // non-zero if B pins are Schmitt input - UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR CSlowSlew; // non-zero if C pins have slow slew - UCHAR CSchmittInput; // non-zero if C pins are Schmitt input - UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR DSlowSlew; // non-zero if D pins have slow slew - UCHAR DSchmittInput; // non-zero if D pins are Schmitt input - UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN - UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN - UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN - UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN - UCHAR AIsVCP8; // non-zero if interface is to use VCP drivers - UCHAR BIsVCP8; // non-zero if interface is to use VCP drivers - UCHAR CIsVCP8; // non-zero if interface is to use VCP drivers - UCHAR DIsVCP8; // non-zero if interface is to use VCP drivers - // - // Rev 9 (FT232H) Extensions - // - UCHAR PullDownEnableH; // non-zero if pull down enabled - UCHAR SerNumEnableH; // non-zero if serial number to be used - UCHAR ACSlowSlewH; // non-zero if AC pins have slow slew - UCHAR ACSchmittInputH; // non-zero if AC pins are Schmitt input - UCHAR ACDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR ADSlowSlewH; // non-zero if AD pins have slow slew - UCHAR ADSchmittInputH; // non-zero if AD pins are Schmitt input - UCHAR ADDriveCurrentH; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR Cbus0H; // Cbus Mux control - UCHAR Cbus1H; // Cbus Mux control - UCHAR Cbus2H; // Cbus Mux control - UCHAR Cbus3H; // Cbus Mux control - UCHAR Cbus4H; // Cbus Mux control - UCHAR Cbus5H; // Cbus Mux control - UCHAR Cbus6H; // Cbus Mux control - UCHAR Cbus7H; // Cbus Mux control - UCHAR Cbus8H; // Cbus Mux control - UCHAR Cbus9H; // Cbus Mux control - UCHAR IsFifoH; // non-zero if interface is 245 FIFO - UCHAR IsFifoTarH; // non-zero if interface is 245 FIFO CPU target - UCHAR IsFastSerH; // non-zero if interface is Fast serial - UCHAR IsFT1248H; // non-zero if interface is FT1248 - UCHAR FT1248CpolH; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) - UCHAR FT1248LsbH; // FT1248 data is LSB (1) or MSB (0) - UCHAR FT1248FlowControlH; // FT1248 flow control enable - UCHAR IsVCPH; // non-zero if interface is to use VCP drivers - UCHAR PowerSaveEnableH; // non-zero if using ACBUS7 to save power for self-powered designs - - } FT_PROGRAM_DATA, *PFT_PROGRAM_DATA; - - FTD2XX_API - FT_STATUS WINAPI FT_EE_Program( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ProgramEx( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData, - char *Manufacturer, - char *ManufacturerId, - char *Description, - char *SerialNumber - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_Read( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ReadEx( - FT_HANDLE ftHandle, - PFT_PROGRAM_DATA pData, - char *Manufacturer, - char *ManufacturerId, - char *Description, - char *SerialNumber - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_UASize( - FT_HANDLE ftHandle, - LPDWORD lpdwSize - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_UAWrite( - FT_HANDLE ftHandle, - PUCHAR pucData, - DWORD dwDataLen - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_UARead( - FT_HANDLE ftHandle, - PUCHAR pucData, - DWORD dwDataLen, - LPDWORD lpdwBytesRead - ); - - - typedef struct ft_eeprom_header { - FT_DEVICE deviceType; // FTxxxx device type to be programmed - // Device descriptor options - WORD VendorId; // 0x0403 - WORD ProductId; // 0x6001 - UCHAR SerNumEnable; // non-zero if serial number to be used - // Config descriptor options - WORD MaxPower; // 0 < MaxPower <= 500 - UCHAR SelfPowered; // 0 = bus powered, 1 = self powered - UCHAR RemoteWakeup; // 0 = not capable, 1 = capable - // Hardware options - UCHAR PullDownEnable; // non-zero if pull down in suspend enabled - } FT_EEPROM_HEADER, *PFT_EEPROM_HEADER; - - - // FT232B EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_232b { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - } FT_EEPROM_232B, *PFT_EEPROM_232B; - - - // FT2232 EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_2232 { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - // Drive options - UCHAR AIsHighCurrent; // non-zero if interface is high current - UCHAR BIsHighCurrent; // non-zero if interface is high current - // Hardware options - UCHAR AIsFifo; // non-zero if interface is 245 FIFO - UCHAR AIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR AIsFastSer; // non-zero if interface is Fast serial - UCHAR BIsFifo; // non-zero if interface is 245 FIFO - UCHAR BIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR BIsFastSer; // non-zero if interface is Fast serial - // Driver option - UCHAR ADriverType; // non-zero if interface is to use VCP drivers - UCHAR BDriverType; // non-zero if interface is to use VCP drivers - } FT_EEPROM_2232, *PFT_EEPROM_2232; - - - // FT232R EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_232r { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - // Drive options - UCHAR IsHighCurrent; // non-zero if interface is high current - // Hardware options - UCHAR UseExtOsc; // Use External Oscillator - UCHAR InvertTXD; // non-zero if invert TXD - UCHAR InvertRXD; // non-zero if invert RXD - UCHAR InvertRTS; // non-zero if invert RTS - UCHAR InvertCTS; // non-zero if invert CTS - UCHAR InvertDTR; // non-zero if invert DTR - UCHAR InvertDSR; // non-zero if invert DSR - UCHAR InvertDCD; // non-zero if invert DCD - UCHAR InvertRI; // non-zero if invert RI - UCHAR Cbus0; // Cbus Mux control - UCHAR Cbus1; // Cbus Mux control - UCHAR Cbus2; // Cbus Mux control - UCHAR Cbus3; // Cbus Mux control - UCHAR Cbus4; // Cbus Mux control - // Driver option - UCHAR DriverType; // non-zero if using D2XX driver - } FT_EEPROM_232R, *PFT_EEPROM_232R; - - - // FT2232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_2232h { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - // Drive options - UCHAR ALSlowSlew; // non-zero if AL pins have slow slew - UCHAR ALSchmittInput; // non-zero if AL pins are Schmitt input - UCHAR ALDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR AHSlowSlew; // non-zero if AH pins have slow slew - UCHAR AHSchmittInput; // non-zero if AH pins are Schmitt input - UCHAR AHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BLSlowSlew; // non-zero if BL pins have slow slew - UCHAR BLSchmittInput; // non-zero if BL pins are Schmitt input - UCHAR BLDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BHSlowSlew; // non-zero if BH pins have slow slew - UCHAR BHSchmittInput; // non-zero if BH pins are Schmitt input - UCHAR BHDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - // Hardware options - UCHAR AIsFifo; // non-zero if interface is 245 FIFO - UCHAR AIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR AIsFastSer; // non-zero if interface is Fast serial - UCHAR BIsFifo; // non-zero if interface is 245 FIFO - UCHAR BIsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR BIsFastSer; // non-zero if interface is Fast serial - UCHAR PowerSaveEnable; // non-zero if using BCBUS7 to save power for self-powered designs - // Driver option - UCHAR ADriverType; // non-zero if interface is to use VCP drivers - UCHAR BDriverType; // non-zero if interface is to use VCP drivers - } FT_EEPROM_2232H, *PFT_EEPROM_2232H; - - - // FT4232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_4232h { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - // Drive options - UCHAR ASlowSlew; // non-zero if A pins have slow slew - UCHAR ASchmittInput; // non-zero if A pins are Schmitt input - UCHAR ADriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR BSlowSlew; // non-zero if B pins have slow slew - UCHAR BSchmittInput; // non-zero if B pins are Schmitt input - UCHAR BDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR CSlowSlew; // non-zero if C pins have slow slew - UCHAR CSchmittInput; // non-zero if C pins are Schmitt input - UCHAR CDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR DSlowSlew; // non-zero if D pins have slow slew - UCHAR DSchmittInput; // non-zero if D pins are Schmitt input - UCHAR DDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - // Hardware options - UCHAR ARIIsTXDEN; // non-zero if port A uses RI as RS485 TXDEN - UCHAR BRIIsTXDEN; // non-zero if port B uses RI as RS485 TXDEN - UCHAR CRIIsTXDEN; // non-zero if port C uses RI as RS485 TXDEN - UCHAR DRIIsTXDEN; // non-zero if port D uses RI as RS485 TXDEN - // Driver option - UCHAR ADriverType; // non-zero if interface is to use VCP drivers - UCHAR BDriverType; // non-zero if interface is to use VCP drivers - UCHAR CDriverType; // non-zero if interface is to use VCP drivers - UCHAR DDriverType; // non-zero if interface is to use VCP drivers - } FT_EEPROM_4232H, *PFT_EEPROM_4232H; - - - // FT232H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_232h { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - // Drive options - UCHAR ACSlowSlew; // non-zero if AC bus pins have slow slew - UCHAR ACSchmittInput; // non-zero if AC bus pins are Schmitt input - UCHAR ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR ADSlowSlew; // non-zero if AD bus pins have slow slew - UCHAR ADSchmittInput; // non-zero if AD bus pins are Schmitt input - UCHAR ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - // CBUS options - UCHAR Cbus0; // Cbus Mux control - UCHAR Cbus1; // Cbus Mux control - UCHAR Cbus2; // Cbus Mux control - UCHAR Cbus3; // Cbus Mux control - UCHAR Cbus4; // Cbus Mux control - UCHAR Cbus5; // Cbus Mux control - UCHAR Cbus6; // Cbus Mux control - UCHAR Cbus7; // Cbus Mux control - UCHAR Cbus8; // Cbus Mux control - UCHAR Cbus9; // Cbus Mux control - // FT1248 options - UCHAR FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) - UCHAR FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) - UCHAR FT1248FlowControl; // FT1248 flow control enable - // Hardware options - UCHAR IsFifo; // non-zero if interface is 245 FIFO - UCHAR IsFifoTar; // non-zero if interface is 245 FIFO CPU target - UCHAR IsFastSer; // non-zero if interface is Fast serial - UCHAR IsFT1248; // non-zero if interface is FT1248 - UCHAR PowerSaveEnable; // - // Driver option - UCHAR DriverType; // non-zero if interface is to use VCP drivers - } FT_EEPROM_232H, *PFT_EEPROM_232H; - - - // FT X Series EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_x_series { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - // Drive options - UCHAR ACSlowSlew; // non-zero if AC bus pins have slow slew - UCHAR ACSchmittInput; // non-zero if AC bus pins are Schmitt input - UCHAR ACDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR ADSlowSlew; // non-zero if AD bus pins have slow slew - UCHAR ADSchmittInput; // non-zero if AD bus pins are Schmitt input - UCHAR ADDriveCurrent; // valid values are 4mA, 8mA, 12mA, 16mA - // CBUS options - UCHAR Cbus0; // Cbus Mux control - UCHAR Cbus1; // Cbus Mux control - UCHAR Cbus2; // Cbus Mux control - UCHAR Cbus3; // Cbus Mux control - UCHAR Cbus4; // Cbus Mux control - UCHAR Cbus5; // Cbus Mux control - UCHAR Cbus6; // Cbus Mux control - // UART signal options - UCHAR InvertTXD; // non-zero if invert TXD - UCHAR InvertRXD; // non-zero if invert RXD - UCHAR InvertRTS; // non-zero if invert RTS - UCHAR InvertCTS; // non-zero if invert CTS - UCHAR InvertDTR; // non-zero if invert DTR - UCHAR InvertDSR; // non-zero if invert DSR - UCHAR InvertDCD; // non-zero if invert DCD - UCHAR InvertRI; // non-zero if invert RI - // Battery Charge Detect options - UCHAR BCDEnable; // Enable Battery Charger Detection - UCHAR BCDForceCbusPWREN; // asserts the power enable signal on CBUS when charging port detected - UCHAR BCDDisableSleep; // forces the device never to go into sleep mode - // I2C options - WORD I2CSlaveAddress; // I2C slave device address - DWORD I2CDeviceId; // I2C device ID - UCHAR I2CDisableSchmitt; // Disable I2C Schmitt trigger - // FT1248 options - UCHAR FT1248Cpol; // FT1248 clock polarity - clock idle high (1) or clock idle low (0) - UCHAR FT1248Lsb; // FT1248 data is LSB (1) or MSB (0) - UCHAR FT1248FlowControl; // FT1248 flow control enable - // Hardware options - UCHAR RS485EchoSuppress; // - UCHAR PowerSaveEnable; // - // Driver option - UCHAR DriverType; // non-zero if interface is to use VCP drivers - } FT_EEPROM_X_SERIES, *PFT_EEPROM_X_SERIES; - - - // FT4222H EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - typedef struct ft_eeprom_4222h { - // Common header - FT_EEPROM_HEADER common; // common elements for all device EEPROMs - CHAR Revision; // 'A', 'B', 'C', or 'D'. - UCHAR I2C_Slave_Address; - // Suspend - UCHAR SPISuspend; // 0 for "Disable SPI, tristate pins", 2 for "Keep SPI pin status", 3 for "Enable SPI pin control" - UCHAR SuspendOutPol; // 0 for negative, 1 for positive (not implemented on Rev A) - UCHAR EnableSuspendOut; // non-zero to enable (not implemented on Rev A) - // QSPI - UCHAR Clock_SlowSlew; // non-zero if clock pin has slow slew - UCHAR Clock_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR IO0_SlowSlew; // non-zero if IO0 pin has slow slew - UCHAR IO1_SlowSlew; // non-zero if IO1 pin has slow slew - UCHAR IO2_SlowSlew; // non-zero if IO2 pin has slow slew - UCHAR IO3_SlowSlew; // non-zero if IO3 pin has slow slew - UCHAR IO_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR SlaveSelect_PullUp; // non-zero to enable pull up - UCHAR SlaveSelect_PullDown; // non-zero to enable pull down - UCHAR SlaveSelect_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR SlaveSelect_SlowSlew; // non-zero if slave select pin has slow slew - UCHAR MISO_Suspend; // 2 for push-low, 3 for push high, 0 and 1 reserved - UCHAR SIMO_Suspend; // 2 for push-low, 3 for push high, 0 and 1 reserved - UCHAR IO2_IO3_Suspend; // 2 for push-low, 3 for push high, 0 and 1 reserved - UCHAR SlaveSelect_Suspend; // 0 for no-change (not implemented on Rev A), 2 for push-low, 3 for push high, 1 reserved - // GPIO - UCHAR GPIO0_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR GPIO1_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR GPIO2_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR GPIO3_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - UCHAR GPIO0_SlowSlew; // non-zero if IO0 pin has slow slew - UCHAR GPIO1_SlowSlew; // non-zero if IO0 pin has slow slew - UCHAR GPIO2_SlowSlew; // non-zero if IO0 pin has slow slew - UCHAR GPIO3_SlowSlew; // non-zero if IO0 pin has slow slew - UCHAR GPIO0_PullDown; // non-zero to enable pull down - UCHAR GPIO1_PullDown; // non-zero to enable pull down - UCHAR GPIO2_PullDown; // non-zero to enable pull down - UCHAR GPIO3_PullDown; // non-zero to enable pull down - UCHAR GPIO0_PullUp; // non-zero to enable pull up - UCHAR GPIO1_PullUp; // non-zero to enable pull up - UCHAR GPIO2_PullUp; // non-zero to enable pull up - UCHAR GPIO3_PullUp; // non-zero to enable pull up - UCHAR GPIO0_OpenDrain; // non-zero to enable open drain - UCHAR GPIO1_OpenDrain; // non-zero to enable open drain - UCHAR GPIO2_OpenDrain; // non-zero to enable open drain - UCHAR GPIO3_OpenDrain; // non-zero to enable open drain - UCHAR GPIO0_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high - UCHAR GPIO1_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high - UCHAR GPIO2_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high - UCHAR GPIO3_Suspend; // 0 for no-change, 1 for input (not implemented on Rev A), 2 for push-low, 3 for push high - UCHAR FallingEdge; // non-zero to change GPIO on falling edge - // BCD - UCHAR BCD_Disable; // non-zero to disable BCD - UCHAR BCD_OutputActiveLow; // non-zero to set BCD output active low - UCHAR BCD_Drive; // valid values are 4mA, 8mA, 12mA, 16mA - } FT_EEPROM_4222H, *PFT_EEPROM_4222H; - - - // Power Delivery structures for use with FT_EEPROM_Read and FT_EEPROM_Program - // PDO Configuration structure, mA supported values 0 to 10230mA, mV supported values 0 to 51100mV - // This is part of the FT_EEPROM_PD structure. - typedef struct ft_eeprom_PD_PDO_mv_ma { - USHORT PDO1ma; // PDO1 mA - USHORT PDO1mv; // PDO1 mV - USHORT PDO2ma; // PDO2 mA - USHORT PDO2mv; // PDO2 mV - USHORT PDO3ma; // PDO3 mA - USHORT PDO3mv; // PDO3 mV - USHORT PDO4ma; // PDO4 mA - USHORT PDO4mv; // PDO4 mV - USHORT PDO5ma; // PDO5 mA (FTx233HP only) - USHORT PDO5mv; // PDO5 mV (FTx233HP only) - USHORT PDO6ma; // PDO6 mA (FTx233HP only) - USHORT PDO6mv; // PDO6 mV (FTx233HP only) - USHORT PDO7ma; // PDO7 mA (FTx233HP only) - USHORT PDO7mv; // PDO7 mV (FTx233HP only) - } FT_EEPROM_PD_PDO_mv_ma; - - // PD EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - // This is appended to the end of the base device structure. e_g. - // struct { - // FT_EEPROM_xxx base; - // FT_EEPROM_PD pd; - // }; - // Device GPIO values are: - // FTx233HP - 0 to 7, 15 for N/A - // FTx232HP - 0 to 3, 15 for N/A - typedef struct ft_eeprom_pd { - // Configuration - UCHAR srprs; // non-zero to enable Sink Request Power Role Swap - UCHAR sraprs; // non-zero to enable Sink Accept PR Swap - UCHAR srrprs; // non-zero to enable Source Request PR SWAP - UCHAR saprs; // non-zero to enable Source Accept PR SWAP - UCHAR vconns; // non-zero to enable vConn Swap - UCHAR passthru; // non-zero to enable Pass Through (FTx233HP only) - UCHAR extmcu; // non-zero to enable External MCU - UCHAR pd2en; // non-zero to enable PD2 (FTx233HP only) - UCHAR pd1autoclk; // non-zero to enable PD1 Auto Clock - UCHAR pd2autoclk; // non-zero to enable PD2 Auto Clock (FTx233HP only) - UCHAR useefuse; // non-zero to Use EFUSE - UCHAR extvconn; // non-zero to enable External vConn - - // GPIO Configuration - UCHAR count; // GPIO Count, supported values are 0 to 7 - UCHAR gpio1; // GPIO Number 1, supports device GPIO values - UCHAR gpio2; // GPIO Number 2, supports device GPIO values - UCHAR gpio3; // GPIO Number 3, supports device GPIO values - UCHAR gpio4; // GPIO Number 4, supports device GPIO values - UCHAR gpio5; // GPIO Number 5, supports device GPIO values (FTx233HP only) - UCHAR gpio6; // GPIO Number 6, supports device GPIO values (FTx233HP only) - UCHAR gpio7; // GPIO Number 7, supports device GPIO values (FTx233HP only) - UCHAR pd1lden; // PD1 Load Enable, supports device GPIO values - UCHAR pd2lden; // PD2 Load Enable, supports device GPIO values (FTx233HP only) - UCHAR dispin; // Discharge Pin, supports device GPIO values - UCHAR disenbm; // Discharge Enable BM, 0 for "Drive Hi", 1 for "Drive Low", 2 for "Input Mode", 3 for "Don't Care" - UCHAR disdisbm; // Discharge Disable BM, 0 for "Drive Hi", 1 for "Drive Low", 2 for "Input Mode", 3 for "Don't Care" - UCHAR ccselect; // CC Select Indicator, supports device GPIO values - - // ISET Configuration - UCHAR iset1; // ISET1, supports device GPIO values - UCHAR iset2; // ISET2, supports device GPIO values - UCHAR iset3; // ISET3, supports device GPIO values - UCHAR extiset; // non-zero to enable EXTEND_ISET - UCHAR isetpd2; // non-zero to enable ISET_PD2 - UCHAR iseten; // non-zero to set ISET_ENABLED - - // BM Configuration, 0 for "Drive Hi", 1 for "Drive Low", 2 for "Input Mode", 3 for "Don't Care" - UCHAR PDO1_GPIO[7]; // PDO1 GPIO1 to GPIO7 - UCHAR PDO2_GPIO[7]; // PDO2 GPIO1 to GPIO7 - UCHAR PDO3_GPIO[7]; // PDO3 GPIO1 to GPIO7 - UCHAR PDO4_GPIO[7]; // PDO4 GPIO1 to GPIO7 - UCHAR PDO5_GPIO[7]; // PDO5 GPIO1 to GPIO7 (FTx233HP only) - UCHAR PDO6_GPIO[7]; // PDO6 GPIO1 to GPIO7 (FTx233HP only) - UCHAR PDO7_GPIO[7]; // PDO7 GPIO1 to GPIO7 (FTx233HP only) - UCHAR VSET0V_GPIO[7]; // PDO7 GPIO1 to GPIO7 - UCHAR VSAFE5V_GPIO[7]; // PDO7 GPIO1 to GPIO7 - - FT_EEPROM_PD_PDO_mv_ma BM_PDO_Sink; - FT_EEPROM_PD_PDO_mv_ma BM_PDO_Source; - FT_EEPROM_PD_PDO_mv_ma BM_PDO_Sink_2; // (FTx233HP only) - - // PD Timers - UCHAR srt; // Sender Response Timer - UCHAR hrt; // Hard Reset Timer - UCHAR sct; // Source Capability Timer - UCHAR dit; // Discover Identity Timer - USHORT srcrt; // Source Recover Timer - USHORT trt; // Transition Timer - USHORT sofft; // Source off timer - USHORT nrt; // No Response Timer - USHORT swct; // Sink Wait Capability Timer - USHORT snkrt; // Sink Request Timer - UCHAR dt; // Discharge Timer - UCHAR cnst; // Chunk not supported timer - USHORT it; // Idle Timer - - // PD Control - UCHAR i2caddr; // I2C Address (hex) - UINT prou; // Power Reserved for OWN use - UINT trim1; // TRIM1 - UINT trim2; // TRIM2 - UCHAR extdc; // non-zero to enable ETERNAL_DC_POWER - } FT_EEPROM_PD, *PFT_EEPROM_PD; - - // FT2233HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - // FT2232H with power delivery - typedef struct _ft_eeprom_2233hp - { - FT_EEPROM_2232H ft2232h; - FT_EEPROM_PD pd; - } FT_EEPROM_2233HP, *PFT_EEPROM_2233HP; - - // FT4233HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - // FT4232H with power delivery - typedef struct _ft_eeprom_4233hp - { - FT_EEPROM_4232H ft4232h; - FT_EEPROM_PD pd; - } FT_EEPROM_4233HP, *PFT_EEPROM_4233HP; - - // FT2232HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - // FT2232H with power delivery - typedef struct _ft_eeprom_2232hp - { - FT_EEPROM_2232H ft2232h; - FT_EEPROM_PD pd; - } FT_EEPROM_2232HP, *PFT_EEPROM_2232HP; - - // FT4232HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - // FT4232H with power delivery - typedef struct _ft_eeprom_4232hp - { - FT_EEPROM_4232H ft4232h; - FT_EEPROM_PD pd; - } FT_EEPROM_4232HP, *PFT_EEPROM_4232HP; - - // FT233HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - // FT233H with power delivery - typedef struct _ft_eeprom_233hp - { - FT_EEPROM_232H ft232h; - FT_EEPROM_PD pd; - } FT_EEPROM_233HP, *PFT_EEPROM_233HP; - - // FT232HP EEPROM structure for use with FT_EEPROM_Read and FT_EEPROM_Program - // FT232H with power delivery - typedef struct _ft_eeprom_232hp - { - FT_EEPROM_232H ft232h; - FT_EEPROM_PD pd; - } FT_EEPROM_232HP, *PFT_EEPROM_232HP; - - FTD2XX_API - FT_STATUS WINAPI FT_EEPROM_Read( - FT_HANDLE ftHandle, - void *eepromData, - DWORD eepromDataSize, - char *Manufacturer, - char *ManufacturerId, - char *Description, - char *SerialNumber - ); - - - FTD2XX_API - FT_STATUS WINAPI FT_EEPROM_Program( - FT_HANDLE ftHandle, - void *eepromData, - DWORD eepromDataSize, - char *Manufacturer, - char *ManufacturerId, - char *Description, - char *SerialNumber - ); - - - FTD2XX_API - FT_STATUS WINAPI FT_SetLatencyTimer( - FT_HANDLE ftHandle, - UCHAR ucLatency - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetLatencyTimer( - FT_HANDLE ftHandle, - PUCHAR pucLatency - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetBitMode( - FT_HANDLE ftHandle, - UCHAR ucMask, - UCHAR ucEnable - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetBitMode( - FT_HANDLE ftHandle, - PUCHAR pucMode - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetUSBParameters( - FT_HANDLE ftHandle, - ULONG ulInTransferSize, - ULONG ulOutTransferSize - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetDeadmanTimeout( - FT_HANDLE ftHandle, - ULONG ulDeadmanTimeout - ); - -#ifndef _WIN32 - // Extra functions for non-Windows platforms to compensate - // for lack of .INF file to specify Vendor and Product IDs. - - FTD2XX_API - FT_STATUS FT_SetVIDPID( - DWORD dwVID, - DWORD dwPID - ); - - FTD2XX_API - FT_STATUS FT_GetVIDPID( - DWORD * pdwVID, - DWORD * pdwPID - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetDeviceLocId( - FT_HANDLE ftHandle, - LPDWORD lpdwLocId - ); -#endif // _WIN32 - - FTD2XX_API - FT_STATUS WINAPI FT_GetDeviceInfo( - FT_HANDLE ftHandle, - FT_DEVICE *lpftDevice, - LPDWORD lpdwID, - PCHAR SerialNumber, - PCHAR Description, - LPVOID Dummy - ); - - FTD2XX_API - FT_STATUS WINAPI FT_StopInTask( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_RestartInTask( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_SetResetPipeRetryCount( - FT_HANDLE ftHandle, - DWORD dwCount - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ResetPort( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_CyclePort( - FT_HANDLE ftHandle - ); - - - // - // Win32-type functions - // - - FTD2XX_API - FT_HANDLE WINAPI FT_W32_CreateFile( - LPCTSTR lpszName, - DWORD dwAccess, - DWORD dwShareMode, - LPSECURITY_ATTRIBUTES lpSecurityAttributes, - DWORD dwCreate, - DWORD dwAttrsAndFlags, - HANDLE hTemplate - ); - - FTD2XX_API - BOOL WINAPI FT_W32_CloseHandle( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_ReadFile( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD nBufferSize, - LPDWORD lpBytesReturned, - LPOVERLAPPED lpOverlapped - ); - - FTD2XX_API - BOOL WINAPI FT_W32_WriteFile( - FT_HANDLE ftHandle, - LPVOID lpBuffer, - DWORD nBufferSize, - LPDWORD lpBytesWritten, - LPOVERLAPPED lpOverlapped - ); - - FTD2XX_API - DWORD WINAPI FT_W32_GetLastError( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetOverlappedResult( - FT_HANDLE ftHandle, - LPOVERLAPPED lpOverlapped, - LPDWORD lpdwBytesTransferred, - BOOL bWait - ); - - FTD2XX_API - BOOL WINAPI FT_W32_CancelIo( - FT_HANDLE ftHandle - ); - - - // - // Win32 COMM API type functions - // - typedef struct _FTCOMSTAT { - DWORD fCtsHold : 1; - DWORD fDsrHold : 1; - DWORD fRlsdHold : 1; - DWORD fXoffHold : 1; - DWORD fXoffSent : 1; - DWORD fEof : 1; - DWORD fTxim : 1; - DWORD fReserved : 25; - DWORD cbInQue; - DWORD cbOutQue; - } FTCOMSTAT, *LPFTCOMSTAT; - - typedef struct _FTDCB { - DWORD DCBlength; /* sizeof(FTDCB) */ - DWORD BaudRate; /* Baudrate at which running */ - DWORD fBinary : 1; /* Binary Mode (skip EOF check) */ - DWORD fParity : 1; /* Enable parity checking */ - DWORD fOutxCtsFlow : 1; /* CTS handshaking on output */ - DWORD fOutxDsrFlow : 1; /* DSR handshaking on output */ - DWORD fDtrControl : 2; /* DTR Flow control */ - DWORD fDsrSensitivity : 1; /* DSR Sensitivity */ - DWORD fTXContinueOnXoff : 1; /* Continue TX when Xoff sent */ - DWORD fOutX : 1; /* Enable output X-ON/X-OFF */ - DWORD fInX : 1; /* Enable input X-ON/X-OFF */ - DWORD fErrorChar : 1; /* Enable Err Replacement */ - DWORD fNull : 1; /* Enable Null stripping */ - DWORD fRtsControl : 2; /* Rts Flow control */ - DWORD fAbortOnError : 1; /* Abort all reads and writes on Error */ - DWORD fDummy2 : 17; /* Reserved */ - WORD wReserved; /* Not currently used */ - WORD XonLim; /* Transmit X-ON threshold */ - WORD XoffLim; /* Transmit X-OFF threshold */ - BYTE ByteSize; /* Number of bits/byte, 4-8 */ - BYTE Parity; /* 0-4=None,Odd,Even,Mark,Space */ - BYTE StopBits; /* FT_STOP_BITS_1 or FT_STOP_BITS_2 */ - char XonChar; /* Tx and Rx X-ON character */ - char XoffChar; /* Tx and Rx X-OFF character */ - char ErrorChar; /* Error replacement char */ - char EofChar; /* End of Input character */ - char EvtChar; /* Received Event character */ - WORD wReserved1; /* Fill for now. */ - } FTDCB, *LPFTDCB; - - typedef struct _FTTIMEOUTS { - DWORD ReadIntervalTimeout; /* Maximum time between read chars. */ - DWORD ReadTotalTimeoutMultiplier; /* Multiplier of characters. */ - DWORD ReadTotalTimeoutConstant; /* Constant in milliseconds. */ - DWORD WriteTotalTimeoutMultiplier; /* Multiplier of characters. */ - DWORD WriteTotalTimeoutConstant; /* Constant in milliseconds. */ - } FTTIMEOUTS, *LPFTTIMEOUTS; - - - FTD2XX_API - BOOL WINAPI FT_W32_ClearCommBreak( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_ClearCommError( - FT_HANDLE ftHandle, - LPDWORD lpdwErrors, - LPFTCOMSTAT lpftComstat - ); - - FTD2XX_API - BOOL WINAPI FT_W32_EscapeCommFunction( - FT_HANDLE ftHandle, - DWORD dwFunc - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommModemStatus( - FT_HANDLE ftHandle, - LPDWORD lpdwModemStatus - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommState( - FT_HANDLE ftHandle, - LPFTDCB lpftDcb - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommTimeouts( - FT_HANDLE ftHandle, - FTTIMEOUTS *pTimeouts - ); - - FTD2XX_API - BOOL WINAPI FT_W32_PurgeComm( - FT_HANDLE ftHandle, - DWORD dwMask - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommBreak( - FT_HANDLE ftHandle - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommMask( - FT_HANDLE ftHandle, - ULONG ulEventMask - ); - - FTD2XX_API - BOOL WINAPI FT_W32_GetCommMask( - FT_HANDLE ftHandle, - LPDWORD lpdwEventMask - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommState( - FT_HANDLE ftHandle, - LPFTDCB lpftDcb - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetCommTimeouts( - FT_HANDLE ftHandle, - FTTIMEOUTS *pTimeouts - ); - - FTD2XX_API - BOOL WINAPI FT_W32_SetupComm( - FT_HANDLE ftHandle, - DWORD dwReadBufferSize, - DWORD dwWriteBufferSize - ); - - FTD2XX_API - BOOL WINAPI FT_W32_WaitCommEvent( - FT_HANDLE ftHandle, - PULONG pulEvent, - LPOVERLAPPED lpOverlapped - ); - - - // - // Device information - // - - typedef struct _ft_device_list_info_node { - ULONG Flags; - ULONG Type; - ULONG ID; - DWORD LocId; - char SerialNumber[16]; - char Description[64]; - FT_HANDLE ftHandle; - } FT_DEVICE_LIST_INFO_NODE; - - // Device information flags - enum { - FT_FLAGS_OPENED = 1, - FT_FLAGS_HISPEED = 2 - }; - - - FTD2XX_API - FT_STATUS WINAPI FT_CreateDeviceInfoList( - LPDWORD lpdwNumDevs - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetDeviceInfoList( - FT_DEVICE_LIST_INFO_NODE *pDest, - LPDWORD lpdwNumDevs - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetDeviceInfoDetail( - DWORD dwIndex, - LPDWORD lpdwFlags, - LPDWORD lpdwType, - LPDWORD lpdwID, - LPDWORD lpdwLocId, - LPVOID lpSerialNumber, - LPVOID lpDescription, - FT_HANDLE *pftHandle - ); - - - // - // Version information - // - - FTD2XX_API - FT_STATUS WINAPI FT_GetDriverVersion( - FT_HANDLE ftHandle, - LPDWORD lpdwVersion - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetLibraryVersion( - LPDWORD lpdwVersion - ); - - - FTD2XX_API - FT_STATUS WINAPI FT_Rescan( - void - ); - - FTD2XX_API - FT_STATUS WINAPI FT_Reload( - WORD wVid, - WORD wPid - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetComPortNumber( - FT_HANDLE ftHandle, - LPLONG lpdwComPortNumber - ); - - - // - // FT232H additional EEPROM functions - // - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ReadConfig( - FT_HANDLE ftHandle, - UCHAR ucAddress, - PUCHAR pucValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_WriteConfig( - FT_HANDLE ftHandle, - UCHAR ucAddress, - UCHAR ucValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_EE_ReadECC( - FT_HANDLE ftHandle, - UCHAR ucOption, - LPWORD lpwValue - ); - - FTD2XX_API - FT_STATUS WINAPI FT_GetQueueStatusEx( - FT_HANDLE ftHandle, - DWORD *dwRxBytes - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ComPortIdle( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_ComPortCancelIdle( - FT_HANDLE ftHandle - ); - - FTD2XX_API - FT_STATUS WINAPI FT_VendorCmdGet( - FT_HANDLE ftHandle, - UCHAR Request, - UCHAR *Buf, - USHORT Len - ); - - FTD2XX_API - FT_STATUS WINAPI FT_VendorCmdSet( - FT_HANDLE ftHandle, - UCHAR Request, - UCHAR *Buf, - USHORT Len - ); - - FTD2XX_API - FT_STATUS WINAPI FT_VendorCmdGetEx( - FT_HANDLE ftHandle, - USHORT wValue, - UCHAR *Buf, - USHORT Len - ); - - FTD2XX_API - FT_STATUS WINAPI FT_VendorCmdSetEx( - FT_HANDLE ftHandle, - USHORT wValue, - UCHAR *Buf, - USHORT Len - ); - -#ifdef __cplusplus -} -#endif - - -#endif /* FTD2XX_H */ \ No newline at end of file diff --git a/hardware/third-party/ftdi/lib/ftd2xx.lib b/hardware/third-party/ftdi/lib/ftd2xx.lib deleted file mode 100644 index 7b28fab0dbe62abcd38b6588f3702be7decbe218..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 20260 zcmdU1OK)677CwX-m!^WxH)BCVt^KBs>PYu{+SB-EFnonF&G+ zLWtFfB}frM2(e(n56ET^LWl)I2q6|MW`S6-idZ0okOt;Eb?R1~y7%^_+wCl-rQ>_+ z)T#RFoO++C>j#e%YV*$@TfeU({O#}Q>dy7{_8jUB`JFbH%jN9r+G%gi$G#+D+A%HE|0@yGj>|&1 zSBRL7ZWik80S;&YIWXvf%k+Ce~a>iI^Px=_#F74eJDFq?{7jAA3=Z6Bfw?a{i@L3ZA6CNz?11`p%Lh2 zXd|AWM_&>ee+}gWjb0KO-b-ZYT|7Y}SA z`V3Fd7;I?>^)Ut--z2pE0uj^DCqjeo!)~C%>xBAIHl{w5k7@U2p*_%@X;)cj=PyJ| zT{A-azem2Hqdy6CeF{D(hcYsqSP(h{+Zsan5NQ8(Lc5^{)4n;OJ;;}-`)Q#^24Q#5 zu5W~P!q$fF#}kyl*%I=|GrcG@gglsfazdSWcIIYhN5`ior)Oq|hEM0GN2g{cr)NiI zhPuw3quJSsGh<`Pc>1RZNc7CfGZUv~hX!T_Xk=z~a;aEB!qVara*+_8`Eqr+=-Cnk zf4W#$@N5$T_iU|HSJDy$f3%vfm%X?IO)ksD`cUygXD|K!~TsOUjXQ_3!y=rCzJ5A|i$6Ta0=w%oAb6h5ol#L6RSBGgVRV%DD8$**WMI}i}Y#63d{8rE12#Y#aL-G zlrwHt@X}Raz=e7wff)`RYnVb-M=ff>5kaRclIEa$*+Z{ivvCoq#J+JY2*? zEDQ1Zi}U4Th~ilsU*mDjo0g|7Z{E~cqzcR)CP_xc<%f#(LP;&xf)q9qX<)e_Xtr3W z*v99Jm5j0&wGzg^$1#NkD;Wj|VnlkZ^lYtAyO<^(i-Now0Fu270NjqFUWRI%FQ_$* z4I`K7V!0Zwy-irkgDkkBCRP@o)e8j4T*W{dt$?c-E8p{_=e%7)w#b=TB1KU1eU8KV zd?GvMNeR{ZQ8zf0FpM$zO-)aZ%Ovf_jNxI=^fD=PzY&Z0khG>54dztM^fI>dr%9?P z$Uk1JEL3ax#RU}K>l|>k6xv*h6mzu{^3s;kxq+`Gkup_M_>df}lcfuu$8n+sr2h&y z>p+{4$9s_b7Wu#3N%T3=f4Pt7H{|sZ@IQQn=+)iu7VRZ^1N1ZKl@X$ipqCycdJXwq z8if~RnCM;574W}4AzqBjC$W1++y=<~06KSy=rd5y81w*bLfQ-ai9Q)3dLLz5hcdi| z@?3(xn}PY!9@t|S(JwoRX1a*JhwM+#^HaQUfQ}2m`TP)`pz9F#1JcZ)>@!IBH1r-s z*}p;hH<12*(9LM=Abj2V)SRanKsaq^aPF4NjgVoX$OtaPC89z=mhPe zM`$N>u4K2MpN`x8m2t0 zr-$fnx{Ef_4Kzmg(M`0OZln$xqFZPaJxuq|AU#EYqo?T^8lWyZKznHqCTw+nnNEx>rq`j-ad2+aJI3rU>!}1ob1@wAhIkJ<8AZQkI zCZ%4Xf`OUCIooW|dIoF=E%Jp)=QIi3EY9_WTpGLEVhb|KH}adTDu`I9BCWl&hZjy2$X!6lirFr3mrDMF=y&;f^XJ>9_ z!>ZzR!9=Ek^mL~A!QdD~G~U1J2~HAw1hJBs?$W>>P`V#wYfC(!Y11ToymgAKsWb6y2Sf zTfgx7wbb|D@8{NEL)hEPZ|_3!*_G!WK5c{W1rYr!3fN~qZ5Z5JTEI#B!|+weZxbG0 zfWDI;U5jTwb$)E+;ERyojd=V9`c8u8Z$EW>ybf=-!k@su^>RCbmD*0XZMYt+uS^7- z9C*I<%*OH3e66}%JzwwGF}<^6Y;*#Cm*kJJxExFb&jr8X`Ud<7{N_PW()sG*5{mlP zGuK@6`|mmhMB#y8iZt#gGNpY!;uzfo$I9eb28qbYbbAi-AK*Xs$GPKPjt%i+9Xu<9 zai;ZUvPLv9R* z$Ee2zC$<^)2zttF5-p?7&ID+@(oz+rUYkT}P-k}mWov{K`3O|*tI+9Sdh56cDET-N zR_txruR$N%44iEat8Lr4d(Yv>$D`|G0g82;D3$zc`}M@jL5#AI>)#v%xQ4!_Jqia` z8Y!rxmNm-gx!WC%JHK!WttHM5g=4RYtsW<%C*c6l+RdN3IH1;RC}S*|c1YTB=54l$ zew)#k9Fo)KJb~X!VaLF zNI{G*ELzquqxN_^E43Foc|FD|+GR$c=9RSIY1eT(Xwe;z_?jJ`H{b?Agcdjn6`X%< zvbAK?-IzxUz1$u|HPq(t-1azSt;Q}2v88sME3nr4T}I7qbZE-)uJmcWon(zFGihk; zuB00DbY5Tc?n`WG4-b?X{cDqTmN70rB;&t2k?^^d`8fv3}>( zAu#meiBiYEHfOg34O-uAK!M|sk?iFJr%dZDyunz=_0Ow!i_vaNi(H?vUdy8&`vrj# zU8-=ZtfjTsM+l7greIC2Wp={qE{IcPO;IKzP3%3BtAjETp_oHCUlXh7kr}<^I)~zp z`V^FZZLYUuc?$*=lD&H44A(MF##*f3;iyAN4}dlp!8WN=hV|Gvk2a0t5O>n)vu_|Twngxcz#_q!WbZjSD6d|V*2~a|y#*SnqAa!woicVfw>YHedX`hD z<&mMgfuDuFIqZWLjnYn~z3p1pF3YPq>QUjXQuq87pnq+@-q^EgwRB(gt=^tlUST-A zBP4ShZuKZxzEhsdMW5thzqecKb(XbG=CeM*)K>`FLCe~6uoqR()@;v_(F<_R+@hu3 zc4$Y(thU1EouXsACkR=q%Ud0aI~i9B`Pb%pS=MgjL5CDx_wX32XptFn8Sgm6e(n;9 zg7UA;X_m_v_m5j7e>27Bz6zZhv_xJxKOso&1`FSJX$z~tZh-sOQ%S6LSL_*@JsCs8 z66C6B_H+{KDr$xgShyVq_lVl5v+f4#0`6bbTp$9;TqMq3#R>qvP+hehp z$9^yn<-fM=^PtDFHx;9=BHF;q7)5&%c=n462iK}4v(|q2=rL+5R=ca)`|wd_3~w!Z zUsk(393Z-HP*!ivj5ClU9xJ?|>Z`2vevr}U@QG?vm*8l(j diff --git a/peripherals.go b/peripherals.go index d359f97..e128c1d 100644 --- a/peripherals.go +++ b/peripherals.go @@ -72,7 +72,7 @@ func (a *App) RemovePeripheral(protocolName string, peripheralID string) error { log.Err(err).Str("file", "peripherals").Str("protocolName", protocolName).Msg("unable to find the finder") return fmt.Errorf("unable to find the finder") } - err = f.UnregisterPeripheral(peripheralID) + err = f.UnregisterPeripheral(a.ctx, peripheralID) if err != nil { log.Err(err).Str("file", "peripherals").Str("peripheralID", peripheralID).Msg("unable to unregister this peripheral") return fmt.Errorf("unable to unregister this peripheral") From 15d0f8b61b7e41ec34242a4e3c5311ef3bd6aeea Mon Sep 17 00:00:00 2001 From: Valentin Boulanger Date: Sun, 2 Nov 2025 10:57:53 +0100 Subject: [PATCH 7/7] clean up arch --- app.go | 34 +++- frontend/src/App.svelte | 6 - .../src/components/Settings/DeviceCard.svelte | 2 - .../Settings/InputsOutputsContent.svelte | 8 - .../src/components/Settings/Settings.svelte | 42 ++-- frontend/src/lang/en.json | 1 + hardware/FTDIFinder.go | 79 +++++--- hardware/FTDIPeripheral.go | 63 +++--- hardware/hardware.go | 79 ++++---- hardware/interfaces.go | 4 +- project.go | 191 ------------------ 11 files changed, 175 insertions(+), 334 deletions(-) delete mode 100644 project.go diff --git a/app.go b/app.go index f55e03f..8708e62 100644 --- a/app.go +++ b/app.go @@ -16,12 +16,15 @@ import ( // App struct type App struct { - ctx context.Context - cancelFunc context.CancelFunc + ctx context.Context + cancelFunc context.CancelFunc + wait sync.WaitGroup + hardwareManager *hardware.HardwareManager // For managing all the hardware wmiMutex sync.Mutex // Avoid some WMI operations at the same time projectInfo ProjectInfo // The project information structure projectSave string // The file name of the project + projectCancel context.CancelFunc // The project cancel function } // NewApp creates a new App application struct @@ -44,11 +47,17 @@ func NewApp() *App { // so we can call the runtime methods func (a *App) onStartup(ctx context.Context) { a.ctx, a.cancelFunc = context.WithCancel(ctx) - err := a.hardwareManager.Start(a.ctx) - if err != nil { - log.Err(err).Str("file", "app").Msg("unable to start the hardware manager") - return - } + + // Starting the hardware manager + a.wait.Add(1) + go func() { + defer a.wait.Done() + err := a.hardwareManager.Start(a.ctx) + if err != nil { + log.Err(err).Str("file", "app").Msg("unable to start the hardware manager") + return + } + }() } // onReady is called when the DOM is ready @@ -69,10 +78,13 @@ func (a *App) onShutdown(ctx context.Context) { log.Trace().Str("file", "app").Msg("app is closing") // Explicitly close the context a.cancelFunc() - err := a.hardwareManager.Stop() - if err != nil { - log.Err(err).Str("file", "app").Msg("unable to stop the hardware manager") - } + // Wait for application to close properly + a.hardwareManager.WaitStop() + // a.cancelFunc() + // err := a.hardwareManager.Stop() + // if err != nil { + // log.Err(err).Str("file", "app").Msg("unable to stop the hardware manager") + // } return } diff --git a/frontend/src/App.svelte b/frontend/src/App.svelte index 0b54732..b7d7ba7 100644 --- a/frontend/src/App.svelte +++ b/frontend/src/App.svelte @@ -92,12 +92,6 @@ }) } - // Instanciate a new project - CreateProject().then((showInfo) => { - showInformation.set(showInfo) - $needProjectSave = true - }) - // Handle window shortcuts document.addEventListener('keydown', function(event) { // Check the CTRL+S keys diff --git a/frontend/src/components/Settings/DeviceCard.svelte b/frontend/src/components/Settings/DeviceCard.svelte index 0c0a46e..8910208 100644 --- a/frontend/src/components/Settings/DeviceCard.svelte +++ b/frontend/src/components/Settings/DeviceCard.svelte @@ -57,8 +57,6 @@
- -