forked from fiam/max7456tool
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decoder.go
91 lines (85 loc) · 1.98 KB
/
decoder.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package main
import (
"bufio"
"fmt"
"io"
"strconv"
"strings"
)
const (
max7456Hdr = "MAX7456\r\n"
max7456AltHdr = "MAX7456\n"
)
// MCMDecoder decodes a .mcm file into its characters.
// Use NewDecoder to initialize a decoder.
type MCMDecoder struct {
chars []*MCMChar
}
// NChars returns the number of characters found in the
// character map. Must always be 256 for standard
// character maps.
func (d *MCMDecoder) NChars() int {
return len(d.chars)
}
// CharAt returns the character at the given index.
func (d *MCMDecoder) CharAt(i int) *MCMChar {
return d.chars[i]
}
// NewDecoder initializes an MCMDecoder reading the
// data from the given reader. The data must represent
// a well formed MAX7456 character map. Otherwise, this
// function will return an error.
func NewDecoder(r io.Reader) (*MCMDecoder, error) {
br := bufio.NewReader(r)
hdr, err := br.ReadString('\n')
if err != nil {
return nil, err
}
if hdr != max7456Hdr && hdr != max7456AltHdr {
return nil, fmt.Errorf("unknown character map header %q", hdr)
}
var builder charBuilder
builder.Reset()
var chars []*MCMChar
appendPixel := func(s string) error {
val, err := strconv.ParseUint(s, 2, 32)
if err != nil {
return err
}
return builder.AppendPixel(MCMPixel(val))
}
ii := 2
for {
line, err := br.ReadString('\n')
if err != nil && line == "" {
if err == io.EOF && builder.IsEmpty() {
break
}
return nil, err
}
line = strings.TrimSpace(line)
if len(line) != 8 {
return nil, fmt.Errorf("line %d has invalid length %d (must be 8)", ii, len(line))
}
ii++
if err := appendPixel(line[0:2]); err != nil {
return nil, err
}
if err := appendPixel(line[2:4]); err != nil {
return nil, err
}
if err := appendPixel(line[4:6]); err != nil {
return nil, err
}
if err := appendPixel(line[6:8]); err != nil {
return nil, err
}
if builder.IsComplete() {
chars = append(chars, builder.Char())
builder.Reset()
}
}
return &MCMDecoder{
chars: chars,
}, nil
}