Rev 4 | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4 | Rev 12 | ||
---|---|---|---|
Line 6... | Line 6... | ||
6 | * Author: Mike |
6 | * Author: Mike |
7 | */ |
7 | */ |
8 | 8 | ||
9 | #include <math.h> |
9 | #include <math.h> |
10 | #include "libOLED/ap_math.h" |
10 | #include "libOLED/ap_math.h" |
- | 11 | namespace |
|
- | 12 | { |
|
- | 13 | static constexpr uint8_t sintab[ap_math::SINE_STEPS] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 10, 11, 12, 13, 14, 15, 16, 17, 18, 20, 21, 22, 23, 24, |
|
- | 14 | 25, 26, 27, 28, 30, 31, 32, 33, 34, 35, 36, 37, 38, 40, 41, 42, 43, 44, 45, 46, 47, |
|
- | 15 | 48, 49, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 63, 64, 65, 66, 67, 68, 69, 70, |
|
- | 16 | 71, 72, 73, 74, 75, 76, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, |
|
- | 17 | 93, 94, 95, 96, 97, 98, 100, 101, 102, 103, 104, 105, 106, 107, 108, 109, 110, 111, |
|
- | 18 | 112, 113, 114, 115, 116, 117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 127, 127, |
|
- | 19 | 128, 129, 130, 131, 132, 133, 134, 135, 136, 137, 138, 139, 140, 141, 142, 143, 144, |
|
- | 20 | 144, 145, 146, 147, 148, 149, 150, 151, 152, 153, 154, 154, 155, 156, 157, 158, 159, |
|
- | 21 | 160, 161, 161, 162, 163, 164, 165, 166, 167, 167, 168, 169, 170, 171, 172, 172, 173, |
|
- | 22 | 174, 175, 176, 177, 177, 178, 179, 180, 181, 181, 182, 183, 184, 184, 185, 186, 187, |
|
- | 23 | 187, 188, 189, 190, 190, 191, 192, 193, 193, 194, 195, 196, 196, 197, 198, 198, 199, |
|
- | 24 | 200, 201, 201, 202, 203, 203, 204, 205, 205, 206, 207, 207, 208, 209, 209, 210, 210, |
|
- | 25 | 211, 212, 212, 213, 214, 214, 215, 215, 216, 217, 217, 218, 218, 219, 220, 220, 221, |
|
- | 26 | 221, 222, 222, 223, 223, 224, 224, 225, 226, 226, 227, 227, 228, 228, 229, 229, 230, |
|
- | 27 | 230, 231, 231, 232, 232, 232, 233, 233, 234, 234, 235, 235, 236, 236, 236, 237, 237, |
|
- | 28 | 238, 238, 238, 239, 239, 240, 240, 240, 241, 241, 242, 242, 242, 243, 243, 243, 244, |
|
- | 29 | 244, 244, 245, 245, 245, 246, 246, 246, 246, 247, 247, 247, 248, 248, 248, 248, 249, |
|
- | 30 | 249, 249, 249, 250, 250, 250, 250, 251, 251, 251, 251, 251, 252, 252, 252, 252, 252, |
|
- | 31 | 253, 253, 253, 253, 253, 253, 253, 254, 254, 254, 254, 254, 254, 254, 254, 255, 255, |
|
- | 32 | 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255} ; |
|
- | 33 | } |
|
- | 34 | ||
- | 35 | ||
11 | /* this is an approximate maths library where all the answers are accurate enough */ |
36 | /* this is an approximate maths library where all the answers are accurate enough */ |
12 | ap_math::ap_math() |
37 | ap_math::ap_math() |
13 | { |
38 | { |
14 | uint16_t i; |
- | |
15 | for(i=0;i<SINE_STEPS;i++) |
- | |
16 | sintab[i]=sin(i/(SINE_STEPS*2.0)*3.14159)*AP_K; |
- | |
17 | } |
39 | } |
18 | 40 | ||
19 | /* returns 255 * sin(ang) where ang is in degrees */ |
- | |
20 | int |
- | |
21 | ap_math::ap_sin (int ang) |
41 | int ap_math::ap_sin(int ang) |
22 | { |
42 | { |
23 | /* wrap into range */ |
43 | /* wrap into range */ |
24 | while (ang < 0) |
44 | while (ang < 0) |
25 | { |
45 | { |
26 | ang += SINE_STEPS * 4; |
46 | ang += SINE_STEPS * 4; |
27 | } |
47 | } |
28 | while (ang >= SINE_STEPS * 4) |
48 | while (ang >= SINE_STEPS * 4) |
29 | { |
49 | { |
30 | ang -= SINE_STEPS * 4; |
50 | ang -= SINE_STEPS * 4; |
31 | } |
51 | } |
32 | 52 | ||
33 | if (ang >= 0 && ang < SINE_STEPS) |
53 | if (ang >= 0 && ang < SINE_STEPS) |
34 | { |
54 | { |
35 | return (sintab[ang]); |
55 | return (sintab[ang]); |
36 | } |
56 | } |
37 | else if (ang == SINE_STEPS) |
57 | else if (ang == SINE_STEPS) |
38 | { |
58 | { |
39 | return AP_K; |
59 | return AP_K; |
40 | } |
60 | } |
41 | else if (ang > SINE_STEPS && ang <= SINE_STEPS * 2) |
61 | else if (ang > SINE_STEPS && ang <= SINE_STEPS * 2) |
42 | { |
62 | { |
43 | return (sintab[SINE_STEPS * 2 - ang]); |
63 | return (sintab[SINE_STEPS * 2 - ang]); |
44 | } |
64 | } |
45 | else if (ang == 3 * SINE_STEPS) |
65 | else if (ang == 3 * SINE_STEPS) |
46 | { |
66 | { |
47 | return -AP_K; |
67 | return -AP_K; |
48 | } |
68 | } |
49 | if (ang > SINE_STEPS * 2 && ang < SINE_STEPS * 3) |
69 | if (ang > SINE_STEPS * 2 && ang < SINE_STEPS * 3) |
50 | { |
70 | { |
51 | return (-sintab[ang - SINE_STEPS * 2]); |
71 | return (-sintab[ang - SINE_STEPS * 2]); |
52 | } |
72 | } |
53 | else |
73 | else |
54 | /* ang > 270 and ang < 360 */ |
74 | /* ang > 270 and ang < 360 */ |
55 | { |
75 | { |
56 | return (-sintab[SINE_STEPS * 4 - ang]); |
76 | return (-sintab[SINE_STEPS * 4 - ang]); |
57 | } |
77 | } |
58 | } |
78 | } |
59 | 79 | ||
60 | /* returns 255 * cos(ang) where ang is in degrees */ |
80 | /* returns 255 * cos(ang) where ang is in degrees */ |
61 | int |
- | |
62 | ap_math::ap_cos (int ang) |
81 | int ap_math::ap_cos(int ang) |
63 | { |
82 | { |
64 | return ap_sin (SINE_STEPS - ang); |
83 | return ap_sin(SINE_STEPS - ang); |
65 | } |
84 | } |
66 | - |