Fix Connected Param

This commit is contained in:
Justin Hammond 2021-09-27 22:23:10 +08:00
parent 7b609977be
commit 5ebfc921de

View file

@ -43,76 +43,77 @@ type DcDcUSB struct {
done func() done func()
log logadapter.Logger log logadapter.Logger
initOnce sync.Once initOnce sync.Once
connected bool
} }
// Represents the Settings for Off and Hardoff Delays when power is lost // Represents the Settings for Off and Hardoff Delays when power is lost
type TimerConfigt struct { type TimerConfigt struct {
// After Ignition Lost, this the time waiting till we toggle the Power Switch I/F // After Ignition Lost, this the time waiting till we toggle the Power Switch I/F
OffDelay time.Duration `json:"off_delay,omitempty"` OffDelay time.Duration `json:"off_delay"`
// After the Power Switch I/F is toggled, this is the delay before we cut power // After the Power Switch I/F is toggled, this is the delay before we cut power
HardOff time.Duration `json:"hard_off,omitempty"` HardOff time.Duration `json:"hard_off"`
} }
// Status of Various Peripherals // Status of Various Peripherals
type Peripheralst struct { type Peripheralst struct {
// ?? // ??
OutSwVin bool `json:"out_sw_vin,omitempty"` OutSwVin bool `json:"out_sw_vin"`
// ?? // ??
OutPsw bool `json:"out_psw,omitempty"` OutPsw bool `json:"out_psw"`
// ?? // ??
OutStartOutput bool `json:"out_start_output,omitempty"` OutStartOutput bool `json:"out_start_output"`
// Status of the Onboard Led // Status of the Onboard Led
OutLed bool `json:"out_led,omitempty"` OutLed bool `json:"out_led"`
// If the VOut is within range. // If the VOut is within range.
InVoutGood bool `json:"in_vout_good,omitempty"` InVoutGood bool `json:"in_vout_good"`
} }
// Overall Status of the DCDCUSB Power Supply // Overall Status of the DCDCUSB Power Supply
type Params struct { type Params struct {
// What the Vout Setting is configured for // What the Vout Setting is configured for
VoutSet float32 `json:"vout_set,omitempty"` VoutSet float32 `json:"vout_set"`
// What Voltage the Config Jumpers are set for VOut // What Voltage the Config Jumpers are set for VOut
VoutConfig float32 `json:"vout_config,omitempty"` VoutConfig float32 `json:"vout_config"`
// The Input Voltage // The Input Voltage
Vin float32 `json:"vin,omitempty"` Vin float32 `json:"vin"`
// The Ignition Voltage // The Ignition Voltage
Vign float32 `json:"vign,omitempty"` Vign float32 `json:"vign"`
// What the Actual VOut Voltage is // What the Actual VOut Voltage is
VoutActual float32 `json:"vout_actual,omitempty"` VoutActual float32 `json:"vout_actual"`
// Status of Various Peripherals // Status of Various Peripherals
Peripherals Peripheralst `json:"peripherals,omitempty"` Peripherals Peripheralst `json:"peripherals"`
// ?? (Not Output Enabled?) // ?? (Not Output Enabled?)
Output bool `json:"output,omitempty"` Output bool `json:"output"`
// ?? // ??
AuxVIn bool `json:"aux_v_in,omitempty"` AuxVIn bool `json:"aux_v_in"`
// Firmware Version? // Firmware Version?
Version string `json:"version,omitempty"` Version string `json:"version"`
// State of the Power Supply // State of the Power Supply
State DcdcStatet `json:"state,omitempty"` State DcdcStatet `json:"state"`
// Config Registers (unknown) // Config Registers (unknown)
CfgRegisters byte `json:"cfg_registers,omitempty"` CfgRegisters byte `json:"cfg_registers"`
// Voltage Flags (Unknown) // Voltage Flags (Unknown)
VoltFlags byte `json:"volt_flags,omitempty"` VoltFlags byte `json:"volt_flags"`
// Timer Flags (Unknown) // Timer Flags (Unknown)
TimerFlags byte `json:"timer_flags,omitempty"` TimerFlags byte `json:"timer_flags"`
// The configured countdown times for the Timer upon Power Loss // The configured countdown times for the Timer upon Power Loss
TimerConfig TimerConfigt `json:"timer_config,omitempty"` TimerConfig TimerConfigt `json:"timer_config"`
// Current Power Loss Debounce Timer // Current Power Loss Debounce Timer
TimerWait time.Duration `json:"timer_wait,omitempty"` TimerWait time.Duration `json:"timer_wait"`
// Current VOut Countdown Timer // Current VOut Countdown Timer
TimerVOut time.Duration `json:"timer_v_out,omitempty"` TimerVOut time.Duration `json:"timer_v_out"`
// Current VAux Countdown timer // Current VAux Countdown timer
TimerVAux time.Duration `json:"timer_v_aux,omitempty"` TimerVAux time.Duration `json:"timer_v_aux"`
// Current Power Switch Toggle Count Down Timer // Current Power Switch Toggle Count Down Timer
TimerPRWSW time.Duration `json:"timer_prwsw,omitempty"` TimerPRWSW time.Duration `json:"timer_prwsw"`
// Current Soft Off Countdown Timer // Current Soft Off Countdown Timer
TimerSoftOff time.Duration `json:"timer_soft_off,omitempty"` TimerSoftOff time.Duration `json:"timer_soft_off"`
// Current Hard Off Countdown Timer // Current Hard Off Countdown Timer
TimerHardOff time.Duration `json:"timer_hard_off,omitempty"` TimerHardOff time.Duration `json:"timer_hard_off"`
// Current Script Position // Current Script Position
ScriptPointer byte `json:"script_pointer,omitempty"` ScriptPointer byte `json:"script_pointer"`
// Current Operating Mode // Current Operating Mode
Mode DcdcModet `json:"mode,omitempty"` Mode DcdcModet `json:"mode"`
} }
// Initialize the DCDCUSB Communications. Should be first function called before any other methods are called // Initialize the DCDCUSB Communications. Should be first function called before any other methods are called
func (dc *DcDcUSB) Init() { func (dc *DcDcUSB) Init() {
@ -123,6 +124,7 @@ func (dc *DcDcUSB) Init() {
dc.log = templogger dc.log = templogger
} }
}) })
dc.connected = false
dc.ctx = gousb.NewContext() dc.ctx = gousb.NewContext()
} }
@ -138,6 +140,10 @@ func (dc *DcDcUSB) SetUSBDebug(level int) {
dc.ctx.Debug(level) dc.ctx.Debug(level)
} }
} }
// Returns if we are connected to the Power Supply
func (dc *DcDcUSB) IsConnected() (bool) {
return dc.connected
}
// Scan for a DCDCUSB connection, returns true if found, or false (and optional error) if there // Scan for a DCDCUSB connection, returns true if found, or false (and optional error) if there
// was a failure setting up communications with it. // was a failure setting up communications with it.
@ -173,6 +179,7 @@ func (dc *DcDcUSB) Scan() (bool, error) {
} }
dc.log.Trace("Interface: %s", dc.intf.String()) dc.log.Trace("Interface: %s", dc.intf.String())
dc.log = dc.log.With("Device", dc.intf.String()) dc.log = dc.log.With("Device", dc.intf.String())
dc.connected = true
return true, nil return true, nil
} }
func (dc *DcDcUSB) Close() { func (dc *DcDcUSB) Close() {
@ -186,19 +193,20 @@ func (dc *DcDcUSB) Close() {
if dc.ctx != nil { if dc.ctx != nil {
dc.ctx.Close() dc.ctx.Close()
} }
dc.connected = false;
} }
// Gets All current Params from the DCDCUSB power Supply. // Gets All current Params from the DCDCUSB power Supply.
// Set a Timeout/Deadline Context to cancel slow calls // Set a Timeout/Deadline Context to cancel slow calls
func (dc *DcDcUSB) GetAllParam(ctx context.Context) (Params) { func (dc *DcDcUSB) GetAllParam(ctx context.Context) (Params, error) {
if dc.intf == nil { if dc.intf == nil {
dc.log.Warn("Interface Not Opened") dc.log.Warn("Interface Not Opened")
return Params{} return Params{}, fmt.Errorf("Interface Not Opened")
} }
outp, err := dc.intf.OutEndpoint(0x01) outp, err := dc.intf.OutEndpoint(0x01)
if err != nil { if err != nil {
dc.log.Warn("Can't Get OutPoint: %s", err) dc.log.Warn("Can't Get OutEndPoint: %s", err)
return Params{} return Params{}, err
} }
//log.Printf("OutEndpoint: %v", outp) //log.Printf("OutEndpoint: %v", outp)
var send = make([]byte, 24) var send = make([]byte, 24)
@ -208,13 +216,13 @@ func (dc *DcDcUSB) GetAllParam(ctx context.Context) (Params) {
len, err := outp.WriteContext(ctx, send) len, err := outp.WriteContext(ctx, send)
if err != nil { if err != nil {
dc.log.Warn("Cant Send GetAllValues Command: %s (%v) - %d", err, send, len) dc.log.Warn("Cant Send GetAllValues Command: %s (%v) - %d", err, send, len)
return Params{} return Params{}, err
} }
//log.Printf("Sent %d Bytes", len) //log.Printf("Sent %d Bytes", len)
inp, err := dc.intf.InEndpoint(0x81) inp, err := dc.intf.InEndpoint(0x81)
if err != nil { if err != nil {
dc.log.Warn("Can't Get OutPoint: %s", err) dc.log.Warn("Can't Get OutPoint: %s", err)
return Params{} return Params{}, err
} }
//log.Printf("InEndpoint: %v", inp) //log.Printf("InEndpoint: %v", inp)
@ -222,12 +230,12 @@ func (dc *DcDcUSB) GetAllParam(ctx context.Context) (Params) {
len, err = inp.ReadContext(ctx, recv) len, err = inp.ReadContext(ctx, recv)
if err != nil { if err != nil {
dc.log.Warn("Can't Read GetAllValues Command: %s", err) dc.log.Warn("Can't Read GetAllValues Command: %s", err)
return Params{} return Params{}, err
} }
//log.Printf("Got %d Bytes", len) //log.Printf("Got %d Bytes", len)
dc.log.Trace("Got %v", recv) dc.log.Trace("Got %v", recv)
params, _ := dc.parseAllValues(recv, len) params, err := dc.parseAllValues(recv, len)
return params return params, err
} }
// 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23