use embassy_ssd1306_graphics::Graphics;
use embedded_hal_async::i2c::I2c;
use crate::draw_utils::{segment, filled_disk};
#[derive(Clone, Copy, Debug)]
pub struct Pendulum {
pub pivot_x: i32,
pub pivot_y: i32,
pub length: i32,
}
impl Pendulum {
#[inline]
pub fn new(pivot_x: i32, pivot_y: i32, length: i32) -> Self {
Self { pivot_x, pivot_y, length }
}
pub fn draw<I: I2c>(
&self,
gfx: &mut Graphics<'_, I>,
angle: f32,
on: bool,
cos_fn: fn(f32) -> f32,
sin_fn: fn(f32) -> f32,
) {
let mx = self.pivot_x + ((self.length as f32) * sin_fn(angle) + 0.5) as i32;
let my = self.pivot_y + ((self.length as f32) * cos_fn(angle) + 0.5) as i32;
segment(gfx, self.pivot_x, self.pivot_y, mx, my, on);
gfx.pixel(self.pivot_x, self.pivot_y, on);
gfx.pixel(self.pivot_x - 1, self.pivot_y, on);
gfx.pixel(self.pivot_x + 1, self.pivot_y, on);
gfx.pixel(self.pivot_x - 2, self.pivot_y, on);
gfx.pixel(self.pivot_x + 2, self.pivot_y, on);
gfx.pixel(self.pivot_x, self.pivot_y - 1, on);
gfx.pixel(self.pivot_x, self.pivot_y + 1, on);
filled_disk(gfx, mx, my, 3, on);
}
#[inline]
pub fn erase<I: I2c>(
&self,
gfx: &mut Graphics<'_, I>,
angle: f32,
cos_fn: fn(f32) -> f32,
sin_fn: fn(f32) -> f32,
) {
self.draw(gfx, angle, false, cos_fn, sin_fn);
}
}