-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add HostContext interface for exposing host elements to the virtual h…
…ardware Not in love with the current interface yet. probably want to add logging methods rather than expose the logger itself, but i-i-i-i-terationnnnnn
- Loading branch information
Showing
6 changed files
with
138 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
package devices | ||
|
||
import ( | ||
"log" | ||
) | ||
|
||
type HostContext interface { | ||
Logger() *log.Logger | ||
SerialCable() SerialCable | ||
} | ||
|
||
type Host struct { | ||
logger *log.Logger | ||
serialCable SerialCable | ||
} | ||
|
||
func NewHost() *Host { | ||
return &Host{ | ||
logger: log.Default(), | ||
serialCable: &NullSerialCable{}, | ||
} | ||
} | ||
|
||
func (h *Host) Logger() *log.Logger { | ||
return h.logger | ||
} | ||
|
||
func (h *Host) SetLogger(logger *log.Logger) { | ||
h.logger = logger | ||
} | ||
|
||
func (h *Host) SerialCable() SerialCable { | ||
return h.serialCable | ||
} | ||
|
||
func (h *Host) SetSerialCable(serialCable SerialCable) { | ||
h.serialCable = serialCable | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package devices | ||
|
||
import ( | ||
"bytes" | ||
"io" | ||
) | ||
|
||
type SerialCable interface { | ||
ReadByte() (byte, error) | ||
WriteByte(value byte) error | ||
} | ||
|
||
type NullSerialCable struct{} | ||
|
||
func (sc *NullSerialCable) ReadByte() (byte, error) { | ||
return 0xFF, nil | ||
} | ||
|
||
func (sc *NullSerialCable) WriteByte(value byte) error { | ||
return nil | ||
} | ||
|
||
type HostSerialCable struct { | ||
reader io.Reader | ||
writer io.Writer | ||
} | ||
|
||
func NewHostSerialCable() *HostSerialCable { | ||
return &HostSerialCable{ | ||
reader: bytes.NewReader([]byte{}), | ||
writer: io.Discard, | ||
} | ||
} | ||
|
||
func (sc *HostSerialCable) ReadByte() (byte, error) { | ||
readBuf := []byte{0x00} | ||
|
||
if _, err := sc.reader.Read(readBuf); err != nil { | ||
return 0xFF, err | ||
} | ||
|
||
return readBuf[0], nil | ||
} | ||
|
||
func (sc *HostSerialCable) WriteByte(value byte) error { | ||
_, err := sc.writer.Write([]byte{value}) | ||
return err | ||
} | ||
|
||
func (sc *HostSerialCable) SetReader(reader io.Reader) { | ||
sc.reader = reader | ||
} | ||
|
||
func (sc *HostSerialCable) SetWriter(writer io.Writer) { | ||
sc.writer = writer | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters