<commit_before>// Package ledstrip has control routines for LED strips
// Author: <NAME> (https://github.com/noxer)
package ledstrip

import (
	"github.com/noxer/tinkerforge"
	"github.com/noxer/tinkerforge/helpers"
)

// LedStrip is a control structure for LED strips
type LedStrip struct {
	t           tinkerforge.Tinkerforge
	uid         uint32
	colorMap    [3]int
	revColorMap [3]int
}

// Color represents a three byte value (8 bit for red, green and blue respectively).
type Color [3]byte

var (
	// ColorMapRGB represents the standard color mapping (R->R, G->G, B->B)
	ColorMapRGB = [3]int{0, 1, 2}
	// ColorMapBGR represents the color mapping needed when red and blue are flipped
	ColorMapBGR = [3]int{2, 1, 0}
)

// ChipType represents different types of control chips.
type ChipType uint16

const (
	// WS2801 chip type ID
	WS2801 ChipType = 2801
	// WS2811 chip type ID
	WS2811 = 2811
	// WS2812 chip type ID
	WS2812 = 2812
)

// New creates a new LED strip control for the bricklet with 'uid'.
func New(t tinkerforge.Tinkerforge, uid string) (*LedStrip, error) {
	readUID, err := helpers.Base58ToU32(uid)
	if err != nil {
		return nil, err
	}
	return &LedStrip{
		t:           t,
		uid:         readUID,
		colorMap:    [3]int{0, 1, 2},
		revColorMap: [3]int{0, 1, 2},
	}, nil
}

// SetRGBValues sets up to 16 color values beginning from 'index' to the values in 'colors'.
func (l *LedStrip) SetRGBValues(index uint16, colors []Color) error {
	// The rgb data
	r, g, b := [16]byte{}, [16]byte{}, [16]byte{}

	// Trim the slice if necessary
	if len(colors) > 16 {
		colors = colors[:16]
	}

	// Copy the colors into the arrays, apply color mapping
	for i, c := range colors {
		r[i] = c[l.colorMap[0]]
		g[i] = c[l.colorMap[1]]
		b[i] = c[l.colorMap[2]]
	}

	// Create packet
	p, err := tinkerforge.NewPacket(l.uid, 1, false, index, uint8(len(colors)), r, g, b)
	if err != nil {
		return err
	}<commit_msg>Helper function for setting more than 16 colors at a time<commit_after>// Package ledstrip has control routines for LED strips
// Author: <NAME> (https://github.com/noxer)
package ledstrip

import (
	"github.com/noxer/tinkerforge"
	"github.com/noxer/tinkerforge/helpers"
)

// LedStrip is a control structure for LED strips
type LedStrip struct {
	t           tinkerforge.Tinkerforge
	uid         uint32
	colorMap    [3]int
	revColorMap [3]int
}

// Color represents a three byte value (8 bit for red, green and blue respectively).
type Color [3]byte

var (
	// ColorMapRGB represents the standard color mapping (R->R, G->G, B->B)
	ColorMapRGB = [3]int{0, 1, 2}
	// ColorMapBGR represents the color mapping needed when red and blue are flipped
	ColorMapBGR = [3]int{2, 1, 0}
)

// ChipType represents different types of control chips.
type ChipType uint16

const (
	// WS2801 chip type ID
	WS2801 ChipType = 2801
	// WS2811 chip type ID
	WS2811 = 2811
	// WS2812 chip type ID
	WS2812 = 2812
)

// New creates a new LED strip control for the bricklet with 'uid'.
func New(t tinkerforge.Tinkerforge, uid string) (*LedStrip, error) {
	readUID, err := helpers.Base58ToU32(uid)
	if err != nil {
		return nil, err
	}
	return &LedStrip{
		t:           t,
		uid:         readUID,
		colorMap:    [3]int{0, 1, 2},
		revColorMap: [3]int{0, 1, 2},
	}, nil
}

// SetAllRGBValues sets all color values beginning from 'index' to the values in 'colors'.
func (l *LedStrip) SetAllRGBValues(index uint16, colors []Color) error {
	for len(colors) > 0 {
		if err := l.SetRGBValues(index, colors); err != nil {
			return err
		}

		// calculate the remaining slice
		colors = colors[min(len(colors), 16):]
		index += 16
	}

	return nil
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// SetRGBValues sets up to 16 color values beginning from 'index' to the values in 'colors'.
func (l *LedStrip) SetRGBValues(index uint16, colors []Color) error {
	// The rgb data
	r, g, b := [16]byte{}, [16]byte{}, [16]byte{}

	// Trim the slice if necessary
	if len(colors) > 16 {
		colors = colors[:16]
	}

	// Copy the colors into the arrays, apply color mapping
	for i, c := range colors {
		r[i] = c[l.colorMap[0]]
		g[i] = c[l.colorMap[1]]
		b[i] = c[l.colorMap[2]]
	}

	// Create packet
	p, err := tinkerforge.NewPacket(l.uid, 1, false, index, uint8(len(colors)), r, g, b)
	if err != nil {
		return err
	}
{'fraction_non_alphanumeric': 0.12572488981674784, 'fraction_numerical': 0.040593829737879845, 'mean_word_length': 3.256663376110563, 'pattern_counts': {'":': 0, '<': 6, '<?xml version=': 0, '>': 14, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6396768', 'n_tokens_mistral': 1730, 'n_tokens_neox': 1555, 'n_words': 535}