Kevin Braun / Mbed 2 deprecated DISCO-F746NG-clockSet

Dependencies:   BSP_DISCO_F746NG BUTTON_GROUP LCD_DISCO_F746NG LCDclockSet_F746 TS_DISCO_F746NG mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "button_group.hpp"
00003 #include "LCDclockSet.h"                    //various display graphics for clock
00004 
00005 #if defined(TARGET_DISCO_F746NG)
00006 #endif
00007 
00008 //------------------------
00009 //hardware equates
00010 
00011 LCD_DISCO_F746NG lcd;                       // Object for LCD display
00012 TS_DISCO_F746NG ts;                         // Object for touch pannel
00013 RawSerial pc(USBTX, USBRX);                 // terminal console
00014 DigitalOut gled(LED1);
00015 
00016 LCDclockSet ss("ss");                       //graphic display items
00017 
00018 TS_StateTypeDef TS_State;
00019 
00020 //------------------------
00021 //LCD specific variables and constants
00022 
00023 uint8_t lcd_text[30];
00024 
00025 //------------------------
00026 //RTC specific variables and constants
00027 
00028 #define TBUFLEN                     10      //size of smaller ascii time buffers
00029 time_t ctTime;                              //time structure
00030 int DST = 1;                                //Daylight Saving Time (or as defined in .ini file)
00031 int TZone = -8;                             //Time Zone from UTC (or as defined in .ini file)
00032 char timebuf_s[4];
00033 char timebuf_hm[TBUFLEN];                   //local time format buffer - 21:16
00034 char timebuf_hms[TBUFLEN];                  //local time format buffer - 21:16:43
00035 char timebuf_mdy[TBUFLEN];                  //local time format buffer - 04/01/14
00036 char timebuf_dMyy[TBUFLEN + 4];             //local time format buffer - 01-Apr-2014
00037 char timebuf_Myy[TBUFLEN];                  //local time format buffer - -Apr-2014
00038 char timebuf_dow[1];                        //local time format buffer - 5
00039 
00040 //--------------------------------------------------------------------------------------------------------------------------------------//
00041 // Display clock on LCD
00042 
00043 #define CLOCKX                  338
00044 #define CLOCKLINE               21
00045 
00046 void DispClockLCD() {
00047     sprintf((char*)lcd_text, "%s %s", timebuf_dMyy, timebuf_hms);
00048     uint32_t tc = lcd.GetTextColor();
00049     lcd.SetTextColor(LCD_COLOR_YELLOW);
00050     lcd.DisplayStringAt(CLOCKX, LINE(CLOCKLINE) + 8, (uint8_t *)&lcd_text, LEFT_MODE);
00051     lcd.SetTextColor(tc);
00052 }
00053 
00054 //--------------------------------------------------------------------------------------------------------------------------------------//
00055 // Update time
00056 
00057 void UpdateTimeRegs() {
00058     strftime(timebuf_s, 4, "%S", localtime(&ctTime));
00059     strftime(timebuf_mdy, TBUFLEN, "%m/%d/%y", localtime(&ctTime));
00060     strftime(timebuf_dMyy, TBUFLEN * 2, "%d-%b-%Y", localtime(&ctTime));
00061     strftime(timebuf_Myy, TBUFLEN, "-%b-%Y", localtime(&ctTime));
00062     strftime(timebuf_hm, TBUFLEN, "%H:%M", localtime(&ctTime));
00063     strftime(timebuf_hms, TBUFLEN, "%H:%M:%S", localtime(&ctTime));
00064     strftime(timebuf_dow, 1, "%u", localtime(&ctTime));
00065 }
00066 
00067 void UpdateTime() {
00068     ctTime = time(NULL) + ((TZone + DST) * 3600);   //timezone and dst offsets
00069     UpdateTimeRegs();
00070 }
00071 
00072 //--------------------------------------------------------------------------------------------------------------------------------------//
00073 //Wait and get a touched character
00074 
00075 int readTouch() {
00076     do{
00077         wait_ms(50);
00078         ts.GetState(&TS_State);
00079     } while(!(TS_State.touchDetected));
00080     int TouchData = 255;
00081     uint8_t idx;
00082     uint16_t x, y;
00083     do{
00084         wait_ms(50);
00085         for (idx = 0; idx < TS_State.touchDetected; idx++) {
00086             x = TS_State.touchX[idx];
00087             y = TS_State.touchY[idx];
00088             TouchData = ss.Touch12Block(x, y);
00089         }
00090         ts.GetState(&TS_State);
00091     } while(TS_State.touchDetected);
00092     return(TouchData);
00093 }
00094     
00095 //--------------------------------------------------------------------------------------------------------------------------------------//
00096 //Get 6 decimal characters from the TFT / touchscreen - use for date and time setting
00097 
00098 char dtArray[6];
00099 
00100 void Get6() {
00101     while(1) {
00102         int rt = 255;
00103         int b = 1;
00104         bool isNumb = false;
00105         while(1) {
00106             do{
00107                 wait_ms(100);
00108                 rt = readTouch();
00109             } while(rt == 255);
00110             
00111             if(rt < 10) {
00112                 ss.TouchClockBlock(b, rt, false);
00113                 isNumb = true;
00114                 dtArray[b - 1] = rt;
00115                 if(b < 6) b++;
00116             }
00117             if(rt == '<') {
00118                 ss.TouchClockBlock(b, NULL, true);
00119                 if(b > 1) {
00120                     b--;
00121                     ss.TouchClockBlock(b, NULL, true);
00122                 }
00123                 isNumb = false;
00124             }
00125             if((rt == '#') && (b == 6) && (isNumb == true)) {
00126                 isNumb = false;
00127                 return;
00128             }
00129             if(rt == '#') {
00130                 isNumb = false;
00131                 wait_ms(100);
00132             }
00133             if(b > 6) b = 6;
00134             if(b < 1) b = 1;
00135             rt = 255;
00136             wait_ms(300);
00137         }
00138     }
00139 }
00140 
00141 //--------------------------------------------------------------------------------------------------------------------------------------//
00142 //Manually set the date and time
00143 
00144 char DateTime[12]; 
00145 
00146 void SetRTClcd() {
00147     ss.Draw12Block(1, LCD_COLOR_DARKGREEN, LCD_COLOR_YELLOW);  
00148     ss.DrawClockBlock('/'); 
00149     Get6();
00150     for(int i = 0; i < 6; i++) {
00151         DateTime[i] = dtArray[i];
00152     }
00153     ss.Draw12Block(1, LCD_COLOR_DARKGREEN, LCD_COLOR_YELLOW); 
00154     ss.DrawClockBlock(':'); 
00155     Get6();
00156     for(int i = 0; i < 6; i++) {
00157         DateTime[i + 6] = dtArray[i];
00158     }
00159 
00160     struct tm t;
00161     t.tm_year = (2000 + (DateTime[4] * 10) + DateTime[5]);
00162     t.tm_mon = ((DateTime[0] * 10) + DateTime[1]);
00163     t.tm_mday = ((DateTime[2] * 10) + DateTime[3]);
00164     t.tm_hour = ((DateTime[6] * 10) + DateTime[7]);
00165     t.tm_min = ((DateTime[8] * 10) + DateTime[9]);
00166     t.tm_sec = ((DateTime[10] * 10) + DateTime[11]);
00167     
00168     // adjust for tm structure required values
00169     t.tm_year = t.tm_year - 1900;
00170     t.tm_mon = t.tm_mon - 1;    
00171     // set the time
00172     time_t ctTime = mktime(&t);
00173     ctTime = ctTime - (TZone + DST) * 3600;     //take out local time
00174     set_time(ctTime);
00175     UpdateTime();
00176 }
00177 
00178 //--------------------------------------------------------------------------------------------------------------------------------------//
00179 //main
00180 
00181 int main()
00182 {
00183     //TS_StateTypeDef TS_State;
00184     uint16_t x, y;
00185     uint8_t status;
00186     uint8_t idx;
00187     uint8_t cleared = 0;
00188     uint8_t prev_nb_touches = 0;
00189     
00190     gled = 1;
00191     
00192     pc.baud(230400);
00193     
00194     // Setting of button group
00195     lcd.Clear(LCD_COLOR_BLACK);
00196     lcd.SetBackColor(LCD_COLOR_BLACK);
00197     lcd.SetTextColor(LCD_COLOR_CYAN);
00198     lcd.SetFont(&Font20);
00199     lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"Set Clock Via LCD Demo", CENTER_MODE);
00200     wait(3.0);
00201     
00202     status = ts.Init(lcd.GetXSize(), lcd.GetYSize());
00203     if (status != TS_OK) {
00204         lcd.Clear(LCD_COLOR_RED);
00205         lcd.SetBackColor(LCD_COLOR_RED);
00206         lcd.SetTextColor(LCD_COLOR_WHITE);
00207         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT FAIL", CENTER_MODE);
00208     } else {
00209         lcd.Clear(LCD_COLOR_GREEN);
00210         lcd.SetBackColor(LCD_COLOR_GREEN);
00211         lcd.SetTextColor(LCD_COLOR_WHITE);
00212         lcd.DisplayStringAt(0, LINE(5), (uint8_t *)"TOUCHSCREEN INIT OK", CENTER_MODE);
00213     }
00214 
00215     wait(1.5);
00216     
00217     //go set the RTC via the LCD
00218     SetRTClcd();
00219 
00220     lcd.SetFont(&Font12);
00221     lcd.Clear(LCD_COLOR_BLUE);
00222     lcd.SetBackColor(LCD_COLOR_BLUE);
00223     lcd.SetTextColor(LCD_COLOR_WHITE);
00224 
00225     //the touchscreen demo with RTC in the lower-right corner
00226     while (true) {
00227         ts.GetState(&TS_State);
00228         if (TS_State.touchDetected) {
00229             // Clear lines corresponding to old touches coordinates
00230             if (TS_State.touchDetected < prev_nb_touches) {
00231                 for (idx = (TS_State.touchDetected + 1); idx <= 5; idx++) {
00232                     lcd.ClearStringLine(idx);
00233                 }
00234             }
00235             prev_nb_touches = TS_State.touchDetected;
00236 
00237             cleared = 0;
00238 
00239             sprintf((char*)lcd_text, "Touched: %d", TS_State.touchDetected);
00240             lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&lcd_text, LEFT_MODE);
00241 
00242             for (idx = 0; idx < TS_State.touchDetected; idx++) {
00243                 x = TS_State.touchX[idx];
00244                 y = TS_State.touchY[idx];
00245                 sprintf((char*)lcd_text, "Touch %d: x=%d y=%d    ", idx+1, x, y);
00246                 lcd.DisplayStringAt(0, LINE(idx+1), (uint8_t *)&lcd_text, LEFT_MODE);
00247             }
00248             lcd.DrawPixel(TS_State.touchX[0], TS_State.touchY[0], LCD_COLOR_ORANGE);
00249         } else {
00250             if (!cleared) {
00251                 lcd.Clear(LCD_COLOR_BLUE);
00252                 sprintf((char*)lcd_text, "Touches: 0");
00253                 lcd.DisplayStringAt(0, LINE(0), (uint8_t *)&lcd_text, LEFT_MODE);
00254                 cleared = 1;
00255             }
00256         }
00257         gled = !gled;
00258         wait_ms(200);
00259         UpdateTime();
00260         DispClockLCD();
00261     }
00262 }