Skip to content
Open
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
9 changes: 8 additions & 1 deletion Helios/Helios.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,7 +328,14 @@ void Helios::handle_state_modes()
uint32_t holdDur = Button::holdDuration();
// calculate a magnitude which corresponds to how many times past the MENU_HOLD_TIME
// the user has held the button, so 0 means haven't held fully past one yet, etc
uint8_t magnitude = (uint8_t)(holdDur / MENU_HOLD_TIME);
// At 1MHz, 32-bit division is a ~240-cycle software routine called every tick.
// Unrolling into threshold comparisons eliminates the divide entirely.
uint8_t magnitude =
(holdDur >= (uint32_t)(MENU_HOLD_TIME * 5)) ? 5 :
(holdDur >= (uint32_t)(MENU_HOLD_TIME * 4)) ? 4 :
(holdDur >= (uint32_t)(MENU_HOLD_TIME * 3)) ? 3 :
(holdDur >= (uint32_t)(MENU_HOLD_TIME * 2)) ? 2 :
(holdDur >= (uint32_t)(MENU_HOLD_TIME * 1)) ? 1 : 0;
// whether the user has held the button longer than a short click
bool heldPast = (holdDur > SHORT_CLICK_THRESHOLD);

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
10 changes: 7 additions & 3 deletions Helios/Timer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,13 @@ 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
// At 1MHz a 32-bit software modulo costs ~240 AVR cycles — called every tick,
// that's a significant fraction of budget. The original (timeDiff % m_alarm == 0)
// was checking if the alarm interval divided evenly, but because start() always
// resets m_startTime the moment the alarm fires, timeDiff simply counts up from
// 0 to m_alarm and then resets. So "has the alarm fired?" is just (timeDiff >= m_alarm),
// no division needed.
if (timeDiff < (int32_t)m_alarm) {
return false;
}
// update the start time of the timer
Expand Down
8 changes: 6 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,9 @@ AVRDUDE_FLAGS = $(AVRDUDE_CONFIG_FLAG) \
### COMPILER FLAGS ####
#######################

CPU_SPEED = 8000000L
CPU_SPEED = 1000000L # 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.

# the port for serial upload
SERIAL_PORT = COM11
Expand Down
26 changes: 13 additions & 13 deletions tests/tests/0008_Cycle_Color_Slots_Exit.test
Original file line number Diff line number Diff line change
Expand Up @@ -3311,6 +3311,19 @@ D200FF
000000
000000
000000
FF0000
FF0000
FF3C00
FF3C00
FF7800
FF7800
00FFD1
00FFD1
0000FF
0000FF
D200FF
D200FF
000000
000000
000000
000000
Expand Down Expand Up @@ -3599,16 +3612,3 @@ D200FF
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
000000
FF0000
FF0000
2 changes: 1 addition & 1 deletion tests/tests/0125_From_First_Mode_Enter_Conjure_Mode.test
Original file line number Diff line number Diff line change
Expand Up @@ -3805,7 +3805,6 @@ D200FF
3C1C00
000000
000000
000000
00FFD1
00FFD1
0000FF
Expand Down Expand Up @@ -4105,3 +4104,4 @@ D200FF
000000
000000
000000
000000
2 changes: 1 addition & 1 deletion tests/tests/0153_Cycle_Master_Reset_Options.test
Original file line number Diff line number Diff line change
Expand Up @@ -3604,7 +3604,7 @@ D200FF
003C00
003C00
003C00
003C00
D200FF
00FF00
00FF00
00FF00
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/0154_Cycle_Global_Brightness_Options.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/0155_Navigate_to_Exit_Master_Reset.test
Original file line number Diff line number Diff line change
Expand Up @@ -3604,7 +3604,7 @@ D200FF
003C00
003C00
003C00
003C00
D200FF
00FF00
00FF00
00FF00
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/0156_Navigate_to_Master_Reset_Option.test
Original file line number Diff line number Diff line change
Expand Up @@ -3604,7 +3604,7 @@ D200FF
003C00
003C00
003C00
003C00
D200FF
00FF00
00FF00
00FF00
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/0157_Navigate_to_Full_Brightness_Option.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/0158_Navigate_to_Medium_Brightness_Option.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/0159_Navigate_to_Low_Brightness_Option.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/0252_Set_Global_Brightness_to_High.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down Expand Up @@ -4906,9 +4906,6 @@ D200FF
000000
0A0A0A
0A0A0A
0A0A0A
D200FF
D200FF
000000
000000
000000
Expand Down Expand Up @@ -5207,6 +5204,9 @@ D200FF
000000
000000
000000
000000
000000
000000
FF0000
000000
000000
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/0253_Set_Global_Brightness_to_Low.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down Expand Up @@ -5508,9 +5508,6 @@ D200FF
0A0A0A
0A0A0A
0A0A0A
0A0A0A
D200FF
D200FF
000000
000000
000000
Expand Down Expand Up @@ -5809,6 +5806,9 @@ D200FF
000000
000000
000000
000000
000000
000000
FF0000
000000
000000
Expand Down
8 changes: 4 additions & 4 deletions tests/tests/0254_Set_Global_Brightness_to_Low_then_Mid.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down Expand Up @@ -5508,9 +5508,6 @@ D200FF
0A0A0A
0A0A0A
0A0A0A
0A0A0A
D200FF
D200FF
000000
000000
000000
Expand Down Expand Up @@ -5809,6 +5806,9 @@ D200FF
000000
000000
000000
000000
000000
000000
FF0000
000000
000000
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down Expand Up @@ -4906,9 +4906,6 @@ D200FF
000000
0A0A0A
0A0A0A
0A0A0A
D200FF
D200FF
000000
000000
000000
Expand Down Expand Up @@ -5207,6 +5204,9 @@ D200FF
000000
000000
000000
000000
000000
000000
FF0000
000000
000000
Expand Down
2 changes: 1 addition & 1 deletion tests/tests/0256_Set_Global_Brightness_to_Lowest.test
Original file line number Diff line number Diff line change
Expand Up @@ -4604,7 +4604,7 @@ D200FF
00003C
00003C
00003C
00003C
D200FF
0A0A0A
0A0A0A
0A0A0A
Expand Down
Loading
Loading