Code used in the SICE E210/P442 lab practical

Dependencies:   ADXL362 TSL2561 mbed

Revision:
0:69b431195dc4
Child:
1:f5c993193adc
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Wed Apr 25 19:11:51 2018 +0000
@@ -0,0 +1,54 @@
+// Code used for the SP18 E210/P442 Lab Practical
+#include "mbed.h"
+#include "ADXL362.h"
+#include "TSL2561.h"
+
+AnalogOut my_output(PA_4);
+
+// Interface pulled from ADXL362.cpp
+// ADXL362::ADXL362(PinName CS, PinName MOSI, PinName MISO, PinName SCK) :
+ADXL362 adxl362(PA_0,PA_7,PA_6,PA_1);
+TSL2561 lightsensor(PB_7,PB_6);
+
+
+#define PI        (3.141592653589793238462)
+#define AMPLITUDE (1.0)    // x * 3.3V
+#define PHASE     (PI * 1) // 2*pi is one period
+#define RANGE     (0x7FFF)
+#define OFFSET    (0x7FFF)
+
+// Configuration for sinewave output
+#define BUFFER_SIZE (360)
+uint16_t buffer[BUFFER_SIZE];
+
+void calculate_sinewave(void);
+
+int main() {
+    printf("Sinewave example\n");
+    calculate_sinewave();
+    adxl362.reset();
+    wait_ms(600); // we need to wait at least 500ms after ADXL362 reset
+    adxl362.set_mode(ADXL362::MEASUREMENT);
+    volatile int8_t x,y,z; 
+    volatile float light;
+    while(1) {
+        for (int i = 0; i < BUFFER_SIZE; i++) {
+            my_output.write_u16(buffer[i]);
+            wait_us(10);
+        }
+        x=adxl362.scanx_u8();
+        y=adxl362.scany_u8();
+        z=adxl362.scanz_u8();
+       // printf("Accelerometer x = %d y = %d z = %d\r\n",x,y,z);
+        // printf("Light sensor %f\n\r",lightsensor.lux());
+//        wait_ms(100);
+    }
+}
+
+// Create the sinewave buffer
+void calculate_sinewave(void){
+  for (int i = 0; i < BUFFER_SIZE; i++) {
+     double rads = (PI * i)/180.0; // Convert degree in radian
+     buffer[i] = (uint16_t)(AMPLITUDE * (RANGE * (cos(rads + PHASE))) + OFFSET);
+  }
+}