Subversion Repositories libOLED

Rev

Rev 4 | Blame | Compare with Previous | Last modification | View Log | Download | RSS feed

  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. 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.  
  36. /* this is an approximate maths library where all the answers are accurate enough */
  37. ap_math::ap_math()
  38. {
  39. }
  40.  
  41. int ap_math::ap_sin(int ang)
  42. {
  43.   /* wrap into range */
  44.   while (ang < 0)
  45.   {
  46.     ang += SINE_STEPS * 4;
  47.   }
  48.   while (ang >= SINE_STEPS * 4)
  49.   {
  50.     ang -= SINE_STEPS * 4;
  51.   }
  52.  
  53.   if (ang >= 0 && ang < SINE_STEPS)
  54.   {
  55.     return (sintab[ang]);
  56.   }
  57.   else if (ang == SINE_STEPS)
  58.   {
  59.     return AP_K;
  60.   }
  61.   else if (ang > SINE_STEPS && ang <= SINE_STEPS * 2)
  62.   {
  63.     return (sintab[SINE_STEPS * 2 - ang]);
  64.   }
  65.   else if (ang == 3 * SINE_STEPS)
  66.   {
  67.     return -AP_K;
  68.   }
  69.   if (ang > SINE_STEPS * 2 && ang < SINE_STEPS * 3)
  70.   {
  71.     return (-sintab[ang - SINE_STEPS * 2]);
  72.   }
  73.   else
  74.   /* ang > 270 and ang < 360 */
  75.   {
  76.     return (-sintab[SINE_STEPS * 4 - ang]);
  77.   }
  78. }
  79.  
  80. /* returns 255 * cos(ang) where ang is in degrees */
  81. int ap_math::ap_cos(int ang)
  82. {
  83.   return ap_sin(SINE_STEPS - ang);
  84. }
  85.