anim1d

package
v0.0.0-...-d0112ad Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 5, 2016 License: Apache-2.0 Imports: 19 Imported by: 0

Documentation

Overview

anim1d is a package to draw 1D (line) animations.

It contains all the building blocks to create animations. All the animations are designed to be stateless and serializable so multiple devices can seamless synchronize.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func FloatToUint8

func FloatToUint8(x float32) uint8

func Marshal

func Marshal(p Pattern) []byte

Marshal is a shorthand to JSON encode a pattern.

Types

type Aurore

type Aurore struct {
}

Aurore commence lentement, se transforme lentement et éventuellement disparait.

func (*Aurore) NextFrame

func (a *Aurore) NextFrame(pixels Frame, timeMS uint32)

type Color

type Color struct {
	R, G, B uint8
}

Color shows a single color on all lights. It knows how to renders itself into a frame.

If you want a single dot, use a Frame of length one.

func (*Color) Add

func (c *Color) Add(d Color)

Add adds two color together with saturation, mixing according to the alpha channel.

func (*Color) MarshalJSON

func (c *Color) MarshalJSON() ([]byte, error)

MarshalJSON encodes the color as a string "#RRGGBB".

func (*Color) Mix

func (c *Color) Mix(d Color, gradient uint8)

Mix blends the second color with the first.

gradient 0 means pure 'c', gradient 255 means pure 'd'.

func (*Color) NextFrame

func (c *Color) NextFrame(pixels Frame, timeMS uint32)

func (*Color) UnmarshalJSON

func (c *Color) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes the string "#RRGGBB" to the color.

If unmarshalling fails, 'c' is not touched.

type Crop

type Crop struct {
	Child  SPattern
	Start  int // Starting pixels to skip
	Length int // Length of the pixels to affect
}

Crop draws a subset of a strip, not touching the rest.

func (*Crop) NextFrame

func (s *Crop) NextFrame(pixels Frame, timeMS uint32)

type Cycle

type Cycle struct {
	Frames          []SPattern
	FrameDurationMS uint32
}

Cycle cycles between multiple patterns. It can be used as an animatable looping frame.

TODO(maruel): Blend between frames with TransitionType, defaults to step. TODO(maruel): Merge with Loop.

func LoadPNG

func LoadPNG(content []byte, frameDuration time.Duration, vertical bool) *Cycle

LoadPNG loads a PNG file and creates a Cycle out of the lines.

If vertical is true, rotate the image by 90°.

func (*Cycle) NextFrame

func (c *Cycle) NextFrame(pixels Frame, timeMS uint32)

type Frame

type Frame []Color

Frame is a strip of colors. It knows how to renders itself into a frame (which is recursive).

func (Frame) MarshalJSON

func (f Frame) MarshalJSON() ([]byte, error)

MarshalJSON encodes the frame as a string "LRRGGBB...".

func (Frame) Mix

func (f Frame) Mix(b Frame, gradient uint8)

Mix blends the second frame with the first.

gradient 0 means pure 'f', gradient 255 means pure 'b'.

func (Frame) NextFrame

func (f Frame) NextFrame(pixels Frame, timeMS uint32)

func (*Frame) UnmarshalJSON

func (f *Frame) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes the string "LRRGGBB..." to the colors.

If unmarshalling fails, 'f' is not touched.

type Gradient

type Gradient struct {
	Left       SPattern
	Right      SPattern
	Transition TransitionType
	// contains filtered or unexported fields
}

Gradient does a gradient between 2 patterns.

A good example is using two colors but it can also be animations.

TODO(maruel): Support N colors at M positions.

func (*Gradient) NextFrame

func (g *Gradient) NextFrame(pixels Frame, timeMS uint32)

type Loop

type Loop struct {
	Patterns             []SPattern
	DurationShowMS       uint32         // Duration for each pattern to be shown as pure
	DurationTransitionMS uint32         // Duration of the transition between two patterns
	Transition           TransitionType // Type of transition, defaults to EaseOut if not set
	// contains filtered or unexported fields
}

Loop rotates between all the animations.

Display starts with one DurationShow for Patterns[0], then starts looping. timeMS is not modified so it's like as all animations continued animating behind.

func (*Loop) NextFrame

func (l *Loop) NextFrame(pixels Frame, timeMS uint32)

type Mixer

type Mixer struct {
	Patterns []SPattern
	Weights  []float32 // In theory Sum(Weights) should be 1 but it doesn't need to. For example, mixing a night sky will likely have all of the Weights set to 1.
	// contains filtered or unexported fields
}

Mixer is a generic mixer that merges the output from multiple patterns.

It doesn't animate.

func (*Mixer) NextFrame

func (m *Mixer) NextFrame(pixels Frame, timeMS uint32)

type NightSky

type NightSky struct {
	Stars     []Cycle
	Frequency float32 // Number of explosions by second.
	// contains filtered or unexported fields
}

NightSky has:

  • Stars
  • WishingStar
  • Aurores
  • Super nova.
  • Rotation de la terre?
  • Station Internationale?

func (*NightSky) NextFrame

func (c *NightSky) NextFrame(pixels Frame, timeMS uint32)

type NightStar

type NightStar struct {
	Intensity uint8
	Type      int
}

type NightStars

type NightStars struct {
	Stars []NightStar
	Seed  int // Change it to create a different pseudo-random animation.
	// contains filtered or unexported fields
}

func (*NightStars) NextFrame

func (e *NightStars) NextFrame(pixels Frame, timeMS uint32)

type Painter

type Painter struct {
	// contains filtered or unexported fields
}

Painter handles the "draw frame, write" loop.

func MakePainter

func MakePainter(s Strip, numLights int) *Painter

MakePainter returns a Painter that manages updating the Patterns to the Strip.

func (*Painter) Close

func (p *Painter) Close() error

func (*Painter) SetPattern

func (p *Painter) SetPattern(s string) error

SetPattern changes the current pattern to a new one.

The pattern is in JSON encoded format. The function will return an error if the encoding is bad. The function is synchronous, it returns only after the pattern was effectively set.

type Pattern

type Pattern interface {
	// NextFrame fills the buffer with the image at this time frame.
	//
	// The image should be derived from timeMS, which is the time since this
	// pattern was started.
	//
	// Calling NextFrame() with a nil pattern is valid. Patterns should be
	// callable without crashing with an object initialized with default values.
	//
	// timeMS will cycle after 49.7 days. The reason it's not using time.Duration
	// is that int64 calculation on ARM is very slow and abysmal on xtensa, which
	// this code is transpiled to.
	NextFrame(pixels Frame, timeMS uint32)
}

Pattern is a interface to draw an animated line.

type PingPong

type PingPong struct {
	Child SPattern // [0] is the front pixel so the pixels are effectively drawn in reverse order.
	// TODO(maruel): PerMoveMS uint32
	MovesPerSec float32 // Expressed in number of light jumps per second.
	// contains filtered or unexported fields
}

PingPong shows a 'ball' with a trail that bounces from one side to the other.

Can be used for a ball, a water wave or K2000 (Knight Rider) style light. The trail can be a Frame or a dynamic pattern.

To get smoothed movement, use Scale{} with a 5x factor or so. TODO(maruel): That's a bit inefficient, enable ScalingType here.

func (*PingPong) NextFrame

func (p *PingPong) NextFrame(pixels Frame, timeMS uint32)

type Rainbow

type Rainbow struct {
	// contains filtered or unexported fields
}

MakeRainbow renders rainbow colors.

func (*Rainbow) MarshalJSON

func (r *Rainbow) MarshalJSON() ([]byte, error)

MarshalJSON encodes the rainbow as a string "Rainbow".

func (*Rainbow) NextFrame

func (r *Rainbow) NextFrame(pixels Frame, timeMS uint32)

func (*Rainbow) UnmarshalJSON

func (r *Rainbow) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes the string "Rainbow" to the rainbow.

type Repeated

type Repeated struct {
	Frame Frame
}

Repeated repeats a Frame to fill the pixels.

func (*Repeated) NextFrame

func (r *Repeated) NextFrame(pixels Frame, timeMS uint32)

type Rotate

type Rotate struct {
	Child       SPattern
	MovesPerSec float32 // Expressed in number of light jumps per second.
	// contains filtered or unexported fields
}

Rotate rotates a pattern that can also cycle either way.

Use negative to go left. Can be used for 'candy bar'.

Similar to PingPong{} except that it doesn't bounce.

Use 5x oversampling with Scale{} to create smoother animation.

func (*Rotate) NextFrame

func (r *Rotate) NextFrame(pixels Frame, timeMS uint32)

type SPattern

type SPattern struct {
	Pattern
}

SPattern is a Pattern that can be serialized.

It is only meant to be used in mixers.

func (*SPattern) MarshalJSON

func (p *SPattern) MarshalJSON() ([]byte, error)

func (*SPattern) UnmarshalJSON

func (p *SPattern) UnmarshalJSON(b []byte) error

UnmarshalJSON decodes a Pattern.

It knows how to decode Color, Frame or other arbitrary Pattern.

If unmarshalling fails, 'f' is not touched.

type Scale

type Scale struct {
	Child  SPattern
	Scale  ScalingType // Defaults to ScalingLinear
	Length int         // A buffer of this length will be provided to Child and will be scaled to the actual pixels length
	Ratio  float32     // Scaling ratio to use, <1 means smaller, >1 means larger. Only one of Length or Ratio can be used
	// contains filtered or unexported fields
}

Scale adapts a larger or smaller patterns to the Strip size

This is useful to create smoother animations or scale down images.

func (*Scale) NextFrame

func (s *Scale) NextFrame(pixels Frame, timeMS uint32)

type ScalingType

type ScalingType string

ScalingType specifies a way to scales a pixel strip.

const (
	ScalingNearestSkip ScalingType = "nearestskip" // Selects the nearest pixel but when upscaling, skips on missing pixels.
	ScalingNearest     ScalingType = "nearest"     // Selects the nearest pixel, gives a blocky view.
	ScalingLinear      ScalingType = "linear"      // Linear interpolation, recommended and default value.
	ScalingBilinear    ScalingType = "bilinear"    // Bilinear interpolation, usually overkill for 1D.
)

type Strip

type Strip interface {
	io.Closer
	// Write writes a new frame.
	Write(pixels Frame) error
	// MinDelay returns the minimum delay between each draw refresh.
	MinDelay() time.Duration
}

Strip is an 1D output device.

type ThumbnailsCache

type ThumbnailsCache struct {
	NumberLEDs       int // Must be set before calling Thumbnail().
	ThumbnailHz      int // Must be set before calling Thumbnail().
	ThumbnailSeconds int // Must be set before calling Thumbnail().
	// contains filtered or unexported fields
}

ThumbnailsCache is a cache of animated GIF thumbnails for each pattern.

func (*ThumbnailsCache) GIF

func (t *ThumbnailsCache) GIF(serialized []byte) ([]byte, error)

GIF returns a serialized animated GIF for a JSON serialized pattern.

type Transition

type Transition struct {
	Before     SPattern       // Old pattern that is disappearing
	After      SPattern       // New pattern to show
	OffsetMS   uint32         // Offset at which the transiton from Before->In starts
	DurationMS uint32         // Duration of the transition while both are rendered
	Transition TransitionType // Type of transition, defaults to EaseOut if not set
	// contains filtered or unexported fields
}

Transition changes from Before to After over time. It doesn't repeat.

In gets timeMS that is subtracted by OffsetMS.

func (*Transition) NextFrame

func (t *Transition) NextFrame(pixels Frame, timeMS uint32)

type TransitionType

type TransitionType string

TransitionType models visually pleasing transitions.

They are modeled against CSS transitions. https://www.w3.org/TR/web-animations/#scaling-using-a-cubic-bezier-curve

const (
	TransitionEase       TransitionType = "ease"
	TransitionEaseIn     TransitionType = "ease-in"
	TransitionEaseInOut  TransitionType = "ease-in-out"
	TransitionEaseOut    TransitionType = "ease-out" // Recommended and default value.
	TransitionLinear     TransitionType = "linear"
	TransitionStepStart  TransitionType = "steps(1,start)"
	TransitionStepMiddle TransitionType = "steps(1,middle)"
	TransitionStepEnd    TransitionType = "steps(1,end)"
)

type WishingStar

type WishingStar struct {
	Duration     time.Duration // Average duration of a star.
	AverageDelay time.Duration // Average delay between each wishing star.
}

WishingStar draws a wishing star from time to time.

It will only draw one star at a time. To increase the likelihood of getting many simultaneously, create multiple instances and use Mixer with Weights of 1.

func (*WishingStar) NextFrame

func (w *WishingStar) NextFrame(pixels Frame, timeMS uint32)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL