Blinks LED cube along with music
Dependencies: mbed mbed-rtos MCP23S17
Diff: main.cpp
- Revision:
- 1:ef3ad9c720c9
- Parent:
- 0:b6451e68016a
diff -r b6451e68016a -r ef3ad9c720c9 main.cpp --- a/main.cpp Mon Feb 28 03:26:53 2011 +0000 +++ b/main.cpp Tue Dec 11 02:04:23 2018 +0000 @@ -1,119 +1,178 @@ - - - - #include "mbed.h" #include "adc.h" -#include "NokiaLCD.h" +#include "MCP23S17.h" +#include "rtos.h" +#define MN 256 +#define SAMPLE_RATE 48000 -#define MN 256 /*Number of points*/ -#define SAMPLE_RATE 48000 -NokiaLCD lcd(p5, p7, p8, p9, NokiaLCD::LCD6610); // mosi, sclk, cs, rst, type - +// Create SPI bus +SPI spi(p5, p6, p7); -//Going to use the Mellen FFT rather than STM, as the STM (port by Igor) won't compile -//extern "C" void cr4_fft_256_stm32(void *pssOUT, void *pssIN, uint16_t Nbin); -extern "C" void fftR4(short *y, short *x, int N); +DigitalOut layer0(p24); +DigitalOut layer1(p23); +DigitalOut layer2(p22); +DigitalOut layer3(p21); +AnalogIn ain(p18); -//use the LED as a bargraph -DigitalOut l1(LED1); -DigitalOut l2(LED2); -DigitalOut l3(LED3); -DigitalOut l4(LED4); - -//set up a timer for timing FFT's +char Opcode = 0x40; +MCP23S17 chip = MCP23S17(spi, p20, Opcode); Timer timer; Ticker ticker; -//Set up filesystem so we can write some useful files -LocalFileSystem local("local"); -FILE *fp; - - -//Set up a global buffer for audio data so interrupt can access it int Counter = 0; +int led_control = 1; int16_t Buffer[5000]; - - -//Initialise ADC to maximum SAMPLE_RATE and cclk divide set to 1 +unsigned char PowerInt[MN/2]; ADC adc(SAMPLE_RATE, 1); - -//Functions to write 16 bit audio data and 32 bit headers to files in au format (cf sndRecorder Cookbook) -void fwrite16(uint16_t v) { - uint8_t *b = (uint8_t *)&v; +float g; - fprintf(fp,"%c%c", b[1], b[0]); -} -void fwrite32(uint32_t v) { - uint8_t *b = (uint8_t *)&v; - - fprintf(fp,"%c%c%c%c", b[3], b[2], b[1], b[0]); +void sample_ADC(int chan, uint32_t value) { + float s; + s = adc.read(p19); + Counter += 1; + g = abs(s-2048); + g = g/2048; } - - -//Our interrupt handler for audio sampling -void sample_ADC(int chan, uint32_t value) { - - float s; - s = adc.read(p20); - int16_t b = (s -2048)*16; - Buffer[Counter] = b; - Counter += 1; - /* bar graph */ - int g = abs(s-2048); - l1 = g > 0.1f*2048; - l2 = g > 0.3f*2048; - l3 = g > 0.6f*2048; - l4 = g > 0.8f*2048; -} +void LED_Thread(void const *args) { + chip.write(PORT_A, 0x00); + chip.write(PORT_B, 0x00); + layer0 = 1; + layer1 = 0; + layer2 = 0; + layer3 = 0; +// Set all 8 Port A bits to output direction + chip.direction(PORT_A, 0x00); +// Set all 8 Port B bits to output direction + chip.direction(PORT_B, 0x00); + unsigned char c[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; + while(1){ + switch(led_control){ + case 0: //Standard Mode + chip.write(PORT_A, 0xFF); + chip.write(PORT_B, 0xFF); + if(g > 0 && g < .25){ + layer3 = 1; + }else{ + layer3 = 0; + } + if(g > .25 && g < .5){ + layer2 = 1; + }else{ + layer2 = 0; + } + if(g > .5 && g < .75){ + layer1 = 1; + }else{ + layer1 = 0; + } + if(g > .75 && g < 1){ + layer0 = 1; + }else{ + layer0 = 0; + } + break; + case 1: //Standard + Screensaver + char c_rand[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; + if(g > 0 && g < .25){ + layer3 = 1; + }else{ + layer3 = 0; + } + if(g > .25 && g < .5){ + layer2 = 1; + }else{ + layer2 = 0; + } + if(g > .5 && g < .75){ + layer1 = 1; + }else{ + layer1 = 0; + } + if(g > .75 && g < 1){ + layer0 = 1; + }else{ + layer0 = 0; + } + for( int i = 0; i <=7; i++){chip.write(PORT_A, c[i]); //A loop + Thread::wait(10);} + chip.write(PORT_A, 0x00); + for( int i = 0; i <=7; i++){chip.write(PORT_B, c[i]); //A loop + Thread::wait(10);} + chip.write(PORT_B, 0x00); //CLEAR + break; + case 2: //Standard + Screensaver {{RANDOM}} + char c_rand[] = {0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80}; + if(g > 0 && g < .25){ + layer3 = 1; + }else{ + layer3 = 0; + } + if(g > .25 && g < .5){ + layer2 = 1; + }else{ + layer2 = 0; + } + if(g > .5 && g < .75){ + layer1 = 1; + }else{ + layer1 = 0; + } + if(g > .75 && g < 1){ + layer0 = 1; + }else{ + layer0 = 0; + } + for (int i=0; i<7; i++) { + int r = rand() % 7; // generate a random position + int temp = c_rand[i]; c_rand[i] = c_rand[r]; c_rand[r] = temp; + } + for( int i = 0; i <=7; i++){chip.write(PORT_A, c_rand[i]); //A loop + Thread::wait(10);} + chip.write(PORT_A, 0x00); + for( int i = 0; i <=7; i++){chip.write(PORT_B, c_rand[i]); //A loop + Thread::wait(10);} + chip.write(PORT_B, 0x00); //CLEAR + break; + case 3: //Screensaver + for(int n = 0; n<= 3; n++){ + switch(n){ + case 1: layer0 = 0; layer1 = 1; layer2 = 0; layer3 = 0; + break; + case 2: layer0 = 0; layer1 = 0; layer2 = 1; layer3 = 0; + break; + case 3: layer0 = 0; layer1 = 0; layer2 = 0; layer3 = 1; + break; + default: layer0 = 1; layer1 = 0; layer2 = 0; layer3 = 0; + break; + } + for( int i = 0; i <=7; i++){chip.write(PORT_A, c[i]); //A loop + Thread::wait(100);} + chip.write(PORT_A, 0x00); + for( int i = 0; i <=7; i++){chip.write(PORT_B, c[i]); //A loop + Thread::wait(100);} + chip.write(PORT_B, 0x00); //CLEAR + } + break; + } + Thread::wait(25); + } +} int main() { - + Thread t1(LED_Thread); while (1) { - //Prepare for burst mode on all ADC pins and set up interrupt handler (using ADC library from Simon Blandford + //Prepare for burst mode on all ADC pins and set up interrupt handler adc.append(sample_ADC); adc.startmode(0,0); adc.burst(1); - adc.setup(p20,1); - //introduce a delay as initial waveform has bias whilst decoupling cap charges + adc.setup(p19,1); wait(.4); - //start the interrupt and wait for about 4096 samples - adc.interrupt_state(p20,1); + adc.interrupt_state(p19,1); wait(0.1); - //Finsh up - Unset pin 20 - adc.interrupt_state(p20,0); - adc.setup(p20,0); + adc.interrupt_state(p19,0); + adc.setup(p19,0); int actual_rate = adc.actual_sample_rate(); - - //now lets try mellen fft - lcd.background(0x0000FF); - - short mx[MN*2]; // input data 16 bit, 4 byte aligned x0r,x0i,x1r,x1i,.... - short my[MN*2]; // output data 16 bit,4 byte aligned y0r,y0i,y1r,y1i,.... - float data2[512]; - for (int i=0;i<MN*2;i++) mx[i]=0; - - for (int i=0;i<MN;i=i+1) { - mx[i*2]=Buffer[i]; - } - //FILE* mlog = fopen("/local/mellen.csv","w"); - //call functions; - fftR4(my, mx, MN); - for (int i=0; i<MN; i=i+2) { - data2[i]= sqrt(float( (my[i]*my[i]) +(my[i+1]*my[i+1]))); - //fprintf(mlog, "%d,%f\n", int(actual_rate/MN/2*i),sqrt(float( (my[i]*my[i]) +(my[i+1]*my[i+1])) ) ); - } - //fclose(mlog); - - - //Display amplitude on Nokia LCD - lcd.cls(); - for (int i=0; i<128; i++) { - data2[i+1] = 20*log10(data2[i+1]); - lcd.fill(i, 0, 2, data2[i+1], 0x00FF00); - } - Counter = 0; } }