LCD implementation of our project.

Dependencies:   mbed mbed-rtos MLX90614

Compass.cpp

Committer:
ovidiup13
Date:
2015-05-25
Revision:
4:024e6a9c2ebf
Parent:
3:688b62ff6474
Child:
5:5b1a8ad6c187

File content as of revision 4:024e6a9c2ebf:

#include "Compass.h"

//constructor
Compass::Compass(ST7565 * lcd, Item * back) {
    this->title = " Compass";
    this->st7565= lcd;
    this->back = back;
    ct = NULL;
}

//display function, starts the thread and displays the compass
void Compass::display(void){
    //create a new thread to get and update compass - do later
    ct = new Thread(&Compass::ct_start, this, osPriorityNormal, 1024);
    ct->signal_set(START_THREAD);
}

//trigger for starting the thread
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(){
    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){
        draw_compass(i++);
        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){
    //kill thread and go back
    if(c == 'y'){
        ct->terminate();
        this->setSelectedScreen(back);
    }
}

//get direction
char * get_direction(double degrees){
    if(degrees >= 330 || degrees < 30)
        return "East";
    else if(degrees >= 30 && degrees <= 60)
        return "North-East";
    else if(degrees >= 60 && degrees < 120)
        return "North";
    else if(degrees >= 120 && degrees < 150)
        return "North-West";
    else if(degrees >= 150 && degrees < 210)
        return "West";
    else if(degrees >= 210 && degrees < 240)
        return "South-West";
    else if(degrees >= 240 && degrees < 300)
        return "South";
    else
        return "South-East";
}

//function that draws the compass on the screen
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();
}