2016年10月5日水曜日

2HD with Arduno

最悪なサンプル、その2。ArdunoとProcessingの連携。やってみて、学生さんに全部やってもらうのはしんどそう、と分かったのは収穫ではあるが...。
import processing.serial.*;
Serial port;
PImage img;
float Y=0;
void setup() {
  size(640, 640, P3D);
  img=loadImage("Kao.jpg");
  port = new Serial(this,"COM3",9600);
  frameRate(30);
  stroke(0, 0, 0, 100);
  //port.bufferUntil('\n'); 
  port.write('a');
}

void draw() {
 
  lights();
  background(0);
  float cameraY = height/2.0;
  float fov = mouseX/float(width) * PI/6;
  float cameraZ = cameraY / tan(fov / 2.0);
  float aspect = float(width)/float(height);
  
  perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
    
  translate(width/2, height/2, 0);
  rotateX((1.9- Y/9.6) * PI/2);
  rotateZ(PI/3 + mouseX/float(height) * PI);
  beginShape();

  box(40,40,50);
  translate(10, 0, -50);
  box(15,15,40);
  translate(-20, 0, 0);
  box(15,15,40);
  translate(-20, 0, 50);
  box(15,15,40);
  translate(60, 0, 0);
  box(15,15,40);
  translate(-30, 0, 40);
  box(25,25,25);
  translate(0, -12.5, 0);
  box(25,1,25);

  texture(img);
  vertex(0,100,0,img.width,img.height);
  endShape();
  translate(0, -10, 20);

  rotateX(radians(-90));
  rotateY(radians(180));
  
  if(Y<4)
  {
  text("H!",0,0,0);
  }
  else if(mousePressed) 
  {

   text("I Love You!",0,0,0);
  }

  if(port.available()>1){
    String ms=port.readStringUntil('\n');
    if(ms!=null){
      ms=trim(ms);
      float Y0=float(ms);
      Y=int(Y0*10)/10;
    }
  port.write("a");  
  }
  print(Y);
  print("\n");
}

void mousePressed(){
  port.write('a');
}
Arudino側。 こちら https://github.com/ArduSat/ArdusatSDK を利用。
#include  Arduino.h
#include  Wire.h 
#include  ArdusatSDK.h

ArdusatSerial serialConnection(SERIAL_MODE_HARDWARE_AND_SOFTWARE, 8, 9);

Acceleration accel;

void setup(void)
{
  Serial.begin(9600);
  serialConnection.println("a");
}

void loop(void)
{
  int a,c;
  if(Serial.available()>0){
  
  accel.readToJSON("accelerometer");
  c=Serial.read();
 Serial.println(accel.z);
 }
}

2016年10月3日月曜日

2HD

最悪なサンプル、その1。 学生さんとProcessingをやっていると、つい楽しくカッとなって作ってしまった...。 サンプルのPerspectiveほぼそのまま。
PImage img;
void setup() {
  size(640, 640, P3D);
  img=loadImage("Kao.jpg");
  stroke(0, 0, 0, 100);
}

void draw() {
  lights();
  background(0);
  float cameraY = height/2.0;
  float fov = mouseX/float(width) * PI/6;
  float cameraZ = cameraY / tan(fov / 2.0);
  float aspect = float(width)/float(height);
  
  perspective(fov, aspect, cameraZ/10.0, cameraZ*10.0);
  
  translate(width/2, height/2, 0);
  rotateX(PI/3 + mouseY/float(height) * PI);
  rotateZ(PI/3 + mouseX/float(height) * PI);
  beginShape();

  box(40,40,50);
  translate(10, 0, -50);
  box(15,15,40);
  translate(-20, 0, 0);
  box(15,15,40);
  translate(-20, 0, 50);
  box(15,15,40);
  translate(60, 0, 0);
  box(15,15,40);
  translate(-30, 0, 40);
  box(25,25,25);
  translate(0, -12.5, 0);
  box(25,1,25);

  texture(img);
  vertex(0,100,0,img.width,img.height);
  endShape();
  translate(0, -10, 20);

  rotateX(radians(-90));
  rotateY(radians(180));
  if(PI/3 + mouseY/float(height) * PI>2.3){
    text("H!",0,0,0);
  }
  else if(mousePressed) {
    text("Love :-)",0,0,0);
  }
}