]> git.donarmstrong.com Git - qmk_firmware.git/blobdiff - lib/lib8tion/trig8.h
Remove more commented out MCUs
[qmk_firmware.git] / lib / lib8tion / trig8.h
index 4907c6ff30a794f6e0ed7e2f57f7e9f06c383c98..cfba6373fb94748743a16188c55b1c8df25d4529 100644 (file)
@@ -147,7 +147,7 @@ LIB8STATIC int16_t cos16( uint16_t theta)
 #endif
 
 
-const uint8_t b_m16_interleave[] = { 0, 49, 49, 41, 90, 27, 117, 10 };
+static const uint8_t b_m16_interleave[8] = { 0, 49, 49, 41, 90, 27, 117, 10 };
 
 /// Fast 8-bit approximation of sin(x). This approximation never varies more than
 /// 2% from the floating point value you'd get by doing
@@ -255,5 +255,30 @@ LIB8STATIC uint8_t cos8( uint8_t theta)
     return sin8( theta + 64);
 }
 
+/// Fast 16-bit approximation of atan2(x).
+/// @returns atan2, value between 0 and 255
+LIB8STATIC uint8_t atan2_8(int16_t dy, int16_t dx)
+{
+    if (dy == 0)
+    {
+        if (dx >= 0)
+            return 0;
+        else
+            return 128;
+    }
+
+    int16_t abs_y = dy > 0 ? dy : -dy;
+    int8_t a;
+
+    if (dx >= 0)
+        a = 32 - (32 * (dx - abs_y) / (dx + abs_y));
+    else
+        a = 96 - (32 * (dx + abs_y) / (abs_y - dx));
+
+    if (dy < 0)
+        return -a;     // negate if in quad III or IV
+    return a;
+}
+
 ///@}
 #endif