Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: mbed
Fork of Project_B13_RGBmaster by
main.cpp
00001 #include "mbed.h" 00002 00003 BusOut switchOut(D2, D3, D4, D5); 00004 BusIn switchIn(D8, D9, D10, D11); 00005 DigitalIn mode1(D13); 00006 DigitalIn mode2(D12); 00007 AnalogIn a0(A0); 00008 AnalogIn a1(A1); 00009 AnalogIn a2(A2); 00010 Serial pc(USBTX, USBRX); 00011 Serial slave(PA_11,PA_12); 00012 void controlChange(uint8_t channel, uint8_t control, uint8_t value); 00013 void noteOn(uint8_t channel, uint8_t pitch, uint8_t velocity); 00014 void noteOff(uint8_t channel, uint8_t pitch); 00015 char mode = '1'; 00016 uint8_t keymap1[4][4]= {{0x2D,0x2E,0x2F,0x30},{0x31,0x32,0x33,0x34},{0x35,0x36,0x37,0x38},{0x39,0x3A,0x3B,0x3C}}; 00017 uint8_t keymap2[4][4]= {{0x2D,0x2E,0x2F,0x30},{0x31,0x32,0x33,0x34},{0x35,0x36,0x37,0x38},{0x39,0x3A,0x3B,0x3C}}; 00018 uint8_t channalmap[4][4]= {{1,1,1,1},{1,1,1,1},{2,2,2,2},{2,2,2,2}}; 00019 Timer t; 00020 uint32_t beat_ms = 150; 00021 uint32_t previous=0,current; 00022 /*uint8_t sequence[2][22]= {{0x32,0x2F,0x2F,0x32,0x2F,0x2F,0x32,0x2F,0x2F,0x39,0x00,0x38,0x37,0x35,0x35,0x33,0x31,0x31,0x30,0x2E,0x2E,0x00}, 00023 {127,127,127,127,127,127,127,127,127,127,0,127,127,127,127,127,127,127,127,127,127,0} 00024 };*/ 00025 uint8_t sequence[3][38]= {{ 00026 0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00, 0x32,0x2F,0x2F,0x00,0x32 ,0x2F,0x2F,0x00,0x32,0x2F, 0x2F, 00027 0x00,0x39,0x00,0x00 ,0x38,0x37,0x35,0x00,0x35, 0x33,0x31,0x00,0x31,0x30, 0x2E,0x00,0x2E,0x00,0x00 00028 }, 00029 {0,0,0,0,0,0,0,0, 127,127,127,0,127, 127,127,0,127,127 ,127,0,127,0,0 ,127,127,127,0,127 ,127,127,0,127,127 ,127,0,127,0,0}, 00030 {1,1,2,0,4, 4,3,0,3,3, 1,2,0,4,2, 2,0,1,0,0, 3,3,4,0,1, 1,2,0,3,3, 0,0,0,0,0,0,0,0} 00031 }; 00032 00033 int i = 1; 00034 //uint8_t song[]; 00035 00036 int main() 00037 { 00038 //uint8_t sw; 00039 int bacsw[4][4] = {0}; 00040 int addsw[4][4] = {0}; 00041 int sw[4]; 00042 pc.baud(115200); 00043 slave.baud(115200); 00044 t.start(); 00045 //current = t.read_ms(); 00046 00047 00048 while(1) { 00049 00050 if(mode == '2') { 00051 i = 1; 00052 while(mode == '2') { 00053 if(mode1 == 0){ 00054 mode = '1'; 00055 noteOff(1,sequence[0][i-1+6]+20); 00056 slave.printf("#1"); 00057 } 00058 current = t.read_ms(); 00059 if(current-previous >= beat_ms) { 00060 if(i>38) { 00061 00062 i = 1; 00063 noteOff(1,sequence[i+4][0]); 00064 } 00065 for(int j = 0;j<4;j++){ 00066 if(addsw[j][sequence[2][sequence[2][i]]-1]==1){ 00067 noteOn(1,sequence[0][i+6]+20,sequence[1][i+4]); 00068 break; 00069 } 00070 } 00071 00072 noteOff(1,sequence[0][i-1+4]+20); 00073 //wait_us(20); 00074 slave.printf("*%c",sequence[2][i-1]+'0'); 00075 i++; 00076 previous = t.read_ms(); 00077 } 00078 for(int i = 0 ; i<4 ; i++) { 00079 switch( i) { 00080 case 0: 00081 switchOut = 0b1110; 00082 sw[i] = 15-switchIn; 00083 break; 00084 case 1: 00085 switchOut = 0b1101; 00086 sw[i] = 15-switchIn; 00087 break; 00088 case 2: 00089 switchOut = 0b1011; 00090 sw[i] = 15-switchIn; 00091 break; 00092 case 3: 00093 switchOut = 0b0111; 00094 sw[i] = 15-switchIn; 00095 break; 00096 } 00097 } 00098 for(int i=0; i<4 ; i++) { 00099 if(sw[i]>7) { 00100 addsw[i][0] = 1; 00101 sw[i]= sw[i]-8; 00102 } else 00103 addsw[i][0] = 0; 00104 if(sw[i]>3) { 00105 addsw[i][1] = 1; 00106 sw[i] = sw[i] - 4; 00107 } else 00108 addsw[i][1] = 0; 00109 if(sw[i]>1) { 00110 addsw[i][2] = 1; 00111 sw[i] = sw[i] - 2; 00112 } else 00113 addsw[i][2] = 0; 00114 if(sw[i]>0) { 00115 addsw[i][3] = 1; 00116 sw[i] = sw[i] - 1; 00117 } else 00118 addsw[i][3] = 0; 00119 } 00120 00121 00122 for(int i =0; i<4; i++) { 00123 for(int j =0 ; j <4; j++) { 00124 if(addsw[i][j] != bacsw[i][j]) { 00125 //pc.printf("(%d,%d)",i,j); 00126 00127 if(addsw[i][j] ==1) { 00128 //noteOn(channalmap[i][j],keymap1[i][j],127); 00129 slave.printf("$1%c%c",'0'+i,'0'+j); 00130 } else { 00131 //noteOff(channalmap[i][j],keymap1[i][j]); 00132 slave.printf("$0%c%c",'0'+i,'0'+j); 00133 } 00134 } 00135 } 00136 } 00137 memcpy(bacsw,addsw,sizeof(bacsw)); 00138 } 00139 } 00140 if(mode == '1') { 00141 int volume; 00142 int shift; 00143 while(mode == '1') { 00144 volume = 127-a0.read()*127; 00145 shift = a1.read()*4; 00146 if(mode2 == 0){ 00147 mode = '2'; 00148 slave.printf("#2"); 00149 } 00150 for(int i = 0 ; i<4 ; i++) { 00151 switch( i) { 00152 case 0: 00153 switchOut = 0b1110; 00154 sw[i] = 15-switchIn; 00155 break; 00156 case 1: 00157 switchOut = 0b1101; 00158 sw[i] = 15-switchIn; 00159 break; 00160 case 2: 00161 switchOut = 0b1011; 00162 sw[i] = 15-switchIn; 00163 break; 00164 case 3: 00165 switchOut = 0b0111; 00166 sw[i] = 15-switchIn; 00167 break; 00168 } 00169 } 00170 for(int i=0; i<4 ; i++) { 00171 if(sw[i]>7) { 00172 addsw[i][0] = 1; 00173 sw[i]= sw[i]-8; 00174 } else 00175 addsw[i][0] = 0; 00176 if(sw[i]>3) { 00177 addsw[i][1] = 1; 00178 sw[i] = sw[i] - 4; 00179 } else 00180 addsw[i][1] = 0; 00181 if(sw[i]>1) { 00182 addsw[i][2] = 1; 00183 sw[i] = sw[i] - 2; 00184 } else 00185 addsw[i][2] = 0; 00186 if(sw[i]>0) { 00187 addsw[i][3] = 1; 00188 sw[i] = sw[i] - 1; 00189 } else 00190 addsw[i][3] = 0; 00191 } 00192 00193 00194 for(int i =0; i<4; i++) { 00195 for(int j =0 ; j <4; j++) { 00196 if(addsw[i][j] != bacsw[i][j]) { 00197 //pc.printf("(%d,%d)",i,j); 00198 00199 if(addsw[i][j] ==1) { 00200 noteOn(channalmap[i][j],keymap1[i][j]-9+8*shift,volume); 00201 slave.printf("$1%c%c",'0'+i,'0'+j); 00202 } else { 00203 noteOff(channalmap[i][j],keymap1[i][j]-9+8*shift); 00204 slave.printf("$0%c%c",'0'+i,'0'+j); 00205 } 00206 } 00207 } 00208 } 00209 memcpy(bacsw,addsw,sizeof(bacsw)); 00210 } 00211 } 00212 } 00213 } 00214 void noteOn(uint8_t channel, uint8_t pitch, uint8_t velocity) 00215 { 00216 channel += 0x90 - 1; 00217 if (channel >= 0x90 && channel <= 0x9F) { 00218 pc.putc(channel); 00219 pc.putc(pitch); 00220 pc.putc(velocity); 00221 } 00222 } 00223 00224 // Send a MIDI note off message 00225 void noteOff(uint8_t channel, uint8_t pitch) 00226 { 00227 channel += 0x80 - 1; 00228 if (channel >= 0x80 && channel <= 0x8F) { 00229 pc.putc(channel); 00230 pc.putc(pitch); 00231 pc.putc((uint8_t)0x00); 00232 } 00233 } 00234 00235 // Send a MIDI control change message 00236 void controlChange(uint8_t channel, uint8_t control, uint8_t value) 00237 { 00238 channel += 0xB0 - 1; 00239 if (channel >= 0xB0 && channel <= 0xBF) { 00240 pc.putc(channel); 00241 pc.putc(control); 00242 pc.putc(value); 00243 } 00244 }
Generated on Mon Jul 18 2022 02:17:58 by
1.7.2
