SWO+USB Serial+TouchScreen Demo

Dependencies:   BSP_DISCO_F429ZI LCD_DISCO_F429ZI SWO TS_DISCO_F429ZI USBDEVICE mbed storage_on_flash

Fork of DISCO-F429ZI_LCDTS_demo by ST

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 //#define USB_STM_HAL
00002 //#define DISCO
00003 #define LANDSCAPE_MODE
00004 
00005 #include "mbed.h"
00006 
00007 
00008 #include "SOFBlock.h"
00009 
00010 
00011 #include "stm32f429i_discovery_sdram.h"
00012 #include "TS_DISCO_F429ZI.h"
00013 #include "LCD_DISCO_F429ZI.h"
00014 #include "USBSerial.h"
00015 #include "SWO.h"
00016 #include "logos.h"
00017 #include "SOFBlock.h"
00018 #include "ubuntu_font.h"
00019 
00020 #include <cstdlib>
00021 
00022 //PA3 PA5
00023 AnalogOut temperature_out(PA_5);
00024 //PC3
00025 
00026 //AnalogIn #conflicts with something somewhere
00027 //SPI PE6 PE5 PE2
00028 
00029 LCD_DISCO_F429ZI lcd;
00030 TS_DISCO_F429ZI ts;
00031 uint8_t text[30];
00032 
00033 SWO_Channel swo;
00034 //USBSerial ser;
00035 
00036 //AnalogOut temp_setpoint(PA_5);
00037 //AnalogIn temp_read(PF_4);
00038 
00039 //#define DEBUG
00040 
00041 #define CODE_VERSION_STRING "1.0"
00042 #define LINE(x) ((x) * (((sFONT *)BSP_LCD_GetFont())->Height))
00043 
00044 #ifdef DEBUG
00045 #define TEMP_SET_LINE 0
00046 #define CLOSER 0
00047 #define TEMP_SET_TEXT LINE(TEMP_SET_LINE)+CLOSER
00048 #define TEMP_SET_DISP LINE(TEMP_SET_LINE+1)+CLOSER
00049 #define TEMP_READ_TEXT LINE(TEMP_SET_LINE+3)-CLOSER
00050 #define TEMP_READ_DISP LINE(TEMP_SET_LINE+4)-CLOSER
00051 #define TEMP_TEXT_X 5
00052 #define TEMP_DISP_X (TEMP_TEXT_X - 5)
00053 #else
00054 #define TEMP_SET_LINE 4
00055 #define OFFSET 5
00056 #define CLOSER 10
00057 #define TEMP_SET_TEXT LINE(TEMP_SET_LINE)+CLOSER+OFFSET
00058 #define TEMP_SET_DISP LINE(TEMP_SET_LINE+1)+CLOSER+OFFSET
00059 #define TEMP_READ_TEXT LINE(TEMP_SET_LINE+3)+CLOSER/2+OFFSET
00060 #define TEMP_READ_DISP LINE(TEMP_SET_LINE+4)+CLOSER/2+OFFSET
00061 #define TEMP_TEXT_X 32
00062 #define TEMP_DISP_X (TEMP_TEXT_X - 5)
00063 #endif
00064     
00065 
00066 #define min(x,y) (x>y?y:x)
00067 #define max(x,y) (x>y?x:y)
00068 
00069 #define DEFAULT_LAYER 0
00070 #define MIN_TEMP -40.
00071 #define MAX_TEMP 20.0
00072     
00073 double temperature_setpoint = -10.0;
00074 double last_read_temperature = temperature_setpoint;
00075 
00076 #define MAX_TP_POINTS (320-2*10-2*10)
00077 #define DELAY_ACQ 200
00078 #define TS_READ_TIME 75
00079 #define TEMPERATURE_SLOW_ACCUMULATE 40
00080 //(5*10) //10s approx 5*10*200ms
00081 
00082 double temperature_curve_fast[MAX_TP_POINTS]; 
00083 double temperature_curve_slow[MAX_TP_POINTS];
00084 int accumulated_slow_temp = 0;
00085 double slow_temp_acc = 0.;
00086 int temperature_curve_slow_count = 0;
00087 int temperature_curve_fast_count = 0;
00088 int temperature_curve_fast_index = 0;
00089 int temperature_curve_slow_index = 0;
00090 
00091 int last_update_index = -1;
00092 
00093 double eeprom_temperature = 0.0;
00094 
00095 double pcoefs[] = {-1.10014853, 10.40077476, -70.49071822,  86.20161024};
00096 double rev_pcoefs[] = {-5.42057541e-08, 5.11915693e-05, -2.13855322e-02, 1.50390471e+00};
00097 
00098 double polyval(double* coefs, int degree, double value)
00099 {
00100     int power = 0;
00101     int degree_current = degree;
00102     double value_out = 0;
00103     do {
00104         value_out += pow(value, power)*coefs[degree_current];
00105         degree_current -= 1;
00106         power += 1;
00107     } while (power <= degree);
00108     return value_out;
00109 }
00110 
00111 double read_temperature() 
00112 {
00113     //fixes a bug for repetitve readout failures
00114     AnalogIn temp_read(PA_3); //PF4 PF5 PF3 PF6 PF8
00115     double bridge_res = 100e3;
00116     double bridge_voltage = 3.3;
00117     int averaging = 16;
00118     double accumulator = 0.;
00119     for (int i=0; i<averaging; i++) {
00120         accumulator += temp_read.read();
00121     }
00122     double temp_averaging = accumulator/averaging;
00123     double midpoint_voltage = double(temp_averaging*bridge_voltage);
00124     double rx = (midpoint_voltage / bridge_voltage) * bridge_res / (1 - midpoint_voltage / bridge_voltage);
00125     #ifdef DEBUG
00126     sprintf((char*)text, "TR %lf V ", midpoint_voltage);
00127     lcd.DisplayStringAt(0, LINE(9), (uint8_t *)&text, LEFT_MODE);
00128     #endif
00129     return polyval(pcoefs, 3, log10(rx/1e3));
00130 }
00131 
00132 void write_temperature_setpoint(double temperature)
00133 {
00134     double bridge_res = 100e3;
00135     double bridge_voltage = 3.3;
00136     double res = pow(10., polyval(rev_pcoefs, 3, temperature))*1e3;
00137     double voltage = (res/(res+bridge_res) * bridge_voltage);
00138     #ifdef DEBUG
00139     sprintf((char*)text, "SP %4.1lf V ", voltage);
00140     lcd.DisplayStringAt(0, LINE(8), (uint8_t *)&text, LEFT_MODE);
00141     #endif
00142     temperature_out.write(voltage/bridge_voltage);
00143 }
00144 
00145 
00146 
00147 #define LCD_REFRESH_RATE 40
00148 
00149 void LCD_Reload_Safe(void)
00150 {
00151     BSP_LCD_Reload(LTDC_RELOAD_VERTICAL_BLANKING);
00152     wait(1./LCD_REFRESH_RATE);
00153 }
00154 
00155 
00156 const uint8_t sector_index = 11;
00157 SOFWriter writer;
00158 SOFReader reader;
00159 
00160 void check_temperature_storage(void)
00161 {
00162     //here could check for data
00163     reader.open(sector_index);
00164     double temp_backup = *(double *)(reader.get_physical_data_addr()-sizeof(double));
00165     reader.close();
00166     writer.open(sector_index);
00167     if (writer.get_free_size() <= sizeof(double)*1024) { // check available byte
00168         SOFBlock::format(sector_index);
00169         writer.write_data((uint8_t*)(&temp_backup), sizeof(double));
00170     } else {
00171         #ifndef NO_WAIT
00172         wait(1);
00173         #endif
00174     }
00175     writer.close();
00176 }
00177 
00178 void load_temperature_storage(void)
00179 {
00180     reader.open(sector_index);
00181     eeprom_temperature = *(double *)(reader.get_physical_data_addr()-sizeof(double));
00182     reader.close();
00183     
00184     if ((eeprom_temperature >= MIN_TEMP) && (eeprom_temperature <= MAX_TEMP))
00185     {
00186         temperature_setpoint = eeprom_temperature;
00187     }
00188     write_temperature_setpoint(temperature_setpoint);
00189     
00190 }
00191 
00192 void save_temperature_storage(void)
00193 {
00194     /* write temperature set point */
00195     if ((temperature_setpoint != eeprom_temperature) && 
00196             (temperature_setpoint < MAX_TEMP) && (temperature_setpoint > MIN_TEMP)) {
00197         writer.open(sector_index);
00198         writer.write_data((uint8_t*)(&temperature_setpoint), sizeof(double));
00199 
00200         eeprom_temperature = temperature_setpoint;
00201         if (writer.get_free_size() <= sizeof(double)+1) { // check available byte
00202             SOFBlock::format(sector_index);
00203         }
00204         writer.close();
00205         eeprom_temperature = temperature_setpoint;
00206     }
00207 
00208 }
00209 
00210 void DrawRectCentered(uint16_t xcenter, uint16_t ycenter, uint16_t width, uint16_t height)
00211 {
00212     //landscape mode only
00213     lcd.FillRect(xcenter+width/2,ycenter-height/2, height, width);
00214 }
00215 
00216 void print_layer(int layer)
00217 {
00218     lcd.SelectLayer(layer);
00219     BSP_LCD_SetLayerVisible(layer, ENABLE);
00220     sprintf((char*)text, "LAYER %d", layer);
00221     lcd.DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);
00222     BSP_LCD_SetLayerVisible(layer, DISABLE);
00223 }
00224 
00225 void print_layers()
00226 {
00227     for (int i=0;i<3;i++)
00228     {
00229         print_layer(i);
00230     }
00231     lcd.SelectLayer(0);
00232     BSP_LCD_SetLayerVisible(0, ENABLE);
00233 
00234 }
00235 
00236 void draw_startup_screen(int layer)
00237 {
00238     lcd.SelectLayer(layer);
00239     BSP_LCD_SetLayerVisible_NoReload(layer, ENABLE);
00240     LCD_Reload_Safe();
00241     lcd.Clear(LCD_COLOR_BLUE);
00242     uint8_t* pBmp = const_cast<uint8_t*>(big_fastlite_logo_68_302);
00243     lcd.DrawBitmap((lcd.GetYSize()-302)/2, (lcd.GetXSize()-68)/2, pBmp);
00244 }
00245 
00246 void draw_version_screen(int layer)
00247 {
00248     lcd.SelectLayer(layer);
00249     lcd.SetBackColor(LCD_COLOR_BLUE);
00250     lcd.SetTextColor(LCD_COLOR_WHITE);
00251     lcd.Clear(LCD_COLOR_BLUE);
00252     
00253     BSP_LCD_SetFont(&UbuntuFont23);
00254     
00255     lcd.Clear(LCD_COLOR_BLUE);
00256     uint8_t* pBmp = (uint8_t*)(big_fastlite_logo_68_302);
00257     lcd.DrawBitmap((lcd.GetYSize()-302)/2, 0, pBmp);
00258     
00259     sprintf((char*)text, "TEC interface");
00260     lcd.DisplayStringAt(5, LINE(0)+70, (uint8_t *)&text, LEFT_MODE);
00261     sprintf((char*)text, "Version %s", CODE_VERSION_STRING);
00262     lcd.DisplayStringAt(5, LINE(1)+70, (uint8_t *)&text, LEFT_MODE);    
00263 }
00264 
00265 void fade_screen(int layer, int fade_count, uint32_t color, int dest_layer)
00266 {
00267     lcd.SelectLayer(layer+1);
00268     BSP_LCD_SetTransparency_NoReload(layer+1, 0);
00269     BSP_LCD_SetColorKeying_NoReload(layer+1, LCD_COLOR_WHITE);
00270     lcd.Clear(color);
00271     LCD_Reload_Safe();
00272     BSP_LCD_SetLayerVisible_NoReload(layer+1, ENABLE);
00273     BSP_LCD_Reload(LTDC_RELOAD_VERTICAL_BLANKING);
00274     lcd.SetTextColor(color);
00275     
00276     for (int i=0; i<fade_count; i++)
00277     {
00278         BSP_LCD_SetTransparency_NoReload(layer+1,255/(fade_count-1)*i); //TRANSPARENCY == ALPHA so 255 is no transparent
00279         BSP_LCD_Reload(LTDC_RELOAD_VERTICAL_BLANKING);
00280         #ifndef NO_WAIT
00281         wait(1./fade_count);
00282         #endif
00283     }
00284     
00285     BSP_LCD_ResetColorKeying_NoReload(layer+1);
00286     BSP_LCD_SetLayerVisible_NoReload(layer+1, DISABLE);
00287     
00288     lcd.SelectLayer(layer);
00289     lcd.Clear(color);
00290     
00291     if (layer != dest_layer) {
00292         lcd.SelectLayer(dest_layer);
00293         lcd.Clear(color);
00294         BSP_LCD_SetLayerVisible_NoReload(dest_layer, ENABLE);
00295     }
00296     LCD_Reload_Safe();
00297 
00298 }
00299 
00300 int curve_y_min_temp = 0;
00301 int curve_y_zero_temp = 0;
00302 int curve_x_zero_time = 0;
00303 int curve_height_x = 0;
00304 int curve_width_y = 0;
00305 double pixel_temp_f=1.;
00306 
00307 void enable_curve_overlay(int layer, bool set_visible)
00308 {
00309     if (set_visible) BSP_LCD_SetLayerVisible_NoReload(layer, ENABLE);
00310     lcd.SelectLayer(layer);
00311     lcd.Clear(LCD_COLOR_TRANSPARENT);
00312     BSP_LCD_SetColorKeying_NoReload(layer, LCD_COLOR_TRANSPARENT);
00313     if (set_visible) {
00314         LCD_Reload_Safe();
00315     }
00316 }
00317 
00318 void disable_curve_overlay(int layer)
00319 {
00320     BSP_LCD_SetLayerVisible_NoReload(layer, DISABLE);
00321     lcd.SelectLayer(layer);
00322     lcd.Clear(LCD_COLOR_TRANSPARENT);
00323     BSP_LCD_SetColorKeying_NoReload(layer, LCD_COLOR_TRANSPARENT);
00324     LCD_Reload_Safe();
00325 
00326 }
00327 
00328 void clear_curve_overlay(int layer)
00329 {
00330     lcd.SelectLayer(layer);
00331     lcd.Clear(LCD_COLOR_TRANSPARENT);
00332 }
00333 
00334 bool update_temperature_read_curve(int layer, int screen, bool clear, bool force_update)
00335 {
00336     lcd.SelectLayer(layer);
00337     int temp_index = 0;
00338     int temp_count = 0;
00339     double* temp_curve = NULL;
00340     uint32_t curve_color = 0;
00341     
00342     if (screen == -1)
00343     {
00344         temp_index = temperature_curve_fast_index;
00345         temp_count = temperature_curve_fast_count;
00346         temp_curve = temperature_curve_fast;
00347         curve_color = LCD_COLOR_RED;
00348     } else {
00349         temp_index = temperature_curve_slow_index;
00350         temp_count = temperature_curve_slow_count;
00351         temp_curve = temperature_curve_slow;
00352         curve_color = LCD_COLOR_DARKGREEN;
00353     }
00354     
00355     if ((last_update_index == temp_index) && (force_update == false))
00356     {
00357         return false;
00358     } else {
00359         last_update_index = temp_index;   
00360     }
00361 
00362     int start_index = (temp_index-temp_count)%MAX_TP_POINTS;
00363     if (start_index < 0) {
00364         start_index += MAX_TP_POINTS;
00365     }
00366     int stop_index = temp_index;
00367     
00368     int prev_y = -1;
00369     int py = 0;
00370     int px = 0;
00371     
00372     if (temp_count == 0)
00373     {
00374         return false;
00375     }
00376     
00377     if (clear) {
00378         clear_curve_overlay(layer);
00379     }
00380     
00381     #define LINE_DRAW
00382     
00383     /*
00384     lcd.SetTextColor(LCD_COLOR_WHITE);
00385     sprintf((char*)text, "U%d %d %d", start_index, stop_index, start_index+MAX_TP_POINTS);
00386     lcd.DisplayStringAt(0, LINE(1), (uint8_t *)&text, LEFT_MODE);  
00387     */
00388     
00389     lcd.SetTextColor(curve_color);
00390     int begin = -1; 
00391     int stop = -1; 
00392 
00393     bool two_step = false;
00394     if (stop_index <= start_index) { 
00395         begin = start_index;
00396         stop = MAX_TP_POINTS;
00397         two_step = true;
00398     } else {
00399         begin = start_index;
00400         stop = stop_index;
00401     }
00402     
00403 
00404     bool draw_done = false;
00405     while (draw_done == false)
00406     {
00407         for (int i=begin; i<stop; i++)
00408         {
00409             py = curve_y_zero_temp - temp_curve[i]*pixel_temp_f;
00410             #ifdef LINE_DRAW
00411             if ((prev_y != -1) && (px<curve_width_y) && (py>curve_y_min_temp-curve_height_x) && (py<curve_y_min_temp))
00412             {
00413                 lcd.DrawLine(px-1+curve_x_zero_time, prev_y, px+curve_x_zero_time, py);
00414             }
00415             prev_y = py;            
00416             #else
00417             if ((px<curve_width_y) && (py>curve_y_min_temp-curve_height_x) && (py<curve_y_min_temp))
00418             {
00419                 lcd.DrawPixel(px+curve_x_zero_time, py, LCD_COLOR_BLACK);
00420             }
00421             #endif
00422             px ++;
00423         }
00424         if (two_step) {
00425             if (begin == 0)
00426             {
00427                 draw_done = true;
00428             } else {
00429                 begin = 0;
00430                 stop = stop_index;
00431             }
00432         } else {
00433             draw_done = true;
00434         }
00435     }
00436     return true;
00437 }
00438 
00439 int plus_x = lcd.GetYSize()*3./4+30;
00440 int plus_y = lcd.GetXSize()*1./4+20;
00441 int minus_x = lcd.GetYSize()*3./4+30;
00442 int minus_y = lcd.GetXSize()*3./4-20;
00443 int radius = lcd.GetXSize()/8.;
00444 
00445 char temp_formatting[] = "  %4.1lf xC  ";
00446 
00447 void update_temperature_read_text()
00448 {
00449     sprintf((char*)text, temp_formatting, last_read_temperature);
00450     lcd.DisplayStringAt(TEMP_DISP_X, TEMP_READ_DISP, (uint8_t *)&text, LEFT_MODE);
00451 }
00452 
00453 void draw_regulation_screen(int layer)
00454 {
00455     temp_formatting[9] = 176; 
00456     
00457     lcd.SetBackColor(LCD_COLOR_BLUE);
00458     lcd.SetTextColor(LCD_COLOR_WHITE);
00459     lcd.Clear(LCD_COLOR_BLUE);
00460     
00461     BSP_LCD_SetFont(&UbuntuFont23);
00462     
00463     lcd.DrawCircle(minus_x, minus_y, radius);
00464     DrawRectCentered(minus_x, minus_y, radius*1.6, 5);
00465 
00466     lcd.DrawCircle(plus_x, plus_y, radius);
00467     DrawRectCentered(plus_x, plus_y, radius*1.6, 5);
00468     DrawRectCentered(plus_x, plus_y, 5, radius*1.6);
00469     
00470     //FASTLITE LOGO + LINE
00471     DrawRectCentered(lcd.GetYSize()/2, lcd.GetXSize()-6, lcd.GetYSize(), 2);
00472     uint8_t* pBmp = (uint8_t*)(fastlite_logo_landscape_90_20);
00473     lcd.DrawBitmap(lcd.GetYSize()-90, lcd.GetXSize()-20, pBmp);
00474     
00475     
00476     //FRINGEEZZ LOGO
00477     #ifdef DEBUG
00478         pBmp = (uint8_t*)(fringeezz_logo_landscape_124_49);
00479         lcd.DrawBitmap(0, lcd.GetXSize()-20-49-40, pBmp);
00480     #else
00481         pBmp = (uint8_t*)(big_fringezz_logo_91_233);
00482         lcd.DrawBitmap(5, 5, pBmp);    
00483     #endif
00484     
00485     sprintf((char*)text, "TEC set-up:");
00486     lcd.DisplayStringAt(TEMP_TEXT_X, TEMP_SET_TEXT, (uint8_t *)&text, LEFT_MODE);
00487     sprintf((char*)text, temp_formatting, temperature_setpoint);
00488     lcd.DisplayStringAt(TEMP_DISP_X, TEMP_SET_DISP, (uint8_t *)&text, LEFT_MODE);
00489     
00490     sprintf((char*)text, "TEC temp:");
00491     lcd.DisplayStringAt(TEMP_TEXT_X, TEMP_READ_TEXT, (uint8_t *)&text, LEFT_MODE);  
00492     
00493     update_temperature_read_text();    
00494 }
00495 
00496 
00497 
00498 void draw_curve_screen(int layer)
00499 {
00500     lcd.SetBackColor(LCD_COLOR_BLUE);
00501     lcd.SetTextColor(LCD_COLOR_WHITE);
00502     lcd.Clear(LCD_COLOR_BLUE);
00503     
00504     uint8_t* pBmp = (uint8_t*)(fringeezz_logo_landscape_124_49);
00505     lcd.DrawBitmap(1, 1, pBmp);
00506    
00507     lcd.SetBackColor(LCD_COLOR_BLUE);
00508     lcd.SetTextColor(LCD_COLOR_WHITE);
00509     BSP_LCD_SetFont(&Font16);
00510     sprintf((char*)text, "TEC temperature");
00511     lcd.DisplayStringAt(130, LINE(0)+10, (uint8_t *)&text, LEFT_MODE);  
00512     
00513     int margin_y = 10;
00514     int margin_x = 21;
00515     int offset_x = 15;
00516     int offset_y = 10;
00517     
00518     lcd.SetTextColor(LCD_COLOR_BLACK);
00519     
00520     int curve_center_y = lcd.GetYSize()/2+offset_y;
00521     int curve_center_x = lcd.GetXSize()/2+offset_x;
00522     curve_height_x = lcd.GetXSize()-offset_x*2-margin_x*2;
00523     curve_width_y = lcd.GetYSize()-offset_y*2-margin_y*2;
00524     
00525     DrawRectCentered(curve_center_y, curve_center_x, curve_width_y, curve_height_x);
00526     
00527     lcd.SetTextColor(LCD_COLOR_WHITE);
00528     DrawRectCentered(curve_center_y, curve_center_x, curve_width_y-2, curve_height_x-2);
00529 
00530     int min_temp = -20;
00531     int max_temp = 35;
00532     int tick_space = 10;
00533 
00534     int delta_temp = max_temp - min_temp;
00535     int vticks_pixels = int((curve_height_x-2)*tick_space/delta_temp);
00536     double max_temp_f = min_temp + (1.*tick_space*curve_height_x)/vticks_pixels;
00537     double delta_temp_f = max_temp - min_temp;
00538     pixel_temp_f = curve_height_x/delta_temp;
00539     curve_y_min_temp = curve_center_x+curve_height_x/2-1;
00540     curve_y_zero_temp = curve_y_min_temp+(1.*min_temp*vticks_pixels/tick_space);    
00541     curve_x_zero_time = curve_center_y-curve_width_y/2+1;
00542     
00543     BSP_LCD_SetFont(&Font12);
00544     for (int i=0; i<curve_height_x/vticks_pixels+1; i++)
00545     {
00546         lcd.SetTextColor(LCD_COLOR_BLACK);
00547 
00548         int vtick_pos = curve_center_x+curve_height_x/2-1-i*(vticks_pixels);       
00549         int vtick_pos_x = curve_center_y-curve_width_y/2-1;    
00550         lcd.DrawVLine(vtick_pos_x+5+1, vtick_pos, 5);
00551 
00552         lcd.SetTextColor(LCD_COLOR_WHITE);
00553 
00554         sprintf((char*)text, "%d", min_temp+10*i);
00555         lcd.DisplayStringAt(30,  vtick_pos-6, (uint8_t *)&text, RIGHT_MODE);  
00556 
00557         lcd.SetTextColor(LCD_COLOR_BLACK);
00558 
00559         vtick_pos = curve_center_x+curve_height_x/2-1-i*(vticks_pixels);       
00560         vtick_pos_x = curve_center_y+curve_width_y/2-1;    
00561         lcd.DrawVLine(vtick_pos_x+1, vtick_pos, 5);
00562         
00563     }
00564                 
00565     lcd.SetTextColor(LCD_COLOR_WHITE);
00566     BSP_LCD_SetFont(&UbuntuFont23);
00567 
00568     
00569     //FASTLITE LOGO + LINE
00570     DrawRectCentered(lcd.GetYSize()/2, lcd.GetXSize()-6, lcd.GetYSize(), 2);
00571     pBmp = (uint8_t*)(fastlite_logo_landscape_90_20);
00572     lcd.DrawBitmap(lcd.GetYSize()-90, lcd.GetXSize()-20, pBmp);
00573 }
00574 
00575 void draw_screens(int screen, int layer)
00576 {
00577     lcd.SelectLayer(layer);
00578     lcd.Clear(LCD_COLOR_BLUE);
00579     if ((screen == -1) || (screen == -2))
00580     {
00581         draw_curve_screen(layer);
00582         
00583         lcd.SetBackColor(LCD_COLOR_BLUE);
00584         lcd.SetTextColor(LCD_COLOR_WHITE);
00585         BSP_LCD_SetFont(&Font16);
00586         if (screen == -1) { 
00587             sprintf((char*)text, "(%dms)", DELAY_ACQ+TS_READ_TIME);
00588         } else {
00589             sprintf((char*)text, "(%ds)", (DELAY_ACQ+TS_READ_TIME)*TEMPERATURE_SLOW_ACCUMULATE/1000);
00590         }
00591         lcd.DisplayStringAt(130, LINE(1)+10, (uint8_t *)&text, LEFT_MODE);  
00592     } else {
00593         if (screen == 0) {
00594             draw_regulation_screen(layer);
00595         } else if (screen == 1) {
00596             draw_version_screen(layer);
00597         }
00598     }
00599     
00600     #ifdef DEBUG
00601         lcd.SelectLayer(layer);
00602         lcd.SetBackColor(LCD_COLOR_BLUE);
00603         lcd.SetTextColor(LCD_COLOR_WHITE);
00604         BSP_LCD_SetFont(&UbuntuFont23);
00605         sprintf((char*)text, "screen(%d)", screen);
00606         lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00607     #endif
00608 }
00609 
00610 
00611 int main()
00612 {
00613     TS_StateTypeDef TS_State;
00614     uint16_t x, y;
00615     uint8_t ts_status;
00616 
00617     load_temperature_storage();
00618  
00619     /*initialize lcd*/
00620     lcd.SelectLayer(2);
00621     BSP_LCD_SetTransparency_NoReload(2, 255);
00622     BSP_LCD_ResetColorKeying_NoReload(2);
00623     lcd.Clear(LCD_COLOR_BLUE);
00624     BSP_LCD_SetLayerVisible_NoReload(2, ENABLE);
00625     LCD_Reload_Safe();
00626     BSP_LCD_SetFont(&UbuntuFont23);
00627     
00628     lcd.SelectLayer(1);
00629     BSP_LCD_SetTransparency_NoReload(1, 255);
00630     BSP_LCD_ResetColorKeying_NoReload(1);
00631     lcd.Clear(LCD_COLOR_BLUE);
00632     BSP_LCD_SetLayerVisible_NoReload(2, DISABLE);
00633     BSP_LCD_SetLayerVisible_NoReload(1, ENABLE);
00634     LCD_Reload_Safe();
00635     lcd.SetTextColor(LCD_COLOR_BLUE);
00636     BSP_LCD_SetFont(&UbuntuFont23);
00637 
00638     lcd.SelectLayer(0);
00639     BSP_LCD_SetTransparency_NoReload(0, 255);
00640     BSP_LCD_ResetColorKeying_NoReload(0);
00641     lcd.Clear(LCD_COLOR_BLUE);
00642     BSP_LCD_SetLayerVisible_NoReload(1, DISABLE);
00643     BSP_LCD_SetLayerVisible_NoReload(0, ENABLE);
00644     LCD_Reload_Safe();
00645     lcd.SetTextColor(LCD_COLOR_BLUE);
00646     BSP_LCD_SetFont(&UbuntuFont23);
00647     
00648     ts_status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
00649     
00650     
00651     int layer_startup = 0;    
00652     draw_startup_screen(layer_startup);
00653     wait(0.01);
00654     
00655     check_temperature_storage(); /*takes 1s */
00656     
00657     //FADE
00658     int fade_count = 30;
00659     fade_screen(layer_startup, fade_count, LCD_COLOR_BLUE, DEFAULT_LAYER);
00660     //FADE DONE
00661     
00662     #define TOUCH_COUNT 512
00663     int touch_history_x[TOUCH_COUNT];
00664     int touch_history_y[TOUCH_COUNT];
00665     int touch_history_count = 0; 
00666     bool touch_screen_idle = false;
00667     int previous_x = -1;
00668     int previous_y = -1;
00669     int swipe_pos_x_start = -1;
00670     
00671     int swipe_dir = 0;
00672     bool start_swipe = false;
00673     int swipe_pos = 0;
00674     int selected_screen = 0;
00675     
00676     enable_curve_overlay(DEFAULT_LAYER+1, false);
00677     draw_screens(selected_screen, DEFAULT_LAYER);
00678     
00679     int accel = 1;
00680     Timer t;
00681     t.start();
00682     Timer idle_time;
00683     t.start();
00684     
00685     while (ts_status != TS_OK) {
00686         lcd.SelectLayer(DEFAULT_LAYER);
00687         lcd.SetTextColor(LCD_COLOR_RED);
00688         lcd.DisplayStringAt(0, LINE(0), (uint8_t *)"TOUCH INIT FAIL", LEFT_MODE); /*center mode is broken in landscape for fixing right mode*/
00689         lcd.SetTextColor(LCD_COLOR_WHITE);
00690     }
00691     
00692     int dest_loc = 0;
00693     int swipe_layer = 1;
00694     uint32_t swipe_layer_address = LCD_FRAME_BUFFER+(lcd.GetXSize()*lcd.GetYSize()*4)*swipe_layer;
00695     int destination_screen = 0 ;
00696 
00697     /*
00698     int source_layer = 0;
00699     int dest_layer = 0;
00700     int backup_layer = 0;
00701     int backup_loc = 0;
00702     */
00703     int curve_update = 0;
00704     while(1)
00705     {
00706       ts.GetState(&TS_State);  
00707       //#ifdef TOUCHSCREEN_DEMO    
00708       if ((TS_State.TouchDetected) && (start_swipe == false))
00709       {
00710         idle_time.reset();
00711 
00712         x = TS_State.X;
00713         y = TS_State.Y;
00714         
00715         int portrait_x = lcd.GetYSize()-y;
00716         int portrait_y = x;
00717         
00718         x = portrait_x;
00719         y = portrait_y;
00720         
00721 
00722         
00723         //sprintf((char*)text, "%d %d %d", minus_dist, plus_dist, radius*radius);
00724         //lcd.DisplayStringAt(0, LINE(6), (uint8_t *)&text, LEFT_MODE);
00725 
00726         if (touch_screen_idle) { 
00727             touch_screen_idle = false;
00728             previous_y = -1;
00729             previous_x = -1;
00730         }
00731         
00732         bool button_pressed = false;
00733         
00734         /* handle buttons */
00735         if (selected_screen == 0) /* screen has buttons (regulation screen) */ {
00736             int minus_dist = (x-minus_x)*(x-minus_x) + (y-minus_y)*(y-minus_y);
00737             int plus_dist = (x-plus_x)*(x-plus_x) + (y-plus_y)*(y-plus_y);
00738             
00739             if  (minus_dist <= radius*radius) {
00740                 button_pressed = true;
00741                 lcd.SetTextColor(LCD_COLOR_RED);
00742                 lcd.DrawCircle(minus_x, minus_y, radius);
00743                 temperature_setpoint = temperature_setpoint - 0.1;
00744                 sprintf((char*)text, temp_formatting, temperature_setpoint);
00745                 lcd.DisplayStringAt(TEMP_DISP_X, TEMP_SET_DISP, (uint8_t *)&text, LEFT_MODE);
00746                 write_temperature_setpoint(temperature_setpoint);
00747                 wait(0.1/max(accel/10,1));
00748                 lcd.SetTextColor(LCD_COLOR_WHITE);
00749                 lcd.DrawCircle(minus_x, minus_y, radius);
00750                 if (accel < 1000)
00751                 {
00752                     accel += 1;
00753                 }
00754             } else {
00755                 if  (plus_dist <= radius*radius) {
00756                     button_pressed = true;
00757                     lcd.SetTextColor(LCD_COLOR_GREEN);
00758                     lcd.DrawCircle(plus_x, plus_y, radius);
00759                     temperature_setpoint = temperature_setpoint + 0.1;
00760                     sprintf((char*)text, temp_formatting, temperature_setpoint);
00761                     lcd.DisplayStringAt(TEMP_DISP_X, TEMP_SET_DISP, (uint8_t *)&text, LEFT_MODE);
00762                     write_temperature_setpoint(temperature_setpoint);
00763                     wait(0.1/max(accel/10,1));
00764                     lcd.SetTextColor(LCD_COLOR_WHITE);
00765                     lcd.DrawCircle(plus_x, plus_y, radius);
00766                     if (accel < 1000)
00767                     {
00768                         accel += 1;
00769                     }
00770                 }
00771             }
00772         }
00773         
00774         /* handle swipe */
00775         if (not button_pressed) {
00776             accel = 0;
00777             if (start_swipe == false)
00778             {
00779                 if (swipe_pos_x_start == -1)
00780                 {
00781                     swipe_pos_x_start = x;
00782                 } else {
00783                     if (abs(x - swipe_pos_x_start) > 15) {
00784                         start_swipe = true;
00785                         if ((x-swipe_pos_x_start) < 0)
00786                         {
00787                             swipe_dir = +1;
00788                         } else {
00789                             swipe_dir = -1;
00790                         }
00791                     }
00792                 }
00793             } else {
00794              /*swipe started do nothing*/
00795             }
00796             //sprintf((char*)text, "x=%d, px=%d y=%d    ", x, swipe_pos_x_start, touch_screen_idle);
00797             //lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00798         }
00799         
00800 
00801         previous_x = x;
00802         previous_y = y;
00803 
00804         #ifdef TOUCH_TEST
00805         swo.puts((char*)text);
00806         ser.puts((char*)text);
00807         for (int l=0; l<10; l++) {
00808             sprintf((char*)text, "l=%d, x=%d y=%d    ", l, x, y);
00809             lcd.DisplayStringAt(0, LINE(l), (uint8_t *)&text, LEFT_MODE);
00810         }
00811         #endif
00812       } else {
00813         //no touch
00814         if (not touch_screen_idle)
00815         {
00816             idle_time.start();
00817         }
00818         if (idle_time.read_ms() >= 100)
00819         {
00820             touch_history_count = 0;
00821             //sprintf((char*)text, "px=%d i=%d    ", swipe_pos_x_start, touch_screen_idle);
00822             //lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00823             
00824             if (not touch_screen_idle) {
00825                 touch_screen_idle = true;
00826                 swipe_pos_x_start = -1;
00827             }
00828 
00829         } else {
00830             
00831         }
00832         accel = 0;   
00833       }
00834       
00835       if (start_swipe && (((selected_screen == 1) && (swipe_dir == +1)) || ((selected_screen == -2) && (swipe_dir == -1))))
00836       {
00837           start_swipe = false;
00838       } 
00839           
00840       if (start_swipe == true)
00841       {
00842 
00843           if (swipe_pos >= lcd.GetYSize())
00844           {
00845               start_swipe = false;
00846               
00847               wait(1./LCD_REFRESH_RATE);
00848               
00849               // swipe occurs to 
00850               
00851               // swipe to 0. changes the pointer
00852               BSP_LCD_SetLayerAddress_NoReload(swipe_layer, swipe_layer_address);
00853                 
00854               //copy current displayed layer to 0
00855               if (dest_loc != DEFAULT_LAYER) {
00856                 BSP_LCD_CopyLayer(dest_loc, DEFAULT_LAYER, 0); //overwrite layer 0 with data displayed        
00857               }
00858               
00859               if (DEFAULT_LAYER != swipe_layer) {
00860                     if (swipe_layer == DEFAULT_LAYER+1)
00861                     {
00862                         if (destination_screen < 0) /* destination screen has overlay */
00863                         {
00864                             /* clear the swipe layer for overlay */
00865                             /* 0 != 1 */
00866                             /* this means it goes DOWN */
00867                             /* swipe layer is 1 */
00868                             /* swipe layer is pointing to dest_loc */
00869                             /* dest_loc is copied to layer 0 */
00870                             
00871                             /* clear_curve_overlay(swipe_layer); this flickers because layer is allready visible */
00872                             BSP_LCD_SetLayerVisible_NoReload(swipe_layer, ENABLE);
00873                             curve_update = 0;
00874                         } else {
00875                             BSP_LCD_SetLayerVisible_NoReload(swipe_layer, DISABLE);
00876                         }
00877                     } else {
00878                         BSP_LCD_SetLayerVisible_NoReload(swipe_layer, DISABLE);
00879                     }
00880                     lcd.SelectLayer(DEFAULT_LAYER);
00881                     BSP_LCD_SetLayerVisible_NoReload(DEFAULT_LAYER, ENABLE);
00882               } else {
00883                     /* it swipes UP */
00884                     /* there is a not displayed overlay clear it before */
00885                     if (destination_screen < 0) /* destination screen has overlay */
00886                     {
00887                         /* clear the swipe layer for overlay */
00888                         /* clear_curve_overlay(DEFAULT_LAYER+1); */                   
00889                         BSP_LCD_SetLayerVisible_NoReload(DEFAULT_LAYER+1, ENABLE);
00890                         /* go from 1 to 0*/
00891                         
00892                         curve_update = 0;
00893                     } else {
00894                         /* should allready be disabled */
00895                         BSP_LCD_SetLayerVisible_NoReload(DEFAULT_LAYER+1, DISABLE);
00896                     }
00897               }
00898               lcd.SelectLayer(DEFAULT_LAYER);
00899               wait(1./LCD_REFRESH_RATE);
00900               LCD_Reload_Safe();
00901 
00902               //BSP_LCD_Reload(LTDC_RELOAD_IMMEDIATE);
00903               selected_screen = destination_screen;
00904               
00905               swipe_pos = 0;
00906               swipe_dir = 0;
00907           } else {
00908               if (swipe_pos == 0) {
00909                 /* start swipe */
00910                 
00911                 wait(0.1);
00912                 
00913                 /* if (selected_screen < 0)
00914                 {
00915                     // this would flicker
00916                     clear_curve_overlay(DEFAULT_LAYER+1); //hide the curve displayed
00917                     lcd.SelectLayer(DEFAULT_LAYER);
00918                    
00919                 }
00920                 */
00921                 
00922                 dest_loc = 1-swipe_dir;
00923                 if (dest_loc == 0)
00924                 {
00925                     swipe_layer = 1;
00926                 } else {
00927                     if (dest_loc == 2)
00928                     {
00929                         swipe_layer = 0;
00930                         dest_loc = 1;
00931                     } else {
00932                         start_swipe = false;
00933                         swipe_pos = 0;
00934                         swipe_dir = 0;    
00935                     }
00936                 }
00937                 
00938                 swipe_layer_address = LCD_FRAME_BUFFER+(lcd.GetXSize()*lcd.GetYSize()*4)*swipe_layer;
00939 
00940                 if (swipe_layer == DEFAULT_LAYER) {
00941                     if (selected_screen < 0) /* selected screen has overlay */ {
00942                         /* hides overlay */
00943                         BSP_LCD_SetLayerVisible_NoReload(DEFAULT_LAYER+1, DISABLE);  
00944                         /* update destination display */
00945                         update_temperature_read_curve(DEFAULT_LAYER, selected_screen, false, true);
00946                         wait(1./LCD_REFRESH_RATE);
00947                         /* hides overlay */
00948                     } else {
00949                         /* no overlay do nothing */   
00950                     }
00951                 } else {
00952                     /* copy visible layer to swipe layer */
00953                     BSP_LCD_CopyLayer(DEFAULT_LAYER, swipe_layer, 0); //reload backup of layer 0 or save it
00954                     if (selected_screen < 0) /* selected screen has overlay */ {
00955                         update_temperature_read_curve(swipe_layer, selected_screen, false, true);
00956                     }
00957                     BSP_LCD_SetLayerVisible_NoReload(DEFAULT_LAYER, DISABLE);  
00958                     BSP_LCD_SetLayerVisible_NoReload(swipe_layer, ENABLE);
00959                     wait(1./LCD_REFRESH_RATE);
00960                 }
00961                 LCD_Reload_Safe();
00962                                 
00963                 destination_screen = selected_screen + swipe_dir;
00964                 //prepare destination screen (destination screen will be 2)
00965                 //cannot be overwriten by previous command
00966 
00967                 draw_screens(destination_screen, dest_loc);
00968                 /*
00969                 lcd.SelectLayer(swipe_layer);
00970                 sprintf((char*)text, "screen(%d)", selected_screen);
00971                 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&text, LEFT_MODE);
00972                 */
00973               } else {
00974                 BSP_LCD_SetLayerAddress(swipe_layer, swipe_layer_address-swipe_dir*lcd.GetXSize()*4*swipe_pos);
00975               }
00976               
00977               wait(0.);
00978               swipe_pos++;
00979           }
00980 
00981       }
00982       
00983       if (t.read_ms() >= DELAY_ACQ and (not start_swipe)) {
00984         //read temperature
00985         last_read_temperature = read_temperature();
00986         
00987         /* slow buffer */
00988         slow_temp_acc = slow_temp_acc + last_read_temperature;
00989         if (accumulated_slow_temp == TEMPERATURE_SLOW_ACCUMULATE-1)
00990         {
00991             temperature_curve_slow[temperature_curve_slow_index]= slow_temp_acc/TEMPERATURE_SLOW_ACCUMULATE;
00992             slow_temp_acc = 0.;
00993             accumulated_slow_temp = 0;
00994             if (temperature_curve_slow_count < MAX_TP_POINTS)
00995             {
00996                 temperature_curve_slow_count++;
00997             }
00998             temperature_curve_slow_index = (temperature_curve_slow_index+1)%MAX_TP_POINTS;
00999         } else {
01000             accumulated_slow_temp++;
01001         }
01002         
01003         temperature_curve_fast[temperature_curve_fast_index] = last_read_temperature;
01004         if (temperature_curve_fast_count < MAX_TP_POINTS)
01005         {
01006             temperature_curve_fast_count++;
01007         }
01008         temperature_curve_fast_index = (temperature_curve_fast_index+1)%MAX_TP_POINTS;
01009         
01010         if (selected_screen < 0)
01011         {
01012             /* diminish flicker */
01013             bool updated = update_temperature_read_curve(DEFAULT_LAYER+2, selected_screen, true, false);
01014             if (updated)
01015             {
01016                 BSP_LCD_CopyLayer(DEFAULT_LAYER+2, DEFAULT_LAYER+1, 0);
01017                 //LCD_Reload_Safe();
01018                 BSP_LCD_Reload(LTDC_RELOAD_IMMEDIATE);
01019 
01020             } else {
01021                 lcd.SelectLayer(DEFAULT_LAYER);
01022             }
01023             curve_update = (curve_update+1)%2;
01024         } else {
01025             if (selected_screen == 0) { 
01026                 update_temperature_read_text();
01027             }
01028         }
01029         save_temperature_storage();
01030 
01031         
01032         #ifdef DEBUG
01033         reader.open(sector_index);
01034         sprintf((char*)text, "SOF(%d.%p)", reader.get_data_size(), reader.get_physical_data_addr());
01035         lcd.DisplayStringAt(0, LINE(TEMP_SET_LINE+6), (uint8_t *)&text, LEFT_MODE);
01036         reader.close();
01037         #endif
01038         
01039         t.reset();
01040         
01041       }
01042       //#endif
01043       
01044       
01045     }
01046 }