LCD implementation of our project.
Dependencies: mbed mbed-rtos MLX90614
Compass.cpp@5:5b1a8ad6c187, 2015-05-28 (annotated)
- Committer:
- ovidiup13
- Date:
- Thu May 28 16:07:00 2015 +0000
- Revision:
- 5:5b1a8ad6c187
- Parent:
- 4:024e6a9c2ebf
- Child:
- 7:11675c1dce4f
added levelmeter functionality and 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 | 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 | 5:5b1a8ad6c187 | 32 | |
ovidiup13 | 4:024e6a9c2ebf | 33 | int i = 0; |
ovidiup13 | 4:024e6a9c2ebf | 34 | //get degrees from other functions and display the compass |
ovidiup13 | 5:5b1a8ad6c187 | 35 | while(true) { |
ovidiup13 | 4:024e6a9c2ebf | 36 | draw_compass(i++); |
ovidiup13 | 5:5b1a8ad6c187 | 37 | Thread::wait(30); |
ovidiup13 | 4:024e6a9c2ebf | 38 | st7565->clear(); |
ovidiup13 | 4:024e6a9c2ebf | 39 | } |
ovidiup13 | 4:024e6a9c2ebf | 40 | } |
ovidiup13 | 4:024e6a9c2ebf | 41 | |
ovidiup13 | 4:024e6a9c2ebf | 42 | //update function handles updates from the user - cancels the thread |
ovidiup13 | 4:024e6a9c2ebf | 43 | //and returns to main menu |
ovidiup13 | 5:5b1a8ad6c187 | 44 | void Compass::update(char c) |
ovidiup13 | 5:5b1a8ad6c187 | 45 | { |
ovidiup13 | 3:688b62ff6474 | 46 | //kill thread and go back |
ovidiup13 | 5:5b1a8ad6c187 | 47 | if(c == 'y') { |
ovidiup13 | 4:024e6a9c2ebf | 48 | ct->terminate(); |
ovidiup13 | 5:5b1a8ad6c187 | 49 | free(ct); |
ovidiup13 | 5:5b1a8ad6c187 | 50 | ct = NULL; |
ovidiup13 | 5:5b1a8ad6c187 | 51 | st7565->clear();//clear everything |
ovidiup13 | 3:688b62ff6474 | 52 | this->setSelectedScreen(back); |
ovidiup13 | 5:5b1a8ad6c187 | 53 | } else |
ovidiup13 | 5:5b1a8ad6c187 | 54 | return; |
ovidiup13 | 3:688b62ff6474 | 55 | } |
ovidiup13 | 3:688b62ff6474 | 56 | |
ovidiup13 | 3:688b62ff6474 | 57 | //get direction |
ovidiup13 | 5:5b1a8ad6c187 | 58 | char * get_direction(double degrees) |
ovidiup13 | 5:5b1a8ad6c187 | 59 | { |
ovidiup13 | 3:688b62ff6474 | 60 | if(degrees >= 330 || degrees < 30) |
ovidiup13 | 3:688b62ff6474 | 61 | return "East"; |
ovidiup13 | 3:688b62ff6474 | 62 | else if(degrees >= 30 && degrees <= 60) |
ovidiup13 | 3:688b62ff6474 | 63 | return "North-East"; |
ovidiup13 | 3:688b62ff6474 | 64 | else if(degrees >= 60 && degrees < 120) |
ovidiup13 | 3:688b62ff6474 | 65 | return "North"; |
ovidiup13 | 3:688b62ff6474 | 66 | else if(degrees >= 120 && degrees < 150) |
ovidiup13 | 3:688b62ff6474 | 67 | return "North-West"; |
ovidiup13 | 3:688b62ff6474 | 68 | else if(degrees >= 150 && degrees < 210) |
ovidiup13 | 3:688b62ff6474 | 69 | return "West"; |
ovidiup13 | 3:688b62ff6474 | 70 | else if(degrees >= 210 && degrees < 240) |
ovidiup13 | 3:688b62ff6474 | 71 | return "South-West"; |
ovidiup13 | 3:688b62ff6474 | 72 | else if(degrees >= 240 && degrees < 300) |
ovidiup13 | 3:688b62ff6474 | 73 | return "South"; |
ovidiup13 | 3:688b62ff6474 | 74 | else |
ovidiup13 | 3:688b62ff6474 | 75 | return "South-East"; |
ovidiup13 | 3:688b62ff6474 | 76 | } |
ovidiup13 | 3:688b62ff6474 | 77 | |
ovidiup13 | 3:688b62ff6474 | 78 | //function that draws the compass on the screen |
ovidiup13 | 5:5b1a8ad6c187 | 79 | void Compass::draw_compass(double degrees) |
ovidiup13 | 5:5b1a8ad6c187 | 80 | { |
ovidiup13 | 3:688b62ff6474 | 81 | //variables |
ovidiup13 | 3:688b62ff6474 | 82 | int x_temp, y_temp; |
ovidiup13 | 4:024e6a9c2ebf | 83 | double rad = degrees * M_PI / 180; //radians |
ovidiup13 | 5:5b1a8ad6c187 | 84 | |
ovidiup13 | 3:688b62ff6474 | 85 | //calculate coordinates to point |
ovidiup13 | 3:688b62ff6474 | 86 | x_temp = X_CENTER + (int) (POINTER_LENGTH * cos(rad)); |
ovidiup13 | 3:688b62ff6474 | 87 | y_temp = Y_CENTER + (int) (POINTER_LENGTH * (-sin(rad))); |
ovidiup13 | 5:5b1a8ad6c187 | 88 | |
ovidiup13 | 3:688b62ff6474 | 89 | //draw the main circle and small one |
ovidiup13 | 3:688b62ff6474 | 90 | st7565->drawcircle(X_CENTER, Y_CENTER, RADIUS, 20); |
ovidiup13 | 3:688b62ff6474 | 91 | st7565->drawcircle(X_CENTER, Y_CENTER, 2, 20); |
ovidiup13 | 5:5b1a8ad6c187 | 92 | |
ovidiup13 | 3:688b62ff6474 | 93 | //draw the lines |
ovidiup13 | 3:688b62ff6474 | 94 | st7565->drawline(X_CENTER, Y_CENTER, x_temp, y_temp, 20); //draw line from center to coordinates |
ovidiup13 | 3:688b62ff6474 | 95 | st7565->drawline(X_CENTER, Y_CENTER - RADIUS, X_CENTER, Y_CENTER - 15, 20); //north line |
ovidiup13 | 3:688b62ff6474 | 96 | st7565->drawline(X_CENTER, Y_CENTER + RADIUS, X_CENTER, Y_CENTER + 15, 20); //south line |
ovidiup13 | 3:688b62ff6474 | 97 | st7565->drawline(X_CENTER + RADIUS, Y_CENTER, X_CENTER + 15, Y_CENTER, 20); //east line |
ovidiup13 | 3:688b62ff6474 | 98 | st7565->drawline(X_CENTER - RADIUS, Y_CENTER, X_CENTER - 15, Y_CENTER, 20); //west line |
ovidiup13 | 5:5b1a8ad6c187 | 99 | |
ovidiup13 | 3:688b62ff6474 | 100 | //draw the initials |
ovidiup13 | 3:688b62ff6474 | 101 | st7565->drawstring(X_CENTER - 2, 1, "N"); |
ovidiup13 | 3:688b62ff6474 | 102 | st7565->drawstring(X_CENTER - 2, 7, "S"); |
ovidiup13 | 3:688b62ff6474 | 103 | st7565->drawstring(X_CENTER + 21, 4, "E"); |
ovidiup13 | 3:688b62ff6474 | 104 | st7565->drawstring(X_CENTER - 25, 4, "W"); |
ovidiup13 | 5:5b1a8ad6c187 | 105 | |
ovidiup13 | 3:688b62ff6474 | 106 | //display pointing direction |
ovidiup13 | 3:688b62ff6474 | 107 | st7565->drawstring(0, 2, "Pointing:"); |
ovidiup13 | 3:688b62ff6474 | 108 | char * pointer = get_direction(degrees); |
ovidiup13 | 3:688b62ff6474 | 109 | st7565->drawstring(0, 4, pointer); |
ovidiup13 | 5:5b1a8ad6c187 | 110 | |
ovidiup13 | 3:688b62ff6474 | 111 | //display degrees and radians in bottom left corner |
ovidiup13 | 3:688b62ff6474 | 112 | char s_deg[10], s_rad[10]; |
ovidiup13 | 3:688b62ff6474 | 113 | sprintf(s_deg, "DEG:%g", degrees); |
ovidiup13 | 3:688b62ff6474 | 114 | sprintf(s_rad, "RAD:%.2g", rad); |
ovidiup13 | 3:688b62ff6474 | 115 | st7565->drawstring(1, 6, s_deg); |
ovidiup13 | 3:688b62ff6474 | 116 | st7565->drawstring(1, 7, s_rad); |
ovidiup13 | 5:5b1a8ad6c187 | 117 | |
ovidiup13 | 3:688b62ff6474 | 118 | st7565->display(); |
ovidiup13 | 3:688b62ff6474 | 119 | } |