72 lines
1.5 KiB
Go
72 lines
1.5 KiB
Go
package codetable
|
|
|
|
import "errors"
|
|
|
|
var (
|
|
ErrOutOfBounds = errors.New("Coordinates out of bounds")
|
|
)
|
|
|
|
const (
|
|
CodeSpaceRowCount = 16
|
|
ControlCodeSpaceColumnCount = 2
|
|
GraphicCodeSpaceColumnCount = 6
|
|
)
|
|
|
|
type CodeSpace interface {
|
|
Get(int, int) (byte, error)
|
|
ID() string
|
|
Columns() int
|
|
}
|
|
|
|
type controlRow [ControlCodeSpaceColumnCount]byte
|
|
type controlTable [CodeSpaceRowCount]controlRow
|
|
|
|
type ControlCodeSpace struct {
|
|
table controlTable
|
|
id string
|
|
}
|
|
|
|
func (t ControlCodeSpace) Get(col, row int) (byte, error) {
|
|
colOffset := t.table[0][0] & 0xF0
|
|
if col < int(colOffset) || col > int(colOffset+CodeSpaceRowCount) || row < 0 || row > 16 {
|
|
return 0, ErrOutOfBounds
|
|
}
|
|
return t.table[col][row], nil
|
|
}
|
|
|
|
func (t ControlCodeSpace) ID() string {
|
|
return t.id
|
|
}
|
|
|
|
func (t ControlCodeSpace) Columns() int {
|
|
return len(t.table[0])
|
|
}
|
|
|
|
type graphicRow [GraphicCodeSpaceColumnCount]byte
|
|
type graphicTable [CodeSpaceRowCount]graphicRow
|
|
|
|
type GraphicCodeSpace struct {
|
|
table graphicTable
|
|
id string
|
|
}
|
|
|
|
func (t GraphicCodeSpace) ColumnOffset() int {
|
|
return int(t.table[0][0] & 0xF0) // Get highest order nibble.
|
|
}
|
|
|
|
func (t GraphicCodeSpace) Get(row, col int) (byte, error) {
|
|
colOffset := t.ColumnOffset()
|
|
if col < colOffset || col > (colOffset+GraphicCodeSpaceColumnCount) || row < 0 || row > 16 {
|
|
return 0, ErrOutOfBounds
|
|
}
|
|
return t.table[col][row], nil
|
|
}
|
|
|
|
func (t GraphicCodeSpace) ID() string {
|
|
return t.id
|
|
}
|
|
|
|
func (t GraphicCodeSpace) Columns() int {
|
|
return len(t.table[0])
|
|
}
|