2024-12-23 17:22:37 +01:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"changeme/hardware"
|
|
|
|
|
"fmt"
|
2024-12-29 13:09:46 +01:00
|
|
|
|
|
|
|
|
"github.com/rs/zerolog/log"
|
2024-12-23 17:22:37 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// GetPeripherals gets all the peripherals connected
|
|
|
|
|
func (a *App) GetPeripherals() error {
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Debug().Str("file", "peripherals").Msg("getting peripherals...")
|
2024-12-23 17:22:37 +01:00
|
|
|
return a.hardwareManager.Scan(a.ctx)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddPeripheral adds a peripheral to the project
|
|
|
|
|
func (a *App) AddPeripheral(protocolName string, peripheralID string) error {
|
|
|
|
|
// Get the device from its finder
|
|
|
|
|
p, found := a.hardwareManager.GetPeripheral(protocolName, peripheralID)
|
|
|
|
|
if !found {
|
2024-12-29 13:09:46 +01:00
|
|
|
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)
|
2024-12-23 17:22:37 +01:00
|
|
|
}
|
|
|
|
|
// Add the peripheral ID to the project
|
|
|
|
|
a.projectInfo.PeripheralsInfo[peripheralID] = p.GetInfo()
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Info().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("peripheral added to project")
|
|
|
|
|
|
2024-12-23 17:22:37 +01:00
|
|
|
// TODO: Connect the peripheral
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// RemovePeripheral adds a peripheral to the project
|
|
|
|
|
func (a *App) RemovePeripheral(protocolName string, peripheralID string) error {
|
|
|
|
|
// TODO: Disconnect the peripheral
|
|
|
|
|
// Remove the peripheral ID from the project
|
|
|
|
|
delete(a.projectInfo.PeripheralsInfo, peripheralID)
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Info().Str("file", "peripheral").Str("protocolName", protocolName).Str("periphID", peripheralID).Msg("peripheral removed from project")
|
2024-12-23 17:22:37 +01:00
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// AddOS2LPeripheral adds a new OS2L peripheral
|
|
|
|
|
func (a *App) AddOS2LPeripheral() (hardware.PeripheralInfo, error) {
|
|
|
|
|
// Get the OS2L driver
|
|
|
|
|
os2lDriver, err := a.hardwareManager.GetDriver("OS2L")
|
|
|
|
|
if err != nil {
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Err(err).Str("file", "peripheral").Msg("unable to found the OS2L driver")
|
2024-12-23 17:22:37 +01:00
|
|
|
return hardware.PeripheralInfo{}, err
|
|
|
|
|
}
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Trace().Str("file", "peripheral").Msg("OS2L driver got")
|
|
|
|
|
|
2024-12-23 17:22:37 +01:00
|
|
|
// Create a new OS2L peripheral with this driver
|
|
|
|
|
os2lPeripheral, err := os2lDriver.CreatePeripheral(a.ctx)
|
|
|
|
|
if err != nil {
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Err(err).Str("file", "peripheral").Msg("unable to create the OS2L peripheral")
|
|
|
|
|
return hardware.PeripheralInfo{}, err
|
2024-12-23 17:22:37 +01:00
|
|
|
}
|
2024-12-29 13:09:46 +01:00
|
|
|
|
2024-12-23 17:22:37 +01:00
|
|
|
os2lInfo := os2lPeripheral.GetInfo()
|
2024-12-29 13:09:46 +01:00
|
|
|
log.Info().Str("file", "peripheral").Str("s/n", os2lInfo.SerialNumber).Msg("OS2L peripheral created, adding to project")
|
2024-12-23 17:22:37 +01:00
|
|
|
// Add this new peripheral to the project
|
|
|
|
|
return os2lInfo, a.AddPeripheral(os2lDriver.GetName(), os2lInfo.SerialNumber)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// FOR TESTING PURPOSE ONLY
|
|
|
|
|
|
|
|
|
|
func (a *App) ConnectFTDI() error {
|
|
|
|
|
// Create a new FTDI object
|
|
|
|
|
var err error
|
|
|
|
|
a.ftdi, err = hardware.NewFTDIPeripheral("FTDI TEST INTERFACE", "A50825I", 0)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
return a.ftdi.Connect()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) ActivateFTDI() error {
|
|
|
|
|
return a.ftdi.Activate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) SetDeviceFTDI(channelValue byte) error {
|
|
|
|
|
return a.ftdi.SetDeviceProperty(0, 0, channelValue)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) DeactivateFTDI() error {
|
|
|
|
|
return a.ftdi.Deactivate()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (a *App) DisconnectFTDI() error {
|
|
|
|
|
return a.ftdi.Disconnect()
|
|
|
|
|
}
|