PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

1199 lines
33KB

  1. #define FASTLED_INTERNAL
  2. #define __PROG_TYPES_COMPAT__
  3. #include <stdint.h>
  4. #include <math.h>
  5. #include "FastLED.h"
  6. FASTLED_NAMESPACE_BEGIN
  7. void fill_solid( struct CRGB * leds, int numToFill,
  8. const struct CRGB& color)
  9. {
  10. for( int i = 0; i < numToFill; i++) {
  11. leds[i] = color;
  12. }
  13. }
  14. void fill_solid( struct CHSV * targetArray, int numToFill,
  15. const struct CHSV& hsvColor)
  16. {
  17. for( int i = 0; i < numToFill; i++) {
  18. targetArray[i] = hsvColor;
  19. }
  20. }
  21. // void fill_solid( struct CRGB* targetArray, int numToFill,
  22. // const struct CHSV& hsvColor)
  23. // {
  24. // fill_solid<CRGB>( targetArray, numToFill, (CRGB) hsvColor);
  25. // }
  26. void fill_rainbow( struct CRGB * pFirstLED, int numToFill,
  27. uint8_t initialhue,
  28. uint8_t deltahue )
  29. {
  30. CHSV hsv;
  31. hsv.hue = initialhue;
  32. hsv.val = 255;
  33. hsv.sat = 240;
  34. for( int i = 0; i < numToFill; i++) {
  35. pFirstLED[i] = hsv;
  36. hsv.hue += deltahue;
  37. }
  38. }
  39. void fill_rainbow( struct CHSV * targetArray, int numToFill,
  40. uint8_t initialhue,
  41. uint8_t deltahue )
  42. {
  43. CHSV hsv;
  44. hsv.hue = initialhue;
  45. hsv.val = 255;
  46. hsv.sat = 240;
  47. for( int i = 0; i < numToFill; i++) {
  48. targetArray[i] = hsv;
  49. hsv.hue += deltahue;
  50. }
  51. }
  52. void fill_gradient_RGB( CRGB* leds,
  53. uint16_t startpos, CRGB startcolor,
  54. uint16_t endpos, CRGB endcolor )
  55. {
  56. // if the points are in the wrong order, straighten them
  57. if( endpos < startpos ) {
  58. uint16_t t = endpos;
  59. CRGB tc = endcolor;
  60. endcolor = startcolor;
  61. endpos = startpos;
  62. startpos = t;
  63. startcolor = tc;
  64. }
  65. saccum87 rdistance87;
  66. saccum87 gdistance87;
  67. saccum87 bdistance87;
  68. rdistance87 = (endcolor.r - startcolor.r) << 7;
  69. gdistance87 = (endcolor.g - startcolor.g) << 7;
  70. bdistance87 = (endcolor.b - startcolor.b) << 7;
  71. uint16_t pixeldistance = endpos - startpos;
  72. int16_t divisor = pixeldistance ? pixeldistance : 1;
  73. saccum87 rdelta87 = rdistance87 / divisor;
  74. saccum87 gdelta87 = gdistance87 / divisor;
  75. saccum87 bdelta87 = bdistance87 / divisor;
  76. rdelta87 *= 2;
  77. gdelta87 *= 2;
  78. bdelta87 *= 2;
  79. accum88 r88 = startcolor.r << 8;
  80. accum88 g88 = startcolor.g << 8;
  81. accum88 b88 = startcolor.b << 8;
  82. for( uint16_t i = startpos; i <= endpos; i++) {
  83. leds[i] = CRGB( r88 >> 8, g88 >> 8, b88 >> 8);
  84. r88 += rdelta87;
  85. g88 += gdelta87;
  86. b88 += bdelta87;
  87. }
  88. }
  89. #if 0
  90. void fill_gradient( const CHSV& c1, const CHSV& c2)
  91. {
  92. fill_gradient( FastLED[0].leds(), FastLED[0].size(), c1, c2);
  93. }
  94. void fill_gradient( const CHSV& c1, const CHSV& c2, const CHSV& c3)
  95. {
  96. fill_gradient( FastLED[0].leds(), FastLED[0].size(), c1, c2, c3);
  97. }
  98. void fill_gradient( const CHSV& c1, const CHSV& c2, const CHSV& c3, const CHSV& c4)
  99. {
  100. fill_gradient( FastLED[0].leds(), FastLED[0].size(), c1, c2, c3, c4);
  101. }
  102. void fill_gradient_RGB( const CRGB& c1, const CRGB& c2)
  103. {
  104. fill_gradient_RGB( FastLED[0].leds(), FastLED[0].size(), c1, c2);
  105. }
  106. void fill_gradient_RGB( const CRGB& c1, const CRGB& c2, const CRGB& c3)
  107. {
  108. fill_gradient_RGB( FastLED[0].leds(), FastLED[0].size(), c1, c2, c3);
  109. }
  110. void fill_gradient_RGB( const CRGB& c1, const CRGB& c2, const CRGB& c3, const CRGB& c4)
  111. {
  112. fill_gradient_RGB( FastLED[0].leds(), FastLED[0].size(), c1, c2, c3, c4);
  113. }
  114. #endif
  115. void fill_gradient_RGB( CRGB* leds, uint16_t numLeds, const CRGB& c1, const CRGB& c2)
  116. {
  117. uint16_t last = numLeds - 1;
  118. fill_gradient_RGB( leds, 0, c1, last, c2);
  119. }
  120. void fill_gradient_RGB( CRGB* leds, uint16_t numLeds, const CRGB& c1, const CRGB& c2, const CRGB& c3)
  121. {
  122. uint16_t half = (numLeds / 2);
  123. uint16_t last = numLeds - 1;
  124. fill_gradient_RGB( leds, 0, c1, half, c2);
  125. fill_gradient_RGB( leds, half, c2, last, c3);
  126. }
  127. void fill_gradient_RGB( CRGB* leds, uint16_t numLeds, const CRGB& c1, const CRGB& c2, const CRGB& c3, const CRGB& c4)
  128. {
  129. uint16_t onethird = (numLeds / 3);
  130. uint16_t twothirds = ((numLeds * 2) / 3);
  131. uint16_t last = numLeds - 1;
  132. fill_gradient_RGB( leds, 0, c1, onethird, c2);
  133. fill_gradient_RGB( leds, onethird, c2, twothirds, c3);
  134. fill_gradient_RGB( leds, twothirds, c3, last, c4);
  135. }
  136. void nscale8_video( CRGB* leds, uint16_t num_leds, uint8_t scale)
  137. {
  138. for( uint16_t i = 0; i < num_leds; i++) {
  139. leds[i].nscale8_video( scale);
  140. }
  141. }
  142. void fade_video(CRGB* leds, uint16_t num_leds, uint8_t fadeBy)
  143. {
  144. nscale8_video( leds, num_leds, 255 - fadeBy);
  145. }
  146. void fadeLightBy(CRGB* leds, uint16_t num_leds, uint8_t fadeBy)
  147. {
  148. nscale8_video( leds, num_leds, 255 - fadeBy);
  149. }
  150. void fadeToBlackBy( CRGB* leds, uint16_t num_leds, uint8_t fadeBy)
  151. {
  152. nscale8( leds, num_leds, 255 - fadeBy);
  153. }
  154. void fade_raw( CRGB* leds, uint16_t num_leds, uint8_t fadeBy)
  155. {
  156. nscale8( leds, num_leds, 255 - fadeBy);
  157. }
  158. void nscale8_raw( CRGB* leds, uint16_t num_leds, uint8_t scale)
  159. {
  160. nscale8( leds, num_leds, scale);
  161. }
  162. void nscale8( CRGB* leds, uint16_t num_leds, uint8_t scale)
  163. {
  164. for( uint16_t i = 0; i < num_leds; i++) {
  165. leds[i].nscale8( scale);
  166. }
  167. }
  168. void fadeUsingColor( CRGB* leds, uint16_t numLeds, const CRGB& colormask)
  169. {
  170. uint8_t fr, fg, fb;
  171. fr = colormask.r;
  172. fg = colormask.g;
  173. fb = colormask.b;
  174. for( uint16_t i = 0; i < numLeds; i++) {
  175. leds[i].r = scale8_LEAVING_R1_DIRTY( leds[i].r, fr);
  176. leds[i].g = scale8_LEAVING_R1_DIRTY( leds[i].g, fg);
  177. leds[i].b = scale8 ( leds[i].b, fb);
  178. }
  179. }
  180. CRGB& nblend( CRGB& existing, const CRGB& overlay, fract8 amountOfOverlay )
  181. {
  182. if( amountOfOverlay == 0) {
  183. return existing;
  184. }
  185. if( amountOfOverlay == 255) {
  186. existing = overlay;
  187. return existing;
  188. }
  189. #if 0
  190. // Old blend method which unfortunately had some rounding errors
  191. fract8 amountOfKeep = 255 - amountOfOverlay;
  192. existing.red = scale8_LEAVING_R1_DIRTY( existing.red, amountOfKeep)
  193. + scale8_LEAVING_R1_DIRTY( overlay.red, amountOfOverlay);
  194. existing.green = scale8_LEAVING_R1_DIRTY( existing.green, amountOfKeep)
  195. + scale8_LEAVING_R1_DIRTY( overlay.green, amountOfOverlay);
  196. existing.blue = scale8_LEAVING_R1_DIRTY( existing.blue, amountOfKeep)
  197. + scale8_LEAVING_R1_DIRTY( overlay.blue, amountOfOverlay);
  198. cleanup_R1();
  199. #else
  200. // Corrected blend method, with no loss-of-precision rounding errors
  201. existing.red = blend8( existing.red, overlay.red, amountOfOverlay);
  202. existing.green = blend8( existing.green, overlay.green, amountOfOverlay);
  203. existing.blue = blend8( existing.blue, overlay.blue, amountOfOverlay);
  204. #endif
  205. return existing;
  206. }
  207. void nblend( CRGB* existing, CRGB* overlay, uint16_t count, fract8 amountOfOverlay)
  208. {
  209. for( uint16_t i = count; i; i--) {
  210. nblend( *existing, *overlay, amountOfOverlay);
  211. existing++;
  212. overlay++;
  213. }
  214. }
  215. CRGB blend( const CRGB& p1, const CRGB& p2, fract8 amountOfP2 )
  216. {
  217. CRGB nu(p1);
  218. nblend( nu, p2, amountOfP2);
  219. return nu;
  220. }
  221. CRGB* blend( const CRGB* src1, const CRGB* src2, CRGB* dest, uint16_t count, fract8 amountOfsrc2 )
  222. {
  223. for( uint16_t i = 0; i < count; i++) {
  224. dest[i] = blend(src1[i], src2[i], amountOfsrc2);
  225. }
  226. return dest;
  227. }
  228. CHSV& nblend( CHSV& existing, const CHSV& overlay, fract8 amountOfOverlay, TGradientDirectionCode directionCode)
  229. {
  230. if( amountOfOverlay == 0) {
  231. return existing;
  232. }
  233. if( amountOfOverlay == 255) {
  234. existing = overlay;
  235. return existing;
  236. }
  237. fract8 amountOfKeep = 255 - amountOfOverlay;
  238. uint8_t huedelta8 = overlay.hue - existing.hue;
  239. if( directionCode == SHORTEST_HUES ) {
  240. directionCode = FORWARD_HUES;
  241. if( huedelta8 > 127) {
  242. directionCode = BACKWARD_HUES;
  243. }
  244. }
  245. if( directionCode == LONGEST_HUES ) {
  246. directionCode = FORWARD_HUES;
  247. if( huedelta8 < 128) {
  248. directionCode = BACKWARD_HUES;
  249. }
  250. }
  251. if( directionCode == FORWARD_HUES) {
  252. existing.hue = existing.hue + scale8( huedelta8, amountOfOverlay);
  253. }
  254. else /* directionCode == BACKWARD_HUES */
  255. {
  256. huedelta8 = -huedelta8;
  257. existing.hue = existing.hue - scale8( huedelta8, amountOfOverlay);
  258. }
  259. existing.sat = scale8_LEAVING_R1_DIRTY( existing.sat, amountOfKeep)
  260. + scale8_LEAVING_R1_DIRTY( overlay.sat, amountOfOverlay);
  261. existing.val = scale8_LEAVING_R1_DIRTY( existing.val, amountOfKeep)
  262. + scale8_LEAVING_R1_DIRTY( overlay.val, amountOfOverlay);
  263. cleanup_R1();
  264. return existing;
  265. }
  266. void nblend( CHSV* existing, CHSV* overlay, uint16_t count, fract8 amountOfOverlay, TGradientDirectionCode directionCode )
  267. {
  268. if(existing == overlay) return;
  269. for( uint16_t i = count; i; i--) {
  270. nblend( *existing, *overlay, amountOfOverlay, directionCode);
  271. existing++;
  272. overlay++;
  273. }
  274. }
  275. CHSV blend( const CHSV& p1, const CHSV& p2, fract8 amountOfP2, TGradientDirectionCode directionCode )
  276. {
  277. CHSV nu(p1);
  278. nblend( nu, p2, amountOfP2, directionCode);
  279. return nu;
  280. }
  281. CHSV* blend( const CHSV* src1, const CHSV* src2, CHSV* dest, uint16_t count, fract8 amountOfsrc2, TGradientDirectionCode directionCode )
  282. {
  283. for( uint16_t i = 0; i < count; i++) {
  284. dest[i] = blend(src1[i], src2[i], amountOfsrc2, directionCode);
  285. }
  286. return dest;
  287. }
  288. // Forward declaration of the function "XY" which must be provided by
  289. // the application for use in two-dimensional filter functions.
  290. uint16_t XY( uint8_t, uint8_t);// __attribute__ ((weak));
  291. // blur1d: one-dimensional blur filter. Spreads light to 2 line neighbors.
  292. // blur2d: two-dimensional blur filter. Spreads light to 8 XY neighbors.
  293. //
  294. // 0 = no spread at all
  295. // 64 = moderate spreading
  296. // 172 = maximum smooth, even spreading
  297. //
  298. // 173..255 = wider spreading, but increasing flicker
  299. //
  300. // Total light is NOT entirely conserved, so many repeated
  301. // calls to 'blur' will also result in the light fading,
  302. // eventually all the way to black; this is by design so that
  303. // it can be used to (slowly) clear the LEDs to black.
  304. void blur1d( CRGB* leds, uint16_t numLeds, fract8 blur_amount)
  305. {
  306. uint8_t keep = 255 - blur_amount;
  307. uint8_t seep = blur_amount >> 1;
  308. CRGB carryover = CRGB::Black;
  309. for( uint16_t i = 0; i < numLeds; i++) {
  310. CRGB cur = leds[i];
  311. CRGB part = cur;
  312. part.nscale8( seep);
  313. cur.nscale8( keep);
  314. cur += carryover;
  315. if( i) leds[i-1] += part;
  316. leds[i] = cur;
  317. carryover = part;
  318. }
  319. }
  320. void blur2d( CRGB* leds, uint8_t width, uint8_t height, fract8 blur_amount)
  321. {
  322. blurRows(leds, width, height, blur_amount);
  323. blurColumns(leds, width, height, blur_amount);
  324. }
  325. // blurRows: perform a blur1d on every row of a rectangular matrix
  326. void blurRows( CRGB* leds, uint8_t width, uint8_t height, fract8 blur_amount)
  327. {
  328. for( uint8_t row = 0; row < height; row++) {
  329. CRGB* rowbase = leds + (row * width);
  330. blur1d( rowbase, width, blur_amount);
  331. }
  332. }
  333. // blurColumns: perform a blur1d on each column of a rectangular matrix
  334. void blurColumns(CRGB* leds, uint8_t width, uint8_t height, fract8 blur_amount)
  335. {
  336. // blur columns
  337. uint8_t keep = 255 - blur_amount;
  338. uint8_t seep = blur_amount >> 1;
  339. for( uint8_t col = 0; col < width; col++) {
  340. CRGB carryover = CRGB::Black;
  341. for( uint8_t i = 0; i < height; i++) {
  342. CRGB cur = leds[XY(col,i)];
  343. CRGB part = cur;
  344. part.nscale8( seep);
  345. cur.nscale8( keep);
  346. cur += carryover;
  347. if( i) leds[XY(col,i-1)] += part;
  348. leds[XY(col,i)] = cur;
  349. carryover = part;
  350. }
  351. }
  352. }
  353. // CRGB HeatColor( uint8_t temperature)
  354. //
  355. // Approximates a 'black body radiation' spectrum for
  356. // a given 'heat' level. This is useful for animations of 'fire'.
  357. // Heat is specified as an arbitrary scale from 0 (cool) to 255 (hot).
  358. // This is NOT a chromatically correct 'black body radiation'
  359. // spectrum, but it's surprisingly close, and it's fast and small.
  360. //
  361. // On AVR/Arduino, this typically takes around 70 bytes of program memory,
  362. // versus 768 bytes for a full 256-entry RGB lookup table.
  363. CRGB HeatColor( uint8_t temperature)
  364. {
  365. CRGB heatcolor;
  366. // Scale 'heat' down from 0-255 to 0-191,
  367. // which can then be easily divided into three
  368. // equal 'thirds' of 64 units each.
  369. uint8_t t192 = scale8_video( temperature, 191);
  370. // calculate a value that ramps up from
  371. // zero to 255 in each 'third' of the scale.
  372. uint8_t heatramp = t192 & 0x3F; // 0..63
  373. heatramp <<= 2; // scale up to 0..252
  374. // now figure out which third of the spectrum we're in:
  375. if( t192 & 0x80) {
  376. // we're in the hottest third
  377. heatcolor.r = 255; // full red
  378. heatcolor.g = 255; // full green
  379. heatcolor.b = heatramp; // ramp up blue
  380. } else if( t192 & 0x40 ) {
  381. // we're in the middle third
  382. heatcolor.r = 255; // full red
  383. heatcolor.g = heatramp; // ramp up green
  384. heatcolor.b = 0; // no blue
  385. } else {
  386. // we're in the coolest third
  387. heatcolor.r = heatramp; // ramp up red
  388. heatcolor.g = 0; // no green
  389. heatcolor.b = 0; // no blue
  390. }
  391. return heatcolor;
  392. }
  393. // lsrX4: helper function to divide a number by 16, aka four LSR's.
  394. // On avr-gcc, "u8 >> 4" generates a loop, which is big, and slow.
  395. // merely forcing it to be four /=2's causes avr-gcc to emit
  396. // a SWAP instruction followed by an AND 0x0F, which is faster, and smaller.
  397. inline uint8_t lsrX4( uint8_t dividend) __attribute__((always_inline));
  398. inline uint8_t lsrX4( uint8_t dividend)
  399. {
  400. #if defined(__AVR__)
  401. dividend /= 2;
  402. dividend /= 2;
  403. dividend /= 2;
  404. dividend /= 2;
  405. #else
  406. dividend >>= 4;
  407. #endif
  408. return dividend;
  409. }
  410. CRGB ColorFromPalette( const CRGBPalette16& pal, uint8_t index, uint8_t brightness, TBlendType blendType)
  411. {
  412. // hi4 = index >> 4;
  413. uint8_t hi4 = lsrX4(index);
  414. uint8_t lo4 = index & 0x0F;
  415. // const CRGB* entry = &(pal[0]) + hi4;
  416. // since hi4 is always 0..15, hi4 * sizeof(CRGB) can be a single-byte value,
  417. // instead of the two byte 'int' that avr-gcc defaults to.
  418. // So, we multiply hi4 X sizeof(CRGB), giving hi4XsizeofCRGB;
  419. uint8_t hi4XsizeofCRGB = hi4 * sizeof(CRGB);
  420. // We then add that to a base array pointer.
  421. const CRGB* entry = (CRGB*)( (uint8_t*)(&(pal[0])) + hi4XsizeofCRGB);
  422. uint8_t blend = lo4 && (blendType != NOBLEND);
  423. uint8_t red1 = entry->red;
  424. uint8_t green1 = entry->green;
  425. uint8_t blue1 = entry->blue;
  426. if( blend ) {
  427. if( hi4 == 15 ) {
  428. entry = &(pal[0]);
  429. } else {
  430. entry++;
  431. }
  432. uint8_t f2 = lo4 << 4;
  433. uint8_t f1 = 255 - f2;
  434. // rgb1.nscale8(f1);
  435. uint8_t red2 = entry->red;
  436. red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
  437. red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
  438. red1 += red2;
  439. uint8_t green2 = entry->green;
  440. green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
  441. green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
  442. green1 += green2;
  443. uint8_t blue2 = entry->blue;
  444. blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
  445. blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
  446. blue1 += blue2;
  447. cleanup_R1();
  448. }
  449. if( brightness != 255) {
  450. if( brightness ) {
  451. brightness++; // adjust for rounding
  452. // Now, since brightness is nonzero, we don't need the full scale8_video logic;
  453. // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
  454. if( red1 ) {
  455. red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
  456. #if !(FASTLED_SCALE8_FIXED==1)
  457. red1++;
  458. #endif
  459. }
  460. if( green1 ) {
  461. green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
  462. #if !(FASTLED_SCALE8_FIXED==1)
  463. green1++;
  464. #endif
  465. }
  466. if( blue1 ) {
  467. blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
  468. #if !(FASTLED_SCALE8_FIXED==1)
  469. blue1++;
  470. #endif
  471. }
  472. cleanup_R1();
  473. } else {
  474. red1 = 0;
  475. green1 = 0;
  476. blue1 = 0;
  477. }
  478. }
  479. return CRGB( red1, green1, blue1);
  480. }
  481. CRGB ColorFromPalette( const TProgmemRGBPalette16& pal, uint8_t index, uint8_t brightness, TBlendType blendType)
  482. {
  483. // hi4 = index >> 4;
  484. uint8_t hi4 = lsrX4(index);
  485. uint8_t lo4 = index & 0x0F;
  486. CRGB entry = FL_PGM_READ_DWORD_NEAR( &(pal[0]) + hi4 );
  487. uint8_t red1 = entry.red;
  488. uint8_t green1 = entry.green;
  489. uint8_t blue1 = entry.blue;
  490. uint8_t blend = lo4 && (blendType != NOBLEND);
  491. if( blend ) {
  492. if( hi4 == 15 ) {
  493. entry = FL_PGM_READ_DWORD_NEAR( &(pal[0]) );
  494. } else {
  495. entry = FL_PGM_READ_DWORD_NEAR( &(pal[1]) + hi4 );
  496. }
  497. uint8_t f2 = lo4 << 4;
  498. uint8_t f1 = 255 - f2;
  499. uint8_t red2 = entry.red;
  500. red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
  501. red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
  502. red1 += red2;
  503. uint8_t green2 = entry.green;
  504. green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
  505. green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
  506. green1 += green2;
  507. uint8_t blue2 = entry.blue;
  508. blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
  509. blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
  510. blue1 += blue2;
  511. cleanup_R1();
  512. }
  513. if( brightness != 255) {
  514. if( brightness ) {
  515. brightness++; // adjust for rounding
  516. // Now, since brightness is nonzero, we don't need the full scale8_video logic;
  517. // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
  518. if( red1 ) {
  519. red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
  520. #if !(FASTLED_SCALE8_FIXED==1)
  521. red1++;
  522. #endif
  523. }
  524. if( green1 ) {
  525. green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
  526. #if !(FASTLED_SCALE8_FIXED==1)
  527. green1++;
  528. #endif
  529. }
  530. if( blue1 ) {
  531. blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
  532. #if !(FASTLED_SCALE8_FIXED==1)
  533. blue1++;
  534. #endif
  535. }
  536. cleanup_R1();
  537. } else {
  538. red1 = 0;
  539. green1 = 0;
  540. blue1 = 0;
  541. }
  542. }
  543. return CRGB( red1, green1, blue1);
  544. }
  545. CRGB ColorFromPalette( const CRGBPalette32& pal, uint8_t index, uint8_t brightness, TBlendType blendType)
  546. {
  547. uint8_t hi5 = index;
  548. #if defined(__AVR__)
  549. hi5 /= 2;
  550. hi5 /= 2;
  551. hi5 /= 2;
  552. #else
  553. hi5 >>= 3;
  554. #endif
  555. uint8_t lo3 = index & 0x07;
  556. // const CRGB* entry = &(pal[0]) + hi5;
  557. // since hi5 is always 0..31, hi4 * sizeof(CRGB) can be a single-byte value,
  558. // instead of the two byte 'int' that avr-gcc defaults to.
  559. // So, we multiply hi5 X sizeof(CRGB), giving hi5XsizeofCRGB;
  560. uint8_t hi5XsizeofCRGB = hi5 * sizeof(CRGB);
  561. // We then add that to a base array pointer.
  562. const CRGB* entry = (CRGB*)( (uint8_t*)(&(pal[0])) + hi5XsizeofCRGB);
  563. uint8_t red1 = entry->red;
  564. uint8_t green1 = entry->green;
  565. uint8_t blue1 = entry->blue;
  566. uint8_t blend = lo3 && (blendType != NOBLEND);
  567. if( blend ) {
  568. if( hi5 == 31 ) {
  569. entry = &(pal[0]);
  570. } else {
  571. entry++;
  572. }
  573. uint8_t f2 = lo3 << 5;
  574. uint8_t f1 = 255 - f2;
  575. uint8_t red2 = entry->red;
  576. red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
  577. red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
  578. red1 += red2;
  579. uint8_t green2 = entry->green;
  580. green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
  581. green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
  582. green1 += green2;
  583. uint8_t blue2 = entry->blue;
  584. blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
  585. blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
  586. blue1 += blue2;
  587. cleanup_R1();
  588. }
  589. if( brightness != 255) {
  590. if( brightness ) {
  591. brightness++; // adjust for rounding
  592. // Now, since brightness is nonzero, we don't need the full scale8_video logic;
  593. // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
  594. if( red1 ) {
  595. red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
  596. #if !(FASTLED_SCALE8_FIXED==1)
  597. red1++;
  598. #endif
  599. }
  600. if( green1 ) {
  601. green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
  602. #if !(FASTLED_SCALE8_FIXED==1)
  603. green1++;
  604. #endif
  605. }
  606. if( blue1 ) {
  607. blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
  608. #if !(FASTLED_SCALE8_FIXED==1)
  609. blue1++;
  610. #endif
  611. }
  612. cleanup_R1();
  613. } else {
  614. red1 = 0;
  615. green1 = 0;
  616. blue1 = 0;
  617. }
  618. }
  619. return CRGB( red1, green1, blue1);
  620. }
  621. CRGB ColorFromPalette( const TProgmemRGBPalette32& pal, uint8_t index, uint8_t brightness, TBlendType blendType)
  622. {
  623. uint8_t hi5 = index;
  624. #if defined(__AVR__)
  625. hi5 /= 2;
  626. hi5 /= 2;
  627. hi5 /= 2;
  628. #else
  629. hi5 >>= 3;
  630. #endif
  631. uint8_t lo3 = index & 0x07;
  632. CRGB entry = FL_PGM_READ_DWORD_NEAR( &(pal[0]) + hi5);
  633. uint8_t red1 = entry.red;
  634. uint8_t green1 = entry.green;
  635. uint8_t blue1 = entry.blue;
  636. uint8_t blend = lo3 && (blendType != NOBLEND);
  637. if( blend ) {
  638. if( hi5 == 31 ) {
  639. entry = FL_PGM_READ_DWORD_NEAR( &(pal[0]) );
  640. } else {
  641. entry = FL_PGM_READ_DWORD_NEAR( &(pal[1]) + hi5 );
  642. }
  643. uint8_t f2 = lo3 << 5;
  644. uint8_t f1 = 255 - f2;
  645. uint8_t red2 = entry.red;
  646. red1 = scale8_LEAVING_R1_DIRTY( red1, f1);
  647. red2 = scale8_LEAVING_R1_DIRTY( red2, f2);
  648. red1 += red2;
  649. uint8_t green2 = entry.green;
  650. green1 = scale8_LEAVING_R1_DIRTY( green1, f1);
  651. green2 = scale8_LEAVING_R1_DIRTY( green2, f2);
  652. green1 += green2;
  653. uint8_t blue2 = entry.blue;
  654. blue1 = scale8_LEAVING_R1_DIRTY( blue1, f1);
  655. blue2 = scale8_LEAVING_R1_DIRTY( blue2, f2);
  656. blue1 += blue2;
  657. cleanup_R1();
  658. }
  659. if( brightness != 255) {
  660. if( brightness ) {
  661. brightness++; // adjust for rounding
  662. // Now, since brightness is nonzero, we don't need the full scale8_video logic;
  663. // we can just to scale8 and then add one (unless scale8 fixed) to all nonzero inputs.
  664. if( red1 ) {
  665. red1 = scale8_LEAVING_R1_DIRTY( red1, brightness);
  666. #if !(FASTLED_SCALE8_FIXED==1)
  667. red1++;
  668. #endif
  669. }
  670. if( green1 ) {
  671. green1 = scale8_LEAVING_R1_DIRTY( green1, brightness);
  672. #if !(FASTLED_SCALE8_FIXED==1)
  673. green1++;
  674. #endif
  675. }
  676. if( blue1 ) {
  677. blue1 = scale8_LEAVING_R1_DIRTY( blue1, brightness);
  678. #if !(FASTLED_SCALE8_FIXED==1)
  679. blue1++;
  680. #endif
  681. }
  682. cleanup_R1();
  683. } else {
  684. red1 = 0;
  685. green1 = 0;
  686. blue1 = 0;
  687. }
  688. }
  689. return CRGB( red1, green1, blue1);
  690. }
  691. CRGB ColorFromPalette( const CRGBPalette256& pal, uint8_t index, uint8_t brightness, TBlendType)
  692. {
  693. const CRGB* entry = &(pal[0]) + index;
  694. uint8_t red = entry->red;
  695. uint8_t green = entry->green;
  696. uint8_t blue = entry->blue;
  697. if( brightness != 255) {
  698. brightness++; // adjust for rounding
  699. red = scale8_video_LEAVING_R1_DIRTY( red, brightness);
  700. green = scale8_video_LEAVING_R1_DIRTY( green, brightness);
  701. blue = scale8_video_LEAVING_R1_DIRTY( blue, brightness);
  702. cleanup_R1();
  703. }
  704. return CRGB( red, green, blue);
  705. }
  706. CHSV ColorFromPalette( const struct CHSVPalette16& pal, uint8_t index, uint8_t brightness, TBlendType blendType)
  707. {
  708. // hi4 = index >> 4;
  709. uint8_t hi4 = lsrX4(index);
  710. uint8_t lo4 = index & 0x0F;
  711. // CRGB rgb1 = pal[ hi4];
  712. const CHSV* entry = &(pal[0]) + hi4;
  713. uint8_t hue1 = entry->hue;
  714. uint8_t sat1 = entry->sat;
  715. uint8_t val1 = entry->val;
  716. uint8_t blend = lo4 && (blendType != NOBLEND);
  717. if( blend ) {
  718. if( hi4 == 15 ) {
  719. entry = &(pal[0]);
  720. } else {
  721. entry++;
  722. }
  723. uint8_t f2 = lo4 << 4;
  724. uint8_t f1 = 255 - f2;
  725. uint8_t hue2 = entry->hue;
  726. uint8_t sat2 = entry->sat;
  727. uint8_t val2 = entry->val;
  728. // Now some special casing for blending to or from
  729. // either black or white. Black and white don't have
  730. // proper 'hue' of their own, so when ramping from
  731. // something else to/from black/white, we set the 'hue'
  732. // of the black/white color to be the same as the hue
  733. // of the other color, so that you get the expected
  734. // brightness or saturation ramp, with hue staying
  735. // constant:
  736. // If we are starting from white (sat=0)
  737. // or black (val=0), adopt the target hue.
  738. if( sat1 == 0 || val1 == 0) {
  739. hue1 = hue2;
  740. }
  741. // If we are ending at white (sat=0)
  742. // or black (val=0), adopt the starting hue.
  743. if( sat2 == 0 || val2 == 0) {
  744. hue2 = hue1;
  745. }
  746. sat1 = scale8_LEAVING_R1_DIRTY( sat1, f1);
  747. val1 = scale8_LEAVING_R1_DIRTY( val1, f1);
  748. sat2 = scale8_LEAVING_R1_DIRTY( sat2, f2);
  749. val2 = scale8_LEAVING_R1_DIRTY( val2, f2);
  750. // cleanup_R1();
  751. // These sums can't overflow, so no qadd8 needed.
  752. sat1 += sat2;
  753. val1 += val2;
  754. uint8_t deltaHue = (uint8_t)(hue2 - hue1);
  755. if( deltaHue & 0x80 ) {
  756. // go backwards
  757. hue1 -= scale8( 256 - deltaHue, f2);
  758. } else {
  759. // go forwards
  760. hue1 += scale8( deltaHue, f2);
  761. }
  762. cleanup_R1();
  763. }
  764. if( brightness != 255) {
  765. val1 = scale8_video( val1, brightness);
  766. }
  767. return CHSV( hue1, sat1, val1);
  768. }
  769. CHSV ColorFromPalette( const struct CHSVPalette32& pal, uint8_t index, uint8_t brightness, TBlendType blendType)
  770. {
  771. uint8_t hi5 = index;
  772. #if defined(__AVR__)
  773. hi5 /= 2;
  774. hi5 /= 2;
  775. hi5 /= 2;
  776. #else
  777. hi5 >>= 3;
  778. #endif
  779. uint8_t lo3 = index & 0x07;
  780. uint8_t hi5XsizeofCHSV = hi5 * sizeof(CHSV);
  781. const CHSV* entry = (CHSV*)( (uint8_t*)(&(pal[0])) + hi5XsizeofCHSV);
  782. uint8_t hue1 = entry->hue;
  783. uint8_t sat1 = entry->sat;
  784. uint8_t val1 = entry->val;
  785. uint8_t blend = lo3 && (blendType != NOBLEND);
  786. if( blend ) {
  787. if( hi5 == 31 ) {
  788. entry = &(pal[0]);
  789. } else {
  790. entry++;
  791. }
  792. uint8_t f2 = lo3 << 5;
  793. uint8_t f1 = 255 - f2;
  794. uint8_t hue2 = entry->hue;
  795. uint8_t sat2 = entry->sat;
  796. uint8_t val2 = entry->val;
  797. // Now some special casing for blending to or from
  798. // either black or white. Black and white don't have
  799. // proper 'hue' of their own, so when ramping from
  800. // something else to/from black/white, we set the 'hue'
  801. // of the black/white color to be the same as the hue
  802. // of the other color, so that you get the expected
  803. // brightness or saturation ramp, with hue staying
  804. // constant:
  805. // If we are starting from white (sat=0)
  806. // or black (val=0), adopt the target hue.
  807. if( sat1 == 0 || val1 == 0) {
  808. hue1 = hue2;
  809. }
  810. // If we are ending at white (sat=0)
  811. // or black (val=0), adopt the starting hue.
  812. if( sat2 == 0 || val2 == 0) {
  813. hue2 = hue1;
  814. }
  815. sat1 = scale8_LEAVING_R1_DIRTY( sat1, f1);
  816. val1 = scale8_LEAVING_R1_DIRTY( val1, f1);
  817. sat2 = scale8_LEAVING_R1_DIRTY( sat2, f2);
  818. val2 = scale8_LEAVING_R1_DIRTY( val2, f2);
  819. // cleanup_R1();
  820. // These sums can't overflow, so no qadd8 needed.
  821. sat1 += sat2;
  822. val1 += val2;
  823. uint8_t deltaHue = (uint8_t)(hue2 - hue1);
  824. if( deltaHue & 0x80 ) {
  825. // go backwards
  826. hue1 -= scale8( 256 - deltaHue, f2);
  827. } else {
  828. // go forwards
  829. hue1 += scale8( deltaHue, f2);
  830. }
  831. cleanup_R1();
  832. }
  833. if( brightness != 255) {
  834. val1 = scale8_video( val1, brightness);
  835. }
  836. return CHSV( hue1, sat1, val1);
  837. }
  838. CHSV ColorFromPalette( const struct CHSVPalette256& pal, uint8_t index, uint8_t brightness, TBlendType)
  839. {
  840. CHSV hsv = *( &(pal[0]) + index );
  841. if( brightness != 255) {
  842. hsv.value = scale8_video( hsv.value, brightness);
  843. }
  844. return hsv;
  845. }
  846. void UpscalePalette(const struct CRGBPalette16& srcpal16, struct CRGBPalette256& destpal256)
  847. {
  848. for( int i = 0; i < 256; i++) {
  849. destpal256[(uint8_t)(i)] = ColorFromPalette( srcpal16, i);
  850. }
  851. }
  852. void UpscalePalette(const struct CHSVPalette16& srcpal16, struct CHSVPalette256& destpal256)
  853. {
  854. for( int i = 0; i < 256; i++) {
  855. destpal256[(uint8_t)(i)] = ColorFromPalette( srcpal16, i);
  856. }
  857. }
  858. void UpscalePalette(const struct CRGBPalette16& srcpal16, struct CRGBPalette32& destpal32)
  859. {
  860. for( uint8_t i = 0; i < 16; i++) {
  861. uint8_t j = i * 2;
  862. destpal32[j+0] = srcpal16[i];
  863. destpal32[j+1] = srcpal16[i];
  864. }
  865. }
  866. void UpscalePalette(const struct CHSVPalette16& srcpal16, struct CHSVPalette32& destpal32)
  867. {
  868. for( uint8_t i = 0; i < 16; i++) {
  869. uint8_t j = i * 2;
  870. destpal32[j+0] = srcpal16[i];
  871. destpal32[j+1] = srcpal16[i];
  872. }
  873. }
  874. void UpscalePalette(const struct CRGBPalette32& srcpal32, struct CRGBPalette256& destpal256)
  875. {
  876. for( int i = 0; i < 256; i++) {
  877. destpal256[(uint8_t)(i)] = ColorFromPalette( srcpal32, i);
  878. }
  879. }
  880. void UpscalePalette(const struct CHSVPalette32& srcpal32, struct CHSVPalette256& destpal256)
  881. {
  882. for( int i = 0; i < 256; i++) {
  883. destpal256[(uint8_t)(i)] = ColorFromPalette( srcpal32, i);
  884. }
  885. }
  886. #if 0
  887. // replaced by PartyColors_p
  888. void SetupPartyColors(CRGBPalette16& pal)
  889. {
  890. fill_gradient( pal, 0, CHSV( HUE_PURPLE,255,255), 7, CHSV(HUE_YELLOW - 18,255,255), FORWARD_HUES);
  891. fill_gradient( pal, 8, CHSV( HUE_ORANGE,255,255), 15, CHSV(HUE_BLUE + 18,255,255), BACKWARD_HUES);
  892. }
  893. #endif
  894. void nblendPaletteTowardPalette( CRGBPalette16& current, CRGBPalette16& target, uint8_t maxChanges)
  895. {
  896. uint8_t* p1;
  897. uint8_t* p2;
  898. uint8_t changes = 0;
  899. p1 = (uint8_t*)current.entries;
  900. p2 = (uint8_t*)target.entries;
  901. const uint8_t totalChannels = sizeof(CRGBPalette16);
  902. for( uint8_t i = 0; i < totalChannels; i++) {
  903. // if the values are equal, no changes are needed
  904. if( p1[i] == p2[i] ) { continue; }
  905. // if the current value is less than the target, increase it by one
  906. if( p1[i] < p2[i] ) { p1[i]++; changes++; }
  907. // if the current value is greater than the target,
  908. // increase it by one (or two if it's still greater).
  909. if( p1[i] > p2[i] ) {
  910. p1[i]--; changes++;
  911. if( p1[i] > p2[i] ) { p1[i]--; }
  912. }
  913. // if we've hit the maximum number of changes, exit
  914. if( changes >= maxChanges) { break; }
  915. }
  916. }
  917. uint8_t applyGamma_video( uint8_t brightness, float gamma)
  918. {
  919. float orig;
  920. float adj;
  921. orig = (float)(brightness) / (255.0);
  922. adj = pow( orig, gamma) * (255.0);
  923. uint8_t result = (uint8_t)(adj);
  924. if( (brightness > 0) && (result == 0)) {
  925. result = 1; // never gamma-adjust a positive number down to zero
  926. }
  927. return result;
  928. }
  929. CRGB applyGamma_video( const CRGB& orig, float gamma)
  930. {
  931. CRGB adj;
  932. adj.r = applyGamma_video( orig.r, gamma);
  933. adj.g = applyGamma_video( orig.g, gamma);
  934. adj.b = applyGamma_video( orig.b, gamma);
  935. return adj;
  936. }
  937. CRGB applyGamma_video( const CRGB& orig, float gammaR, float gammaG, float gammaB)
  938. {
  939. CRGB adj;
  940. adj.r = applyGamma_video( orig.r, gammaR);
  941. adj.g = applyGamma_video( orig.g, gammaG);
  942. adj.b = applyGamma_video( orig.b, gammaB);
  943. return adj;
  944. }
  945. CRGB& napplyGamma_video( CRGB& rgb, float gamma)
  946. {
  947. rgb = applyGamma_video( rgb, gamma);
  948. return rgb;
  949. }
  950. CRGB& napplyGamma_video( CRGB& rgb, float gammaR, float gammaG, float gammaB)
  951. {
  952. rgb = applyGamma_video( rgb, gammaR, gammaG, gammaB);
  953. return rgb;
  954. }
  955. void napplyGamma_video( CRGB* rgbarray, uint16_t count, float gamma)
  956. {
  957. for( uint16_t i = 0; i < count; i++) {
  958. rgbarray[i] = applyGamma_video( rgbarray[i], gamma);
  959. }
  960. }
  961. void napplyGamma_video( CRGB* rgbarray, uint16_t count, float gammaR, float gammaG, float gammaB)
  962. {
  963. for( uint16_t i = 0; i < count; i++) {
  964. rgbarray[i] = applyGamma_video( rgbarray[i], gammaR, gammaG, gammaB);
  965. }
  966. }
  967. FASTLED_NAMESPACE_END