aboutsummaryrefslogtreecommitdiffstats
path: root/nrf52/i2c_ring_buffer.cpp
blob: 0f215f254661927d6bd5e201ec4ef8c59e71e18e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include "i2c_ring_buffer.h"

#include <SimplyAtomic.h>

I2CRingBuffer::I2CRingBuffer(): head(0), tail(0) {}

bool I2CRingBuffer::read(uint8_t *v) {
  ATOMIC() {
    if (this->head == this->tail) {
      return false;
    }

    *v = this->buf[this->head];
    this->head = (this->head + 1) % RINGBUFLEN;
    return true;
  }
}

void I2CRingBuffer::write(uint8_t v) {
  ATOMIC() {
    this->buf[this->tail] = v;
    this->tail = (this->tail + 1) % RINGBUFLEN;
  }
}