Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop atomic crate #127

Merged
merged 4 commits into from
Jul 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ edition = "2021"
[dependencies]
log = "0.4.14"
once_cell = "1.9.0"
atomic = "0.5.1"
cgmath = "0.18.0"
libc = "0.2.69"


# framebuffer
memmap2 = { version = "0.5.2", optional = true }
ioctl-gen = { version = "0.1.1", optional = true }
Expand Down
72 changes: 58 additions & 14 deletions examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,14 @@ use libremarkable::{end_bench, start_bench};
#[cfg(feature = "enable-runtime-benchmarking")]
use libremarkable::stopwatch;

use atomic::Atomic;
use chrono::{DateTime, Local};
use log::info;
use once_cell::sync::Lazy;

use std::collections::VecDeque;
use std::fmt;
use std::process::Command;
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::atomic::{AtomicBool, AtomicI32, AtomicU8, Ordering};
use std::sync::Mutex;
use std::thread::sleep;
use std::time::Duration;
Expand Down Expand Up @@ -55,6 +54,24 @@ impl DrawMode {
}
}

impl From<DrawMode> for i32 {
fn from(mode: DrawMode) -> Self {
match mode {
DrawMode::Draw(s) => s as i32,
DrawMode::Erase(s) => -(s as i32),
}
}
}
impl From<i32> for DrawMode {
fn from(mode: i32) -> Self {
if mode >= 0 {
Self::Draw(mode as u32)
} else {
Self::Erase((-mode) as u32)
}
}
}

#[derive(Copy, Clone, PartialEq, Debug)]
enum TouchMode {
OnlyUI,
Expand Down Expand Up @@ -88,6 +105,30 @@ impl fmt::Display for TouchMode {
}
}

impl From<TouchMode> for u8 {
fn from(mode: TouchMode) -> Self {
match mode {
TouchMode::OnlyUI => 1,
TouchMode::Bezier => 2,
TouchMode::Circles => 3,
TouchMode::Diamonds => 4,
TouchMode::FillDiamonds => 5,
}
}
}
impl From<u8> for TouchMode {
fn from(mode: u8) -> Self {
match mode {
1 => Self::OnlyUI,
2 => Self::Bezier,
3 => Self::Circles,
4 => Self::Diamonds,
5 => Self::FillDiamonds,
_ => panic!("Unmapped mode value: {mode}"),
}
}
}

// This region will have the following size at rest:
// raw: 5896 kB
// zstd: 10 kB
Expand All @@ -100,8 +141,8 @@ const CANVAS_REGION: mxcfb_rect = mxcfb_rect {

type PointAndPressure = (cgmath::Point2<f32>, i32);

static G_TOUCH_MODE: Lazy<Atomic<TouchMode>> = Lazy::new(|| Atomic::new(TouchMode::OnlyUI));
static G_DRAW_MODE: Lazy<Atomic<DrawMode>> = Lazy::new(|| Atomic::new(DrawMode::Draw(2)));
static G_TOUCH_MODE: Lazy<AtomicU8> = Lazy::new(|| AtomicU8::new(TouchMode::OnlyUI.into()));
static G_DRAW_MODE: Lazy<AtomicI32> = Lazy::new(|| AtomicI32::new(DrawMode::Draw(2).into()));
static UNPRESS_OBSERVED: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
static WACOM_IN_RANGE: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
static WACOM_RUBBER_SIDE: Lazy<AtomicBool> = Lazy::new(|| AtomicBool::new(false));
Expand Down Expand Up @@ -310,11 +351,11 @@ fn on_touch_rustlogo(app: &mut appctx::ApplicationContext<'_>, _element: UIEleme
}

fn on_toggle_eraser(app: &mut appctx::ApplicationContext<'_>, _: UIElementHandle) {
let (new_mode, name) = match G_DRAW_MODE.load(Ordering::Relaxed) {
let (new_mode, name) = match G_DRAW_MODE.load(Ordering::Relaxed).into() {
DrawMode::Erase(s) => (DrawMode::Draw(s), "Black".to_owned()),
DrawMode::Draw(s) => (DrawMode::Erase(s), "White".to_owned()),
};
G_DRAW_MODE.store(new_mode, Ordering::Relaxed);
G_DRAW_MODE.store(new_mode.into(), Ordering::Relaxed);

let indicator = app.get_element_by_name("colorIndicator");
if let UIElement::Text { ref mut text, .. } = indicator.unwrap().write().inner {
Expand All @@ -324,8 +365,8 @@ fn on_toggle_eraser(app: &mut appctx::ApplicationContext<'_>, _: UIElementHandle
}

fn on_change_touchdraw_mode(app: &mut appctx::ApplicationContext<'_>, _: UIElementHandle) {
let new_val = G_TOUCH_MODE.load(Ordering::Relaxed).toggle();
G_TOUCH_MODE.store(new_val, Ordering::Relaxed);
let new_val = TouchMode::from(G_TOUCH_MODE.load(Ordering::Relaxed)).toggle();
G_TOUCH_MODE.store(new_val.into(), Ordering::Relaxed);

let indicator = app.get_element_by_name("touchModeIndicator");
if let UIElement::Text { ref mut text, .. } = indicator.unwrap().write().inner {
Expand Down Expand Up @@ -399,15 +440,15 @@ fn draw_color_test_rgb(app: &mut appctx::ApplicationContext<'_>, _element: UIEle
}

fn change_brush_width(app: &mut appctx::ApplicationContext<'_>, delta: i32) {
let current = G_DRAW_MODE.load(Ordering::Relaxed);
let current = DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed));
let current_size = current.get_size() as i32;
let proposed_size = current_size + delta;
let new_size = proposed_size.clamp(1, 99);
if new_size == current_size {
return;
}

G_DRAW_MODE.store(current.set_size(new_size as u32), Ordering::Relaxed);
G_DRAW_MODE.store(current.set_size(new_size as u32).into(), Ordering::Relaxed);

let element = app.get_element_by_name("displaySize").unwrap();
if let UIElement::Text { ref mut text, .. } = element.write().inner {
Expand Down Expand Up @@ -471,7 +512,7 @@ fn on_wacom_input(app: &mut appctx::ApplicationContext<'_>, input: input::WacomE
return;
}

let (mut col, mut mult) = match G_DRAW_MODE.load(Ordering::Relaxed) {
let (mut col, mut mult) = match G_DRAW_MODE.load(Ordering::Relaxed).into() {
DrawMode::Draw(s) => (color::BLACK, s),
DrawMode::Erase(s) => (color::WHITE, s * 3),
};
Expand Down Expand Up @@ -568,7 +609,7 @@ fn on_touch_handler(app: &mut appctx::ApplicationContext<'_>, input: input::Mult
if !CANVAS_REGION.contains_point(&finger.pos.cast().unwrap()) {
return;
}
let rect = match G_TOUCH_MODE.load(Ordering::Relaxed) {
let rect = match TouchMode::from(G_TOUCH_MODE.load(Ordering::Relaxed)) {
TouchMode::Bezier => {
let position_float = finger.pos.cast().unwrap();
let points = [
Expand Down Expand Up @@ -877,7 +918,7 @@ fn main() {
onclick: None,
inner: UIElement::Text {
foreground: color::BLACK,
text: G_DRAW_MODE.load(Ordering::Relaxed).color_as_string(),
text: DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)).color_as_string(),
scale: 40.0,
border_px: 0,
},
Expand Down Expand Up @@ -927,7 +968,10 @@ fn main() {
refresh: UIConstraintRefresh::Refresh,
inner: UIElement::Text {
foreground: color::BLACK,
text: format!("size: {0}", G_DRAW_MODE.load(Ordering::Relaxed).get_size()),
text: format!(
"size: {0}",
DrawMode::from(G_DRAW_MODE.load(Ordering::Relaxed)).get_size()
),
scale: 45.0,
border_px: 0,
},
Expand Down
7 changes: 3 additions & 4 deletions src/input/wacom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,10 @@ use crate::device::rotate::CoordinatePart;
use crate::device::CURRENT_DEVICE;
use crate::input::scan::SCANNED;
use crate::input::{InputDeviceState, InputEvent, WacomEvent, WacomPen};
use atomic::Atomic;
use evdev::InputEvent as EvInputEvent;
use log::debug;
use once_cell::sync::Lazy;
use std::sync::atomic::{AtomicU16, Ordering};
use std::sync::atomic::{AtomicBool, AtomicU16, Ordering};

use crate::cgmath;
use crate::dimensions::{DISPLAYHEIGHT, DISPLAYWIDTH, WACOMHEIGHT, WACOMWIDTH};
Expand All @@ -22,7 +21,7 @@ pub struct WacomState {
last_ytilt: AtomicU16,
last_dist: AtomicU16,
last_pressure: AtomicU16,
last_touch_state: Atomic<bool>,
last_touch_state: AtomicBool,
}

impl ::std::default::Default for WacomState {
Expand All @@ -34,7 +33,7 @@ impl ::std::default::Default for WacomState {
last_ytilt: AtomicU16::new(0),
last_dist: AtomicU16::new(0),
last_pressure: AtomicU16::new(0),
last_touch_state: Atomic::new(false),
last_touch_state: AtomicBool::new(false),
}
}
}
Expand Down
Loading