Added some basic command handling for the devices
This commit is contained in:
@@ -1,9 +1,15 @@
|
||||
package devices
|
||||
|
||||
import helper "esdi/helpers"
|
||||
import (
|
||||
"fmt"
|
||||
"strconv"
|
||||
|
||||
helper "esdi/helpers"
|
||||
"esdi/peripheral/types"
|
||||
)
|
||||
|
||||
const (
|
||||
newWindowCMDID = iota
|
||||
newWindowCMDID types.CommandID = iota
|
||||
destroyWindowCMDID
|
||||
)
|
||||
|
||||
@@ -26,10 +32,62 @@ var CDashDisplay = Device{
|
||||
},
|
||||
}
|
||||
|
||||
func createWindow(data []byte) {
|
||||
func createWindow(dCMD *DeviceCMD, args []string) (types.CommandID, []byte, error) {
|
||||
// Parse the command
|
||||
// x0, y0, width, height, title # add other decorations later on
|
||||
// fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args)
|
||||
|
||||
type UIWindow struct {
|
||||
X0 uint16
|
||||
Y0 uint16
|
||||
Width uint16
|
||||
Height uint16
|
||||
Title [32]byte
|
||||
}
|
||||
|
||||
if len(args) != 5 {
|
||||
return 0, []byte{}, fmt.Errorf("wrong parameters, got %d, want %d. "+
|
||||
"Command asks for: x0 y0 width height title", len(args), 5)
|
||||
}
|
||||
|
||||
x0, err := strconv.ParseInt(args[0], 10, 0)
|
||||
if err != nil {
|
||||
return 0, []byte{}, err
|
||||
}
|
||||
y0, err := strconv.ParseInt(args[1], 10, 0)
|
||||
if err != nil {
|
||||
return 0, []byte{}, err
|
||||
}
|
||||
width, err := strconv.ParseInt(args[2], 10, 0)
|
||||
if err != nil {
|
||||
return 0, []byte{}, err
|
||||
}
|
||||
height, err := strconv.ParseInt(args[3], 10, 0)
|
||||
if err != nil {
|
||||
return 0, []byte{}, err
|
||||
}
|
||||
|
||||
data := UIWindow{
|
||||
X0: uint16(x0),
|
||||
Y0: uint16(y0),
|
||||
Width: uint16(width),
|
||||
Height: uint16(height),
|
||||
Title: helper.B32(args[4]),
|
||||
}
|
||||
|
||||
// fmt.Printf("Data: %+v\n", data)
|
||||
|
||||
bytes, err := helper.StructToBytes(data)
|
||||
if err != nil {
|
||||
return 0, []byte{}, err
|
||||
}
|
||||
|
||||
return dCMD.GetIdentifier(), bytes, nil
|
||||
}
|
||||
|
||||
func destroyWindow(data []byte) {
|
||||
func destroyWindow(dCMD *DeviceCMD, args []string) (types.CommandID, []byte, error) {
|
||||
// Parse the command
|
||||
fmt.Printf("%s command called: %+v\n", dCMD.GetName(), args)
|
||||
|
||||
return dCMD.GetIdentifier(), []byte{}, nil
|
||||
}
|
||||
|
||||
@@ -23,6 +23,14 @@ type Device struct {
|
||||
API map[string]DeviceCMD
|
||||
}
|
||||
|
||||
func (dev *Device) HasFunction(f string) *DeviceCMD {
|
||||
if cmd, ok := dev.API[f]; ok {
|
||||
return &cmd
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
type DeviceCMDHeader struct {
|
||||
Payload [64]byte
|
||||
}
|
||||
@@ -34,7 +42,7 @@ type DeviceCMDPayload struct {
|
||||
// DeviceCMDFn defines the basic type for the functions the devices can have
|
||||
// The function will receive a []byte that should be the arguments the user
|
||||
// types in the REPL - or however this will be used
|
||||
type DeviceCMDFn func(data []byte)
|
||||
type DeviceCMDFn func(dCMD *DeviceCMD, args []string) (types.CommandID, []byte, error)
|
||||
|
||||
type DeviceCMD struct {
|
||||
Identifier types.CommandID
|
||||
@@ -44,3 +52,15 @@ type DeviceCMD struct {
|
||||
Data DeviceCMDPayload
|
||||
Fn DeviceCMDFn
|
||||
}
|
||||
|
||||
func (dCMD *DeviceCMD) GetIdentifier() types.CommandID {
|
||||
return dCMD.Identifier
|
||||
}
|
||||
|
||||
func (dCMD *DeviceCMD) GetName() string {
|
||||
return dCMD.Name
|
||||
}
|
||||
|
||||
func (dCMD *DeviceCMD) GetDesc() string {
|
||||
return dCMD.Desc
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user