Statistics
| Branch: | Revision:

adafruit_bno055 / examples / bunny / processing / cuberotate / cuberotate.pde @ 40f91f6f

History | View | Annotate | Download (4.27 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));
86
  rotateX(radians(pitch));
87
  rotateY(radians(yaw));
88

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

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

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

    
146
// UI event handlers
147

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

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

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