Blinks LED cube along with music
Dependencies: mbed mbed-rtos MCP23S17
main.cpp@1:ef3ad9c720c9, 2018-12-11 (annotated)
- Committer:
- kgoins3
- Date:
- Tue Dec 11 02:04:23 2018 +0000
- Revision:
- 1:ef3ad9c720c9
- Parent:
- 0:b6451e68016a
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
gth646f | 0:b6451e68016a | 1 | #include "mbed.h" |
gth646f | 0:b6451e68016a | 2 | #include "adc.h" |
kgoins3 | 1:ef3ad9c720c9 | 3 | #include "MCP23S17.h" |
kgoins3 | 1:ef3ad9c720c9 | 4 | #include "rtos.h" |
kgoins3 | 1:ef3ad9c720c9 | 5 | #define MN 256 |
kgoins3 | 1:ef3ad9c720c9 | 6 | #define SAMPLE_RATE 48000 |
gth646f | 0:b6451e68016a | 7 | |
kgoins3 | 1:ef3ad9c720c9 | 8 | // Create SPI bus |
kgoins3 | 1:ef3ad9c720c9 | 9 | SPI spi(p5, p6, p7); |
gth646f | 0:b6451e68016a | 10 | |
kgoins3 | 1:ef3ad9c720c9 | 11 | DigitalOut layer0(p24); |
kgoins3 | 1:ef3ad9c720c9 | 12 | DigitalOut layer1(p23); |
kgoins3 | 1:ef3ad9c720c9 | 13 | DigitalOut layer2(p22); |
kgoins3 | 1:ef3ad9c720c9 | 14 | DigitalOut layer3(p21); |
kgoins3 | 1:ef3ad9c720c9 | 15 | AnalogIn ain(p18); |
gth646f | 0:b6451e68016a | 16 | |
kgoins3 | 1:ef3ad9c720c9 | 17 | char Opcode = 0x40; |
kgoins3 | 1:ef3ad9c720c9 | 18 | MCP23S17 chip = MCP23S17(spi, p20, Opcode); |
gth646f | 0:b6451e68016a | 19 | Timer timer; |
gth646f | 0:b6451e68016a | 20 | Ticker ticker; |
gth646f | 0:b6451e68016a | 21 | |
gth646f | 0:b6451e68016a | 22 | int Counter = 0; |
kgoins3 | 1:ef3ad9c720c9 | 23 | int led_control = 1; |
gth646f | 0:b6451e68016a | 24 | int16_t Buffer[5000]; |
kgoins3 | 1:ef3ad9c720c9 | 25 | unsigned char PowerInt[MN/2]; |
gth646f | 0:b6451e68016a | 26 | ADC adc(SAMPLE_RATE, 1); |
kgoins3 | 1:ef3ad9c720c9 | 27 | float g; |
gth646f | 0:b6451e68016a | 28 | |
kgoins3 | 1:ef3ad9c720c9 | 29 | void sample_ADC(int chan, uint32_t value) { |
kgoins3 | 1:ef3ad9c720c9 | 30 | float s; |
kgoins3 | 1:ef3ad9c720c9 | 31 | s = adc.read(p19); |
kgoins3 | 1:ef3ad9c720c9 | 32 | Counter += 1; |
kgoins3 | 1:ef3ad9c720c9 | 33 | g = abs(s-2048); |
kgoins3 | 1:ef3ad9c720c9 | 34 | g = g/2048; |
gth646f | 0:b6451e68016a | 35 | } |
gth646f | 0:b6451e68016a | 36 | |
kgoins3 | 1:ef3ad9c720c9 | 37 | void LED_Thread(void const *args) { |
kgoins3 | 1:ef3ad9c720c9 | 38 | chip.write(PORT_A, 0x00); |
kgoins3 | 1:ef3ad9c720c9 | 39 | chip.write(PORT_B, 0x00); |
kgoins3 | 1:ef3ad9c720c9 | 40 | layer0 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 41 | layer1 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 42 | layer2 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 43 | layer3 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 44 | // Set all 8 Port A bits to output direction |
kgoins3 | 1:ef3ad9c720c9 | 45 | chip.direction(PORT_A, 0x00); |
kgoins3 | 1:ef3ad9c720c9 | 46 | // Set all 8 Port B bits to output direction |
kgoins3 | 1:ef3ad9c720c9 | 47 | chip.direction(PORT_B, 0x00); |
kgoins3 | 1:ef3ad9c720c9 | 48 | unsigned char c[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; |
kgoins3 | 1:ef3ad9c720c9 | 49 | while(1){ |
kgoins3 | 1:ef3ad9c720c9 | 50 | switch(led_control){ |
kgoins3 | 1:ef3ad9c720c9 | 51 | case 0: //Standard Mode |
kgoins3 | 1:ef3ad9c720c9 | 52 | chip.write(PORT_A, 0xFF); |
kgoins3 | 1:ef3ad9c720c9 | 53 | chip.write(PORT_B, 0xFF); |
kgoins3 | 1:ef3ad9c720c9 | 54 | if(g > 0 && g < .25){ |
kgoins3 | 1:ef3ad9c720c9 | 55 | layer3 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 56 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 57 | layer3 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 58 | } |
kgoins3 | 1:ef3ad9c720c9 | 59 | if(g > .25 && g < .5){ |
kgoins3 | 1:ef3ad9c720c9 | 60 | layer2 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 61 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 62 | layer2 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 63 | } |
kgoins3 | 1:ef3ad9c720c9 | 64 | if(g > .5 && g < .75){ |
kgoins3 | 1:ef3ad9c720c9 | 65 | layer1 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 66 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 67 | layer1 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 68 | } |
kgoins3 | 1:ef3ad9c720c9 | 69 | if(g > .75 && g < 1){ |
kgoins3 | 1:ef3ad9c720c9 | 70 | layer0 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 71 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 72 | layer0 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 73 | } |
kgoins3 | 1:ef3ad9c720c9 | 74 | break; |
kgoins3 | 1:ef3ad9c720c9 | 75 | case 1: //Standard + Screensaver |
kgoins3 | 1:ef3ad9c720c9 | 76 | char c_rand[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; |
kgoins3 | 1:ef3ad9c720c9 | 77 | if(g > 0 && g < .25){ |
kgoins3 | 1:ef3ad9c720c9 | 78 | layer3 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 79 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 80 | layer3 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 81 | } |
kgoins3 | 1:ef3ad9c720c9 | 82 | if(g > .25 && g < .5){ |
kgoins3 | 1:ef3ad9c720c9 | 83 | layer2 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 84 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 85 | layer2 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 86 | } |
kgoins3 | 1:ef3ad9c720c9 | 87 | if(g > .5 && g < .75){ |
kgoins3 | 1:ef3ad9c720c9 | 88 | layer1 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 89 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 90 | layer1 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 91 | } |
kgoins3 | 1:ef3ad9c720c9 | 92 | if(g > .75 && g < 1){ |
kgoins3 | 1:ef3ad9c720c9 | 93 | layer0 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 94 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 95 | layer0 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 96 | } |
kgoins3 | 1:ef3ad9c720c9 | 97 | for( int i = 0; i <=7; i++){chip.write(PORT_A, c[i]); //A loop |
kgoins3 | 1:ef3ad9c720c9 | 98 | Thread::wait(10);} |
kgoins3 | 1:ef3ad9c720c9 | 99 | chip.write(PORT_A, 0x00); |
kgoins3 | 1:ef3ad9c720c9 | 100 | for( int i = 0; i <=7; i++){chip.write(PORT_B, c[i]); //A loop |
kgoins3 | 1:ef3ad9c720c9 | 101 | Thread::wait(10);} |
kgoins3 | 1:ef3ad9c720c9 | 102 | chip.write(PORT_B, 0x00); //CLEAR |
kgoins3 | 1:ef3ad9c720c9 | 103 | break; |
kgoins3 | 1:ef3ad9c720c9 | 104 | case 2: //Standard + Screensaver {{RANDOM}} |
kgoins3 | 1:ef3ad9c720c9 | 105 | char c_rand[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; |
kgoins3 | 1:ef3ad9c720c9 | 106 | if(g > 0 && g < .25){ |
kgoins3 | 1:ef3ad9c720c9 | 107 | layer3 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 108 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 109 | layer3 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 110 | } |
kgoins3 | 1:ef3ad9c720c9 | 111 | if(g > .25 && g < .5){ |
kgoins3 | 1:ef3ad9c720c9 | 112 | layer2 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 113 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 114 | layer2 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 115 | } |
kgoins3 | 1:ef3ad9c720c9 | 116 | if(g > .5 && g < .75){ |
kgoins3 | 1:ef3ad9c720c9 | 117 | layer1 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 118 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 119 | layer1 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 120 | } |
kgoins3 | 1:ef3ad9c720c9 | 121 | if(g > .75 && g < 1){ |
kgoins3 | 1:ef3ad9c720c9 | 122 | layer0 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 123 | }else{ |
kgoins3 | 1:ef3ad9c720c9 | 124 | layer0 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 125 | } |
kgoins3 | 1:ef3ad9c720c9 | 126 | for (int i=0; i<7; i++) { |
kgoins3 | 1:ef3ad9c720c9 | 127 | int r = rand() % 7; // generate a random position |
kgoins3 | 1:ef3ad9c720c9 | 128 | int temp = c_rand[i]; c_rand[i] = c_rand[r]; c_rand[r] = temp; |
kgoins3 | 1:ef3ad9c720c9 | 129 | } |
kgoins3 | 1:ef3ad9c720c9 | 130 | for( int i = 0; i <=7; i++){chip.write(PORT_A, c_rand[i]); //A loop |
kgoins3 | 1:ef3ad9c720c9 | 131 | Thread::wait(10);} |
kgoins3 | 1:ef3ad9c720c9 | 132 | chip.write(PORT_A, 0x00); |
kgoins3 | 1:ef3ad9c720c9 | 133 | for( int i = 0; i <=7; i++){chip.write(PORT_B, c_rand[i]); //A loop |
kgoins3 | 1:ef3ad9c720c9 | 134 | Thread::wait(10);} |
kgoins3 | 1:ef3ad9c720c9 | 135 | chip.write(PORT_B, 0x00); //CLEAR |
kgoins3 | 1:ef3ad9c720c9 | 136 | break; |
kgoins3 | 1:ef3ad9c720c9 | 137 | case 3: //Screensaver |
kgoins3 | 1:ef3ad9c720c9 | 138 | for(int n = 0; n<= 3; n++){ |
kgoins3 | 1:ef3ad9c720c9 | 139 | switch(n){ |
kgoins3 | 1:ef3ad9c720c9 | 140 | case 1: layer0 = 0; layer1 = 1; layer2 = 0; layer3 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 141 | break; |
kgoins3 | 1:ef3ad9c720c9 | 142 | case 2: layer0 = 0; layer1 = 0; layer2 = 1; layer3 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 143 | break; |
kgoins3 | 1:ef3ad9c720c9 | 144 | case 3: layer0 = 0; layer1 = 0; layer2 = 0; layer3 = 1; |
kgoins3 | 1:ef3ad9c720c9 | 145 | break; |
kgoins3 | 1:ef3ad9c720c9 | 146 | default: layer0 = 1; layer1 = 0; layer2 = 0; layer3 = 0; |
kgoins3 | 1:ef3ad9c720c9 | 147 | break; |
kgoins3 | 1:ef3ad9c720c9 | 148 | } |
kgoins3 | 1:ef3ad9c720c9 | 149 | for( int i = 0; i <=7; i++){chip.write(PORT_A, c[i]); //A loop |
kgoins3 | 1:ef3ad9c720c9 | 150 | Thread::wait(100);} |
kgoins3 | 1:ef3ad9c720c9 | 151 | chip.write(PORT_A, 0x00); |
kgoins3 | 1:ef3ad9c720c9 | 152 | for( int i = 0; i <=7; i++){chip.write(PORT_B, c[i]); //A loop |
kgoins3 | 1:ef3ad9c720c9 | 153 | Thread::wait(100);} |
kgoins3 | 1:ef3ad9c720c9 | 154 | chip.write(PORT_B, 0x00); //CLEAR |
kgoins3 | 1:ef3ad9c720c9 | 155 | } |
kgoins3 | 1:ef3ad9c720c9 | 156 | break; |
kgoins3 | 1:ef3ad9c720c9 | 157 | } |
kgoins3 | 1:ef3ad9c720c9 | 158 | Thread::wait(25); |
kgoins3 | 1:ef3ad9c720c9 | 159 | } |
kgoins3 | 1:ef3ad9c720c9 | 160 | } |
gth646f | 0:b6451e68016a | 161 | |
gth646f | 0:b6451e68016a | 162 | int main() { |
kgoins3 | 1:ef3ad9c720c9 | 163 | Thread t1(LED_Thread); |
gth646f | 0:b6451e68016a | 164 | while (1) { |
kgoins3 | 1:ef3ad9c720c9 | 165 | //Prepare for burst mode on all ADC pins and set up interrupt handler |
gth646f | 0:b6451e68016a | 166 | adc.append(sample_ADC); |
gth646f | 0:b6451e68016a | 167 | adc.startmode(0,0); |
gth646f | 0:b6451e68016a | 168 | adc.burst(1); |
kgoins3 | 1:ef3ad9c720c9 | 169 | adc.setup(p19,1); |
gth646f | 0:b6451e68016a | 170 | wait(.4); |
kgoins3 | 1:ef3ad9c720c9 | 171 | adc.interrupt_state(p19,1); |
gth646f | 0:b6451e68016a | 172 | wait(0.1); |
kgoins3 | 1:ef3ad9c720c9 | 173 | adc.interrupt_state(p19,0); |
kgoins3 | 1:ef3ad9c720c9 | 174 | adc.setup(p19,0); |
gth646f | 0:b6451e68016a | 175 | int actual_rate = adc.actual_sample_rate(); |
gth646f | 0:b6451e68016a | 176 | } |
gth646f | 0:b6451e68016a | 177 | } |
gth646f | 0:b6451e68016a | 178 | |
gth646f | 0:b6451e68016a | 179 | |
gth646f | 0:b6451e68016a | 180 | |
gth646f | 0:b6451e68016a | 181 | |
gth646f | 0:b6451e68016a | 182 |