forked from google/periph
/
display.go
58 lines (47 loc) · 1.23 KB
/
display.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
// Copyright 2016 The Periph Authors. All rights reserved.
// Use of this source code is governed under the Apache License, Version 2.0
// that can be found in the LICENSE file.
package devicestest
import (
"errors"
"fmt"
"image"
"image/color"
"image/draw"
"periph.io/x/periph/conn"
"periph.io/x/periph/devices"
)
// Display is a fake devices.Display.
type Display struct {
Img *image.NRGBA
}
func (d *Display) String() string {
return "Display"
}
// Halt implements conn.Resource. It is a noop.
func (d *Display) Halt() error {
return nil
}
// Write implements devices.Display.
func (d *Display) Write(pixels []byte) (int, error) {
if len(pixels)%3 != 0 {
return 0, errors.New("devicetest: invalid RGB stream length")
}
copy(d.Img.Pix, pixels)
return len(pixels), nil
}
// ColorModel implements image.Image.
func (d *Display) ColorModel() color.Model {
return d.Img.ColorModel()
}
// Bounds implements image.Image.
func (d *Display) Bounds() image.Rectangle {
return d.Img.Bounds()
}
// Draw implements draw.Image.
func (d *Display) Draw(r image.Rectangle, src image.Image, sp image.Point) {
draw.Draw(d.Img, r, src, sp, draw.Src)
}
var _ conn.Resource = &Display{}
var _ devices.Display = &Display{}
var _ fmt.Stringer = &Display{}