Skip to content

Pen DFT data format

quo edited this page Feb 25, 2023 · 1 revision

Links

Overview

On SP7 and newer Surface devices, the touch device sends low-level pen data that requires additional processing to extract coordinates, pressure, etc. The data is contained in packets of various types, encoded inside HID reports. See surface-parser and iptsd (linked above) for details about the encoding and contents of the various packets (surface-parser has a --dft option for visualizing pen data).

The pens use something called Microsoft Pen Protocol (MPP). The way it works at the hardware level is:

  • The screen contains a grid of horizontal and vertical wires (antennas).
  • The pen contains a transmitter at the tip.
  • The position of the pen can be determined simply by seeing which antennas receive the strongest signals from the transmitter.
  • The pen can transmit at various frequencies and modulate these signals to send additional data (pressure, button presses, etc).
  • The pen contains an additional transmitter at the other end for eraser functionality.
  • Newer pens (>=MPP2?) contain a secondary tip transmitter, a few mm behind the first one. This provides a second position signal which can be used to determine pen tilt.

DFT window packets

The data we receive inside the HID packets consists primarily of signal strengths received at different antennas at various frequencies. These are encoded in "pen DFT window" packets with the following format:

struct pen_dft_window {
	u32 timestamp;
	u8 num_rows, seq_num, unknown0[3], data_type;
	u16 unknown1;
	struct pen_dft_window_row {
		u32 frequency, magnitude;
		i16 real[9], imag[9];
		i8 first, last, mid, zero;
	} x[num_rows], y[num_rows];
}

So essentially, the hardware calculates DFT bins for various frequencies for each antenna. It determines which antenna has the strongest signal, and sends the bins from a window of 9 antennas centered on the antenna with the strongest signal. This is done for both horizontal and vertical antennas, resulting in X rows and Y rows (one row for each frequency).

The first/last/mid values for each row are the indices of the first, last and middle antenna in the window (these are always related by first+4==mid==last-4).

The magnitude value is simply the squared amplitude of the middle antenna, i.e. real[4]*real[4]+imag[4]*imag[4].

The frequencies can differ between various Surface devices (possibly the hardware can change frequencies automatically depending on background noise/interference).

The pen encodes additional data by modulating signals in various ways:

  • Amplitude-shift keying (ASK): E.g. to encode a single bit, the signal is present for a 1, or absent for a 0.
  • Frequency-shift keying (FSK): Signal is present at one frequency for a 0, but at a different frequency for a 1.
  • Phase-shift keying (PSK): Data is encoded by changing the phase of the signal.
  • Frequency modulation (FM): The frequency of the signal is changed to represent a continuous value.

DFT row data

The table below contains information about the data in each of the DFT rows in the various different DFT window packets.

Transmitter A = primary tip and eraser, B = secondary tip.

Note: Data type 7, 8, 10, and all transmitter B signals are not sent by older pens (<=MPP1.51?) (the packets will still be present, but the data is just background noise).

Data type Row # Example frequency Transmitter Encoding Description
6 0 25000 A None Position signal from primary transmitter. Always on.
6 1 170454 B None/FSK? Position signal from secondary tip transmitter. Can be used to determine tilt.
6 2 179545 B FSK? Sent instead of row 1 when pen tip is not touching the screen? (Possibly indicates that transmitter B is about to shut down?)
6 3 24219 ? ? Possibly used to detect frequency drift of primary signal? (Since frequency is similar.)
6 4 25781 ? ? Possibly used to detect frequency drift of primary signal? (Since frequency is similar.)
6 5 180454 ? ? ?
6 6 189545 B ? ?
6 7 177200 B ? ?
7 0 161364 A ASK? ?
7 1 175000 B ASK? ?
7 2 205357 A FSK? Row 2/3 are related to whether the pen tip is touching the screen.
7 3 196428 A FSK?
7 4 178571 B FSK? Row 4/5 are similar to row 2/3, but from the second tip transmitter.
7 5 169642 B FSK?
7 6 160714 ? ? ?
7 7 165179 ? ? ?
7 8 174107 ? ? ?
7 9 178671 ? ? ?
8 0 165909 A ASK? Type 8 data looks similar/identical to type 7.
8 1 175000 B ASK?
8 2 205357 A FSK?
8 3 196428 A FSK?
8 4 178571 B FSK?
8 5 169642 B FSK?
8 6 160714 ? ?
8 7 165179 ? ?
8 8 174107 ? ?
8 9 178671 ? ?
9 0 25000 A ASK+PSK Button/eraser signal. If in-phase with position signal, side button is pressed. If 180 degrees out of phase, eraser is active.
9 1 21875 A ASK Start signal for unknown binary code.
9 2 40625 A FSK Row 2 and 3 encode a single bit for the code started by row 1.
9 3 43750 A FSK
10 0 205357 A FSK Each pair of rows in a type 10 packet encodes a bit using FSK.
10 1 196428 A FSK Two type 10 packets are sent in every group, with different data.
10 2 178571 B FSK The bit pattern repeats after 6 groups (12 packets).
10 3 169642 B FSK The meaning of the bits is unknown. Could be a unique ID?
10 4 205457 A FSK
10 5 196528 A FSK
10 6 178671 B FSK
10 7 169742 B FSK
10 8 205557 A FSK
10 9 196628 A FSK
10 10 178771 B FSK
10 11 169842 B FSK
10 12 205657 A FSK
10 13 196728 A FSK
10 14 178871 B FSK
10 15 169942 B FSK
11 0 18162 A FM Tip pressure (max)
11 1 19231 A FM Tip pressure
11 2 20299 A FM Tip pressure
11 3 21368 A FM Tip pressure
11 4 22436 A FM Tip pressure
11 5 23504 A FM Tip pressure (min)
11 6 37500 A ? ? Related to tip pressure ?
11 7 76500 B FSK Row 7 to 12 seem to encode 3 bits using FSK.
11 8 85400 B FSK
11 9 76600 B FSK
11 10 85500 B FSK
11 11 76700 B FSK
11 12 85600 B FSK
11 13 85700 B ASK Row 13/14/15 seem to encode 3 bits using ASK.
11 14 85800 B ASK
11 15 85900 B ASK
Clone this wiki locally