Team Alpha / Mbed 2 deprecated UserIntefaceLCD

Dependencies:   mbed mbed-rtos MLX90614

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers LevelMeter.cpp Source File

LevelMeter.cpp

00001 #include "LevelMeter.h"
00002 
00003 LevelMeter::LevelMeter(ST7565 *lcd, Item * back, DigitalOut *gyro, DigitalOut *thermo)
00004 {
00005     this->st7565 = lcd;
00006     this->title = " Level meter";
00007     this->back = back;
00008     this->gyro = gyro;
00009     this->thermo = thermo;
00010     lt = NULL;
00011 }
00012 
00013 void LevelMeter::display(void)
00014 {
00015     //create a new thread to get and update compass - do later
00016     if(lt != NULL) return;//thread is already running
00017     thermo->write(0); gyro->write(1);
00018     lt = new Thread(&LevelMeter::lt_start, this);
00019     lt->signal_set(START_THREAD);
00020 }
00021 
00022 //trigger for starting the thread
00023 void LevelMeter::lt_start(void const *args)
00024 {
00025     LevelMeter *l = (LevelMeter*)args;
00026     l->update_cross();
00027 }
00028 
00029 void LevelMeter::update_cross()
00030 {
00031     lt->signal_wait(START_THREAD); //wait for signal to start thread
00032     initSensors();
00033     Result_avrg result;
00034     //draw the static circle
00035     //int i = 0, j = 0;
00036     while(true) {
00037         calc_avrg_or(&result,2);
00038         draw_elements(result.x, result.y);
00039         printf("%0.0f, %0.0f\n", result.x, result.y);
00040         Thread::wait(15);
00041         st7565->clear();
00042     }
00043 }
00044 
00045 void LevelMeter::draw_elements(double rx, double ry){
00046     int cross_x, cross_y;
00047     
00048     //draw the circles
00049     st7565->drawcircle(X0, Y0, RADIUS_lvl, 1);
00050 
00051     //draw middle cross
00052     st7565->drawline(X0 - 2, Y0, X0 + 2, Y0, 20);
00053     st7565->drawline(X0, Y0 - 2, X0, Y0 + 2, 20);
00054     
00055     //check if rotation in negative interval
00056     if(rx >= 270) rx -= 360;
00057     if(ry >= 270) ry -= 360;
00058     
00059     //calculate coordinates
00060     cross_x = (int) ((2*rx * SCREEN_WIDTH)/360.0) + X0;
00061     cross_y = (int) ((2*ry * SCREEN_HEIGHT)/360.0) + Y0;
00062     
00063     //printf("Coordinates for cross are: %d, %d\n", cross_x, cross_y);
00064     
00065     //draw the cross
00066     st7565->drawline(cross_x, cross_y - RADIUS_lvl*3, cross_x, cross_y + RADIUS_lvl*3, DEFAULT_COLOR);
00067     st7565->drawline(cross_x - RADIUS_lvl*3, cross_y, cross_x + RADIUS_lvl*3, cross_y, DEFAULT_COLOR);
00068     
00069     //draw results
00070     char *rsx = (char*)malloc(sizeof(char)*7);
00071     char *rsy = (char*)malloc(sizeof(char)*7);
00072     sprintf(rsx, "X: %0.0f", rx);
00073     sprintf(rsy, "Y: %0.0f", ry);
00074     st7565->drawstring(3, 7, rsx);
00075     st7565->drawstring(SCREEN_WIDTH - 38, 7, rsy);
00076     
00077     //display
00078     st7565->display();
00079     //free(rsx);
00080     //free(rsy);
00081 }
00082 
00083 void LevelMeter::update(char c)
00084 {
00085     if(c == 'y') {
00086         lt->terminate();
00087         free(lt); lt = NULL;
00088         gyro->write(0);
00089         st7565->clear();
00090         this->setSelectedScreen(back);
00091     }
00092 }