LCD implementation of our project.
Dependencies: mbed mbed-rtos MLX90614
Compass.cpp@4:024e6a9c2ebf, 2015-05-25 (annotated)
- Committer:
- ovidiup13
- Date:
- Mon May 25 14:46:39 2015 +0000
- Revision:
- 4:024e6a9c2ebf
- Parent:
- 3:688b62ff6474
- Child:
- 5:5b1a8ad6c187
updated compass with threading
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ovidiup13 | 3:688b62ff6474 | 1 | #include "Compass.h" |
ovidiup13 | 3:688b62ff6474 | 2 | |
ovidiup13 | 4:024e6a9c2ebf | 3 | //constructor |
ovidiup13 | 4:024e6a9c2ebf | 4 | Compass::Compass(ST7565 * lcd, Item * back) { |
ovidiup13 | 4:024e6a9c2ebf | 5 | this->title = " Compass"; |
ovidiup13 | 4:024e6a9c2ebf | 6 | this->st7565= lcd; |
ovidiup13 | 4:024e6a9c2ebf | 7 | this->back = back; |
ovidiup13 | 4:024e6a9c2ebf | 8 | ct = NULL; |
ovidiup13 | 4:024e6a9c2ebf | 9 | } |
ovidiup13 | 4:024e6a9c2ebf | 10 | |
ovidiup13 | 4:024e6a9c2ebf | 11 | //display function, starts the thread and displays the compass |
ovidiup13 | 3:688b62ff6474 | 12 | void Compass::display(void){ |
ovidiup13 | 3:688b62ff6474 | 13 | //create a new thread to get and update compass - do later |
ovidiup13 | 4:024e6a9c2ebf | 14 | ct = new Thread(&Compass::ct_start, this, osPriorityNormal, 1024); |
ovidiup13 | 4:024e6a9c2ebf | 15 | ct->signal_set(START_THREAD); |
ovidiup13 | 4:024e6a9c2ebf | 16 | } |
ovidiup13 | 4:024e6a9c2ebf | 17 | |
ovidiup13 | 4:024e6a9c2ebf | 18 | //trigger for starting the thread |
ovidiup13 | 4:024e6a9c2ebf | 19 | void Compass::ct_start(void const *args){ |
ovidiup13 | 4:024e6a9c2ebf | 20 | Compass *c = (Compass*)args; |
ovidiup13 | 4:024e6a9c2ebf | 21 | c->compass_update(); |
ovidiup13 | 3:688b62ff6474 | 22 | } |
ovidiup13 | 3:688b62ff6474 | 23 | |
ovidiup13 | 4:024e6a9c2ebf | 24 | //function that the thread runs, waits for the signal before starting |
ovidiup13 | 4:024e6a9c2ebf | 25 | void Compass::compass_update(){ |
ovidiup13 | 4:024e6a9c2ebf | 26 | ct->signal_wait(START_THREAD); |
ovidiup13 | 4:024e6a9c2ebf | 27 | //lock the mutex so that no other thread may access the lcd |
ovidiup13 | 4:024e6a9c2ebf | 28 | |
ovidiup13 | 4:024e6a9c2ebf | 29 | int i = 0; |
ovidiup13 | 4:024e6a9c2ebf | 30 | //get degrees from other functions and display the compass |
ovidiup13 | 4:024e6a9c2ebf | 31 | while(true){ |
ovidiup13 | 4:024e6a9c2ebf | 32 | draw_compass(i++); |
ovidiup13 | 4:024e6a9c2ebf | 33 | st7565->clear(); |
ovidiup13 | 4:024e6a9c2ebf | 34 | Thread::wait(30); |
ovidiup13 | 4:024e6a9c2ebf | 35 | } |
ovidiup13 | 4:024e6a9c2ebf | 36 | } |
ovidiup13 | 4:024e6a9c2ebf | 37 | |
ovidiup13 | 4:024e6a9c2ebf | 38 | //update function handles updates from the user - cancels the thread |
ovidiup13 | 4:024e6a9c2ebf | 39 | //and returns to main menu |
ovidiup13 | 3:688b62ff6474 | 40 | void Compass::update(char c){ |
ovidiup13 | 3:688b62ff6474 | 41 | //kill thread and go back |
ovidiup13 | 4:024e6a9c2ebf | 42 | if(c == 'y'){ |
ovidiup13 | 4:024e6a9c2ebf | 43 | ct->terminate(); |
ovidiup13 | 3:688b62ff6474 | 44 | this->setSelectedScreen(back); |
ovidiup13 | 4:024e6a9c2ebf | 45 | } |
ovidiup13 | 3:688b62ff6474 | 46 | } |
ovidiup13 | 3:688b62ff6474 | 47 | |
ovidiup13 | 3:688b62ff6474 | 48 | //get direction |
ovidiup13 | 3:688b62ff6474 | 49 | char * get_direction(double degrees){ |
ovidiup13 | 3:688b62ff6474 | 50 | if(degrees >= 330 || degrees < 30) |
ovidiup13 | 3:688b62ff6474 | 51 | return "East"; |
ovidiup13 | 3:688b62ff6474 | 52 | else if(degrees >= 30 && degrees <= 60) |
ovidiup13 | 3:688b62ff6474 | 53 | return "North-East"; |
ovidiup13 | 3:688b62ff6474 | 54 | else if(degrees >= 60 && degrees < 120) |
ovidiup13 | 3:688b62ff6474 | 55 | return "North"; |
ovidiup13 | 3:688b62ff6474 | 56 | else if(degrees >= 120 && degrees < 150) |
ovidiup13 | 3:688b62ff6474 | 57 | return "North-West"; |
ovidiup13 | 3:688b62ff6474 | 58 | else if(degrees >= 150 && degrees < 210) |
ovidiup13 | 3:688b62ff6474 | 59 | return "West"; |
ovidiup13 | 3:688b62ff6474 | 60 | else if(degrees >= 210 && degrees < 240) |
ovidiup13 | 3:688b62ff6474 | 61 | return "South-West"; |
ovidiup13 | 3:688b62ff6474 | 62 | else if(degrees >= 240 && degrees < 300) |
ovidiup13 | 3:688b62ff6474 | 63 | return "South"; |
ovidiup13 | 3:688b62ff6474 | 64 | else |
ovidiup13 | 3:688b62ff6474 | 65 | return "South-East"; |
ovidiup13 | 3:688b62ff6474 | 66 | } |
ovidiup13 | 3:688b62ff6474 | 67 | |
ovidiup13 | 3:688b62ff6474 | 68 | //function that draws the compass on the screen |
ovidiup13 | 3:688b62ff6474 | 69 | void Compass::draw_compass(double degrees){ |
ovidiup13 | 3:688b62ff6474 | 70 | //variables |
ovidiup13 | 3:688b62ff6474 | 71 | int x_temp, y_temp; |
ovidiup13 | 4:024e6a9c2ebf | 72 | double rad = degrees * M_PI / 180; //radians |
ovidiup13 | 3:688b62ff6474 | 73 | |
ovidiup13 | 3:688b62ff6474 | 74 | //calculate coordinates to point |
ovidiup13 | 3:688b62ff6474 | 75 | x_temp = X_CENTER + (int) (POINTER_LENGTH * cos(rad)); |
ovidiup13 | 3:688b62ff6474 | 76 | y_temp = Y_CENTER + (int) (POINTER_LENGTH * (-sin(rad))); |
ovidiup13 | 3:688b62ff6474 | 77 | |
ovidiup13 | 3:688b62ff6474 | 78 | //draw the main circle and small one |
ovidiup13 | 3:688b62ff6474 | 79 | st7565->drawcircle(X_CENTER, Y_CENTER, RADIUS, 20); |
ovidiup13 | 3:688b62ff6474 | 80 | st7565->drawcircle(X_CENTER, Y_CENTER, 2, 20); |
ovidiup13 | 3:688b62ff6474 | 81 | |
ovidiup13 | 3:688b62ff6474 | 82 | //draw the lines |
ovidiup13 | 3:688b62ff6474 | 83 | st7565->drawline(X_CENTER, Y_CENTER, x_temp, y_temp, 20); //draw line from center to coordinates |
ovidiup13 | 3:688b62ff6474 | 84 | st7565->drawline(X_CENTER, Y_CENTER - RADIUS, X_CENTER, Y_CENTER - 15, 20); //north line |
ovidiup13 | 3:688b62ff6474 | 85 | st7565->drawline(X_CENTER, Y_CENTER + RADIUS, X_CENTER, Y_CENTER + 15, 20); //south line |
ovidiup13 | 3:688b62ff6474 | 86 | st7565->drawline(X_CENTER + RADIUS, Y_CENTER, X_CENTER + 15, Y_CENTER, 20); //east line |
ovidiup13 | 3:688b62ff6474 | 87 | st7565->drawline(X_CENTER - RADIUS, Y_CENTER, X_CENTER - 15, Y_CENTER, 20); //west line |
ovidiup13 | 3:688b62ff6474 | 88 | |
ovidiup13 | 3:688b62ff6474 | 89 | //draw the initials |
ovidiup13 | 3:688b62ff6474 | 90 | st7565->drawstring(X_CENTER - 2, 1, "N"); |
ovidiup13 | 3:688b62ff6474 | 91 | st7565->drawstring(X_CENTER - 2, 7, "S"); |
ovidiup13 | 3:688b62ff6474 | 92 | st7565->drawstring(X_CENTER + 21, 4, "E"); |
ovidiup13 | 3:688b62ff6474 | 93 | st7565->drawstring(X_CENTER - 25, 4, "W"); |
ovidiup13 | 3:688b62ff6474 | 94 | |
ovidiup13 | 3:688b62ff6474 | 95 | //display pointing direction |
ovidiup13 | 3:688b62ff6474 | 96 | st7565->drawstring(0, 2, "Pointing:"); |
ovidiup13 | 3:688b62ff6474 | 97 | char * pointer = get_direction(degrees); |
ovidiup13 | 3:688b62ff6474 | 98 | st7565->drawstring(0, 4, pointer); |
ovidiup13 | 3:688b62ff6474 | 99 | |
ovidiup13 | 3:688b62ff6474 | 100 | //display degrees and radians in bottom left corner |
ovidiup13 | 3:688b62ff6474 | 101 | char s_deg[10], s_rad[10]; |
ovidiup13 | 3:688b62ff6474 | 102 | sprintf(s_deg, "DEG:%g", degrees); |
ovidiup13 | 3:688b62ff6474 | 103 | sprintf(s_rad, "RAD:%.2g", rad); |
ovidiup13 | 3:688b62ff6474 | 104 | st7565->drawstring(1, 6, s_deg); |
ovidiup13 | 3:688b62ff6474 | 105 | st7565->drawstring(1, 7, s_rad); |
ovidiup13 | 3:688b62ff6474 | 106 | |
ovidiup13 | 3:688b62ff6474 | 107 | st7565->display(); |
ovidiup13 | 3:688b62ff6474 | 108 | } |