Per Mårtensson 1 year ago
parent
commit
ea4e8d4219
5 changed files with 456 additions and 379 deletions
  1. 19 0
      sw/DC_LOAD/include/config.h
  2. 12 0
      sw/DC_LOAD/include/screen.h
  3. 9 8
      sw/DC_LOAD/platformio.ini
  4. 240 371
      sw/DC_LOAD/src/main.cpp
  5. 176 0
      sw/DC_LOAD/src/screen.cpp

+ 19 - 0
sw/DC_LOAD/include/config.h

@@ -20,4 +20,23 @@
 #define MODE_TT                 6
 #define MODE_TL                 7
 #define MODE_BC                 8
+
+
+#define pinA                    2                   //digital pin (also interrupt pin) for the A pin of the Rotary Encoder (changed to digital pin 2)
+#define pinB                    4                   //digital pin for the B pin of the Rotary Encoder
+
+#define CursorPos               17                  //analog pin A3 used as a digital pin to set cursor position (rotary encoder push button)
+#define LoadOnOff               15                  //analog pin A1 used as a digital pin to set Load ON/OFF
+
+#define TriggerPulse            16                  //analog pin A2 used as a digital pin for trigger pulse input in transient mode
+
+#define fan                     3                   //digital pin 3 for fan control output (changed to Digital pin 3)
+#define temperature             A6                  //analog pin used for temperature output from LM35 (was A0 previously but changed)
+#define setCurrentCalibrationFactor     0.980       //calibration adjustment - set as required if needed (was 1.000)
+#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
 #endif // DCLOAD_CONFIG_H

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

@@ -0,0 +1,12 @@
+#ifndef DCLOAD_SCREEN_H
+#define DCLOAD_SCREEN_H
+#include <Arduino.h>
+void setmode();
+void print_char(int x1,int z, char text);
+void print_chars(int x1,int z, char * text,int len);
+void print_chars2(int x1,int len);
+void screen_clear(char empty);
+void writeStr(char *  command, char * txt);
+void writeNum2(char * command, uint16_t value);
+void color_load (bool load);
+#endif //DCLOAD_SCREEN_H

+ 9 - 8
sw/DC_LOAD/platformio.ini

@@ -8,17 +8,18 @@
 ; Please visit documentation for the other options and examples
 ; https://docs.platformio.org/page/projectconf.html
 
-[env:nanoatmega328]
-platform = atmelavr
-board = nanoatmega328new
+[env:nanoatmega4809]
+platform = atmelmegaavr
+board = nano_every
+
+; change microcontroller
+board_build.mcu = atmega4809
 board_build.f_cpu = 16000000L
 framework = arduino
-lib_deps =  https://git.sweproj.com/IHOLL/EasyNextionLibrary.git#master
-            https://github.com/adafruit/Adafruit_MCP4725
-            https://github.com/stevemarple/MCP342x
+lib_deps = https://github.com/stevemarple/MCP342x
         
 
 
-upload_port = /dev/ttyUSB0
-monitor_port = /dev/ttyUSB0
+upload_port = /dev/ttyACM0
+monitor_port = /dev/ttyACM0
 monitor_speed = 38400

+ 240 - 371
sw/DC_LOAD/src/main.cpp

@@ -1,5 +1,3 @@
-
-
 //SCULLCOM HOBBY ELECTRONICS
 //ELECTRONIC DC LOAD PROJECT
 //Software Version 35 (4x5 Matrix Keypad Version - modified key layout)
@@ -10,15 +8,13 @@
 #include <SPI.h>                              //include SPI library (Serial Peripheral Interface)
 #include <Wire.h>                             //include I2C library
 #include <math.h>                             //
-#include <Adafruit_MCP4725.h>                 //Adafruit DAC library  https://github.com/adafruit/Adafruit_MCP4725
 #include <MCP342x.h>                          //Steve Marple library avaiable from    https://github.com/stevemarple/MCP342x
 #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 "EasyNextionLibrary.h"
 #include <config.h>
+#include <screen.h>
 #include <SoftwareSerial.h>
+
 SoftwareSerial nexSerial (rxPin,txPin);
 
 static uint8_t __buffer[10];
@@ -40,7 +36,7 @@ void Power(void);
 void transientType(void);
 void userSetUp (void);
 void fanControl (void);
-void batteryCapacity (void);
+void batteryCapacityloop (void);
 void LoadSwitch(void);
 void dacControlVoltage (void);
 void dacControl (void);
@@ -63,7 +59,7 @@ char customKey;
 
 char decimalPoint;                            //used to test for more than one press of * key (decimal point)
 
-Adafruit_MCP4725 dac;                         //constructor
+                    
 
 MCP342x adc = MCP342x(I2C_MCP342x_ADDRESS);
 
@@ -72,16 +68,7 @@ MCP79410_Timer timer = MCP79410_Timer(I2C_MCP79410_ADDRESS);
 //Set the pins on the I2C chip used for LCD connections
 //ADDR,EN,R/W,RS,D4,D5,D6,D7
 
-const byte pinA = 2;                          //digital pin (also interrupt pin) for the A pin of the Rotary Encoder (changed to digital pin 2)
-const byte pinB = 4;                          //digital pin for the B pin of the Rotary Encoder
 
-const byte CursorPos = 17;                    //analog pin A3 used as a digital pin to set cursor position (rotary encoder push button)
-const byte LoadOnOff = 15;                    //analog pin A1 used as a digital pin to set Load ON/OFF
-
-const byte TriggerPulse = 16;                 //analog pin A2 used as a digital pin for trigger pulse input in transient mode
-
-const byte fan = 3;                           //digital pin 3 for fan control output (changed to Digital pin 3)
-const byte temperature = A6;                  //analog pin used for temperature output from LM35 (was A0 previously but changed)
 int temp;                                     //
 
 float BatteryLife = 0;                        //
@@ -107,9 +94,9 @@ float reading = 0;                            //variable for Rotary Encoder valu
 float setCurrent = 0;                         //variable used for the set current of the load
 float setPower = 0;                           //variable used for the set power of the load
 float setResistance = 0;                      //variable used for the set resistance of the load
-float setCurrentCalibrationFactor = 0.980;    //calibration adjustment - set as required if needed (was 1.000)
 
-float displayCurrentCal = 0.000;              //calibration correction for LCD current display (was 0.040)
+
+
 int Load = 0;                                 //Load On/Off flag
 
 float setControlCurrent = 0;                  //variable used to set the temporary store for control current required
@@ -120,13 +107,12 @@ float ActualVoltage = 0;                      //variable used for Actual Voltage
 float ActualCurrent = 0;                      //variable used for Actual Current reading of Load
 float ActualPower = 0;                        //variable used for Actual Power reading of Load
 
-float ResistorCutOff = 999;                   //maximum Resistor we want to deal with in software
 float BatteryCurrent;                         //
 float LoadCurrent;                            //
 
-int CurrentCutOff = EEPROM.read(0x00);
-int PowerCutOff = EEPROM.read(0x20);
-int tempCutOff = EEPROM.read(0x40);
+int CurrentCutOff =0; 
+int PowerCutOff = 0;
+int tempCutOff = 0; 
 
 int setReading = 0;                           //
 
@@ -178,167 +164,29 @@ char line2_buf[line_len]= {0};
 char line3_buf[line_len]= {0};
 char line4_buf[line_len]= {0};
 
-bool old_load=false;
+bool old_load=true;
 void setmode(void);
 void color_load (bool load);
+bool setVoltage(uint16_t output, bool writeEEPROM) {
 
-void writeNum2(char * command, uint16_t value){ 
-  nexSerial.print(command);  
-  if(command!= "cmd"){
-    nexSerial.print("=");
-    nexSerial.print(value);
-  }
-  nexSerial.print("\xFF\xFF\xFF");
-}
-void writeStr(char *  command, char * txt){ 
+  uint8_t packet[3];
 
-  nexSerial.print(command);
-    
-  if(String(command) != "cmd"){
-    nexSerial.print(".txt=\"");
-    nexSerial.print(txt);
-    nexSerial.print("\"");
+  if (writeEEPROM) {
+    packet[0] = MCP4725_CMD_WRITEDACEEPROM;
+  } else {
+    packet[0] = MCP4725_CMD_WRITEDAC;
   }
-  nexSerial.print("\xFF\xFF\xFF");
-}
-
-void screen_clear(char empty){
-  memset(line1, empty, 20);
-  memset(line2, empty, 20);
-  memset(line3, empty, 20);
-  memset(line4, empty, 20);
-  writeStr("t0",(char *)line1);
-  writeStr("t1",(char *)line2);
-  writeStr("t2",(char *)line3);
-  writeStr("t3",(char *)line4);
-}
-
+  packet[1] = output / 16;        // Upper data bits (D11.D10.D9.D8.D7.D6.D5.D4)
+  packet[2] = (output % 16) << 4; // Lower data bits (D3.D2.D1.D0.x.x.x.x)
 
-void print_chars2(int x1,int len){
-      uint8_t compare=0;
-      switch(x1){
-        case 0:
-          
-          compare=memcmp(line1_buf,line1,20);
-          line1[21]='\0';
-          if (compare!=0){
-            writeStr("t0",line1);
-          }
-          memcpy(line1_buf,line1,21);
-          break;
-        case 1:
-          compare=memcmp(line2_buf,line2,20);
-          line2[21]='\0';
-          if (compare!=0){
-            writeStr("t1",line2);
-          }
-          memcpy(line2_buf,line2,21);    
-          break;
-        case 2:
-          line3[21]='\0';
-          compare=memcmp(line3_buf,line3,20);
-          if (compare!=0)
-            writeStr("t2",line3);
-          memcpy(line3_buf,line3,21);
-          break;
-        case 3:
-          line4[21]='\0';
-          compare=memcmp(line4_buf,line4,20);
-          if (compare!=0)
-            writeStr("t3",line4);
-          memcpy(line4_buf,line4,21); 
-          break;         
-      }
-      delay(1);
-}
-void print_chars(int x1,int z, char * text,int len){
-      switch(x1){
-        case 0:
-          memcpy(line1+z,text,len);         
-          break;
-        case 1:
-          memcpy(line2+z,text,len);
-          break;
-        case 2:
-          memcpy(line3+z,text,len);
-          break;
-        case 3:         
-          memcpy(line4+z,text,len);
-          break;          
+  if (!Wire.write(packet, 3)) {
+    return false;
+  }
 
-      }
-      print_chars2(x1,20);
+  return true;
 }
-void print_char(int x1,int z, char text){
-      switch(x1){
-        case 0:  
-          line1[z]=text;
-          break;
-        case 1:
-          line2[z]=text;
-          break;
-        case 2:
-          line3[z]=text;
-          break;
-        case 3:
-          line4[z]=text;
-          break;          
 
-      }
-      print_chars2(x1,20);
-}
-void setmode(){
-  if (Mode!=old_Mode){
-    if (Mode == MODE_TC || Mode == MODE_TP || Mode == MODE_TT || Mode == MODE_TL ) {
-          writeNum2("bi.bco",(int) col_gray);
-          writeNum2("bw.bco",(int) col_gray);
-          writeNum2("br.bco",(int) col_gray);  
-          writeNum2("bbat.bco",(int) col_gray); 
-          writeNum2("btrans.bco",(int) col_blue);
-    }
-    if (Mode == MODE_CC ) {
-          writeNum2("bi.bco",(int) col_blue);
-          writeNum2("bw.bco",(int) col_gray);
-          writeNum2("br.bco",(int) col_gray);  
-          writeNum2("bbat.bco",(int) col_gray); 
-          writeNum2("btrans.bco",(int) col_gray);
-    }
-    if (Mode == MODE_CP ) {
-          writeNum2("bi.bco",(int) col_gray);
-          writeNum2("bw.bco",(int) col_blue);
-          writeNum2("br.bco",(int) col_gray);  
-          writeNum2("bbat.bco",(int) col_gray); 
-          writeNum2("btrans.bco",(int) col_gray);
-    }
-    if (Mode == MODE_CR ) {
-          writeNum2("bi.bco",(int) col_gray);
-          writeNum2("bw.bco",(int) col_gray);
-          writeNum2("br.bco",(int) col_blue);  
-          writeNum2("bbat.bco",(int) col_gray); 
-          writeNum2("btrans.bco",(int) col_gray);
-    }
-    if (Mode == MODE_BC ) {
-          writeNum2("bi.bco",(int) col_gray);
-          writeNum2("bw.bco",(int) col_gray);
-          writeNum2("br.bco",(int) col_gray);  
-          writeNum2("bbat.bco",(int) col_blue); 
-          writeNum2("btrans.bco",(int) col_gray);
-    }
-    Mode=old_Mode;
-  }
-}
-void color_load (bool load){
 
-  if (old_load != load){
-    if (load){
-      writeNum2("bload.bco",(int) col_green); 
-    }else{
-      writeNum2("bload.bco",(int) col_red); 
-    }
-  }
-  old_load=load;
-  
-}
 //--------------------------------Interrupt Routine for Rotary Encoder------------------------
 void isr()
 {
@@ -442,9 +290,7 @@ void serialEvent() {
 
                     }
                 }
-              for (i1=0;i1<7;i1++){
-                __buffer[i1]='\0';
-              } 
+              memset(__buffer,0x00,8);
               i1=0;  
             }
         }
@@ -461,24 +307,69 @@ void setup() {
   pinMode(txPin, OUTPUT);
   nexSerial.begin(38400);
   Serial.begin(38400);
+  analogReference(INTERNAL2V5);
   /* Register the pop event callback function of the current button component. */
+  delay(100);
+  unsigned long _tmr1;
+  _tmr1 = millis();
+  while(nexSerial.available() > 0){     // Read the Serial until it is empty. This is used to clear Serial buffer
+    if((millis() - _tmr1) > 400UL){    // Reading... Waiting... But not forever...... 
+      break;                            
+    } 
+      nexSerial.read();                // Read and delete bytes
+  }
+
+  CurrentCutOff = EEPROM.read(0x00);
+  PowerCutOff = EEPROM.read(0x20);
+  tempCutOff = EEPROM.read(0x40);
   MCP342x::generalCallReset();                             //Reset devices
-  delay(1);                                                //MC342x needs 300us to settle, wait 1ms - (may not be required)
   pinMode (pinA, INPUT);
   pinMode (pinB, INPUT);
   pinMode (CursorPos, INPUT_PULLUP);
   pinMode (LoadOnOff, INPUT_PULLUP);
  
   pinMode (TriggerPulse, INPUT_PULLUP);
+    byte error, address;
+  int nDevices;
+nDevices = 0;
+  for(address = 1; address < 127; address++ )
+  {
+    // The i2c_scanner uses the return value of
+    // the Write.endTransmisstion to see if
+    // a device did acknowledge to the address.
+    Wire.beginTransmission(address);
+    error = Wire.endTransmission();
+ 
+    if (error == 0)
+    {
+      Serial.print("I2C device found at address 0x");
+      if (address<16)
+        Serial.print("0");
+      Serial.print(address,HEX);
+      Serial.println("  !");
+ 
+      nDevices++;
+    }
+    else if (error==4)
+    {
+      Serial.print("Unknown error at address 0x");
+      if (address<16)
+        Serial.print("0");
+      Serial.println(address,HEX);
+    }    
+  }
+  if (nDevices == 0)
+    Serial.println("No I2C devices found\n");
+  else
+    Serial.println("done\n");
 
   pinMode (fan, OUTPUT);
-  TCCR2B = (TCCR2B & 0b11111000) | 1;                      //change PWM to above hearing (Kenneth Larvsen recommendation)
+  //TCCR2B = (TCCR2B & 0b11111000) | 1;                      //change PWM to above hearing (Kenneth Larvsen recommendation)
   pinMode (temperature, INPUT);
- 
+  setVoltage(0,false);                                 //reset DAC to zero for no output current set at Switch On
   analogReference(INTERNAL);                               //use Arduino internal reference for tempurature monitoring
   attachInterrupt(digitalPinToInterrupt(pinA), isr, LOW);
-  dac.begin(I2C_MCP4725_ADDRESS);                           //the DAC I2C address with MCP4725 pin A0 set high
-  dac.setVoltage(0,false);                                 //reset DAC to zero for no output current set at Switch On
+ 
   screen_clear('\0');
   writeNum2("t0.xcen", 1);                                 //print SCULLCOM to display with 5 leading spaces (you can change to your own)
   memcpy(line1,(char*)"SCULLCOM",9);
@@ -506,12 +397,11 @@ void setup() {
 
   line1[18]=0xB0;
   line1[19]='C';
-  //color_load(0); 
-  print_chars2(0,20);
+  color_load(0); 
   Current();                                               //sets initial mode to be CC (Constant Current) at Power Up
-  EEPROM.write(0x00, 0xff);
-  //customKey = customKeypad.getKey();
+  //EEPROM.write(0x00, 0xff);
   customKey='\0';
+
 }
 
 //------------------------------------------Main Program Loop---------------------------------
@@ -554,7 +444,7 @@ void loop() {
     dacControl();
     dacControlVoltage();                                   //sets the drive voltage to control the MOSFET
 
-    batteryCapacity();                                     //test if Battery Capacity (BC) mode is selected - if so action
+    batteryCapacityloop();                                     //test if Battery Capacity (BC) mode is selected - if so action
     fanControl();                                          //call heatsink fan control
   }
 }
@@ -639,13 +529,12 @@ void readKeypadInput (void) {
   }
 
   if(customKey == '*'){                                   //check if Battery Capacity button pressed
-    dac.setVoltage(0,false);                                //Ensures Load is OFF - sets DAC output voltage to 0
+    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
-
     if (exitMode == 1){                                   //if NO battery type selected revert to CC Mode
       line1[18]=0xB0;
       line1[19]='C';
@@ -657,11 +546,8 @@ void readKeypadInput (void) {
     }
     else
     {
-
-      memcpy(line3,(char*)BatteryType.c_str(),4);
+      memcpy(line3+16,(char*)BatteryType.c_str(),4);
       print_chars2(2,20);
-      //print_chars(16,2,(char*)BatteryType.c_str(),4);
-
       line1[18]=0xB0;
       line1[19]='C';
       color_load(0);
@@ -748,15 +634,15 @@ void maxConstantCurrentSetting (void) {
 //----------------------Power Level Cutoff Routine-------------------------------------------
 void powerLevelCutOff (void) {
   if (ActualPower  > PowerCutOff){                        //Check if Power Limit has been exceed
-  reading = 0;
-  encoderPosition = 0; 
+    reading = 0;
+    encoderPosition = 0; 
 
-  print_chars(3,0,(char*)"Exceeded Power      ",20);
-  line1[18]=0xB0;
-  line1[19]='C';
-  color_load(0);
-  print_chars2(0,20);
-  toggle = false;                                         //switch Load Off
+    print_chars(3,0,(char*)"Exceeded Power      ",20);
+    line1[18]=0xB0;
+    line1[19]='C';
+    color_load(0);
+    print_chars2(0,20);
+    toggle = false;                                         //switch Load Off
   }
 }
 
@@ -784,7 +670,7 @@ void displayEncoderReading (void) {
         print_chars(2,cursor_pos++,(char*)"0",1);
     }
     */
-    if ( Mode == MODE_CP|| Mode == MODE_CR ) {
+    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);
@@ -919,16 +805,17 @@ if (Mode == MODE_TC || Mode == MODE_TP || Mode == MODE_TT || Mode == MODE_TL){
 }
 
 //-------------------------------------Battery Capacity Discharge Routine----------------------------------------------------
-void batteryCapacity (void) {
+void batteryCapacityloop (void) {
   if (Mode == MODE_BC){
-    
+        Serial.println("TEST");  
     setCurrent = reading*1000;                             //set current is equal to input value in Amps
     setReading = setCurrent;                               //show the set current reading being used
     setControlCurrent = setCurrent * setCurrentCalibrationFactor;
     controlVoltage = setControlCurrent;
 
     //lcd.setCursor(0,3);
-    //lcd.print (timer.getTime());     
+    //lcd.print (timer.getTime()); 
+  
     print_chars(3,0,(char*)timer.getTime().c_str(),8);
     Seconds = timer.getTotalSeconds();                     //get totals seconds
   
@@ -957,8 +844,7 @@ void batteryCapacity (void) {
       }
   
     //lcd.print(BatteryLife,0);
-    //lcd.setCursor(13,3);
-    //lcd.print("mAh");
+    print_chars(3,13,"mAh",3);
     BatteryLifePrevious = BatteryLife;                      //update displayed battery capacity on LCD
     } 
   }
@@ -967,7 +853,7 @@ void batteryCapacity (void) {
   if (Mode == MODE_BC && ActualVoltage <= BatteryCutoffVolts){ //stops clock if battery reached cutoff level and switch load off
 
   BatteryCurrent = ActualCurrent;
-  dac.setVoltage(0,false);                                  //reset DAC to zero for no output current set at switch on                                             
+  setVoltage(0,false);                                  //reset DAC to zero for no output current set at switch on                                             
   toggle = false;                                           //Load is toggled OFF
   line1[18]=0xB0;
   line1[19]='C';
@@ -990,7 +876,7 @@ void batteryCapacity (void) {
 //--------------------------------------------------Fan Control----------------------------------------------------------
 void fanControl (void) {
   temp = analogRead(temperature);
-  temp = temp * 0.107421875;                                // convert to Celsius
+  temp = temp * 0.107421875/2;                                // convert to Celsius
   
   if (temp >= 40){                                        //if temperature 40 degree C or above turn fan on.
     digitalWrite(fan, HIGH); 
@@ -1013,8 +899,6 @@ void LoadSwitch(void) {
  
     if(toggle)
     {
-      //lcd.setCursor(8,0);
-      //lcd.print("OFF");
       line1[18]=0xB0;
       line1[19]='C';
       color_load(0);
@@ -1106,43 +990,43 @@ void batteryType (void) {
   wait_for_key(true);               //stop everything till the user press a key.
 
   if (customKey == '1'){
-  BatteryCutoffVolts = LiPoCutOffVoltage;
-  BatteryType = ("LiPo");
-    }
+    BatteryCutoffVolts = LiPoCutOffVoltage;
+    BatteryType = ("LiPo");
+  }
 
   if (customKey == '2'){
-  BatteryCutoffVolts = LiFeCutOffVoltage;
-  BatteryType = ("LiFe");
-    }
+    BatteryCutoffVolts = LiFeCutOffVoltage;
+    BatteryType = ("LiFe");
+  }
 
   if (customKey == '3'){
-  BatteryCutoffVolts = NiCdCutOffVoltage;
-  BatteryType = ("NiCd");  
-    }
+    BatteryCutoffVolts = NiCdCutOffVoltage;
+    BatteryType = ("NiCd");  
+  }
 
   if (customKey == '4'){
-  BatteryCutoffVolts = ZiZnCutOffVoltage;
-  BatteryType = ("ZiZn"); 
-    }
+    BatteryCutoffVolts = ZiZnCutOffVoltage;
+    BatteryType = ("ZiZn"); 
+  }
 
   if (customKey == '5'){ 
-  BatteryType = ("SetV");
-    }
+    BatteryType = ("SetV");
+  }
 
   if (customKey == '6'){                                  //Exit selection screen
-  exitMode = 1;
-    }
+    exitMode = 1;
+  }
 
   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
-    }
+    batteryType();                                                      //ignore other keys
+  }
 
   if(BatteryType == "SetV" && exitMode != 1){
-  setBatteryCutOff();
-    }
+    setBatteryCutOff();
+  }
 
   batteryTypeSelected();                                    //briefly display battery type selected and discharge cut off voltage
-  customKey='\0';
+  customKey=0x00;
   screen_clear(' ');
 
 }
@@ -1150,7 +1034,7 @@ void batteryType (void) {
 //--------------------------Set DAC Voltage--------------------------------------------
 void dacControl (void) {
   if (!toggle){
-  dac.setVoltage(0,false);                                 //set DAC output voltage to 0 if Load Off selected
+  setVoltage(0,false);                                 //set DAC output voltage to 0 if Load Off selected
     if(Mode == MODE_BC && ActualVoltage >= BatteryCutoffVolts && timer.status() == 1){
     timer.stop();
     }
@@ -1158,7 +1042,7 @@ void dacControl (void) {
   }else{
   //Serial.println("Control Voltage");                    //used for testing only
   //Serial.println(controlVoltage);                       //used for testing only
-  dac.setVoltage(controlVoltage,false);                   //set DAC output voltage for Range selected
+  setVoltage(controlVoltage,false);                   //set DAC output voltage for Range selected
     if(Mode == MODE_BC && ActualVoltage >= BatteryCutoffVolts && timer.status() != 1){
     timer.start();
     }
@@ -1168,44 +1052,39 @@ void dacControl (void) {
 //--------------------------Battery Selected Information--------------------------------------------
 void batteryTypeSelected (void) {
   char buff[6];
-  if (exitMode !=1){                                      //if battery selection was EXIT then skip this routine
-  screen_clear(' ');
-  memcpy(line1+2,"Battery Selected",16);
-  memcpy(line2+8,(char *)BatteryType.c_str(),4);
-  memcpy(line3+2,"Discharge Cutoff",16);
-  dtostrf(BatteryCutoffVolts, 4, 2, buff);
-  memcpy(line4+6,buff,4);
-  memcpy(line4+11," volts",6);
-  print_chars2(0,21);
-  print_chars2(1,21);
-  print_chars2(2,21);
-  print_chars2(3,21);                                
-  delay(3000);
+  if (exitMode != 1){                                      //if battery selection was EXIT then skip this routine
+    screen_clear(' ');
+    memcpy(line1+2,"Battery Selected",16);
+    memcpy(line2+8,(char *)BatteryType.c_str(),4);
+    memcpy(line3+2,"Discharge Cutoff",16);
+    dtostrf(BatteryCutoffVolts, 4, 2, buff);
+    memcpy(line4+6,buff,4);
+    memcpy(line4+11," volts",6);
+    print_chars2(0,21);
+    print_chars2(1,21);
+    print_chars2(2,21);
+    print_chars2(3,21);                                
+    delay(3000);
   }
 }
 
 //--------------------------Set Battery Cut-Off Voltage--------------------------------------------
 void setBatteryCutOff (void) {
 
-  //lcd.clear();
-  //lcd.setCursor(4,0);
-  //lcd.print("Enter Battery");
-  //lcd.setCursor(3,1);
-  //lcd.print("Cut-Off Voltage");
+  screen_clear(' ');
+  print_chars(0,4,(char*)"Enter Battery",14);  
+  print_chars(1,3,(char*)"Cut-Off Voltage",16); 
   y = 8;
   z = 8;
-
-
   inputValue(2);
   BatteryCutoffVolts = x;
-
-  //lcd.clear();
+  screen_clear(' ');
 }
 void wait_for_key(bool wait){
 
   bool waiting = true;
 
-  while (customKey=='\0' && (waiting)){  
+  while (customKey==0x00 && (waiting)){  
     waiting = wait;
     delay(10);
     while (customKey==0x00 ){   
@@ -1213,15 +1092,13 @@ void wait_for_key(bool wait){
         
     }
   }
-  Serial.println(customKey);
 }
 //------------------------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='\0'; 
+   customKey=0x00; 
    wait_for_key(true);
-  //customKey = customKeypad.getKey();
   if(customKey >= '0' && customKey <= '9'){               //check for keypad number input
        numbers[index++] = customKey;
        numbers[index] = '\0';
@@ -1232,7 +1109,7 @@ void inputValue (int r=0){
   if(customKey == '>'){                                   //Decimal point
       if (decimalPoint != ('>')){                         //test if decimal point entered twice - if so ski
       numbers[index++] = '.';
-      numbers[index] = '\0';
+      numbers[index] = 0x00;
       print_char(r,z,'.');
       z = z+1;
       decimalPoint = ('>');                               //used to indicate decimal point has been input
@@ -1243,7 +1120,7 @@ void inputValue (int r=0){
     index = 0;
     z = y;
 
-    numbers[index] = '\0'; 
+    numbers[index] = 0x00; 
                                    //
     print_chars(r,z,(char *)"     ",5);
     decimalPoint = (' ');                                 //clear decimal point test character reset
@@ -1254,88 +1131,82 @@ void inputValue (int r=0){
   if(customKey == 'F') {                                  //enter value 
     x = atof(numbers);     
     index = 0;
-    numbers[index] = '\0';
+    numbers[index] = 0x00;
     decimalPoint = (' ');                                 //clear decimal point test character reset
   }
-  customKey='\0';
+  customKey=0x00;
 }
 
 //----------------------------------------Transient Mode--------------------------------------------
 void transientMode (void) {
 
-if(Mode != MODE_TL){
-
-  y = 11;
-  z = 11;
-  
-  //lcd.noCursor();                                       //switch Cursor OFF for this menu               
-  //lcd.clear();
-  //lcd.setCursor(3,0);
-  //lcd.print("Transient Mode");  
-  //lcd.setCursor(0,1);
-  //lcd.print("Set Low  I=");
-  //lcd.setCursor(19,1);
-  //lcd.print("A");
-
-  inputValue(1);
-  
-  if(x >= CurrentCutOff){
-    LowCurrent = CurrentCutOff;
-  }else{
-    LowCurrent = x;
-  }
-  //lcd.setCursor(11,r);
-  //lcd.print(LowCurrent,3);
-  
-  customKey = '\0';
-
-  z = 11;
-
-  //lcd.setCursor(0,2);
-  //lcd.print("Set High I=");
-  //lcd.setCursor(19,2);
-  //lcd.print("A");
+  if(Mode != MODE_TL){
+    char buff[6];
+    y = 11;
+    z = 11;
+    screen_clear(' ');
+    print_chars(0,4,(char*)"Transient Mode",15); 
+    memcpy(line2,(char *)"Set Low  I=",11);
+    memcpy(line2+19,(char *)"A " ,2);
+    print_chars2(1,0); 
+    inputValue(1);
+    
+    if(x >= CurrentCutOff){
+      LowCurrent = CurrentCutOff;
+    }else{
+      LowCurrent = x;
+    }
 
-  inputValue(2);
-  if(x >= CurrentCutOff){
-    HighCurrent = CurrentCutOff;
-  }else{
-    HighCurrent = x;
-  }
-  //lcd.setCursor(11,r);
-  //lcd.print(HighCurrent,3);
+    r++;
+    dtostrf(LowCurrent, 5, 3, buff);
+    print_chars(r,11,buff,5); 
 
-  customKey = '\0';
+    customKey = 0x00;
 
-  if(Mode == MODE_TC || Mode == MODE_TP){
     z = 11;
+    memcpy(line3,(char *)"Set High I=",11);
+    memcpy(line3+19,(char *)"A " ,2);
+    print_chars2(2,0); 
+    inputValue(2);
+    if(x >= CurrentCutOff){
+      HighCurrent = CurrentCutOff;
+    }else{
+      HighCurrent = x;
+    }
+    r++;
+    dtostrf(HighCurrent, 5, 3, buff);
+    print_chars(r,11,buff,5); 
+
+    customKey = 0x00;
+
+    if(Mode == MODE_TC || Mode == MODE_TP){
+      z = 11;
+      memcpy(line4,(char *)"Set Time  =",11);
+      memcpy(line4+16,(char *)"mSec " ,5);
+      print_chars2(3,0); 
+      inputValue(3);
+      transientPeriod = x;
+      r++;
+      dtostrf(transientPeriod, 5, 0, buff);
+      print_chars(r,11,buff,5); 
 
-    //lcd.setCursor(0,3);
-    //lcd.print("Set Time  = ");
-    //lcd.setCursor(16,3);
-    //lcd.print("mSec");
-
-    inputValue(3);
-    transientPeriod = x;
-    //lcd.setCursor(11,r);
-    //lcd.print(transientPeriod);
-  }else{
-  //lcd.setCursor(0,3);
-  //lcd.print("                    ");
-  }
+    }else{
 
-  //lcd.clear();
+      memset(line4,' ',20);
+      print_chars2(3,0);
+    }
 
-  toggle = false;                                           //switch Load OFF
+    screen_clear(' ');
 
-  line1[18]=0xB0;
-  line1[19]='C';
-  color_load(0);
-  print_chars2(0,20);
+    toggle = false;                                           //switch Load OFF
+    memset(line1,' ',18);
+    line1[18]=0xB0;
+    line1[19]='C';
+    color_load(0);
+    print_chars2(0,20);
   }else{  
     transientListSetup();
-    //lcd.clear();
-  
+    screen_clear(' ');
     toggle = false;                                           //switch Load OFF
 
     line1[18]=0xB0;
@@ -1362,7 +1233,6 @@ void transientType (void) {
   memcpy(line4+11,(char*)"5 = Exit",9);
   print_chars2(3,0);
 
-  delay(3000);
   wait_for_key(1);                 //stop everything till the user press a key.
 
   if (customKey == '1'){
@@ -1389,7 +1259,7 @@ void transientType (void) {
     transientType();                                                      //ignore other keys
   }
 screen_clear(' ');
-customKey='\0';
+customKey=0x00;
 if (exitMode == 1){                                       //if NO Transient Mode type selected revert to CC Mode
     line1[18]=0xB0;
     line1[19]='C';
@@ -1406,39 +1276,31 @@ if (exitMode == 1){                                       //if NO Transient Mode
  
 //----------------------------------------Transient--------------------------------------------
 void transient (void) {
-  
+  char buff[6]={0x00};
   if(Mode ==  MODE_TC || Mode ==  MODE_TP || Mode ==  MODE_TT || Mode ==  MODE_TL){
     memcpy(line1,(char*)"DC LOAD ",8);
     print_chars2(0,20);
     if(Mode != MODE_TL){
-    //lcd.setCursor(0,2);
-    //lcd.print("Lo=");
-    //lcd.setCursor(3,2);
-    //lcd.print(LowCurrent,3);
-    //lcd.setCursor(8,2);
-    //lcd.print("A");
-    //lcd.setCursor(11,2);
-    //lcd.print("Hi=");
-    //lcd.setCursor(14,2);                                    //
-    //lcd.print(HighCurrent,3);
-    //lcd.setCursor(19,2);
-    //lcd.print("A");
-  }else{
-    delay(1);
-  }
+      memcpy(line3,"Lo=     A  Hi=     A",20);
+      dtostrf(LowCurrent, 4, 3, buff);
+      memcpy(line3+3,buff ,5);
+      dtostrf(HighCurrent, 4, 3, buff);
+      memcpy(line3+14,buff ,5);
+      print_chars2(2,20);
+    }else{
+      delay(1);
+    }
 
 
-  if(Mode == MODE_TC || Mode == MODE_TP || Mode == MODE_TL){
-    //lcd.setCursor(0,3);                                     //
-    //lcd.print("Time = ");
-    //lcd.setCursor(7,3);
-    //lcd.print(transientPeriod);
-    //lcd.setCursor(12,3);                                    //
-    //lcd.print("mSecs");
+    if(Mode == MODE_TC || Mode == MODE_TP || Mode == MODE_TL){
+      memcpy(line4,"Time =       mSecs   ",20);
+      dtostrf(transientPeriod, 5, 0, buff);
+      memcpy(line4+7,buff,5);
     }else{
-     //lcd.setCursor(0,3);
-     //lcd.print("  ");
+      memcpy(line4,"  ",2);
+     
     }
+    print_chars2(3,20);
   }
 delay(1);
 }
@@ -1452,36 +1314,43 @@ void transientListSetup(){
   print_chars2(0,0);
   memcpy(line2,(char*)"Enter Number in List",21);
   print_chars2(1,0);
-  memcpy(line2,(char*)"(between 2 to 10 max",21);
+  memcpy(line3,(char*)"(between 2 to 10 max",21);
   print_chars2(2,0);
   y = 0;
   z = 0;
   inputValue(3);
   total_instructions = int(x-1);
-  customKey = '\0';
+  customKey = 0x00;
   //lcd.clear();
-  
+  char buff[5] = {0x00};
     for(int i=0; i<=(total_instructions); i++){
-      //lcd.setCursor(0,0);
-      //lcd.print("Set Current ");
-      //lcd.print(i+1);
-      //lcd.setCursor(16,1);
-      //lcd.print("A");
+      screen_clear(' ');
+      memcpy(line1,"Set Current     ",16);
+      int length = snprintf( NULL, 0, "%d", i+1 );
+      itoa(i+1 ,buff,10);
+      memcpy(line1+12,buff,length);
+      memcpy(line2,"               A",16);
+      print_chars2(0,0);
+      print_chars2(1,0);
+
       y = 0;
       z = 0;
       inputValue(1);            //get the users input value
       transientList[i][0] = x; //store the users entered value in the transient list 
-      customKey = '\0';
-      //lcd.setCursor(0,2);
-      //lcd.print("Set Time ");
-      //lcd.print(i+1);
-      //lcd.setCursor(16,3);
-      //lcd.print("mSec");
+      customKey = 0x00;
+      memcpy(line3,"Set Time        ",16);
+      length = snprintf( NULL, 0, "%d", i+1 );
+      itoa(i+1 ,buff,10);
+      
+      memcpy(line3+12,buff,length);
+      memcpy(line4,"               mSec",19);
+      print_chars2(2,0);
+      print_chars2(3,0);
       y = 0;
       z = 0;
       inputValue(3);            //get the users input value
       transientList[i][1] = x; //store the users entered value in the transient list
-      customKey = '\0';
+      customKey = 0x00;
       screen_clear(' ');  
   }
   current_instruction = 0;      //start at first instrution
@@ -1602,7 +1471,7 @@ void userSetUp (void) {
   CurrentCutOff = x;
   EEPROM.write(0x00, CurrentCutOff);
   
-  customKey = '\0';
+  customKey = 0x00;
   z = 15;
   memcpy(line3,"Power Limit =      W",21);
   print_chars2(2,20);
@@ -1610,7 +1479,7 @@ void userSetUp (void) {
   PowerCutOff = x;
   EEPROM.write(0x20, PowerCutOff);        
 
-  customKey = '\0';
+  customKey = 0x00;
   z = 15;
   line4[18]=0xB0;
   memcpy(line4,"Temp. Limit =      C",21);

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

@@ -0,0 +1,176 @@
+#include <Arduino.h>
+#include <config.h>
+#include <SoftwareSerial.h>
+extern SoftwareSerial nexSerial;
+extern uint8_t Mode; 
+extern uint8_t old_Mode;
+extern char line1[line_len];
+extern char line2[line_len];
+extern char line3[line_len];
+extern char line4[line_len];
+extern char line1_buf[line_len];
+extern char line2_buf[line_len];
+extern char line3_buf[line_len];
+extern char line4_buf[line_len];
+extern bool old_load;
+
+void writeNum2(char * command, uint16_t value){ 
+  nexSerial.print(command);  
+  if(command !="cmd"){
+    nexSerial.print("=");
+    nexSerial.print(value);
+  }
+  nexSerial.print("\xFF\xFF\xFF");
+}
+void writeStr(char *  command, char * txt){ 
+
+  nexSerial.print(command);
+    
+  if(command != "cmd"){
+    nexSerial.print(F(".txt=\""));
+    nexSerial.print(txt);
+    nexSerial.print("\"");
+  }
+  nexSerial.print("\xFF\xFF\xFF");
+}
+
+void screen_clear(char empty){
+  memset(line1, empty, 20);
+  memset(line2, empty, 20);
+  memset(line3, empty, 20);
+  memset(line4, empty, 20);
+  writeStr("t0",(char *)line1);
+  writeStr("t1",(char *)line2);
+  writeStr("t2",(char *)line3);
+  writeStr("t3",(char *)line4);
+}
+
+
+void print_chars2(int x1,int len){
+      uint8_t compare=0;
+      switch(x1){
+        case 0: 
+          line1[21]=0x00;        
+          compare=memcmp(line1_buf,line1,20);
+          if (compare!=0){
+            writeStr("t0",line1);
+          }
+          line1[21]=0x00;
+          memcpy(line1_buf,line1,21);
+          break;
+        case 1:
+          line2[21]=0x00;
+          compare=memcmp(line2_buf,line2,20);
+          if (compare!=0){
+            writeStr("t1",line2);
+          }
+          line2[21]=0x00;
+          memcpy(line2_buf,line2,21);    
+          break;
+        case 2:
+          line3[21]=0x00;
+          compare=memcmp(line3_buf,line3,20);
+          if (compare!=0)
+            writeStr("t2",line3);
+          line3[21]=0x00;
+          memcpy(line3_buf,line3,21);
+          break;
+        case 3:
+          line4[21]=0x00;
+          compare=memcmp(line4_buf,line4,20);
+          if (compare!=0)
+            writeStr("t3",line4);
+          line4[21]=0x00;
+          memcpy(line4_buf,line4,21); 
+          break;         
+      }
+      delay(1);
+}
+void print_chars(int x1,int z, char * text,int len){
+      switch(x1){
+        case 0:
+          memcpy(line1+z,text,len);         
+          break;
+        case 1:
+          memcpy(line2+z,text,len);
+          break;
+        case 2:
+          memcpy(line3+z,text,len);
+          break;
+        case 3:         
+          memcpy(line4+z,text,len);
+          break;          
+
+      }
+      print_chars2(x1,20);
+}
+void print_char(int x1,int z, char text){
+      switch(x1){
+        case 0:  
+          line1[z]=text;
+          break;
+        case 1:
+          line2[z]=text;
+          break;
+        case 2:
+          line3[z]=text;
+          break;
+        case 3:
+          line4[z]=text;
+          break;          
+
+      }
+      print_chars2(x1,20);
+}
+void setmode(){
+  if (Mode!=old_Mode){
+    if (Mode == MODE_TC || Mode == MODE_TP || Mode == MODE_TT || Mode == MODE_TL ) {
+          writeNum2("bi.bco",(int) col_gray);
+          writeNum2("bw.bco",(int) col_gray);
+          writeNum2("br.bco",(int) col_gray);  
+          writeNum2("bbat.bco",(int) col_gray); 
+          writeNum2("btrans.bco",(int) col_blue);
+    }
+    if (Mode == MODE_CC ) {
+          writeNum2("bi.bco",(int) col_blue);
+          writeNum2("bw.bco",(int) col_gray);
+          writeNum2("br.bco",(int) col_gray);  
+          writeNum2("bbat.bco",(int) col_gray); 
+          writeNum2("btrans.bco",(int) col_gray);
+    }
+    if (Mode == MODE_CP ) {
+          writeNum2("bi.bco",(int) col_gray);
+          writeNum2("bw.bco",(int) col_blue);
+          writeNum2("br.bco",(int) col_gray);  
+          writeNum2("bbat.bco",(int) col_gray); 
+          writeNum2("btrans.bco",(int) col_gray);
+    }
+    if (Mode == MODE_CR ) {
+          writeNum2("bi.bco",(int) col_gray);
+          writeNum2("bw.bco",(int) col_gray);
+          writeNum2("br.bco",(int) col_blue);  
+          writeNum2("bbat.bco",(int) col_gray); 
+          writeNum2("btrans.bco",(int) col_gray);
+    }
+    if (Mode == MODE_BC ) {
+          writeNum2("bi.bco",(int) col_gray);
+          writeNum2("bw.bco",(int) col_gray);
+          writeNum2("br.bco",(int) col_gray);  
+          writeNum2("bbat.bco",(int) col_blue); 
+          writeNum2("btrans.bco",(int) col_gray);
+    }
+    Mode=old_Mode;
+  }
+}
+void color_load (bool load){
+
+  if (old_load != load){
+    if (load){
+      writeNum2("bload.bco",(int) col_green); 
+    }else{
+      writeNum2("bload.bco",(int) col_red); 
+    }
+  }
+  old_load=load;
+  
+}