'Interleaving Data in C
I have the following problem - my embedded system has four fifos consisting of 64-byte chunks of data. These fifo's and the corresponding data chunks are represented in the diagram below. Once all 4 fifo's have been popped, I need to interleave the data chunks. I can do a simple loop but I am concerned this might be too processor intensive. Does anyone have a smarter way that is less processor intensive in which I can rapidly interleave the different chunks of memory?
Solution 1:[1]
Use an array of FIFO pointers:
#define FIFO_COUNT 4
fifo *fifos[FIFO_COUNT] = {&fifo0, &fifo1, &fifo2, &fifo3];
int cur_fifo = 0;
Then when you read from a FIFO read from the one referenced by cur_fifo, then increment it with wraparound:
next_block = fifo_read(fifos[cur_fifo]);
cur_fifo = (cur_fifo + 1) % FIFO_COUNT;
Solution 2:[2]
#define FIFO1 (volatile uint64_t *)FIFO1_ADDRESS
#define FIFO2 (volatile uint64_t *)FIFO2_ADDRESS
#define FIFO3 (volatile uint64_t *)FIFO3_ADDRESS
#define FIFO4 (volatile uint64_t *)FIFO4_ADDRESS
static inline uint64_t *popfifos(uint64_t *data)
{
data[0] = *FIFO1;
data[1] = *FIFO2;
data[2] = *FIFO3;
data[3] = *FIFO4;
return data + 4;
}
void readFIFOS(uint64_t *buff, size_t nreads)
{
while(nreads--)
{
if(fifosReady()) buff = popfifos(buff);
}
}
fifosReady reads some hardware registers waiting for data to be ready.
Solution 3:[3]
I've been trying to solve this and something similar as the following code works for me:
export function useWindowSize() {
const [windowSize, setWindowSize] = useState({
width: undefined,
height: undefined,
})
const handleResize = useCallback(() => {
console.log('handleResize') // It does log this out
// handle the resize...
}, [windowSize])
useEventListener('resize', throttle(handleResize, 4000)) // call it here instead
return windowSize
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Clifford |
| Solution 2 | 0___________ |
| Solution 3 | utopia |

