xxxxxxxxxx
268
let serial;
let portName = "COM3";
let options = { baudrate: 9600 };
let lion;
let w;
let hh;
let wild;
let o;
let pp;
let startvideo = false;
let starttimer = true;
let count = 4;
let khatamv = 0;
let setframecount = 0;
function setup() {
let check = windowHeight % 100;
hh = windowHeight - check
w = 0.4 * hh
pp = windowHeight - check
o = 1.77 * hh
serial = new p5.SerialPort();
serial.on('list',printList);
serial.on('connected', serverConnected);
serial.on('open', portOpen);
serial.on('data', serialEvent);
serial.on('error', serialError);
serial.on('close', portClose);
serial.list();
serial.open(portName, options);
createCanvas(windowWidth, windowHeight);
// pixelDensity(1);
lion = createVideo("anar.mp4")
lion.size(w, hh)
lion.hide();
lion.play()
wild = createVideo("rock.mp4")
wild.size(o, pp)
wild.hide();
wild.play()
}
function draw() {
background(0);
if(startvideo) {
console.log('start1')
burst();
}
if(starttimer) {
if(!setframecount) {
setframecount = frameCount;
}
timer();
}
// lion.oneded(rocket());
}
function finish() {
console.log("KHATAM");
khatamv = 1;
startvideo = false;
serial.write(khatamv);
//rocket();
}
function timer() {
let diff = frameCount - setframecount;
textAlign(CENTER, CENTER);
textSize(200);
fill(235, 177, 19);
text(count, width/2, height/2);
// rect(0,0, width, height)
if(diff%24 == 0) {
count --;
if(diff%2 == 0){
fill(235, 177, 19);
rect(0,0, width, height)
}
}
if(count == 0 || count < 0) {
console.log('START VIDDD')
starttimer = false;
startvideo = true;
}
}
function burst() {
console.log('start2')
lion.onended(finish)
lion.loadPixels();
loadPixels();
for (let x = 0; x < w; x = x + 3) {
for (let y = 0; y < hh; y = y + 3) {
let i = (x + y * w) * 4;
let br = (lion.pixels[i + 0] + lion.pixels[i + 1] + lion.pixels[i + 2]) / 3
if (br > 50) {
noStroke()
if (x < 50 || y < 70) {
fill(random(100, 255), random(30, 190), random(30, 100))
} else if (x > w - 50) {
fill(random(180, 210), random(30, 190), random(30, 100))
} else {
fill(random(170, 225), random(50, 225), 0)
}
rect(x - 1 + ((width / 2) - (w / 2)), y - 1, 4 / (br / 100), 4 / (br / 100))
fill(255, 255, 0)
ellipse(x - 1 + ((width / 2) - (w / 2)), y - 1, (br / 100), (br / 100))
if (br < 60) {
// stroke(random(50, 250), random(100, 200), 0)
stroke(random(150,255), random(150,255),random(150,255))
strokeWeight(1.5)
line(x + ((width / 2) - (w / 2)), y + 35, x + ((width / 2) - (w / 2)) + 4, y + 4 + 35)
line(x + ((width / 2) - (w / 2)) + 4, y + 35, x + ((width / 2) - (w / 2)), y + 4 + 35)
}
}
}
}
}
function rocket() {
console.log('start3')
wild.onended(finish)
wild.loadPixels();
loadPixels();
for (let x = 0; x < o; x = x + 3) {
for (let y = 0; y < pp; y = y + 3) {
console.log('loop')
let i = (x + y * o) * 4;
let br = (wild.pixels[i + 0] + wild.pixels[i + 1] + wild.pixels[i + 2]) / 3
if (br > 50) {
noStroke()
if (x < 50 || y < 70) {
fill(random(100, 255), random(30, 190), random(30, 100))
} else if (x > o - 50) {
fill(random(180, 210), random(30, 190), random(30, 100))
} else {
fill(random(80, 225), random(80, 225), random(80, 130))
}
rect(x - 1 + ((width / 2) - (o / 2)), y - 1, 4 / (br / 100), 4 / (br / 100))
fill(255, 255, 0)
ellipse(x - 1 + ((width / 2) - (o / 2)), y - 1, (br / 100), (br / 100))
if (br < 60) {
stroke(random(50, 250), random(50, 200), 0)
strokeWeight(1.5)
line(x + ((width / 2) - (o / 2)), y + 35, x + ((width / 2) - (o / 2)) + 4, y + 4 + 35)
line(x + ((width / 2) - (o / 2)) + 4, y + 35, x + ((width / 2) - (o / 2)), y + 4 + 35)
}
}
}
}
}
function serialEvent() {
// read a string from the serial port
// until you get carriage return and newline:
var inString = serial.readStringUntil('\r\n');
//console.log('inString',inString)
//check to see that there's actually a string there:
if (inString.length > 0 ) {
var sensors = split(inString, ','); // split the string on the commas
console.log('receiving sensor data',sensors);
if(sensors && sensors[0] < 435 && !startvideo && !starttimer) {
starttimer = true;
}
if (sensors.length > 2) { // if there are three elements
//locV = map(sensors[0], -0.98,0.98 , -10, 10);
//locH = map(sensors[1], -0.98, 0.98, -10, 10);
//airblow = sensors[2];
}
}
}
function printList(portList) {
for (var i = 0; i < portList.length; i++) {
// Display the list the console:
console.log(i + portList[i]);
}
}
function serverConnected() {
console.log('connected to server.');
}
function portOpen() {
console.log('the serial port opened.')
}
function serialError(err) {
console.log('Something went wrong with the serial port. ' + err);
}
function portClose() {
console.log('The serial port closed.');
}