aboutsummaryrefslogtreecommitdiff
path: root/lib/pbdrv/drv
diff options
context:
space:
mode:
Diffstat (limited to 'lib/pbdrv/drv')
-rw-r--r--lib/pbdrv/drv/arduino/mod.cpp33
-rw-r--r--lib/pbdrv/drv/arduino/mod.h19
2 files changed, 52 insertions, 0 deletions
diff --git a/lib/pbdrv/drv/arduino/mod.cpp b/lib/pbdrv/drv/arduino/mod.cpp
new file mode 100644
index 0000000..c7bbe45
--- /dev/null
+++ b/lib/pbdrv/drv/arduino/mod.cpp
@@ -0,0 +1,33 @@
+#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 "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.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();
+}
+
diff --git a/lib/pbdrv/drv/arduino/mod.h b/lib/pbdrv/drv/arduino/mod.h
new file mode 100644
index 0000000..e545d9b
--- /dev/null
+++ b/lib/pbdrv/drv/arduino/mod.h
@@ -0,0 +1,19 @@
+#pragma once
+
+#include "../../pb-mod.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief puzzle bus driver setup
+ *
+ * This function should be called from the Arduino \c setup() function.
+ */
+void pbdrv_setup();
+
+#ifdef __cplusplus
+}
+#endif
+