LCD.clear(int y) clears more than expected !?

This is where you talk about the EV3 software itself, installation issues, and programming talk.

Moderators: roger, gloomyandy, skoehler

ralfb1105
New User
Posts: 12
Joined: Mon Jan 15, 2018 10:14 am

LCD.clear(int y) clears more than expected !?

Postby ralfb1105 » Fri Jan 26, 2018 3:46 pm

Hello,

I'm new at the LeJOS Forum and I have just started programming my EV3 with Java using LeJOS 0.9.1. I'm also a beginner using Java :roll:
Here is what I tried to do. I want to call a class in a thread which simply show the actual Power and Current values on the EV3 display. The main class do some other stuff, the battery info will be displayed and updated in a 5 sec interval.
Here are my code as a reference:
TestThread.java

Code: Select all

package com.lego.ev3.lejos;

import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.Button;
import lejos.hardware.lcd.Font;
import lejos.hardware.lcd.GraphicsLCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;

public class TestThread {
   public static void main(String[] args) {
      Runnable batteryInfos = new BatteryDetails();
      Thread thread1 = new Thread(batteryInfos);
      thread1.start();
      
      GraphicsLCD lcd = LocalEV3.get().getGraphicsLCD();
      lcd.setFont(Font.getSmallFont());
      lcd.drawString("Press ESCAPE to exit ...", 2, 60, 0);
      
      RegulatedMotor left = new EV3LargeRegulatedMotor(MotorPort.B);
      RegulatedMotor right = new EV3LargeRegulatedMotor(MotorPort.C);
      
      while(Button.ESCAPE.isUp()) {
         left.forward();
         right.forward();
      }
      
      /*
       *  Clean Up after ESCAPE was pressed ...
       */
      
      thread1.interrupt();
      left.stop();
      left.close();
      right.stop();
      right.close();
      System.exit(0);
   }
}

BatteryDetails.java

Code: Select all

package com.lego.ev3.lejos;

import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.Battery;
import lejos.hardware.lcd.Font;
import lejos.hardware.lcd.GraphicsLCD;
import lejos.hardware.lcd.LCD;
import lejos.utility.Delay;

public class BatteryDetails implements Runnable {
   public void run () {
      while (true) {
      GraphicsLCD lcd = LocalEV3.get().getGraphicsLCD();
      lcd.setFont(Font.getSmallFont());
      
      Float batteryPower = Battery.getVoltage();
      Float batteryCurrent = Battery.getBatteryCurrent();
      String valueBatteryPower = "Volt: " + String.format("%.2f", batteryPower) + "V";
      String valueBatteryCurrent = "Amp:  " + String.format("%.2f", batteryCurrent) + "A";
      
      //clear the lines 0-25 before printing the details
      for (int i=0; i<=25; i+=1) {
         LCD.clear(i);
      }
      lcd.drawLine(0, 25, 178, 25);
      lcd.drawString(valueBatteryPower, 30, 2, 0);
      lcd.drawString(valueBatteryCurrent, 30, 12, 0);
      
      Delay.msDelay(5000);
      }
   }
}

In general it works :D
BUT, the Message printed by the main class

Code: Select all

lcd.drawString("Press ESCAPE to exit ...", 2, 60, 0);

will be overwritten by the Thread "BatteryDetails.java".
I figured out that the message will not be overwritten if I comment out the for-loop which clears my rows 0-25

Code: Select all

for (int i=0; i<=25; i+=1) {
         LCD.clear(i);
      }

which is necessary because otherwise the voltage/current value, which are changing during the loop, will not shown clearly.

I don't understand why the message at row 60 (y=60) will be cleared??

Has anybody an idea?

Thanks for your help!

Regards,

Ralf

User avatar
gloomyandy
leJOS Team Member
Posts: 6017
Joined: Fri Sep 28, 2007 2:06 pm
Location: UK

Re: LCD.clear(int y) clears more than expected !?

Postby gloomyandy » Fri Jan 26, 2018 8:37 pm

LCD is different to the lcd you are using in the rest of your code. The screen can be accessed in two modes one is character based and uses co-ordinates that are in characters (not pixels), the other is in graphics mode and uses co-ordinates that are in pixels. The static LCD references the character mode interface (and should probably not be used). So in this case you will be using the text mode display (see the class TextLCD) and the call to clear will be clearing an entire line of text. You are using the graphics API for most of your operations. You have a couple of choices, you can either switch to using the text mode LCD (and get that interface rather than the graphics one by calling getTextLCD) and adjust your co-ordinates to be in characters and lines rather than pixels, or continue to use the graphics interface and use something other than clear to erase the section of the display, in this case you should probably use fillRect to erase the selected screen area.
leJOS news https://lejosnews.wordpress.com/

ralfb1105
New User
Posts: 12
Joined: Mon Jan 15, 2018 10:14 am

Re: LCD.clear(int y) clears more than expected !?

Postby ralfb1105 » Sat Jan 27, 2018 11:46 am

Hi,

Thanks a lot for this very good explanation!! I will remove the LCD* code and use only the GraphicsLCD class. If I have finished my code I will post it here :wink:

Regards,

Ralf

ralfb1105
New User
Posts: 12
Joined: Mon Jan 15, 2018 10:14 am

Re: LCD.clear(int y) clears more than expected !?

Postby ralfb1105 » Sun Jan 28, 2018 4:25 pm

Hi,

as mentioned, here are my class which prints the battery details including a short graphic. The BatteryDetails class will be executed in in a thread to make sure it will be updated in parallel.
Maybe someone else find it useful :wink:

TestThread.java

Code: Select all

package com.lego.ev3.lejos;

import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.Button;
import lejos.hardware.lcd.Font;
import lejos.hardware.lcd.GraphicsLCD;
import lejos.hardware.motor.EV3LargeRegulatedMotor;
import lejos.hardware.port.MotorPort;
import lejos.robotics.RegulatedMotor;
import java.io.File;
import lejos.hardware.Sound;
import lejos.utility.Delay;

public class TestThread {
   public static void main(String[] args) {
      /*
       * Start Class BatteryDetails in new Thread
       */
      Runnable batteryInfos = new BatteryDetails();
      Thread thread1 = new Thread(batteryInfos);
      thread1.start();
      
      /*
       * Play some sound files :-)
       */
      
      File wavFile1 = new File("Welcome.wav");
      File wavFile2 = new File("R2D2.wav");
      File wavFile3 = new File("SpecFX.wav");
      
      if (!wavFile1.exists() || !wavFile2.exists() || !wavFile3.exists()) {
         System.out.println("Error!");
         System.exit(1);
      }
      Sound.playSample(wavFile1, 100);
      Delay.msDelay(1000);
      
      Sound.playSample(wavFile2, 100);
      Delay.msDelay(1000);
      
      Sound.playSample(wavFile3, 100);
      Delay.msDelay(2000);
      
      /*
       * Print message ...
       */
      
      GraphicsLCD lcd = LocalEV3.get().getGraphicsLCD();
      lcd.setFont(Font.getSmallFont());
      lcd.drawString("Press ESCAPE to exit ...", 2, 60, 0);
      
      /*
       * Initialize Motor on Port B + C and start Motors until ESCAPE is pressed
       */
      RegulatedMotor left = new EV3LargeRegulatedMotor(MotorPort.B);
      RegulatedMotor right = new EV3LargeRegulatedMotor(MotorPort.C);
      
      while(Button.ESCAPE.isUp()) {
         left.forward();
         right.forward();
      }
      
      /*
       *  Clean Up after ESCAPE was pressed ...
       */
      thread1.interrupt();
      left.stop();
      left.close();
      right.stop();
      right.close();
      System.exit(0);
   }
}

BatteryDetails.java

Code: Select all

package com.lego.ev3.lejos;

import lejos.hardware.ev3.LocalEV3;
import lejos.hardware.Battery;
import lejos.hardware.lcd.Font;
import lejos.hardware.lcd.GraphicsLCD;
import lejos.utility.Delay;

public class BatteryDetails implements Runnable {
   public void run () {
      while (true) {
      GraphicsLCD lcd = LocalEV3.get().getGraphicsLCD();
      lcd.setFont(Font.getSmallFont());
      
      /*
       *  Fully charged chargeable Battery pack has ~ 8300 mV
       *  Adapt this to 9000mv for non chargeable battery
       */
      
      Integer batteryPowerMvMax = 8400;
      
      Float batteryPower = Battery.getVoltage();
      Integer batteryPowerMv = Battery.getVoltageMilliVolt();
      Integer batteryPowerPercent = batteryPowerMv / (batteryPowerMvMax / 100);
      Float batteryCurrent = Battery.getBatteryCurrent();
      String valueBatteryPower = String.format("%.2f", batteryPower) + "V";
      String valueBatteryCurrent = String.format("%.2f", batteryCurrent) + "A";
      String valueBatteryPowerPercent = batteryPowerPercent + "%";
      
      /*
       * clear the area with the Power/Current Values and Battery fill area before printing the details.
       * Use fillRect with color white (rgb>0)
       */
      lcd.setColor(1);
      lcd.fillRect(0, 0, 30, 24);
      lcd.fillRect(120, 0, 28, 24);
      lcd.fillRect(62, 5, 40, 15);

      /*
       * Print the Battery Details
       */
      lcd.setColor(0);
      lcd.drawLine(0, 25, 178, 25);
      lcd.drawString(valueBatteryPower, 2, 2, 0);
      lcd.drawString(valueBatteryCurrent, 2, 12, 0);
      lcd.drawString(valueBatteryPowerPercent, 120, 9, 0);
      /*
       * Print the Battery empty frame
       */
      lcd.drawRect(60, 3, 50, 18);
      lcd.fillRect(110, 5, 4, 15);
      
      /*
       * Print the Rectangles to indicate the charging level
       * The batteryPowerMv steps have to be changed for non chargeable batteries
       */
      if (batteryPowerMv >=0) {
         lcd.fillRect(62, 5, 5, 15);
      }
      if (batteryPowerMv >840) {
         lcd.fillRect(67, 5, 5, 15);
      }
      if (batteryPowerMv >1680) {
         lcd.fillRect(72, 5, 5, 15);
      }
      if (batteryPowerMv >2520) {
         lcd.fillRect(77, 5, 5, 15);
      }
      if (batteryPowerMv >3360) {
         lcd.fillRect(82, 5, 5, 15);
      }
      if (batteryPowerMv >4200) {
         lcd.fillRect(87, 5, 5, 15);
      }
      if (batteryPowerMv >5040) {
         lcd.fillRect(82, 5, 5, 15);
      }
      if (batteryPowerMv >5880) {
         lcd.fillRect(87, 5, 5, 15);
      }
      if (batteryPowerMv >6720) {
         lcd.fillRect(92, 5, 5, 15);
      }
      if (batteryPowerMv >7560) {
         lcd.fillRect(97, 5, 5, 15);
      }
      
      Delay.msDelay(5000);
      }
   }
}


Again, THANKS a lot for your help about LCD vs. GraphicsLCD.

Regards,

Ralf


Return to “EV3 Software”

Who is online

Users browsing this forum: No registered users and 1 guest