draft

Dependencies:   FastAnalogIn HSI2RGBW_PWM NVIC_set_all_priorities mbed-dsp mbed MMA8451Q

Fork of KL25Z_FFT_Demo by Frank Vannieuwkerke

Revision:
4:5d4bcc4751d7
Parent:
3:a8238ddc2868
--- a/main.cpp	Sun Apr 24 21:50:46 2016 +0000
+++ b/main.cpp	Mon Apr 25 21:52:32 2016 +0000
@@ -9,13 +9,21 @@
 #include "arm_const_structs.h"
 #include "hsi2rgbw_pwm.h"
 #include "FastAnalogIn.h"
+#include "MMA8451Q.h"
 
 Serial pc(USBTX, USBRX);
 
 FastAnalogIn   Audio(PTC2);
 
+
+PinName const SDA = PTE25;
+PinName const SCL = PTE24;
+
+SPI spi(PTD2, PTD3, PTD1);          // Arduino compatible MOSI, MISO, SCLK
+DigitalOut cs(PTD0);                // Chip select
 //#define RGBW_ext // Disable this line when you want to use the KL25Z on-board RGB LED.
-
+#define MMA8451_I2C_ADDRESS (0x1d<<1)
+/*
 #ifndef RGBW_ext
 // HSI to RGB conversion with direct output to PWM channels - on-board RGB LED
 hsi2rgbw_pwm led(LED_RED, LED_GREEN, LED_BLUE);
@@ -23,7 +31,7 @@
 // HSI to RGBW conversion with direct output to external PWM channels - RGBW LED
 hsi2rgbw_pwm led(PTD4, PTA12, PTA4, PTA5); //Red, Green, Blue, White
 #endif
-
+*/
 // Dummy ISR for disabling NMI on PTA4 - !! DO NOT REMOVE THIS !!
 // More info at https://mbed.org/questions/1387/How-can-I-access-the-FTFA_FOPT-register-/
 extern "C" void NMI_Handler() {
@@ -46,7 +54,7 @@
                                         // Only active when nonzero.
                                         // A value >= 1000 and <= 1000 + PIXEL_COUNT fixes the output to a single frequency
                                         // window = a single color.
-int SAMPLE_RATE_HZ = 10000;             // Sample rate of the audio in hertz.
+int SAMPLE_RATE_HZ = 20000;             // Sample rate of the audio in hertz.
 float SPECTRUM_MIN_DB = 30.0;           // Audio intensity (in decibels) that maps to low LED brightness.
 float SPECTRUM_MAX_DB = 80.0;           // Audio intensity (in decibels) that maps to high LED brightness.
 int LEDS_ENABLED = 1;                   // Control if the LED's should display the spectrum or not.  1 is true, 0 is false.
@@ -56,7 +64,7 @@
                                         // any other changes to the program.
 const int MAX_CHARS = 65;               // Max size of the input command buffer
 
-//const int tempCurrentLevel[8] = {0b00000001, 0b00000011, 0b00000111, 0b00001111, 0b00011111, 0b00111111, 0b01111111, 0b11111111};
+const unsigned char tempCurrentLevel[] = {0x01, 0x03, 0x07, 0x0F, 0x1F, 0x3F, 0x7F, 0xFF};
 
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -76,6 +84,37 @@
 // UTILITY FUNCTIONS
 ////////////////////////////////////////////////////////////////////////////////
 
+/// Send two bytes to SPI bus
+void SPI_Write2(unsigned char MSB, unsigned char LSB)
+{
+    cs = 0;                         // Set CS Low
+    spi.write(MSB);                 // Send two bytes
+    spi.write(LSB);
+    cs = 1;                         // Set CS High
+}
+
+
+/// MAX7219 initialisation
+void Init_MAX7219(void)
+{
+    SPI_Write2(0x09, 0x00);         // Decoding off
+    SPI_Write2(0x0A, 0x08);         // Brightness to intermediate
+    SPI_Write2(0x0B, 0x07);         // Scan limit = 7
+    SPI_Write2(0x0C, 0x01);         // Normal operation mode
+    SPI_Write2(0x0F, 0x0F);         // Enable display test
+    wait_ms(500);                   // 500 ms delay
+    SPI_Write2(0x01, 0x00);         // Clear row 0.
+    SPI_Write2(0x02, 0x00);         // Clear row 1.
+    SPI_Write2(0x03, 0x00);         // Clear row 2.
+    SPI_Write2(0x04, 0x00);         // Clear row 3.
+    SPI_Write2(0x05, 0x00);         // Clear row 4.
+    SPI_Write2(0x06, 0x00);         // Clear row 5.
+    SPI_Write2(0x07, 0x00);         // Clear row 6.
+    SPI_Write2(0x08, 0x00);         // Clear row 7.
+    SPI_Write2(0x0F, 0x00);         // Disable display test
+    wait_ms(500);                   // 500 ms delay
+}
+/*
 void rxisr() {
     char c = pc.getc();
     // Add any characters that aren't the end of a command (semicolon) to the input buffer.
@@ -87,7 +126,7 @@
         commandRecv = 1;
     }
 }
-
+*/
 // Compute the average magnitude of a target frequency window vs. all other frequencies.
 void windowMean(float* magnitudes, int lowBin, int highBin, float* windowMean, float* otherMean)
 {
@@ -149,26 +188,17 @@
                    frequencyToBin(frequencyWindow[i+1]),
                    &intensity,
                    &otherMean);
-            printf("%d: %d \n",i, (int)intensity);
+            printf("%d: %d \n",i, ((int)intensity)/3);
+            int j = ((int)intensity)/5;
+            j = ((j>280) ? 280 : j)/40;
+            printf("%d \n",j);
+
+            SPI_Write2(8-i,tempCurrentLevel[j]);
 
             
 
-
-
-
-
-/*          
- 
-        intensity_4x8[i*4+offset]=intensity;
-        offset=(offset+1)%4;
-*/    }
-/*
-    for (int k=0;k<32;k++){
-    printf("%d: %d ",k, intensity_4x8[k]);
-    printf("\n");
-
     }
-*/}
+}
 
 
 ////////////////////////////////////////////////////////////////////////////////
@@ -216,7 +246,7 @@
 // - Set the sample rate of the device to 400 hertz.
 //
 ////////////////////////////////////////////////////////////////////////////////
-
+/*
 void parseCommand(char* command)
 {
     if (strcmp(command, "GET MAGNITUDES") == 0) {
@@ -278,25 +308,34 @@
         }
     }
 }
-
+*/
 ////////////////////////////////////////////////////////////////////////////////
 // MAIN FUNCTION
 ////////////////////////////////////////////////////////////////////////////////
 
 int main()
 {
-    NVIC_set_all_irq_priorities(1);
-    NVIC_SetPriority(UART0_IRQn, 0);
+    cs = 1;                         // CS initially High
+    spi.format(8,0);                // 8-bit format, mode 0,0
+    spi.frequency(1000000);         // SCLK = 1 MHz
+    Init_MAX7219();                 // Initialize the LED controller
+    MMA8451Q acc(SDA, SCL, MMA8451_I2C_ADDRESS);
+
+ //   NVIC_set_all_irq_priorities(1);
+ //   NVIC_SetPriority(UART0_IRQn, 0);
     // Set up serial port.
-    pc.baud (9600);
-    pc.attach(&rxisr);
+  //  pc.baud (9600);
+   // pc.attach(&rxisr);
+   /*
 #ifndef RGBW_ext
     led.invertpwm(1); //On-board KL25Z RGB LED uses common anode.
 #endif 
+*/
     // Clear the input command buffer
-    memset(commandBuffer, 0, sizeof(commandBuffer));
-
+  //  memset(commandBuffer, 0, sizeof(commandBuffer));
+    
     // Initialize spectrum display
+    
     spectrumSetup();
 
     // Begin sampling audio
@@ -350,7 +389,7 @@
             // Restart audio sampling.
             samplingBegin();
         }
-
+/*
         // Parse any pending commands.
         if(commandRecv) {
 //            pc.attach(NULL);
@@ -360,5 +399,5 @@
             memset(commandBuffer, 0, sizeof(commandBuffer));
 //            pc.attach(&rxisr);
         }
-    }
+*/      }
 }