Browse Source

Move code and fixes

Per Mårtensson 11 months ago
parent
commit
d6583fde9c

+ 13 - 5
sw/DC_LOAD/include/config.h

@@ -1,6 +1,8 @@
 #ifndef DCLOAD_CONFIG_H
 #define DCLOAD_CONFIG_H
 
+#define DC_LOAD_VER             "36"
+
 #define I2C_EPROM_ADDRESS       0x57
 #define I2C_MCP4725_ADDRESS     0x61            //the DAC I2C address with MCP4725 pin A0 set high
 #define I2C_MCP79410_ADDRESS    0x6f            //0x6f is the default address for the MCP79410 Real Time Clock IC
@@ -11,7 +13,7 @@
 #define col_green               1024
 #define col_gray                50712
 #define col_blue                1055
-#define  line_len               21
+#define line_len                21
 #define MODE_CC                 1
 #define MODE_CP                 2
 #define MODE_CR                 3
@@ -36,12 +38,18 @@
 #define displayCurrentCal               0.000       //calibration correction for LCD current display (was 0.040)
 #define ResistorCutOff                  999         //maximum Resistor we want to deal with in software
 
-#define MCP4725_I2CADDR_DEFAULT (0x62) ///< Default i2c address
-#define MCP4725_CMD_WRITEDAC (0x40)    ///< Writes data to the DAC
-#define MCP4725_CMD_WRITEDACEEPROM    (0x60) ///< Writes data to the DAC and the EEPROM (persisting the assigned
+#define MCP4725_I2CADDR_DEFAULT (0x62)              //< Default i2c address
+#define MCP4725_CMD_WRITEDAC (0x40)                 //< Writes data to the DAC
+#define MCP4725_CMD_WRITEDACEEPROM    (0x60)        //< Writes data to the DAC and the EEPROM (persisting the assigned
 
 #define pic_usb_grey            2
 #define pic_usb_black           3
 
-#define button_trig             30
+#define LiPoCutOffVoltage        3.0                //set cutoff voltage for LiPo battery
+#define LiFeCutOffVoltage        2.8                //set cutoff voltage for LiFe battery
+#define NiCdCutOffVoltage        1.0                //set cutoff voltage for NiCd battery
+#define ZiZnCutOffVoltage        1.0                //set cutoff voltage for ZiZn battery
+#define PbAcCutOffVoltage        1.75               //set cutoff voltage for PbAc battery
+
+#define DEBOUNCE_DELAY           50
 #endif // DCLOAD_CONFIG_H

+ 29 - 0
sw/DC_LOAD/include/input.h

@@ -0,0 +1,29 @@
+#ifndef DCLOAD_INPUT_H
+#define DCLOAD_INPUT_H
+
+#define DIGIT_0              11
+#define DIGIT_1              2
+#define DIGIT_2              3
+#define DIGIT_3              4
+#define DIGIT_4              5
+#define DIGIT_5              6
+#define DIGIT_6              7
+#define DIGIT_7              8
+#define DIGIT_8              9
+#define DIGIT_9              10
+#define DIGIT_ENTER          20
+#define DIGIT_ONOFF          21
+#define DIGIT_SETUP          19
+#define DIGIT_TRANS          14
+#define DIGIT_BATTERY        15
+#define DIGIT_RESISTANS      18
+#define DIGIT_POWER          17
+#define DIGIT_CURRENT        16
+#define DIGIT_DECIMAL        13
+#define DIGIT_DELETE         12
+#define BUTTON_SERIAL        29
+#define BUTTON_TRIGGER       30
+#define NO_KEY               0
+
+void serialEvent(void);
+#endif //DCLOAD_INPUT_H

+ 2 - 0
sw/DC_LOAD/include/screen.h

@@ -10,4 +10,6 @@ void writeStr(char *  command, char * txt);
 void writeNum2(char * command, uint16_t value);
 void print_temp(void);
 void color_load (bool load);
+void print_reading(void);
+void print_input(void);
 #endif //DCLOAD_SCREEN_H

+ 50 - 0
sw/DC_LOAD/src/input.cpp

@@ -0,0 +1,50 @@
+#include <Arduino.h>
+#include <config.h>
+#include <input.h>
+#include <SoftwareSerial.h>
+
+extern uint8_t customKey;
+extern SoftwareSerial nexSerial;
+extern bool trig_key;
+extern void toggle_usb(void);
+uint16_t i1;
+uint8_t c1;
+static uint8_t __buffer[10];
+
+void serialEvent() {
+    while (nexSerial.available() > 0 ){   
+        
+        c1 = nexSerial.read();
+        if (0x65 == c1 ||__buffer[0] == 0x65)
+        {
+            if (0x65 == c1){
+              __buffer[0] = c1; 
+              i1++;
+            }else{
+              __buffer[i1]=c1; 
+              i1++; 
+            if (i1>=7)
+            {
+                __buffer[i1] = 0x00;
+                if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6])
+                {
+                    if (__buffer[1]==0){
+                      customKey=__buffer[2];
+                      switch(customKey){
+                        case BUTTON_SERIAL:
+                          toggle_usb();
+                          break;
+                        case BUTTON_TRIGGER:
+                          trig_key = true;
+                          break;
+                      }
+
+                    }
+                }
+              memset(__buffer,0x00,8);
+              i1=0;  
+            }
+        }
+    }
+    }
+}

+ 221 - 259
sw/DC_LOAD/src/main.cpp

@@ -12,14 +12,14 @@
 #include <MCP79410_Timer.h>                   //Scullcom Hobby Electronics library  http://www.scullcom.com/MCP79410Timer-master.zip
 #include <EEPROM.h>                           //include EEPROM library used for storing setup data
 #include <config.h>
+#include <input.h>
 #include <screen.h>
 #include <SoftwareSerial.h>
 
 SoftwareSerial nexSerial (rxPin,txPin);
 
-static uint8_t __buffer[10];
-uint16_t i1;
-uint8_t c1;
+
+
 void setupLimits (void);
 void transientSwitch(float current_setting, boolean toggle_status);
 void transientListSetup();
@@ -55,9 +55,9 @@ void wait_for_key(bool wait =true );
 
 
 
-char customKey;
+uint8_t customKey;
 
-char decimalPoint;                            //used to test for more than one press of * key (decimal point)
+bool decimalPoint=false;                            //used to test for more than one press of * key (decimal point)
 
                     
 
@@ -128,11 +128,6 @@ volatile float encoderPosition = 0;           //
 volatile unsigned long factor= 0;             //number of steps to jump
 volatile unsigned long encoderMax = 999000;   //sets maximum Rotary Encoder value allowed CAN BE CHANGED AS REQUIRED (was 50000)
 
-float LiPoCutOffVoltage = 3.0;                //set cutoff voltage for LiPo battery
-float LiFeCutOffVoltage = 2.8;                //set cutoff voltage for LiFe battery
-float NiCdCutOffVoltage = 1.0;                //set cutoff voltage for NiCd battery
-float ZiZnCutOffVoltage = 1.0;                //set cutoff voltage for ZiZn battery
-float PbAcCutOffVoltage = 1.75;               //set cutoff voltage for PbAc battery
 String BatteryType ="    ";
 
 byte exitMode = 0;                           //used to exit battery selection menu and return to CC Mode
@@ -163,14 +158,28 @@ char line0_old[line_len]= {0};
 char line1_old[line_len]= {0};
 char line2_old[line_len]= {0};
 char line3_old[line_len]= {0};
+char line_current[7]={0};
+char line_volt[7]={0};
+char line_power[7]={0};
+char line_current_old[7]={0};
+char line_volt_old[7]={0};
+char line_power_old[7]={0};
 char temperature_val[5]={0x00};
 char temperature_buf[5]={0x00};
+char line_input[10]={0};
 bool old_load=true;
 
 bool usb_enable=false;
 bool trig_key = false;
 void setmode(void);
 void color_load (bool load);
+
+// Variables will change:
+int OnOfflastSteadyState = LOW;       // the previous steady state from the input pin
+int OnOfflastFlickerableState = LOW;  // the previous flickerable state from the input pin
+int OnOffcurrentState;                // the current reading from the input pin
+unsigned long OnOfflastDebounceTime = 0;  // the last time the output pin was toggled
+
 bool setVoltage(uint16_t output, bool writeEEPROM) {
 
   uint8_t packet[3];
@@ -218,103 +227,7 @@ void toggle_usb(void){
   usb_enable=!usb_enable;
 }
 
-void serialEvent() {
-    while (nexSerial.available() > 0 ){   
-        
-        c1 = nexSerial.read();
-        Serial.println(c1,HEX);
-        if (0x65 == c1 ||__buffer[0] == 0x65)
-        {
-            if (0x65 == c1){
-              __buffer[0] = c1; 
-              i1++;
-            }else{
-              __buffer[i1]=c1; 
-              i1++; 
-            if (i1>=7)
-            {
-                __buffer[i1] = 0x00;
-                if (0xFF == __buffer[4] && 0xFF == __buffer[5] && 0xFF == __buffer[6])
-                {
-                    if (__buffer[1]==0){
-                      switch(__buffer[2]){
-                        case 11:
-                          customKey='0';
-                          break;
-                        case 2:
-                          customKey='1';
-                          break;
-                        case 3:
-                          customKey='2';
-                          break;
-                        case 4:
-                          customKey='3';
-                          break;
-                        case 5:
-                          customKey='4';
-                          break;
-                        case 6:
-                          customKey='5';
-                          break;
-                        case 7:
-                          customKey='6';
-                          break; 
-                        case 8:
-                          customKey='7';
-                          break;
-                        case 9:
-                          customKey='8';
-                          break;
-                        case 10:
-                          customKey='9';
-                          break;
-                        case 20:
-                          customKey='F';
-                          break;
-                        case 13:
-                          customKey='>';
-                          break;
-                        case 12:
-                          customKey='<';
-                          break;
-                        case 21:
-                          customKey='E';
-                          break;
-                        case 19:
-                          customKey='D';
-                          break;
-                        case 16:
-                          customKey='A';
-                          break;
-                        case 17:
-                          customKey='B';
-                          break;
-                        case 18:
-                          customKey='#';
-                          break;
-                        case 15:
-                          customKey='*';
-                          break;
-                        case 14:
-                          customKey='C';
-                          break;
-                        case 29:
-                          toggle_usb();
-                          break;
-                        case button_trig:
-                          trig_key = true;
-                          break;
-                      }
-
-                    }
-                }
-              memset(__buffer,0x00,8);
-              i1=0;  
-            }
-        }
-    }
-    }
-}
+
 
 //---------------------------------Initial Set up---------------------------------------
 void setup() {
@@ -336,7 +249,7 @@ void setup() {
     } 
       nexSerial.read();                // Read and delete bytes
   }
-
+  memset(line_input,' ',10);
   CurrentCutOff = EEPROM.read(0x00);
   PowerCutOff = EEPROM.read(0x20);
   tempCutOff = EEPROM.read(0x40);
@@ -388,8 +301,12 @@ nDevices = 0;
   analogReference(INTERNAL);                               //use Arduino internal reference for tempurature monitoring
   attachInterrupt(digitalPinToInterrupt(pinA), isr, LOW);
  
-  screen_clear('\0');
+  screen_clear(0x00);
   writeNum2("t0.xcen", 1);                                 //print SCULLCOM to display with 5 leading spaces (you can change to your own)
+  writeNum2("t0.xcen",(int) 1); 
+  writeNum2("t1.xcen",(int) 1); 
+  writeNum2("t2.xcen",(int) 1); 
+  writeNum2("t3.xcen",(int) 1); 
   memcpy(line0,(char*)"SCULLCOM",9);
   print_chars2(0,9);
   
@@ -399,10 +316,20 @@ nDevices = 0;
   memcpy(line2,(char*)"DC Electronic Load",19);
   print_chars2(2,19);
 
-  memcpy(line3,(char*)"Ver. 35 (5x4 Keypad)",21);
+  memcpy(line3,(char*)"Ver. ",5);
+  memcpy(line3+5,(char*)DC_LOAD_VER,2);
+  memcpy(line3+7,(char*)" (Nextion)",11);
   print_chars2(3,21);
+  memcpy(line_current," ",1);
+  memcpy(line_volt," ",1);
+  memcpy(line_power," ",1);
+  print_reading();
 
-  delay(2000);                                             //3000 mSec delay for intro display
+  delay(2000);   
+  writeNum2("t0.xcen",(int) 1); 
+  writeNum2("t1.xcen",(int) 0); 
+  writeNum2("t2.xcen",(int) 0); 
+  writeNum2("t3.xcen",(int) 0);                                           //3000 mSec delay for intro display
   screen_clear(' ');  
   setupLimits();
   delay(3000);
@@ -415,7 +342,7 @@ nDevices = 0;
   color_load(0); 
   Current();                                               //sets initial mode to be CC (Constant Current) at Power Up
   //EEPROM.write(0x00, 0xff);
-  customKey='\0';
+  customKey=NO_KEY;
 
 }
 
@@ -427,14 +354,21 @@ void loop() {
   if(CurrentCutOff > 5){                                 //Test and go to user set limits if required
     userSetUp();
     writeNum2("t0.xcen", 0);
-    customKey='\0';
+    customKey=NO_KEY;
   }else{
   
     readKeypadInput();                                     //read Keypad entry
-    
-    if (digitalRead(LoadOnOff) == LOW) {
-      LoadSwitch();                                          //Load on/off
+    OnOffcurrentState = digitalRead(LoadOnOff);
+    if (OnOffcurrentState != OnOfflastFlickerableState) {
+      OnOfflastDebounceTime = millis();
+      OnOfflastFlickerableState = OnOffcurrentState;
     }
+    if ((millis() - OnOfflastDebounceTime) > DEBOUNCE_DELAY && OnOffcurrentState == LOW) {
+      LoadSwitch();                                     //Load On Off
+      OnOfflastSteadyState = OnOffcurrentState;
+
+    }
+
     
     transient();                                           //test for Transient Mode
     setmode() ;
@@ -475,37 +409,37 @@ void readKeypadInput (void) {
 // Serial.println(customKey);                             //only used for testing keypad (uncomment if required for testing keypad)
 //  }                                                     //only used for testing keypad (uncomment if required for testing keypad)
 
-  if(customKey == 'E'){                                     //check for Load on/off
+  if(customKey == DIGIT_ONOFF){                                     //check for Load on/off
     LoadSwitch();
-    customKey='\0';
+    customKey=NO_KEY;
   }
 
-  if(customKey == 'D'){                                   //check if Set-Up Mode Selected 
+  if(customKey == DIGIT_SETUP){                                   //check if Set-Up Mode Selected 
     delay(200);
-    toggle = false;  
-    customKey='\0';                                       //switch Load OFF
+    toggle = true;  
+    customKey=NO_KEY;                                       //switch Load OFF
     LoadSwitch();
     userSetUp();
     encoderPosition = 0;                                    //reset encoder reading to zero
     index = 0;
     z = 1;                                                  //sets column position for LCD displayed character
-    decimalPoint = (' ');                                   //clear decimal point text character reset
+    decimalPoint = false;                                //clear decimal point text character reset
 
 }
  
-  if(customKey == 'C'){                                   //check if Transient Mode Selected
+  if(customKey == DIGIT_TRANS){                                   //check if Transient Mode Selected
     toggle = false;    
-    customKey='\0';                                     //switch Load OFF
+    customKey=NO_KEY;                                     //switch Load OFF
     transientType();
   
     encoderPosition = 0;                                    //reset encoder reading to zero
     index = 0;
     z = 1;                                                  //sets column position for LCD displayed character
-    decimalPoint = (' ');                                   //clear decimal point text character reset
+    decimalPoint = false;                                //clear decimal point text character reset
 
   }
                
-  if(customKey == 'A'){                                   //check if Constant Current button pressed
+  if(customKey == DIGIT_CURRENT){                                   //check if Constant Current button pressed
     toggle = false;                                         //switch Load OFF
     color_load(0);
     print_chars2(0,20);
@@ -513,11 +447,11 @@ void readKeypadInput (void) {
     encoderPosition = 0;                                    //reset encoder reading to zero
     index = 0;
     z = 1;                                                  //sets column position for LCD displayed character
-    decimalPoint = (' ');                                   //clear decimal point test character reset
-    customKey='\0';
+    decimalPoint = false;                                //clear decimal point test character reset
+    customKey=NO_KEY;
   }
          
-  if(customKey == 'B'){                                   //check if Constant Power button pressed
+  if(customKey == DIGIT_POWER){                                   //check if Constant Power button pressed
     toggle = false;                                         //switch Load OFF
     color_load(0);
     print_chars2(0,20);
@@ -525,11 +459,11 @@ void readKeypadInput (void) {
     encoderPosition = 0;                                    //reset encoder reading to zero
     index = 0;
     z = 1;                                                  //sets column position for LCD displayed character
-    decimalPoint = (' ');                                   //clear decimal point test character reset
-    customKey='\0';
+    decimalPoint = false;                                //clear decimal point test character reset
+    customKey=NO_KEY;
   }
           
-  if(customKey == '#'){                                   //check if Constant Resistance button pressed  
+  if(customKey == DIGIT_RESISTANS){                                   //check if Constant Resistance button pressed  
     toggle = false;                                         //switch Load OFF
     color_load(0);
     print_chars2(0,20);
@@ -537,23 +471,23 @@ void readKeypadInput (void) {
     encoderPosition = 0;                                    //reset encoder reading to zero
     index = 0;
     z = 1;                                                  //sets column position for LCD displayed character
-    decimalPoint = (' ');                                   //clear decimal point test character reset
-    customKey='\0';
+    decimalPoint = false;                                //clear decimal point test character reset
+    customKey=NO_KEY;
   }
 
-  if(customKey == '*'){                                   //check if Battery Capacity button pressed
+  if(customKey == DIGIT_BATTERY){                                   //check if Battery Capacity button pressed
     setVoltage(0,false);                                //Ensures Load is OFF - sets DAC output voltage to 0
     toggle = false;                                         //switch Load OFF
     batteryType();                                          //select battery type
     index = 0;
     z = 1;                                                  //sets column position for LCD displayed character
-    decimalPoint = (' ');                                   //clear decimal point test character reset
+    decimalPoint = false;                                //clear decimal point test character reset
     if (exitMode == 1){                                   //if NO battery type selected revert to CC Mode
       color_load(0);
       print_chars2(0,20);
       Current();                                            //if selected go to Constant Current Selected routine
       encoderPosition = 0;                                  //reset encoder reading to zero
-      customKey = 'A';
+      customKey = DIGIT_CURRENT;
     }
     else
     {
@@ -569,18 +503,18 @@ void readKeypadInput (void) {
   }
 
   if (Mode != MODE_TC && Mode != MODE_TL && Mode != MODE_TP && Mode != MODE_TT){
-
-    if(customKey >= '0' && customKey <= '9'){               //check for keypad number input
-        numbers[index++] = customKey;
-        numbers[index] = '\0';
-        line3[z]=customKey;
-        print_chars2(3,20);
-        z = z+1;
-        customKey ='\0';
-      }
     
-    if(customKey == '>'){                                   //check if decimal button key pressed
-        if (decimalPoint != '>'){                         //test if decimal point entered twice - if so skip 
+    if(customKey >= DIGIT_1 && customKey <= DIGIT_0){               //check for keypad number input
+      numbers[index++] = (customKey-1 == 10 ?  '0' : '0' +customKey-1 );  //Convert into into to ascii and fix 0
+      numbers[index] = 0x00;
+      line_input[z]=(customKey-1 == 10 ?  '0' : '0' +customKey-1 );
+      print_input();
+      z = z+1;
+      customKey=NO_KEY;
+    }
+    
+    if(customKey == DIGIT_DECIMAL){                                   //check if decimal button key pressed
+        if (!decimalPoint){                         //test if decimal point entered twice - if so skip 
           uint8_t index_old = index;
           if (index==0){
             numbers[index++] = '0';
@@ -588,35 +522,37 @@ void readKeypadInput (void) {
           numbers[index++] = '.';
           numbers[index] = 0x00;
           if (index_old==0){
-            line3[z++]='0';
+            line_input[z++]='0';
           }
-          line3[z]='.';
-          print_chars2(3,20);
+          line_input[z]='.';
+          print_input();
           z = z+1;
-          decimalPoint = '>';                             //used to indicate decimal point has been input
+          decimalPoint = true;                             //used to indicate decimal point has been input
           }
-        customKey ='\0';
+        customKey=NO_KEY;
         }
 
-    if(customKey == '<'){                                 //clear input entry
+    if(customKey == DIGIT_DELETE){                                 //clear input entry
       index = 0;
-      z = 1;                                             //sets column position for LCD displayed character
-      print_chars(3,z,(char *) "     ",5);
-      numbers[index] = '\0';                             //
-      decimalPoint = ' ';                              //clear decimal point test character reset
-      customKey ='\0';
+      z = 1;   
+      memcpy(line_input+z,(char *) "     ",5);                                          //sets column position for LCD displayed character
+      print_input();
+      numbers[index] = 0x00;                             //
+      decimalPoint = false;                              //clear decimal point test character reset
+      customKey=NO_KEY;
       }
 
-    if(customKey == 'F') {                                //use entered number
+    if(customKey == DIGIT_ENTER) {                                //use entered number
       x = atof(numbers);     
       reading = x;
       encoderPosition = reading*1000;
       index = 0;
-      numbers[index] = '\0';
+      numbers[index] = 0x00;
       z = 1;                                         //sets column position for LCD displayed character
-      print_chars(3,z,(char *) "     ",5);
-      decimalPoint = (' ');                          //clear decimal point test character reset
-      customKey ='\0';
+      memcpy(line_input+z,(char *) "     ",5); 
+      print_input();
+      decimalPoint = false;                       //clear decimal point test character reset
+      customKey=NO_KEY;
     }
   }
   
@@ -687,10 +623,10 @@ void displayEncoderReading (void) {
     if ( Mode == MODE_CP || Mode == MODE_CR ) {
         //lcd.print (reading, 2);                              //show input reading from Rotary Encoder on LCD
         dtostrf(reading, 5, 2, buff);
-        print_chars(2,cursor_pos++,(char*)buff,5);
+        print_chars(1,cursor_pos++,(char*)buff,5);
     } else {
         dtostrf(reading, 5, 3, buff);
-        print_chars(2,cursor_pos++,(char*)buff,5);
+        print_chars(1,cursor_pos++,(char*)buff,5);
     }
     //lcd.setCursor (CP, 2);                                   //sets cursor position
     //lcd.cursor();                                            //show cursor on LCD
@@ -765,8 +701,8 @@ void ActualReading(void) {
     } else {
         dtostrf(ActualCurrent, 4, 2, buff);
     }
-    memcpy(line1,buff ,5);
-    memcpy(line1+5,(char *)"A " ,2);
+    memcpy(line_current,buff ,5);
+    memcpy(line_current+5,(char *)"A" ,1);
     
     if (ActualVoltage < 10.0) {
         //lcd.print(ActualVoltage, 3);
@@ -774,16 +710,18 @@ void ActualReading(void) {
     } else {
         dtostrf(ActualVoltage, 4, 2, buff);
     }    
-    memcpy(line1+7,buff ,6);
-    memcpy(line1+12,(char *)"V " ,2);
-     
+    memcpy(line_volt,buff ,5);
+    memcpy(line_volt+5,(char *)"V" ,1);
     if (ActualPower < 100 ) {
          dtostrf(ActualPower, 4, 2, buff);
+
     } else {
          dtostrf(ActualPower, 4, 1, buff);
     }
-    memcpy(line1+14,buff ,6);
-    print_chars(1,18,(char *)"W " ,2);
+    uint8_t len = strlen(buff);
+    memcpy(line_power,buff, 5);
+    memcpy(line_power+len,(char *)"W" ,1);
+    print_reading();
 }
 
 //-----------------------DAC Control Voltage for Mosfet---------------------------------------
@@ -910,7 +848,6 @@ void LoadSwitch(void) {
     {
       color_load(0);
 
-      color_load(0);
       current_instruction = 0;                            //reset current instruction for Transient List Mode to zero
       last_time = 0;                                      //reset last time to zero
       transientPeriod = 0;                                //reset transient period time to zero
@@ -933,7 +870,9 @@ void Current(void) {
   memcpy(line0,(char*)"DC LOAD     ",12);
   print_chars2(0,20);
   writeNum2("t0.xcen",(uint16_t) 0);
-  print_chars(2,0,(char*)"Set I =            A",20);
+  print_chars(1,0,(char*)"Set I =            A",20);
+  memset(line2,' ',20);
+  print_chars2(2,20);
   memset(line3,' ',20);
   print_chars2(3,20);
   CP = 9;                                            //sets cursor starting position to units.
@@ -946,7 +885,9 @@ void Power(void) {
   writeNum2("t0.xcen",(int) 0);
   memcpy(line0,(char*)"DC LOAD     ",12);
   print_chars2(0,20);
-  print_chars(2,0,(char*)"Set W =            W",22);
+  print_chars(1,0,(char*)"Set W =            W",20);
+  memset(line2,' ',20);
+  print_chars2(2,20);
   memset(line3,' ',20);
   print_chars2(3,20);
   CP = 10;                                             //sets cursor starting position to units.
@@ -959,7 +900,9 @@ void Resistance(void) {
   writeNum2("t0.xcen",(int) 0);
   memcpy(line0,(char*)"DC LOAD     ",12);
   print_chars2(0,20);
-  print_chars(2,0,(char*)"Set R =            R",20);
+  print_chars(1,0,(char*)"Set R =            R",20);
+  memset(line2,' ',20);
+  print_chars2(2,20);
   memset(line3,' ',20);
   print_chars2(3,20);
   CP = 10;                                             //sets cursor starting position to units.
@@ -975,7 +918,9 @@ void BatteryCapacity(void) {
   memcpy(line0,"BATTERY             ",20);
   memcpy(line0+8,(char *)BatteryType.c_str(),4);
   print_chars2(0,20);
-  print_chars(2,0,(char*)"Set I =            A",20);
+  print_chars(1,0,(char*)"Set I =            A",20);
+  memset(line2,' ',20);
+  print_chars2(2,20);
   memset(line3,' ',20);
   print_chars2(3,20);
   CP = 9;                                             //sets cursor starting position to units.
@@ -991,47 +936,49 @@ void batteryType (void) {
   print_chars(1,0,(char*)"1=LiPo/Li-Ion 2=LiFe",20);
   print_chars(2,0,(char*)"3=NiCd/NiMH   4=ZiZn",20);
   print_chars(3,0,(char*)"5=Set Voltage 6=Exit",20);                 
-  customKey='\0';
-  wait_for_key(true);               //stop everything till the user press a key.
-
-  if (customKey == '1'){
-    BatteryCutoffVolts = LiPoCutOffVoltage;
-    BatteryType = ("LiPo");
-  }
-
-  if (customKey == '2'){
-    BatteryCutoffVolts = LiFeCutOffVoltage;
-    BatteryType = ("LiFe");
-  }
+  customKey=NO_KEY;
+  BatteryType =("");
+  while(customKey==0x00){
+    wait_for_key(true);               //stop everything till the user press a key.
+
+    if (customKey == DIGIT_1){
+      BatteryCutoffVolts = LiPoCutOffVoltage;
+      BatteryType = ("LiPo");
+    }
+    else if (customKey == DIGIT_2){
+      BatteryCutoffVolts = LiFeCutOffVoltage;
+      BatteryType = ("LiFe");
+    }
 
-  if (customKey == '3'){
-    BatteryCutoffVolts = NiCdCutOffVoltage;
-    BatteryType = ("NiCd");  
-  }
+     else if (customKey == DIGIT_3){
+      BatteryCutoffVolts = NiCdCutOffVoltage;
+      BatteryType = ("NiCd");  
+    }
 
-  if (customKey == '4'){
-    BatteryCutoffVolts = ZiZnCutOffVoltage;
-    BatteryType = ("ZiZn"); 
-  }
+    else if (customKey == DIGIT_4){
+      BatteryCutoffVolts = ZiZnCutOffVoltage;
+      BatteryType = ("ZiZn"); 
+    }
 
-  if (customKey == '5'){ 
-    BatteryType = ("SetV");
-  }
+    else if (customKey == DIGIT_5){ 
+      BatteryType = ("SetV");
+    }
 
-  if (customKey == '6'){                                  //Exit selection screen
-    exitMode = 1;
+    else if (customKey == DIGIT_6){                                  //Exit selection screen
+      exitMode = 1;
+    }
+    else{
+      customKey=NO_KEY;
+    }
   }
 
-  if (customKey == '7' || customKey == '8' || customKey == '9' || customKey == '0' || customKey == 'A' || customKey == 'B' || customKey == 'C' || customKey == 'D' || customKey == '*' || customKey == '#' || customKey == 'E' || customKey == 'F' || customKey == '<' || customKey == '>' ){
-    batteryType();                                                      //ignore other keys
-  }
 
   if(BatteryType == "SetV" && exitMode != 1){
     setBatteryCutOff();
   }
 
   batteryTypeSelected();                                    //briefly display battery type selected and discharge cut off voltage
-  customKey=0x00;
+  customKey=NO_KEY;
   screen_clear(' ');
 
 }
@@ -1101,18 +1048,18 @@ void wait_for_key(bool wait){
 //------------------------Key input used for Battery Cut-Off and Transient Mode------------------------
 void inputValue (int r=0){
  
- while(customKey != 'F'){               //check if enter pressed (was previously #)
-  customKey=0x00; 
+ while(customKey != DIGIT_ENTER){               //check if enter pressed (was previously #)
+  customKey=NO_KEY; 
   wait_for_key(true);
-  if(customKey >= '0' && customKey <= '9'){               //check for keypad number input
-       numbers[index++] = customKey;
-       numbers[index] = '\0';
-       print_char(r,z,customKey);
+  if(customKey >= DIGIT_1 && customKey <= DIGIT_0){               //check for keypad number input
+       numbers[index++] = (customKey-1 == 10 ?  '0' : '0' +customKey-1 );
+       numbers[index] = 0x00;
+       print_char(r,z,(customKey-1 == 10 ?  '0' : '0' +customKey-1 ));
        z++;
      }
   
-  if(customKey == '>'){                                   //Decimal point
-    if (decimalPoint != '>'){                         //test if decimal point entered twice - if so skip
+  if(customKey == DIGIT_DECIMAL){                                   //Decimal point
+    if (!decimalPoint ){                         //test if decimal point entered twice - if so skip
       if (index==0){
         numbers[index++] = '0';  
         print_char(r,z,'0');
@@ -1122,29 +1069,29 @@ void inputValue (int r=0){
       numbers[index] = 0x00;
       print_char(r,z,'.');
       z++;
-      decimalPoint = '>';                               //used to indicate decimal point has been input
+      decimalPoint = true;                               //used to indicate decimal point has been input
     }
   }
 
-  if(customKey == '<'){                                    //clear entry
+  if(customKey == DIGIT_DELETE){                                    //clear entry
     index = 0;
     z = y;
 
     numbers[index] = 0x00; 
                                    //
     print_chars(r,z,(char *)"     ",5);
-    decimalPoint = (' ');                                 //clear decimal point test character reset
+    decimalPoint = false;                              //clear decimal point test character reset
   }
 
  }
  
-  if(customKey == 'F') {                                  //enter value 
+  if(customKey == DIGIT_ENTER) {                                  //enter value 
     x = atof(numbers);     
     index = 0;
     numbers[index] = 0x00;
-    decimalPoint = (' ');                                 //clear decimal point test character reset
+    decimalPoint = false;                              //clear decimal point test character reset
   }
-  customKey=0x00;
+  customKey=NO_KEY;
 }
 
 //----------------------------------------Transient Mode--------------------------------------------
@@ -1171,7 +1118,7 @@ void transientMode (void) {
     dtostrf(LowCurrent, 5, 3, buff);
     print_chars(r,11,buff,5); 
 
-    customKey = 0x00;
+    customKey=NO_KEY;
 
     z = 11;
     memcpy(line2,(char *)"Set High I=",11);
@@ -1187,7 +1134,7 @@ void transientMode (void) {
     dtostrf(HighCurrent, 5, 3, buff);
     print_chars(r,11,buff,5); 
 
-    customKey = 0x00;
+    customKey=NO_KEY;
 
     if(Mode == MODE_TC || Mode == MODE_TP){
       z = 11;
@@ -1236,51 +1183,41 @@ void transientType (void) {
   memcpy(line3+11,(char*)"5 = Exit",9);
   print_chars2(3,0);
   while ( customKey == 0x00 || 
-          customKey == '6' || 
-          customKey == '7' || 
-          customKey == '8' || 
-          customKey == '9' || 
-          customKey == '0' || 
-          customKey == 'A' || 
-          customKey == 'B' || 
-          customKey == 'C' || 
-          customKey == 'D' || 
-          customKey == '*' || 
-          customKey == '#' || 
-          customKey == 'E' || 
-          customKey == 'F' || 
-          customKey == '<' || 
-          customKey == '>' ){
-    customKey = 0x00;     
+          customKey == DIGIT_6 || 
+          customKey == DIGIT_7 || 
+          customKey == DIGIT_8 || 
+          customKey == DIGIT_9 || 
+          customKey >= DIGIT_0){
+    customKey=NO_KEY;     
     wait_for_key(1);                 //stop everything till the user press a key.
   }
-  if (customKey == '1'){
+  if (customKey == DIGIT_1){
     Mode = MODE_TC; 
   }
 
-  if (customKey == '2'){
+  if (customKey == DIGIT_2){
     Mode = MODE_TT;
   }
 
-  if (customKey == '3'){
+  if (customKey == DIGIT_3){
     Mode = MODE_TP;  
   }
 
-  if (customKey == '4'){
+  if (customKey == DIGIT_4){
     Mode = MODE_TL;  
   }
 
-  if (customKey == '5'){                                  //Exit selection screen
+  if (customKey == DIGIT_5){                                  //Exit selection screen
     exitMode = 1;
   }
 
   screen_clear(' ');
-  customKey=0x00;
+  customKey=NO_KEY;
   if (exitMode == 1){                                       //if NO Transient Mode type selected revert to CC Mode
     color_load(0);
     Current();                                            //if selected go to Constant Current Selected routine
     encoderPosition = 0;                                  //reset encoder reading to zero
-    customKey = 'A';
+    customKey = DIGIT_CURRENT;
       }else{
     transientMode();
       }
@@ -1335,7 +1272,7 @@ void transientListSetup(){
   z = 0;
   inputValue(3);
   total_instructions = int(x-1);
-  customKey = 0x00;
+  customKey=NO_KEY;
   //lcd.clear();
   char buff[5] = {0x00};
     for(int i=0; i<=(total_instructions); i++){
@@ -1352,7 +1289,7 @@ void transientListSetup(){
       z = 0;
       inputValue(1);            //get the users input value
       transientList[i][0] = x; //store the users entered value in the transient list 
-      customKey = 0x00;
+      customKey=NO_KEY;
       memcpy(line2,"Set Time        ",16);
       length = snprintf( NULL, 0, "%d", i+1 );
       itoa(i+1 ,buff,10);
@@ -1365,7 +1302,7 @@ void transientListSetup(){
       z = 0;
       inputValue(3);            //get the users input value
       transientList[i][1] = x; //store the users entered value in the transient list
-      customKey = 0x00;
+      customKey=NO_KEY;
       screen_clear(' ');  
   }
   current_instruction = 0;      //start at first instrution
@@ -1373,62 +1310,87 @@ void transientListSetup(){
 
 //-------------------------------------Transcient Load Toggel-------------------------------------------
 void transientLoadToggle(){
-
+  char buff[5] = {0x00};
   if(Mode == MODE_TC){
   current_time = micros();                              //get the current time in micro seconds()
   if (last_time == 0){
     last_time = current_time;
   } else {
+    memcpy(line1,"Set I =            A",20);
     switch (transient_mode_status){
       case (false):
           // we are in the low current setting
           if ((current_time - last_time) >= (transientPeriod * 1000.0)){
-             transientSwitch(LowCurrent, true);  
+            transientSwitch(LowCurrent, true); 
+            dtostrf(LowCurrent, 4, 3, buff);
+            memcpy(line1+8,buff ,5);
+            print_chars2(1,20); 
           }
         break;
       case (true):
           // we are in the high current setting 
           if ((current_time - last_time) >= (transientPeriod * 1000.0)){
-            transientSwitch(HighCurrent, true);            
+            transientSwitch(HighCurrent, true);      
+            dtostrf(HighCurrent, 4, 3, buff);
+            memcpy(line1+8,buff ,5);  
+            print_chars2(1,20);    
           }
         break; 
       } 
-     }
-    }
+
+  }
+}
 
 
   if(Mode == MODE_TP){
     current_time = micros();                            //get the current time in micro seconds()
+    memcpy(line1,"Set I =            A",20);
     if (last_time == 0){
         last_time = current_time;
         transientSwitch(LowCurrent, true);
+        dtostrf(LowCurrent, 4, 3, buff);
+        memcpy(line1+8,buff ,5);    
     }
     if (digitalRead(TriggerPulse) == LOW || trig_key){
       // a trigger pluse is received
       // set to the high current
       trig_key=false;
       transientSwitch(HighCurrent, true); 
+      dtostrf(HighCurrent, 4, 3, buff);
+      memcpy(line1+8,buff ,5);  
+      print_chars2(1,20);    
     } else {
         if ((current_time - last_time) >= (transientPeriod * 1000.0)){
-            transientSwitch(LowCurrent, true);            
+            transientSwitch(LowCurrent, true);    
+            dtostrf(LowCurrent, 4, 3, buff);
+            memcpy(line1+8,buff ,5);      
+            print_chars2(1,20);  
         }
-      }  
+    } 
+     
   }
 
 
  if(Mode == MODE_TT){          // this function will toggle between high and low current when the trigger pin is taken low
+
   if (digitalRead(TriggerPulse) == LOW  || trig_key){
+    memcpy(line1,"Set I =            A",20);
     trig_key=false;
     switch (transient_mode_status){
       case (false):
         transientSwitch(LowCurrent, true);
+        dtostrf(LowCurrent, 4, 3, buff);
+        memcpy(line1+8,buff ,5);   
         delay(200);         //added to prevent key bounce when transient button used (date of addition 31-03-2018)
         break;
       case (true):
         transientSwitch(HighCurrent, true);
+        dtostrf(HighCurrent, 4, 3, buff);
+        memcpy(line1+8,buff ,5);  
         delay(200);         //added to prevent key bounce when transient button used (date of addition 31-03-2018)   
         break;
       }
+      print_chars2(1,20); 
     }
   }
 
@@ -1488,7 +1450,7 @@ void userSetUp (void) {
   CurrentCutOff = x;
   EEPROM.write(0x00, CurrentCutOff);
   
-  customKey = 0x00;
+  customKey=NO_KEY;
   z = 15;
   memcpy(line2,"Power Limit =      W",21);
   print_chars2(2,20);
@@ -1496,7 +1458,7 @@ void userSetUp (void) {
   PowerCutOff = x;
   EEPROM.write(0x20, PowerCutOff);        
 
-  customKey = 0x00;
+  customKey=NO_KEY;
   z = 15;
   line3[18]=0xB0;
   memcpy(line3,"Temp. Limit =      C",21);
@@ -1512,7 +1474,7 @@ void userSetUp (void) {
     color_load(0);
     Current();                                            //if selected go to Constant Current Selected routine
     encoderPosition = 0;                                  //reset encoder reading to zero
-    customKey = 'A';
+    customKey = DIGIT_CURRENT;
 }
 
 //------------------------------------------High Temperature Cut-Off--------------------------------------------------------------

+ 41 - 0
sw/DC_LOAD/src/screen.cpp

@@ -15,6 +15,14 @@ extern char line3_old[line_len];
 extern bool old_load;
 extern char temperature_val[5];
 extern char temperature_buf[5];
+extern char line_current[7];
+extern char line_volt[7];
+extern char line_power[7];
+extern char line_current_old[7];
+extern char line_volt_old[7];
+extern char line_power_old[7];
+extern char line_input[10];
+
 void writeNum2(char * command, uint16_t value){ 
   nexSerial.print(command);  
   if(command !="cmd"){
@@ -40,6 +48,11 @@ void screen_clear(char empty){
   memset(line1, empty, 20);
   memset(line2, empty, 20);
   memset(line3, empty, 20);
+  line0[20]=0x00;
+  line1[20]=0x00;
+  line2[20]=0x00;
+  line3[20]=0x00;
+
   writeStr("t0",(char *)line0);
   writeStr("t1",(char *)line1);
   writeStr("t2",(char *)line2);
@@ -54,6 +67,34 @@ void print_temp(void){
          memcpy(temperature_buf,temperature_val,5);
     }
     
+}
+void print_input(void){
+        writeStr("tinput",line_input);    
+}
+void print_reading(void){
+    uint8_t compare=0;
+    compare=memcmp(line_current,line_current_old,5);
+    line_current[6] =0x00;
+    if (compare!=0){
+        writeStr("tcurrent",line_current);
+        line_current[6] =0x00;
+        memcpy(line_current_old,line_current,5);
+    }
+    compare=memcmp(line_volt,line_volt_old,5);
+    line_volt[6] =0x00;
+    if (compare!=0){
+        writeStr("tvolt",line_volt);
+        line_volt[5] =0x00;
+        memcpy(line_volt_old,line_volt,6);
+    }
+    compare=memcmp(line_power_old,line_power,5);
+    line_power[6] =0x00;
+    if (compare!=0){
+        writeStr("tpower",line_power);
+        line_power[6] =0x00;
+        memcpy(line_power_old,line_power,6);
+    }
+    
 }
 void print_chars2(int x1,int len){
       uint8_t compare=0;