mirror of https://github.com/periph/devices
conn/display: simplify Drawer
display: move devices.Display as display.Drawer This removes the last interface from devices, which was misplaced due to historical accident. The new package display will also be the location for an interface for text output only devices. Remove io.Writer from this interface. While it's a good performance optimization for some drivers, it shouldn't be required. Change Draw(): - Return an error, so that communication erorr can be surfaced correctly, instead of an adhoc driver specific Err() method.pull/1/head
parent
f7d0d4e3d5
commit
501558c627
@ -1,42 +0,0 @@
|
||||
// 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 devices
|
||||
|
||||
import (
|
||||
"image"
|
||||
"image/color"
|
||||
"io"
|
||||
|
||||
"periph.io/x/periph/conn"
|
||||
)
|
||||
|
||||
// Display represents a pixel output device. It is a write-only interface.
|
||||
//
|
||||
// What Display represents can be as varied as a 1 bit OLED display or a strip
|
||||
// of LED lights.
|
||||
type Display interface {
|
||||
conn.Resource
|
||||
|
||||
// Writer can be used when the native display pixel format is known. Each
|
||||
// write must cover exactly the whole screen as a single packed stream of
|
||||
// pixels.
|
||||
io.Writer
|
||||
// ColorModel returns the device native color model.
|
||||
//
|
||||
// It is generally color.NRGBA for a color display.
|
||||
ColorModel() color.Model
|
||||
// Bounds returns the size of the output device.
|
||||
//
|
||||
// Generally displays should have Min at {0, 0} but this is not guaranteed in
|
||||
// multiple displays setup or when an instance of this interface represents a
|
||||
// section of a larger logical display.
|
||||
Bounds() image.Rectangle
|
||||
// Draw updates the display with this image starting at 'sp' offset into the
|
||||
// display into 'r'. The code will likely be faster if the image is in the
|
||||
// display's native color format.
|
||||
//
|
||||
// To be compatible with draw.Drawer, this function doesn't return an error.
|
||||
Draw(r image.Rectangle, src image.Image, sp image.Point)
|
||||
}
|
||||
@ -1,58 +0,0 @@
|
||||
// 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{}
|
||||
@ -1,7 +0,0 @@
|
||||
// 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 contains non-hardware devices implementations for
|
||||
// testing or emulation purpose.
|
||||
package devicestest
|
||||
Loading…
Reference in New Issue