LCD implementation of our project.

Dependencies:   mbed mbed-rtos MLX90614

Committer:
ovidiup13
Date:
Wed Jun 03 12:22:46 2015 +0000
Revision:
7:11675c1dce4f
Parent:
5:5b1a8ad6c187
Child:
8:81ed1135ba02
updated header, cleaned up menu, fixed controls for device

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