Finished project.

Dependencies:   X_NUCLEO_COMMON

Fork of ReferredCoursework2016 by Stage-1 Students SoCEM

Files at this revision

API Documentation at this revision

Comitter:
J_Satchell
Date:
Thu Aug 17 06:58:29 2017 +0000
Parent:
90:38dfa3f350aa
Commit message:
Added mutex

Changed in this revision

buffer.cpp Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
sensor.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/buffer.cpp	Thu Aug 17 02:42:30 2017 +0000
+++ b/buffer.cpp	Thu Aug 17 06:58:29 2017 +0000
@@ -19,23 +19,25 @@
 int32_t averageY;
 int32_t averageZ;
 
+Mutex mutex2;
 //Pushes data on to buffer using modulo operation.
 void log_push(AccelData data) {
-
-  rear = (rear + 1) % 10;
+    mutex2.lock();
 
-  if (rear == front) {
-    front = (front + 1) % 10;
+    rear = (rear + 1) % 10;
+
+    if (rear == front) {
+     front = (front + 1) % 10;
   }
 
-  circular_buffer_array[rear] = data;
-
+    circular_buffer_array[rear] = data;
+     mutex2.unlock();
 }
 
 //Retrieves a single data struct using the argument to specify which.
 AccelData log_get(int index)
 {
-
+    
   AccelData record;
   record = circular_buffer_array[(front + index) % 10];
 
@@ -50,7 +52,7 @@
 void print_averages(){
 
     while(1){
-
+         mutex2.lock();
     for (int i = 0; i < 10; i++) {
 
       AccelData entry = log_get(i);
@@ -75,5 +77,6 @@
     sumX = 0;
     sumY = 0;
     sumZ = 0;
+    mutex2.unlock();
 }
 }
--- a/main.cpp	Thu Aug 17 02:42:30 2017 +0000
+++ b/main.cpp	Thu Aug 17 06:58:29 2017 +0000
@@ -12,13 +12,15 @@
   printf("\r\n--- Starting new run ---\r\n");
 
   wait(3);
-
+  
+  sensorThread.start(sensor_run);
+  Thread::wait(0.1);  
+  
   while (1) {
-     
-    sensorThread.start(sensor_run);
+         
     print_averages();     
     
-    wait(0.1);
+    Thread::wait(0.1);
   }
 
 
--- a/sensor.cpp	Thu Aug 17 02:42:30 2017 +0000
+++ b/sensor.cpp	Thu Aug 17 06:58:29 2017 +0000
@@ -6,6 +6,8 @@
 #include "buffer.h"
 #include "sensor.h"
 
+Mutex mutex;
+
 /* Instantiate the expansion board */
 static X_NUCLEO_IKS01A1 *mems_expansion_board = X_NUCLEO_IKS01A1::Instance(D14, D15);
 
@@ -18,15 +20,17 @@
 
 void sensor_run(){
      while (1) {
-   
-    accelerometer->Get_X_Axes(axes);
+          mutex.lock();
+    
+          accelerometer->Get_X_Axes(axes);
 
-    AccelData d;
-    d.x = axes[0];
-    d.y = axes[1];
-    d.z = axes[2];  
+          AccelData d;
+          d.x = axes[0];
+          d.y = axes[1];
+          d.z = axes[2];  
     
-    log_push(d);
+          log_push(d);
+          mutex.unlock();
     }
 
 }
\ No newline at end of file