timer

Dependencies:   mbed

Revision:
0:2a4b25dd671a
Child:
1:c19d6ecc34ac
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sat Dec 26 02:58:58 2015 +0000
@@ -0,0 +1,115 @@
+#include "mbed.h"
+#include <string>
+
+//board
+Serial timerLed(PB_6, PA_10);
+DigitalOut led(LED1); //onboard led
+DigitalIn startSW(PB_8);
+DigitalIn goalSW(PB_9);
+
+//setting
+const int DRAW_DELAY    = 230;
+const int SWITCH_DELAY  = 3000;
+const int LED_DELAY     = 1500;
+
+//local
+int system_time = 0;
+int draw_time   = 0;
+int start_time  = 0;
+int goal_time   = 0;
+int led_time    = 0;
+bool running    = false;
+Timer timer; //https://developer.mbed.org/users/okini3939/notebook/timer_jp/
+//time https://developer.mbed.org/users/okini3939/notebook/time_jp/
+
+/*
+ * LED表示
+ */
+void drawLED(int ms){
+    int sec = ms / 1000;
+    int time_min = sec / 60;
+    int time_sec = sec % 60;
+    int time_ms  = ms % 1000;
+    
+    //TODO 0.004秒遅延する
+    timerLed.printf("%1d.", time_min);
+    wait(0.001);
+    
+    timerLed.printf("%2d.", time_sec);
+    wait(0.001);
+    
+    timerLed.printf("%3d", time_ms);
+    wait(0.001);
+    
+    if(running){
+        timerLed.printf("-1");
+    } else {
+        timerLed.printf("-0");
+    }
+    wait(0.001);
+    
+    timerLed.printf("\r");
+}
+
+/*
+ * start
+ */
+void start(){
+    timer.start();
+    running = true;
+}
+
+/*
+ * goal
+ */
+void goal(){
+    timer.stop();
+    running = false;
+    
+    drawLED(timer.read_ms());
+    timer.reset();
+}
+
+/*
+ * Main
+ */    
+int main() {
+
+  int i = 0;
+  
+  wait(1);
+  timerLed.format(8, Serial::None, 1);
+    drawLED(0);      
+    
+  while(1) {       
+    system_time = time(NULL);
+    
+    //start or goal
+    if(startSW == 0){
+        if(start_time + SWITCH_DELAY <= system_time){
+            if(running){
+                goal();
+            } else {
+                start();
+            }
+            
+        }
+    }
+    
+    //draw led
+    if(running && draw_time + DRAW_DELAY <= system_time){
+        draw_time = system_time;
+        drawLED(timer.read_ms());
+    }
+  
+    //led signal
+    if(led_time + 1500 <= LED_DELAY){
+        led_time = system_time;
+        led = !led;
+        i++;
+        if(i > 9) i = 0;
+    }
+    
+
+}
+}