Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: SPI_STMPE610 SPI_TFT_ILI9341 TFT_fonts mbed
main.cpp
00001 /** Tokei 00002 * "Tokei" means a clock or watch in Japanese 00003 */ 00004 00005 #include "mbed.h" 00006 #include "SPI_TFT_ILI9341.h" 00007 #include "SPI_STMPE610.h" 00008 #include "Arial12x12.h" 00009 #include "Arial24x23.h" 00010 #include "Arial28x28.h" 00011 #include "font_big.h" 00012 00013 #define PIN_MOSI PTD2 00014 #define PIN_MISO PTD3 00015 #define PIN_SCLK PTD1 00016 #define PIN_CS_TFT PTD0 00017 #define PIN_DC_TFT PTD5 00018 #define PIN_BL_TFT PTC9 00019 #define PIN_CS_SD PTA4 00020 #define PIN_CS_TSC PTA13 00021 #define PIN_TSC_INTR PTC9 00022 00023 // return values for the adjust menu 00024 #define AM_CANCEL 0x00 00025 #define AM_OK 0x01 00026 #define AM_UP 0x02 00027 #define AM_DOWN 0x04 00028 #define AM_NG 0xFF 00029 00030 SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT) ; 00031 SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ; 00032 00033 DigitalOut backlight(PTA12) ; 00034 00035 Ticker timer ; 00036 bool ticked = false ; 00037 int hour = 14 ; 00038 int min = 40 ; 00039 int sec = 0 ; 00040 char time_str[32] ; 00041 00042 void initTFT(void) 00043 { 00044 //Configure the display driver 00045 TFT.background(Black); 00046 TFT.foreground(White); 00047 wait(0.01) ; 00048 TFT.cls(); 00049 } 00050 00051 void drawAdjustMenu(void) 00052 { 00053 TFT.set_font((unsigned char*) Arial12x12); 00054 TFT.locate(180, 100) ; 00055 TFT.foreground(Yellow) ; 00056 TFT.printf("UP") ; 00057 TFT.locate(180, 160) ; 00058 TFT.foreground(Yellow) ; 00059 TFT.printf("DOWN") ; 00060 TFT.locate(20, 280) ; 00061 TFT.foreground(Blue) ; 00062 TFT.printf("CANCEL") ; 00063 TFT.locate(200, 280) ; 00064 TFT.foreground(Green) ; 00065 TFT.printf("OK") ; 00066 } 00067 00068 void draw_tmp_value(uint16_t value) 00069 { 00070 char str[8] ; 00071 TFT.foreground(White) ; 00072 TFT.set_font((unsigned char*) Arial28x28); 00073 sprintf(str, " %02d ", value) ; 00074 TFT.locate(80, 120) ; 00075 TFT.printf(str) ; 00076 } 00077 00078 int getAdjustMenu(void) 00079 { 00080 uint16_t x, y, z ; 00081 int result = AM_NG ; 00082 if ( TSC.getPoint(&x, &y, &z)) { 00083 if ((180 <= x)&&(x < 240)&&(80 <= y)&&(y < 120)) { // up 00084 result = AM_UP ; 00085 } else if ((180 <= x)&&(x < 240)&&(140 <= y)&&(y < 180)) { // down 00086 result = AM_DOWN ; 00087 } else if ((20 <= x)&&(x < 60)&&(260 <= y)) { // cancel 00088 result = AM_CANCEL ; 00089 } else if ((200 <= x)&&(x < 240)&&(260 <= y)) { // OK 00090 result = AM_OK ; 00091 } 00092 } 00093 return(result) ; 00094 } 00095 00096 void set_hour(void) 00097 { 00098 bool selected = false ; 00099 int16_t tmp_hour, prev_value = -1 ; 00100 tmp_hour = hour ; 00101 TFT.cls() ; 00102 TFT.foreground(White) ; 00103 TFT.set_font((unsigned char*) Arial28x28); 00104 TFT.locate(60, 50) ; 00105 TFT.printf("Set Hour") ; 00106 00107 while(!selected) { 00108 if (tmp_hour != prev_value) { 00109 draw_tmp_value(tmp_hour) ; 00110 drawAdjustMenu() ; 00111 prev_value = tmp_hour ; 00112 } 00113 00114 switch(getAdjustMenu()) { 00115 case AM_CANCEL: 00116 selected = true ; 00117 break ; 00118 case AM_OK: 00119 hour = tmp_hour ; 00120 selected = true ; 00121 break ; 00122 case AM_UP: 00123 tmp_hour++ ; 00124 if (tmp_hour > 23) { 00125 tmp_hour = 0 ; 00126 } 00127 break ; 00128 case AM_DOWN: 00129 tmp_hour-- ; 00130 if (tmp_hour < 0) { 00131 tmp_hour = 0 ; 00132 } 00133 default: 00134 break ; 00135 } 00136 wait(0.1) ; 00137 } 00138 TFT.cls() ; 00139 } 00140 00141 void set_min(void) 00142 { 00143 bool selected = false ; 00144 int16_t tmp_min, prev_value = -1 ; 00145 tmp_min = min ; 00146 TFT.cls() ; 00147 TFT.set_font((unsigned char*) Arial28x28); 00148 TFT.foreground(White) ; 00149 TFT.locate(60, 50) ; 00150 TFT.printf("Set Min") ; 00151 00152 while(!selected) { 00153 if (tmp_min != prev_value) { 00154 draw_tmp_value(tmp_min) ; 00155 drawAdjustMenu() ; 00156 prev_value = tmp_min ; 00157 } 00158 00159 switch(getAdjustMenu()) { 00160 case AM_CANCEL: 00161 selected = true ; 00162 break ; 00163 case AM_OK: 00164 min = tmp_min ; 00165 selected = true ; 00166 break ; 00167 case AM_UP: 00168 tmp_min++ ; 00169 if (tmp_min > 59) { 00170 tmp_min = 0 ; 00171 } 00172 break ; 00173 case AM_DOWN: 00174 tmp_min-- ; 00175 if (tmp_min < 0) { 00176 tmp_min = 0 ; 00177 } 00178 default: 00179 break ; 00180 } 00181 wait(0.1) ; 00182 } 00183 TFT.cls() ; 00184 } 00185 00186 void set_sec(void) 00187 { 00188 bool selected = false ; 00189 int16_t tmp_sec, prev_value = -1 ; 00190 tmp_sec = sec ; 00191 TFT.cls() ; 00192 TFT.foreground(White) ; 00193 TFT.set_font((unsigned char*) Arial28x28); 00194 TFT.locate(60, 50) ; 00195 TFT.printf("Set Sec") ; 00196 00197 while(!selected) { 00198 if (tmp_sec != prev_value) { 00199 draw_tmp_value(tmp_sec) ; 00200 drawAdjustMenu() ; 00201 prev_value = tmp_sec ; 00202 } 00203 00204 switch(getAdjustMenu()) { 00205 case AM_CANCEL: 00206 selected = true ; 00207 break ; 00208 case AM_OK: 00209 sec = tmp_sec ; 00210 selected = true ; 00211 break ; 00212 case AM_UP: 00213 tmp_sec++ ; 00214 if (tmp_sec > 59) { 00215 tmp_sec = 0 ; 00216 } 00217 break ; 00218 case AM_DOWN: 00219 tmp_sec-- ; 00220 if (tmp_sec < 0) { 00221 tmp_sec = 0 ; 00222 } 00223 default: 00224 break ; 00225 } 00226 wait(0.2) ; 00227 } 00228 TFT.cls() ; 00229 } 00230 00231 void set_time(void) 00232 { 00233 set_hour() ; 00234 set_min() ; 00235 set_sec() ; 00236 } 00237 00238 void display_time(void) 00239 { 00240 TFT.set_font((unsigned char*) Arial28x28); 00241 TFT.foreground(White) ; 00242 sprintf(time_str, " %02d:%02d:%02d ",hour,min,sec) ; 00243 TFT.locate(50, 100) ; 00244 wait(0.01) ; 00245 TFT.printf(time_str) ; 00246 wait(0.01) ; 00247 } 00248 00249 void tick_time(void) 00250 { 00251 sec++ ; 00252 if (sec > 59) { 00253 min++ ; 00254 sec = sec % 60 ; 00255 } 00256 if (min > 59) { 00257 hour++ ; 00258 min = min % 60 ; 00259 } 00260 if (hour > 23) { 00261 hour = hour % 24 ; 00262 } 00263 ticked = true ; 00264 } 00265 00266 void draw_adjust_str(void) 00267 { 00268 TFT.set_font((unsigned char*) Arial12x12) ; 00269 TFT.foreground(Blue) ; 00270 TFT.locate(180, 280) ; 00271 TFT.printf("adjust") ; 00272 } 00273 00274 int main() 00275 { 00276 uint16_t x, y ; 00277 00278 initTFT() ; 00279 timer.attach(&tick_time, 1) ; 00280 backlight = 1 ; 00281 set_time() ; 00282 draw_adjust_str() ; 00283 00284 for(;;) { 00285 if (ticked) { 00286 display_time() ; 00287 ticked = false ; 00288 } 00289 00290 if ( TSC.getPoint(&x, &y)) { 00291 if ((180 < x)&&(260 < y)) { 00292 set_time() ; 00293 draw_adjust_str() ; 00294 } 00295 } 00296 } 00297 }
Generated on Wed Jul 20 2022 03:41:04 by
1.7.2