First Release

Dependencies:   USBDevice

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers InputDeviceDetector.h Source File

InputDeviceDetector.h

00001 /** Class: InputDeviceDetector
00002  *
00003  * Detect input device
00004  *
00005  */
00006 
00007 #include "mbed.h"
00008 #include "NiseKabuto.h"
00009 
00010 class InputDeviceDetector
00011 {
00012     private:
00013         // constant
00014         static const int SELSTATETIME__MICROSEC    = 2;     // Select信号の立ち上げ_立ち下げを行う長さ
00015                                                             // 短すぎると6Bが反応しない?
00016 
00017         // mbed pins
00018         DigitalIn   _IN_D0;
00019         DigitalIn   _IN_D1;
00020         DigitalIn   _IN_D2;
00021         DigitalIn   _IN_D3;
00022         DigitalIn   _IN_D4;
00023         DigitalIn   _IN_D5;
00024         DigitalOut  _OUT_SEL;
00025     
00026         char        _InputType;
00027         
00028     public:
00029         // Constructor
00030         InputDeviceDetector(PinName inputPins[])
00031             :_IN_D0(inputPins[0]), _IN_D1(inputPins[1]), 
00032              _IN_D2(inputPins[2]), _IN_D3(inputPins[3]), 
00033              _IN_D4(inputPins[4]), _IN_D5(inputPins[5]), 
00034              _OUT_SEL(inputPins[6])
00035         {
00036             // Pin Setting
00037             _IN_D0.mode(PullUp);
00038             _IN_D1.mode(PullUp);
00039             _IN_D2.mode(PullUp);
00040             _IN_D3.mode(PullUp);
00041             _IN_D4.mode(PullUp);
00042             _IN_D5.mode(PullUp);
00043             
00044             // Initialize pin status
00045             _OUT_SEL = 1;   // output SEL = H
00046         }
00047 
00048         // public: Return input device type
00049         char GetInputType()
00050         {
00051             char retVal = NiseKabuto::CONFIG_INMODE_CYBERSTICK_ANALOG;
00052             
00053             if( Check_MD6B() )
00054             {
00055                 retVal = NiseKabuto::CONFIG_INMODE_MD6B;
00056             }
00057             
00058             return retVal;
00059         }
00060 
00061         // Check if MD6B is connected ?
00062         //  return:
00063         //      non-0: Connected
00064         //          0: Not connected
00065         char Check_MD6B()
00066         {
00067             char retVal = 0;
00068             
00069             char flag6B = 0;
00070             int idx = 0;
00071             char _PhaseData[8];
00072 
00073             // Selを立ち下げ、データ読みを8回
00074             idx = 0;
00075             for(int i=0; i<4; i++)
00076             {
00077                 _OUT_SEL = 0;
00078                 wait_us(SELSTATETIME__MICROSEC);
00079                 
00080                 _PhaseData[idx++] = ReadPinValue();
00081                 
00082                 _OUT_SEL = 1;
00083                 wait_us(SELSTATETIME__MICROSEC);
00084 
00085                 _PhaseData[idx++] = ReadPinValue();
00086             }
00087             
00088             idx = 0;
00089             while(1)
00090             {
00091                 // D0-D3がLなものを探す
00092                 if( (_PhaseData[idx]&0x0f)==0 )
00093                 {
00094                     // idx+2をチェック
00095                     // ただし6を超えた場合はidx-6の位置をチェック
00096                     if(idx < 6)
00097                     {
00098                         if( (_PhaseData[idx+2]&0x0f)==0x0f )
00099                         {
00100                             flag6B = 1;
00101                             break;
00102                         }
00103                     }
00104                     else
00105                     {
00106                         if( (_PhaseData[idx-6]&0x0f)==0x0f )
00107                         {
00108                             flag6B = 1;
00109                             break;
00110                         }
00111                     }
00112                 }
00113                 
00114                 // このidxではなかった
00115                 idx++;
00116                 // 全部チェックしてしまった
00117                 if( idx == 8 )
00118                 {
00119                     break;
00120                 }
00121             }
00122             
00123             // この時点でflag6B==0の場合、6Bではない
00124             if( flag6B != 1 )
00125             {
00126                 retVal = 0;
00127             }
00128             else
00129             {
00130                 retVal = 1;
00131             }
00132             
00133             return retVal;
00134         }
00135         
00136         // ReadPinValue
00137         char ReadPinValue()
00138         {
00139             return ( 
00140                 (_IN_D5<<5) | (_IN_D4<<4) |
00141                 (_IN_D3<<3) | (_IN_D2<<2) | (_IN_D1<<1) |  _IN_D0
00142             );
00143         }
00144 
00145         
00146 };