While I was thinking about logging sensor values, I noticed that I have not got clock for this board. Blowsing the mbed API, I found that the ticker could be used. So this is my first trial of implementing a clock using the ticker. Meantime I added getPoint in my SPI_STMPE610 lib so that the x and y coordinate can be directly read.
Dependencies: SPI_STMPE610 SPI_TFT_ILI9341 TFT_fonts mbed
Tokei means a clock or a watch in Japanese. To log values of sensor(s) I needed a way to trace the time. At first, knowing that the RTC is not connected in FRDM-KL25Z I thought it must be difficult to implement a clock, but finding the "ticker" in mbed lib, at least I could give a try.
センサの値をログするのに時計機能が欲しいかなと思っていたのですが、FRDM-KL25ZではRTCに32KHzが接続されていないのでちょっと難しいかなと mbed の API を見ていたら ticker というのがありました(笑)
Since it must be painful to set the time each time powering the board, I implemented the minimum functions to set the time (hour, min, sec) using the Adafruit 2.8" TFT and Touch Sensor.
流石に起動毎にシリアルから時間を設定するのは厳しそうだったので とりあえず時、分、秒を設定できる最低限のUIも付けて見ました。

Diff: main.cpp
- Revision:
- 0:94d52137914c
- Child:
- 1:a3749e465942
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Sat Dec 13 08:48:34 2014 +0000
@@ -0,0 +1,308 @@
+/** Tokei
+ * "Tokei" means a clock or watch in Japanese
+ */
+
+#include "mbed.h"
+#include "SPI_TFT_ILI9341.h"
+#include "SPI_STMPE610.h"
+#include "Arial12x12.h"
+#include "Arial24x23.h"
+#include "Arial28x28.h"
+#include "font_big.h"
+
+#define PIN_MOSI PTD2
+#define PIN_MISO PTD3
+#define PIN_SCLK PTD1
+#define PIN_CS_TFT PTD0
+#define PIN_DC_TFT PTD5
+#define PIN_BL_TFT PTC9
+#define PIN_CS_SD PTA4
+#define PIN_CS_TSC PTA13
+#define PIN_TSC_INTR PTC9
+
+// return values for the adjust menu
+#define AM_CANCEL 0x00
+#define AM_OK 0x01
+#define AM_UP 0x02
+#define AM_DOWN 0x04
+#define AM_NG 0xFF
+
+SPI_TFT_ILI9341 TFT(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TFT, PIN_BL_TFT, PIN_DC_TFT) ;
+SPI_STMPE610 TSC(PIN_MOSI, PIN_MISO, PIN_SCLK, PIN_CS_TSC) ;
+
+DigitalOut backlight(PTA12) ;
+
+Ticker timer ;
+bool ticked = false ;
+int year = 2014 ;
+int month = 12 ;
+int day = 13 ;
+int hour = 14 ;
+int min = 40 ;
+int sec = 0 ;
+char time_str[32] ;
+
+void initTFT(void)
+{
+ //Configure the display driver
+ TFT.background(Black);
+ TFT.foreground(White);
+ wait(0.01) ;
+ TFT.cls();
+}
+
+void drawAdjustMenu(void)
+{
+ TFT.set_font((unsigned char*) Arial12x12);
+ TFT.locate(180, 100) ;
+ TFT.foreground(Yellow) ;
+ TFT.printf("UP") ;
+ TFT.locate(180, 160) ;
+ TFT.foreground(Yellow) ;
+ TFT.printf("DOWN") ;
+ TFT.locate(20, 280) ;
+ TFT.foreground(Blue) ;
+ TFT.printf("CANCEL") ;
+ TFT.locate(200, 280) ;
+ TFT.foreground(Green) ;
+ TFT.printf("OK") ;
+}
+
+int getAdjustMenu(void)
+{
+ uint16_t x, y, z ;
+ int result = AM_NG ;
+ if ( TSC.getPoint(&x, &y, &z)) {
+ if ((180 <= x)&&(x < 240)&&(80 <= y)&&(y < 120)) { // up
+ result = AM_UP ;
+ } else if ((180 <= x)&&(x < 240)&&(140 <= y)&&(y < 180)) { // down
+ result = AM_DOWN ;
+ } else if ((20 <= x)&&(x < 60)&&(260 <= y)) { // cancel
+ result = AM_CANCEL ;
+ } else if ((200 <= x)&&(x < 240)&&(260 <= y)) { // OK
+ result = AM_OK ;
+ }
+ }
+ return(result) ;
+}
+
+void set_hour(void)
+{
+ bool selected = false ;
+ int16_t tmp_hour, prev_value = -1 ;
+ char str[4] ;
+ tmp_hour = hour ;
+ TFT.cls() ;
+ TFT.foreground(White) ;
+ TFT.set_font((unsigned char*) Arial28x28);
+ TFT.locate(60, 50) ;
+ TFT.printf("Set Hour") ;
+
+ while(!selected) {
+ if (tmp_hour != prev_value) {
+ TFT.foreground(White) ;
+ TFT.set_font((unsigned char*) Arial28x28);
+ sprintf(str, " %02d ", tmp_hour) ;
+ TFT.locate(80, 120) ;
+ TFT.printf(str) ;
+
+ drawAdjustMenu() ;
+ prev_value = tmp_hour ;
+ }
+
+ switch(getAdjustMenu()) {
+ case AM_CANCEL:
+ selected = true ;
+ break ;
+ case AM_OK:
+ hour = tmp_hour ;
+ selected = true ;
+ break ;
+ case AM_UP:
+ tmp_hour++ ;
+ if (tmp_hour > 23) {
+ tmp_hour = 0 ;
+ }
+ break ;
+ case AM_DOWN:
+ tmp_hour-- ;
+ if (tmp_hour < 0) {
+ tmp_hour = 0 ;
+ }
+ default:
+ break ;
+ }
+ wait(0.1) ;
+ }
+ TFT.cls() ;
+}
+
+void set_min(void)
+{
+ bool selected = false ;
+ int16_t tmp_min, prev_value = -1 ;
+ char str[4] ;
+ tmp_min = min ;
+ TFT.cls() ;
+ TFT.set_font((unsigned char*) Arial28x28);
+ TFT.foreground(White) ;
+ TFT.locate(60, 50) ;
+ TFT.printf("Set Min") ;
+
+ while(!selected) {
+ if (tmp_min != prev_value) {
+ TFT.set_font((unsigned char*) Arial28x28);
+ TFT.foreground(White) ;
+ sprintf(str, " %02d ", tmp_min) ;
+ TFT.locate(80, 120) ;
+ TFT.printf(str) ;
+
+ drawAdjustMenu() ;
+ prev_value = tmp_min ;
+ }
+
+ switch(getAdjustMenu()) {
+ case AM_CANCEL:
+ selected = true ;
+ break ;
+ case AM_OK:
+ min = tmp_min ;
+ selected = true ;
+ break ;
+ case AM_UP:
+ tmp_min++ ;
+ if (tmp_min > 59) {
+ tmp_min = 0 ;
+ }
+ break ;
+ case AM_DOWN:
+ tmp_min-- ;
+ if (tmp_min < 0) {
+ tmp_min = 0 ;
+ }
+ default:
+ break ;
+ }
+ wait(0.1) ;
+ }
+ TFT.cls() ;
+}
+
+void set_sec(void)
+{
+ bool selected = false ;
+ int16_t tmp_sec, prev_value = -1 ;
+ char str[4] ;
+ tmp_sec = sec ;
+ TFT.cls() ;
+ TFT.foreground(White) ;
+ TFT.set_font((unsigned char*) Arial28x28);
+ TFT.locate(60, 50) ;
+ TFT.printf("Set Sec") ;
+
+ while(!selected) {
+ if (tmp_sec != prev_value) {
+ TFT.set_font((unsigned char*) Arial28x28);
+ TFT.foreground(White) ;
+ sprintf(str, " %02d ", tmp_sec) ;
+ TFT.locate(80, 120) ;
+ TFT.printf(str) ;
+
+ drawAdjustMenu() ;
+ prev_value = tmp_sec ;
+ }
+
+ switch(getAdjustMenu()) {
+ case AM_CANCEL:
+ selected = true ;
+ break ;
+ case AM_OK:
+ sec = tmp_sec ;
+ selected = true ;
+ break ;
+ case AM_UP:
+ tmp_sec++ ;
+ if (tmp_sec > 59) {
+ tmp_sec = 0 ;
+ }
+ break ;
+ case AM_DOWN:
+ tmp_sec-- ;
+ if (tmp_sec < 0) {
+ tmp_sec = 0 ;
+ }
+ default:
+ break ;
+ }
+ wait(0.2) ;
+ }
+ TFT.cls() ;
+}
+
+void set_time(void)
+{
+ set_hour() ;
+ set_min() ;
+ set_sec() ;
+}
+
+void display_time(void)
+{
+ TFT.set_font((unsigned char*) Arial28x28);
+ TFT.foreground(White) ;
+ sprintf(time_str, " %02d:%02d:%02d ",hour,min,sec) ;
+ TFT.locate(50, 100) ;
+ wait(0.01) ;
+ TFT.printf(time_str) ;
+ wait(0.01) ;
+}
+
+void tick_time(void)
+{
+ sec++ ;
+ if (sec > 59) {
+ min++ ;
+ sec = sec % 60 ;
+ }
+ if (min > 59) {
+ hour++ ;
+ min = min % 60 ;
+ }
+ if (hour > 23) {
+ day++ ;
+ hour = hour % 24 ;
+ }
+ ticked = true ;
+}
+
+void draw_adjust_str(void)
+{
+ TFT.set_font((unsigned char*) Arial12x12) ;
+ TFT.foreground(Blue) ;
+ TFT.locate(180, 280) ;
+ TFT.printf("adjust") ;
+}
+
+int main()
+{
+ uint16_t x, y ;
+
+ initTFT() ;
+ timer.attach(&tick_time, 1) ;
+ draw_adjust_str() ;
+ backlight = 1 ;
+
+ for(;;) {
+ if (ticked) {
+ display_time() ;
+ ticked = false ;
+ }
+
+ if ( TSC.getPoint(&x, &y)) {
+ if ((180 < x)&&(260 < y)) {
+ set_time() ;
+ draw_adjust_str() ;
+ }
+ }
+ }
+}