it7259 0.0.1

embedded-hal driver of IT7259 touch controller
Documentation
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
#![no_std]

use embedded_hal_1::{delay::DelayUs, digital::OutputPin, i2c::I2c};

pub const DEFAULT_ADDR: u8 = 0x46;

// 4.3. Buffer Type and Format
/// Command Buffer
const BUFFER_TYPE_COMMAND: u8 = 0b001_00000; // 0x20
/// Query Buffer, read only
const BUFFER_TYPE_QUERY: u8 = 0b100_00000; // 0x80
/// Command Response Buffer, read only. 1 byte
const BUFFER_TYPE_RESPONSE: u8 = 0b101_00000; // 0xa0
/// Point Information Buffer, read only
const BUFFER_TYPE_POINT_INFO: u8 = 0b111_00000; // 0xe0

mod commands {
    pub const DEVICE_NAME: u8 = 0x00;

    /// Get Cap Sensor Information
    pub const GET_SENSOR_INFO: u8 = 0x01;
    pub const SET_SENSOR_INFO: u8 = 0x02;

    pub const SET_POWER_MODE: u8 = 0x04;

    pub mod sensor_info {
        pub const FIRMWARE_INFOMATION: u8 = 0x00;
        pub const RESOLUTIONS_2D: u8 = 0x02;
        pub const FLASH_SIZE: u8 = 0x03;
        pub const INTERRUPT_NOTIFICATION_STATUS: u8 = 0x04; // rw
        pub const GESTURE_INFO: u8 = 0x05;
        pub const CONFIGURATION_VERSION: u8 = 0x06;
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct PointReport {
    /// Finger or Pen
    pub finger: bool,
    /// Palm detected
    pub palm: bool,
    /// x, y, pressure(0, 1, 2, 4, 8, f)
    pub point1: Option<(u16, u16, u8)>,
    pub point2: Option<(u16, u16, u8)>,
    pub point3: Option<(u16, u16, u8)>,
}

impl PointReport {
    fn from_bytes(buf: &[u8]) -> Option<Self> {
        if buf[0] >> 4 != 0 {
            return None;
        }

        let finger = buf[0] & 0b1000 != 0;
        let palm = buf[1] & 0b1 != 0;

        let point1 = if buf[0] & 0b001 != 0 {
            let x = u16::from(buf[2]) | ((u16::from(buf[3]) & 0b1111) << 8);
            let y = u16::from(buf[4]) | ((u16::from(buf[3]) & 0b1111_0000) << 4);
            let pressure = buf[5] & 0b1111;
            Some((x, y, pressure))
        } else {
            None
        };
        let point2 = if buf[0] & 0b010 != 0 {
            let x = u16::from(buf[6]) | ((u16::from(buf[7]) & 0b1111) << 8);
            let y = u16::from(buf[8]) | ((u16::from(buf[7]) & 0b1111_0000) << 4);
            let pressure = buf[9] & 0b1111;
            Some((x, y, pressure))
        } else {
            None
        };
        let point3 = if buf[0] & 0b100 != 0 {
            let x = u16::from(buf[10]) | ((u16::from(buf[11]) & 0b1111) << 8);
            let y = u16::from(buf[12]) | ((u16::from(buf[11]) & 0b1111_0000) << 4);
            let pressure = buf[13] & 0b1111;
            Some((x, y, pressure))
        } else {
            None
        };
        Some(Self {
            finger,
            palm,
            point1,
            point2,
            point3,
        })
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Gesture {
    /// Tap, GID: 0x20
    Tap {
        x: u16,
        y: u16,
    },
    /// Press, GID: 0x21
    Press {
        x: u16,
        y: u16,
    },
    /// Flick, GID: 0x22
    Flick {
        start_x: u16,
        start_y: u16,
        end_x: u16,
        end_y: u16,
        direction: u8,
    },
    /// Double-Tap, GID: 0x23
    DoubleTap {
        x: u16,
        y: u16,
    },
    /// Tap-and-Slide, GID: 0x24
    TapAndSlide {
        x: u16,
        y: u16,
    },
    /// Drag, GID: 0x25
    Drag {
        x: u16,
        y: u16,
    },
    /// Direction, GID: 0x26
    /// - 000b Up
    /// - 001b Upper right
    /// - 010b Right
    /// - 011b Lower right
    /// - 100b Down
    /// - 101b Lower left
    /// - 110b Left
    /// - 111b Upper left
    Direction {
        direction: u8,
    },
    /// Turn, GID: 0x27
    Turn {
        start_direction: u8,
        end_direction: u8,
    },
    /// Clockwise, GID: 0x28
    Clockwise {
        cw: bool,
        count: u8,
    },
    /// Dir_4Way, GID: 0x29
    Dir4Way {
        direction: u8,
    },

    // 2-finger gestures
    /// 2-finger Tap, GID: 0x40
    Tap2Finger {
        x0: u16,
        y0: u16,
        x1: u16,
        y1: u16,
    },
    /// 2-finger Double Tap, GID: 0x41
    DoubleTap2Finger {
        x0: u16,
        y0: u16,
        x1: u16,
        y1: u16,
    },

    /// 2D Gesture (Rotate + Scale + Translate), GID: 0x42
    Gesture2D {
        x_trans: u16,
        y_trans: u16,
        scale: u16,
        rorate: i16,
    },
    Unknown(u8),
}

impl Gesture {
    fn from_bytes(raw: &[u8]) -> Self {
        let gid = raw[0];
        match gid {
            0x20 => {
                let x = u16::from_le_bytes(raw[1..3].try_into().unwrap());
                let y = u16::from_le_bytes(raw[3..5].try_into().unwrap());
                Self::Tap { x, y }
            }
            0x21 => {
                let x = u16::from_le_bytes(raw[1..3].try_into().unwrap());
                let y = u16::from_le_bytes(raw[3..5].try_into().unwrap());
                Self::Press { x, y }
            }
            0x22 => {
                let start_x = u16::from_le_bytes(raw[1..3].try_into().unwrap());
                let start_y = u16::from_le_bytes(raw[3..5].try_into().unwrap());
                let end_x = u16::from_le_bytes(raw[5..7].try_into().unwrap());
                let end_y = u16::from_le_bytes(raw[7..9].try_into().unwrap());
                let direction = raw[9];
                Self::Flick {
                    start_x,
                    start_y,
                    end_x,
                    end_y,
                    direction,
                }
            }
            0x23 => {
                let x = u16::from_le_bytes(raw[1..3].try_into().unwrap());
                let y = u16::from_le_bytes(raw[3..5].try_into().unwrap());
                Self::DoubleTap { x, y }
            }
            0x24 => {
                let x = u16::from_le_bytes(raw[1..3].try_into().unwrap());
                let y = u16::from_le_bytes(raw[3..5].try_into().unwrap());
                Self::TapAndSlide { x, y }
            }
            0x25 => {
                let x = u16::from_le_bytes(raw[1..3].try_into().unwrap());
                let y = u16::from_le_bytes(raw[3..5].try_into().unwrap());
                Self::Drag { x, y }
            }
            0x26 => {
                let direction = raw[1] & 0b111;
                Self::Direction { direction }
            }
            0x27 => {
                let start_direction = raw[1] & 0b111;
                let end_direction = (raw[1] >> 4) & 0b111;
                Self::Turn {
                    start_direction,
                    end_direction,
                }
            }
            0x28 => {
                let cw = raw[1] >> 7 != 0;
                let count = (raw[1] >> 4) & 0b111;
                Self::Clockwise { cw, count }
            }
            0x29 => {
                let direction = raw[1] & 0b111;
                Self::Dir4Way { direction }
            }
            0x40 => {
                let x0 = u16::from_le_bytes(raw[1..3].try_into().unwrap());
                let y0 = u16::from_le_bytes(raw[3..5].try_into().unwrap());
                let x1 = u16::from_le_bytes(raw[5..7].try_into().unwrap());
                let y1 = u16::from_le_bytes(raw[7..9].try_into().unwrap());
                Self::Tap2Finger { x0, y0, x1, y1 }
            }
            _ => Self::Unknown(gid),
        }
    }
}

/// Format Tag (1000b)
#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct GestureReport {
    /// Gesture Identification (GID)
    pub gid: u8,
    pub info: [u8; 11],
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub struct TouchReport {
    pub tid: u8,
    pub touch_type: u8,
    pub info: [u8; 11],
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
pub enum Event {
    Point(PointReport),
    Gesture(Gesture),
    Touch(TouchReport),
    Wakeup,
}

impl Event {
    pub fn from_bytes(buf: &[u8]) -> Option<Self> {
        match buf[0] >> 4 {
            0b0000 => PointReport::from_bytes(buf).map(Self::Point),
            0b1000 => {
                // let gid = buf[0] & 0b1111;
                Some(Self::Gesture(Gesture::from_bytes(&buf[1..])))
            }
            0b0100 => Some(Self::Touch(TouchReport {
                tid: buf[0] & 0b1111,
                touch_type: buf[1],
                info: [
                    buf[2], buf[3], buf[4], buf[5], buf[6], buf[7], buf[8], buf[9], buf[10], buf[11], buf[12],
                ],
            })),
            0b1100 => Some(Self::Wakeup),
            _ => None,
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum InterruptTrigger {
    Low = 0x00,
    High = 0x01,
    FallingEdge = 0x10,
    RisingEdge = 0x11,
}

impl Default for InterruptTrigger {
    fn default() -> Self {
        Self::Low
    }
}

impl From<u8> for InterruptTrigger {
    fn from(v: u8) -> Self {
        match v {
            0x00 => Self::Low,
            0x01 => Self::High,
            0x10 => Self::FallingEdge,
            0x11 => Self::RisingEdge,
            _ => unreachable!(),
        }
    }
}

#[derive(Debug, Eq, PartialEq, Clone, Copy)]
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
#[repr(u8)]
pub enum IdleGesture {
    Point = 0x00,
    Tap = 0x20,
    DoubleTap = 0x23,
    Palm = 0x80,
}

pub struct IT7259<I2C> {
    i2c: I2C,
    addr: u8,
}

impl<I2C> IT7259<I2C>
where
    I2C: I2c,
{
    pub fn new(i2c: I2C, addr: u8) -> Self {
        Self { i2c, addr }
    }

    pub fn hard_reset<P: OutputPin, D: DelayUs>(&self, reset_n: &mut P, delay: &mut D) -> Result<(), P::Error> {
        reset_n.set_low()?;
        delay.delay_us(10);
        reset_n.set_high()?;
        delay.delay_ms(100);

        Ok(())
    }

    pub fn init(&mut self) -> Result<(), I2C::Error> {
        // 0x46 = 0b1000_110

        let mut buf = [0u8; 0xf];
        self.transfer_command(commands::DEVICE_NAME, &mut buf)?;
        let raw = &buf[1..buf[0] as usize];

        if &raw[0..3] != b"ITE" {
            todo!();
        }
        if &raw[3..6] != b"7259" {
            todo!();
        }
        Ok(())
    }

    pub fn poll_event(&mut self) -> Result<Option<Event>, I2C::Error> {
        let query = self.get_query_buffer()?;
        let point_info = query >> 6;
        if point_info & 0b10 != 0 {
            let mut buf = [0u8; 14];
            self.i2c
                .write_read(self.addr, &[BUFFER_TYPE_POINT_INFO], &mut buf[..])?;

            if buf[0] != 0 {
                return Ok(Event::from_bytes(&buf[..]));
            }
        }
        Ok(None)
    }

    pub fn get_interrupt(&mut self) -> Result<Option<InterruptTrigger>, I2C::Error> {
        let mut buf = [0u8; 2];
        self.transfer_raw_command(
            &[
                BUFFER_TYPE_COMMAND,
                commands::GET_SENSOR_INFO,
                commands::sensor_info::INTERRUPT_NOTIFICATION_STATUS,
            ],
            &mut buf,
        )?;
        #[cfg(feature = "defmt")]
        defmt::info!("get_interrupt: {:?}", buf);
        if buf == [0, 0] {
            Ok(None)
        } else {
            Ok(Some(buf[1].into()))
        }
    }

    pub fn set_interrupt(&mut self, trigger: Option<InterruptTrigger>) -> Result<bool, I2C::Error> {
        let mut buf = [0u8; 2];
        if let Some(trigger) = trigger {
            self.transfer_raw_command(
                &[
                    BUFFER_TYPE_COMMAND,
                    commands::SET_SENSOR_INFO,
                    commands::sensor_info::INTERRUPT_NOTIFICATION_STATUS,
                    0x01,
                    trigger as u8,
                ],
                &mut buf,
            )?;
        } else {
            self.transfer_raw_command(
                &[
                    BUFFER_TYPE_COMMAND,
                    commands::SET_SENSOR_INFO,
                    commands::sensor_info::INTERRUPT_NOTIFICATION_STATUS,
                    0x00,
                    0x00,
                ],
                &mut buf,
            )?;
        }
        Ok(buf == [0, 0])
    }

    #[inline]
    fn get_query_buffer(&mut self) -> Result<u8, I2C::Error> {
        let mut buf = [0u8; 1];
        self.i2c.write_read(self.addr, &[BUFFER_TYPE_QUERY], &mut buf[..1])?;
        Ok(buf[0])
    }

    // From active mode to idle mode,
    pub fn enter_idle(&mut self) -> Result<(), I2C::Error> {
        // no response
        self.i2c.write(
            self.addr,
            &[
                BUFFER_TYPE_COMMAND,
                commands::SET_POWER_MODE,
                0x00,
                0x01, // Idle mode
            ],
        )?;
        Ok(())
    }

    pub fn set_idle_gesture(&mut self, gesture: Option<IdleGesture>) -> Result<bool, I2C::Error> {
        // 5.3.17.5. Sub Command 0x87: Idle Gesture Setting
        const COMMAND: u8 = 0x15;
        const SUBCOMMAND_IDLE_GESTURE_SETTING: u8 = 0x87;

        let mut buf = [0u8; 2];
        if let Some(gesture) = gesture {
            self.transfer_raw_command(
                &[
                    BUFFER_TYPE_COMMAND,
                    COMMAND,
                    SUBCOMMAND_IDLE_GESTURE_SETTING,
                    0x00,
                    0x00,
                    gesture as u8,
                    0x80, // enable
                ],
                &mut buf,
            )?;
        } else {
            self.transfer_raw_command(
                &[
                    BUFFER_TYPE_COMMAND,
                    COMMAND,
                    SUBCOMMAND_IDLE_GESTURE_SETTING,
                    0x00,
                    0x00,
                    0x00,
                    0x00, // disable
                ],
                &mut buf,
            )?;
        }
        Ok(buf == [0, 0])
    }

    fn transfer_command(&mut self, command: u8, buf: &mut [u8]) -> Result<(), I2C::Error> {
        self.i2c.write(self.addr, &[BUFFER_TYPE_COMMAND, command])?;
        self.i2c.write_read(self.addr, &[BUFFER_TYPE_QUERY], &mut buf[..1])?;
        let query_data = buf[0];
        if query_data & 0b11 != 0 {
            todo!();
        }
        self.i2c.write_read(self.addr, &[BUFFER_TYPE_RESPONSE], buf)?;
        Ok(())
    }

    fn transfer_raw_command(&mut self, bytes: &[u8], buf: &mut [u8]) -> Result<(), I2C::Error> {
        self.i2c.write(self.addr, bytes)?;

        loop {
            self.i2c.write_read(self.addr, &[BUFFER_TYPE_QUERY], &mut buf[..1])?;
            let query_data = buf[0];
            if query_data & 0b11 == 0 {
                break;
            } else if query_data & 0b11 == 0b01 {
                #[cfg(feature = "defmt")]
                defmt::error!("busy");
            } else {
                #[cfg(feature = "defmt")]
                defmt::error!("error {}", query_data);
                todo!();
            }
        }

        self.i2c.write_read(self.addr, &[BUFFER_TYPE_RESPONSE], buf)?;
        Ok(())
    }
}