Skip to content
This repository has been archived by the owner on Apr 18, 2024. It is now read-only.

Commit

Permalink
git-svn-id: https://ledcube.googlecode.com/svn/trunk@46 d0259a58-13ce…
Browse files Browse the repository at this point in the history
…-11de-b208-e5a9466b8568
  • Loading branch information
brice.fernandes committed May 7, 2009
1 parent 097a8a9 commit 73bc2d6
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 36 deletions.
8 changes: 0 additions & 8 deletions CLIENT/state_machine.java

This file was deleted.

11 changes: 7 additions & 4 deletions GUI/MagicCube/src/CubeSimulation.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

import processing.core.PApplet;
import core.CoreAPI;
import core.CubeUserInterface;
import core.MagicCubeData;
import core.UserManager;
import cubeUser.CubeUser;
import cubeUser.IsingUser;
import cubeUser.MillerUser;
import cubeUser.SerialTalk;


Expand Down Expand Up @@ -56,21 +56,24 @@ public void setup() {
//The following must occur in this order:
this.theCube=new MagicCubeData(8);
this.manager=new UserManager(this.theCube);
this.listener=new EventListener(manager, this.theCube);
this.listener=new EventListener(this, manager);
this.P5Gui =new P5GUI(this, listener);


this.manager.adduser(new CubeUser(), "Random");
this.manager.adduser(new SerialTalk(this, "COM3"), "Serial");
this.manager.adduser(new IsingUser(), "Ising");
this.manager.adduser(new MillerUser(1,1,0), "Miller");

this.manager.toggleToUser("Ising");
this.manager.toggleToUser("Random");

this.manager.setTalker("Serial");
this.manager.startTalker();

}

public P5GUI getGui(){
return this.P5Gui;
}
public UserManager getManager(){
return this.manager;
}
Expand Down
2 changes: 2 additions & 0 deletions GUI/MagicCube/src/EventCodes.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ public interface EventCodes {
final static int ISING_START_ID =50;
final static int ISING_SLIDER_ID=51;
final static int UTILS_START_ID=60;
final static int HOME_TAB_EVENT = 70;
final static int DEMO_TOGGLE=80;

//radio refs
final static int CRYSTAL_SC = 1;
Expand Down
45 changes: 35 additions & 10 deletions GUI/MagicCube/src/EventListener.java
Original file line number Diff line number Diff line change
@@ -1,32 +1,53 @@
import controlP5.ControlEvent;
import controlP5.ControlListener;
import core.CoreAPI;
import core.UserManager;
import cubeUser.CubeUser;
import cubeUser.IsingUser;
import cubeUser.MillerUser;


public class EventListener implements ControlListener, EventCodes {
UserManager manager;
CoreAPI cube;
CubeSimulation sim;
int tempi,tempj,tempk;

public EventListener(UserManager manager, CoreAPI mcube){
public EventListener(CubeSimulation sim, UserManager manager){
this.manager=manager;
this.cube=mcube;
this.sim=sim;
}

@Override
public void controlEvent(ControlEvent event) {
System.out.println("[LISTENER]: received event: "+event.toString());
if(event.isTab()){
if(event.tab().id()==HOME_TAB_EVENT){
System.out.println("[LISTENER]: Returning to hometab, Starting randomiser");
manager.adduser(new CubeUser(), "Random");
manager.toggleToUser("Random");
}
}
switch(event.controller().id()){
case DEMO_TOGGLE:
manager.adduser(new CubeUser(), "Random");
manager.toggleToUser("Random");
break;

case MILLER_START_ID:
/*---MillerUser not yet created.---*/
//MillerUser tempUser = new MillerUser(tempi, tempj, tempk);
//tempUser.setCube(this.cube);
//manager.adduser(tempUser, "Miller");
//manager.toggleToUser("Miller");
try{
tempi=Integer.decode(this.sim.getGui().getIBox().getText());
tempj=Integer.decode(this.sim.getGui().getJBox().getText());
tempk=Integer.decode(this.sim.getGui().getKBox().getText());
}catch(NumberFormatException e){
System.out.println("[LISTENER]: Invald format for the miller indices. please enter integers");
break;
}
MillerUser tempUser = new MillerUser(tempi, tempj, tempk);
manager.adduser(tempUser, "Miller");
manager.toggleToUser("Miller");
break;
case IBOX_ID:
tempi=Integer.decode(((controlP5.Textfield)event.controller()).getText());
//System.out.println(tempi);
break;
case JBOX_ID:
tempj=Integer.decode(((controlP5.Textfield)event.controller()).getText());
Expand All @@ -35,7 +56,11 @@ public void controlEvent(ControlEvent event) {
tempk=Integer.decode(((controlP5.Textfield)event.controller()).getText());
break;
case ISING_SLIDER_ID:
((IsingUser)manager.getCurrentUser()).setTemp(event.value());
if(manager.getCurrentuserName().equals("Ising")){
((IsingUser)manager.getCurrentUser()).setTemp(event.value());
}else{
System.out.println("[LISTENER]: Ising sim not running. Ignoring Ising slider event.");
}
break;
case ISING_START_ID:
manager.toggleToUser("Ising");
Expand Down
25 changes: 22 additions & 3 deletions GUI/MagicCube/src/P5GUI.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import controlP5.ControlP5;
import controlP5.Controller;
import controlP5.Radio;
import controlP5.Tab;
import controlP5.Textfield;
import controlP5.Toggle;

Expand All @@ -20,7 +21,9 @@ public class P5GUI implements EventCodes{
//Ising controller
Controller isingGo;
Controller isingSlider;


Controller demoToggle;

public P5GUI(CubeSimulation coreApplet, EventListener listener){

this.parent=coreApplet;
Expand All @@ -35,6 +38,12 @@ public P5GUI(CubeSimulation coreApplet, EventListener listener){
gui.tab("Options");

/*---Default gui----*/
demoToggle=gui.addButton("Run Demo", 10, 50, 250, 50, 20);
demoToggle.setId(DEMO_TOGGLE);
demoToggle.addListener(listener);
demoToggle.setTab("default");


String welcomeText="3D Cube Simulator \n";
welcomeText+="=============================\n";
welcomeText+="The University of Sheffield.\n";
Expand All @@ -53,7 +62,8 @@ public P5GUI(CubeSimulation coreApplet, EventListener listener){
welcomeText+="\n";
welcomeText+="See http://ledcube.googlecode.com for the latest info and releases.\n";
welcomeText+="\n";
gui.addTextarea("Welcome", welcomeText, 10, 40, 170, 300);
gui.addTextarea("Welcome", welcomeText, 10, 40, 170, 300).setTab("default");


/*----Utils contoller----*/
Controller utils;
Expand Down Expand Up @@ -103,7 +113,7 @@ public P5GUI(CubeSimulation coreApplet, EventListener listener){
iBox.addListener(listener);
jBox.addListener(listener);
kBox.addListener(listener);
millGo = gui.addButton("Draw Miller Indices", 10, 10, 160, 100, 20);
millGo = gui.addButton("Draw Miller Planes", 10, 10, 160, 100, 20);
millGo.setId(MILLER_START_ID);
millGo.addListener(listener);
millGo.setTab("Miller Planes");
Expand All @@ -116,4 +126,13 @@ void temperature(float t){
public ControlP5 getGUI(){
return gui;
}
public Textfield getIBox() {
return iBox;
}
public Textfield getJBox() {
return jBox;
}
public Textfield getKBox() {
return kBox;
}
}
5 changes: 5 additions & 0 deletions GUI/MagicCube/src/core/UserManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public class UserManager{
AbstractCubeUser currentUser;
AbstractCubeUser talker;
String talkerName;
String currentUserName;

public UserManager(CoreAPI cube){
this.cube=cube;
Expand Down Expand Up @@ -41,6 +42,7 @@ public void toggleToUser(String name){
System.out.println("[MANAGER]: There is no Current User. Starting new Current user.");
currentUser=users.get(name);
currentUser.setCube(cube);
currentUserName=name;
currentUser.start();
}else{//if the current thread is the thread toggled to
System.out.println("[MANAGER]: Selected user already Current user. Doing nothing.");
Expand Down Expand Up @@ -70,6 +72,9 @@ public Iterator<AbstractCubeUser> getIterator(){
public CubeUserInterface getCurrentUser(){
return currentUser;
}
public String getCurrentuserName(){
return this.currentUserName;
}

@Override
protected void finalize(){
Expand Down
29 changes: 18 additions & 11 deletions GUI/MagicCube/src/cubeUser/SerialTalk.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ public class SerialTalk extends AbstractCubeUser implements OpCodes, ColorCodes{
PApplet parentapp;
int val; // Data received from the serial port
int returned=0;
int del=0;
byte[][] doubleBuffer;
Statistics stats=new Statistics();

Expand All @@ -27,7 +26,7 @@ public SerialTalk(PApplet parentapp, String portName){
@Override
public void run(){
while(!this.isKillme()){
writeCube();
writeCube();
}
}

Expand Down Expand Up @@ -190,13 +189,21 @@ public boolean waitForLayerACK() throws Exception{
}

public void SSend(int toSend){
myPort.write(toSend);
//println(myPort.read());
try {
Thread.sleep(del);
} catch (InterruptedException e) {
e.printStackTrace();
}
}

try{
if(myPort.available()==1){
myPort.write(toSend);
}else{
throw new Exception("port not available");
}
}catch(Exception e2){
System.out.println("[SERIAL]: Exception caught. (Arduino not plugged in?). Aborting Serial write attempt. Commiting suicide.");
this.killme();
try {
join();
} catch (InterruptedException e3) {
System.out.println("[SERIAL]: Thread interrupted while trying to commit suicide.");
System.exit(1);
}
}
}
}

0 comments on commit 73bc2d6

Please sign in to comment.