Skip to content

Commit

Permalink
Merge pull request #89 from Unreal-Dan/daniel/crc-fix-2
Browse files Browse the repository at this point in the history
test crc fix 2
  • Loading branch information
livingkurt authored Aug 10, 2024
2 parents 436ad19 + 9636da7 commit 3d81631
Show file tree
Hide file tree
Showing 17 changed files with 32,302 additions and 32,299 deletions.
10 changes: 10 additions & 0 deletions Helios/Colorset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,16 @@ bool Colorset::equals(const Colorset *set) const
return operator==(*set);
}

// crc the colorset
uint32_t Colorset::crc32() const
{
uint32_t hash = 5381;
for (uint8_t i = 0; i < m_numColors; ++i) {
hash = ((hash << 5) + hash) + m_palette[i].raw();
}
return hash;
}

RGBColor Colorset::operator[](int index) const
{
return get(index);
Expand Down
3 changes: 3 additions & 0 deletions Helios/Colorset.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ class Colorset
bool equals(const Colorset &set) const;
bool equals(const Colorset *set) const;

// crc the colorset
uint32_t crc32() const;

// index operator to access color index
RGBColor operator[](int index) const;

Expand Down
18 changes: 4 additions & 14 deletions Helios/Helios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -790,15 +790,6 @@ void Helios::handle_state_set_global_brightness()
show_selection(RGB_WHITE_BRI_LOW);
}

inline uint32_t crc32(const uint8_t *data, uint8_t size)
{
uint32_t hash = 5381;
for (uint8_t i = 0; i < size; ++i) {
hash = ((hash << 5) + hash) + data[i];
}
return hash;
}

void Helios::handle_state_shift_mode()
{
uint8_t new_mode = (cur_mode > 0) ? (uint8_t)(cur_mode - 1) : (uint8_t)(NUM_MODE_SLOTS - 1);
Expand All @@ -814,12 +805,11 @@ void Helios::handle_state_shift_mode()
void Helios::handle_state_randomize()
{
if (Button::onShortClick()) {
uint32_t seed = crc32((const uint8_t *)&pat.colorset(), COLORSET_SIZE);
Random ctx(seed);
Colorset &cur_set = pat.colorset();
uint8_t num_cols = (ctx.next8() + 1) % NUM_COLOR_SLOTS;
cur_set.randomizeColors(ctx, num_cols);
Patterns::make_pattern((PatternID)(ctx.next8() % PATTERN_COUNT), pat);
Random ctx(cur_set.crc32());
uint8_t randVal = ctx.next8();
cur_set.randomizeColors(ctx, (randVal + 1) % NUM_COLOR_SLOTS);
Patterns::make_pattern((PatternID)(randVal % PATTERN_COUNT), pat);
pat.init();
}
if (Button::onLongClick()) {
Expand Down
Loading

0 comments on commit 3d81631

Please sign in to comment.