File: dkey.c

package info (click to toggle)
brickos 0.9.0-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,700 kB
  • ctags: 1,727
  • sloc: ansic: 9,139; cpp: 860; makefile: 717; asm: 693; sh: 123; perl: 61
file content (133 lines) | stat: -rw-r--r-- 3,465 bytes parent folder | download | duplicates (7)
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
/*! \file   dkey.c
    \brief  Implementation: debounced key driver
    \author Markus L. Noga <markus@noga.de>
*/

/*
 *  The contents of this file are subject to the Mozilla Public License
 *  Version 1.0 (the "License"); you may not use this file except in
 *  compliance with the License. You may obtain a copy of the License at
 *  http://www.mozilla.org/MPL/
 *
 *  Software distributed under the License is distributed on an "AS IS"
 *  basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
 *  License for the specific language governing rights and limitations
 *  under the License.
 *
 *  The Original Code is legOS code, released October 17, 1999.
 *
 *  The Initial Developer of the Original Code is Markus L. Noga.
 *  Portions created by Markus L. Noga are Copyright (C) 1999
 *  Markus L. Noga. All Rights Reserved.
 *
 *  Contributor(s): Markus L. Noga <markus@noga.de>
 */

#include <dkey.h>

#ifdef CONF_DKEY

#include <unistd.h>
#include <sys/tm.h>

#ifdef CONF_AUTOSHUTOFF
#include <sys/timeout.h>
#endif

///////////////////////////////////////////////////////////////////////////////
//
// Global Variables
//
///////////////////////////////////////////////////////////////////////////////

volatile unsigned char dkey_multi;   	//! multi-key state
volatile unsigned char dkey;          	//! single key state

///////////////////////////////////////////////////////////////////////////////
//
// Internal Variables
//
///////////////////////////////////////////////////////////////////////////////

char dkey_timer __attribute__ ((unused)); //! debouncing timer

///////////////////////////////////////////////////////////////////////////////
//
// Functions
//
///////////////////////////////////////////////////////////////////////////////
#ifndef DOXYGEN_SHOULD_SKIP_THIS
__asm__("\n\
.text\n\
.align 1\n\
.global _dkey_handler\n\
_dkey_handler:\n\
   mov.b @_dkey_timer,r6l	   ; check debouncing timer==0\n\
   beq dkey_check\n\
\n\
     dec r6l\n\
     mov.b r6l,@_dkey_timer\n\
     rts\n\
\n\
dkey_check:\n\
   sub.b r6l,r6l      	      	   ; generate button codes\n\
      	      	      	      	   ; from PORT4/PORT7 in r6l\n\
   mov.b @_PORT4,r6h\n\
   bld #1,r6h\n\
   bist #0,r6l\n\
   bld #2,r6h\n\
   bist #1,r6l\n\
\n\
   mov.b @_PORT7,r6h\n\
   bld #6,r6h\n\
   bist #2,r6l\n\
   bld #7,r6h\n\
   bist #3,r6l\n\
\n\
   mov.b @_dkey_multi,r6h\n\
   xor.b r6l,r6h      	      	  ; create mask of changed positions in r6h\n\
   beq dkey_same\n\
\n\
     mov.b r6l,@_dkey_multi\n\
\n\
     and.b r6h,r6l    	      	  ; mask out unchanged positions\n\
     mov.b r6l,@_dkey\n\
\n\
     mov.b #100,r6l  	      	  ; set debouncing timer\n\
     mov.b r6l,@_dkey_timer\n\
\n\
dkey_same:\n\
   rts\n\
");
#endif // DOXYGEN_SHOULD_SKIP_THIS

//! wakeup if any of the given keys is pressed.
//
wakeup_t dkey_pressed(wakeup_t data) {
#ifdef CONF_AUTOSHUTOFF
  if (idle_powerdown) { // if idle too long, say the OFF key was pressed
    dkey = KEY_ONOFF;
    return KEY_ONOFF;
  }
#endif
  return (dkey & (unsigned char)data);
}

//! wakeup if all of the given keys are released.
//
wakeup_t dkey_released(wakeup_t data) {
  return ! (dkey & (unsigned char)data);
}

//! get and return a single key press, after waiting for it to arrive
//
int getchar(void) {
  wait_event(dkey_released,KEY_ANY);
#ifdef CONF_AUTOSHUTOFF
  shutoff_restart();
#endif
  wait_event(dkey_pressed ,KEY_ANY);
  return dkey;
}

#endif // CONF_DKEY