Skip to content

Instantly share code, notes, and snippets.

@sdbakker
Last active August 29, 2015 14:05
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sdbakker/51bfe5e9677f4aae380c to your computer and use it in GitHub Desktop.
Save sdbakker/51bfe5e9677f4aae380c to your computer and use it in GitHub Desktop.
WdKA RFID Workshop Mifare RFID image trigger gist
/*
* @title: WdKA RFID Workshop Mifare RFID image trigger gist
* @author: Simon de Bakker <simon@simbits.nl>
*/
// import libraries for serial communication and video
import processing.serial.*;
import processing.video.*;
// create a serial port object
Serial myPort;
// an array with known UIDs from the RFID cards
String[] UIDs = {
"482818799",
"419918016215050128",
"193763787",
"196111177137"
};
final int NUM_IMAGES = 4; // the amount of images loaded
PImage[] images = new PImage[NUM_IMAGES]; // an array with image objects
/* an array with the paths to the images to be loaded
* this should either be an absolute path or the images should
* be in the data folder of the sketch. To copy images to the data folder
* you can drag and drop images onto here */
String[] imagePaths = {
"Jon.jpg",
"Simon.jpg"
"Tim.jpg",
"Roel.jpg",
};
// the current image loaded / displayed
PImage currentImage = null;
/* this function is called only once at the start of the program */
void setup() {
// the size of our window
size(640, 480);
// List all the available serial ports in the output pane.
// You will need to choose the port that the Wiring board is
// connected to from this list. The first port in the list is
// port #0 and the third port in the list is port #2.
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
/* preload all the images into the image array
* this for loop loops over all the paths in the imagePaths array and loads
* it with loadImage()
*/
for (int i=0; i<imagePaths.length; i++) {
if (i >= images.length)
break;
images[i] = loadImage(imagePaths[i]); // Load the image into the program
}
// use the first image as the current image
currentImage = images[0];
}
/* this function is called over and over again from the beginning until the program is stopped */
void draw() {
// clears the window and sets a black background. Change 0 to some other values to change the background color.
background(0);
// Displays the image at its actual size at point (0,0)
//image(currentImage, 0, 0);
// Displays the image at point (0, height/2) at half of its size
image(currentImage, 0, 0, 640, 480);
}
/* this function is called if there is activity on the serial port */
void serialEvent(Serial myPort) {
// read the serial buffer:
String uid = myPort.readStringUntil('\n');
// if we read something
if (uid != null) {
/* make sure there are no whitespaces around the id */
uid = trim(uid);
println("'" + uid + "'");
/* compare the received uid to the known uids in our list
* if it is known and there is a corresponding image loaded
* set that image as the current image
*/
for (int i=0; i<UIDs.length; i++) {
if (uid.equals(UIDs[i])) {
if (i < images.length) {
currentImage = images[i];
}
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment