lib8: implement more fastled 8 bit math as lib8 submodules

This commit is contained in:
2024-11-16 12:28:01 +01:00
parent 7d8d59b508
commit b504ce298b
5 changed files with 153 additions and 6 deletions

32
src/lib8/trig.rs Normal file
View File

@@ -0,0 +1,32 @@
use num::PrimInt;
const b_m16_interleave: [u8; 8] = [0, 49, 49, 41, 90, 27, 117, 10];
pub fn sin8(theta: u8) -> u8 {
let mut offset: u8 = theta;
if theta & 0x40 != 0 {
offset = 255 - offset;
}
offset &= 0x3f;
let mut secoffset: u8 = offset & 0x0f;
if theta & 0x40 != 0 {
secoffset += 1;
}
let section: u8 = offset.unsigned_shr(4);
let s2: u8 = section * 2;
let b: u8 = b_m16_interleave[s2 as usize];
let m16: u8 = b_m16_interleave[s2 as usize + 1];
let mx: u8 = m16.wrapping_mul(secoffset).unsigned_shr(4);
let mut y: i8 = mx as i8 + b as i8;
if theta & 0x80 != 0 {
y = -y;
}
y = y.wrapping_add(128u8 as i8);
return y as u8;
}
pub fn cos8(theta: u8) -> u8 {
sin8(theta.wrapping_add(64))
}