-
Notifications
You must be signed in to change notification settings - Fork 10
/
touchpad_i2c.rs
109 lines (99 loc) · 2.85 KB
/
touchpad_i2c.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
use std::fmt::Debug;
use std::io::ErrorKind::{NotFound, PermissionDenied};
use anyhow::{Context, Error, Result};
use i2cdev::core::I2CDevice;
use i2cdev::linux::{LinuxI2CDevice, LinuxI2CError};
#[derive(Debug, Clone, Copy)]
pub enum Brightness {
Zero = 0,
Low = 31,
Half = 24,
Full = 1,
}
impl Default for Brightness {
fn default() -> Self {
Brightness::Full
}
}
impl std::fmt::Display for Brightness {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use Brightness::*;
let level = match self {
Zero => "Zero",
Low => "Low",
Half => "Half",
Full => "Full",
};
f.write_str(level)
}
}
impl Brightness {
fn next(&self) -> Self {
use Brightness::*;
match self {
Zero => Default::default(), // Jump to default
Low => Half,
Half => Full,
Full => Low,
}
}
pub fn cycle(&mut self) -> Self {
*self = self.next();
*self
}
}
pub struct TouchpadI2C {
dev: LinuxI2CDevice,
i2c_id: u32,
}
impl TouchpadI2C {
pub fn new(i2c_id: u32) -> Result<Self> {
const TOUCHPAD_ADDR: u16 = 0x15;
let dev = unsafe {
LinuxI2CDevice::force_new(format!("/dev/i2c-{}", i2c_id), TOUCHPAD_ADDR).map_err(
|err| {
let mut context = format!("Unable to open Touchpad I2C at /dev/i2c-{}", i2c_id);
let extra_context = match &err {
LinuxI2CError::Io(e) => match e.kind() {
NotFound => "Is i2c-dev kernel module loaded?",
PermissionDenied => "Do you have the permission to read /dev/i2c-*?",
_ => "",
},
LinuxI2CError::Nix(_) => "",
};
if !extra_context.is_empty() {
context.push_str(". ");
context.push_str(extra_context);
};
Error::new(err).context(context)
},
)?
};
Ok(Self { dev, i2c_id })
}
pub fn set_brightness(&mut self, brightness: Brightness) -> Result<()> {
let msg = [
0x05,
0x00,
0x3d,
0x03,
0x06,
0x00,
0x07,
0x00,
0x0d,
0x14,
0x03,
brightness as u8,
0xad,
];
self.dev
.write(&msg)
.with_context(|| format!("Could not set touchpad brightness to {}", brightness))
}
}
impl Debug for TouchpadI2C {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&format!("TouchpadI2C: /dev/i2c-{}", self.i2c_id))
}
}