January 26, 2014

//counter.pde by Phillip Stearns
//an exercise in creating composite objects in Processing
//logic circuit simulation

Counter counter;

void setup() {
  background(0);
  size(5, 5);
  counter = new Counter(height);
}

void draw() {
  saveFrame("counter_###.png");
  counter.clock();
  for (int x = 0 ; x < width ; x++) {    
    for (int y = 0 ; y < counter.length() ; y++) {
      counter.display(y, x, y);
    }
  }
  if (frameCount == pow(2,height)) {
    exit();
  }
}

/*********begin class definitions********/

//Counter is a class of objects that have n number of stages.
//The counter operates like a binary ripple counter similar to the CD4040.
//Creation arguments: int size
//Methods:
//clock() - increments the counter
//clock(boolean clockInput) - allows an external clock to increment the counter. triggered on "falling edge" transistion from true to false.
//reset() - sets all stages to false
//display() - prints the counter's internal state from Least significant bit to Most significant bit from left to right (reverse binary)
//display(int Q, int x, int y) - changes the pixel at the coordinates x, y to match the output state of stage Q
//length() returns int - number of stages in the current counter
//getState(int Q) - returns boolean - the value of stage Q


class Counter {
  FlipFlop[] register;

  Counter(int size) {
    register = new FlipFlop[size];
    for (int i = 0 ; i < register.length ; i++) {
      register[i] = new FlipFlop();
    }
  }

  void reset() {
    for (int i = 0 ; i < register.length ; i++) {
      register[i].reset();
    }
  }

  int length() {
    return register.length;
  }

  void clock() {
    for (int i = 0; i < register.length ; i++) {
      if (i == 0) {
        if (register[0].getState() == true) {
          register[0].setState(false);
        } 
        else if (register[0].getState() == false) {
          register[0].setState(true);
        }
      } 
      else if (i > 0) {
        register[i].flip(register[i-1].getState());
      }
    }
  }

  void clock(boolean clockInput) {
    for (int i = 0; i < register.length ; i++) {
      if (i == 0) {
        register[0].flip(clockInput);
      } 
      else if (i > 0) {
        register[i].flip(register[i-1].getState());
      }
    }
  }

  boolean getState(int Q) {
    return register[Q].getState();
  }

  void display() {
    for (int i = 0 ; i < register.length ; i++) {
      print(int(register[i].getState()));
    }
    println();
  }

  void display(int Q, int x, int y) {
    loadPixels();
    pixels[y*width + x]=color(int(register[Q].getState())*255);
    updatePixels();
  }

  //FlipFlop is a clas of objects that behave like flipflop logic circuits.
  //Creation arguments: boolean init - sets initial state (optional)
  //Methods:
  //flip(boolean clock) - causes the flipflop to change states. triggered by "falling edge", true to false, logic transistion.
  //setState(boolean set) - sets register to set
  //reset() - resets register state to false
  //getState() - returns boolean - the value of the state
  
  class FlipFlop {  
    boolean state;
    boolean currentClock;
    boolean previousClock;

    FlipFlop() {
      state=false;
    }

    FlipFlop(boolean init) {
      state=init;
    }

    void flip(boolean clock) {
      currentClock = clock;
      if (currentClock == false && previousClock == true) {
        if (state == true) {
          state = false;
        } 
        else if (state == false) {
          state = true;
        }
      }
      previousClock = currentClock;
    }

    void setState(boolean set) {
      state = set;
    }

    void reset() {
      state = false;
      currentClock = false;
      previousClock = false;
    }

    boolean getState() {
      return state;
    }
  }
}

November 20, 2013
32 bit LFSR
Seed 73328 w/ feedback taps at position 3&#160;5 19

32 bit LFSR

Seed 73328 w/ feedback taps at position 3 5 19

December 29, 2012
364 of 366
Processing + JPGs
Randomly varying byte 0x37 and byte 0xCD.  Animating the usable frames.

364 of 366

Processing + JPGs

Randomly varying byte 0x37 and byte 0xCD.  Animating the usable frames.

May 3, 2012
124 of 366
Fluctuations 
Created using a prepared/modified Kodak DC215.
Epilepsy Warning

124 of 366

Fluctuations

Created using a prepared/modified Kodak DC215.

Epilepsy Warning