View Full Version : sd card buffer

01-13-2008, 08:05 AM
I hope this isn't too basic...
I'm having trouble trying to create an sd card buffer between two cogs. What I want to do is have one cog dump data into an array and have a second cog see the data and store it into the sd card. Despite searching the forums and the object exchange I haven't come across an example of explanation of how to do this. I don't now how to keep track of where in the array the sd cog should be reading and what array index the writing cog should write into. is there an easier, faster better way than using an array as the buffer?


01-13-2008, 08:10 AM
search for RINGBUFFER, one or two recent threads by Nick Mueller

01-13-2008, 09:40 AM
long ringBuffer[ 64 ]
long putPtr
long getPtr

PUB Main
putPtr := 0
getPtr := 40
ringBuffer[ putPtr++ ] := ReadAdc
putPtr &= $3F
WriteDac( ringBuffer[ getPtr++ ] )
getPtr &= $3F

PRI ReadAdc : adc
adc := ?

PRI WriteDac( dac )
? := dac

PRI WaitForTick

I think this is from the thread you refer to but i'm a bit confused as to how you know when the buffer is full or empty. also i don't entirely understand the use of putPtr &= $3F is this creating a bit mask? how does this work/help? if I dont want a delay in the buffer am I right that putptr and getptr would both be initialized to 0? sorry for all the questions but the code has no comments so I'm still trying to understand.
thanks for the help


01-13-2008, 11:00 AM
I see - this is a special synchronious use. You would need something like these equally simple constructs

long ringBuffer[ $3F+1 ]
long putPtr
long getPtr

PUB Main
putPtr := 0
getPtr := 0

? := getFromBuffer
IF checkBuffer
success := putToBuffer(?)

PUB checkBuffer: somethingToGet
somethingToGet := putPrt <> getPtr

PUB getFromBuffer: value
value := ringBuffer[getPtr]
IF getPtr <> putPtr
getPtr := (getPtr+1) & $3F

PUB putToBuffer(value):success
IF getPtr == (putPtr+1) & $3F
RETURN ' buffer is full
ringBuffer[putPtr] := value
putPtr := (putPtr+1) & $3F
success := TRUE

When you want to call these routines from different COGs you would need LOCKs however..