1. This forum is obsolete and read-only. Feel free to contact us at support.keenswh.com

[Request for Script] - Help a script newb? Battery Status output to LCD

Discussion in 'Programming Questions and Suggestions' started by rottielover, Feb 20, 2015.

Thread Status:
This last post in this thread was made more than 31 days old.
  1. rottielover

    rottielover Apprentice Engineer

    Messages:
    485
    So with the new update I started thinking about all the various stuff I want to display in my command center now...

    Unfortunately, I SUCK at programming. BADLY. I've tried and tried and tried and tried and I just suck.


    Can someone post example code to do the following:

    Search the grid for all Battery Blocks
    Output the status to the LCD panel, preferably in a format like:

    NAME Charge% Status

    Battery 1 100% Recharge
    Battery 2 48% Discharging
    Battery 3 10% OFF


    I know you can color the background and text of an entire LCD block, but can you do specific Text on the same block?


    NAME Charge% Status

    Battery 1 100% Recharge
    Battery 2 48% Discharging
    Battery 3 10% OFF
     
  2. rottielover

    rottielover Apprentice Engineer

    Messages:
    485
    bueller ... bueller ... bueller ... bueller?
     
  3. Wicorel

    Wicorel Senior Engineer

    Messages:
    1,263
    Here is a battery status display.

    name the target LCD "Battery Status"

    Code:
    /*     
     *  Original Written and tested by Stiggan and Malakeh in January 2015.     
     *     
     * Additions and fixes by Wicorel --January 2015     
     *     
     * Status display by request from forums Late Feb 2015 by Wicorel 
     */     
       
      
    const string OurName="Battery Status";  
       
       
    void Log(string text)    
    {    
       
        var lcd = GridTerminalSystem.GetBlockWithName(OurName) as IMyTextPanel;    
        if (lcd != null)  
        { 
            if (text.Equals("clear"))      
            {     
                lcd.WritePublicText("");   
                lcd.WritePublicTitle(OurName + " Status");    
       
            }    
            else   
            {   
                string oldtext = lcd.GetPublicText();    
      
                lcd.ShowTextureOnScreen(); 
                lcd.WritePublicText(oldtext +"\n" + text, false);   
            } 
            lcd.ShowPublicTextOnScreen();   
        }  
      
    }  
      
    string getDetailedInfoValue(IMyTerminalBlock block, string name)    
    {   
        string value = "";   
        string[] lines = block.DetailedInfo.Split(new string[] { "\r\n", "\n", "\r" }, StringSplitOptions.None);   
        for (int i = 0; i < lines.Length; i++)    
        {   
            string[] line = lines[i].Split(':');   
            if (line[0].Equals(name))    
            {   
                value = line[1].Substring(1);   
                break;   
            }   
        }   
        return value;   
    }   
       
    bool detailExist(IMyTerminalBlock block, string name)    
    {   
        return !String.IsNullOrEmpty(getDetailedInfoValue(block, name));   
    }   
       
    int getPowerAsInt(string text)    
    {   
        if (String.IsNullOrWhiteSpace(text))    
        {   
            return 0;   
        }   
        string[] values = text.Split(' ');   
        if (values[1].Equals("kW"))    
        {   
            return (int) (float.Parse(values[0])*1000f);   
        }   
        else if (values[1].Equals("kWh"))    
        {    
            return (int) (float.Parse(values[0])*1000f);    
        }   
        else if (values[1].Equals("MW"))    
        {   
            return (int) (float.Parse(values[0])*1000000f);   
        }   
        else if (values[1].Equals("MWh"))    
        {    
            return (int) (float.Parse(values[0])*1000000f);    
        }   
        else    
        {   
            return (int) float.Parse(values[0]);   
        }   
        return 0;   
    }   
       
    bool isHealthy(IMyTerminalBlock block)    
    {   
        return (block.IsFunctional && block.IsWorking);   
    }   
       
       
       
    int getPower(IMyTerminalBlock block, bool max)    
    {   
        if (max)    
        {   
            if (!block.IsBeingHacked)    
            {   
                return getPowerAsInt(getDetailedInfoValue(block, "Max Output"));   
            }   
        }   
        else    
        {   
            return getPowerAsInt(getDetailedInfoValue(block, "Current Output"));   
        }   
        return 0;   
    }   
       
       
    bool isRecharging(IMyTerminalBlock block)    
    {   
        return detailExist(block, "Fully recharged in");   
    }   
       
        
       
       
    void Main()    
    {   
      Log("clear"); // make the status clear for this run   
       
        List<IMyTerminalBlock> batteries = new List<IMyTerminalBlock>();   
        GridTerminalSystem.GetBlocksOfType<IMyBatteryBlock>(batteries);   
            
       
      Log("NAME             Charge %        Status"); 
     
      for (int i = 0; i < batteries.Count; i++)     
      {     
      int charge=0;    
            int capacity=0;      
            bool thecharge=true;       
            int percentthisbattery=0;   
               
            capacity += getPowerAsInt(getDetailedInfoValue(batteries[i], "Max Stored Power"));      
            charge += getPowerAsInt(getDetailedInfoValue(batteries[i], "Stored power"));     
            if(capacity>0)   
            {     
       
                percentthisbattery=(charge*100)/capacity ;   
                string statustext="";
                statustext=batteries[i].CustomName;
                statustext+="         " + percentthisbattery;
                if(isRecharging(batteries[i]))
                {
                    statustext+="               Recharging";
                }
                else
                {
                    statustext+="               Discharging";
                }
       
                Log(statustext);
            }    
      }  
      
       
    }   
              
    
     
  4. rottielover

    rottielover Apprentice Engineer

    Messages:
    485
    THANK YOU!!!!!!!!
     
Thread Status:
This last post in this thread was made more than 31 days old.