-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlib8tion.h
1434 lines (1255 loc) · 45.1 KB
/
lib8tion.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#ifndef __INC_LIB8TION_H
#define __INC_LIB8TION_H
#include "PicoLED.h"
/// @file lib8tion.h
/// Fast, efficient 8-bit math functions specifically
/// designed for high-performance LED programming.
#include <stdint.h>
/// Define a LIB8TION member function as static inline with an "unused" attribute
#define LIB8STATIC __attribute__ ((unused)) static inline
/// Define a LIB8TION member function as always static inline
#define LIB8STATIC_ALWAYS_INLINE __attribute__ ((always_inline)) static inline
#if !defined(__AVR__)
#include <string.h>
// for memmove, memcpy, and memset if not defined here
#endif // end of !defined(__AVR__)
#if defined(__arm__)
#if defined(FASTLED_TEENSY3)
// Can use Cortex M4 DSP instructions
#define QADD8_C 0
#define QADD7_C 0
#define QADD8_ARM_DSP_ASM 1
#define QADD7_ARM_DSP_ASM 1
#else
// Generic ARM
#define QADD8_C 1
#define QADD7_C 1
#endif // end of defined(FASTLED_TEENSY3)
#define QSUB8_C 1
#define SCALE8_C 1
#define SCALE16BY8_C 1
#define SCALE16_C 1
#define ABS8_C 1
#define MUL8_C 1
#define QMUL8_C 1
#define ADD8_C 1
#define SUB8_C 1
#define EASE8_C 1
#define AVG8_C 1
#define AVG8R_C 1
#define AVG7_C 1
#define AVG16_C 1
#define AVG16R_C 1
#define AVG15_C 1
#define BLEND8_C 1
// end of #if defined(__arm__)
#elif defined(ARDUINO_ARCH_APOLLO3)
// Default to using the standard C functions for now
#define QADD8_C 1
#define QADD7_C 1
#define QSUB8_C 1
#define SCALE8_C 1
#define SCALE16BY8_C 1
#define SCALE16_C 1
#define ABS8_C 1
#define MUL8_C 1
#define QMUL8_C 1
#define ADD8_C 1
#define SUB8_C 1
#define EASE8_C 1
#define AVG8_C 1
#define AVG8R_C 1
#define AVG7_C 1
#define AVG16_C 1
#define AVG16R_C 1
#define AVG15_C 1
#define BLEND8_C 1
// end of #elif defined(ARDUINO_ARCH_APOLLO3)
#elif defined(__AVR__)
// AVR ATmega and friends Arduino
#define QADD8_C 0
#define QADD7_C 0
#define QSUB8_C 0
#define ABS8_C 0
#define ADD8_C 0
#define SUB8_C 0
#define AVG8_C 0
#define AVG8R_C 0
#define AVG7_C 0
#define AVG16_C 0
#define AVG16R_C 0
#define AVG15_C 0
#define QADD8_AVRASM 1
#define QADD7_AVRASM 1
#define QSUB8_AVRASM 1
#define ABS8_AVRASM 1
#define ADD8_AVRASM 1
#define SUB8_AVRASM 1
#define AVG8_AVRASM 1
#define AVG8R_AVRASM 1
#define AVG7_AVRASM 1
#define AVG16_AVRASM 1
#define AVG16R_AVRASM 1
#define AVG15_AVRASM 1
// Note: these require hardware MUL instruction
// -- sorry, ATtiny!
#if !defined(LIB8_ATTINY)
#define SCALE8_C 0
#define SCALE16BY8_C 0
#define SCALE16_C 0
#define MUL8_C 0
#define QMUL8_C 0
#define EASE8_C 0
#define BLEND8_C 0
#define SCALE8_AVRASM 1
#define SCALE16BY8_AVRASM 1
#define SCALE16_AVRASM 1
#define MUL8_AVRASM 1
#define QMUL8_AVRASM 1
#define EASE8_AVRASM 1
#define CLEANUP_R1_AVRASM 1
#define BLEND8_AVRASM 1
#else
// On ATtiny, we just use C implementations
#define SCALE8_C 1
#define SCALE16BY8_C 1
#define SCALE16_C 1
#define MUL8_C 1
#define QMUL8_C 1
#define EASE8_C 1
#define BLEND8_C 1
#define SCALE8_AVRASM 0
#define SCALE16BY8_AVRASM 0
#define SCALE16_AVRASM 0
#define MUL8_AVRASM 0
#define QMUL8_AVRASM 0
#define EASE8_AVRASM 0
#define BLEND8_AVRASM 0
#endif // end of !defined(LIB8_ATTINY)
// end of #elif defined(__AVR__)
#else
// Doxygen: ignore these macros
/// @cond
// unspecified architecture, so
// no ASM, everything in C
#define QADD8_C 1
#define QADD7_C 1
#define QSUB8_C 1
#define SCALE8_C 1
#define SCALE16BY8_C 1
#define SCALE16_C 1
#define ABS8_C 1
#define MUL8_C 1
#define QMUL8_C 1
#define ADD8_C 1
#define SUB8_C 1
#define EASE8_C 1
#define AVG8_C 1
#define AVG8R_C 1
#define AVG7_C 1
#define AVG16_C 1
#define AVG16R_C 1
#define AVG15_C 1
#define BLEND8_C 1
/// @endcond
#endif
/// @defgroup lib8tion Fast Math Functions
/// Fast, efficient 8-bit math functions specifically
/// designed for high-performance LED programming.
///
/// Because of the AVR (Arduino) and ARM assembly language
/// implementations provided, using these functions often
/// results in smaller and faster code than the equivalent
/// program using plain "C" arithmetic and logic.
///
/// Included are:
///
/// - Saturating unsigned 8-bit add and subtract.
/// Instead of wrapping around if an overflow occurs,
/// these routines just 'clamp' the output at a maxumum
/// of 255, or a minimum of 0. Useful for adding pixel
/// values. E.g., qadd8( 200, 100) = 255.
/// @code
/// qadd8( i, j) == MIN( (i + j), 0xFF )
/// qsub8( i, j) == MAX( (i - j), 0 )
/// @endcode
///
/// - Saturating signed 8-bit ("7-bit") add.
/// @code
/// qadd7( i, j) == MIN( (i + j), 0x7F)
/// @endcode
///
/// - Scaling (down) of unsigned 8- and 16- bit values.
/// Scaledown value is specified in 1/256ths.
/// @code
/// scale8( i, sc) == (i * sc) / 256
/// scale16by8( i, sc) == (i * sc) / 256
/// @endcode
///
/// Example: scaling a 0-255 value down into a
/// range from 0-99:
/// @code
/// downscaled = scale8( originalnumber, 100);
/// @endcode
///
/// A special version of scale8 is provided for scaling
/// LED brightness values, to make sure that they don't
/// accidentally scale down to total black at low
/// dimming levels, since that would look wrong:
/// @code
/// scale8_video( i, sc) = ((i * sc) / 256) +? 1
/// @endcode
///
/// Example: reducing an LED brightness by a
/// dimming factor:
/// @code
/// new_bright = scale8_video( orig_bright, dimming);
/// @endcode
///
/// - Fast 8- and 16- bit unsigned random numbers.
/// Significantly faster than Arduino random(), but
/// also somewhat less random. You can add entropy.
/// @code
/// random8() == random from 0..255
/// random8( n) == random from 0..(N-1)
/// random8( n, m) == random from N..(M-1)
///
/// random16() == random from 0..65535
/// random16( n) == random from 0..(N-1)
/// random16( n, m) == random from N..(M-1)
///
/// random16_set_seed( k) == seed = k
/// random16_add_entropy( k) == seed += k
/// @endcode
///
/// - Absolute value of a signed 8-bit value.
/// @code
/// abs8( i) == abs( i)
/// @endcode
///
/// - 8-bit math operations which return 8-bit values.
/// These are provided mostly for completeness,
/// not particularly for performance.
/// @code
/// mul8( i, j) == (i * j) & 0xFF
/// add8( i, j) == (i + j) & 0xFF
/// sub8( i, j) == (i - j) & 0xFF
/// @endcode
///
/// - Fast 16-bit approximations of sin and cos.
/// Input angle is a uint16_t from 0-65535.
/// Output is a signed int16_t from -32767 to 32767.
/// @code
/// sin16( x) == sin( (x/32768.0) * pi) * 32767
/// cos16( x) == cos( (x/32768.0) * pi) * 32767
/// @endcode
///
/// Accurate to more than 99% in all cases.
///
/// - Fast 8-bit approximations of sin and cos.
/// Input angle is a uint8_t from 0-255.
/// Output is an UNsigned uint8_t from 0 to 255.
/// @code
/// sin8( x) == (sin( (x/128.0) * pi) * 128) + 128
/// cos8( x) == (cos( (x/128.0) * pi) * 128) + 128
/// @endcode
///
/// Accurate to within about 2%.
///
/// - Fast 8-bit "easing in/out" function.
/// @code
/// ease8InOutCubic(x) == 3(x^2) - 2(x^3)
/// ease8InOutApprox(x) ==
/// faster, rougher, approximation of cubic easing
/// ease8InOutQuad(x) == quadratic (vs cubic) easing
/// @endcode
///
/// - Cubic, Quadratic, and Triangle wave functions.
/// Input is a uint8_t representing phase withing the wave,
/// similar to how sin8 takes an angle 'theta'.
/// Output is a uint8_t representing the amplitude of
/// the wave at that point.
/// @code
/// cubicwave8( x)
/// quadwave8( x)
/// triwave8( x)
/// @endcode
///
/// - Square root for 16-bit integers. About three times
/// faster and five times smaller than Arduino's built-in
/// generic 32-bit sqrt routine.
/// @code
/// sqrt16( uint16_t x ) == sqrt( x)
/// @endcode
///
/// - Dimming and brightening functions for 8-bit
/// light values.
/// @code
/// dim8_video( x) == scale8_video( x, x)
/// dim8_raw( x) == scale8( x, x)
/// dim8_lin( x) == (x<128) ? ((x+1)/2) : scale8(x,x)
/// brighten8_video( x) == 255 - dim8_video( 255 - x)
/// brighten8_raw( x) == 255 - dim8_raw( 255 - x)
/// brighten8_lin( x) == 255 - dim8_lin( 255 - x)
/// @endcode
///
/// The dimming functions in particular are suitable
/// for making LED light output appear more 'linear'.
///
/// - Linear interpolation between two values, with the
/// fraction between them expressed as an 8- or 16-bit
/// fixed point fraction (fract8 or fract16).
/// @code
/// lerp8by8( fromU8, toU8, fract8 )
/// lerp16by8( fromU16, toU16, fract8 )
/// lerp15by8( fromS16, toS16, fract8 )
/// == from + (( to - from ) * fract8) / 256)
/// lerp16by16( fromU16, toU16, fract16 )
/// == from + (( to - from ) * fract16) / 65536)
/// map8( in, rangeStart, rangeEnd)
/// == map( in, 0, 255, rangeStart, rangeEnd);
/// @endcode
///
/// - Optimized memmove, memcpy, and memset, that are
/// faster than standard avr-libc 1.8.
/// @code
/// memmove8( dest, src, bytecount)
/// memcpy8( dest, src, bytecount)
/// memset8( buf, value, bytecount)
/// @endcode
///
/// - Beat generators which return sine or sawtooth
/// waves in a specified number of Beats Per Minute.
/// Sine wave beat generators can specify a low and
/// high range for the output. Sawtooth wave beat
/// generators always range 0-255 or 0-65535.
/// @code
/// beatsin8( BPM, low8, high8)
/// = (sine(beatphase) * (high8-low8)) + low8
/// beatsin16( BPM, low16, high16)
/// = (sine(beatphase) * (high16-low16)) + low16
/// beatsin88( BPM88, low16, high16)
/// = (sine(beatphase) * (high16-low16)) + low16
/// beat8( BPM) = 8-bit repeating sawtooth wave
/// beat16( BPM) = 16-bit repeating sawtooth wave
/// beat88( BPM88) = 16-bit repeating sawtooth wave
/// @endcode
///
/// BPM is beats per minute in either simple form
/// e.g. 120, or Q8.8 fixed-point form.
/// BPM88 is beats per minute in ONLY Q8.8 fixed-point
/// form.
///
/// Lib8tion is pronounced like 'libation': lie-BAY-shun
///
/// @{
///////////////////////////////////////////////////////////////////////
///
/// @defgroup FractionalTypes Fixed-Point Fractional Types.
/// Types for storing fractional data.
///
/// * ::sfract7 should be interpreted as signed 128ths.
/// * ::fract8 should be interpreted as unsigned 256ths.
/// * ::sfract15 should be interpreted as signed 32768ths.
/// * ::fract16 should be interpreted as unsigned 65536ths.
///
/// Example: if a fract8 has the value "64", that should be interpreted
/// as 64/256ths, or one-quarter.
///
/// accumXY types should be interpreted as X bits of integer,
/// and Y bits of fraction.
/// E.g., ::accum88 has 8 bits of int, 8 bits of fraction
///
/// @{
/// ANSI: unsigned short _Fract.
/// Range is 0 to 0.99609375 in steps of 0.00390625.
/// Should be interpreted as unsigned 256ths.
typedef uint8_t fract8;
/// ANSI: signed short _Fract.
/// Range is -0.9921875 to 0.9921875 in steps of 0.0078125.
/// Should be interpreted as signed 128ths.
typedef int8_t sfract7;
/// ANSI: unsigned _Fract.
/// Range is 0 to 0.99998474121 in steps of 0.00001525878.
/// Should be interpreted as unsigned 65536ths.
typedef uint16_t fract16;
/// ANSI: signed _Fract.
/// Range is -0.99996948242 to 0.99996948242 in steps of 0.00003051757.
/// Should be interpreted as signed 32768ths.
typedef int16_t sfract15;
typedef uint16_t accum88; ///< ANSI: unsigned short _Accum. 8 bits int, 8 bits fraction
typedef int16_t saccum78; ///< ANSI: signed short _Accum. 7 bits int, 8 bits fraction
typedef uint32_t accum1616; ///< ANSI: signed _Accum. 16 bits int, 16 bits fraction
typedef int32_t saccum1516; ///< ANSI: signed _Accum. 15 bits int, 16 bits fraction
typedef uint16_t accum124; ///< no direct ANSI counterpart. 12 bits int, 4 bits fraction
typedef int32_t saccum114; ///< no direct ANSI counterpart. 1 bit int, 14 bits fraction
/// typedef for IEEE754 "binary32" float type internals
/// @see https://en.wikipedia.org/wiki/IEEE_754
typedef union {
uint32_t i; ///< raw value, as an integer
float f; ///< raw value, as a float
struct {
uint32_t mantissa: 23; ///< 23-bit mantissa
uint32_t exponent: 8; ///< 8-bit exponent
uint32_t signbit: 1; ///< sign bit
};
struct {
uint32_t mant7 : 7; ///< @todo Doc: what is this for?
uint32_t mant16: 16; ///< @todo Doc: what is this for?
uint32_t exp_ : 8; ///< @todo Doc: what is this for?
uint32_t sb_ : 1; ///< @todo Doc: what is this for?
};
struct {
uint32_t mant_lo8 : 8; ///< @todo Doc: what is this for?
uint32_t mant_hi16_exp_lo1 : 16; ///< @todo Doc: what is this for?
uint32_t sb_exphi7 : 8; ///< @todo Doc: what is this for?
};
} IEEE754binary32_t;
/// @} FractionalTypes
#include "lib8tion/math8.h"
#include "lib8tion/scale8.h"
#include "lib8tion/random8.h"
#include "lib8tion/trig8.h"
///////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////
///
/// @defgroup FloatConversions Float-to-Fixed and Fixed-to-Float Conversions
/// Functions to convert between floating point and fixed point types.
/// @note Anything involving a "float" on AVR will be slower.
/// @{
/// Conversion from 16-bit fixed point (::sfract15) to IEEE754 32-bit float.
LIB8STATIC float sfract15ToFloat( sfract15 y)
{
return y / 32768.0;
}
/// Conversion from IEEE754 float in the range (-1,1) to 16-bit fixed point (::sfract15).
/// @note The extremes of one and negative one are NOT representable! The
/// representable range is 0.99996948242 to -0.99996948242, in steps of 0.00003051757.
LIB8STATIC sfract15 floatToSfract15( float f)
{
return f * 32768.0;
}
/// @} FloatConversions
///////////////////////////////////////////////////////////////////////
///
/// @defgroup FastMemory Fast Memory Functions for AVR
/// Alternatives to memmove, memcpy, and memset that are
/// faster on AVR than standard avr-libc 1.8.
/// @{
#if defined(__AVR__) || defined(FASTLED_DOXYGEN)
extern "C" {
void * memmove8( void * dst, const void * src, uint16_t num ); ///< Faster alternative to memmove() on AVR
void * memcpy8 ( void * dst, const void * src, uint16_t num ) __attribute__ ((noinline)); ///< Faster alternative to memcpy() on AVR
void * memset8 ( void * ptr, uint8_t value, uint16_t num ) __attribute__ ((noinline)) ; ///< Faster alternative to memset() on AVR
}
#else
// on non-AVR platforms, these names just call standard libc.
#define memmove8 memmove
#define memcpy8 memcpy
#define memset8 memset
#endif
/// @} FastMemory
///////////////////////////////////////////////////////////////////////
///
/// @defgroup LinearInterpolation Linear Interpolation
/// Fast linear interpolation functions, such as could be used for Perlin noise, etc.
///
/// A note on the structure of the lerp functions:
/// The cases for b>a and b<=a are handled separately for
/// speed. Without knowing the relative order of a and b,
/// the value (a-b) might be overflow the width of a or b,
/// and have to be promoted to a wider, slower type.
/// To avoid that, we separate the two cases, and are able
/// to do all the math in the same width as the arguments,
/// which is much faster and smaller on AVR.
/// @{
/// Linear interpolation between two unsigned 8-bit values,
/// with 8-bit fraction
LIB8STATIC uint8_t lerp8by8( uint8_t a, uint8_t b, fract8 frac)
{
uint8_t result;
if( b > a) {
uint8_t delta = b - a;
uint8_t scaled = scale8( delta, frac);
result = a + scaled;
} else {
uint8_t delta = a - b;
uint8_t scaled = scale8( delta, frac);
result = a - scaled;
}
return result;
}
/// Linear interpolation between two unsigned 16-bit values,
/// with 16-bit fraction
LIB8STATIC uint16_t lerp16by16( uint16_t a, uint16_t b, fract16 frac)
{
uint16_t result;
if( b > a ) {
uint16_t delta = b - a;
uint16_t scaled = scale16(delta, frac);
result = a + scaled;
} else {
uint16_t delta = a - b;
uint16_t scaled = scale16( delta, frac);
result = a - scaled;
}
return result;
}
/// Linear interpolation between two unsigned 16-bit values,
/// with 8-bit fraction
LIB8STATIC uint16_t lerp16by8( uint16_t a, uint16_t b, fract8 frac)
{
uint16_t result;
if( b > a) {
uint16_t delta = b - a;
uint16_t scaled = scale16by8( delta, frac);
result = a + scaled;
} else {
uint16_t delta = a - b;
uint16_t scaled = scale16by8( delta, frac);
result = a - scaled;
}
return result;
}
/// Linear interpolation between two signed 15-bit values,
/// with 8-bit fraction
LIB8STATIC int16_t lerp15by8( int16_t a, int16_t b, fract8 frac)
{
int16_t result;
if( b > a) {
uint16_t delta = b - a;
uint16_t scaled = scale16by8( delta, frac);
result = a + scaled;
} else {
uint16_t delta = a - b;
uint16_t scaled = scale16by8( delta, frac);
result = a - scaled;
}
return result;
}
/// Linear interpolation between two signed 15-bit values,
/// with 8-bit fraction
LIB8STATIC int16_t lerp15by16( int16_t a, int16_t b, fract16 frac)
{
int16_t result;
if( b > a) {
uint16_t delta = b - a;
uint16_t scaled = scale16( delta, frac);
result = a + scaled;
} else {
uint16_t delta = a - b;
uint16_t scaled = scale16( delta, frac);
result = a - scaled;
}
return result;
}
/// Map from one full-range 8-bit value into a narrower
/// range of 8-bit values, possibly a range of hues.
///
/// E.g. map `myValue` into a hue in the range blue..purple..pink..red
/// @code
/// hue = map8( myValue, HUE_BLUE, HUE_RED);
/// @endcode
///
/// Combines nicely with the waveform functions (like sin8(), etc)
/// to produce continuous hue gradients back and forth:
/// @code
/// hue = map8( sin8( myValue), HUE_BLUE, HUE_RED);
/// @endcode
///
/// Mathematically simiar to lerp8by8(), but arguments are more
/// like Arduino's "map"; this function is similar to
/// @code
/// map( in, 0, 255, rangeStart, rangeEnd)
/// @endcode
///
/// but faster and specifically designed for 8-bit values.
LIB8STATIC uint8_t map8( uint8_t in, uint8_t rangeStart, uint8_t rangeEnd)
{
uint8_t rangeWidth = rangeEnd - rangeStart;
uint8_t out = scale8( in, rangeWidth);
out += rangeStart;
return out;
}
/// @} LinearInterpolation
///////////////////////////////////////////////////////////////////////
///
/// @defgroup Easing Easing Functions
/// Specify the rate of change of a parameter over time.
/// @see http://easings.net
/// @{
/// 8-bit quadratic ease-in / ease-out function.
/// Takes around 13 cycles on AVR.
#if (EASE8_C == 1) || defined(FASTLED_DOXYGEN)
LIB8STATIC uint8_t ease8InOutQuad( uint8_t i)
{
uint8_t j = i;
if( j & 0x80 ) {
j = 255 - j;
}
uint8_t jj = scale8( j, j);
uint8_t jj2 = jj << 1;
if( i & 0x80 ) {
jj2 = 255 - jj2;
}
return jj2;
}
#elif EASE8_AVRASM == 1
// This AVR asm version of ease8InOutQuad preserves one more
// low-bit of precision than the C version, and is also slightly
// smaller and faster.
LIB8STATIC uint8_t ease8InOutQuad(uint8_t val) {
uint8_t j=val;
asm volatile (
"sbrc %[val], 7 \n"
"com %[j] \n"
"mul %[j], %[j] \n"
"add r0, %[j] \n"
"ldi %[j], 0 \n"
"adc %[j], r1 \n"
"lsl r0 \n" // carry = high bit of low byte of mul product
"rol %[j] \n" // j = (j * 2) + carry // preserve add'l bit of precision
"sbrc %[val], 7 \n"
"com %[j] \n"
"clr __zero_reg__ \n"
: [j] "+&a" (j)
: [val] "a" (val)
: "r0", "r1"
);
return j;
}
#else
#error "No implementation for ease8InOutQuad available."
#endif
/// 16-bit quadratic ease-in / ease-out function.
/// C implementation at this point.
LIB8STATIC uint16_t ease16InOutQuad( uint16_t i)
{
uint16_t j = i;
if( j & 0x8000 ) {
j = 65535 - j;
}
uint16_t jj = scale16( j, j);
uint16_t jj2 = jj << 1;
if( i & 0x8000 ) {
jj2 = 65535 - jj2;
}
return jj2;
}
/// 8-bit cubic ease-in / ease-out function.
/// Takes around 18 cycles on AVR.
LIB8STATIC fract8 ease8InOutCubic( fract8 i)
{
uint8_t ii = scale8_LEAVING_R1_DIRTY( i, i);
uint8_t iii = scale8_LEAVING_R1_DIRTY( ii, i);
uint16_t r1 = (3 * (uint16_t)(ii)) - ( 2 * (uint16_t)(iii));
/* the code generated for the above *'s automatically
cleans up R1, so there's no need to explicitily call
cleanup_R1(); */
uint8_t result = r1;
// if we got "256", return 255:
if( r1 & 0x100 ) {
result = 255;
}
return result;
}
/// Fast, rough 8-bit ease-in/ease-out function.
/// Shaped approximately like ease8InOutCubic(),
/// it's never off by more than a couple of percent
/// from the actual cubic S-curve, and it executes
/// more than twice as fast. Use when the cycles
/// are more important than visual smoothness.
/// Asm version takes around 7 cycles on AVR.
#if (EASE8_C == 1) || defined(FASTLED_DOXYGEN)
LIB8STATIC fract8 ease8InOutApprox( fract8 i)
{
if( i < 64) {
// start with slope 0.5
i /= 2;
} else if( i > (255 - 64)) {
// end with slope 0.5
i = 255 - i;
i /= 2;
i = 255 - i;
} else {
// in the middle, use slope 192/128 = 1.5
i -= 64;
i += (i / 2);
i += 32;
}
return i;
}
#elif EASE8_AVRASM == 1
LIB8STATIC uint8_t ease8InOutApprox( fract8 i)
{
// takes around 7 cycles on AVR
asm volatile (
" subi %[i], 64 \n\t"
" cpi %[i], 128 \n\t"
" brcc Lshift_%= \n\t"
// middle case
" mov __tmp_reg__, %[i] \n\t"
" lsr __tmp_reg__ \n\t"
" add %[i], __tmp_reg__ \n\t"
" subi %[i], 224 \n\t"
" rjmp Ldone_%= \n\t"
// start or end case
"Lshift_%=: \n\t"
" lsr %[i] \n\t"
" subi %[i], 96 \n\t"
"Ldone_%=: \n\t"
: [i] "+a" (i)
:
: "r0"
);
return i;
}
#else
#error "No implementation for ease8 available."
#endif
/// @} Easing
///////////////////////////////////////////////////////////////////////
///
/// @defgroup WaveformGenerators Waveform Generators
/// General purpose wave generator functions.
///
/// @{
/// Triangle wave generator.
/// Useful for turning a one-byte ever-increasing value into a
/// one-byte value that oscillates up and down.
/// @code
/// input output
/// 0..127 0..254 (positive slope)
/// 128..255 254..0 (negative slope)
/// @endcode
///
/// On AVR this function takes just three cycles.
///
LIB8STATIC uint8_t triwave8(uint8_t in)
{
if( in & 0x80) {
in = 255 - in;
}
uint8_t out = in << 1;
return out;
}
/// Quadratic waveform generator. Spends just a little
/// more time at the limits than "sine" does.
///
/// S-shaped wave generator (like "sine"). Useful
/// for turning a one-byte "counter" value into a
/// one-byte oscillating value that moves smoothly up and down,
/// with an "acceleration" and "deceleration" curve.
///
/// This is even faster than "sin8()", and has
/// a slightly different curve shape.
LIB8STATIC uint8_t quadwave8(uint8_t in)
{
return ease8InOutQuad( triwave8( in));
}
/// Cubic waveform generator. Spends visibly more time
/// at the limits than "sine" does.
/// @copydetails quadwave8()
LIB8STATIC uint8_t cubicwave8(uint8_t in)
{
return ease8InOutCubic( triwave8( in));
}
/// Square wave generator.
/// Useful for turning a one-byte ever-increasing value
/// into a one-byte value that is either 0 or 255.
/// The width of the output "pulse" is determined by
/// the pulsewidth argument:
/// @code
/// if pulsewidth is 255, output is always 255.
/// if pulsewidth < 255, then
/// if input < pulsewidth then output is 255
/// if input >= pulsewidth then output is 0
/// @endcode
///
/// The output looking like:
///
/// @code
/// 255 +--pulsewidth--+
/// . | |
/// 0 0 +--------(256-pulsewidth)--------
/// @endcode
///
/// @param in input value
/// @param pulsewidth width of the output pulse
/// @returns square wave output
LIB8STATIC uint8_t squarewave8( uint8_t in, uint8_t pulsewidth=128)
{
if( in < pulsewidth || (pulsewidth == 255)) {
return 255;
} else {
return 0;
}
}
/// @} WaveformGenerators
/// @addtogroup FractionalTypes
/// @{
/// Template class for representing fractional ints.
/// @tparam T underlying type for data storage
/// @tparam F number of fractional bits
/// @tparam I number of integer bits
template<class T, int F, int I> class q {
T i:I; ///< Integer value of number
T f:F; ///< Fractional value of number
public:
/// Constructor, storing a float as a fractional int
q(float fx) { i = fx; f = (fx-i) * (1<<F); }
/// Constructor, storing a fractional int directly
q(uint8_t _i, uint8_t _f) {i=_i; f=_f; }
/// Multiply the fractional int by a value
uint32_t operator*(uint32_t v) { return (v*i) + ((v*f)>>F); }
/// @copydoc operator*(uint32_t)
uint16_t operator*(uint16_t v) { return (v*i) + ((v*f)>>F); }
/// @copydoc operator*(uint32_t)
int32_t operator*(int32_t v) { return (v*i) + ((v*f)>>F); }
/// @copydoc operator*(uint32_t)
int16_t operator*(int16_t v) { return (v*i) + ((v*f)>>F); }
#if defined(FASTLED_ARM) | defined(FASTLED_RISCV) | defined(FASTLED_APOLLO3)
/// @copydoc operator*(uint32_t)
int operator*(int v) { return (v*i) + ((v*f)>>F); }
#endif
};
template<class T, int F, int I> static uint32_t operator*(uint32_t v, q<T,F,I> & q) { return q * v; }
template<class T, int F, int I> static uint16_t operator*(uint16_t v, q<T,F,I> & q) { return q * v; }
template<class T, int F, int I> static int32_t operator*(int32_t v, q<T,F,I> & q) { return q * v; }
template<class T, int F, int I> static int16_t operator*(int16_t v, q<T,F,I> & q) { return q * v; }
#if defined(FASTLED_ARM) | defined(FASTLED_RISCV) | defined(FASTLED_APOLLO3)
template<class T, int F, int I> static int operator*(int v, q<T,F,I> & q) { return q * v; }
#endif
/// A 4.4 integer (4 bits integer, 4 bits fraction)
typedef q<uint8_t, 4,4> q44;
/// A 6.2 integer (6 bits integer, 2 bits fraction)
typedef q<uint8_t, 6,2> q62;
/// A 8.8 integer (8 bits integer, 8 bits fraction)
typedef q<uint16_t, 8,8> q88;
/// A 12.4 integer (12 bits integer, 4 bits fraction)
typedef q<uint16_t, 12,4> q124;
/// @}
/// @} lib8tion (excluding the timekeeping functions from the nested group)
///////////////////////////////////////////////////////////////////////
///
/// @defgroup Timekeeping Timekeeping Functions
/// Tools for tracking and working with time
///
/// @{
#if ((defined(ARDUINO) || defined(SPARK) || defined(FASTLED_HAS_MILLIS)) && !defined(USE_GET_MILLISECOND_TIMER)) || defined(FASTLED_DOXYGEN)
// Forward declaration of Arduino function 'millis'.
//uint32_t millis();
/// The a number of functions need access to a millisecond counter
/// in order to keep time. On Arduino, this is "millis()".
/// On other platforms, you'll need to provide a function with this
/// signature which provides similar functionality:
/// @code{.cpp}
/// uint32_t get_millisecond_timer();
/// @endcode
///
/// You can also force use of the get_millisecond_timer() function
/// by \#defining `USE_GET_MILLISECOND_TIMER`.
#define GET_MILLIS millis
#else
uint32_t get_millisecond_timer();
#define GET_MILLIS get_millisecond_timer
#endif
/// @} Timekeeping
/// @addtogroup lib8tion
/// @{
///////////////////////////////////////////////////////////////////////
///
/// @defgroup BeatGenerators Waveform Beat Generators
/// Waveform generators that reset at a given number
/// of "beats per minute" (BPM).
///
/// The standard "beat" functions generate "sawtooth" waves which rise from
/// 0 up to a max value and then reset, continuously repeating that cycle at
/// the specified frequency (BPM).
///
/// The "sin" versions function similarly, but create an oscillating sine wave
/// at the specified frequency.
///
/// BPM can be supplied two ways. The simpler way of specifying BPM is as
/// a simple 8-bit integer from 1-255, (e.g., "120").
/// The more sophisticated way of specifying BPM allows for fractional
/// "Q8.8" fixed point number (an ::accum88) with an 8-bit integer part and
/// an 8-bit fractional part. The easiest way to construct this is to multiply
/// a floating point BPM value (e.g. 120.3) by 256, (e.g. resulting in 30796
/// in this case), and pass that as the 16-bit BPM argument.
///
/// Originally these functions were designed to make an entire animation project pulse.
/// with brightness. For that effect, add this line just above your existing call to
/// "FastLED.show()":
/// @code
/// uint8_t bright = beatsin8( 60 /*BPM*/, 192 /*dimmest*/, 255 /*brightest*/ ));
/// FastLED.setBrightness( bright );
/// FastLED.show();
/// @endcode
///