Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions Helios/Colortypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -369,8 +369,14 @@ RGBColor hsv_to_rgb_generic(const HSVColor &rhs)
return col;
}

region = rhs.hue / 43;
remainder = ((rhs.hue - (region * 43)) * 6);
// At 1MHz the AVR has ~8x fewer cycles per tick. The original division
// (hue / 43) compiles to a ~200-cycle software divide on AVR. Replacing it
// with a branchless compare tree + a LUT subtraction cuts this to ~10 cycles.
static const uint8_t region_base[6] = { 0, 43, 86, 129, 172, 215 };
region = (rhs.hue < 86) ? ((rhs.hue < 43) ? 0 : 1)
: (rhs.hue < 172) ? ((rhs.hue < 129) ? 2 : 3)
: ((rhs.hue < 215) ? 4 : 5);
remainder = ((rhs.hue - region_base[region]) * 6);

// extraneous casts to uint16_t are to prevent overflow
p = (uint8_t)(((uint16_t)(rhs.val) * (255 - rhs.sat)) >> 8);
Expand Down
6 changes: 6 additions & 0 deletions Helios/Helios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -717,6 +717,12 @@ void Helios::handle_state_pat_select()

void Helios::handle_state_toggle_flag(Flags flag)
{
// Play the pattern for this one-tick toggle state so the blink timer does
// not drop a tick across the transition. This handler runs for a single
// tick and otherwise never calls pat.play(), so the next poll would see
// timeDiff = m_alarm + 1 -- a phantom one-tick cadence gap. (Visible with
// the catch-up alarm; harmless to always run.)
pat.play();
// toggle the conjure flag
toggle_flags(flag);
// write out the new global flags and the current mode
Expand Down
9 changes: 7 additions & 2 deletions Helios/TimeControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,13 @@ uint32_t Time::microseconds()
// should always just rely on the current tick to perform operations
uint8_t oldSREG = SREG;
cli();
// multiply by 8 early to avoid floating point math or division
uint32_t micros = (timer0_overflow_count * (256 * 8)) + (TCNT0 * 8);
// Scale overflow count and timer ticks by the number of microseconds each Timer0
// tick represents at the configured CPU speed. Timer0 runs at F_CPU/1 (no prescaler),
// so each tick = (1/F_CPU) seconds = (1000000/F_CPU) microseconds.
// The factor (64000000UL/F_CPU) bakes that in as an integer: 8 @ 8MHz, 64 @ 1MHz,
// 4 @ 16MHz. Using F_CPU here means this formula automatically adapts when
// CPU_SPEED is changed in the Makefile — no manual constant updates needed.
uint32_t micros = (timer0_overflow_count * (256 * (64000000UL / F_CPU))) + (TCNT0 * (64000000UL / F_CPU));
SREG = oldSREG;
// then shift right to counteract the multiplication by 8
return micros >> 6;
Expand Down
25 changes: 20 additions & 5 deletions Helios/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,27 @@ bool Timer::alarm()
if (timeDiff == 0) {
return true;
}
// if the current alarm duration is not a multiple of the current tick
if (m_alarm && (timeDiff % m_alarm) != 0) {
// then the alarm was not hit
return false;
// Recurring alarm: returns true once per m_alarm ticks.
//
// Small-slip branch (timeDiff in [m_alarm, 2*m_alarm)): re-anchor to now
// so consecutive beats stay evenly spaced -- a 1-tick slip that would
// produce a long-then-short pair instead advances the anchor to the actual
// fire time, spreading the slip smoothly across future beats.
// (This is the behavior Kurt confirmed "looks perfect" on hardware.)
//
// Big-gap branch (timeDiff >= 2*m_alarm): the timer was suspended for a
// long menu hold or similar; realign to the period grid so post-menu
// cadence matches the original schedule and menu-test timing stays intact.
//
// No 32-bit divide/modulo in the per-tick hot path (expensive on AVR).
if (timeDiff < (int32_t)m_alarm) { return false; }
if (timeDiff < (int32_t)(2 * m_alarm)) {
m_startTime = now;
return true;
}
// update the start time of the timer
int32_t rem = timeDiff;
while (rem >= (int32_t)m_alarm) { rem -= (int32_t)m_alarm; }
if (rem != 0) { return false; }
m_startTime = now;
return true;
}
9 changes: 7 additions & 2 deletions HeliosEmbedded/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,9 @@ AVRDUDE_FLAGS = $(AVRDUDE_CONFIG_FLAG) \
-P$(AVRDUDE_PORT) \
-b$(AVRDUDE_BAUDRATE) \
-v \
-B1
-B10 # ISP bit-clock period in microseconds. Was -B1 (1µs = 1MHz ISP clock).
# At 1MHz CPU the target can only accept an ISP clock up to F_CPU/4 = 250kHz,
# so -B1 overclocked the ISP bus and caused upload failures. -B10 = 100kHz ISP.

# -v -- Verbose output - display detailed progress
# -B1 -- Bit clock period (in microseconds) - sets programming speed
Expand All @@ -96,7 +98,10 @@ AVRDUDE_FLAGS = $(AVRDUDE_CONFIG_FLAG) \
### COMPILER FLAGS ####
#######################

CPU_SPEED = 8000000L
# 1MHz internal oscillator. Drops power draw significantly vs 8MHz.
# Requires fuse H:0xDF L:0x62 (CKDIV8 enabled, SUT_CKSEL=internal 8MHz / 8).
# Run `make set_fuses` after changing this value.
CPU_SPEED = 1000000L

# the port for serial upload
SERIAL_PORT = COM11
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/0125_From_First_Mode_Enter_Conjure_Mode.test
Original file line number Diff line number Diff line change
Expand Up @@ -3804,8 +3804,6 @@ D200FF
3C1C00
3C1C00
000000
000000
000000
00FFD1
00FFD1
0000FF
Expand Down Expand Up @@ -4105,3 +4103,5 @@ D200FF
000000
000000
000000
000000
000000
2 changes: 1 addition & 1 deletion tests/tests/0152_Enter_Glow_Lock.test
Original file line number Diff line number Diff line change
Expand Up @@ -2605,4 +2605,4 @@ D200FF
3C0000
3C0000
000000
000000
D200FF
4 changes: 2 additions & 2 deletions tests/tests/0160_Exit_Glow_Lock.test
Original file line number Diff line number Diff line change
Expand Up @@ -2605,8 +2605,8 @@ D200FF
3C0000
3C0000
000000
000000
000000
D200FF
D200FF
3C0000
3C0000
3C0000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3804,8 +3804,6 @@ D200FF
3C1C00
3C1C00
000000
000000
000000
00FFD1
00FFD1
0000FF
Expand Down Expand Up @@ -5104,6 +5102,8 @@ D200FF
000000
000000
000000
000000
000000
003C31
003C31
003C31
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/0403_Enter_Glow_Lock_Force_Enter_Sleep.test
Original file line number Diff line number Diff line change
Expand Up @@ -2605,8 +2605,8 @@ D200FF
3C0000
3C0000
000000
000000
000000
D200FF
D200FF
3C0000
3C0000
3C0000
Expand Down
4 changes: 2 additions & 2 deletions tests/tests/0411_Exit_Glow_Lock_Force_Enter_Sleep.test
Original file line number Diff line number Diff line change
Expand Up @@ -2605,8 +2605,8 @@ D200FF
3C0000
3C0000
000000
000000
000000
D200FF
D200FF
3C0000
3C0000
3C0000
Expand Down
Loading