Эх сурвалжийг харах

Added timer and statistics

Per Mårtensson 4 жил өмнө
parent
commit
cf41045ab2

BIN
nextion/clock_b.png


BIN
nextion/clock_black.png


BIN
nextion/clock_g.png


BIN
nextion/clock_r.png


BIN
nextion/currentranger.HMI


+ 2 - 2
platformio.ini

@@ -12,7 +12,7 @@
 platform = atmelsam
 board = current_ranger
 framework = arduino
-upload_port = com10
+upload_port = /dev/ttyACM0
 ; change microcontroller
 board_build.mcu = samd21g18a
 
@@ -20,5 +20,5 @@ board_build.mcu = samd21g18a
 board_build.f_cpu = 48000000L
 lib_deps=https://github.com/cmaglie/FlashStorage#master 
         https://github.com/adafruit/Adafruit_FreeTouch#master 
-        seithan/Easy Nextion Library @ ^1.0.4
+        https://git.sweproj.com/IHOLL/EasyNextionLibrary.git#master
 lib_ignore=U8g2

+ 344 - 15
src/main.cpp

@@ -26,7 +26,7 @@
 #include "EasyNextionLibrary.h"
 #endif
 // CurrentRanger Firmware Version
-#define FW_VERSION "1.1.0"
+#define FW_VERSION "1.1.0b"
 
 //***********************************************************************************************************
 #define BIAS_LED       11
@@ -135,6 +135,27 @@ Adafruit_FreeTouch qt[3] = {
 #define NEXTION_REFRESH_INTERVAL       300     //ms
 EasyNex myNex(SerialNextion);
 uint8_t nex_page=0;
+
+bool nex_timer_run=false;
+bool nex_timerstats_run=false;
+uint8_t nex_timer_range = 0;
+uint8_t nex_timer_d0=0;
+uint8_t nex_timer_d1=0;
+uint8_t nex_timer_d2=0;
+uint8_t nex_timer_d3=0;
+uint8_t nex_timer_set=0;
+uint8_t nex_timer_output=0;
+uint32_t nex_timer_start=0;
+uint32_t nex_timer_stop=0;
+String nex_timer_current="";
+bool nex_timer_trigger=false;
+float nex_stats_min = 0;
+float nex_stats_avg = 0;
+uint32_t nex_stats_count=0;
+float nex_stats_max = 0;
+String nex_stats_max_s = "";
+String nex_stats_avg_s = "";
+String nex_stats_min_s = "";
 #endif
 int offsetCorrectionValue = 0;
 uint16_t gainCorrectionValue = 0;
@@ -187,6 +208,7 @@ void saveLDO(float newLdoValue);
 void analogReadCorrectionForced(int offset, uint16_t gain);
 void WDTset();
 void ldoOptimizeRefresh();
+void chronometer();
 float vbat=0, VOUT=0;
 #endif
 //***********************************************************************************************************
@@ -634,15 +656,15 @@ void loop() {
         else 
         myNex.writeNum("p0.pic",0);  //u8g2.drawStr(88,12,"LoBat!");
     }
-    char buff[9];
+    char buff[7];
     float currentout=(((BIAS&&abs(VOUT)>=0.4)||(!BIAS&&VOUT>=0.4))?VOUT:0);
     snprintf (buff, sizeof(buff), "%f", ((BIAS&&abs(VOUT)>=0.4)||(!BIAS&&VOUT>=0.4))?VOUT:0, (abs(VOUT)>=1000?0:1));
     if ((!BIAS && readDiff>ADC_OVERLOAD) || (BIAS && abs(readDiff)>ADC_OVERLOAD/2))
     {
-        if (nex_page == 0 || nex_page==1) 
+        if (nex_page == 0 || nex_page==1 || nex_page==6) 
         myNex.writeStr("current.txt","OVERLOAD!");
     }else{
-        if (nex_page == 0 || nex_page==1) 
+        if (nex_page == 0 || nex_page==1 || nex_page==6) 
         myNex.writeStr("current.txt",String(buff)+rangeUnit+'A');
         if (nex_page==1){
         int Value = (uint8_t) map(currentout,0,3300,0,255);  //Read the pot value ann map it to 0.255 (max value of waveform=255)
@@ -655,6 +677,57 @@ void loop() {
         myNex.writeStr(Tosend,"cmd");
         }
     }  
+    if (nex_page == 5 ){
+      chronometer();
+    }
+    float current_test=currentout;
+    if (rangeUnit=='m'){
+      current_test=current_test*1000000;
+    }
+    if (rangeUnit=='u'){
+      current_test=current_test*1000;
+    }
+    if (nex_timer_run )
+    {
+        
+
+        Serial.println(currentout);
+        Serial.println(current_test);
+        if (nex_timer_set!=0){
+          if (current_test>nex_timer_set){
+            nex_timer_run =false;
+            nex_timer_stop = millis();
+            nex_timer_trigger=true;
+            nex_timer_current = String(buff)+rangeUnit+'A';
+            if (nex_page==5){
+              myNex.writeNum("stoptimer.bco",NEX_BLUE);
+              myNex.writeNum("starttimer.bco",NEX_LIGHT_GREY);
+              myNex.writeStr("timercurrent.txt",nex_timer_current);
+            }
+            if (nex_page==0){
+              myNex.writeNum("timer.pic",21);
+
+            }
+          }
+        }
+        
+    }
+    if (nex_timerstats_run)
+    {
+      if (nex_stats_min > current_test)
+      {
+        nex_stats_min = current_test;
+        nex_stats_min_s = String(buff)+rangeUnit+'A';
+        myNex.writeStr("avgmin.txt",nex_stats_min_s);
+      } 
+      if (nex_stats_max < current_test)
+      {
+        nex_stats_max = current_test;
+        nex_stats_max_s = String(buff)+rangeUnit+'A';
+        myNex.writeStr("avgmax.txt",nex_stats_max_s);
+      } 
+          
+    } 
   }
 #endif
   WDTclear();
@@ -1081,16 +1154,63 @@ void trigger16(){
   if( rangeUnit=='n') myNex.writeNum("b0.bco",NEX_BLUE);
   if( rangeUnit=='u') myNex.writeNum("b1.bco",NEX_BLUE);
   if( rangeUnit=='m') myNex.writeNum("b2.bco",NEX_BLUE);
-
+  if (nex_timer_run){
+        myNex.writeNum("timer.pic",20);
+  }
   nex_page=0;
 }
 void trigger17(){
   lastKeepAlive=millis(); 
   Beep(20, false);
+  myNex.writeStr("page page5","cmd");
+  if( nex_timer_range==0) 
+    myNex.writeNum("tma.bco",NEX_BLUE) ;
+  else 
+    myNex.writeNum("tma.bco",NEX_LIGHT_GREY) ;
+  if( nex_timer_range==1) 
+    myNex.writeNum("tua.bco",NEX_BLUE) ;
+  else 
+    myNex.writeNum("tua.bco",NEX_LIGHT_GREY) ;
+  if( nex_timer_range==2) 
+    myNex.writeNum("tna.bco",NEX_BLUE) ;
+  else 
+    myNex.writeNum("tna.bco",NEX_LIGHT_GREY) ;
+  myNex.writeNum("dec0.val",nex_timer_d0 );
+  myNex.writeNum("dec1.val",nex_timer_d1 );
+  myNex.writeNum("dec2.val",nex_timer_d2 );
+  myNex.writeNum("dec3.val",nex_timer_d3 );
+  if (nex_timer_run){
+    myNex.writeNum("starttimer.bco",NEX_BLUE);
+    myNex.writeNum("stoptimer.bco",NEX_LIGHT_GREY);
+  }else{
+    myNex.writeNum("stoptimer.bco",NEX_BLUE);
+    myNex.writeNum("starttimer.bco",NEX_LIGHT_GREY);  
+  }
+  myNex.writeStr("timercurrent.txt",nex_timer_current);
+  nex_page=5;
+}
+void trigger18(){
+  Beep(20, false);
+  myNex.writeStr("page page6","cmd");
+  if (nex_timerstats_run){
+    myNex.writeNum("startstats.bco",NEX_BLUE);
+    myNex.writeNum("stopstats.bco",NEX_LIGHT_GREY);
+    myNex.writeStr("avgmin.txt",nex_stats_min_s);
+    myNex.writeStr("avgavg.txt",nex_stats_avg_s);
+    myNex.writeStr("avgmax.txt",nex_stats_max_s);
+  }else{
+    myNex.writeNum("stopstats.bco",NEX_BLUE);
+    myNex.writeNum("startstats.bco",NEX_LIGHT_GREY);  
+  }
+  nex_page=6;
+}
+void trigger19(){
+  lastKeepAlive=millis(); 
+  Beep(20, false);
  myNex.writeStr("page page1","cmd");
  nex_page=1;
 }
-void trigger18(){
+void trigger20(){
   lastKeepAlive=millis(); 
   Beep(20, false);
   myNex.writeStr("page page2","cmd");
@@ -1100,18 +1220,18 @@ void trigger18(){
     vbat=adcRead(SENSE_VIN);
     analogReferenceHalf(true);
   }
-    else vbat=adcRead(SENSE_VIN);
+  else vbat=adcRead(SENSE_VIN);
     vbat=((vbat/ADCFULLRANGE) * ldoValue) * 1.5; //1.5 given by vbat->A5 resistor ratio (1 / (2M * 1/(1M+2M)))
-    nex_page=2;
-    myNex.writeStr("page page2","cmd");
-    myNex.writeStr("offset.txt",(String)offsetCorrectionValue);
-    myNex.writeStr("gain.txt",(String)gainCorrectionValue);
-    myNex.writeStr("ldo.txt",String(ldoValue,3));
-    myNex.writeStr("fw.txt",(String)FW_VERSION);
-    myNex.writeStr("bat.txt",(String)vbat);
   nex_page=2;
+  myNex.writeStr("page page2","cmd");
+  myNex.writeStr("offset.txt",(String)offsetCorrectionValue);
+  myNex.writeStr("gain.txt",(String)gainCorrectionValue);
+  myNex.writeStr("ldo.txt",String(ldoValue,3));
+  myNex.writeStr("fw.txt",(String)FW_VERSION);
+  myNex.writeStr("bat.txt",(String)vbat);
+
 }
-void trigger19(){
+void trigger21(){
   lastKeepAlive=millis();
   Beep(20, false);
   myNex.writeStr("page page3","cmd");
@@ -1162,4 +1282,213 @@ void trigger35()
       myNex.writeNum("bluetooth.pic",13);
     }
   }
+}
+void update_current_limit()
+{
+  nex_timer_set= nex_timer_d0*1000+nex_timer_d1*100+nex_timer_d2*10+nex_timer_d0;
+  Beep(20, false);
+}
+
+void chronometer(void){   //This function print:   "New: Actual time"
+  unsigned long currentMillis = millis();  
+  uint32_t diff_time=0;
+  if (nex_timer_run){
+      diff_time=currentMillis-nex_timer_start;
+  }else{
+      diff_time=nex_timer_stop-nex_timer_start;
+  }
+  long days = 0;
+  long hours = 0;
+  long mins = 0;
+  long secs = 0;
+  String secs_o = ":";
+  String mins_o = ":";
+  String hours_o = ":";
+  secs = diff_time / 1000; // set the seconds remaining
+  mins = secs / 60; //convert seconds to minutes
+  hours = mins / 60; //convert minutes to hours
+  days = hours / 24; //convert hours to days
+  secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
+  mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
+  hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
+  if (secs < 10) {
+    secs_o = ":0";
+  }
+  if (mins < 10) {
+    mins_o = ":0";
+  }
+  if (hours < 10) {
+    hours_o = ":0";
+  }
+  myNex.writeStr("timertext.txt",String(hours)+mins_o+String(mins)+secs_o+String(secs));
+}
+
+void trigger64()
+{
+  if (nex_timer_d0==9){
+    nex_timer_d0=0;
+  }else{
+    nex_timer_d0++;
+  }
+  myNex.writeNum("dec0.val",nex_timer_d0);
+  update_current_limit();
+}
+void trigger65()
+{
+  if (nex_timer_d1==9){
+    nex_timer_d1=0;
+  }else{
+    nex_timer_d1++;
+  }
+  myNex.writeNum("dec1.val",nex_timer_d1);
+  update_current_limit();
+}
+void trigger66()
+{
+  if (nex_timer_d2==9){
+    nex_timer_d2=0;
+  }else{
+    nex_timer_d2++;
+  }
+  myNex.writeNum("dec2.val",nex_timer_d2);
+  update_current_limit();
+}
+void trigger67()
+{
+  if (nex_timer_d3==9){
+    nex_timer_d3=0;
+  }else{
+    nex_timer_d3++;
+  }
+  myNex.writeNum("dec3.val",nex_timer_d3);
+  update_current_limit();
+}
+
+void trigger72()
+{
+  if (nex_timer_d0==0){
+    nex_timer_d0=9;
+  }else{
+    nex_timer_d0--;
+  }
+  myNex.writeNum("dec0.val",nex_timer_d0);
+  update_current_limit();
+}
+void trigger73()
+{
+  if (nex_timer_d1==0){
+    nex_timer_d1=9;
+  }else{
+    nex_timer_d1--;
+  }
+  myNex.writeNum("dec1.val",nex_timer_d1);
+  update_current_limit();
+}
+void trigger80()
+{
+  if (nex_timer_d2==0){
+    nex_timer_d2=9;
+  }else{
+    nex_timer_d2--;
+  }
+  myNex.writeNum("dec2.val",nex_timer_d2);
+  update_current_limit();
+}
+void trigger81()
+{
+  if (nex_timer_d3==0){
+    nex_timer_d3=9;
+  }else{
+    nex_timer_d3--;
+  }
+  myNex.writeNum("dec3.val",nex_timer_d3);
+  update_current_limit();
+}
+void trigger82()
+{
+  nex_timer_range=0;
+  myNex.writeNum("tma.bco",NEX_BLUE);
+  myNex.writeNum("tua.bco",NEX_LIGHT_GREY);
+  myNex.writeNum("tna.bco",NEX_LIGHT_GREY);
+  Beep(20, false);
+}
+void trigger83()
+{
+  nex_timer_range=1;
+  myNex.writeNum("tma.bco",NEX_LIGHT_GREY);
+  myNex.writeNum("tua.bco",NEX_BLUE);
+  myNex.writeNum("tna.bco",NEX_LIGHT_GREY);
+  Beep(20, false);
+}
+void trigger84()
+{
+  nex_timer_range=2;
+  myNex.writeNum("tma.bco",NEX_LIGHT_GREY);
+  myNex.writeNum("tua.bco",NEX_LIGHT_GREY);
+  myNex.writeNum("tna.bco",NEX_BLUE);
+  Beep(20, false);
+}
+void trigger85()
+{
+  myNex.writeNum("starttimer.bco",NEX_BLUE);
+  myNex.writeNum("stoptimer.bco",NEX_LIGHT_GREY);
+  nex_timer_current="";
+  myNex.writeStr("timercurrent.txt",nex_timer_current);
+  if (!nex_timer_run){
+    nex_timer_start=millis();
+    nex_timer_run=true;
+
+  }
+  Beep(20, false);
+}
+void trigger86()
+{
+  myNex.writeNum("stoptimer.bco",NEX_BLUE);
+  myNex.writeNum("starttimer.bco",NEX_LIGHT_GREY);
+  if (nex_timer_run){
+    nex_timer_stop=millis();
+    nex_timer_run=false;
+  }
+  nex_timer_current="";
+  myNex.writeStr("timercurrent.txt",nex_timer_current);
+  Beep(20, false);
+
+}
+void trigger87()
+{
+  myNex.writeNum("startstats.bco",NEX_BLUE);
+  myNex.writeNum("stopstats.bco",NEX_LIGHT_GREY);
+  nex_timer_current="";
+
+  if (!nex_timerstats_run){
+    nex_timerstats_run=true;
+
+  }
+  nex_stats_min = 0;
+  nex_stats_count=0;
+  nex_stats_max=0;
+  nex_stats_avg=0;
+  nex_stats_avg_s="0nA";
+  nex_stats_min_s="0nA";
+  nex_stats_max_s="0nA";
+  myNex.writeStr("avgmin.txt",nex_stats_min_s);
+  myNex.writeStr("avgavg.txt",nex_stats_avg_s);
+  myNex.writeStr("avgmax.txt",nex_stats_max_s);
+  Beep(20, false);
+}
+void trigger88()
+{
+  myNex.writeNum("stopstats.bco",NEX_BLUE);
+  myNex.writeNum("startstats.bco",NEX_LIGHT_GREY);
+  if (nex_timerstats_run){
+    nex_timerstats_run=false;
+  }
+  nex_timer_current="";
+  nex_stats_min = 0;
+  nex_stats_count=0;
+  nex_stats_max=0;
+  nex_stats_avg=0;
+
+  Beep(20, false);
+
 }