summaryrefslogtreecommitdiff
path: root/doc/devel/PenButtons.txt
blob: ee35c2129d29004312d12cf957ba81ff1c3b9211 (plain)
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
----------------------------------------------
xf86-input-tc1kpen Pen Button events resolving
----------------------------------------------

As the implemented algorithm for button 3 emulaton
is quite complex (in meaning of compression of
semantics), I shall describe its derivation, here.
The HW mode is quite straightforward and doesn't
require closer explanation as well as the 3-button
extension to the following algorithm, I guess.



Emulating button 3 with pen tap while pen button pressed
--------------------------------------------------------

From now on, I shall use term button for both the pen tip
and pen button (they are both buttons in terms of functionality).


First, what happens upon a button press/release:

1/ If a button is pressed, 2 packets are sent to the serial
   device:

   i)  STATUS bit set,   BUTTON bit clear
   ii) STATUS bit clear, BUTTON bit set

2/ From that moment, the BUTTON bit stays set until the
   button is released. Then, 2 packets are sent. The first
   of those may differ regarding current state of the other
   button:

   i)  STATUS bit set,   BUTTON bit clear if the other btn is released
                         BUTTON bit set   if the other btn is pressed
   ii) STATUS bit clear, BUTTON bit clear

The following image describes that:

                     PRESS                          RELEASE
                       V                              V
   PACKET #:           i     i+1               j-1    j     j+1
   -------------------------------------~----------------------------------

             1      +------+                       +------+
                    |      |                       |      |
   STAT bit: 0 -----+      +---------->   ---+-----+      +------+------>

   -------------------------------------~----------------------------------

   Other btn. not pressed:

             1             +------+--->   ---+-----+
                           |                       |
   BTN bit:  0 -----+------+                       +------+------+------>

   -------------------------------------~----------------------------------

   Other btn. pressed:


             1             +------+--->   ---+-----+------+
                           |                              |
   BTN bit:  0 -----+------+                              +------+------>

   -------------------------------------~----------------------------------



Now, assuming that we watch the STATUS bit changes,
this leads to the following algorithm:

                               +-------------------+
                               | STATUS bit change |
                               +---------+---------+
                                         |
                                         |
                                         V
                          TRUE +-------------------+ FALSE
                    +----------+  STATUS bit set   +-------+
                    |          +-------------------+       |
                    |                                      |
                    |                                      |
                    V                                      V
      FALSE +---------------+                 TRUE +---------------+ FALSE
  X<--------+  button > 0   |            +---------+  button > 0   +----+
            +-------+-------+            |         +---------------+    |
                    | TRUE               |                              |
                    |                    |                              |
                    V                    V                              V
      TRUE +----------------+   +----------------+ T     F +----------------+
  X<-------+ btn 1 bit set  |   | btn 1 bit set  +--->X<---+ btn 1 bit set  |
           +--------+-------+   +--------+-------+         +--------+-------+
                    | FALSE              | FALSE                    | TRUE
                    |                    |                          |
                    +--------------------+                          V
                    |                                 TRUE +----------------+
                    |                          +-----------+ btn 2 bit set  |
                    V                          |           +--------+-------+
         +--------------------+                |                    | FALSE
         | Release(button);   |                V                    V
         | button = 0;        |       +-------------+      +----------------+
         +----------+---------+       | button = 3; |      |  button = 1;   |
                    |                 +--------+----+      +--------+-------+
                    |                          |                    |
                    V                          +--------------------+
                    X                          |
                                               V
                                      +-----------------+
                                      | Press(button);  |
                                      +--------+--------+
                                               |
                                               V
                                               X

  ('X' points mean exit).

The algorithm is obviously far from being efficient.
Minimalization follows:

The original algorithm in table form:
=============+==============+=============+=============+====================
 STATUS bit  |  button > 0  |  btn 1 bit  |  btn 2 bit  |  result
=============+==============+=============+=============+====================
      0      |    FALSE     |      0      |      0      |
-------------+--------------+-------------+-------------+
      0      |    FALSE     |      0      |      1      |
-------------+--------------+-------------+-------------+--------------------
      0      |    FALSE     |      1      |      0      |  Press(1)
-------------+--------------+-------------+-------------+--------------------
      0      |    FALSE     |      1      |      1      |  Press(3)
=============+==============+=============+=============+====================
      0      |    TRUE      |      0      |      0      |
-------------+--------------+-------------+-------------+  Release(button);
      0      |    TRUE      |      0      |      1      |
-------------+--------------+-------------+-------------+--------------------
      0      |    TRUE      |      1      |      0      |
-------------+--------------+-------------+-------------+
      0      |    TRUE      |      1      |      1      |
=============+==============+=============+=============+====================
      1      |    FALSE     |      0      |      0      |
-------------+--------------+-------------+-------------+
      1      |    FALSE     |      0      |      1      |
-------------+--------------+-------------+-------------+
      1      |    FALSE     |      1      |      0      |
-------------+--------------+-------------+-------------+
      1      |    FALSE     |      1      |      1      |
=============+==============+=============+=============+====================
      1      |    TRUE      |      0      |      0      |
-------------+--------------+-------------+-------------+  Release(button);
      1      |    TRUE      |      0      |      1      |
-------------+--------------+-------------+-------------+--------------------
      1      |    TRUE      |      1      |      0      |
-------------+--------------+-------------+-------------+
      1      |    TRUE      |      1      |      1      |
=============+==============+=============+=============+====================



Minimized table:

=============+==============+=============+=============+====================
 STATUS bit  |  button > 0  |  btn 1 bit  |  btn 2 bit  |  result
=============+==============+=============+=============+====================
      0      |    FALSE     |      1      |      0      |  Press(1);
-------------+--------------+-------------+-------------+--------------------
      0      |    FALSE     |      1      |      1      |  Press(3);
=============+==============+=============+=============+====================
      ?      |    TRUE      |      0      |      ?      |  Release(button);
=============+==============+=============+=============+====================
      ?      |    TRUE      |      1      |      ?      |
-------------+--------------+-------------+-------------+
      1      |    FALSE     |      ?      |      ?      |
-------------+--------------+-------------+-------------+
      0      |    FALSE     |      0      |      ?      |
=============+==============+=============+=============+====================


The minimized table may be effectively represented
by the following algorithm:


                         +-------------------+
                         | STATUS bit change |
                         +---------+---------+
                                   |
                                   |
                                   V
             FALSE +--------------------------------+
        X<---------+  button   XOR   btn 1 bit set  |
                   +---------------+----------------+
                                   | TRUE
                                   |
                                   V
                      TRUE +---------------+ FALSE
             +-------------+  button > 0   +--------------+
             |             +---------------+              |
             |                                            |
             V                                            V
   +-----------------------+               TRUE +-------------------+
   |  Release(button);     |         X<---------+  STATUS bit set   |
   |  button = 0;          |                    +---------+---------+
   +---------+-------------+                              | FALSE
             |                                            |
             V                                            V
             X                       +--------------------------------------+
                                     |  button = (btn 1 bit) | (btn 2 bit); |
                                     |  Press(button);                      |
                                     +--------------------+-----------------+
                                                          |
                                                          V
                                                          X

  Note that the btn bits can be used like this ^^^ because they are in fact
  2^0 and 2^1, so the bit OR equals to 1 (btn 1 bit set, btn 2 bit clear)
  or 3 (btn 1 bit set, btn 2 bit set).
  This was also conveniently used in the implementation.