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