power rangers
/
I2C_Master
master
main.cpp@0:3951c8490b9e, 2014-02-19 (annotated)
- Committer:
- TylerMuch
- Date:
- Wed Feb 19 01:00:59 2014 +0000
- Revision:
- 0:3951c8490b9e
done
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
TylerMuch | 0:3951c8490b9e | 1 | #include "mbed.h" |
TylerMuch | 0:3951c8490b9e | 2 | #include "music.h" |
TylerMuch | 0:3951c8490b9e | 3 | |
TylerMuch | 0:3951c8490b9e | 4 | float fu = 2; |
TylerMuch | 0:3951c8490b9e | 5 | float ha = fu/2; |
TylerMuch | 0:3951c8490b9e | 6 | float qu = ha/2; |
TylerMuch | 0:3951c8490b9e | 7 | float tq = fu*3/4; |
TylerMuch | 0:3951c8490b9e | 8 | float ei = fu/8; |
TylerMuch | 0:3951c8490b9e | 9 | float si = fu/16; |
TylerMuch | 0:3951c8490b9e | 10 | |
TylerMuch | 0:3951c8490b9e | 11 | char nyanNotes[] = { Eb5,E5,Fs5,Bb5,E5,Eb5,E5,Fs5,B5,Eb6,E6,Eb6,Bb5,B5,Fs5,Eb5,E5,Fs5,B5,Cs6,Bb5,B5,Cs6,E6,Eb6,E6,Cs6, |
TylerMuch | 0:3951c8490b9e | 12 | Fs5,Gs5,D5,Eb5,Cs5,Eb5,Cs5,B4,B4,Cs5,D5,D5,Cs5,B4,Cs5,Eb5,Fs5,Gs5,Eb5,Fs5,Cs5,Eb5,B4,Cs5,B4, |
TylerMuch | 0:3951c8490b9e | 13 | Eb5,Fs5,Gs5,Eb5,Fs5,Cs5,Eb5,B4,D5,Eb5,D5,Cs5,B4,Cs5,Eb5,B4,Cs5,Eb5,Fs5,Cs5,D5,Cs5,D5,Cs5,B4,Cs5,Fs5,Gs5,D5,Eb5,Cs5,Eb5,Cs5,B4,B4,Cs5, |
TylerMuch | 0:3951c8490b9e | 14 | D5,D5,Cs5,B4,Cs5,Eb5,Fs5,Gs5,Eb5,Fs5,Cs5,Eb5,B4,Cs5,B4,Eb5,Fs5,Gs5,Eb5,Gs5,Cs5,Eb5,B4,D5,E5,D5,Cs5,B4,Cs5, |
TylerMuch | 0:3951c8490b9e | 15 | Eb5,B4,Cs5,Eb5,Fs5,Cs5,D5,Cs5,D5,Cs5,B4,B4,B4,Fs4,Gs4,B4,Fs4,Gs4,B4,Cs5,Eb5,Cs5,E5,Eb5,E5,Fs5, |
TylerMuch | 0:3951c8490b9e | 16 | B4,B4,Fs4,Gs4,B4,Fs4,E5,Eb5,Cs5,B4,E4,Eb4,E4,Fs4,B4,Fs4,Gs4,B4,Fs4,Gs4,B4,B4,Cs5,Eb5,B4,Fs4,Gs4,Fs4, |
TylerMuch | 0:3951c8490b9e | 17 | B4,B4,Bb4,B4,Fs4,Gs4,B4,E5,Eb5,E5,Fs5,Eb5,Cs5,B4,Fs4,Gs4,B4,Fs4,Gs4,B4,Cs5,Eb5,Cs5,E5,Eb5,E5,Fs5, |
TylerMuch | 0:3951c8490b9e | 18 | B4,B4,Fs4,Gs4,B4,Fs4,E5,Eb5,Cs5,B4,E4,Eb4,E4,Fs4,B4,Fs4,Gs4,B4,Fs4,Gs4,B4,B4,Cs5,Eb5,B4,Fs4,Gs4,Fs4, |
TylerMuch | 0:3951c8490b9e | 19 | B4,B4,Bb4,B4,Fs4,Gs4,B4,E5,Eb5,E5,Fs5,B4,B4 |
TylerMuch | 0:3951c8490b9e | 20 | }; |
TylerMuch | 0:3951c8490b9e | 21 | float nyanBeat[]= { si,si,ei,si,si,si,si,si,si,si,si,si,si,ei,ei,si,si,ei,si,si,si,si,si,si,si,si,ei, |
TylerMuch | 0:3951c8490b9e | 22 | ei,ei,si,ei,si,si,si,ei,ei,ei,ei,si,si,si,si,si,si,si,si,si,si,si,si,si,si, |
TylerMuch | 0:3951c8490b9e | 23 | ei,ei,si,si,si,si,si,si,si,si,si,si,si,si,ei,si,si,si,si,si,si,si,si,ei,ei,ei,ei,ei,si,ei,si,si,si,ei,ei,ei, |
TylerMuch | 0:3951c8490b9e | 24 | ei,si,si,si,si,si,si,si,si,si,si,si,si,si,si,ei,ei,si,si,si,si,si,si,si,si,si,si,si,si, |
TylerMuch | 0:3951c8490b9e | 25 | ei,si,si,si,si,si,si,si,si,ei,ei,ei,ei,si,si,ei,si,si,si,si,si,si,si,si,si,si, |
TylerMuch | 0:3951c8490b9e | 26 | ei,ei,si,si,si,si,si,si,si,si,si,si,si,si,ei,si,si,ei,si,si,si,si,si,si,si,si,si,si, |
TylerMuch | 0:3951c8490b9e | 27 | ei,si,si,si,si,si,si,si,si,si,si,ei,ei,ei,si,si,ei,si,si,si,si,si,si,si,si,si,si, |
TylerMuch | 0:3951c8490b9e | 28 | ei,ei,si,si,si,si,si,si,si,si,si,si,si,si,ei,si,si,ei,si,si,si,si,si,si,si,si,si,si, |
TylerMuch | 0:3951c8490b9e | 29 | ei,si,si,si,si,si,si,si,si,si,si,si,si |
TylerMuch | 0:3951c8490b9e | 30 | };//beat array |
TylerMuch | 0:3951c8490b9e | 31 | |
TylerMuch | 0:3951c8490b9e | 32 | |
TylerMuch | 0:3951c8490b9e | 33 | //float song[]; |
TylerMuch | 0:3951c8490b9e | 34 | //float beat[]; |
TylerMuch | 0:3951c8490b9e | 35 | |
TylerMuch | 0:3951c8490b9e | 36 | //float alive_1[]; |
TylerMuch | 0:3951c8490b9e | 37 | //float alive_2[]; |
TylerMuch | 0:3951c8490b9e | 38 | |
TylerMuch | 0:3951c8490b9e | 39 | PwmOut spkr(p26); |
TylerMuch | 0:3951c8490b9e | 40 | DigitalIn fire(p14); |
TylerMuch | 0:3951c8490b9e | 41 | DigitalIn up(p15); |
TylerMuch | 0:3951c8490b9e | 42 | DigitalIn down(p12); |
TylerMuch | 0:3951c8490b9e | 43 | DigitalIn left(p13); |
TylerMuch | 0:3951c8490b9e | 44 | I2C i2c(p9, p10); |
TylerMuch | 0:3951c8490b9e | 45 | |
TylerMuch | 0:3951c8490b9e | 46 | const unsigned char addr = 0x42; |
TylerMuch | 0:3951c8490b9e | 47 | const unsigned char read = 0x01; |
TylerMuch | 0:3951c8490b9e | 48 | |
TylerMuch | 0:3951c8490b9e | 49 | |
TylerMuch | 0:3951c8490b9e | 50 | int main() { |
TylerMuch | 0:3951c8490b9e | 51 | i2c.frequency(1000); // 1kHz |
TylerMuch | 0:3951c8490b9e | 52 | while(1) |
TylerMuch | 0:3951c8490b9e | 53 | { |
TylerMuch | 0:3951c8490b9e | 54 | /* |
TylerMuch | 0:3951c8490b9e | 55 | if(up) |
TylerMuch | 0:3951c8490b9e | 56 | { |
TylerMuch | 0:3951c8490b9e | 57 | // both play master's song |
TylerMuch | 0:3951c8490b9e | 58 | |
TylerMuch | 0:3951c8490b9e | 59 | unsigned char freq; |
TylerMuch | 0:3951c8490b9e | 60 | float note; |
TylerMuch | 0:3951c8490b9e | 61 | |
TylerMuch | 0:3951c8490b9e | 62 | for(int i = 0; i < sizeof(song) / sizeof(float); i++) |
TylerMuch | 0:3951c8490b9e | 63 | { |
TylerMuch | 0:3951c8490b9e | 64 | freq = song[i]; |
TylerMuch | 0:3951c8490b9e | 65 | note = notes[freq]; |
TylerMuch | 0:3951c8490b9e | 66 | |
TylerMuch | 0:3951c8490b9e | 67 | i2c.start(); |
TylerMuch | 0:3951c8490b9e | 68 | i2c.write(addr); |
TylerMuch | 0:3951c8490b9e | 69 | i2c.write(freq); |
TylerMuch | 0:3951c8490b9e | 70 | i2c.stop(); |
TylerMuch | 0:3951c8490b9e | 71 | |
TylerMuch | 0:3951c8490b9e | 72 | spkr.period(1/note); |
TylerMuch | 0:3951c8490b9e | 73 | spkr = 0.5f; |
TylerMuch | 0:3951c8490b9e | 74 | wait(beat[i]); |
TylerMuch | 0:3951c8490b9e | 75 | } |
TylerMuch | 0:3951c8490b9e | 76 | |
TylerMuch | 0:3951c8490b9e | 77 | i2c.start(); |
TylerMuch | 0:3951c8490b9e | 78 | i2c.write(addr); |
TylerMuch | 0:3951c8490b9e | 79 | i2c.write(0xFF); // end song code |
TylerMuch | 0:3951c8490b9e | 80 | i2c.stop(); |
TylerMuch | 0:3951c8490b9e | 81 | |
TylerMuch | 0:3951c8490b9e | 82 | spkr = 0; |
TylerMuch | 0:3951c8490b9e | 83 | |
TylerMuch | 0:3951c8490b9e | 84 | } |
TylerMuch | 0:3951c8490b9e | 85 | |
TylerMuch | 0:3951c8490b9e | 86 | if(down) |
TylerMuch | 0:3951c8490b9e | 87 | { |
TylerMuch | 0:3951c8490b9e | 88 | // both play slave's song |
TylerMuch | 0:3951c8490b9e | 89 | int prev = -1; |
TylerMuch | 0:3951c8490b9e | 90 | freq = 0; |
TylerMuch | 0:3951c8490b9e | 91 | |
TylerMuch | 0:3951c8490b9e | 92 | i2c.start(); |
TylerMuch | 0:3951c8490b9e | 93 | i2c.write(addr | read); |
TylerMuch | 0:3951c8490b9e | 94 | |
TylerMuch | 0:3951c8490b9e | 95 | while(freq != 0xFF) |
TylerMuch | 0:3951c8490b9e | 96 | { |
TylerMuch | 0:3951c8490b9e | 97 | freq = i2c.read(addr); |
TylerMuch | 0:3951c8490b9e | 98 | if(freq != prev) { |
TylerMuch | 0:3951c8490b9e | 99 | note = notes[freq]; |
TylerMuch | 0:3951c8490b9e | 100 | spkr.period(1/note); |
TylerMuch | 0:3951c8490b9e | 101 | spkr = 0.5f; |
TylerMuch | 0:3951c8490b9e | 102 | } |
TylerMuch | 0:3951c8490b9e | 103 | prev = freg; |
TylerMuch | 0:3951c8490b9e | 104 | } |
TylerMuch | 0:3951c8490b9e | 105 | |
TylerMuch | 0:3951c8490b9e | 106 | i2c.stop(); |
TylerMuch | 0:3951c8490b9e | 107 | spkr = 0; |
TylerMuch | 0:3951c8490b9e | 108 | } |
TylerMuch | 0:3951c8490b9e | 109 | |
TylerMuch | 0:3951c8490b9e | 110 | if(fire) |
TylerMuch | 0:3951c8490b9e | 111 | { |
TylerMuch | 0:3951c8490b9e | 112 | // secret special sauce |
TylerMuch | 0:3951c8490b9e | 113 | unsigned char freq; |
TylerMuch | 0:3951c8490b9e | 114 | float note; |
TylerMuch | 0:3951c8490b9e | 115 | |
TylerMuch | 0:3951c8490b9e | 116 | for(int i = 0; i < sizeof(...); i++) |
TylerMuch | 0:3951c8490b9e | 117 | { |
TylerMuch | 0:3951c8490b9e | 118 | // play |
TylerMuch | 0:3951c8490b9e | 119 | freq = alive_1[i]; |
TylerMuch | 0:3951c8490b9e | 120 | note = notes[freq]; |
TylerMuch | 0:3951c8490b9e | 121 | |
TylerMuch | 0:3951c8490b9e | 122 | spkr.period(1/freq); |
TylerMuch | 0:3951c8490b9e | 123 | spkr = 0.5f; |
TylerMuch | 0:3951c8490b9e | 124 | |
TylerMuch | 0:3951c8490b9e | 125 | // send |
TylerMuch | 0:3951c8490b9e | 126 | freq = alive_2[i]; |
TylerMuch | 0:3951c8490b9e | 127 | note = notes[freq]; |
TylerMuch | 0:3951c8490b9e | 128 | |
TylerMuch | 0:3951c8490b9e | 129 | i2c.start(); |
TylerMuch | 0:3951c8490b9e | 130 | i2c.write(addr); |
TylerMuch | 0:3951c8490b9e | 131 | i2c.write(freq); |
TylerMuch | 0:3951c8490b9e | 132 | i2c.stop(); |
TylerMuch | 0:3951c8490b9e | 133 | |
TylerMuch | 0:3951c8490b9e | 134 | wait(beat[i]); |
TylerMuch | 0:3951c8490b9e | 135 | } |
TylerMuch | 0:3951c8490b9e | 136 | |
TylerMuch | 0:3951c8490b9e | 137 | i2c.start(); |
TylerMuch | 0:3951c8490b9e | 138 | i2c.write(addr); |
TylerMuch | 0:3951c8490b9e | 139 | i2c.write(0xFF); |
TylerMuch | 0:3951c8490b9e | 140 | i2c.stop(); |
TylerMuch | 0:3951c8490b9e | 141 | |
TylerMuch | 0:3951c8490b9e | 142 | spkr = 0; |
TylerMuch | 0:3951c8490b9e | 143 | }*/ |
TylerMuch | 0:3951c8490b9e | 144 | |
TylerMuch | 0:3951c8490b9e | 145 | if(left) |
TylerMuch | 0:3951c8490b9e | 146 | { |
TylerMuch | 0:3951c8490b9e | 147 | char freq; |
TylerMuch | 0:3951c8490b9e | 148 | float note; |
TylerMuch | 0:3951c8490b9e | 149 | for(int i = 0; i < sizeof(nyanNotes) / sizeof(nyanNotes[0]); i++) |
TylerMuch | 0:3951c8490b9e | 150 | { |
TylerMuch | 0:3951c8490b9e | 151 | freq = nyanNotes[i]; |
TylerMuch | 0:3951c8490b9e | 152 | note = notes[freq]; |
TylerMuch | 0:3951c8490b9e | 153 | spkr.period( 1 / note); |
TylerMuch | 0:3951c8490b9e | 154 | spkr = .5; |
TylerMuch | 0:3951c8490b9e | 155 | wait(nyanBeat[i]); |
TylerMuch | 0:3951c8490b9e | 156 | } |
TylerMuch | 0:3951c8490b9e | 157 | spkr = 0; |
TylerMuch | 0:3951c8490b9e | 158 | } |
TylerMuch | 0:3951c8490b9e | 159 | } |
TylerMuch | 0:3951c8490b9e | 160 | } |