@@ -35,17 +35,37 @@ void BlendPattern::init()
3535
3636void BlendPattern::onBlinkOn ()
3737{
38+ // if the current hue has reached the next hue
39+ if (m_cur == m_next) {
40+ // get the next color
41+ m_next = m_colorset.getNext ();
42+ }
43+ // only transition the blend once every 4 ticks, this will make sure the blend doesn't go
44+ // to fast and it allows the blendspeed parameter to speed it up to preference
45+ if ((Time::getCurtime () % 4 ) == 0 ) {
46+ // transition each value of the current hsv to the next hsv, the 'hue' has a
47+ // special handling where 255 is beside 0 (circular transition)
48+ transitionValue (m_cur.hue , m_next.hue , true );
49+ transitionValue (m_cur.sat , m_next.sat , false );
50+ transitionValue (m_cur.val , m_next.val , false );
51+ }
52+ // make a copy of the current color being rendered so that it can be
53+ // flipped to an inverse color if the flips are enabled
54+ HSVColor hsvCol = m_cur;
55+ // flip the hue if there is any flips
3856 if (m_flip) {
39- // do a flip as bender would say
40- doFlip ();
41- } else {
42- // if there is no flips then just do a normal blink
43- doBlink ();
57+ // note: no division by zero because m_numFlips cannot be 0 if m_flip is non-zero
58+ hsvCol.hue += (m_flip * (255 / m_numFlips));
4459 }
45- // now if there is a flip amount set
46- if (m_numFlips > 0 ) {
47- // then increase the flip counter and modulate it
48- m_flip = (m_flip + 1 ) % m_numFlips;
60+ // convert the HSV to RGB with the generic function because
61+ // this will generate a different appearance from using the
62+ // default hsv_to_rgb_rainbow()
63+ Leds::setIndex (m_ledPos, hsv_to_rgb_generic (hsvCol));
64+ // increase the flip counter and modulate it, this actually takes less space
65+ // on avr than using a modulo of numflips, because you must check for nonzero
66+ m_flip++;
67+ if (m_flip >= m_numFlips) {
68+ m_flip = 0 ;
4969 }
5070}
5171
@@ -87,33 +107,6 @@ void BlendPattern::transitionValue(uint8_t ¤t, const uint8_t next, bool hu
87107 if (hue) {
88108 // wrap around the hue automatically, this handles for example if step is -1
89109 // and it subtracts past 0 to -1, then adding 256 will result in 255
90- current = (current + 256 ) % 256 ;
91- }
92- }
93-
94- void BlendPattern::doBlink ()
95- {
96- // if the current hue has reached the next hue
97- if (m_cur == m_next) {
98- // get the next color
99- m_next = m_colorset.getNext ();
110+ current += 256 ;
100111 }
101- // transition each value of the current hsv to the next hsv, the 'hue' has a
102- // special handling where 255 is beside 0 (circular transition)
103- transitionValue (m_cur.hue , m_next.hue , true );
104- transitionValue (m_cur.sat , m_next.sat , false );
105- transitionValue (m_cur.val , m_next.val , false );
106- // set the target led with the current HSV color
107- Leds::setIndex (m_ledPos, hsv_to_rgb_generic (m_cur));
108- }
109-
110- void BlendPattern::doFlip ()
111- {
112- uint32_t hueOffset = m_flip * (255 / m_numFlips);
113- // generate an inverse hue based on the current hue position
114- HSVColor hsvCol ((m_cur.hue + hueOffset) % 256 , m_cur.sat , m_cur.val );
115- // convert the HSV to RGB with the generic function because
116- // this will generate a different appearance from using the
117- // default hsv_to_rgb_rainbow()
118- Leds::setIndex (m_ledPos, hsv_to_rgb_generic (hsvCol));
119- }
112+ }
0 commit comments