1: 2: 3: 4: 5: 6: 7: 8: 9: 10: 11: 12: 13:
14:
15: 16: 17: 18: 19: 20:
21:
22: #include "kbpd.h"
23:
24: #define pdMode (kpMgrInfo.pd.pdMode)
25: #define pdInStat (kpMgrInfo.statMach.pdInStat)
26:
27: 28: 29:
30: LOCAL void makePdMoveEvent( InnerEvent *evt, PdInput *msg )
31: {
32: evt->type |= IE_PDMOVE;
33: evt->i.pd.stat.o = msg->stat;
34: evt->i.pd.x = msg->xpos;
35: evt->i.pd.y = msg->ypos;
36: }
37:
38: 39: 40:
41: LOCAL void makePdButDownEvent( PdButState *pdBut, InnerEvent *evt )
42: {
43: evt->type |= ( pdBut->button == BK_MAIN )? IE_MBUTDOWN: IE_SBUTDOWN;
44: evt->i.pd.stat = pdInStat;
45:
46: pdBut->butdown = TRUE;
47: }
48:
49: 50: 51:
52: LOCAL void makePdButUpEvent( PdButState *pdBut, InnerEvent *evt )
53: {
54: evt->type |= ( pdBut->button == BK_MAIN )? IE_MBUTUP: IE_SBUTUP;
55: evt->i.pd.stat = pdInStat;
56:
57: pdBut->butdown = FALSE;
58: }
59:
60:
61:
62: 63: 64:
65: LOCAL BOOL buttonPress( PdButState *pdBut )
66: {
67: return ( (pdInStat.u.button & pdBut->button) != 0 )? TRUE: FALSE;
68: }
69:
70: 71: 72:
73: LOCAL void forceButtonRelease( PdButState *pdBut )
74: {
75: pdInStat.u.button &= ~pdBut->button;
76: }
77:
78:
79:
80: 81: 82:
83: LOCAL BState bsRelease( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
84: {
85: if ( msg == NULL ) return BS_RELEASE;
86:
87: switch ( msg->head.cmd.cmd ) {
88: case INP_PD:
89: if ( !buttonPress(pdBut) ) return BS_RELEASE;
90: break;
91:
92: default:
93: return BS_RELEASE;
94: }
95:
96: return BS_ONTIME;
97: }
98:
99: 100: 101:
102: LOCAL BState bsOnTime( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
103: {
104: ER err;
105:
106: if ( msg == NULL ) {
107: if ( pdMode.ontime == 0 ) goto butdown;
108:
109:
110: err = kpSetAlarm(&pdBut->alm, pdMode.ontime);
111: if ( err != E_OK ) {
112: DEBUG_PRINT(("bsOnTime, SetAlarm err = %d\n", err));
113: return BS_ERROR;
114: }
115: return BS_ONTIME;
116: }
117:
118: switch ( msg->head.cmd.cmd ) {
119: case INP_PD:
120: if ( !buttonPress(pdBut) ) return BS_RELEASE;
121: return BS_ONTIME;
122:
123: case PIC_TIMEOUT:
124: if ( !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_ONTIME;
125: break;
126:
127: default:
128: return BS_ONTIME;
129: }
130:
131: butdown:
132:
133: makePdButDownEvent(pdBut, evt);
134:
135: return BS_PRESS;
136: }
137:
138: 139: 140:
141: LOCAL BState bsPress( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
142: {
143: ER err;
144:
145: if ( msg == NULL ) {
146: reset:
147:
148: if ( pdInStat.u.tmout == 0 ) return BS_PRESS;
149:
150:
151: err = kpSetAlarm(&pdBut->alm, pdMode.timeout);
152: if ( err != E_OK ) {
153: DEBUG_PRINT(("bsPress, SetAlarm err = %d\n", err));
154: return BS_ERROR;
155: }
156: return BS_PRESS;
157: }
158:
159: switch ( msg->head.cmd.cmd ) {
160: case INP_PD:
161: if ( buttonPress(pdBut) ) goto reset;
162: return BS_OFFTIME;
163:
164: case PIC_TIMEOUT:
165: if ( pdInStat.u.tmout == 0
166: || !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_PRESS;
167:
168:
169: forceButtonRelease(pdBut);
170: break;
171:
172: default:
173: return BS_PRESS;
174: }
175:
176:
177: makePdButUpEvent(pdBut, evt);
178:
179: return BS_INVTIME;
180: }
181:
182: 183: 184:
185: LOCAL BState bsOffTime( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
186: {
187: ER err;
188:
189: if ( msg == NULL ) {
190: if ( pdMode.offtime == 0 ) goto butup;
191:
192:
193: err = kpSetAlarm(&pdBut->alm, pdMode.offtime);
194: if ( err != E_OK ) {
195: DEBUG_PRINT(("bsOffTime, SetAlarm err = %d\n", err));
196: return BS_ERROR;
197: }
198: return BS_OFFTIME;
199: }
200:
201: switch ( msg->head.cmd.cmd ) {
202: case INP_PD:
203: if ( buttonPress(pdBut) ) return BS_PRESS;
204: return BS_OFFTIME;
205:
206: case PIC_TIMEOUT:
207: if ( !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_OFFTIME;
208: break;
209:
210: default:
211: return BS_OFFTIME;
212: }
213:
214: butup:
215:
216: makePdButUpEvent(pdBut, evt);
217:
218: return BS_INVTIME;
219: }
220:
221: 222: 223:
224: LOCAL BState bsInvTime( PdButState *pdBut, InnerEvent *evt, ReceiveData *msg )
225: {
226: ER err;
227:
228: if ( msg == NULL ) {
229: if ( pdMode.invtime == 0 ) goto timeout;
230:
231:
232: err = kpSetAlarm(&pdBut->alm, pdMode.invtime);
233: if ( err != E_OK ) {
234: DEBUG_PRINT(("bsInvTime, SetAlarm err = %d\n", err));
235: return BS_ERROR;
236: }
237: return BS_INVTIME;
238: }
239:
240: switch ( msg->head.cmd.cmd ) {
241: case PIC_TIMEOUT:
242: if ( !kpChkAlarm(&pdBut->alm, &msg->tmout) ) return BS_INVTIME;
243: break;
244:
245: default:
246: return BS_INVTIME;
247: }
248:
249: timeout:
250: return ( buttonPress(pdBut) )? BS_ONTIME: BS_RELEASE;
251: }
252:
253: 254: 255:
256: LOCAL BState bsReset( PdButState *pdBut, InnerEvent *evt )
257: {
258: if ( pdBut->butdown ) {
259:
260: makePdButUpEvent(pdBut, evt);
261: }
262: return BS_RELEASE;
263: }
264:
265: 266: 267:
268: LOCAL void execPdButStateMachine( PdButState *pdBut,
269: InnerEvent *evt, ReceiveData *msg )
270: {
271: BState prev;
272:
273: do {
274: switch ( prev = pdBut->state ) {
275: case BS_RELEASE:
276: pdBut->state = bsRelease(pdBut, evt, msg);
277: break;
278: case BS_ONTIME:
279: pdBut->state = bsOnTime(pdBut, evt, msg);
280: break;
281: case BS_PRESS:
282: pdBut->state = bsPress(pdBut, evt, msg);
283: break;
284: case BS_OFFTIME:
285: pdBut->state = bsOffTime(pdBut, evt, msg);
286: break;
287: case BS_INVTIME:
288: pdBut->state = bsInvTime(pdBut, evt, msg);
289: break;
290:
291: default:
292: DEBUG_PRINT(("unknown BState(%d)\n", pdBut->state));
293: pdBut->state = bsReset(pdBut, evt);
294: return;
295: }
296: msg = NULL;
297: } while ( pdBut->state != prev );
298: }
299:
300: 301: 302:
303: EXPORT void kpExecPdButStateMachine( InnerEvent *evt, ReceiveData *msg, ButtonKind button )
304: {
305: PdButState *pdBut;
306: W i;
307:
308: if ( msg->head.cmd.err != DEV_OK ) {
309:
310: for ( i = 0; i < NumOfPdBut; ++i ) {
311: if ( (button & (1 << i)) == 0 ) continue;
312: pdBut = &kpMgrInfo.statMach.pdBut[i];
313: pdBut->state = bsReset(pdBut, evt);
314: }
315: return;
316: }
317:
318: if ( msg->head.cmd.cmd == INP_PD ) {
319:
320:
321: pdInStat.o = msg->pd.stat;
322:
323: if ( msg->pd.stat.inv == 0 ) {
324:
325: makePdMoveEvent(evt, &msg->pd);
326: }
327: }
328:
329:
330: for ( i = 0; i < NumOfPdBut; ++i ) {
331: if ( (button & (1 << i)) == 0 ) continue;
332: pdBut = &kpMgrInfo.statMach.pdBut[i];
333: execPdButStateMachine(pdBut, evt, msg);
334: }
335: }