<commit_before>package leds

import (
	"fmt"
	"math/rand"

	"github.com/siggy/rpi_ws281x/golang/ws2811"
)

const (
	// 2x side fins
	STRAND_COUNT1 = 5
	STRAND_LEN1   = 144

	// 1x top and back fins
	STRAND_COUNT2 = 10
	STRAND_LEN2   = 60

	LED_COUNT1 = STRAND_COUNT1 * STRAND_LEN1 // 5*144 // * 2x(5) // 144/m
	LED_COUNT2 = STRAND_COUNT2 * STRAND_LEN2 // 10*60 // * 1x(4 + 2 + 4) // 60/m
)

type Fish struct {
	ampLevel float64
	closing  chan struct{}
	level    <-chan float64
	press    <-chan struct{}
}

func InitFish(level <-chan float64, press <-chan struct{}) *Fish {
	InitLeds(LED_COUNT1, LED_COUNT2)

	return &Fish{
		closing: make(chan struct{}),
		level:   level,
		press:   press,
	}
}

func (f *Fish) Run() {
	defer func() {
		ws2811.Clear()
		ws2811.Render()
		ws2811.Wait()
		ws2811.Fini()
	}()

	ws2811.Clear()

	strand1 := make([]uint32, LED_COUNT1)
	strand2 := make([]uint32, LED_COUNT2)

	mode := EQUALIZE

	// STANDARD mode
	iter := 0
	weight := float64(0)

	// FLICKER mode
	flickerIter := 0

	// precompute random color rotation
	randColors := make([]uint32, LED_COUNT1)
	for i := 0; i < LED_COUNT1; i++ {
		randColors[i] = MkColor(0, uint32(rand.Int31n(256)), uint32(rand.Int31n(256)), uint32(rand.Int31n(256)))
	}

	for {
		select {
		case _, more := <-f.press:
			if more {
				mode = (mode + 1) % NUM_MODES
			} else {
				return
			}
		case level, more := <-f.level:
			if more {
				f.ampLevel = level
			} else {
				return
			}
		case _, more := <-f.closing:
			if !more {
				return
			}
		default:
			ampLevel := uint32(255.0 * f.ampLevel)

			switch mode {
			case EQUALIZE:
				ampLevel1 := int(LED_COUNT1 * f.ampLevel)
				ampLevel2 := int(LED_COUNT2 * f.ampLevel)

				for i := 0; i < STRAND_COUNT1; i++ {
					color1 := Colors[(iter+i)%len(Colors)]
					color2 := Colors[(iter+i+1)%len(Colors)]
					color := MkColorWeight(color1, color2, weight)
					ampColor := AmpColor(color, 255)

					for j := 0; j < STRAND_LEN1; j++ {
						idx := i*STRAND_LEN1 + j
						if idx < ampLevel1 {
							strand1[idx] = ampColor
						} else {
							strand1[idx] = color
						}
					}
				}

				for i := 0; i < STRAND_COUNT2; i++ {
					color1 := Colors[(iter+i)%len(Colors)]
					color2 := Colors[(iter+i+1)%len(Colors)]<commit_msg>decrease fish brightness and audio amplitude<commit_after>package leds

import (
	"fmt"
	"math/rand"

	"github.com/siggy/rpi_ws281x/golang/ws2811"
)

const (
	// 2x side fins
	STRAND_COUNT1 = 5
	STRAND_LEN1   = 144

	// 1x top and back fins
	STRAND_COUNT2 = 10
	STRAND_LEN2   = 60

	LED_COUNT1 = STRAND_COUNT1 * STRAND_LEN1 // 5*144 // * 2x(5) // 144/m
	LED_COUNT2 = STRAND_COUNT2 * STRAND_LEN2 // 10*60 // * 1x(4 + 2 + 4) // 60/m

	AMPLITUDE_FACTOR = 0.75
)

type Fish struct {
	ampLevel float64
	closing  chan struct{}
	level    <-chan float64
	press    <-chan struct{}
}

func InitFish(level <-chan float64, press <-chan struct{}) *Fish {
	InitLeds(LED_COUNT1, LED_COUNT2)

	return &Fish{
		closing: make(chan struct{}),
		level:   level,
		press:   press,
	}
}

func (f *Fish) Run() {
	defer func() {
		ws2811.Clear()
		ws2811.Render()
		ws2811.Wait()
		ws2811.Fini()
	}()

	ws2811.Clear()

	strand1 := make([]uint32, LED_COUNT1)
	strand2 := make([]uint32, LED_COUNT2)

	mode := EQUALIZE

	// STANDARD mode
	iter := 0
	weight := float64(0)

	// FLICKER mode
	flickerIter := 0

	// precompute random color rotation
	randColors := make([]uint32, LED_COUNT1)
	for i := 0; i < LED_COUNT1; i++ {
		randColors[i] = MkColor(0, uint32(rand.Int31n(256)), uint32(rand.Int31n(256)), uint32(rand.Int31n(128)))
	}

	for {
		select {
		case _, more := <-f.press:
			if more {
				mode = (mode + 1) % NUM_MODES
			} else {
				return
			}
		case level, more := <-f.level:
			if more {
				f.ampLevel = level
			} else {
				return
			}
		case _, more := <-f.closing:
			if !more {
				return
			}
		default:
			ampLevel := uint32(255.0 * f.ampLevel * AMPLITUDE_FACTOR)

			switch mode {
			case EQUALIZE:
				ampLevel1 := int(LED_COUNT1 * f.ampLevel)
				ampLevel2 := int(LED_COUNT2 * f.ampLevel)

				for i := 0; i < STRAND_COUNT1; i++ {
					color1 := Colors[(iter+i)%len(Colors)]
					color2 := Colors[(iter+i+1)%len(Colors)]
					color := MkColorWeight(color1, color2, weight)
					ampColor := AmpColor(color, 255)

					for j := 0; j < STRAND_LEN1; j++ {
						idx := i*STRAND_LEN1 + j
						if idx < ampLevel1 {
							strand1[idx] = ampColor
						} else {
							strand1[idx] = color
						}
					}
				}

				for i := 0; i < STRAND_COUNT2; i++ {
					color1 := Colors[(iter+i)%len(Colors)]
					color2 := Colors[(iter+i+1)%len(Colors)]
{'fraction_non_alphanumeric': 0.1514161220043573, 'fraction_numerical': 0.06427015250544663, 'mean_word_length': 2.7631147540983605, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 3, 'https://': 0, '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': '4692868', 'n_tokens_mistral': 2328, 'n_tokens_neox': 1847, 'n_words': 415}