Making Things Interactive

April 3, 2008

digital book – text display

Filed under: Assignments, Final Project, Nadeem Haidary — nhaidary @ 12:04 am

I have been doing research on electronic ink displays and exploring gestures for generating electricity. I have been working on the Processing end of things and figured out how to load, format and display a whole lot of text. For now, I’m using the < and > keys to navigate, but eventually it will be done tangibly. Here is a screenshot:

page example

And the code:

 /*
 *   Digital Book Reader
 *   State Machine
 *   Spring 2008
 *
 *   Nadeem Haidary
 */

//LIBRARIES
import processing.serial.*;

//GLOBAL VARIABLES
PFont title;                  // title font
PFont font;                   // text font
Serial port;                  // Create object from Serial class
int val;                      // Data received from the serial port
int x = 0;                    // stores nothing important right now
int page = 0;                 // page number
int spacer = 0;               // tracks line number within loop
int lineStart = -46;          // line that page starts at
int lineEnd = 0;              // line that page ends at
int linesPerPage = 46;        // the total amount of lines dispalyed up until a certain page
boolean saveImage = false;    // for saving a frame

void setup() {
  size (450, 600);
  frameRate(10);
  /*
  println(Serial.list());                                   // Set up the serial communication
   port = new Serial(this, Serial.list()[1], 9600);         // Open the port that the board is connected to (in this case COM4 = [1])
   // and use the same speed (9600 bps)
   */
  font = loadFont("BookAntiqua-10.vlw");
  title = loadFont("BookAntiqua-BoldItalic-48.vlw");
  String lines[] = loadStrings("ulysses_text only.txt");    // Loads the book from a text file and seperates each line into a String in an array
  println("there are " + lines.length + " lines");          // prints the number of lines
}

void draw() {
  background (250);                                         // sets the background color
  fill (0);                                                 // sets the text color
  smooth();                                                 // anti-aliasing
  String lines[] = loadStrings("ulysses_text only.txt");    // for some reason, the only way I can get this to work is by reloading the strings every frame

  // NAVIGATION
  if (keyPressed == true){
    if (key == '.') {                                       // press the > key to advance a page
      delay(40);
      page ++;
      lineStart += linesPerPage;
      lineEnd += linesPerPage;
    }
    if (key == ',' && page > 0) {                           // press the < key to go back a page
      delay(40);
      page--;
      lineStart -= linesPerPage;
      lineEnd -= linesPerPage;
    }
    if (key == 's') {                                       // press the s key to save a frame
      saveImage = true;
    }
    //println(lineStart + "   " + lineEnd);
  }

  if (page == 0) {                                          // display title page
    textFont(title, 48);                                    // sets the type to be used
    text("Ulysses", 250, 522);
    textFont(title, 18);
    text("James Joyce", 255, 480);
  }
  else {                                                    // display text pages
    textFont(font, 10);
    spacer = 0;
    for (int i=lineStart; i < lineEnd; i++) {               // display all the lines for this page
      text(lines[i], 20, 30 + (spacer*12));                 // starting corner and spaces between lines
      spacer++;                                             // count the lines
    }
    textFont(title, 10);
    text(page, 400, 20);                                    // display page number
  }

  if (saveImage == true) {                                  // save a frame
    save("page.tif");
  }

  /*
  if (0 < port.available()) {                       // If data is available,
   val = port.read();                              // read it and store it in val
   }

   if (val == 1) {
   x++;
   }
   */
}

Blog at WordPress.com.