Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <inttypes.h>
- #include <Wire.h>
- #define BUFFER_SIZE (192)
- uint8_t buf1[BUFFER_SIZE];
- uint8_t buf2[BUFFER_SIZE];
- uint8_t *readBuf = buf1;
- uint8_t *writeBuf = buf2;
- volatile bool needsSwap = false;
- volatile uint8_t pos = 0;
- void setup()
- {
- Wire.begin(4); // join i2c bus with address #4
- Wire.onReceive(receiveEvent); // register event
- }
- void loop()
- {
- //Do whatever you need to with buf in here.
- /* you can access the bytes directly as an array, with the first 96 being readBuf[0] to readBuf[95] and second readBuf[96] to readBuf[191],
- or if you need to pass to a function whateverFunction(readBuf) or with the size whateverFunction(readBuf, 96);
- Then the second 96 bytes are passed like this whateverFunction(readBuf + 96, 96) */
- delay(100); //remove this when you have code in this loop
- //Swap the buffers,
- if(needsSwap){
- //I assume this loop() is fast, so you'll get here before the master does another tranmission?
- uint8_t *temp = writeBuf;
- writeBuf = readBuf;
- readBuf = temp;
- needsSwap = false;
- }
- }
- // function that executes whenever data is received from master
- // this function is registered as an event, see setup()
- // this is blocking, so no other code will run while this is receiving
- void receiveEvent(int howMany)
- {
- while(Wire.available() && (pos < BUFFER_SIZE))
- {
- writeBuf[pos++] = Wire.read(); // receive byte
- }
- if(pos == BUFFER_SIZE){
- pos = 0;
- needsSwap = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment