![](/media/cache/profiles/a7bf3f5462cc82062e41b3a2262e1a21.50x50_q85.jpg)
Digital JoyStick function, interrupt driven
joy.h@0:1136d6d5aa1c, 2011-03-13 (annotated)
- Committer:
- pwheels
- Date:
- Sun Mar 13 13:17:21 2011 +0000
- Revision:
- 0:1136d6d5aa1c
initial version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
pwheels | 0:1136d6d5aa1c | 1 | /* |
pwheels | 0:1136d6d5aa1c | 2 | Copyright (c) 2011 Pro-Serv |
pwheels | 0:1136d6d5aa1c | 3 | |
pwheels | 0:1136d6d5aa1c | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy |
pwheels | 0:1136d6d5aa1c | 5 | of this software and associated documentation files (the "Software"), to deal |
pwheels | 0:1136d6d5aa1c | 6 | in the Software without restriction, including without limitation the rights |
pwheels | 0:1136d6d5aa1c | 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
pwheels | 0:1136d6d5aa1c | 8 | copies of the Software, and to permit persons to whom the Software is |
pwheels | 0:1136d6d5aa1c | 9 | furnished to do so, subject to the following conditions: |
pwheels | 0:1136d6d5aa1c | 10 | |
pwheels | 0:1136d6d5aa1c | 11 | The above copyright notice and this permission notice shall be included in |
pwheels | 0:1136d6d5aa1c | 12 | all copies or substantial portions of the Software. |
pwheels | 0:1136d6d5aa1c | 13 | |
pwheels | 0:1136d6d5aa1c | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
pwheels | 0:1136d6d5aa1c | 15 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
pwheels | 0:1136d6d5aa1c | 16 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
pwheels | 0:1136d6d5aa1c | 17 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
pwheels | 0:1136d6d5aa1c | 18 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
pwheels | 0:1136d6d5aa1c | 19 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN |
pwheels | 0:1136d6d5aa1c | 20 | THE SOFTWARE. |
pwheels | 0:1136d6d5aa1c | 21 | |
pwheels | 0:1136d6d5aa1c | 22 | Usage and assumptions: |
pwheels | 0:1136d6d5aa1c | 23 | the digital joystick device is directly attached to the used pins as per |
pwheels | 0:1136d6d5aa1c | 24 | function call, common contact to ground, using the internal pull-up's |
pwheels | 0:1136d6d5aa1c | 25 | but one can use external pull-up's too or even a small capaitor if device |
pwheels | 0:1136d6d5aa1c | 26 | shows signs of contact bounce. |
pwheels | 0:1136d6d5aa1c | 27 | |
pwheels | 0:1136d6d5aa1c | 28 | Operation modes: |
pwheels | 0:1136d6d5aa1c | 29 | By default it's using dynamic mode which means that the main program may |
pwheels | 0:1136d6d5aa1c | 30 | not see the expected pressed joystick contact in it's scan loop as another |
pwheels | 0:1136d6d5aa1c | 31 | joystick contact may already be active. |
pwheels | 0:1136d6d5aa1c | 32 | Using the static mode will capture the first joystick contact untill it's |
pwheels | 0:1136d6d5aa1c | 33 | been read by the getJoyValue function |
pwheels | 0:1136d6d5aa1c | 34 | |
pwheels | 0:1136d6d5aa1c | 35 | To do: |
pwheels | 0:1136d6d5aa1c | 36 | Adding a callable service routine to be used by the main program loop |
pwheels | 0:1136d6d5aa1c | 37 | */ |
pwheels | 0:1136d6d5aa1c | 38 | |
pwheels | 0:1136d6d5aa1c | 39 | #ifndef MBED_H |
pwheels | 0:1136d6d5aa1c | 40 | #include "mbed.h" |
pwheels | 0:1136d6d5aa1c | 41 | #endif |
pwheels | 0:1136d6d5aa1c | 42 | |
pwheels | 0:1136d6d5aa1c | 43 | #ifndef JOY_DELAY_PERIOD |
pwheels | 0:1136d6d5aa1c | 44 | #define JOY_DELAY_PERIOD 5000 |
pwheels | 0:1136d6d5aa1c | 45 | #endif |
pwheels | 0:1136d6d5aa1c | 46 | |
pwheels | 0:1136d6d5aa1c | 47 | #ifndef JOY_STATIC |
pwheels | 0:1136d6d5aa1c | 48 | #define JOY_STATIC 0 |
pwheels | 0:1136d6d5aa1c | 49 | #endif |
pwheels | 0:1136d6d5aa1c | 50 | |
pwheels | 0:1136d6d5aa1c | 51 | #ifndef JOY_CENTER |
pwheels | 0:1136d6d5aa1c | 52 | #define JOY_CENTER 1 |
pwheels | 0:1136d6d5aa1c | 53 | #endif |
pwheels | 0:1136d6d5aa1c | 54 | |
pwheels | 0:1136d6d5aa1c | 55 | #ifndef JOY_RIGHT |
pwheels | 0:1136d6d5aa1c | 56 | #define JOY_RIGHT 2 |
pwheels | 0:1136d6d5aa1c | 57 | #endif |
pwheels | 0:1136d6d5aa1c | 58 | |
pwheels | 0:1136d6d5aa1c | 59 | #ifndef JOY_DOWN |
pwheels | 0:1136d6d5aa1c | 60 | #define JOY_DOWN 4 |
pwheels | 0:1136d6d5aa1c | 61 | #endif |
pwheels | 0:1136d6d5aa1c | 62 | |
pwheels | 0:1136d6d5aa1c | 63 | #ifndef JOY_LEFT |
pwheels | 0:1136d6d5aa1c | 64 | #define JOY_LEFT 8 |
pwheels | 0:1136d6d5aa1c | 65 | #endif |
pwheels | 0:1136d6d5aa1c | 66 | |
pwheels | 0:1136d6d5aa1c | 67 | #ifndef JOY_UP |
pwheels | 0:1136d6d5aa1c | 68 | #define JOY_UP 16 |
pwheels | 0:1136d6d5aa1c | 69 | #endif |
pwheels | 0:1136d6d5aa1c | 70 | |
pwheels | 0:1136d6d5aa1c | 71 | namespace JOY { |
pwheels | 0:1136d6d5aa1c | 72 | |
pwheels | 0:1136d6d5aa1c | 73 | |
pwheels | 0:1136d6d5aa1c | 74 | class JoyRead { |
pwheels | 0:1136d6d5aa1c | 75 | |
pwheels | 0:1136d6d5aa1c | 76 | protected: |
pwheels | 0:1136d6d5aa1c | 77 | InterruptIn *_int0; |
pwheels | 0:1136d6d5aa1c | 78 | InterruptIn *_int1; |
pwheels | 0:1136d6d5aa1c | 79 | InterruptIn *_int2; |
pwheels | 0:1136d6d5aa1c | 80 | InterruptIn *_int3; |
pwheels | 0:1136d6d5aa1c | 81 | InterruptIn *_int4; |
pwheels | 0:1136d6d5aa1c | 82 | int _sampleDelay; |
pwheels | 0:1136d6d5aa1c | 83 | int _staticMode; |
pwheels | 0:1136d6d5aa1c | 84 | int _joy_status; |
pwheels | 0:1136d6d5aa1c | 85 | |
pwheels | 0:1136d6d5aa1c | 86 | void init(PinName px0, PinName px1, PinName px2, PinName px3, PinName px4, PinMode m) { |
pwheels | 0:1136d6d5aa1c | 87 | _sampleDelay = JOY_DELAY_PERIOD; |
pwheels | 0:1136d6d5aa1c | 88 | _staticMode = JOY_STATIC; |
pwheels | 0:1136d6d5aa1c | 89 | _joy_status = 0; |
pwheels | 0:1136d6d5aa1c | 90 | _int0 = new InterruptIn( px0 ); |
pwheels | 0:1136d6d5aa1c | 91 | _int1 = new InterruptIn( px1 ); |
pwheels | 0:1136d6d5aa1c | 92 | _int2 = new InterruptIn( px2 ); |
pwheels | 0:1136d6d5aa1c | 93 | _int3 = new InterruptIn( px3 ); |
pwheels | 0:1136d6d5aa1c | 94 | _int4 = new InterruptIn( px4 ); |
pwheels | 0:1136d6d5aa1c | 95 | _int0->mode( m ); |
pwheels | 0:1136d6d5aa1c | 96 | _int1->mode( m ); |
pwheels | 0:1136d6d5aa1c | 97 | _int2->mode( m ); |
pwheels | 0:1136d6d5aa1c | 98 | _int3->mode( m ); |
pwheels | 0:1136d6d5aa1c | 99 | _int4->mode( m ); |
pwheels | 0:1136d6d5aa1c | 100 | _int0->fall(this, &JoyRead::isr_int0_f); |
pwheels | 0:1136d6d5aa1c | 101 | _int1->fall(this, &JoyRead::isr_int1_f); |
pwheels | 0:1136d6d5aa1c | 102 | _int2->fall(this, &JoyRead::isr_int2_f); |
pwheels | 0:1136d6d5aa1c | 103 | _int3->fall(this, &JoyRead::isr_int3_f); |
pwheels | 0:1136d6d5aa1c | 104 | _int4->fall(this, &JoyRead::isr_int4_f); |
pwheels | 0:1136d6d5aa1c | 105 | _int0->rise(this, &JoyRead::isr_int0_r); |
pwheels | 0:1136d6d5aa1c | 106 | _int1->rise(this, &JoyRead::isr_int1_r); |
pwheels | 0:1136d6d5aa1c | 107 | _int2->rise(this, &JoyRead::isr_int2_r); |
pwheels | 0:1136d6d5aa1c | 108 | _int3->rise(this, &JoyRead::isr_int3_r); |
pwheels | 0:1136d6d5aa1c | 109 | _int4->rise(this, &JoyRead::isr_int4_r); |
pwheels | 0:1136d6d5aa1c | 110 | } |
pwheels | 0:1136d6d5aa1c | 111 | |
pwheels | 0:1136d6d5aa1c | 112 | public: |
pwheels | 0:1136d6d5aa1c | 113 | |
pwheels | 0:1136d6d5aa1c | 114 | /* PinDetect constructor(s) & overloading */ |
pwheels | 0:1136d6d5aa1c | 115 | JoyRead() { error("Provide a valid PinName"); } |
pwheels | 0:1136d6d5aa1c | 116 | |
pwheels | 0:1136d6d5aa1c | 117 | JoyRead(PinName px0, PinName px1, PinName px2, PinName px3, PinName px4) { |
pwheels | 0:1136d6d5aa1c | 118 | init( px0, px1, px2, px3, px4, PullUp); |
pwheels | 0:1136d6d5aa1c | 119 | } |
pwheels | 0:1136d6d5aa1c | 120 | |
pwheels | 0:1136d6d5aa1c | 121 | JoyRead(PinName px0, PinName px1, PinName px2, PinName px3, PinName px4, PinMode m) { |
pwheels | 0:1136d6d5aa1c | 122 | init( px0, px1, px2, px3, px4, m ); |
pwheels | 0:1136d6d5aa1c | 123 | } |
pwheels | 0:1136d6d5aa1c | 124 | |
pwheels | 0:1136d6d5aa1c | 125 | /* Set the sampling delay time in microseconds. */ |
pwheels | 0:1136d6d5aa1c | 126 | void setDelaySample(int i = JOY_DELAY_PERIOD) { _sampleDelay = i; } |
pwheels | 0:1136d6d5aa1c | 127 | |
pwheels | 0:1136d6d5aa1c | 128 | /* Set the sampling delay time in microseconds. */ |
pwheels | 0:1136d6d5aa1c | 129 | void setJoyStatic(int i = JOY_STATIC) { _staticMode = i; } |
pwheels | 0:1136d6d5aa1c | 130 | |
pwheels | 0:1136d6d5aa1c | 131 | /* Returns true if any joystick position is active */ |
pwheels | 0:1136d6d5aa1c | 132 | bool getJoyStatus(void) { |
pwheels | 0:1136d6d5aa1c | 133 | if ((_joy_status & 0x80) == 0x80) { |
pwheels | 0:1136d6d5aa1c | 134 | return true; |
pwheels | 0:1136d6d5aa1c | 135 | } else { |
pwheels | 0:1136d6d5aa1c | 136 | return false; |
pwheels | 0:1136d6d5aa1c | 137 | } |
pwheels | 0:1136d6d5aa1c | 138 | } |
pwheels | 0:1136d6d5aa1c | 139 | |
pwheels | 0:1136d6d5aa1c | 140 | /* Return true if selected joystick key is active */ |
pwheels | 0:1136d6d5aa1c | 141 | bool getJoyKey(int i) { |
pwheels | 0:1136d6d5aa1c | 142 | if ((_joy_status & 0x1f) == i) { |
pwheels | 0:1136d6d5aa1c | 143 | return true; |
pwheels | 0:1136d6d5aa1c | 144 | } else { |
pwheels | 0:1136d6d5aa1c | 145 | return false; |
pwheels | 0:1136d6d5aa1c | 146 | } |
pwheels | 0:1136d6d5aa1c | 147 | } |
pwheels | 0:1136d6d5aa1c | 148 | |
pwheels | 0:1136d6d5aa1c | 149 | /* Return value of all key's active state (may result in upto 3 keys at same time) */ |
pwheels | 0:1136d6d5aa1c | 150 | int getJoyValue(void) { |
pwheels | 0:1136d6d5aa1c | 151 | int tmp; |
pwheels | 0:1136d6d5aa1c | 152 | if ((_joy_status & 0x80) == 0x80) { |
pwheels | 0:1136d6d5aa1c | 153 | /* in _stiticMode = 1 the getJoyValue will reset joy available */ |
pwheels | 0:1136d6d5aa1c | 154 | tmp = _joy_status & 0x1f; |
pwheels | 0:1136d6d5aa1c | 155 | if (_staticMode == 1) { _joy_status = 0x00;} |
pwheels | 0:1136d6d5aa1c | 156 | return tmp; |
pwheels | 0:1136d6d5aa1c | 157 | } else { |
pwheels | 0:1136d6d5aa1c | 158 | return 0x00; |
pwheels | 0:1136d6d5aa1c | 159 | } |
pwheels | 0:1136d6d5aa1c | 160 | } |
pwheels | 0:1136d6d5aa1c | 161 | |
pwheels | 0:1136d6d5aa1c | 162 | protected: |
pwheels | 0:1136d6d5aa1c | 163 | /* The interrupt service routines are: fall and rise */ |
pwheels | 0:1136d6d5aa1c | 164 | void isr_int0_f(void) { |
pwheels | 0:1136d6d5aa1c | 165 | _joy_status |= 0x81; |
pwheels | 0:1136d6d5aa1c | 166 | } |
pwheels | 0:1136d6d5aa1c | 167 | void isr_int1_f(void) { |
pwheels | 0:1136d6d5aa1c | 168 | _joy_status |= 0x82; |
pwheels | 0:1136d6d5aa1c | 169 | } |
pwheels | 0:1136d6d5aa1c | 170 | void isr_int2_f(void) { |
pwheels | 0:1136d6d5aa1c | 171 | _joy_status |= 0x84; |
pwheels | 0:1136d6d5aa1c | 172 | } |
pwheels | 0:1136d6d5aa1c | 173 | void isr_int3_f(void) { |
pwheels | 0:1136d6d5aa1c | 174 | _joy_status |= 0x88; |
pwheels | 0:1136d6d5aa1c | 175 | } |
pwheels | 0:1136d6d5aa1c | 176 | void isr_int4_f(void) { |
pwheels | 0:1136d6d5aa1c | 177 | _joy_status |= 0x90; |
pwheels | 0:1136d6d5aa1c | 178 | } |
pwheels | 0:1136d6d5aa1c | 179 | /* The interrupt service routine for rise is only actively used in case of _staticMode = 0 */ |
pwheels | 0:1136d6d5aa1c | 180 | void isr_int0_r(void) { |
pwheels | 0:1136d6d5aa1c | 181 | if (_staticMode == 0) { |
pwheels | 0:1136d6d5aa1c | 182 | _joy_status &= 0x9e; |
pwheels | 0:1136d6d5aa1c | 183 | if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;} |
pwheels | 0:1136d6d5aa1c | 184 | } |
pwheels | 0:1136d6d5aa1c | 185 | } |
pwheels | 0:1136d6d5aa1c | 186 | void isr_int1_r(void) { |
pwheels | 0:1136d6d5aa1c | 187 | if (_staticMode == 0) { |
pwheels | 0:1136d6d5aa1c | 188 | _joy_status &= 0x9d; |
pwheels | 0:1136d6d5aa1c | 189 | if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;} |
pwheels | 0:1136d6d5aa1c | 190 | } |
pwheels | 0:1136d6d5aa1c | 191 | } |
pwheels | 0:1136d6d5aa1c | 192 | void isr_int2_r(void) { |
pwheels | 0:1136d6d5aa1c | 193 | if (_staticMode == 0) { |
pwheels | 0:1136d6d5aa1c | 194 | _joy_status &= 0x9b; |
pwheels | 0:1136d6d5aa1c | 195 | if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;} |
pwheels | 0:1136d6d5aa1c | 196 | } |
pwheels | 0:1136d6d5aa1c | 197 | } |
pwheels | 0:1136d6d5aa1c | 198 | void isr_int3_r(void) { |
pwheels | 0:1136d6d5aa1c | 199 | if (_staticMode == 0) { |
pwheels | 0:1136d6d5aa1c | 200 | _joy_status &= 0x97; |
pwheels | 0:1136d6d5aa1c | 201 | if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;} |
pwheels | 0:1136d6d5aa1c | 202 | } |
pwheels | 0:1136d6d5aa1c | 203 | } |
pwheels | 0:1136d6d5aa1c | 204 | void isr_int4_r(void) { |
pwheels | 0:1136d6d5aa1c | 205 | if (_staticMode == 0) { |
pwheels | 0:1136d6d5aa1c | 206 | _joy_status &= 0x8f; |
pwheels | 0:1136d6d5aa1c | 207 | if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;} |
pwheels | 0:1136d6d5aa1c | 208 | } |
pwheels | 0:1136d6d5aa1c | 209 | } |
pwheels | 0:1136d6d5aa1c | 210 | }; |
pwheels | 0:1136d6d5aa1c | 211 | |
pwheels | 0:1136d6d5aa1c | 212 | }; // namespace JOY ends. |
pwheels | 0:1136d6d5aa1c | 213 | |
pwheels | 0:1136d6d5aa1c | 214 | using namespace JOY; |