// Peggy 2.0 simulator in Processing, version 1.0 // Aaron Bell, August 2008. // - If you're not familiar with Peggy, see http://www.youtube.com/watch?v=r5oZLjBvTAM // - This sketch is a way to work on Peggy demos without buying the board : ) // - In good MVC fashion the data and display are kept separate, so you can plug in your own display // - Display is basic but resizes to any x:y cleanly // - Next steps: (1) faster (2) more fancy demos with the LEDs (3) more realistic LED rendering with 'bloom' // key objects PeggyData data = new PeggyData(); PeggyDisplay display = new PeggyDisplay(data); // variables for a demo with sine wave bars int BARS = data.COLS; int MID_ROW = data.ROWS / 2; float[] bars = new float[BARS]; float[] bar_v = new float[BARS]; // main setup method void setup() { frameRate(20); display.setup(); // standard way to init the display setupBars(); // for this demo } // main loop void draw() { drawBars(); // update the PeggyData using the demo logic display.draw(); // standard way to draw the display } // initialise the demo bars void setupBars() { for (int i=0; iright == slow->fast } } // draw the demo bars void drawBars() { data.setAll(data.LED_MIN_BRIGHTNESS); // clear. Could do quicker by overwriting the 'off' LEDs below // for each bar... for (int col=0; col LED_MAX_BRIGHTNESS) { println("setLed brightness out of bounds: " + value); exit(); } if (row >= ROWS || row < 0) { println("setLed row out of bounds: " + row); exit(); } if (col >= COLS || col < 0) { println("setLed col out of bounds: " + col); exit(); } data[(COLS * row) + col] = value; } // set a row void setRow(int row, int value) { if (value < LED_MIN_BRIGHTNESS || value > LED_MAX_BRIGHTNESS) { println("setRow brightness out of bounds: " + value); exit(); } if (row >= ROWS || row < 0) { println("setRow row out of bounds: " + row); exit(); } for (int col=0; col LED_MAX_BRIGHTNESS) { println("setColumn brightness out of bounds: " + value); exit(); } if (col >= COLS || col < 0) { println("setColumn col out of bounds: " + col); exit(); } for (int row=0; row LED_MAX_BRIGHTNESS) { println("setAll brightness out of bounds: " + value); exit(); } for (int led=0; led= ROWS || row < 0) { println("getLed row out of bounds: " + row); exit(); } if (col >= COLS || col < 0) { println("getLed col out of bounds: " + col); exit(); } return data[(COLS * row) + col]; } } // A basic rendering of a Peggy board. Can be set to any size and shape. // This is the 'insane bounds checking edition' for ease of debugging class PeggyDisplay { int BOARD_COLOR = color(10, 30, 10); int LED_DIAMETER = 15; int LED_COLOR = color(200, 200, 255); int LED_SPACING = 20; // assumes square layout int MARGIN = 4; int WIDTH; // will be based on data size int HEIGHT; // will be based on data size PeggyData data; PeggyDisplay(PeggyData d) { data = d; // calculate canvas based on parameters WIDTH = MARGIN + (data.COLS * LED_SPACING) + (LED_DIAMETER - LED_SPACING) + MARGIN; HEIGHT = MARGIN + (data.ROWS * LED_SPACING) + (LED_DIAMETER - LED_SPACING) + MARGIN; } void setup() { size(WIDTH, HEIGHT); println("canvas is " + WIDTH + " x " + HEIGHT); ellipseMode(CORNER); colorMode(RGB,255,255,255,100); smooth(); noStroke(); } void draw() { background(BOARD_COLOR); for (int row=0; row= data.ROWS || row < 0) { println("drawLed row out of bounds: " + row); exit(); } if (col >= data.COLS || col < 0) { println("drawLed col out of bounds: " + col); exit(); } fill(LED_COLOR, value); ellipse(MARGIN + (col * LED_SPACING), MARGIN + (row * LED_SPACING), LED_DIAMETER, LED_DIAMETER); } }