[CGSG] SVN Commit r431 - in projects/FPGAMatrix/arduino: . bounce fractal keftales sinecurve
kintel at metalab.at
kintel at metalab.at
Sat Apr 26 02:02:55 CEST 2008
Author: kintel
Date: 2008-04-26 02:02:55 +0200 (Sat, 26 Apr 2008)
New Revision: 431
Added:
projects/FPGAMatrix/arduino/bounce/
projects/FPGAMatrix/arduino/bounce/bounce.pde
projects/FPGAMatrix/arduino/fractal/
projects/FPGAMatrix/arduino/fractal/fractal.pde
projects/FPGAMatrix/arduino/keftales/
projects/FPGAMatrix/arduino/keftales/keftales.pde
projects/FPGAMatrix/arduino/sinecurve/
projects/FPGAMatrix/arduino/sinecurve/sinecurve.pde
Log:
First wave of matrix demos
Added: projects/FPGAMatrix/arduino/bounce/bounce.pde
===================================================================
--- projects/FPGAMatrix/arduino/bounce/bounce.pde (rev 0)
+++ projects/FPGAMatrix/arduino/bounce/bounce.pde 2008-04-26 00:02:55 UTC (rev 431)
@@ -0,0 +1,132 @@
+#define CS PB2 // Pin 10
+#define MOSI PB3 // Pin 11
+#define MISO PB4 // Pin 12
+#define SCK PB5 // Pin 13
+
+void spi_init()
+{
+ // Configure SPI pins
+ DDRB |= _BV(CS) | _BV(MOSI) | _BV(SCK);
+ DDRB &= ~_BV(MISO);
+ PORTB &= ~_BV(CS);
+
+ // interrupt disabled, spi enabled, lsb 1st, master, clk low when idle,
+ // sample on leading edge of clk, system clock/2 (fastest)
+ SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR) | _BV(CPOL) | _BV(CPHA);
+ // SPSR = 0;
+// SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR);
+ SPSR = _BV(SPI2X);
+}
+
+uint8_t spi_transfer(byte data)
+{
+ SPDR = data; // Start the transmission
+ while (!(SPSR & _BV(SPIF))) {} // Wait until the end of the transmission
+// delayMicroseconds(3);
+ return SPDR; // return the received byte
+}
+
+void setAddress(uint16_t address)
+{
+ // Toggle CS makes the matrix reset its address register
+ PORTB |= _BV(CS);
+ PORTB &= ~_BV(CS);
+ spi_transfer(address & 0xFF);
+ spi_transfer(address >> 8);
+}
+
+void setAddress(uint8_t col, uint8_t row)
+{
+ uint16_t addr = (row/8)*576 + (row%8) + (col*8);
+ setAddress(addr);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void setPixel(uint8_t x, uint8_t y, uint8_t intensity)
+{
+ setAddress(x,y);
+ spi_transfer(intensity << 4);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void clearPixel(uint8_t x, uint8_t y)
+{
+ setPixel(x, y, 0x00);
+}
+
+void fill(uint8_t val, uint8_t mask = 0xff, uint16_t numpixels = 72*48)
+{
+ setAddress(0);
+ for (uint8_t m=0;m<6;m++) {
+ for (uint8_t c=0;c<72;c++) {
+ for (uint8_t r=0;r<8;r++) {
+ spi_transfer((mask & (1<<r)) ? val : 0x00);
+ }
+ }
+ }
+}
+
+uint16_t xstart1 = 0;
+uint16_t xstart2 = 0;
+uint16_t ystart1 = 0;
+uint16_t ystart2 = 0;
+
+int16_t xpos[10];
+int16_t xinc[10];
+float yscale[10];
+
+uint8_t halfsintable[128];
+
+void setup()
+{
+ Serial.begin(115200);
+ Serial.println("Matrix tester ready.");
+ spi_init();
+ fill(0x00);
+
+ for (int i=0;i<128;i++) {
+ halfsintable[i] = 48*(sin(i*2*M_PI/255));
+ }
+ for (int i=0;i<10;i++) {
+ xpos[i] = (i*123)%720;
+ xinc[i] = i%6-3;
+ if (xinc[i] == 0) xinc[i] = 1;
+ yscale[i] = 0.3 + 0.7*i/9;
+ }
+}
+
+void loop()
+{
+ unsigned long starttime = millis();
+
+ fill(0x00);
+
+ for (uint16_t i=0;i<10;i++) {
+ uint8_t y = 48-yscale[i]*halfsintable[(ystart1+i*2)%128];
+ if (y == 48) y = 47;
+ setPixel(xpos[i]/10, y, 0x0f);
+ }
+
+ ystart1 = (ystart1 + 1)%128;
+
+ for (int i=0;i<10;i++) {
+ xpos[i] += xinc[i];
+ if (xpos[i] >= 720) {
+ xpos[i] -= xpos[i] - 720 + 1;
+ xinc[i] = -xinc[i];
+ }
+ else if (xpos[i] < 0) {
+ xpos[i] += -xpos[i];
+ xinc[i] = -xinc[i];
+ }
+ }
+
+ int total = millis() - starttime;
+ if (total < 20) {
+ delay(20-total);
+ }
+}
Added: projects/FPGAMatrix/arduino/fractal/fractal.pde
===================================================================
--- projects/FPGAMatrix/arduino/fractal/fractal.pde (rev 0)
+++ projects/FPGAMatrix/arduino/fractal/fractal.pde 2008-04-26 00:02:55 UTC (rev 431)
@@ -0,0 +1,139 @@
+#define CS PB2 // Pin 10
+#define MOSI PB3 // Pin 11
+#define MISO PB4 // Pin 12
+#define SCK PB5 // Pin 13
+
+void spi_init()
+{
+ // Configure SPI pins
+ DDRB |= _BV(CS) | _BV(MOSI) | _BV(SCK);
+ DDRB &= ~_BV(MISO);
+ PORTB &= ~_BV(CS);
+
+ // interrupt disabled, spi enabled, lsb 1st, master, clk low when idle,
+ // sample on leading edge of clk, system clock/2 (fastest)
+ SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR) | _BV(CPOL) | _BV(CPHA);
+ // SPSR = 0;
+// SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR);
+ SPSR = _BV(SPI2X);
+}
+
+uint8_t spi_transfer(byte data)
+{
+ SPDR = data; // Start the transmission
+ while (!(SPSR & _BV(SPIF))) {} // Wait until the end of the transmission
+// delayMicroseconds(3);
+ return SPDR; // return the received byte
+}
+
+void setAddress(uint16_t address)
+{
+ // Toggle CS makes the matrix reset its address register
+ PORTB |= _BV(CS);
+ PORTB &= ~_BV(CS);
+ spi_transfer(address & 0xFF);
+ spi_transfer(address >> 8);
+}
+
+void setAddress(uint8_t col, uint8_t row)
+{
+ uint16_t addr = (row/8)*576 + (row%8) + (col*8);
+ setAddress(addr);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void setPixel(uint8_t x, uint8_t y, uint8_t intensity)
+{
+ setAddress(x,y);
+ spi_transfer(intensity << 4);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void clearPixel(uint8_t x, uint8_t y)
+{
+ setPixel(x, y, 0x00);
+}
+
+void fill(uint8_t val, uint8_t mask = 0xff, uint16_t numpixels = 72*48)
+{
+ setAddress(0);
+ for (uint8_t m=0;m<6;m++) {
+ for (uint8_t c=0;c<72;c++) {
+ for (uint8_t r=0;r<8;r++) {
+ spi_transfer((mask & (1<<r)) ? val : 0x00);
+ }
+ }
+ }
+}
+
+int8_t currx = 0;
+int8_t curry = 0;
+int8_t xinc = 2;
+int8_t yinc = 1;
+
+uint16_t powtable[37];
+
+void setup()
+{
+ Serial.begin(115200);
+ Serial.println("Matrix tester ready.");
+ spi_init();
+ fill(0x00);
+
+ for (int i=0;i<37;i++) {
+ powtable[i] = i*i/16;
+ }
+}
+
+float ulr = -2;
+float uli = 1.5;
+float lrr = 1;
+float lri = -1.5;
+float span = 3;
+float centerx = -0.541;
+float centery = 0.621;
+//float centerx = -0.5;
+//float centery = 0;
+
+void calcMandelbrot()
+{
+ register float zr, zr_old, zi, cr, ci;
+ register int w;
+ float xspan = span;
+ float yspan = span * 42 / 72;
+ ulr = centerx - xspan/2;
+ uli = centery - yspan/2;
+ // For each pixel
+ for (int y = 0; y < 48; y++) {
+ for (int x = 0; x < 72; x++) {
+ // Mandelbrot: c is complex number corresponding to pixel coord.
+ cr = ulr+xspan*x/72;
+ ci = uli+yspan*y/48;
+ // Mandelbrot: z0 is zero.
+ zr = 0;
+ zi = 0;
+ // While iterations<max and |z| < 2
+ for (w=0;(w<16)&&(zr*zr+zi*zi)<4;w++) {
+ zr_old=zr;
+ zr=zr*zr-zi*zi+cr;
+ zi=2*zr_old*zi+ci;
+ }
+ if (w == 16) w = 15;
+ // Set pixel to # of iterations
+ setPixel(x, y, 15-w);
+ }
+ }
+}
+
+void loop()
+{
+ unsigned long starttime = millis();
+
+ calcMandelbrot();
+
+ span = span * 0.9;
+}
Added: projects/FPGAMatrix/arduino/keftales/keftales.pde
===================================================================
--- projects/FPGAMatrix/arduino/keftales/keftales.pde (rev 0)
+++ projects/FPGAMatrix/arduino/keftales/keftales.pde 2008-04-26 00:02:55 UTC (rev 431)
@@ -0,0 +1,121 @@
+#define CS PB2 // Pin 10
+#define MOSI PB3 // Pin 11
+#define MISO PB4 // Pin 12
+#define SCK PB5 // Pin 13
+
+void spi_init()
+{
+ // Configure SPI pins
+ DDRB |= _BV(CS) | _BV(MOSI) | _BV(SCK);
+ DDRB &= ~_BV(MISO);
+ PORTB &= ~_BV(CS);
+
+ // interrupt disabled, spi enabled, lsb 1st, master, clk low when idle,
+ // sample on leading edge of clk, system clock/2 (fastest)
+ SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR) | _BV(CPOL) | _BV(CPHA);
+ // SPSR = 0;
+// SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR);
+ SPSR = _BV(SPI2X);
+}
+
+uint8_t spi_transfer(byte data)
+{
+ SPDR = data; // Start the transmission
+ while (!(SPSR & _BV(SPIF))) {} // Wait until the end of the transmission
+// delayMicroseconds(3);
+ return SPDR; // return the received byte
+}
+
+void setAddress(uint16_t address)
+{
+ // Toggle CS makes the matrix reset its address register
+ PORTB |= _BV(CS);
+ PORTB &= ~_BV(CS);
+ spi_transfer(address & 0xFF);
+ spi_transfer(address >> 8);
+}
+
+void setAddress(uint8_t col, uint8_t row)
+{
+ uint16_t addr = (row/8)*576 + (row%8) + (col*8);
+ setAddress(addr);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void setPixel(uint8_t x, uint8_t y, uint8_t intensity)
+{
+ setAddress(x,y);
+ spi_transfer(intensity << 4);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void clearPixel(uint8_t x, uint8_t y)
+{
+ setPixel(x, y, 0x00);
+}
+
+void fill(uint8_t val, uint8_t mask = 0xff, uint16_t numpixels = 72*48)
+{
+ setAddress(0);
+ for (uint8_t m=0;m<6;m++) {
+ for (uint8_t c=0;c<72;c++) {
+ for (uint8_t r=0;r<8;r++) {
+ spi_transfer((mask & (1<<r)) ? val : 0x00);
+ }
+ }
+ }
+}
+
+int8_t currx = 0;
+int8_t curry = 0;
+int8_t xinc = 2;
+int8_t yinc = 1;
+
+uint16_t powtable[37];
+
+void setup()
+{
+ Serial.begin(115200);
+ Serial.println("Matrix tester ready.");
+ spi_init();
+ fill(0x00);
+
+ for (int i=0;i<37;i++) {
+ powtable[i] = i*i/16;
+ }
+}
+
+uint16_t xstart1 = 0;
+uint16_t xstart2 = 0;
+uint16_t ystart1 = 0;
+uint16_t ystart2 = 0;
+int offset = 0;
+
+void loop()
+{
+ unsigned long starttime = millis();
+
+ // fill(0x00);
+
+ for (int y=0;y<24;y++) {
+ for (int x=0;x<36;x++) {
+ uint8_t val = (powtable[x] + powtable[y] + 32 - offset)%32;
+ if (val >=16) val = 31-val;
+ setPixel(36+x, 24+y, val);
+ setPixel(36+x, 24-y, val);
+ setPixel(36-x, 24+y, val);
+ setPixel(36-x, 24-y, val);
+ }
+ }
+
+ offset = (offset + 1)%32;
+
+// int total = millis() - starttime;
+// if (total < 20) {
+// delay(20-total);
+// }
+}
Added: projects/FPGAMatrix/arduino/sinecurve/sinecurve.pde
===================================================================
--- projects/FPGAMatrix/arduino/sinecurve/sinecurve.pde (rev 0)
+++ projects/FPGAMatrix/arduino/sinecurve/sinecurve.pde 2008-04-26 00:02:55 UTC (rev 431)
@@ -0,0 +1,127 @@
+#define CS PB2 // Pin 10
+#define MOSI PB3 // Pin 11
+#define MISO PB4 // Pin 12
+#define SCK PB5 // Pin 13
+
+void spi_init()
+{
+ // Configure SPI pins
+ DDRB |= _BV(CS) | _BV(MOSI) | _BV(SCK);
+ DDRB &= ~_BV(MISO);
+ PORTB &= ~_BV(CS);
+
+ // interrupt disabled, spi enabled, lsb 1st, master, clk low when idle,
+ // sample on leading edge of clk, system clock/2 (fastest)
+ SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR) | _BV(CPOL) | _BV(CPHA);
+ // SPSR = 0;
+// SPCR = _BV(SPE) | _BV(DORD) | _BV(MSTR);
+ SPSR = _BV(SPI2X);
+}
+
+uint8_t spi_transfer(byte data)
+{
+ SPDR = data; // Start the transmission
+ while (!(SPSR & _BV(SPIF))) {} // Wait until the end of the transmission
+// delayMicroseconds(3);
+ return SPDR; // return the received byte
+}
+
+void setAddress(uint16_t address)
+{
+ // Toggle CS makes the matrix reset its address register
+ PORTB |= _BV(CS);
+ PORTB &= ~_BV(CS);
+ spi_transfer(address & 0xFF);
+ spi_transfer(address >> 8);
+}
+
+void setAddress(uint8_t col, uint8_t row)
+{
+ uint16_t addr = (row/8)*576 + (row%8) + (col*8);
+ setAddress(addr);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void setPixel(uint8_t x, uint8_t y, uint8_t intensity)
+{
+ setAddress(x,y);
+ spi_transfer(intensity << 4);
+}
+
+/*!
+ intensity is from 0-15
+*/
+void clearPixel(uint8_t x, uint8_t y)
+{
+ setPixel(x, y, 0x00);
+}
+
+void fill(uint8_t val, uint8_t mask = 0xff, uint16_t numpixels = 72*48)
+{
+ setAddress(0);
+ for (uint8_t m=0;m<6;m++) {
+ for (uint8_t c=0;c<72;c++) {
+ for (uint8_t r=0;r<8;r++) {
+ spi_transfer((mask & (1<<r)) ? val : 0x00);
+ }
+ }
+ }
+}
+
+int8_t currx = 0;
+int8_t curry = 0;
+int8_t xinc = 2;
+int8_t yinc = 1;
+
+uint8_t xtable[256];
+uint8_t ytable[256];
+
+void setup()
+{
+ Serial.begin(115200);
+ Serial.println("Matrix tester ready.");
+ spi_init();
+ fill(0x00);
+
+ for (int i=0;i<256;i++) {
+ xtable[i] = 36*(cos(i*2*M_PI/255) + 1);
+ ytable[i] = 24*(sin(i*2*M_PI/255) + 1);
+ }
+
+}
+
+uint16_t xstart1 = 0;
+uint16_t xstart2 = 0;
+uint16_t ystart1 = 0;
+uint16_t ystart2 = 0;
+
+void loop()
+{
+ unsigned long starttime = millis();
+
+ fill(0x00);
+
+ for (uint16_t i=0;i<50;i++) {
+ uint8_t x = xtable[(xstart1 + 2*i)%255] + xtable[(xstart2 + 4*i)%255];
+ // uint8_t x = xtable[(xstart1 + i)%255] + xtable[(xstart1 + i)%255];
+ x >>= 1;
+ if (x == 72) x = 71;
+ uint8_t y = ytable[(ystart1 + 2*i)%255] + ytable[(ystart2 + 6*i)%255];
+ // uint8_t y = (ytable[(ystart1 + i)%255]);
+ y >>= 1;
+ if (y == 48) y = 47;
+ setPixel(x, y, 0x0f);
+ }
+
+ xstart1 = (xstart1 + 1)%256;
+ xstart2 = (xstart2 + 1)%256;
+ ystart1 = (ystart1 + 2)%256;
+ ystart2 = (ystart2 - 1)%256;
+
+ int total = millis() - starttime;
+ if (total < 20) {
+ delay(20-total);
+ }
+}
More information about the CGSG
mailing list