Slight Mod

Dependencies:   mbed-dsp mbed

Fork of Hat_Board_v5 by John Scharf

Revision:
4:e9df42113893
Parent:
3:8334f137c151
Child:
5:ef42f4648198
--- a/main.cpp	Fri Mar 21 01:13:05 2014 +0000
+++ b/main.cpp	Thu Apr 03 07:31:24 2014 +0000
@@ -1,11 +1,14 @@
-//Hat Board Test by James Cooke 3/18/2014
-//Goal of  program: read and output data from LIS3DH accelerometer
-//Started with Si_4088_PC working program: able to get data from Si1142 on new hat board
+//Hat Board v5 by Cooke Scharf 4/2/2014
 
 #include "mbed.h"
 #include "SI_LIS.h"
 #include <time.h>
 
+#include "arm_math.h"
+#include "math_helper.h"
+#include "dsp.h"
+#include "arm_const_structs.h"
+
 DigitalOut  myled3(LED3);
 DigitalOut  myled4(LED4);
 Serial      pc(USBTX,USBRX);
@@ -23,25 +26,50 @@
 float       dataX_fl;
 float       dataY_fl;
 float       dataZ_fl;
-float       accel_fl;
+//float       accel_fl;
+
+////////////////////////////////////////////
+///////////////////////////////////////////
+#define         MAX_BLOCKSIZE   4096
+uint32_t        fftsize = MAX_BLOCKSIZE/2; 
+const static    arm_cfft_instance_f32 *S;  
+
+///////////////////////////////////////////
+long int        BAUD_RATE = 230400;
+void baud(int baudrate)        
+{
+    Serial s(USBTX,USBRX);
+    s.baud(baudrate);
+}
+
+///////////////////////////////////////////
+unsigned short  i,j,k, m;
+float32_t       temp;   
+float32_t       temp2;
+float           sclr;
+
+float32_t       v[MAX_BLOCKSIZE], w[MAX_BLOCKSIZE], acc[MAX_BLOCKSIZE];
+float32_t       testoutput[MAX_BLOCKSIZE/2];
+float32_t       testoutputb[26], testoutputc[26], outputacc[26];
+
+////////////////////////////////////////////
 
 int main()
 {
-    // To test:  Block data update (CTRL_REG4);  1.25 KHz data;  low power vs normal power
-
-    myled4 = 0;  // ODD: if this line not included, there is a compiler "internal error"
+    myled4 = 0;
     pc.baud(460800);
     Init_Accel();   // starts LIS3DH
     restart();      // starts Si1142
-
     wait_ms(30);
     command (PS_AUTO);   //start measuring
     wait (0.5);
 
+    j = 0;
+    
     while(1) {
         if(!int_pin) {
-            t.reset();
-            t.start();
+            //t.reset();
+            //t.start();
 
             write_reg(IRQ_STATUS,0x04);  // clear the interrupt.
 
@@ -49,30 +77,139 @@
             reading_IR  = rx_data[1] << 8 | rx_data[0];
             reading_660 = rx_data[3] << 8 | rx_data[1];
 
-            pc.printf ("%d, %d, ", reading_IR,reading_660);
-
             Get_Accel_Reg_6 (0x28);
 
             dataX = accel_data[1] << 8 | accel_data[0];
+            dataY = accel_data[3] << 8 | accel_data[2];
+            dataZ = accel_data[5] << 8 | accel_data[4];
+            
+            k = (j/2)*2;
+            if (j==k) {
+            v[j] = (float32_t)reading_IR;
+            v[j+1] = 0;
+            w[j] = (float32_t)reading_660;
+            w[j+1] = 0;
+
             dataX_fl = (float) dataX;
-            dataY = accel_data[3] << 8 | accel_data[2];
             dataY_fl = (float) dataY;
-            dataZ = accel_data[5] << 8 | accel_data[4];
             dataZ_fl = (float) dataZ;
-            accel_fl = sqrt( (dataX_fl*dataX_fl) + (dataY_fl*dataY_fl) + (dataZ_fl*dataZ_fl) );
-            pc.printf ("%6.0f ",accel_fl);
+            
+            acc[j]   = sqrt( (dataX_fl*dataX_fl) + (dataY_fl*dataY_fl) + (dataZ_fl*dataZ_fl) );
+            acc[j+1] = 0;
+                        
+         }
+            pc.printf ("%4d  %5d  %5d  %5d  %5d  %5d\n\r", j, reading_IR, reading_660, dataX, dataY, dataZ);
+
+            j++;
+    
+            if(j==MAX_BLOCKSIZE)
+    {
+   
+        switch (fftsize) {
+        case 64:
+            S = & arm_cfft_sR_f32_len64;
+            break;
+        case 128:
+            S = & arm_cfft_sR_f32_len128;
+            break;
+        case 256:
+            S = & arm_cfft_sR_f32_len256;
+            break;
+        case 512:
+            S = & arm_cfft_sR_f32_len512;
+            break;
+        case 1024:
+            S = & arm_cfft_sR_f32_len1024;
+            break;
+        case 2048:
+            S = & arm_cfft_sR_f32_len2048;
+            break;
+        case 4096:
+            S = & arm_cfft_sR_f32_len4096;
+            break;
+        }
 
-            t.stop();
-            t_msec = t.read() * 1000;
-            printf("%6.3f msec\n", t_msec);
+/////////////////////////////////////////////////////////////////////
+        
+/////////////////////////////////////////////////////////////////////
+        float32_t   maxvaluea, maxvalueb, maxacc, rvalue, spo2;
+        uint32_t    testindexa, testindexb, indexacc;
+        
+        for (i = 0; i < 26; i++) {
+                                   pc.printf ("\n\r%4d  %10.2f  %10.2f  %10.2f\n\r", i, v[i], w[i], acc[i]);                                        
+                                    }
+                                    
+/////////////////////////////////////////////////////////////////////        
+        arm_cfft_f32(S, v, 0, 1);
+        arm_cmplx_mag_squared_f32(v, testoutput, fftsize); 
+        sclr = 1000000/testoutput[0];
+        arm_scale_f32(testoutput, sclr, testoutput, fftsize);
+    
+        for (i = 0; i < 26; i++) {
+                                   testoutputb[i] = testoutput[i];
+                                   if (i<4) {
+                                                testoutputb[i] = 0;
+                                                }   
+                                   pc.printf ("\n\r%4d  %10.2f\n\r", i, testoutputb[i]);                                        
+                                    }
+                                                                    // 805 nm
+        arm_max_f32(testoutputb, 26, &maxvalueb, &testindexb);      // 720 hz sampling / 4096 fft = 0.1758 hz bin spacing = 360 hz sampling / 2048 fft
+                                                                    // 1 beat-sec / 0.1758 = about 6 bins (HR = 60 bpm)
+                                                                    // 3.6667 beats-sec / 0.1758 hz bin spacing = about 21 bins (HR = 220 bpm)              
+        pc.printf ("\n\r\n\r%4d  %10.3f\n\r\n\r", testindexb, maxvalueb);
+                                   
+/////////////////////////////////////////////////////////////////////
+        arm_cfft_f32(S, w, 0, 1);
+        arm_cmplx_mag_squared_f32(w, testoutput, fftsize); 
+        sclr = 1000000/testoutput[0];
+        arm_scale_f32(testoutput, sclr, testoutput, fftsize);
+    
+        for (i = 0; i < 26; i++) {
+                                    testoutputc[i] = testoutput[i];
+                                    if (i<4) {
+                                                testoutputc[i] = 0;
+                                                }                              
+                                    }
+                                                                    // 660 nm
+        arm_max_f32(testoutputc, 26, &maxvaluea, &testindexa);      // 720 hz sampling / 4096 fft = 0.1758 hz bin spacing = 360 hz sampling / 2048 fft
+                                                                    // 1 beat-sec / 0.1758 = about 6 bins (HR = 60 bpm)
+                                                                    // 3.6667 beats-sec / 0.1758 hz bin spacing = about 21 bins (HR = 220 bpm)        
+        pc.printf ("\n\r\n\r%4d  %10.3f\n\r\n\r", testindexa, maxvaluea);
+        
+        rvalue = maxvaluea/maxvalueb;
+        spo2 = -22.6 * rvalue + 108;
+        
+        pc.printf ("\n\r\n\r%5.2f  %5.2f\n\r\n\r", rvalue, spo2);
+               
+/////////////////////////////////////////////////////////////////////
+        arm_cfft_f32(S, acc, 0, 1);
+        arm_cmplx_mag_squared_f32(acc, testoutput, fftsize); 
+        sclr = 1000000/testoutput[0];
+        arm_scale_f32(testoutput, sclr, testoutput, fftsize);
+    
+        for (i = 0; i < 26; i++) {
+                                   outputacc[i] = testoutput[i];
+                                   if (i<4) {
+                                                outputacc[i] = 0;
+                                                }   
+                                   pc.printf ("\n\r%4d  %10.2f\n\r", i, outputacc[i]);                                        
+                                    }
+                                                                    // accelerometer
+        arm_max_f32(outputacc, 26, &maxacc, &indexacc);             // 720 hz sampling / 4096 fft = 0.1758 hz bin spacing = 360 hz sampling / 2048 fft
+                                                                    // 1 beat-sec / 0.1758 = about 6 bins (HR = 60 bpm)
+                                                                    // 3.6667 beats-sec / 0.1758 hz bin spacing = about 21 bins (HR = 220 bpm)              
+        pc.printf ("\n\r\n\r%4d  %10.3f\n\r\n\r", indexacc, maxacc);
+
+/////////////////////////////////////////////////////////////////////
+
+        j = 0;
+
+/////////////////////////////////////////////////////////////////////
+    
+    }
+    
+//----------------------------------------------------------
         }
     }
 }
-/*
-Notes on RJ-45: Pin 1: Vcc  Pin 2: GND  Pin 3: SDA > 9 on 4088
-                Pin 4: SCL >> 10 on 4088  Pin 6: INT > 8 on 4088
 
-Bluetooth:  Tx on 4088: Pin 37 >> RX   Rx on 4088: Pin 31 >> TX
-PC COMM port on Square board: 12
-
-*/
\ No newline at end of file