generated from thinkode/modelRepository
rework on the peripherals and finders
This commit is contained in:
196
peripherals.go
196
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)
|
||||
// }
|
||||
|
||||
Reference in New Issue
Block a user