bChange

Dependencies:   TextLCD

Revision:
2:dbb39def2b89
Parent:
1:60e68d110d01
Child:
3:7e046dd4942c
--- a/main.cpp	Mon Jul 27 06:41:27 2020 +0000
+++ b/main.cpp	Tue Jul 28 02:09:24 2020 +0000
@@ -14,16 +14,42 @@
 int b = 0;
 AnalogIn battery(p15);
 Serial pc(USBTX, USBRX); // USBシリアルポートのインスタンス
-double test;
-void change(void const *argument){
+int isBacklight = 0;
+int isTimer = 0;
+void lcdBacklight(void const *argument);
+RtosTimer sensor_timer(lcdBacklight, osTimerPeriodic);
+void lcdBacklight(void const *argument){
+    if(isBacklight){
+        lcd.setBacklight(TextLCD::LightOn);
+    }else{
+        lcd.setBacklight(TextLCD::LightOff);
+    }
+    isBacklight = !isBacklight;
+}
+void change(){
+        lcd.setBacklight(TextLCD::LightOn);
         while(true){
-            //lcd.setBacklight(TextLCD::LightOn);
-            test = battery.read()*MAX_V;
-            b = (int)((test - MIN_V)/0.107 + 0.5)*10;
-            pc.printf("%f\r\n",test);
+            b = (int)((battery.read()* MAX_V - MIN_V)/0.107 + 0.5)*10;
+            if(b < 0){
+                b = 0;
+            }else if(b > 100){
+                b = 100;
+            }
             lcd.locate(0,0);
             lcd.printf("Battery:%3d%%",b);
-            thread_sleep_for(200);
+            if(b <= 30){
+                if(isTimer == 0){
+                    isTimer = 1;
+                    sensor_timer.start(500);
+                }
+            }else{
+                if(isTimer == 1){
+                    sensor_timer.stop();
+                    lcd.setBacklight(TextLCD::LightOn);
+                    isTimer = 0;
+                }
+            }
+            wait_us(1000000);
             /*for(double i = MAX_V;i > MIN_V;i -= 0.05){
                 b = (int)((i - MIN_V)/0.107 + 0.5)*10;
                 //printf("i = %.2f,b=%d%\r\n",i,b);
@@ -51,11 +77,9 @@
 
 int main()
 {
-    Thread thread(change,NULL,osPriorityNormal);
-    //Thread thread;
-    //thread.start(change);
-    thread.terminate();
-    thread.start(mbed::Callback<void()>(change));
+    //Thread thread(change,NULL,osPriorityNormal);
+    Thread thread;
+    thread.start(change);
 
     while (true) {
         wait(1);