Paul van der Wielen / Mbed 2 deprecated DigitalJoyStick

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers joy.h Source File

joy.h

00001 /*
00002     Copyright (c) 2011 Pro-Serv
00003  
00004     Permission is hereby granted, free of charge, to any person obtaining a copy
00005     of this software and associated documentation files (the "Software"), to deal
00006     in the Software without restriction, including without limitation the rights
00007     to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00008     copies of the Software, and to permit persons to whom the Software is
00009     furnished to do so, subject to the following conditions:
00010  
00011     The above copyright notice and this permission notice shall be included in
00012     all copies or substantial portions of the Software.
00013  
00014     THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00015     IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00016     FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00017     AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00018     LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00019     OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00020     THE SOFTWARE.
00021     
00022     Usage and assumptions:
00023         the digital joystick device is directly attached to the used pins as per
00024         function call, common contact to ground, using the internal pull-up's
00025         but one can use external pull-up's too or even a small capaitor if device
00026         shows signs of contact bounce.
00027         
00028     Operation modes:
00029         By default it's using dynamic mode which means that the main program may
00030         not see the expected pressed joystick contact in it's scan loop as another
00031         joystick contact may already be active.
00032         Using the static mode will capture the first joystick contact untill it's
00033         been read by the getJoyValue function
00034         
00035     To do:
00036         Adding a callable service routine to be used by the main program loop
00037  */
00038 
00039 #ifndef MBED_H
00040 #include "mbed.h"
00041 #endif
00042 
00043 #ifndef JOY_DELAY_PERIOD
00044 #define JOY_DELAY_PERIOD 5000
00045 #endif
00046 
00047 #ifndef JOY_STATIC
00048 #define JOY_STATIC 0
00049 #endif
00050 
00051 #ifndef JOY_CENTER
00052 #define JOY_CENTER 1
00053 #endif
00054 
00055 #ifndef JOY_RIGHT
00056 #define JOY_RIGHT 2
00057 #endif
00058 
00059 #ifndef JOY_DOWN
00060 #define JOY_DOWN 4
00061 #endif
00062 
00063 #ifndef JOY_LEFT
00064 #define JOY_LEFT 8
00065 #endif
00066 
00067 #ifndef JOY_UP
00068 #define JOY_UP 16
00069 #endif
00070 
00071 namespace JOY {
00072 
00073  
00074 class JoyRead {
00075 
00076 protected:
00077     InterruptIn *_int0;
00078     InterruptIn *_int1;
00079     InterruptIn *_int2;
00080     InterruptIn *_int3;
00081     InterruptIn *_int4;
00082     int         _sampleDelay;
00083     int         _staticMode;
00084     int         _joy_status;
00085     
00086     void init(PinName px0, PinName px1, PinName px2, PinName px3, PinName px4, PinMode m) {
00087         _sampleDelay             = JOY_DELAY_PERIOD;
00088         _staticMode              = JOY_STATIC;
00089         _joy_status = 0;
00090         _int0 = new InterruptIn( px0 ); 
00091         _int1 = new InterruptIn( px1 ); 
00092         _int2 = new InterruptIn( px2 ); 
00093         _int3 = new InterruptIn( px3 ); 
00094         _int4 = new InterruptIn( px4 ); 
00095         _int0->mode( m );
00096         _int1->mode( m );
00097         _int2->mode( m );
00098         _int3->mode( m );
00099         _int4->mode( m );
00100         _int0->fall(this, &JoyRead::isr_int0_f);
00101         _int1->fall(this, &JoyRead::isr_int1_f);
00102         _int2->fall(this, &JoyRead::isr_int2_f);
00103         _int3->fall(this, &JoyRead::isr_int3_f);
00104         _int4->fall(this, &JoyRead::isr_int4_f);
00105         _int0->rise(this, &JoyRead::isr_int0_r);
00106         _int1->rise(this, &JoyRead::isr_int1_r);
00107         _int2->rise(this, &JoyRead::isr_int2_r);
00108         _int3->rise(this, &JoyRead::isr_int3_r);
00109         _int4->rise(this, &JoyRead::isr_int4_r);
00110     }
00111 
00112 public:
00113 
00114     /* PinDetect constructor(s) & overloading */
00115     JoyRead() { error("Provide a valid PinName"); }
00116 
00117     JoyRead(PinName px0, PinName px1, PinName px2, PinName px3, PinName px4) {
00118         init( px0, px1, px2, px3, px4, PullUp);
00119     }
00120 
00121     JoyRead(PinName px0, PinName px1, PinName px2, PinName px3, PinName px4, PinMode m) {
00122         init(  px0, px1, px2, px3, px4, m );
00123     }
00124 
00125     /* Set the sampling delay time in microseconds. */
00126     void setDelaySample(int i = JOY_DELAY_PERIOD) { _sampleDelay = i; }
00127     
00128     /* Set the sampling delay time in microseconds. */
00129     void setJoyStatic(int i = JOY_STATIC) { _staticMode = i; }
00130     
00131     /* Returns true if any joystick position is active */
00132     bool getJoyStatus(void) {
00133         if ((_joy_status & 0x80) == 0x80) {
00134             return true;
00135         } else {
00136             return false;
00137         }
00138     }
00139     
00140     /* Return true if selected joystick key is active */
00141     bool getJoyKey(int i) {
00142         if ((_joy_status & 0x1f) == i) {
00143             return true;
00144         } else {
00145             return false;
00146         }
00147     }
00148     
00149     /* Return value of all key's active state (may result in upto 3 keys at same time) */
00150     int getJoyValue(void) {
00151         int tmp;
00152         if ((_joy_status & 0x80) == 0x80) {
00153             /* in _stiticMode = 1 the getJoyValue will reset joy available */
00154             tmp = _joy_status & 0x1f;
00155             if (_staticMode == 1) { _joy_status = 0x00;}
00156             return tmp;
00157         } else {
00158             return 0x00;
00159         }
00160     }
00161     
00162 protected:    
00163     /* The interrupt service routines are: fall and rise */
00164     void isr_int0_f(void) {
00165         _joy_status |= 0x81;
00166     }
00167     void isr_int1_f(void) {
00168         _joy_status |= 0x82;
00169     }
00170     void isr_int2_f(void) {
00171         _joy_status |= 0x84;
00172     }
00173     void isr_int3_f(void) {
00174         _joy_status |= 0x88;
00175     }
00176     void isr_int4_f(void) {
00177         _joy_status |= 0x90;
00178     }
00179     /* The interrupt service routine for rise is only actively used in case of _staticMode = 0 */
00180     void isr_int0_r(void) {
00181         if (_staticMode == 0) {
00182             _joy_status &= 0x9e;
00183             if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;}
00184         }
00185     }
00186     void isr_int1_r(void) {
00187         if (_staticMode == 0) {
00188             _joy_status &= 0x9d;
00189             if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;}
00190         }
00191     }
00192     void isr_int2_r(void) {
00193         if (_staticMode == 0) {
00194             _joy_status &= 0x9b;
00195             if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;}
00196         }
00197     }
00198     void isr_int3_r(void) {
00199         if (_staticMode == 0) {
00200             _joy_status &= 0x97;
00201             if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;}
00202         }
00203     }
00204     void isr_int4_r(void) {
00205         if (_staticMode == 0) {
00206             _joy_status &= 0x8f;
00207             if ((_joy_status & 0x1f) == 0x00) {_joy_status = 0x00;}
00208         }
00209     }
00210 };
00211 
00212 }; // namespace JOY ends.
00213 
00214 using namespace JOY;