Skip to content

Commit

Permalink
Merge branch 'main' of https://github.com/TeamSunride/Protocol into HEAD
Browse files Browse the repository at this point in the history
  • Loading branch information
danverstom committed Dec 18, 2022
2 parents 7c714d3 + 054217b commit a2ba0ef
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 8 deletions.
41 changes: 33 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
- UART protocol
File renamed without changes.
2 changes: 2 additions & 0 deletions src/protocol.h → protocol.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down

0 comments on commit a2ba0ef

Please sign in to comment.