Created
October 8, 2014 22:25
-
-
Save Craigson/bc95b774e293e5c18b8f to your computer and use it in GitHub Desktop.
Instagram multi button
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import http.requests.*; | |
import processing.serial.*; //import serial library | |
Serial myPort; //declare a serial port | |
int buttonValue; | |
int previousButtonValue; | |
boolean receivingData = false; | |
int sensorvalue=0; | |
//create a boolean variable to establish whether or not the arduino | |
//has attempted to communicate | |
boolean firstContact = false; | |
//USER_TOKEN SAVE | |
String usertoken = "243036695.399933e.d49b4ab95acd4b94b5f7cc9adfbfdadb"; | |
//IMAGE COUNT | |
int maxcount = 30; | |
//IMAGE SIZE | |
int imagesize = 250; | |
//MARGINE LENGTH | |
int imagemargine = 15; | |
//MOUSE POSITION WHEN MOUSED PRESSED | |
float px=0.0; | |
float py=0.0; | |
//MOUSE POSITION WHEN MOUSED RELEASED & DISTANCE & PRESSED CHECK & PAGE CHECK | |
float rx=0.0; | |
float ry=0.0; | |
float dx=0.0; | |
boolean pressed = false; | |
int xcheck = 0; | |
float speed = 0; | |
float maxspeed = 5; | |
float accel = 0.2; | |
float location; | |
//INPUT CHECK | |
int mousecheck; | |
//Heart | |
PImage heart; | |
float time = 0.0; | |
float inc = 0.2; | |
float r = 0; | |
//INSTAGRAM DATA SAVE ARRAY | |
ArrayList<Instainfo> instas; | |
boolean imflag = false; | |
public void setup() | |
{ | |
size(3*imagesize + 4*imagemargine, 3*imagesize + 4*imagemargine); | |
smooth(); | |
String portName = "/dev/tty.usbmodem1411"; //create string to assign port | |
myPort = new Serial(this, portName, 9600); //initialise serial port | |
myPort.bufferUntil('\n'); //read incoming bytes to buffer until linefeed is received | |
instas = new ArrayList<Instainfo>(); | |
//IMAGE GET | |
getImage(); | |
//IMAGE LOCATIOM SET | |
setLocation(); | |
setImage(); | |
} | |
void draw() { | |
background(0); | |
for (Instainfo s : instas) { | |
s.move(speed); | |
s.display(); | |
} | |
tilePressed(); | |
if(imflag == true) { | |
displaySelected(); | |
} | |
distCheck(); | |
speedCheck(); | |
} | |
//HTTP REQUEST LIBRARY REF | |
void getImage() { | |
GetRequest get = new GetRequest("https://api.instagram.com/v1/users/self/feed.json?access_token="+usertoken+"&count="+maxcount); | |
//GetRequest get = new GetRequest("GetRequest get = new GetRequest("https://api.instagram.com/v1/users/self/feed.json?access_token=243036695.399933e.d49b4ab95acd4b94b5f7cc9adfbfdadb&count=30);"); | |
get.send(); // program will wait untill the request is completed | |
JSONObject response = parseJSONObject(get.getContent()); | |
JSONArray boxes = response.getJSONArray("data"); | |
for (int i=0; i<boxes.size (); i++) { | |
JSONObject box = boxes.getJSONObject(i); | |
println("Box 1: " + box); | |
String id = box.getString("id"); | |
println("Media_ID:" + id); | |
boolean liked = box.getBoolean("user_has_liked"); | |
JSONObject user = box.getJSONObject("user"); | |
String username = user.getString("full_name"); | |
//println("User:" + username); | |
JSONObject images = box.getJSONObject("images"); | |
JSONObject stimages = images.getJSONObject("standard_resolution"); | |
//println("Image URL" + stimages.getString("url")); | |
PImage userphoto = loadImage(stimages.getString("url"), "png"); | |
instas.add(new Instainfo(i, id, liked, username, userphoto, imagesize)); | |
} | |
} | |
void setLocation() { | |
int leng = 0; | |
int page = 0; | |
//println("Inastas Class Size:" + leng); | |
while (page < maxcount/9 + 1) { | |
for (int i = 0; i < 3; i++) { | |
for (int j = 0; j < 3; j++) { | |
if (leng < instas.size()) { | |
Instainfo insta = instas.get(leng); | |
insta.location.set(new PVector(width*page + (j+1)*imagemargine + j*imagesize + imagesize/2, imagemargine + i*(imagesize+imagemargine) + imagesize/2)); | |
//println("id:"+insta.count+"leng"+leng); | |
leng++; | |
} | |
} | |
} | |
page ++; | |
} | |
} | |
void setImage() { | |
heart = loadImage("heart.png"); | |
} | |
void displaySelected() { | |
r = noise(time)*width/9; | |
time += inc; | |
Instainfo insta = instas.get(mousecheck + (xcheck*9)); | |
println("Media_ID:" + insta.id); | |
imageMode(CENTER); | |
tint(255); | |
image(insta.image, width/2, height/2, width-imagemargine, height-imagemargine); | |
if (insta.liked == false) { | |
tint(255, 170); | |
image(heart, width/12*11, height/12*11, r, r); | |
} else { | |
tint(255, 51, 51, 170); | |
image(heart, width/12*11, height/12*11, r*1.5, r*1.5); | |
} | |
} | |
void keyPressed() { | |
if (imflag == true) { | |
Instainfo insta = instas.get(mousecheck + (xcheck*9)); | |
insta.liked = true; | |
//GetRequest setliked = new GetRequest("https://api.instagram.com/v1/media/" +insta.id + "/likes.json?access_token="+usertoken); | |
PostRequest post = new PostRequest("https://api.instagram.com/v1/media/" +insta.id + "/likes?access_token="+usertoken); | |
post.send(); | |
} | |
} | |
void tilePressed(){ | |
if (receivingData){ | |
inputCheck(); | |
if(imflag == true) { | |
println("disappear"); | |
imflag = false; | |
} else { | |
println("appear"); | |
imflag = true; | |
} | |
} | |
} | |
// void mouseClicked() { | |
// inputCheck(); | |
// println("mouse check:"+mousecheck); | |
// if (imflag == true) { | |
// println("disappear"); | |
// imflag = false; | |
// } else { | |
// println("appear"); | |
// imflag = true; | |
// } | |
// } | |
void mousePressed() { | |
if (pressed==false) { | |
println("mouse pressed"); | |
px = mouseX; | |
pressed = true; | |
} | |
} | |
void mouseReleased() { | |
if (pressed==true) { | |
rx = mouseX; | |
dx = px-rx; | |
println("mouse released, dx:" + dx); | |
if (dx > 200) { | |
goRight(); | |
} else if (dx < -200) { | |
goLeft(); | |
} | |
pressed = false; | |
} | |
} | |
void distCheck() { | |
if (!mousePressed) { | |
for ( int i = instas.size ()-1; i >= 0; i--) { | |
Instainfo insta = instas.get(i); | |
if (dist(insta.location.x, insta.location.y, mouseX, mouseY) < imagesize/2) { | |
insta.size += 1; | |
insta.size = constrain(insta.size, imagesize, imagesize+imagemargine); | |
} else { | |
insta.size -= 1; | |
insta.size = constrain(insta.size, imagesize, imagesize+imagemargine); | |
} | |
} | |
} | |
} | |
void goRight() { | |
xcheck++; | |
location -= width; | |
if ( xcheck > 3 ) { | |
xcheck = 3; | |
location = 3*width; | |
} | |
Instainfo insta = instas.get(0); | |
if ( insta.location.x <= (imagemargine + imagesize/2 + location)) { | |
speed = 0; | |
} else { | |
speed = -1 * maxspeed; | |
//speed -= accel; | |
} | |
println("speed:" + speed); | |
} | |
void goLeft() { | |
xcheck--; | |
location += width; | |
if ( xcheck < 0 ) { | |
xcheck = 0; | |
location = 0; | |
} | |
Instainfo insta = instas.get(0); | |
if ( insta.location.x >= (imagemargine + imagesize/2 + location)) { | |
speed = 0; | |
} else { | |
speed = maxspeed; | |
//speed += accel; | |
} | |
println("speed:" + speed); | |
} | |
void speedCheck() { | |
Instainfo insta = instas.get(0); | |
if (speed < 0) { | |
if ( insta.location.x <= (imagemargine + imagesize/2 + location)) { | |
speed = 0; | |
} | |
} else if (speed > 0) { | |
if ( insta.location.x >= (imagemargine + imagesize/2 + location)) { | |
speed = 0; | |
} | |
} | |
//println("speed:" + speed); | |
} | |
void serialEvent(Serial myPort) { | |
//read the serial buffer | |
String incomingData = myPort.readStringUntil('\n'); | |
if (incomingData != null) { | |
incomingData = trim(incomingData); //this removes whitespace from the incoming string | |
//if no data has been received from Arduino, continue to listen for it | |
if (firstContact == false) { | |
if (incomingData.equals("begin")) { | |
myPort.clear(); //clear the serail buffer | |
firstContact = true; //confirm that data has been received | |
myPort.write('A'); // request more information | |
} | |
} else { //if first contact has previously been established | |
// split the string at the commas | |
// and convert the sections into integers: | |
int sensors[] = int (split (incomingData, ',') ); | |
if (sensorvalue != sensors[0]){ | |
receivingData = true; | |
sensorvalue = sensors[0]; | |
buttonValue = sensors[0]; | |
} else { | |
receivingData = false; | |
} | |
myPort.write('A'); | |
// print out the values you got: | |
for (int sensorNum = 0; sensorNum < sensors.length; sensorNum++) { | |
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t"); | |
} | |
// | |
} | |
println(); | |
} | |
} | |
//To match input info with Arduino | |
void inputCheck() { | |
println("mouseX:"+mouseX + "mouseY:" + mouseY); | |
if (buttonValue == 1){ | |
mousecheck = 0; | |
} else if (buttonValue == 4) { | |
mousecheck = 3; | |
} else if (buttonValue== 7){ | |
mousecheck = 6; | |
} else if (buttonValue== 2) { | |
mousecheck = 1; | |
} else if (buttonValue == 5) { | |
mousecheck = 4; | |
} else if (buttonValue == 8) { | |
mousecheck = 7; | |
} else if (buttonValue == 3 ) { | |
mousecheck = 2; | |
} else if (buttonValue == 6) { | |
mousecheck = 5; | |
} else if (buttonValue == 9){ | |
mousecheck = 8; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment