Digital JoyStick function, interrupt driven

Dependencies:   mbed

Committer:
pwheels
Date:
Sun Mar 13 13:17:21 2011 +0000
Revision:
0:1136d6d5aa1c
initial version

Who changed what in which revision?

UserRevisionLine numberNew 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 #include "mbed.h"
pwheels 0:1136d6d5aa1c 40 #include "joy.h"
pwheels 0:1136d6d5aa1c 41
pwheels 0:1136d6d5aa1c 42 JoyRead joy ( p21, p22, p23, p24, p25, PullUp); // center,
pwheels 0:1136d6d5aa1c 43
pwheels 0:1136d6d5aa1c 44 DigitalOut led1(LED1);
pwheels 0:1136d6d5aa1c 45
pwheels 0:1136d6d5aa1c 46 int main() {
pwheels 0:1136d6d5aa1c 47
pwheels 0:1136d6d5aa1c 48 // buffer first joystick movement, clear it upon read value
pwheels 0:1136d6d5aa1c 49 joy.setJoyStatic(1);
pwheels 0:1136d6d5aa1c 50
pwheels 0:1136d6d5aa1c 51 while(1) {
pwheels 0:1136d6d5aa1c 52 if (joy.getJoyStatus()) {
pwheels 0:1136d6d5aa1c 53 if (joy.getJoyKey(4)) {
pwheels 0:1136d6d5aa1c 54 printf("Joy Down is active\r\n");
pwheels 0:1136d6d5aa1c 55 }
pwheels 0:1136d6d5aa1c 56 printf("Joy value %x\r\n", joy.getJoyValue());
pwheels 0:1136d6d5aa1c 57 }
pwheels 0:1136d6d5aa1c 58 led1 = 0;
pwheels 0:1136d6d5aa1c 59 wait(0.2);
pwheels 0:1136d6d5aa1c 60 led1 = 1;
pwheels 0:1136d6d5aa1c 61 wait(0.2);
pwheels 0:1136d6d5aa1c 62 }
pwheels 0:1136d6d5aa1c 63 }