LCD implementation of our project.

Dependencies:   mbed mbed-rtos MLX90614

Revision:
5:5b1a8ad6c187
Parent:
4:024e6a9c2ebf
Child:
7:11675c1dce4f
--- a/Compass.cpp	Mon May 25 14:46:39 2015 +0000
+++ b/Compass.cpp	Thu May 28 16:07:00 2015 +0000
@@ -1,7 +1,8 @@
 #include "Compass.h"
 
 //constructor
-Compass::Compass(ST7565 * lcd, Item * back) {
+Compass::Compass(ST7565 * lcd, Item * back)
+{
     this->title = " Compass";
     this->st7565= lcd;
     this->back = back;
@@ -9,44 +10,53 @@
 }
 
 //display function, starts the thread and displays the compass
-void Compass::display(void){
+void Compass::display(void)
+{
     //create a new thread to get and update compass - do later
-    ct = new Thread(&Compass::ct_start, this, osPriorityNormal, 1024);
+    if(ct != NULL) return; //thread is already running
+    ct = new Thread(&Compass::ct_start, this);
     ct->signal_set(START_THREAD);
 }
 
 //trigger for starting the thread
-void Compass::ct_start(void const *args){
+void Compass::ct_start(void const *args)
+{
     Compass *c = (Compass*)args;
     c->compass_update();
 }
 
 //function that the thread runs, waits for the signal before starting
-void Compass::compass_update(){
+void Compass::compass_update()
+{
     ct->signal_wait(START_THREAD);
-    //lock the mutex so that no other thread may access the lcd
-    
+
     int i = 0;
     //get degrees from other functions and display the compass
-    while(true){
+    while(true) {
         draw_compass(i++);
+        Thread::wait(30);
         st7565->clear();
-        Thread::wait(30);
     }
 }
 
 //update function handles updates from the user - cancels the thread
 //and returns to main menu
-void Compass::update(char c){
+void Compass::update(char c)
+{
     //kill thread and go back
-    if(c == 'y'){
+    if(c == 'y') {
         ct->terminate();
+        free(ct);
+        ct = NULL;
+        st7565->clear();//clear everything
         this->setSelectedScreen(back);
-    }
+    } else
+        return;
 }
 
 //get direction
-char * get_direction(double degrees){
+char * get_direction(double degrees)
+{
     if(degrees >= 330 || degrees < 30)
         return "East";
     else if(degrees >= 30 && degrees <= 60)
@@ -66,43 +76,44 @@
 }
 
 //function that draws the compass on the screen
-void Compass::draw_compass(double degrees){
+void Compass::draw_compass(double degrees)
+{
     //variables
     int x_temp, y_temp;
     double rad = degrees * M_PI / 180; //radians
-    
+
     //calculate coordinates to point
     x_temp = X_CENTER + (int) (POINTER_LENGTH * cos(rad));
     y_temp = Y_CENTER + (int) (POINTER_LENGTH * (-sin(rad)));
-    
+
     //draw the main circle and small one
     st7565->drawcircle(X_CENTER, Y_CENTER, RADIUS, 20);
     st7565->drawcircle(X_CENTER, Y_CENTER, 2, 20);
-    
+
     //draw the lines
     st7565->drawline(X_CENTER, Y_CENTER, x_temp, y_temp, 20); //draw line from center to coordinates
     st7565->drawline(X_CENTER, Y_CENTER - RADIUS, X_CENTER, Y_CENTER - 15, 20); //north line
     st7565->drawline(X_CENTER, Y_CENTER + RADIUS, X_CENTER, Y_CENTER + 15, 20); //south line
     st7565->drawline(X_CENTER + RADIUS, Y_CENTER, X_CENTER + 15, Y_CENTER, 20); //east line
     st7565->drawline(X_CENTER - RADIUS, Y_CENTER, X_CENTER - 15, Y_CENTER, 20); //west line
-    
+
     //draw the initials
     st7565->drawstring(X_CENTER - 2, 1, "N");
     st7565->drawstring(X_CENTER - 2, 7, "S");
     st7565->drawstring(X_CENTER + 21, 4, "E");
     st7565->drawstring(X_CENTER - 25, 4, "W");
-    
+
     //display pointing direction
     st7565->drawstring(0, 2, "Pointing:");
     char * pointer = get_direction(degrees);
     st7565->drawstring(0, 4, pointer);
-    
+
     //display degrees and radians in bottom left corner
     char s_deg[10], s_rad[10];
     sprintf(s_deg, "DEG:%g", degrees);
     sprintf(s_rad, "RAD:%.2g", rad);
     st7565->drawstring(1, 6, s_deg);
     st7565->drawstring(1, 7, s_rad);
-    
+
     st7565->display();
 }
\ No newline at end of file