LCD implementation of our project.

Dependencies:   mbed mbed-rtos MLX90614

Files at this revision

API Documentation at this revision

Comitter:
ovidiup13
Date:
Thu May 28 16:07:00 2015 +0000
Parent:
4:024e6a9c2ebf
Child:
6:49a007861c76
Commit message:
added levelmeter functionality and threading

Changed in this revision

Compass.cpp Show annotated file Show diff for this revision Revisions of this file
Compass.h Show annotated file Show diff for this revision Revisions of this file
Item.h Show annotated file Show diff for this revision Revisions of this file
LevelMeter.cpp Show annotated file Show diff for this revision Revisions of this file
LevelMeter.h 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
--- 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
--- a/Compass.h	Mon May 25 14:46:39 2015 +0000
+++ b/Compass.h	Thu May 28 16:07:00 2015 +0000
@@ -13,9 +13,6 @@
 #define POINTER_LENGTH 10
 #define RADIUS 19
 
-#define START_THREAD 1
-#define PAUSE_THREAD 2
-
 class Compass: public Item {
     public:
         //inherited functions
--- a/Item.h	Mon May 25 14:46:39 2015 +0000
+++ b/Item.h	Thu May 28 16:07:00 2015 +0000
@@ -6,6 +6,12 @@
 #define LEFT_MARGIN 5
 #define DEFAULT_COLOR 20
 
+#define SCREEN_WIDTH 128
+#define SCREEN_HEIGHT 64
+
+#define START_THREAD 1
+#define PAUSE_THREAD 2
+
 class Item {
     public:
         //name
--- a/LevelMeter.cpp	Mon May 25 14:46:39 2015 +0000
+++ b/LevelMeter.cpp	Thu May 28 16:07:00 2015 +0000
@@ -1,25 +1,75 @@
 #include "LevelMeter.h"
 
-void LevelMeter::display(void){
-    draw_circles(0,0);
+LevelMeter::LevelMeter(ST7565 *lcd, Item * back)
+{
+    this->st7565 = lcd;
+    this->title = " Level meter";
+    this->back = back;
+    lt = NULL;
+}
+
+void LevelMeter::display(void)
+{
+    //create a new thread to get and update compass - do later
+    if(lt != NULL) return;//thread is already running
+    lt = new Thread(&LevelMeter::lt_start, this);
+    lt->signal_set(START_THREAD);
 }
 
-void LevelMeter::update(char c){
-    if(c == 'y')
-        this->setSelectedScreen(back);
+//trigger for starting the thread
+void LevelMeter::lt_start(void const *args)
+{
+    LevelMeter *l = (LevelMeter*)args;
+    l->update_cross();
 }
 
-void LevelMeter::draw_circles(double rX, double rY){
-    
-    int X1 = 95, Y1 = 35, X2 = 30, Y2 = 35; //need to calculate these
+void LevelMeter::update_cross()
+{
+    lt->signal_wait(START_THREAD); //wait for signal to start thread
+    //draw the static circle
+    int i = 0, j = 0;
+    while(true) {
+        draw_elements(i++, j++);
+        //do something
+        Thread::wait(30);
+        st7565->clear();
+    }
+}
+
+void LevelMeter::draw_elements(double rx, double ry){
+    int cross_x, cross_y;
     
     //draw the circles
-    st7565->fillcircle(X1, Y1, RADIUS_lvl, 20);
-    st7565->drawcircle(X2, Y2, RADIUS_lvl, 1);
-    
-    //draw cross
+    st7565->drawcircle(X0, Y0, RADIUS_lvl, 1);
+
+    //draw middle cross
     st7565->drawline(X0 - 2, Y0, X0 + 2, Y0, 20);
     st7565->drawline(X0, Y0 - 2, X0, Y0 + 2, 20);
     
+    //check if rotation in negative interval
+    if(rx >= 270) rx -= 360;
+    if(ry >= 270) ry -= 360;
+    
+    //calculate coordinates
+    cross_x = (int) ((rx * SCREEN_WIDTH)/360.0) + 63;
+    cross_y = (int) ((ry * SCREEN_HEIGHT)/360.0) + 35;
+    
+    printf("Coordinates for cross are: %d, %d\n", cross_x, cross_y);
+    
+    //draw the cross
+    st7565->drawline(cross_x, cross_y - RADIUS_lvl/2, cross_x, cross_y + RADIUS_lvl/2, DEFAULT_COLOR);
+    st7565->drawline(cross_x - RADIUS_lvl/2, cross_y, cross_x + RADIUS_lvl/2, cross_y, DEFAULT_COLOR);
+    
+    //display
     st7565->display();
+}
+
+void LevelMeter::update(char c)
+{
+    if(c == 'y') {
+        lt->terminate();
+        free(lt); lt = NULL;
+        st7565->clear();
+        this->setSelectedScreen(back);
+    }
 }
\ No newline at end of file
--- a/LevelMeter.h	Mon May 25 14:46:39 2015 +0000
+++ b/LevelMeter.h	Thu May 28 16:07:00 2015 +0000
@@ -9,18 +9,17 @@
 class LevelMeter: public Item {
     public:
         //constructors
-        LevelMeter(ST7565 *lcd, Item * back){
-            this->st7565 = lcd;
-            this->title = " Level meter";
-            this->back = back;
-        }
+        LevelMeter(ST7565 *lcd, Item * back);
         
         //inherited functions
         virtual void display(void);
         virtual void update(char c);
         
         private:
-            void draw_circles(double rX, double rY);
-            void update_degrees(double degrees);
+            Thread *lt;
+            
+            void draw_elements(double rx, double ry);
+            static void lt_start(void const *args);
+            void update_cross(void);
         
 };
\ No newline at end of file
--- a/main.cpp	Mon May 25 14:46:39 2015 +0000
+++ b/main.cpp	Thu May 28 16:07:00 2015 +0000
@@ -2,11 +2,11 @@
 #include "st7565LCD.h"
 
 //define mbed pins
-#define _MOSI p5
-#define _SCLK p7
+#define _MOSI p11
+#define _SCLK p13
 #define _RST p24
 #define _A0 p8
-#define _CS p6
+#define _CS p12
 
 ST7565 st7565(_MOSI, _SCLK, _CS, _RST, _A0); // mosi, sclk, cs, rst, a0
 Serial pc(USBTX, USBRX); //rx, tx
@@ -19,6 +19,7 @@
     //create main menu
     Menu * main_menu = new Menu(" Main Menu", &st7565);
     
+    /*
     //create distance screens
     Measure *distance = new Measure(" Distance", &st7565, main_menu);
     distance->setDescription("Select Start from the menu below to start laser.");
@@ -32,6 +33,8 @@
     distance2->setNext(" Select", distance3);
     distance3->setNext(" Start again", distance);
     main_menu->addItem(distance);
+    */
+    
     
     //create point-to-point screens
     Measure *p2p = new Measure(" Point-to-Point", &st7565, main_menu);
@@ -72,9 +75,11 @@
     thermo3->setNext(" Start", thermo);
     main_menu->addItem(thermo);
     
+    /*
     //create settings menu
     Menu *settings = new Menu(" Settings", &st7565);
     main_menu->addItem(settings);
+    */
     
     //create header object
     Header * header = new Header(70, "", &st7565);