Statistics
| Branch: | Revision:

adafruit_bno055 / examples / bunny / processing / cuberotate / cuberotate.pde @ 67f3cff5

History | View | Annotate | Download (4.253 KB)

1
import processing.serial.*;
2
import java.awt.datatransfer.*;
3
import java.awt.Toolkit;
4
import processing.opengl.*;
5
import saito.objloader.*;
6
import g4p_controls.*;
7

    
8
float roll  = 0.0F;
9
float pitch = 0.0F;
10
float yaw   = 0.0F;
11
float temp  = 0.0F;
12
float alt   = 0.0F;
13

    
14
OBJModel model;
15

    
16
// Serial port state.
17
Serial       port;
18
String       buffer = "";
19
final String serialConfigFile = "serialconfig.txt";
20
boolean      printSerial = false;
21

    
22
// UI controls.
23
GPanel    configPanel;
24
GDropList serialList;
25
GLabel    serialLabel;
26
GCheckbox printSerialCheckbox;
27

    
28
void setup()
29
{
30
  size(400, 550, OPENGL);
31
  frameRate(30);
32
  model = new OBJModel(this);
33
  model.load("bunny.obj");
34
  model.scale(20);
35
  
36
  // Serial port setup.
37
  // Grab list of serial ports and choose one that was persisted earlier or default to the first port.
38
  int selectedPort = 0;
39
  String[] availablePorts = Serial.list();
40
  if (availablePorts == null) {
41
    println("ERROR: No serial ports available!");
42
    exit();
43
  }
44
  String[] serialConfig = loadStrings(serialConfigFile);
45
  if (serialConfig != null && serialConfig.length > 0) {
46
    String savedPort = serialConfig[0];
47
    // Check if saved port is in available ports.
48
    for (int i = 0; i < availablePorts.length; ++i) {
49
      if (availablePorts[i].equals(savedPort)) {
50
        selectedPort = i;
51
      } 
52
    }
53
  }
54
  // Build serial config UI.
55
  configPanel = new GPanel(this, 10, 10, width-20, 90, "Configuration (click to hide/show)");
56
  serialLabel = new GLabel(this,  0, 20, 80, 25, "Serial port:");
57
  configPanel.addControl(serialLabel);
58
  serialList = new GDropList(this, 90, 20, 200, 200, 6);
59
  serialList.setItems(availablePorts, selectedPort);
60
  configPanel.addControl(serialList);
61
  printSerialCheckbox = new GCheckbox(this, 5, 50, 200, 20, "Print serial data");
62
  printSerialCheckbox.setSelected(printSerial);
63
  configPanel.addControl(printSerialCheckbox);
64
  // Set serial port.
65
  setSerialPort(serialList.getSelectedText());
66
}
67
 
68
void draw()
69
{
70
  background(0,0,0);
71

    
72
  // Set a new co-ordinate space
73
  pushMatrix();
74

    
75
  // Simple 3 point lighting for dramatic effect.
76
  // Slightly red light in upper right, slightly blue light in upper left, and white light from behind.
77
  pointLight(255, 200, 200,  400, 400,  500);
78
  pointLight(200, 200, 255, -400, 400,  500);
79
  pointLight(255, 255, 255,    0,   0, -500);
80
  
81
  // Displace objects from 0,0
82
  translate(200, 300, 0);
83
  
84
  // Rotate shapes around the X/Y/Z axis (values in radians, 0..Pi*2)
85
  rotateZ(radians(roll * -1.0F));
86
  rotateX(radians(pitch));
87

    
88
  pushMatrix();
89
  noStroke();
90
  model.draw();
91
  popMatrix();
92
  popMatrix();
93
  //print("draw");
94
}
95

    
96
void serialEvent(Serial p) 
97
{
98
  String incoming = p.readString();
99
  if (printSerial) {
100
    println(incoming);
101
  }
102
  
103
  if ((incoming.length() > 8))
104
  {
105
    String[] list = split(incoming, " ");
106
    if ( (list.length > 0) && (list[0].equals("Orientation:")) ) 
107
    {
108
      roll  = float(list[3]); // Roll = Z
109
      pitch = float(list[2]); // Pitch = Y 
110
      yaw   = float(list[1]); // Yaw/Heading = X
111
      buffer = incoming;
112
    }
113
    if ( (list.length > 0) && (list[0].equals("Alt:")) ) 
114
    {
115
      alt  = float(list[1]);
116
      buffer = incoming;
117
    }
118
    if ( (list.length > 0) && (list[0].equals("Temp:")) ) 
119
    {
120
      temp  = float(list[1]);
121
      buffer = incoming;
122
    }
123
  }
124
}
125

    
126
// Set serial port to desired value.
127
void setSerialPort(String portName) {
128
  // Close the port if it's currently open.
129
  if (port != null) {
130
    port.stop();
131
  }
132
  try {
133
    // Open port.
134
    port = new Serial(this, portName, 115200);
135
    port.bufferUntil('\n');
136
    // Persist port in configuration.
137
    saveStrings(serialConfigFile, new String[] { portName });
138
  }
139
  catch (RuntimeException ex) {
140
    // Swallow error if port can't be opened, keep port closed.
141
    port = null; 
142
  }
143
}
144

    
145
// UI event handlers
146

    
147
void handlePanelEvents(GPanel panel, GEvent event) {
148
  // Panel events, do nothing.
149
}
150

    
151
void handleDropListEvents(GDropList list, GEvent event) { 
152
  // Drop list events, check if new serial port is selected.
153
  if (list == serialList) {
154
    setSerialPort(serialList.getSelectedText()); 
155
  }
156
}
157

    
158
void handleToggleControlEvents(GToggleControl checkbox, GEvent event) { 
159
  // Checkbox toggle events, check if print events is toggled.
160
  if (checkbox == printSerialCheckbox) {
161
    printSerial = printSerialCheckbox.isSelected(); 
162
  }
163
}