We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
Page Index Toggle Pages: 1
buffers (Read 1123 times)
buffers
Feb 24th, 2007, 8:21am
 
Hello, I have been working on a project for a while now using processing and arduino and was wondering if someone could take a peek at some example code and make any suggestions for improvement. Basically, I'm sure that the array buffer section can be improved, I just can't figure a way to do it properly. I wanted to use a for loop instead of individually updating each item in the array, but as you can see, the array is meant to "loop" so to speak and I can't get "for" to wrap back around... any suggestions?

import processing.serial.*;

Serial port; // call a serial port

int[] frameOne = new int[8]; // array call for frame one
int[] bufferFrame = new int[8]; // array for the buffer

int timer = 60; // delay timer interval in ms

void setup()
{
 size(200, 200); // window size
 println(Serial.list()); // print a list of serial ports
 port = new Serial(this, Serial.list()[2], 9600); // call the USB port

 // data storage for frames of amimation
 frameOne[0] = 129;
 frameOne[1] = 195;
 frameOne[2] = 231;
 frameOne[3] = 255;
 frameOne[4] = 255;
 frameOne[5] = 126;
 frameOne[6] = 60;
 frameOne[7] = 24;
}

void draw()
{
 for(byte j = 0; j < 8; j++){ // for loop to step through the array
   port.write(frameOne[j]); // write data to the serial port
 }
 bufferFrame[0] = frameOne[0];
 bufferFrame[1] = frameOne[1];
 bufferFrame[2] = frameOne[2];
 bufferFrame[3] = frameOne[3];
 bufferFrame[4] = frameOne[4];
 bufferFrame[5] = frameOne[5];
 bufferFrame[6] = frameOne[6];
 bufferFrame[7] = frameOne[7];

 frameOne[0] = bufferFrame[7];
 frameOne[1] = bufferFrame[0];
 frameOne[2] = bufferFrame[1];
 frameOne[3] = bufferFrame[2];
 frameOne[4] = bufferFrame[3];
 frameOne[5] = bufferFrame[4];
 frameOne[6] = bufferFrame[5];
 frameOne[7] = bufferFrame[6];

 delay(timer); // delay the interval stored in "timer"
}
Re: buffers
Reply #1 - Feb 24th, 2007, 4:51pm
 
I would change those two lists in draw for two for loops and use modulo. Think wrap, think modulo.

Code:

for(int i = 0; i < bufferFrame.length; i++){
bufferFrame[i] = frameOne[i];
}
for(int i = 0; i < bufferFrame.length; i++){
bufferFrame[i] = frameOne[(i + 7) % frameOne.length];
}


You have to do the same thing to get a spline to join up from start to finish.

Code:

void setup(){
size(200,200);
smooth();
float [] x = {40, 150, 40};
float [] y = {50, 50, 150};
beginShape();
for(int i = 0; i < 6; i++){
curveVertex(x[i % x.length], y[i % y.length]);
}
endShape();
}
Re: buffers
Reply #2 - Feb 25th, 2007, 8:16am
 
hmmmm... I did change the first list just prior to your reply with perfect results, but changing the second as you've suggested doesn't seem to have the desired effect. in effect its seems to try to step through elements of the list that don't exist... starting at 7 and up through 14 (verified by printing the result of i+7).
i essentially need to shift the data in the array "down" one step per draw cycle... the effect is that of vertical scrolling on an LED matrix.
though the current method is working:

void draw()
{
 for(byte j = 0; j < 8; j++){ // for loop to step through the array
   port.write(frameOne[j]); // write data to the serial port
 }
 for(byte i = 0; i < 8; i++){
 bufferFrame[i] = frameOne[i];
 }
 frameOne[0] = bufferFrame[7];
 frameOne[1] = bufferFrame[0];
 frameOne[2] = bufferFrame[1];
 frameOne[3] = bufferFrame[2];
 frameOne[4] = bufferFrame[3];
 frameOne[5] = bufferFrame[4];
 frameOne[6] = bufferFrame[5];
 frameOne[7] = bufferFrame[6];

 delay(timer); // delay the interval stored in "timer"
}

I can't help but think there's a better way to do it... really just pursuing this out of curiosity at this point, so any additional suggestions would be appreciated.
Re: buffers
Reply #3 - Feb 25th, 2007, 4:18pm
 
Execute the following code:
Code:

int [] dave = {0, 1, 2, 3, 4, 5, 6, 7};
for(int i = 0; i < dave.length; i++){
println(dave[(i + 7) % dave.length]);
}

Gives me:
Code:

7
0
1
2
3
4
5
6

Which is essentially the indexing system you want right?
Re: buffers
Reply #4 - Feb 25th, 2007, 8:53pm
 
Oh what a difference a full nights sleep makes... I seem to have overlooked the obvious all together.
Your original suggestion was:

for(int i = 0; i < bufferFrame.length; i++){
 bufferFrame[i] = frameOne[(i + 7) % frameOne.length];

which is correct but backwards for my needs. After switching it to:

for(byte i = 0; i < frameOne.length; i++){
 frameOne[i] = bufferFrame[(i + 7) % bufferFrame.length];

It works like a charm now and down to 39 lines of code from an original 140 that defined every frame in advance.
I need to investigate modulo more... I've never used it before and don't really understand correctly what it's doing.

thanks st33d for your help

 
Re: buffers
Reply #5 - Feb 25th, 2007, 11:09pm
 
the modulus operator returns the remainder of a division.

for instance, 5 % 2 will return 1, since 2 can only be divided into 5 twice--with a remainder of 1. this only works for integer division.

10 % 5 = 0
10 % 4 = 2

etc.. does this help?
Re: buffers
Reply #6 - Feb 25th, 2007, 11:21pm
 
No worries greyskale. You should have seen me with my tirade of abuse at my computer over a typo earlier this week,

"The code is perfect, there is nothing wrong with it, it should bloody work you stupid machine!!!"

Typed an x instead of a y.

haliphax is right but modulo is generally used to contain a value within a range of numbers. Wrapping it round. Take the pixel array:

Lets say our pixel is: img.pixels[num];

pixelX = num % img.width;

pixelY = num / img.width;

img.pixels[pixelX + pixelY * img.width];

Thus you can treat a one dimensional array like a square (like the pixels array).

http://processing.org/learning/examples/modulo.html
Page Index Toggle Pages: 1