diff --git a/README.md b/README.md index ec96773..257d9bd 100644 --- a/README.md +++ b/README.md @@ -18,26 +18,51 @@ protected: protocol* device; ``` -Then have overloaded constructors for each class member: +Then have overloaded constructors for each protocol: ```cpp public: - LSM6DS032(TwoWire *pipe, uint32_t freq) { // constructor for I2C protocol - device = new I2CProtocol(LSM6DS032_DEFAULT_I2C_ADDRESS, pipe, freq); + LSM6DSO32(TwoWire *pipe, uint32_t freq) { // constructor for I2C protocol + device = new I2CProtocol(LSM6DSO32_DEFAULT_I2C_ADDRESS, pipe, freq); } - LSM6DS032(byte chipSelect, SPIClass& spi, SPISettings settings) {// constructor overload for SPI protocol + LSM6DSO32(byte chipSelect, SPIClass& spi, SPISettings settings) {// constructor overload for SPI protocol device = new SPIProtocol(chipSelect, spi, settings, READ_BYTE, WRITE_BYTE); } ``` Then you can build code regardless of the protocol, using `device`. For example: ```cpp -uint8_t LSM6DS032::enable_sdo_pullup(bool enable) { - byte data = device->read_reg(LSM6DS032_REGISTER::PIN_CTRL); +uint8_t LSM6DSO32::enable_sdo_pullup(bool enable) { + byte data = device->read_reg(LSM6DSO32_REGISTER::PIN_CTRL); setBit(&data, 6, enable); - return device->write_reg(LSM6DS032_REGISTER::PIN_CTRL, data); + return device->write_reg(LSM6DSO32_REGISTER::PIN_CTRL, data); } ``` +### Alternatively +If you are using `protocol` independent of a class, your `device` will be of type (e.g.) `I2CProtocol` or `SPIProtocol`: + +```cpp +I2CProtocol device = I2CProtocol(0x1C, &Wire, 400000); + +// or +SPISettings settings = SPISettings(1000000, MSBFIRST, SPI_MODE0); +SPIProtocol device = SPIProtocol(CS, SPI, settings, 0x80, 0x00); +``` +Remember to begin the protocol with +```cpp +device.protocol_begin(); +``` +then use the `read_reg` and `write_reg` functions as shown: +```cpp +byte WHO_AM_I = device.read_reg(0x0F); +byte mag_data[6]; +device.read_regs(0x28, mag_data, 6); + +device.write_reg(0x20, 0x67); +byte data_to_write[6] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06}; +device.write_regs(0x28, data_to_write, 6); +``` + TODO: -- UART protocol \ No newline at end of file +- UART protocol diff --git a/src/protocol.cpp b/protocol.cpp similarity index 100% rename from src/protocol.cpp rename to protocol.cpp diff --git a/src/protocol.h b/protocol.h similarity index 98% rename from src/protocol.h rename to protocol.h index 7835965..9067873 100644 --- a/src/protocol.h +++ b/protocol.h @@ -9,6 +9,8 @@ #include "SPI.h" // TODO: Add UART support? +typedef unsigned int uint; + byte getBit(byte bits, int bitIndex); void setBit(byte* bits, int bitIndex, int val);