LCD implementation of our project.

Dependencies:   mbed mbed-rtos MLX90614

Committer:
ovidiup13
Date:
Wed Jun 03 17:42:47 2015 +0000
Revision:
10:97389d774ae1
Parent:
8:81ed1135ba02
working version, comment out thermo in main;

Who changed what in which revision?

UserRevisionLine numberNew contents of line
ovidiup13 3:688b62ff6474 1 #include "Compass.h"
ovidiup13 3:688b62ff6474 2
ovidiup13 4:024e6a9c2ebf 3 //constructor
ovidiup13 8:81ed1135ba02 4 Compass::Compass(ST7565 * lcd, Item * back, DigitalOut *gyro, DigitalOut *thermo)
ovidiup13 5:5b1a8ad6c187 5 {
ovidiup13 4:024e6a9c2ebf 6 this->title = " Compass";
ovidiup13 4:024e6a9c2ebf 7 this->st7565= lcd;
ovidiup13 4:024e6a9c2ebf 8 this->back = back;
ovidiup13 8:81ed1135ba02 9 this->gyro = gyro;
ovidiup13 8:81ed1135ba02 10 this->thermo = thermo;
ovidiup13 8:81ed1135ba02 11
ovidiup13 4:024e6a9c2ebf 12 ct = NULL;
ovidiup13 4:024e6a9c2ebf 13 }
ovidiup13 4:024e6a9c2ebf 14
ovidiup13 4:024e6a9c2ebf 15 //display function, starts the thread and displays the compass
ovidiup13 5:5b1a8ad6c187 16 void Compass::display(void)
ovidiup13 5:5b1a8ad6c187 17 {
ovidiup13 3:688b62ff6474 18 //create a new thread to get and update compass - do later
ovidiup13 5:5b1a8ad6c187 19 if(ct != NULL) return; //thread is already running
ovidiup13 8:81ed1135ba02 20 thermo->write(0); gyro->write(1);
ovidiup13 5:5b1a8ad6c187 21 ct = new Thread(&Compass::ct_start, this);
ovidiup13 4:024e6a9c2ebf 22 ct->signal_set(START_THREAD);
ovidiup13 4:024e6a9c2ebf 23 }
ovidiup13 4:024e6a9c2ebf 24
ovidiup13 4:024e6a9c2ebf 25 //trigger for starting the thread
ovidiup13 5:5b1a8ad6c187 26 void Compass::ct_start(void const *args)
ovidiup13 5:5b1a8ad6c187 27 {
ovidiup13 4:024e6a9c2ebf 28 Compass *c = (Compass*)args;
ovidiup13 4:024e6a9c2ebf 29 c->compass_update();
ovidiup13 3:688b62ff6474 30 }
ovidiup13 3:688b62ff6474 31
ovidiup13 4:024e6a9c2ebf 32 //function that the thread runs, waits for the signal before starting
ovidiup13 5:5b1a8ad6c187 33 void Compass::compass_update()
ovidiup13 5:5b1a8ad6c187 34 {
ovidiup13 4:024e6a9c2ebf 35 ct->signal_wait(START_THREAD);
ovidiup13 7:11675c1dce4f 36 initSensors();
ovidiup13 7:11675c1dce4f 37
ovidiup13 7:11675c1dce4f 38 SensorState_t state;
ovidiup13 4:024e6a9c2ebf 39 //get degrees from other functions and display the compass
ovidiup13 5:5b1a8ad6c187 40 while(true) {
ovidiup13 7:11675c1dce4f 41 #ifdef LSM303_on
ovidiup13 7:11675c1dce4f 42 LSM303(&state);
ovidiup13 7:11675c1dce4f 43 #endif
ovidiup13 8:81ed1135ba02 44 draw_compass(state.heading - 33); //add offset
ovidiup13 5:5b1a8ad6c187 45 Thread::wait(30);
ovidiup13 4:024e6a9c2ebf 46 st7565->clear();
ovidiup13 4:024e6a9c2ebf 47 }
ovidiup13 4:024e6a9c2ebf 48 }
ovidiup13 4:024e6a9c2ebf 49
ovidiup13 4:024e6a9c2ebf 50 //update function handles updates from the user - cancels the thread
ovidiup13 4:024e6a9c2ebf 51 //and returns to main menu
ovidiup13 5:5b1a8ad6c187 52 void Compass::update(char c)
ovidiup13 5:5b1a8ad6c187 53 {
ovidiup13 3:688b62ff6474 54 //kill thread and go back
ovidiup13 5:5b1a8ad6c187 55 if(c == 'y') {
ovidiup13 4:024e6a9c2ebf 56 ct->terminate();
ovidiup13 8:81ed1135ba02 57 free(ct); ct = NULL;
ovidiup13 8:81ed1135ba02 58 gyro->write(0);
ovidiup13 5:5b1a8ad6c187 59 st7565->clear();//clear everything
ovidiup13 3:688b62ff6474 60 this->setSelectedScreen(back);
ovidiup13 5:5b1a8ad6c187 61 } else
ovidiup13 5:5b1a8ad6c187 62 return;
ovidiup13 3:688b62ff6474 63 }
ovidiup13 3:688b62ff6474 64
ovidiup13 3:688b62ff6474 65 //get direction
ovidiup13 5:5b1a8ad6c187 66 char * get_direction(double degrees)
ovidiup13 5:5b1a8ad6c187 67 {
ovidiup13 3:688b62ff6474 68 if(degrees >= 330 || degrees < 30)
ovidiup13 3:688b62ff6474 69 return "East";
ovidiup13 3:688b62ff6474 70 else if(degrees >= 30 && degrees <= 60)
ovidiup13 3:688b62ff6474 71 return "North-East";
ovidiup13 3:688b62ff6474 72 else if(degrees >= 60 && degrees < 120)
ovidiup13 3:688b62ff6474 73 return "North";
ovidiup13 3:688b62ff6474 74 else if(degrees >= 120 && degrees < 150)
ovidiup13 3:688b62ff6474 75 return "North-West";
ovidiup13 3:688b62ff6474 76 else if(degrees >= 150 && degrees < 210)
ovidiup13 3:688b62ff6474 77 return "West";
ovidiup13 3:688b62ff6474 78 else if(degrees >= 210 && degrees < 240)
ovidiup13 3:688b62ff6474 79 return "South-West";
ovidiup13 3:688b62ff6474 80 else if(degrees >= 240 && degrees < 300)
ovidiup13 3:688b62ff6474 81 return "South";
ovidiup13 3:688b62ff6474 82 else
ovidiup13 3:688b62ff6474 83 return "South-East";
ovidiup13 3:688b62ff6474 84 }
ovidiup13 3:688b62ff6474 85
ovidiup13 3:688b62ff6474 86 //function that draws the compass on the screen
ovidiup13 5:5b1a8ad6c187 87 void Compass::draw_compass(double degrees)
ovidiup13 5:5b1a8ad6c187 88 {
ovidiup13 3:688b62ff6474 89 //variables
ovidiup13 3:688b62ff6474 90 int x_temp, y_temp;
ovidiup13 8:81ed1135ba02 91 double rad = (degrees + 90) * M_PI / 180; //radians
ovidiup13 5:5b1a8ad6c187 92
ovidiup13 3:688b62ff6474 93 //calculate coordinates to point
ovidiup13 3:688b62ff6474 94 x_temp = X_CENTER + (int) (POINTER_LENGTH * cos(rad));
ovidiup13 3:688b62ff6474 95 y_temp = Y_CENTER + (int) (POINTER_LENGTH * (-sin(rad)));
ovidiup13 5:5b1a8ad6c187 96
ovidiup13 3:688b62ff6474 97 //draw the main circle and small one
ovidiup13 3:688b62ff6474 98 st7565->drawcircle(X_CENTER, Y_CENTER, RADIUS, 20);
ovidiup13 3:688b62ff6474 99 st7565->drawcircle(X_CENTER, Y_CENTER, 2, 20);
ovidiup13 5:5b1a8ad6c187 100
ovidiup13 3:688b62ff6474 101 //draw the lines
ovidiup13 3:688b62ff6474 102 st7565->drawline(X_CENTER, Y_CENTER, x_temp, y_temp, 20); //draw line from center to coordinates
ovidiup13 3:688b62ff6474 103 st7565->drawline(X_CENTER, Y_CENTER - RADIUS, X_CENTER, Y_CENTER - 15, 20); //north line
ovidiup13 3:688b62ff6474 104 st7565->drawline(X_CENTER, Y_CENTER + RADIUS, X_CENTER, Y_CENTER + 15, 20); //south line
ovidiup13 3:688b62ff6474 105 st7565->drawline(X_CENTER + RADIUS, Y_CENTER, X_CENTER + 15, Y_CENTER, 20); //east line
ovidiup13 3:688b62ff6474 106 st7565->drawline(X_CENTER - RADIUS, Y_CENTER, X_CENTER - 15, Y_CENTER, 20); //west line
ovidiup13 5:5b1a8ad6c187 107
ovidiup13 3:688b62ff6474 108 //draw the initials
ovidiup13 3:688b62ff6474 109 st7565->drawstring(X_CENTER - 2, 1, "N");
ovidiup13 3:688b62ff6474 110 st7565->drawstring(X_CENTER - 2, 7, "S");
ovidiup13 3:688b62ff6474 111 st7565->drawstring(X_CENTER + 21, 4, "E");
ovidiup13 3:688b62ff6474 112 st7565->drawstring(X_CENTER - 25, 4, "W");
ovidiup13 5:5b1a8ad6c187 113
ovidiup13 3:688b62ff6474 114 //display pointing direction
ovidiup13 3:688b62ff6474 115 st7565->drawstring(0, 2, "Pointing:");
ovidiup13 8:81ed1135ba02 116 char * pointer = get_direction(degrees + 90);
ovidiup13 3:688b62ff6474 117 st7565->drawstring(0, 4, pointer);
ovidiup13 5:5b1a8ad6c187 118
ovidiup13 3:688b62ff6474 119 //display degrees and radians in bottom left corner
ovidiup13 3:688b62ff6474 120 char s_deg[10], s_rad[10];
ovidiup13 8:81ed1135ba02 121 sprintf(s_deg, "DEG:%d", (int)degrees % 360);
ovidiup13 8:81ed1135ba02 122 sprintf(s_rad, "RAD:%0.2f", (int)degrees % 360 * M_PI / 180.0);
ovidiup13 3:688b62ff6474 123 st7565->drawstring(1, 6, s_deg);
ovidiup13 3:688b62ff6474 124 st7565->drawstring(1, 7, s_rad);
ovidiup13 5:5b1a8ad6c187 125
ovidiup13 3:688b62ff6474 126 st7565->display();
ovidiup13 3:688b62ff6474 127 }