LCD implementation of our project.
Dependencies: mbed mbed-rtos MLX90614
LevelMeter.cpp@6:49a007861c76, 2015-05-30 (annotated)
- Committer:
- ovidiup13
- Date:
- Sat May 30 14:58:44 2015 +0000
- Revision:
- 6:49a007861c76
- Parent:
- 5:5b1a8ad6c187
- Child:
- 8:81ed1135ba02
integrated level meter functionality and added 3-way switch control
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
ovidiup13 | 3:688b62ff6474 | 1 | #include "LevelMeter.h" |
ovidiup13 | 3:688b62ff6474 | 2 | |
ovidiup13 | 5:5b1a8ad6c187 | 3 | LevelMeter::LevelMeter(ST7565 *lcd, Item * back) |
ovidiup13 | 5:5b1a8ad6c187 | 4 | { |
ovidiup13 | 5:5b1a8ad6c187 | 5 | this->st7565 = lcd; |
ovidiup13 | 5:5b1a8ad6c187 | 6 | this->title = " Level meter"; |
ovidiup13 | 5:5b1a8ad6c187 | 7 | this->back = back; |
ovidiup13 | 5:5b1a8ad6c187 | 8 | lt = NULL; |
ovidiup13 | 5:5b1a8ad6c187 | 9 | } |
ovidiup13 | 5:5b1a8ad6c187 | 10 | |
ovidiup13 | 5:5b1a8ad6c187 | 11 | void LevelMeter::display(void) |
ovidiup13 | 5:5b1a8ad6c187 | 12 | { |
ovidiup13 | 5:5b1a8ad6c187 | 13 | //create a new thread to get and update compass - do later |
ovidiup13 | 5:5b1a8ad6c187 | 14 | if(lt != NULL) return;//thread is already running |
ovidiup13 | 5:5b1a8ad6c187 | 15 | lt = new Thread(&LevelMeter::lt_start, this); |
ovidiup13 | 5:5b1a8ad6c187 | 16 | lt->signal_set(START_THREAD); |
ovidiup13 | 3:688b62ff6474 | 17 | } |
ovidiup13 | 3:688b62ff6474 | 18 | |
ovidiup13 | 5:5b1a8ad6c187 | 19 | //trigger for starting the thread |
ovidiup13 | 5:5b1a8ad6c187 | 20 | void LevelMeter::lt_start(void const *args) |
ovidiup13 | 5:5b1a8ad6c187 | 21 | { |
ovidiup13 | 5:5b1a8ad6c187 | 22 | LevelMeter *l = (LevelMeter*)args; |
ovidiup13 | 5:5b1a8ad6c187 | 23 | l->update_cross(); |
ovidiup13 | 3:688b62ff6474 | 24 | } |
ovidiup13 | 3:688b62ff6474 | 25 | |
ovidiup13 | 5:5b1a8ad6c187 | 26 | void LevelMeter::update_cross() |
ovidiup13 | 5:5b1a8ad6c187 | 27 | { |
ovidiup13 | 5:5b1a8ad6c187 | 28 | lt->signal_wait(START_THREAD); //wait for signal to start thread |
ovidiup13 | 6:49a007861c76 | 29 | initSensors(); |
ovidiup13 | 6:49a007861c76 | 30 | Result_avrg result; |
ovidiup13 | 5:5b1a8ad6c187 | 31 | //draw the static circle |
ovidiup13 | 6:49a007861c76 | 32 | //int i = 0, j = 0; |
ovidiup13 | 5:5b1a8ad6c187 | 33 | while(true) { |
ovidiup13 | 6:49a007861c76 | 34 | calc_avrg_or(&result,2); |
ovidiup13 | 6:49a007861c76 | 35 | draw_elements(result.x, result.y); |
ovidiup13 | 6:49a007861c76 | 36 | //printf("%0.0f, %0.0f\n", result.x, result.y); |
ovidiup13 | 6:49a007861c76 | 37 | Thread::wait(15); |
ovidiup13 | 5:5b1a8ad6c187 | 38 | st7565->clear(); |
ovidiup13 | 5:5b1a8ad6c187 | 39 | } |
ovidiup13 | 5:5b1a8ad6c187 | 40 | } |
ovidiup13 | 5:5b1a8ad6c187 | 41 | |
ovidiup13 | 5:5b1a8ad6c187 | 42 | void LevelMeter::draw_elements(double rx, double ry){ |
ovidiup13 | 5:5b1a8ad6c187 | 43 | int cross_x, cross_y; |
ovidiup13 | 3:688b62ff6474 | 44 | |
ovidiup13 | 3:688b62ff6474 | 45 | //draw the circles |
ovidiup13 | 5:5b1a8ad6c187 | 46 | st7565->drawcircle(X0, Y0, RADIUS_lvl, 1); |
ovidiup13 | 5:5b1a8ad6c187 | 47 | |
ovidiup13 | 5:5b1a8ad6c187 | 48 | //draw middle cross |
ovidiup13 | 3:688b62ff6474 | 49 | st7565->drawline(X0 - 2, Y0, X0 + 2, Y0, 20); |
ovidiup13 | 3:688b62ff6474 | 50 | st7565->drawline(X0, Y0 - 2, X0, Y0 + 2, 20); |
ovidiup13 | 3:688b62ff6474 | 51 | |
ovidiup13 | 5:5b1a8ad6c187 | 52 | //check if rotation in negative interval |
ovidiup13 | 5:5b1a8ad6c187 | 53 | if(rx >= 270) rx -= 360; |
ovidiup13 | 5:5b1a8ad6c187 | 54 | if(ry >= 270) ry -= 360; |
ovidiup13 | 5:5b1a8ad6c187 | 55 | |
ovidiup13 | 5:5b1a8ad6c187 | 56 | //calculate coordinates |
ovidiup13 | 6:49a007861c76 | 57 | cross_x = (int) ((2*rx * SCREEN_WIDTH)/360.0) + X0; |
ovidiup13 | 6:49a007861c76 | 58 | cross_y = (int) ((2*ry * SCREEN_HEIGHT)/360.0) + Y0; |
ovidiup13 | 5:5b1a8ad6c187 | 59 | |
ovidiup13 | 6:49a007861c76 | 60 | //printf("Coordinates for cross are: %d, %d\n", cross_x, cross_y); |
ovidiup13 | 5:5b1a8ad6c187 | 61 | |
ovidiup13 | 5:5b1a8ad6c187 | 62 | //draw the cross |
ovidiup13 | 6:49a007861c76 | 63 | st7565->drawline(cross_x, cross_y - RADIUS_lvl*3, cross_x, cross_y + RADIUS_lvl*3, DEFAULT_COLOR); |
ovidiup13 | 6:49a007861c76 | 64 | st7565->drawline(cross_x - RADIUS_lvl*3, cross_y, cross_x + RADIUS_lvl*3, cross_y, DEFAULT_COLOR); |
ovidiup13 | 6:49a007861c76 | 65 | |
ovidiup13 | 6:49a007861c76 | 66 | //draw results |
ovidiup13 | 6:49a007861c76 | 67 | char *rsx = (char*)malloc(sizeof(char)*7); |
ovidiup13 | 6:49a007861c76 | 68 | char *rsy = (char*)malloc(sizeof(char)*7); |
ovidiup13 | 6:49a007861c76 | 69 | sprintf(rsx, "X: %0.0f", rx); |
ovidiup13 | 6:49a007861c76 | 70 | sprintf(rsy, "Y: %0.0f", ry); |
ovidiup13 | 6:49a007861c76 | 71 | st7565->drawstring(3, 7, rsx); |
ovidiup13 | 6:49a007861c76 | 72 | st7565->drawstring(SCREEN_WIDTH - 38, 7, rsy); |
ovidiup13 | 5:5b1a8ad6c187 | 73 | |
ovidiup13 | 5:5b1a8ad6c187 | 74 | //display |
ovidiup13 | 3:688b62ff6474 | 75 | st7565->display(); |
ovidiup13 | 5:5b1a8ad6c187 | 76 | } |
ovidiup13 | 5:5b1a8ad6c187 | 77 | |
ovidiup13 | 5:5b1a8ad6c187 | 78 | void LevelMeter::update(char c) |
ovidiup13 | 5:5b1a8ad6c187 | 79 | { |
ovidiup13 | 5:5b1a8ad6c187 | 80 | if(c == 'y') { |
ovidiup13 | 5:5b1a8ad6c187 | 81 | lt->terminate(); |
ovidiup13 | 5:5b1a8ad6c187 | 82 | free(lt); lt = NULL; |
ovidiup13 | 5:5b1a8ad6c187 | 83 | st7565->clear(); |
ovidiup13 | 5:5b1a8ad6c187 | 84 | this->setSelectedScreen(back); |
ovidiup13 | 5:5b1a8ad6c187 | 85 | } |
ovidiup13 | 3:688b62ff6474 | 86 | } |