Compare commits

...

2 Commits

23
i2c.hpp
View File

@@ -1,5 +1,6 @@
#pragma once #pragma once
#include <stddef.h>
#include <stdint.h> #include <stdint.h>
#include "hardware.hpp" #include "hardware.hpp"
@@ -24,12 +25,34 @@ struct I2c {
return Driver::write(data); return Driver::write(data);
} }
template <size_t Bytes>
static bool writeBytes(const uint8_t *buffer)
{
for (size_t i = 0; i < Bytes; ++i) {
if (!write(buffer[i]))
return false;
}
return true;
}
template <bool LastByte = false> template <bool LastByte = false>
static uint8_t read() static uint8_t read()
{ {
return Driver::template read<LastByte>(); return Driver::template read<LastByte>();
} }
template <size_t Bytes>
static void readBytes(uint8_t *buffer)
{
static_assert(Bytes > 0, "Must read at least 1 byte");
for (size_t i = 0; i < Bytes - 1; ++i) {
buffer[i] = read();
}
buffer[Bytes - 1] = read<true>();
}
static void stop() static void stop()
{ {
Driver::stop(); Driver::stop();