2022-09-08 21:39:08 -07:00
|
|
|
use std::io::Write;
|
2022-04-14 14:55:45 -07:00
|
|
|
|
2022-08-31 19:20:49 -07:00
|
|
|
use crate::protocol::{Decode, Encode};
|
2022-04-14 14:55:45 -07:00
|
|
|
|
|
|
|
/// Represents an angle in steps of 1/256 of a full turn.
|
|
|
|
#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
|
|
|
|
pub struct ByteAngle(pub u8);
|
|
|
|
|
|
|
|
impl ByteAngle {
|
2022-05-16 02:36:14 -07:00
|
|
|
pub fn from_degrees(f: f32) -> ByteAngle {
|
|
|
|
ByteAngle((f.rem_euclid(360.0) / 360.0 * 256.0).round() as u8)
|
2022-04-14 14:55:45 -07:00
|
|
|
}
|
|
|
|
|
2022-07-11 05:08:02 -07:00
|
|
|
pub fn to_degrees(self) -> f32 {
|
|
|
|
self.0 as f32 / 256.0 * 360.0
|
|
|
|
}
|
2022-04-14 14:55:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Encode for ByteAngle {
|
|
|
|
fn encode(&self, w: &mut impl Write) -> anyhow::Result<()> {
|
|
|
|
self.0.encode(w)
|
|
|
|
}
|
2022-10-19 01:52:02 -07:00
|
|
|
|
|
|
|
fn encoded_len(&self) -> usize {
|
|
|
|
self.0.encoded_len()
|
|
|
|
}
|
2022-04-14 14:55:45 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Decode for ByteAngle {
|
2022-09-08 21:39:08 -07:00
|
|
|
fn decode(r: &mut &[u8]) -> anyhow::Result<Self> {
|
2022-07-11 05:08:02 -07:00
|
|
|
u8::decode(r).map(ByteAngle)
|
2022-04-14 14:55:45 -07:00
|
|
|
}
|
|
|
|
}
|