JOPUI

From JopWiki

Jump to: navigation, search

A user interface for JOP.


Contents

[edit] Features

[edit] General

  • Theme
  • Image Loading (trough converter tool - no animation, no alpha channel), known formats: png, gif, bmp, jpg - depending on the installed java environment

[edit] VGA

[edit] Mouse

  • PS/2
  • 3 Buttons
  • No scrolling
  • Poll and interrupt operation possible

[edit] Keyboard

  • PS/2
  • US International
  • 101 supported keys
  • Poll and interrupt operation possible
  • doc

[edit] Components

Available components in JopUi for drawing.

  • Button
  • Canvas
  • CheckBox
  • ImageBox
  • Label
  • Option (and OptionGroup)
  • TextField

[edit] Graphics Routines

  • draw Point
  • draw Line (using the Bresenham line drawing algorithm)
  • draw Circle (using the Bresenham circle drawing algorithm)
  • draw Ellipse (using the Bresenham ellipse drawing algorithm)
  • draw Rectangle
  • draw Char
  • draw String
  • draw Image

[edit] Font

  • 10x8 pixel grid

[edit] Tutorial

[edit] GUI and IO Example

This tutorial explains a simple program with a label and a button.

Each application is derived from JopUiApplication. We call our program JopUiHello:

import com.jopdesign.jopui.JopUiApplication;
import com.jopdesign.jopui.event.JopEvent;

public class JopUiButton extends JopUiApplication {
	
	public boolean init() { // is called at start of the application
 
		return true; // return true to confirm the successful initialization
	}

	public boolean notify(JopEvent ev) { // is called when an event happens
	
		return false; // when true is returned a redraw is forced
	}	

	public void terminate() { // is called on program end
	}
}

The JopUi also has to be started, you can do this in a main method. Here we use a seperate class called JopUiStart.

import com.jopdesign.jopui.JopUi;

public class JopUIStart {
	public static void main(String[] args) {
		
		JopUi.register(new JopUiHello()); // add all programs which should be executed
		JopUi.run(); // start the JopUi
	}
}

Now we want to add a label at the center of the screen and a button on the left lower corner.

import com.jopdesign.jopui.JopUiApplication;
import com.jopdesign.jopui.event.JopEvent;
import com.jopdesign.jopui.lcdui.Button;
import com.jopdesign.jopui.lcdui.Label;

public class JopUiButton extends JopUiApplication {

	private Label disp; // we want to change the text value later so we have to keep the object

	public boolean init() {

 		disp = new Label(59,89,200,60, "button");
		disp.setHalign(Label.CENTER); // change horizontal alignment
		disp.setValign(Label.MIDDLE); // change vertical alignment
		disp.setColorBody(new Color8Bit(Color8Bit.BLACK)); // set a background color
		canvas.add(disp); // add the label to the canvas of your application 
  
		Button b = new Button(209,214,100,15, "press");
 		b.register(this, "press"); // as the button receives an event we want it to send it to our notify
 		canvas.add(b); // add the button to the canvas

 		return true; 
	}

	public boolean notify(JopEvent ev) { 
 	
 		return false; 
 	}	

	public void terminate() { 
	}	
}

And now we want to change the background of the label to blue when the button is pressed.

import com.jopdesign.jopui.JopUiApplication;
import com.jopdesign.jopui.event.JopEvent;
import com.jopdesign.jopui.event.KeyboardEvent;
import com.jopdesign.jopui.event.MouseEvent;
import com.jopdesign.jopui.lcdui.Button;
import com.jopdesign.jopui.lcdui.Label;

public class JopUiButton extends JopUiApplication {

	private Label disp; // we want to change the text value later so we have to keep the object

	public boolean init() {

 		disp = new Label(59,89,200,60, "button");
		disp.setHalign(Label.CENTER);
		disp.setValign(Label.MIDDLE);
		disp.setColorBody(new Color8Bit(Color8Bit.BLACK));
		canvas.add(disp);
  
		Button b = new Button(209,214,100,15, "press");
 		b.register(this, "press");
  		canvas.add(b);

 		return true; 
	}

	public boolean notify(JopEvent ev) { 
		boolean ret = false;
	
		if(ev.getCommand() == "apply") {
			if(ev.getEventType() == JopEvent.MOUSE_EVENT) { // when the event was a mouse event
				MouseEvent mev = (MouseEvent) ev;
				if(mev.getButton() == MouseEvent.LEFT_BUTTON &&
				   mev.getAction() == MouseEvent.MOUSE_UP) { // and the left button went up
					ret = true; // force redraw
					disp.setColorBody(new Color8Bit(Color8Bit.BLUE));
				}
					
			} else { // it must have been a keyboard event
				KeyboardEvent kbev = (KeyboardEvent) ev;
				if(kbev.getCharacter() == ' ' && 
				   kbev.getAction() == KeyboardEvent.KEY_RELEASED) { // if the spacebar is released
					ret = true;
					disp.setColorBody(new Color8Bit(Color8Bit.BLUE)); // change the color
				}
			}
		
		}
		return ret;
	}	

	public void terminate() { 
	}	
}

[edit] I/O Interrupt examples

The keyboard and mouse devices allow operation in polling and interrupt mode. The following example shows, how to use the devices.

[edit] Preparations

When using the ALTERA DE2-70 target without a PS/2 splitter (which splits the single PS/2 port of the device in a mouse and a keyboard port), the pin definitions of MS_CLK has to be exchanged with KB_CLK and MS_DAT with KB_DAT when trying to use a mouse.

[edit] Interrupt example

This example shows, how to use the keyboard and mouse in interrupt mode. There are special classes, the MouseInterruptHandler and the KeyboardInterruptHandler, which collect each event of the attached device. They both implement the Runnable interface and are executed in paralell to the main program. The main program then tests for occured events in an infinite loop, retrieves one event a time and prints the details to the screen.


 package test;
 import util.*;
 import com.jopdesign.io.*;
 import com.jopdesign.sys.Native;
 import com.jopdesign.sys.Const;
 import com.jopdesign.jopui.helper.KeyBoard;
 import com.jopdesign.jopui.MouseInterruptHandler;
 import com.jopdesign.jopui.KeyboardInterruptHandler;
 import com.jopdesign.jopui.event.KeyboardEvent;
 import com.jopdesign.jopui.event.MouseEvent;
 public class IRQTest {		
 	public static void main(String[] args) {
 		MouseInterruptHandler mh = new MouseInterruptHandler();
 		KeyboardInterruptHandler kh = new KeyboardInterruptHandler();
 		IOFactory fact = IOFactory.getFactory();
 		SysDevice sys = fact.getSysDevice();
 		
 		// register the handler
 		fact.registerInterruptHandler(1, mh);
 		fact.registerInterruptHandler(2, kh);
 		
 		// enable interrupt 1
 		fact.enableInterrupt(1);
 		fact.enableInterrupt(2);
 		
 		System.out.println("Program started.");
 		for (;;) {
 			if(mh.NewEventPending()){
 				MouseEvent mev = mh.getEvent();
 				System.out.println("New Mouse Event:");
 				System.out.println("X:           "+mev.getXChange());
 				System.out.println("Y:           "+mev.getYChange());
 				if( mev.isButtonPressed(MouseEvent.LEFT_BUTTON)){
 					System.out.println("LEFT Button pressed.");
 				}
 				if( mev.isButtonPressed(MouseEvent.MIDDLE_BUTTON)){
 					System.out.println("MIDDLE Button pressed.");
 				}
 				if( mev.isButtonPressed(MouseEvent.RIGHT_BUTTON)){
 					System.out.println("RIGHT Button pressed.");
 				}
 			}
 			
 			if(kh.NewEventPending()){
 				KeyboardEvent kev = kh.getEvent();
 				if(kev.getAction() == KeyboardEvent.KEY_PRESSED){
 					System.out.print(kev.getCharacter());
 				}
 			}
 		}
 	}
 }

[edit] Mouse polling example

When using polling mode, the main task has to do the whole work of reading the device registers and evaluating them. There is only one task running and if the processor is under load, the possibility of lost events exists. The following example outlines the usage of the mouse in polling mode. package test;

 import util.*;
 import com.jopdesign.sys.Native;
 import com.jopdesign.sys.Const;
 public class Mouse_test {
 		
 	public static void main(String[] agrgs) throws Exception {
 
 		System.out.println("download worked !");
 		System.out.println("Press 'c' to continue !");
 	
 		for (;;) {
 			System.out.println("Waiting for changes.");
 			int status = 0;
 			int flag = 0;
 			int xval = 0;
 			int yval = 0;
 			int status_old = -1;
 			int flag_old = -1;
 			int xval_old = -1;
 			int yval_old = -1;
 			while(( flag & Const.MSK_DTA_RDY) == 0) {
 			  status_old = status;
 			  flag_old   = flag;
 			  xval_old   = xval;
 			  yval_old   = yval;
 
 			  status = Native.rd(Const.MOUSE_STATUS);
 			  flag   = Native.rd(Const.MOUSE_FLAG);
                         xval   = Native.rd(Const.MOUSE_X_INC);
 			  yval   = Native.rd(Const.MOUSE_Y_INC);
 			}
 
 			System.out.print("STATUS: ");
 			System.out.println(Integer.toHexString(status));
 			System.out.print("FLAG: ");
 			System.out.println(Integer.toHexString(flag));
 			System.out.print("X_INC: ");
 			System.out.println(xval);
 			System.out.print("Y_INC: ");
 			System.out.println(yval);
 		}
 		
 	}
 }

[edit] Class Model

Image:jopui_componentmodel.png

[edit] API

The javadoc of JopUi.

Personal tools