tone-generator/soft_twi.hpp

35 lines
541 B
C++
Raw Normal View History

2024-08-31 01:53:25 +10:00
#pragma once
#include <inttypes.h>
2024-10-01 13:39:28 +10:00
#include "util.hpp"
2024-08-31 01:53:25 +10:00
struct SoftTWI {
2024-10-01 16:19:43 +10:00
constexpr SoftTWI::SoftTWI(unsigned sda, unsigned scl)
: _sda(sda)
, _scl(scl) {
}
2024-08-31 01:53:25 +10:00
unsigned write(const uint8_t*, unsigned);
void read(uint8_t*, unsigned, bool=false);
2024-10-01 16:19:43 +10:00
uint8_t read(bool=true);
bool write(uint8_t);
void end();
inline bool start_read(uint8_t addr) {
return start((addr << 1) | 1);
}
2024-08-31 01:53:25 +10:00
2024-10-01 16:19:43 +10:00
inline bool start_write(uint8_t addr) {
return start(addr << 1);
}
2024-08-31 01:53:25 +10:00
private:
2024-08-31 16:31:19 +10:00
unsigned _sda;
unsigned _scl;
2024-10-01 16:19:43 +10:00
bool start(uint8_t addr);
2024-08-31 01:53:25 +10:00
};