Skip to content

Commit

Permalink
Merge pull request #101 from Unreal-Dan/kurt/randomizer-consistency-2
Browse files Browse the repository at this point in the history
kurt/randomizer-consistency-2
  • Loading branch information
livingkurt authored Dec 8, 2024
2 parents 44bba6e + ad4c9f4 commit 685af7e
Show file tree
Hide file tree
Showing 20 changed files with 32,916 additions and 32,892 deletions.
18 changes: 9 additions & 9 deletions Helios/Colorset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -181,19 +181,19 @@ void Colorset::removeColor(uint8_t index)
m_palette[--m_numColors].clear();
}

uint8_t current_color_mode = Colorset::THEORY;

void Colorset::randomizeColors(Random &ctx, uint8_t numColors)
void Colorset::randomizeColors(Random &ctx, uint8_t numColors, ColorMode mode)
{
current_color_mode = (current_color_mode + 1) % COLOR_MODE_COUNT;
ColorMode mode = (ColorMode)current_color_mode;
// if they specify randomly pick the color mode then roll it
if (mode >= COLOR_MODE_RANDOMLY_PICK) {
mode = (ColorMode)(ctx.next8() % COLOR_MODE_COUNT);
}
clear();
if (!numColors) {
numColors = ctx.next8(mode == MONOCHROMATIC ? 2 : 1, 9);
numColors = ctx.next8(mode == COLOR_MODE_MONOCHROMATIC ? 2 : 1, 9);
}
uint8_t randomizedHue = ctx.next8();
uint8_t colorGap = 0;
if (mode == THEORY && numColors > 1) {
if (mode == COLOR_MODE_COLOR_THEORY && numColors > 1) {
colorGap = ctx.next8(16, 256 / (numColors - 1));
}
ValueStyle valStyle = (ValueStyle)ctx.next8(0, VAL_STYLE_COUNT);
Expand All @@ -208,9 +208,9 @@ void Colorset::randomizeColors(Random &ctx, uint8_t numColors)
for (uint8_t i = 0; i < numColors; i++) {
uint8_t hueToUse;
uint8_t valueToUse = 255;
if (mode == THEORY) {
if (mode == COLOR_MODE_COLOR_THEORY) {
hueToUse = (randomizedHue + (i * colorGap));
} else if (mode == MONOCHROMATIC) {
} else if (mode == COLOR_MODE_MONOCHROMATIC) {
hueToUse = randomizedHue;
valueToUse = 255 - (i * (256 / numColors));
} else { // EVENLY_SPACED
Expand Down
22 changes: 15 additions & 7 deletions Helios/Colorset.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,15 +71,23 @@ class Colorset
ValueStyle valStyle, uint8_t numColors, uint8_t colorPos);
void removeColor(uint8_t index);


// function to randomize the colors with various different modes of randomization
// various modes of randomization types to use with randomizeColors
enum ColorMode {
THEORY,
MONOCHROMATIC,
EVENLY_SPACED,
COLOR_MODE_COUNT
// randomize with color theory
COLOR_MODE_COLOR_THEORY,
// randomize a nonochromatic set
COLOR_MODE_MONOCHROMATIC,
// randomize an evenly spaced hue set
COLOR_MODE_EVENLY_SPACED,

// total different randomize modes above
COLOR_MODE_COUNT,

// EXTRA OPTION: randomly pick one of the other 3 options
COLOR_MODE_RANDOMLY_PICK = COLOR_MODE_COUNT,
};
void randomizeColors(Random &ctx, uint8_t numColors);
// function to randomize the colors with various different modes of randomization
void randomizeColors(Random &ctx, uint8_t numColors, ColorMode color_mode);

// fade all of the colors in the set
void adjustBrightness(uint8_t fadeby);
Expand Down
4 changes: 2 additions & 2 deletions Helios/Helios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -807,9 +807,9 @@ void Helios::handle_state_randomize()
{
if (Button::onShortClick()) {
Colorset &cur_set = pat.colorset();
Random ctx(cur_set.crc32());
Random ctx(pat.crc32());
uint8_t randVal = ctx.next8();
cur_set.randomizeColors(ctx, (randVal + 1) % NUM_COLOR_SLOTS);
cur_set.randomizeColors(ctx, (randVal + 1) % NUM_COLOR_SLOTS, Colorset::COLOR_MODE_RANDOMLY_PICK);
Patterns::make_pattern((PatternID)(randVal % PATTERN_COUNT), pat);
pat.init();
}
Expand Down
9 changes: 9 additions & 0 deletions Helios/Pattern.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,15 @@ void Pattern::updateColor(uint8_t index, const RGBColor &col)
init();
}

uint32_t Pattern::crc32() const
{
uint32_t hash = 5381;
for (uint8_t i = 0; i < PATTERN_SIZE; ++i) {
hash = ((hash << 5) + hash) + ((uint8_t *)this)[i];
}
return hash;
}

void Pattern::blendBlinkOn()
{
// if we reached the next color, then cycle the colorset
Expand Down
3 changes: 3 additions & 0 deletions Helios/Pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,9 @@ class Pattern
// set a color in the colorset and re-initialize
void updateColor(uint8_t index, const RGBColor &col);

// calculate crc of the colorset + pattern
uint32_t crc32() const;

// get the pattern flags
uint32_t getFlags() const { return m_patternFlags; }
bool hasFlags(uint32_t flags) const { return (m_patternFlags & flags) != 0; }
Expand Down
10 changes: 7 additions & 3 deletions HeliosEmbedded/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,21 @@ ifeq ($(OS),Windows_NT) # Windows
AVRDUDE_BAUDRATE = 115200
AVRDUDE_PORT = usb
else ifeq ($(OS),Darwin)
AVRDUDE_PROGRAMMER = stk500v1
AVRDUDE_PROGRAMMER = usbtiny
AVRDUDE_BAUDRATE = 19200
AVRDUDE_PORT = /dev/cu.usbmodem2101
AVRDUDE_PORT = usb
endif

AVRDUDE_FLAGS = -C$(AVRDUDE_CONF) \
-p$(AVRDUDE_CHIP) \
-c$(AVRDUDE_PROGRAMMER) \
-P$(AVRDUDE_PORT) \
-b$(AVRDUDE_BAUDRATE) \
-v
-v \
-B1 \
-V \
-D


CPU_SPEED = 8000000L

Expand Down
Loading

0 comments on commit 685af7e

Please sign in to comment.