Go to most recent revision | Details | Last modification | View Log | RSS feed
| Rev | Author | Line No. | Line |
|---|---|---|---|
| 4 | mjames | 1 | /* |
| 2 | * ap_math.c |
||
| 3 | |||
| 4 | * |
||
| 5 | * Created on: 31 Jan 2016 |
||
| 6 | * Author: Mike |
||
| 7 | */ |
||
| 8 | |||
| 9 | #include <math.h> |
||
| 10 | #include "libOLED/ap_math.h" |
||
| 11 | /* this is an approximate maths library where all the answers are accurate enough */ |
||
| 12 | ap_math::ap_math() |
||
| 13 | { |
||
| 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 | } |
||
| 18 | |||
| 19 | /* returns 255 * sin(ang) where ang is in degrees */ |
||
| 20 | int |
||
| 21 | ap_math::ap_sin (int ang) |
||
| 22 | { |
||
| 23 | /* wrap into range */ |
||
| 24 | while (ang < 0) |
||
| 25 | { |
||
| 26 | ang += SINE_STEPS * 4; |
||
| 27 | } |
||
| 28 | while (ang >= SINE_STEPS * 4) |
||
| 29 | { |
||
| 30 | ang -= SINE_STEPS * 4; |
||
| 31 | } |
||
| 32 | |||
| 33 | if (ang >= 0 && ang < SINE_STEPS) |
||
| 34 | { |
||
| 35 | return (sintab[ang]); |
||
| 36 | } |
||
| 37 | else if (ang == SINE_STEPS) |
||
| 38 | { |
||
| 39 | return AP_K; |
||
| 40 | } |
||
| 41 | else if (ang > SINE_STEPS && ang <= SINE_STEPS * 2) |
||
| 42 | { |
||
| 43 | return (sintab[SINE_STEPS * 2 - ang]); |
||
| 44 | } |
||
| 45 | else if (ang == 3 * SINE_STEPS) |
||
| 46 | { |
||
| 47 | return -AP_K; |
||
| 48 | } |
||
| 49 | if (ang > SINE_STEPS * 2 && ang < SINE_STEPS * 3) |
||
| 50 | { |
||
| 51 | return (-sintab[ang - SINE_STEPS * 2]); |
||
| 52 | } |
||
| 53 | else |
||
| 54 | /* ang > 270 and ang < 360 */ |
||
| 55 | { |
||
| 56 | return (-sintab[SINE_STEPS * 4 - ang]); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /* returns 255 * cos(ang) where ang is in degrees */ |
||
| 61 | int |
||
| 62 | ap_math::ap_cos (int ang) |
||
| 63 | { |
||
| 64 | return ap_sin (SINE_STEPS - ang); |
||
| 65 | } |
||
| 66 |