FastLED 3.9.15
Loading...
Searching...
No Matches
sin32.h
Go to the documentation of this file.
1#pragma once
2
3
4#include <stdint.h>
5
6#include "namespace.h"
7#include "force_inline.h"
8
9namespace fl {
10
11// fast and accurate sin and cos approximations
12// they differ only 0.01 % from actual sinf and cosf funtions
13// sin32 and cos32 use 24 bit unsigned integer arguments
14// 0 to 16777216 is one cycle
15// they output an integer between -2147418112 and 2147418112
16// that's 32767 * 65536
17// this is because I use int16_t look up table to interpolate the results
18//
19// sin16 and cos16 are faster and more accurate funtions that take uint16_t as arguments
20// and return int16_t as output
21// they can replace the older implementation and are 62 times more accurate and twice as fast
22// the downside with these funtions is that they use 640 bytes for the look up table
23// thats a lot for old microcontrollers but nothing for modern ones
24//
25// sin32 and cos32 take about 13 cyces to execute on an esp32
26// they're 32 times faster than sinf and cosf
27// you can use choose to use these new by writing
28// #define USE_SIN_32 before #include "FastLED.h"
29
30extern const int16_t *sinArray;
31
32extern const int16_t *cosArray;
33
34// 0 to 16777216 is a full circle
35// output is between -2147418112 and 2147418112
36FASTLED_FORCE_INLINE static int32_t sin32(uint32_t angle)
37{
38 uint8_t angle256 = angle / 65536;
39 int32_t subAngle = angle % 65536;
40 return sinArray[angle256] * (65536 - subAngle) + sinArray[angle256 + 1] * subAngle;
41}
42
43// 0 to 16777216 is a full circle
44// output is between -2147418112 and 2147418112
45FASTLED_FORCE_INLINE static int32_t cos32(uint32_t angle)
46{
47 uint8_t angle256 = angle / 65536;
48 int32_t subAngle = angle % 65536;
49 return cosArray[angle256] * (65536 - subAngle) + cosArray[angle256 + 1] * subAngle;
50}
51
52// 0 to 65536 is a full circle
53// output is between -32767 and 32767
54FASTLED_FORCE_INLINE static int16_t sin16lut(uint16_t angle)
55{
56 uint8_t angle256 = angle / 256;
57 int32_t subAngle = angle % 256;
58 return (sinArray[angle256] * (256 - subAngle) + sinArray[angle256 + 1] * subAngle) / 256;
59}
60
61// 0 to 65536 is a full circle
62// output is between -32767 and 32767
63FASTLED_FORCE_INLINE static int16_t cos16lut(uint16_t angle)
64{
65 uint8_t angle256 = angle / 256;
66 int32_t subAngle = angle % 256;
67 return (cosArray[angle256] * (256 - subAngle) + cosArray[angle256 + 1] * subAngle) / 256;
68}
69
70} // namespace fl
#define FASTLED_FORCE_INLINE
Definition force_inline.h:7
Implements the FastLED namespace macros.
static FASTLED_FORCE_INLINE int32_t cos32(uint32_t angle)
Definition sin32.h:45
static FASTLED_FORCE_INLINE int16_t sin16lut(uint16_t angle)
Definition sin32.h:54
static FASTLED_FORCE_INLINE int32_t sin32(uint32_t angle)
Definition sin32.h:36
static FASTLED_FORCE_INLINE int16_t cos16lut(uint16_t angle)
Definition sin32.h:63
const int16_t * sinArray
Definition sin32.cpp:31
const int16_t * cosArray
Definition sin32.cpp:33
Implements a simple red square effect for 2D LED grids.
Definition crgb.h:16