aboutsummaryrefslogtreecommitdiff
path: root/lib/pbdrv/drv/arduino/mod.cpp
blob: 8a38a5b17d5fee5e66b018c62c85704533296cb1 (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
25
26
27
28
29
30
31
32
33
34
35
36
37
#ifndef ARDUINO
#error This driver only works on the Arduino platform!
#endif

#include <Arduino.h>
#include <Wire.h>

#include <stdlib.h>
#include <stdint.h>

#include "../../pb.h"
#include "../../pb-mod.h"
#include "mod.h"

static void recv_event(int bytes) {
	uint8_t * data = (uint8_t *) malloc(bytes);
	size_t size = 0;
	while (Wire.available())
		data[size++] = Wire.read();

	pbdrv_i2c_recv(data, size);
}

void pbdrv_setup() {
	Wire.begin((int) PBDRV_MOD_ADDR);
	Wire.setWireTimeout(PB_TIMEOUT_US, true);
	Wire.setClock(PB_CLOCK_SPEED_HZ);
	Wire.onReceive(recv_event);
}

__weak void pbdrv_i2c_send(i2c_addr_t addr, const uint8_t * buf, size_t sz) {
	Wire.beginTransmission((int) addr);
	Wire.write(buf, sz);
	Wire.endTransmission(true);
	Wire.setWireTimeout(PB_TIMEOUT_US, true);
}