LAB
Dependencies: mbed
Fork of LAB11_Oppgave2 by
main.cpp@2:132db1b8730d, 2015-12-08 (annotated)
- Committer:
- knut_johan
- Date:
- Tue Dec 08 15:42:57 2015 +0000
- Revision:
- 2:132db1b8730d
- Parent:
- 1:d4624c8ba9f5
LAB11
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
rlanghbv | 0:87a84e072ec7 | 1 | #include "mbed.h" |
rlanghbv | 0:87a84e072ec7 | 2 | //SDA , SCL |
rlanghbv | 0:87a84e072ec7 | 3 | I2C i2cBus(PB_9, PB_8); |
rlanghbv | 0:87a84e072ec7 | 4 | |
rlanghbv | 0:87a84e072ec7 | 5 | Serial pc(SERIAL_TX, SERIAL_RX); |
knut_johan | 2:132db1b8730d | 6 | DigitalOut LED(PA_5); |
knut_johan | 2:132db1b8730d | 7 | PwmOut servoMotor(PA_7); |
knut_johan | 2:132db1b8730d | 8 | DigitalOut direction(PA_6); |
rlanghbv | 0:87a84e072ec7 | 9 | |
rlanghbv | 0:87a84e072ec7 | 10 | #define NUNCHUK_8BIT_ADDR 0x52<<1 // 8-bit I2C slave address = ( ( 7bit i2c address) << 1 | (read/write) bit |
rlanghbv | 0:87a84e072ec7 | 11 | |
rlanghbv | 0:87a84e072ec7 | 12 | typedef struct { |
rlanghbv | 0:87a84e072ec7 | 13 | int xStick; |
rlanghbv | 0:87a84e072ec7 | 14 | int yStick; |
rlanghbv | 0:87a84e072ec7 | 15 | int xAcc; |
rlanghbv | 0:87a84e072ec7 | 16 | int yAcc; |
rlanghbv | 0:87a84e072ec7 | 17 | int zAcc; |
rlanghbv | 0:87a84e072ec7 | 18 | bool bButtonC; |
rlanghbv | 0:87a84e072ec7 | 19 | bool bButtonZ; |
rlanghbv | 0:87a84e072ec7 | 20 | } NunchukData; |
rlanghbv | 0:87a84e072ec7 | 21 | |
rlanghbv | 0:87a84e072ec7 | 22 | void initNunchuk() |
rlanghbv | 0:87a84e072ec7 | 23 | { |
rlanghbv | 0:87a84e072ec7 | 24 | i2cBus.frequency(100000); |
rlanghbv | 0:87a84e072ec7 | 25 | |
rlanghbv | 0:87a84e072ec7 | 26 | wait(0.5); |
rlanghbv | 0:87a84e072ec7 | 27 | |
rlanghbv | 0:87a84e072ec7 | 28 | char cmd[2]; |
rlanghbv | 0:87a84e072ec7 | 29 | cmd[0] = 0x40; |
rlanghbv | 0:87a84e072ec7 | 30 | cmd[1] = 0x00; |
rlanghbv | 0:87a84e072ec7 | 31 | i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; |
rlanghbv | 0:87a84e072ec7 | 32 | |
rlanghbv | 0:87a84e072ec7 | 33 | cmd[0] = 0xF0; |
rlanghbv | 0:87a84e072ec7 | 34 | cmd[1] = 0x55; |
rlanghbv | 0:87a84e072ec7 | 35 | i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; |
rlanghbv | 0:87a84e072ec7 | 36 | |
rlanghbv | 0:87a84e072ec7 | 37 | cmd[0] = 0xFB; |
rlanghbv | 0:87a84e072ec7 | 38 | cmd[1] = 0x00; |
rlanghbv | 0:87a84e072ec7 | 39 | i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 2) ; |
rlanghbv | 0:87a84e072ec7 | 40 | } |
rlanghbv | 0:87a84e072ec7 | 41 | |
rlanghbv | 0:87a84e072ec7 | 42 | void GetNunchukData(NunchukData &data) |
rlanghbv | 0:87a84e072ec7 | 43 | { |
rlanghbv | 0:87a84e072ec7 | 44 | char buffer[6]; |
rlanghbv | 0:87a84e072ec7 | 45 | |
rlanghbv | 0:87a84e072ec7 | 46 | char cmd[1]; |
rlanghbv | 0:87a84e072ec7 | 47 | cmd[0] = 0x00; |
rlanghbv | 0:87a84e072ec7 | 48 | i2cBus.write(NUNCHUK_8BIT_ADDR, cmd, 1) ; |
rlanghbv | 0:87a84e072ec7 | 49 | |
rlanghbv | 0:87a84e072ec7 | 50 | wait(.01); |
rlanghbv | 0:87a84e072ec7 | 51 | i2cBus.read(NUNCHUK_8BIT_ADDR, buffer, 6) ; |
rlanghbv | 0:87a84e072ec7 | 52 | |
rlanghbv | 0:87a84e072ec7 | 53 | data.xStick = buffer[0]; |
rlanghbv | 0:87a84e072ec7 | 54 | data.yStick = buffer[1]; |
rlanghbv | 0:87a84e072ec7 | 55 | data.xAcc = (buffer[2] << 2) + ((buffer[5] & 0x0C) >> 2); |
rlanghbv | 0:87a84e072ec7 | 56 | data.yAcc = (buffer[3] << 2) + ((buffer[5] & 0x30) >> 4); |
rlanghbv | 0:87a84e072ec7 | 57 | data.zAcc = (buffer[4] << 2) + ((buffer[5] & 0xC0) >> 6); |
rlanghbv | 0:87a84e072ec7 | 58 | data.bButtonZ = (buffer[5] & 1) == 0; |
rlanghbv | 0:87a84e072ec7 | 59 | data.bButtonC = (buffer[5] & 2) == 0; |
rlanghbv | 0:87a84e072ec7 | 60 | } |
rlanghbv | 0:87a84e072ec7 | 61 | |
rlanghbv | 0:87a84e072ec7 | 62 | int main() |
rlanghbv | 0:87a84e072ec7 | 63 | { |
rlanghbv | 0:87a84e072ec7 | 64 | NunchukData nunchukData; |
rlanghbv | 0:87a84e072ec7 | 65 | |
rlanghbv | 0:87a84e072ec7 | 66 | initNunchuk(); |
knut_johan | 2:132db1b8730d | 67 | direction=0; |
rlanghbv | 0:87a84e072ec7 | 68 | while(true) { |
knut_johan | 2:132db1b8730d | 69 | LED = 0; |
knut_johan | 2:132db1b8730d | 70 | if(nunchukData.bButtonC == 1) { |
knut_johan | 2:132db1b8730d | 71 | LED = 1; |
knut_johan | 2:132db1b8730d | 72 | } |
rlanghbv | 0:87a84e072ec7 | 73 | GetNunchukData(nunchukData); |
rlanghbv | 0:87a84e072ec7 | 74 | printf("x-yoy %3d,y-yoy %3d ",nunchukData.xStick,nunchukData.yStick ); |
knut_johan | 2:132db1b8730d | 75 | printf("X-acc: %3d, Y-acc: %3d, Z-acc: %3d, knapp Z: %d, knapp C: %d",nunchukData.xAcc,nunchukData.yAcc, nunchukData.zAcc,nunchukData.bButtonZ, nunchukData.bButtonC ); |
rlanghbv | 1:d4624c8ba9f5 | 76 | printf("\r\n"); |
knut_johan | 2:132db1b8730d | 77 | |
knut_johan | 2:132db1b8730d | 78 | if(nunchukData.xStick<139) { |
knut_johan | 2:132db1b8730d | 79 | servoMotor=nunchukData.xStick/139.0f; |
knut_johan | 2:132db1b8730d | 80 | direction=1; |
knut_johan | 2:132db1b8730d | 81 | } else if(nunchukData.xStick>139) { |
knut_johan | 2:132db1b8730d | 82 | servoMotor=nunchukData.xStick/139.0f; |
knut_johan | 2:132db1b8730d | 83 | direction=0; |
knut_johan | 2:132db1b8730d | 84 | } |
knut_johan | 2:132db1b8730d | 85 | |
knut_johan | 2:132db1b8730d | 86 | if(nunchukData.bButtonZ==1) |
knut_johan | 2:132db1b8730d | 87 | { |
knut_johan | 2:132db1b8730d | 88 | if(nunchukData.xAcc<600) |
knut_johan | 2:132db1b8730d | 89 | { |
knut_johan | 2:132db1b8730d | 90 | servoMotor=nunchukData.xAcc/600.0f; |
knut_johan | 2:132db1b8730d | 91 | direction=0; |
knut_johan | 2:132db1b8730d | 92 | } |
knut_johan | 2:132db1b8730d | 93 | if(nunchukData.xAcc>600); |
knut_johan | 2:132db1b8730d | 94 | { |
knut_johan | 2:132db1b8730d | 95 | servoMotor=nunchukData.xAcc/600.0f; |
knut_johan | 2:132db1b8730d | 96 | direction=!direction; |
knut_johan | 2:132db1b8730d | 97 | } |
knut_johan | 2:132db1b8730d | 98 | } |
knut_johan | 2:132db1b8730d | 99 | |
rlanghbv | 0:87a84e072ec7 | 100 | } |
rlanghbv | 0:87a84e072ec7 | 101 | } |