simple game using 2 mbed devices
Dependencies: 4DGL-uLCD-SE mbed-rtos mbed LSM9DS0
player2/main.cpp@0:8016d44e8294, 2015-10-20 (annotated)
- Committer:
- bfoley13
- Date:
- Tue Oct 20 20:04:10 2015 +0000
- Revision:
- 0:8016d44e8294
Project
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
bfoley13 | 0:8016d44e8294 | 1 | #include "rtos.h" |
bfoley13 | 0:8016d44e8294 | 2 | #include "mbed.h" |
bfoley13 | 0:8016d44e8294 | 3 | #include <math.h> |
bfoley13 | 0:8016d44e8294 | 4 | #include "LSM9DS0.h" |
bfoley13 | 0:8016d44e8294 | 5 | #include "SongPlayer.h" |
bfoley13 | 0:8016d44e8294 | 6 | |
bfoley13 | 0:8016d44e8294 | 7 | SongPlayer mySpeaker(p26); |
bfoley13 | 0:8016d44e8294 | 8 | AnalogIn aIn(p20); |
bfoley13 | 0:8016d44e8294 | 9 | AnalogOut aOut(p18); |
bfoley13 | 0:8016d44e8294 | 10 | |
bfoley13 | 0:8016d44e8294 | 11 | DigitalOut left(p21); |
bfoley13 | 0:8016d44e8294 | 12 | DigitalOut right(p22); |
bfoley13 | 0:8016d44e8294 | 13 | DigitalOut up(p23); |
bfoley13 | 0:8016d44e8294 | 14 | DigitalOut down(p24); |
bfoley13 | 0:8016d44e8294 | 15 | DigitalOut center(p25); |
bfoley13 | 0:8016d44e8294 | 16 | |
bfoley13 | 0:8016d44e8294 | 17 | DigitalOut led1(LED1); |
bfoley13 | 0:8016d44e8294 | 18 | DigitalOut led2(LED2); |
bfoley13 | 0:8016d44e8294 | 19 | DigitalOut led3(LED3); |
bfoley13 | 0:8016d44e8294 | 20 | DigitalOut led4(LED4); |
bfoley13 | 0:8016d44e8294 | 21 | |
bfoley13 | 0:8016d44e8294 | 22 | // SDO_XM and SDO_G are pulled up, so our addresses are: |
bfoley13 | 0:8016d44e8294 | 23 | #define LSM9DS0_XM_ADDR 0x1D // Would be 0x1E if SDO_XM is LOW |
bfoley13 | 0:8016d44e8294 | 24 | #define LSM9DS0_G_ADDR 0x6B // Would be 0x6A if SDO_G is LOW |
bfoley13 | 0:8016d44e8294 | 25 | |
bfoley13 | 0:8016d44e8294 | 26 | // refresh time. set to 500 for part 2 and 50 for part 4 |
bfoley13 | 0:8016d44e8294 | 27 | #define REFRESH_TIME_MS 500 |
bfoley13 | 0:8016d44e8294 | 28 | |
bfoley13 | 0:8016d44e8294 | 29 | // Verify that the pin assignments below match your breadboard |
bfoley13 | 0:8016d44e8294 | 30 | LSM9DS0 imu(p9, p10, LSM9DS0_G_ADDR, LSM9DS0_XM_ADDR); |
bfoley13 | 0:8016d44e8294 | 31 | |
bfoley13 | 0:8016d44e8294 | 32 | |
bfoley13 | 0:8016d44e8294 | 33 | Serial pc(USBTX, USBRX); |
bfoley13 | 0:8016d44e8294 | 34 | float note[18]= {1568.0,1396.9,1244.5,1244.5,1396.9,1568.0,1568.0,1568.0,1396.9, |
bfoley13 | 0:8016d44e8294 | 35 | 1244.5,1396.9,1568.0,1396.9,1244.5,1174.7,1244.5,1244.5, 0.0 |
bfoley13 | 0:8016d44e8294 | 36 | }; |
bfoley13 | 0:8016d44e8294 | 37 | float duration[18]= {0.48,0.24,0.72,0.48,0.24,0.48,0.24,0.24,0.24, |
bfoley13 | 0:8016d44e8294 | 38 | 0.24,0.24,0.24,0.24,0.48,0.24,0.48,0.48, 0.0 |
bfoley13 | 0:8016d44e8294 | 39 | }; |
bfoley13 | 0:8016d44e8294 | 40 | |
bfoley13 | 0:8016d44e8294 | 41 | void setup() |
bfoley13 | 0:8016d44e8294 | 42 | { |
bfoley13 | 0:8016d44e8294 | 43 | //pc.baud(115200); |
bfoley13 | 0:8016d44e8294 | 44 | |
bfoley13 | 0:8016d44e8294 | 45 | // Use the begin() function to initialize the LSM9DS0 library. |
bfoley13 | 0:8016d44e8294 | 46 | // You can either call it with no parameters (the easy way): |
bfoley13 | 0:8016d44e8294 | 47 | uint16_t status = imu.begin(); |
bfoley13 | 0:8016d44e8294 | 48 | |
bfoley13 | 0:8016d44e8294 | 49 | //Make sure communication is working |
bfoley13 | 0:8016d44e8294 | 50 | pc.printf("LSM9DS0 WHO_AM_I's returned: 0x%X\n", status); |
bfoley13 | 0:8016d44e8294 | 51 | pc.printf("Should be 0x49D4\n\n"); |
bfoley13 | 0:8016d44e8294 | 52 | } |
bfoley13 | 0:8016d44e8294 | 53 | |
bfoley13 | 0:8016d44e8294 | 54 | void playInputSig(void const *args) { |
bfoley13 | 0:8016d44e8294 | 55 | float testVal = aIn; |
bfoley13 | 0:8016d44e8294 | 56 | float testNote = 1333.3; |
bfoley13 | 0:8016d44e8294 | 57 | while(1) { |
bfoley13 | 0:8016d44e8294 | 58 | //aOut.write(0.0); |
bfoley13 | 0:8016d44e8294 | 59 | testVal = aIn.read(); |
bfoley13 | 0:8016d44e8294 | 60 | mySpeaker.PlaySong(&testNote, &testVal); |
bfoley13 | 0:8016d44e8294 | 61 | wait(testVal * 10); |
bfoley13 | 0:8016d44e8294 | 62 | } |
bfoley13 | 0:8016d44e8294 | 63 | } |
bfoley13 | 0:8016d44e8294 | 64 | |
bfoley13 | 0:8016d44e8294 | 65 | void readData() { |
bfoley13 | 0:8016d44e8294 | 66 | // To read from the device, you must first call the |
bfoley13 | 0:8016d44e8294 | 67 | // readMag(), readAccel(), and readGyro() functions. |
bfoley13 | 0:8016d44e8294 | 68 | // When this exits, it'll update the appropriate |
bfoley13 | 0:8016d44e8294 | 69 | // variables ([mx, my, mz], [ax, ay, az], [gx, gy, gz]) |
bfoley13 | 0:8016d44e8294 | 70 | // with the most current data. |
bfoley13 | 0:8016d44e8294 | 71 | |
bfoley13 | 0:8016d44e8294 | 72 | imu.readMag(); |
bfoley13 | 0:8016d44e8294 | 73 | imu.readAccel(); |
bfoley13 | 0:8016d44e8294 | 74 | imu.readGyro(); |
bfoley13 | 0:8016d44e8294 | 75 | } |
bfoley13 | 0:8016d44e8294 | 76 | |
bfoley13 | 0:8016d44e8294 | 77 | int main() { |
bfoley13 | 0:8016d44e8294 | 78 | aOut = 0.0; |
bfoley13 | 0:8016d44e8294 | 79 | Thread t1(playInputSig); |
bfoley13 | 0:8016d44e8294 | 80 | |
bfoley13 | 0:8016d44e8294 | 81 | imu.begin(); |
bfoley13 | 0:8016d44e8294 | 82 | float testVal = 0; |
bfoley13 | 0:8016d44e8294 | 83 | |
bfoley13 | 0:8016d44e8294 | 84 | while(1){ |
bfoley13 | 0:8016d44e8294 | 85 | readData(); |
bfoley13 | 0:8016d44e8294 | 86 | if(imu.gx < testVal) { |
bfoley13 | 0:8016d44e8294 | 87 | left = 0; |
bfoley13 | 0:8016d44e8294 | 88 | right = 1; |
bfoley13 | 0:8016d44e8294 | 89 | led1 = 1; |
bfoley13 | 0:8016d44e8294 | 90 | led2 = 0; |
bfoley13 | 0:8016d44e8294 | 91 | center = 1; |
bfoley13 | 0:8016d44e8294 | 92 | } else if(imu.gx > testVal) { |
bfoley13 | 0:8016d44e8294 | 93 | left = 1; |
bfoley13 | 0:8016d44e8294 | 94 | right = 0; |
bfoley13 | 0:8016d44e8294 | 95 | led1 = 0; |
bfoley13 | 0:8016d44e8294 | 96 | led2 = 1; |
bfoley13 | 0:8016d44e8294 | 97 | center = 1; |
bfoley13 | 0:8016d44e8294 | 98 | } |
bfoley13 | 0:8016d44e8294 | 99 | if(imu.gy < testVal) { |
bfoley13 | 0:8016d44e8294 | 100 | down = 0; |
bfoley13 | 0:8016d44e8294 | 101 | up = 1; |
bfoley13 | 0:8016d44e8294 | 102 | led3 = 1; |
bfoley13 | 0:8016d44e8294 | 103 | led4 = 0; |
bfoley13 | 0:8016d44e8294 | 104 | } else if(imu.gy > testVal) { |
bfoley13 | 0:8016d44e8294 | 105 | down = 1; |
bfoley13 | 0:8016d44e8294 | 106 | up = 0; |
bfoley13 | 0:8016d44e8294 | 107 | led3 = 0; |
bfoley13 | 0:8016d44e8294 | 108 | led4 = 1; |
bfoley13 | 0:8016d44e8294 | 109 | } |
bfoley13 | 0:8016d44e8294 | 110 | if((led1 | led2 | led3 | led4) == 0){ |
bfoley13 | 0:8016d44e8294 | 111 | led1 = 1; |
bfoley13 | 0:8016d44e8294 | 112 | led2 = 1; |
bfoley13 | 0:8016d44e8294 | 113 | led3 = 1; |
bfoley13 | 0:8016d44e8294 | 114 | led4 = 1; |
bfoley13 | 0:8016d44e8294 | 115 | center = 0; |
bfoley13 | 0:8016d44e8294 | 116 | } |
bfoley13 | 0:8016d44e8294 | 117 | |
bfoley13 | 0:8016d44e8294 | 118 | } |
bfoley13 | 0:8016d44e8294 | 119 | |
bfoley13 | 0:8016d44e8294 | 120 | } |