Checkpoint

This commit is contained in:
maze authored and maze committed 2026-10-29 17:29:54 +01:00
1 parent c5d6137fab
commit 4ae7655cd8
223 files changed
+14770 -18352

No files matched your search

+72
View File
@@ -0,0 +1,72 @@
package ioctl
import (
"fmt"
"reflect"
"syscall"
)
// Mode is the IOCTL mode.
type Mode uint8
// Modes
const (
None Mode = iota
Write
Read
)
// Command to be sent over ioctl.
type Command uintptr
func (c Command) String() string {
var (
mode = Mode(c >> 30 & 0x03)
size = c >> 16 & 0x3fff
cmd = c & 0xffff
str string
)
if mode&Write > 0 {
str += " write"
}
if mode&Read > 0 {
str += " read "
}
return fmt.Sprintf("ioctl%s (%d bytes) 0x%04x", str, size, uintptr(cmd))
}
// Do executes the ioctl call.
func Do(fd uintptr, command Command, ptr interface{}) error {
var p uintptr
if ptr != nil {
v := reflect.ValueOf(ptr)
p = v.Pointer()
}
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, uintptr(command), p)
if errno != 0 {
return fmt.Errorf("ioctl %s failed: %v", command, errno)
}
return nil
}
// Call does a plain ioctl system call.
func Call(fd, command, arg uintptr) error {
_, _, errno := syscall.Syscall(syscall.SYS_IOCTL, fd, command, arg)
if errno != 0 {
return fmt.Errorf("ioctl %s failed: %v", Command(command), errno)
}
return nil
}
// Encode an ioctl command.
func Encode(mode Mode, size uint16, cmd uintptr) Command {
return Command(mode)<<30 | Command(size)<<16 | Command(cmd)
}
// Pointer to a value.
func Pointer(mode Mode, ref interface{}, cmd uintptr) Command {
size := uint16(reflect.TypeOf(ref).Elem().Size())
return Encode(mode, size, cmd)
}
-221
View File
@@ -1,221 +0,0 @@
package pixel
import "image/color"
// Pixel with a particular color, matching the underlying hardware of a
// particular display. Each pixel is at least 1 byte in size.
// The color format is sRGB (or close to it) in all cases except for 1-bit.
type Color interface {
Monochrome | RGB444 | RGB565
BaseColor
}
// BaseColor contains all the methods needed in a color format. This can be used
// in display drivers that want to define their own Color type with just the
// pixel formats the display supports.
type BaseColor interface {
color.Color
// The number of bits when stored.
// This means for example that RGB555 (which is still stored as a 16-bit
// integer) returns 16, while RGB444 returns 12.
BitsPerPixel() int
// Return the given color in color.RGBA format, which is always sRGB. The
// alpha channel is always 255.
RGBAColor() color.RGBA
}
// NewColor returns the given color based on the RGB values passed in the
// parameters. The input value is assumed to be in sRGB color space.
func NewColor[T Color](r, g, b uint8) T {
// Ugly cast from color.RGBA to T. The type switch and interface casts are
// trivially optimized away after instantiation.
var value T
switch any(value).(type) {
case Monochrome:
return any(NewMonochrome(r, g, b)).(T)
case RGB444:
return any(NewRGB444(r, g, b)).(T)
case RGB565:
return any(NewRGB565(r, g, b)).(T)
/*
case RGB555:
return any(NewRGB555(r, g, b)).(T)
case RGB888:
return any(NewRGB888(r, g, b)).(T)
*/
default:
panic("unknown color format")
}
}
// ToGrayBits converts a color to a n-bit grayscale color.
func ToGrayBits(c color.Color, n uint8) uint16 {
r, g, b, _ := c.RGBA()
if n < 1 {
return 0 // no color
} else if n > 16 {
n = 16 // most that will fit
}
// Shift values to fit n bits.
s := 32 - n
// These coefficients (the fractions 0.299, 0.587 and 0.114) are the same
// as those given by the JFIF specification and used by func RGBToYCbCr in
// image/color/ycbcr.go.
//
// Note that 19595 + 38470 + 7471 equals 65536.
//
// The 28 is 16 + 8 + n. The 16 is the same as used in RGBToYCbCr. The 8 + n is
// because the return value is n-bit color, not 16 bit color.
return uint16((19595*r + 38470*g + 7471*b + 1<<15) >> s)
}
// Monochrome color.
type Monochrome uint8
// Monochrome color names.
const (
Off Monochrome = iota
On
)
func NewMonochrome(r, g, b uint8) Monochrome {
if ToGrayBits(color.RGBA{r, g, b, 0xff}, 1) == 1 {
return On
}
return Off
}
func (c Monochrome) RGBA() (r, g, b, a uint32) {
if c == 0 {
return 0, 0, 0, 0
}
return 0xffff, 0xffff, 0xffff, 0xffff
}
func IsOn(c color.Color) bool {
if _, ok := c.(Monochrome); ok {
return c == On
}
return ToGrayBits(c, 1) == 1
}
// Color format that is supported by the ST7789 for example.
// It may be a bit faster to use than RGB565BE on very slow SPI buses.
//
// The color format is native endian as a uint16 (0000rrrr_ggggbbbb), not big
// endian which you might expect. I tried swapping the bytes, but it didn't have
// much of a performance impact and made the code harder to read. It is stored
// as a 12-bit big endian value in Image[RGB444] though.
type RGB444 uint16
func NewRGB444(r, g, b uint8) RGB444 {
return RGB444(r>>4)<<8 | RGB444(g>>4)<<4 | RGB444(b>>4)
}
func (c RGB444) BitsPerPixel() int {
return 12
}
func (c RGB444) RGBA() (r, g, b, a uint32) {
r = uint32(c>>8) & 0x0f
r |= (r << 4) | (r << 8) | (r << 12)
g = uint32(c>>4) & 0x0f
g |= (g << 4) | (g << 8) | (g << 12)
b = uint32(c>>0) & 0x0f
b |= (b << 4) | (b << 8) | (b << 12)
a |= 0xffff
return
}
func (c RGB444) RGBAColor() color.RGBA {
color := color.RGBA{
R: uint8(c>>8) << 4,
G: uint8(c>>4) << 4,
B: uint8(c>>0) << 4,
A: 255,
}
// Correct color rounding, so that 0xff roundtrips back to 0xff.
color.R |= color.R >> 4
color.G |= color.G >> 4
color.B |= color.B >> 4
return color
}
// RGB565 color.
type RGB565 uint16
func NewRGB565(r, g, b uint8) RGB565 {
val := uint16(r&0xF8)<<8 +
uint16(g&0xFC)<<3 +
uint16(b&0xF8)>>3
// Swap endianness (make big endian).
// This is done using a single instruction on ARM (rev16).
// TODO: this should only be done on little endian systems, but TinyGo
// doesn't currently (2023) support big endian systems so it's difficult to
// test. Also, big endian systems don't seem fasionable these days.
//val = bits.ReverseBytes16(val)
return RGB565(val)
}
// FromColor retusn the given color.
func FromColor[T Color](c color.Color) T {
var value T
switch any(value).(type) {
case Monochrome:
return monochromeModel(c).(T)
case RGB444:
return rgb444Model(c).(T)
case RGB565:
return rgb565Model(c).(T)
/*
case RGB555:
return rgb555Model(c).(T)
case RGB888:
return rgb888Model(c).(T)
*/
default:
panic("unknown color format")
}
}
func (c RGB565) BitsPerPixel() int {
return 16
}
func (c RGB565) RGBA() (r, g, b, a uint32) {
r = uint32(uint8(c>>12) << 3)
r |= (r >> 5)
r |= (r << 8)
g = uint32(uint8(c>>5) << 2)
g |= (g >> 6)
g |= (g << 8)
b = uint32(uint8(c>>0) << 3)
b |= (b >> 5)
b |= (b << 8)
a = 0xffff
return
}
func (c RGB565) RGBAColor() color.RGBA {
// Note: on ARM, the compiler uses a rev instruction instead of a rev16
// instruction. I wonder whether this can be optimized further to use rev16
// instead?
c = c<<8 | c>>8
color := color.RGBA{
R: uint8(c>>11) << 3,
G: uint8(c>>5) << 2,
B: uint8(c) << 3,
A: 255,
}
// Correct color rounding, so that 0xff roundtrips back to 0xff.
color.R |= color.R >> 5
color.G |= color.G >> 6
color.B |= color.B >> 5
return color
}
-476
View File
@@ -1,476 +0,0 @@
package pixel
import (
"image"
"image/color"
"image/draw"
"unsafe"
)
// Image buffer, used for working with the native image format of various
// displays. It works a lot like a slice: it can be rescaled while reusing the
// underlying buffer and should be passed around by value.
type Image[T Color] struct {
width int16
height int16
data unsafe.Pointer
}
// NewImage creates a new image of the given size.
func NewImage[T Color](width, height int) Image[T] {
if width < 0 || height < 0 || int(int16(width)) != width || int(int16(height)) != height {
// The width/height are stored as 16-bit integers and should never be
// negative.
panic("NewImage: width/height out of bounds")
}
var zeroColor T
var data unsafe.Pointer
switch {
case zeroColor.BitsPerPixel()%8 == 0:
// Typical formats like RGB888 and RGB565.
// Each color starts at a whole byte offset from the start.
buf := make([]T, width*height)
data = unsafe.Pointer(&buf[0])
default:
// Formats like RGB444 that have 12 bits per pixel.
// We access these as bytes, so allocate the buffer as a byte slice.
bufBits := width * height * zeroColor.BitsPerPixel()
bufBytes := (bufBits + 7) / 8
buf := make([]byte, bufBytes)
data = unsafe.Pointer(&buf[0])
}
return Image[T]{
width: int16(width),
height: int16(height),
data: data,
}
}
// NewImageFromBytes creates a new image of the given size using an existing data slice of bytes.
func NewImageFromBytes[T Color](width, height int, buf []byte) Image[T] {
if width < 0 || height < 0 || int(int16(width)) != width || int(int16(height)) != height {
// The width/height are stored as 16-bit integers and should never be
// negative.
panic("NewImageFromBytes: width/height out of bounds")
}
var zeroColor T
var data unsafe.Pointer
switch {
case zeroColor.BitsPerPixel()%8 == 0:
// Typical formats like RGB888 and RGB565.
// Each color starts at a whole byte offset from the start.
if len(buf) != width*height*int(unsafe.Sizeof(zeroColor)) {
panic("NewImageFromBytes: data slice size mismatch")
}
data = unsafe.Pointer(&buf[0])
default:
// Formats like RGB444 that have 12 bits per pixel.
// We access these as bytes, so allocate the buffer as a byte slice.
bufBits := width * height * zeroColor.BitsPerPixel()
bufBytes := (bufBits + 7) / 8
if len(buf) != bufBytes {
panic("NewImageFromBytes: data slice size mismatch")
}
data = unsafe.Pointer(&buf[0])
}
return Image[T]{
width: int16(width),
height: int16(height),
data: data,
}
}
// NewImageFromImage creates a new image from the provided image.
func NewImageFromImage[T Color](src image.Image) Image[T] {
var (
r = src.Bounds()
width = r.Dx()
height = r.Dy()
dst = NewImage[T](width, height)
)
draw.Draw(dst, r, src, image.Point{}, draw.Src)
return dst
}
// Rescale returns a new Image buffer based on the img buffer.
// The contents is undefined after the Rescale operation, and any modification
// to the returned image will overwrite the underlying image buffer in undefined
// ways. It will panic if width*height is larger than img.Len().
func (img Image[T]) Rescale(width, height int) Image[T] {
if width*height > img.Len() {
panic("Image.Rescale size out of bounds")
}
return Image[T]{
width: int16(width),
height: int16(height),
data: img.data,
}
}
// LimitHeight returns a subimage with the bottom part cut off, as specified by
// height.
func (img Image[T]) LimitHeight(height int) Image[T] {
if height < 0 || height > int(img.height) {
panic("Image.LimitHeight: out of bounds")
}
return Image[T]{
width: img.width,
height: int16(height),
data: img.data,
}
}
// Len returns the number of pixels in this image buffer.
func (img Image[T]) Len() int {
return int(img.width) * int(img.height)
}
// RawBuffer returns a byte slice that can be written directly to the screen
// using DrawRGBBitmap8.
func (img Image[T]) RawBuffer() []uint8 {
var zeroColor T
var numBytes int
switch {
case zeroColor.BitsPerPixel()%8 == 0:
// Each color starts at a whole byte offset.
numBytes = int(unsafe.Sizeof(zeroColor)) * int(img.width) * int(img.height)
default:
// Formats like RGB444 that aren't a whole number of bytes.
numBits := zeroColor.BitsPerPixel() * int(img.width) * int(img.height)
numBytes = (numBits + 7) / 8 // round up (see NewImage)
}
return unsafe.Slice((*byte)(img.data), numBytes)
}
// Size returns the image size.
func (img Image[T]) Size() (int, int) {
return int(img.width), int(img.height)
}
func (img Image[T]) setPixel(index int, c T) {
var zeroColor T
switch {
case zeroColor.BitsPerPixel() == 1:
// Monochrome.
offset := index / 8
bits := index % 8
ptr := (*byte)(unsafe.Add(img.data, offset))
if c != zeroColor {
*((*byte)(ptr)) |= (1 << (7 - uint8(bits)))
} else {
*((*byte)(ptr)) &^= (1 << (7 - uint8(bits)))
}
return
case zeroColor.BitsPerPixel()%8 == 0:
// Each color starts at a whole byte offset.
// This is the easy case.
offset := index * int(unsafe.Sizeof(zeroColor))
ptr := unsafe.Add(img.data, offset)
*((*T)(ptr)) = c
return
}
if c, ok := any(c).(RGB444); ok {
// Special case for RGB444.
bitIndex := index * zeroColor.BitsPerPixel()
if bitIndex%8 == 0 {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
ptr[0] = uint8(c >> 4)
ptr[1] = ptr[1]&0x0f | uint8(c)<<4 // change top bits
} else {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
ptr[0] = ptr[0]&0xf0 | uint8(c>>8) // change bottom bits
ptr[1] = uint8(c)
}
return
}
// TODO: the code for RGB444 should be generalized to support any bit size.
panic("todo: setPixel for odd bits per pixel")
}
// Set sets the pixel at x, y to the given color.
// Use FillSolidColor to efficiently fill the entire image buffer.
func (img Image[T]) SetPixel(x, y int, c T) {
if uint(x) >= uint(int(img.width)) || uint(y) >= uint(int(img.height)) {
return
}
index := y*int(img.width) + x
img.setPixel(index, c)
}
// Get returns the color at the given index.
func (img Image[T]) GetPixel(x, y int) T {
var zeroColor T
if uint(x) >= uint(int(img.width)) || uint(y) >= uint(int(img.height)) {
return zeroColor
}
index := y*int(img.width) + x // index into img.data
switch {
case zeroColor.BitsPerPixel() == 1:
// Monochrome.
var c Monochrome
offset := index / 8
bits := index % 8
ptr := (*byte)(unsafe.Add(img.data, offset))
c = Monochrome((*ptr >> (7 - uint8(bits))) & 0x1)
return any(c).(T)
case zeroColor.BitsPerPixel()%8 == 0:
// Colors like RGB565, RGB888, etc.
offset := index * int(unsafe.Sizeof(zeroColor))
ptr := unsafe.Add(img.data, offset)
return *((*T)(ptr))
}
if _, ok := any(zeroColor).(RGB444); ok {
// Special case for RGB444 that isn't stored in a neat byte multiple.
bitIndex := index * zeroColor.BitsPerPixel()
var c RGB444
if bitIndex%8 == 0 {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
c |= RGB444(ptr[0]) << 4
c |= RGB444(ptr[1] >> 4) // load top bits
} else {
byteOffset := bitIndex / 8
ptr := (*[2]byte)(unsafe.Add(img.data, byteOffset))
c |= RGB444(ptr[0]&0x0f) << 8 // load bottom bits
c |= RGB444(ptr[1])
}
return any(c).(T)
}
// TODO: generalize the above code.
panic("todo: Image.Get for odd bits per pixel")
}
// FillSolidColor fills the entire image with the given color.
// This may be faster than setting individual pixels.
func (img Image[T]) FillSolidColor(color T) {
var zeroColor T
switch {
case zeroColor.BitsPerPixel() == 1:
// Monochrome.
var colorByte uint8
if color != zeroColor {
colorByte = 0xff
}
numBytes := int(img.width) * int(img.height) / 8
for i := 0; i < numBytes; i++ {
// TODO: this can be optimized a lot.
// - The store can be done as a 32-bit integer, after checking for
// alignment.
// - Perhaps the loop can be unrolled to improve copy performance.
ptr := (*byte)(unsafe.Add(img.data, i))
*((*byte)(ptr)) = colorByte
}
return
case zeroColor.BitsPerPixel()%8 == 0:
// Fast pass for colors of 8, 16, 24, etc bytes in size.
ptr := img.data
for i := 0; i < img.Len(); i++ {
// TODO: this can be optimized a lot.
// - The store can be done as a 32-bit integer, after checking for
// alignment.
// - Perhaps the loop can be unrolled to improve copy performance.
*(*T)(ptr) = color
ptr = unsafe.Add(ptr, unsafe.Sizeof(zeroColor))
}
return
}
// Special case for RGB444.
if c, ok := any(color).(RGB444); ok {
// RGB444 can be stored in a more optimized way, by storing two colors
// at a time instead of setting each color individually. This avoids
// loading and masking the old color bits for the half-bytes.
var buf [3]uint8
buf[0] = uint8(c >> 4)
buf[1] = uint8(c)<<4 | uint8(c>>8)
buf[2] = uint8(c)
rawBuf := unsafe.Slice((*[3]byte)(img.data), img.Len()/2)
for i := 0; i < len(rawBuf); i++ {
rawBuf[i] = buf
}
if img.Len()%2 != 0 {
// The image contains an uneven number of pixels.
// This is uncommon, but it can happen and we have to handle it.
img.setPixel(img.Len()-1, color)
}
return
}
// Fallback for other color formats.
for i := 0; i < img.Len(); i++ {
img.setPixel(i, color)
}
}
// At implements the draw.Image interface.
func (img Image[T]) At(x, y int) color.Color {
return img.GetPixel(x, y)
}
// Set implements the draw.Image interface.
func (img Image[T]) Set(x, y int, c color.Color) {
var value T
switch any(value).(type) {
case Monochrome:
img.SetPixel(x, y, monochromeModel(c).(T))
case RGB444:
img.SetPixel(x, y, rgb444Model(c).(T))
case RGB565:
img.SetPixel(x, y, rgb565Model(c).(T))
/*
case RGB888:
img.SetPixel(x, y, rgb888Model(c).(T))
case RGB555:
img.SetPixel(x, y, rgb555Model(c).(T))
*/
default:
}
}
// Bounds returns the image bounding box.
func (img Image[T]) Bounds() image.Rectangle {
return image.Rect(0, 0, int(img.width), int(img.height))
}
// ColorModel returns the image color model.
func (img Image[T]) ColorModel() color.Model {
// Ugly cast from color.RGBA to T. The type switch and interface casts are
// trivially optimized away after instantiation.
var value T
switch any(value).(type) {
case Monochrome:
return MonochromeModel
case RGB444:
return RGB444Model
case RGB565:
return RGB565Model
/*
case RGB888:
return RGB888Model
case RGB555:
return RGB555Model
*/
default:
panic("unknown color format")
}
}
// Clear the image buffer.
func (img *Image[T]) Clear() {
var zeroColor T
img.FillSolidColor(zeroColor)
}
// VerticalLSB is a 1 bit (black and white) image.
//
// Each byte is 8 vertical pixels. Each stride is a horizontal band of 8
// pixels high with LSB first. So the first byte represents the following
// pixels, with lowest bit being the top left pixel.
//
// 0 x x x x x x x
// 1 x x x x x x x
// 2 x x x x x x x
// 3 x x x x x x x
// 4 x x x x x x x
// 5 x x x x x x x
// 6 x x x x x x x
// 7 x x x x x x x
//
// It is designed specifically to work with SSD1306 OLED display controller.
type VerticalLSB struct {
// Pix holds the image's pixels, as vertically LSB-first packed bitmap. It
// can be passed directly to ssd1306.Dev.Write()
Pix []byte
// Stride is the Pix stride (in bytes) between vertically adjacent 8 pixels
// horizontal bands.
Stride int
// Rect is the image's bounds.
Rect image.Rectangle
}
// NewVerticalLSB returns an initialized VerticalLSB instance.
func NewVerticalLSB(r image.Rectangle) *VerticalLSB {
w := r.Dx()
// Round down.
minY := r.Min.Y &^ 7
// Round up.
maxY := (r.Max.Y + 7) & ^7
bands := (maxY - minY) / 8
return &VerticalLSB{Pix: make([]byte, w*bands), Stride: w, Rect: r}
}
// ColorModel implements image.Image.
func (i *VerticalLSB) ColorModel() color.Model {
return MonochromeModel
}
// Bounds implements image.Image.
func (i *VerticalLSB) Bounds() image.Rectangle {
return i.Rect
}
// At implements image.Image.
func (i *VerticalLSB) At(x, y int) color.Color {
return i.BitAt(x, y)
}
// BitAt is the optimized version of At().
func (i *VerticalLSB) BitAt(x, y int) Monochrome {
if !(image.Point{x, y}.In(i.Rect)) {
return Off
}
offset, mask := i.PixOffset(x, y)
if i.Pix[offset]&mask != 0 {
return On
}
return Off
}
// Opaque scans the entire image and reports whether it is fully opaque.
func (i *VerticalLSB) Opaque() bool {
return true
}
// PixOffset returns the index of the first element of Pix that corresponds to
// the pixel at (x, y) and the corresponding mask.
func (i *VerticalLSB) PixOffset(x, y int) (int, byte) {
// Adjust band.
minY := i.Rect.Min.Y &^ 7
pY := (y - minY)
offset := pY/8*i.Stride + (x - i.Rect.Min.X)
bit := uint(pY & 7)
return offset, 1 << bit
}
// Set implements draw.Image
func (i *VerticalLSB) Set(x, y int, c color.Color) {
if ToGrayBits(c, 1) == 0 {
i.SetBit(x, y, Off)
}
i.SetBit(x, y, On)
}
// SetBit is the optimized version of Set().
func (i *VerticalLSB) SetBit(x, y int, b Monochrome) {
if !(image.Point{x, y}.In(i.Rect)) {
return
}
offset, mask := i.PixOffset(x, y)
if b == On {
i.Pix[offset] |= mask
} else {
i.Pix[offset] &^= mask
}
}
-33
View File
@@ -1,33 +0,0 @@
package pixel
import "image/color"
var (
MonochromeModel color.Model = color.ModelFunc(monochromeModel)
RGB444Model color.Model = color.ModelFunc(rgb444Model)
RGB565Model color.Model = color.ModelFunc(rgb565Model)
)
func monochromeModel(c color.Color) color.Color {
if _, ok := c.(Monochrome); ok {
return c
}
r, g, b, _ := c.RGBA()
return NewMonochrome(uint8(r>>8), uint8(g>>8), uint8(b>>8))
}
func rgb444Model(c color.Color) color.Color {
if _, ok := c.(RGB444); ok {
return c
}
r, g, b, _ := c.RGBA()
return RGB444(r>>12)<<8 | RGB444(g>>12)<<4 | RGB444(b>>12)
}
func rgb565Model(c color.Color) color.Color {
if _, ok := c.(RGB565); ok {
return c
}
r, g, b, _ := c.RGBA()
return RGB565(uint16((r>>8)&0xF8)<<8 + uint16((g>>8)&0xFC)<<3 + uint16((b>>8)&0xF8)>>3)
}