Kouichi Iijima
/
KXP84-2050_test
KXP84.cpp@0:ca2662a35eb0, 2011-01-24 (annotated)
- Committer:
- mr11451
- Date:
- Mon Jan 24 06:07:01 2011 +0000
- Revision:
- 0:ca2662a35eb0
- Child:
- 1:0637595215b4
test version
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mr11451 | 0:ca2662a35eb0 | 1 | #include "KXP84.h" |
mr11451 | 0:ca2662a35eb0 | 2 | #include "mbed.h" |
mr11451 | 0:ca2662a35eb0 | 3 | |
mr11451 | 0:ca2662a35eb0 | 4 | #define VERSION 0 |
mr11451 | 0:ca2662a35eb0 | 5 | #define REVISION 1 |
mr11451 | 0:ca2662a35eb0 | 6 | |
mr11451 | 0:ca2662a35eb0 | 7 | Ticker t; |
mr11451 | 0:ca2662a35eb0 | 8 | |
mr11451 | 0:ca2662a35eb0 | 9 | KXX::KXX() |
mr11451 | 0:ca2662a35eb0 | 10 | { |
mr11451 | 0:ca2662a35eb0 | 11 | gT = 0; |
mr11451 | 0:ca2662a35eb0 | 12 | gX = 0; |
mr11451 | 0:ca2662a35eb0 | 13 | gY = 0; |
mr11451 | 0:ca2662a35eb0 | 14 | gZ = 0; |
mr11451 | 0:ca2662a35eb0 | 15 | risther = NULL; |
mr11451 | 0:ca2662a35eb0 | 16 | } |
mr11451 | 0:ca2662a35eb0 | 17 | |
mr11451 | 0:ca2662a35eb0 | 18 | void KXX::Set_d( int x, int y, int z ) |
mr11451 | 0:ca2662a35eb0 | 19 | { |
mr11451 | 0:ca2662a35eb0 | 20 | d.x.sample = x; d.x.buffer = 0; |
mr11451 | 0:ca2662a35eb0 | 21 | d.y.sample = y; d.y.buffer = 0; |
mr11451 | 0:ca2662a35eb0 | 22 | d.z.sample = z; d.z.buffer = 0; |
mr11451 | 0:ca2662a35eb0 | 23 | } |
mr11451 | 0:ca2662a35eb0 | 24 | |
mr11451 | 0:ca2662a35eb0 | 25 | int KXX::Get_X() |
mr11451 | 0:ca2662a35eb0 | 26 | { |
mr11451 | 0:ca2662a35eb0 | 27 | return gX; |
mr11451 | 0:ca2662a35eb0 | 28 | } |
mr11451 | 0:ca2662a35eb0 | 29 | |
mr11451 | 0:ca2662a35eb0 | 30 | int KXX::Get_Y() |
mr11451 | 0:ca2662a35eb0 | 31 | { |
mr11451 | 0:ca2662a35eb0 | 32 | return gY; |
mr11451 | 0:ca2662a35eb0 | 33 | } |
mr11451 | 0:ca2662a35eb0 | 34 | |
mr11451 | 0:ca2662a35eb0 | 35 | int KXX::Get_Z() |
mr11451 | 0:ca2662a35eb0 | 36 | { |
mr11451 | 0:ca2662a35eb0 | 37 | return gZ; |
mr11451 | 0:ca2662a35eb0 | 38 | } |
mr11451 | 0:ca2662a35eb0 | 39 | |
mr11451 | 0:ca2662a35eb0 | 40 | void KXX::tickProc() |
mr11451 | 0:ca2662a35eb0 | 41 | { |
mr11451 | 0:ca2662a35eb0 | 42 | int i; |
mr11451 | 0:ca2662a35eb0 | 43 | |
mr11451 | 0:ca2662a35eb0 | 44 | funcTick(); |
mr11451 | 0:ca2662a35eb0 | 45 | // Average (radian sampling) |
mr11451 | 0:ca2662a35eb0 | 46 | if( SAMPLES<=gT ){ |
mr11451 | 0:ca2662a35eb0 | 47 | gT = 0; |
mr11451 | 0:ca2662a35eb0 | 48 | gX = gD[SAMPLES/2].x.sample; |
mr11451 | 0:ca2662a35eb0 | 49 | gY = gD[SAMPLES/2].y.sample; |
mr11451 | 0:ca2662a35eb0 | 50 | gZ = gD[SAMPLES/2].z.sample; |
mr11451 | 0:ca2662a35eb0 | 51 | if(risther) risther(); |
mr11451 | 0:ca2662a35eb0 | 52 | } |
mr11451 | 0:ca2662a35eb0 | 53 | |
mr11451 | 0:ca2662a35eb0 | 54 | for(i=0;i<gT;i++) |
mr11451 | 0:ca2662a35eb0 | 55 | { |
mr11451 | 0:ca2662a35eb0 | 56 | if ( gD[i].x.sample > d.x.sample ){ |
mr11451 | 0:ca2662a35eb0 | 57 | gD[i].x.buffer ++; |
mr11451 | 0:ca2662a35eb0 | 58 | }else{ |
mr11451 | 0:ca2662a35eb0 | 59 | d.x.buffer++; |
mr11451 | 0:ca2662a35eb0 | 60 | } |
mr11451 | 0:ca2662a35eb0 | 61 | if ( gD[i].y.sample > d.y.sample ){ |
mr11451 | 0:ca2662a35eb0 | 62 | gD[i].y.buffer ++; |
mr11451 | 0:ca2662a35eb0 | 63 | }else{ |
mr11451 | 0:ca2662a35eb0 | 64 | d.y.buffer++; |
mr11451 | 0:ca2662a35eb0 | 65 | } |
mr11451 | 0:ca2662a35eb0 | 66 | if ( gD[i].z.sample > d.z.sample ){ |
mr11451 | 0:ca2662a35eb0 | 67 | gD[i].z.buffer ++; |
mr11451 | 0:ca2662a35eb0 | 68 | }else{ |
mr11451 | 0:ca2662a35eb0 | 69 | d.z.buffer++; |
mr11451 | 0:ca2662a35eb0 | 70 | } |
mr11451 | 0:ca2662a35eb0 | 71 | } |
mr11451 | 0:ca2662a35eb0 | 72 | gD[i] = d; |
mr11451 | 0:ca2662a35eb0 | 73 | gT++; |
mr11451 | 0:ca2662a35eb0 | 74 | } |
mr11451 | 0:ca2662a35eb0 | 75 | |
mr11451 | 0:ca2662a35eb0 | 76 | void KXX::Start() |
mr11451 | 0:ca2662a35eb0 | 77 | { |
mr11451 | 0:ca2662a35eb0 | 78 | initial(); |
mr11451 | 0:ca2662a35eb0 | 79 | t.attach_us(this, &KXX::tickProc, 250); // 250ms |
mr11451 | 0:ca2662a35eb0 | 80 | } |
mr11451 | 0:ca2662a35eb0 | 81 | |
mr11451 | 0:ca2662a35eb0 | 82 | void KXP84_2050::funcTick() |
mr11451 | 0:ca2662a35eb0 | 83 | { |
mr11451 | 0:ca2662a35eb0 | 84 | int iX0,iX1; |
mr11451 | 0:ca2662a35eb0 | 85 | int iY0,iY1; |
mr11451 | 0:ca2662a35eb0 | 86 | int iZ0,iZ1; |
mr11451 | 0:ca2662a35eb0 | 87 | |
mr11451 | 0:ca2662a35eb0 | 88 | _cs = 0; /* Select KXP84-2040 */ |
mr11451 | 0:ca2662a35eb0 | 89 | _spi.write(0x80); /* send Command */ |
mr11451 | 0:ca2662a35eb0 | 90 | wait_us(200); /* Wait 200ms (for sampling) */ |
mr11451 | 0:ca2662a35eb0 | 91 | |
mr11451 | 0:ca2662a35eb0 | 92 | iX0 = _spi.write(0x00); iX1 = _spi.write(0x00); /* recive Status (with auto increment) */ |
mr11451 | 0:ca2662a35eb0 | 93 | iY0 = _spi.write(0x00); iY1 = _spi.write(0x00); /* recive Status (with auto increment) */ |
mr11451 | 0:ca2662a35eb0 | 94 | iZ0 = _spi.write(0x00); iZ1 = _spi.write(0x00); /* recive Status (with auto increment) */ |
mr11451 | 0:ca2662a35eb0 | 95 | _cs = 1; /* UnSelect KXP84-2040 */ |
mr11451 | 0:ca2662a35eb0 | 96 | |
mr11451 | 0:ca2662a35eb0 | 97 | Set_d( (iX0<<8) + iX1, (iY0<<8) + iY1, (iZ0<<8) + iZ1 ); |
mr11451 | 0:ca2662a35eb0 | 98 | } |
mr11451 | 0:ca2662a35eb0 | 99 | |
mr11451 | 0:ca2662a35eb0 | 100 | KXP84_2050::KXP84_2050(PinName mosi, PinName miso, PinName sclk, PinName cs, PinName rst): |
mr11451 | 0:ca2662a35eb0 | 101 | _spi(mosi, miso, sclk), _cs(cs), _rst(rst) |
mr11451 | 0:ca2662a35eb0 | 102 | { |
mr11451 | 0:ca2662a35eb0 | 103 | _rst = 1; |
mr11451 | 0:ca2662a35eb0 | 104 | _cs = 1; |
mr11451 | 0:ca2662a35eb0 | 105 | } |
mr11451 | 0:ca2662a35eb0 | 106 | |
mr11451 | 0:ca2662a35eb0 | 107 | void KXP84_2050::initial() |
mr11451 | 0:ca2662a35eb0 | 108 | { |
mr11451 | 0:ca2662a35eb0 | 109 | // Setup the spi for 8 bit data, high steady state clock, |
mr11451 | 0:ca2662a35eb0 | 110 | // second edge capture, with a 2MHz clock rate |
mr11451 | 0:ca2662a35eb0 | 111 | _spi.format(8,1); |
mr11451 | 0:ca2662a35eb0 | 112 | _spi.frequency(2000000); |
mr11451 | 0:ca2662a35eb0 | 113 | |
mr11451 | 0:ca2662a35eb0 | 114 | _rst = 0; /* WakeUp KXP84-2040 */ |
mr11451 | 0:ca2662a35eb0 | 115 | wait_us(1); /* wait > 130ns */ |
mr11451 | 0:ca2662a35eb0 | 116 | |
mr11451 | 0:ca2662a35eb0 | 117 | _cs = 0; /* Select KXP84-2040 */ |
mr11451 | 0:ca2662a35eb0 | 118 | _spi.write(0x0A); _spi.write(0x00); |
mr11451 | 0:ca2662a35eb0 | 119 | _spi.write(0x0B); _spi.write(0x00); |
mr11451 | 0:ca2662a35eb0 | 120 | _cs = 1; /* UnSelect KXP84-2040 */ |
mr11451 | 0:ca2662a35eb0 | 121 | wait_us(1); /* wait > 130ns */ |
mr11451 | 0:ca2662a35eb0 | 122 | } |
mr11451 | 0:ca2662a35eb0 | 123 | |
mr11451 | 0:ca2662a35eb0 | 124 | void KXM52_1050::funcTick() |
mr11451 | 0:ca2662a35eb0 | 125 | { |
mr11451 | 0:ca2662a35eb0 | 126 | Set_d( _aiX.read_u16(), _aiY.read_u16(), _aiZ.read_u16() ); |
mr11451 | 0:ca2662a35eb0 | 127 | } |
mr11451 | 0:ca2662a35eb0 | 128 | |
mr11451 | 0:ca2662a35eb0 | 129 | KXM52_1050::KXM52_1050(PinName pwr, PinName ix, PinName iy, PinName iz): |
mr11451 | 0:ca2662a35eb0 | 130 | _power(pwr), _aiX(ix), _aiY(iy), _aiZ(iz) |
mr11451 | 0:ca2662a35eb0 | 131 | {} |
mr11451 | 0:ca2662a35eb0 | 132 | |
mr11451 | 0:ca2662a35eb0 | 133 | void KXM52_1050::initial() |
mr11451 | 0:ca2662a35eb0 | 134 | { |
mr11451 | 0:ca2662a35eb0 | 135 | _power = 1; |
mr11451 | 0:ca2662a35eb0 | 136 | } |