|
| 1 | +/** |
| 2 | + * @file sfTkISerialBus.h |
| 3 | + * @brief Header file for the SparkFun Toolkit Base Serial Bus Interface Definition. |
| 4 | + * |
| 5 | + * This file contains the interface declaration for connecting sfTkISerial to the sfTkIBus. |
| 6 | + * |
| 7 | + * @author SparkFun Electronics |
| 8 | + * @date 2025 |
| 9 | + * @copyright Copyright (c) 2025, SparkFun Electronics Inc. This project is released under the MIT License. |
| 10 | + * |
| 11 | + * SPDX-License-Identifier: MIT |
| 12 | + */ |
| 13 | + |
| 14 | +#pragma once |
| 15 | +#include "sfTkError.h" |
| 16 | +#include "sfTkIBus.h" |
| 17 | +#include "sfTkISerial.h" |
| 18 | +#include <cstdint> |
| 19 | + |
| 20 | +const uint8_t ksfTkBusTypeSerialBus = 0x03; |
| 21 | + |
| 22 | +class sfTkISerialBus : sfTkIBus |
| 23 | +{ |
| 24 | + public: |
| 25 | + /** |
| 26 | + * @brief Constructor for the serial bus |
| 27 | + * |
| 28 | + */ |
| 29 | + sfTkISerialBus() |
| 30 | + { |
| 31 | + } |
| 32 | + |
| 33 | + virtual ~sfTkISerialBus() = default; |
| 34 | + |
| 35 | + /** |
| 36 | + * @brief Writes an array of bytes to the serial interface. |
| 37 | + * |
| 38 | + * @param data The data to write |
| 39 | + * @param length The length of the data buffer |
| 40 | + * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code |
| 41 | + */ |
| 42 | + virtual sfTkError_t write(const uint8_t *data, size_t length) = 0; |
| 43 | + |
| 44 | + /** |
| 45 | + * @brief Writes an array of bytes to a register on the target address. Supports any address size |
| 46 | + * |
| 47 | + * @param devReg The device's register's address - can be any size, If nullptr, address is not sent |
| 48 | + * @param regLength The length of the register address. If 0, address is not sent |
| 49 | + * @param data The data to write |
| 50 | + * @param length The length of the data buffer |
| 51 | + * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code |
| 52 | + */ |
| 53 | + virtual sfTkError_t writeRegister(uint8_t *devReg, size_t regLength, const uint8_t *data, size_t length) override |
| 54 | + { |
| 55 | + |
| 56 | + // Do we have a register? If so write it, else skip. |
| 57 | + if (devReg != nullptr && regLength > 0) |
| 58 | + write(devReg, regLength); |
| 59 | + |
| 60 | + // Write the data. |
| 61 | + return write(data, length); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * @brief Read an array of bytes from the serial interface |
| 66 | + * |
| 67 | + * @param data The data buffer to read into |
| 68 | + * @param length The length of the data buffer |
| 69 | + * @param readBytes[out] The number of bytes read |
| 70 | + * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code |
| 71 | + */ |
| 72 | + virtual sfTkError_t read(uint8_t *data, size_t length, size_t &readBytes) = 0; |
| 73 | + |
| 74 | + /** |
| 75 | + * @brief Reads an array of bytes to a register on the target address. Supports any address size |
| 76 | + * |
| 77 | + * @param devReg The device's register's address - can be any size |
| 78 | + * @param regLength The length of the register address |
| 79 | + * @param data The data to buffer to read into |
| 80 | + * @param numBytes The length of the data buffer |
| 81 | + * @param readBytes[out] The number of bytes read |
| 82 | + * @return sfTkError_t Returns ksfTkErrOk on success, or ksfTkErrFail code |
| 83 | + */ |
| 84 | + virtual sfTkError_t readRegister(uint8_t *devReg, size_t regLength, uint8_t *data, size_t numBytes, size_t &readBytes) override |
| 85 | + { |
| 86 | + // Buffer valid? |
| 87 | + if (!data) |
| 88 | + return ksfTkErrBusNullBuffer; |
| 89 | + |
| 90 | + sfTkError_t retVal = ksfTkErrOk; |
| 91 | + |
| 92 | + // Do we have a register? If so, write it, else skip. |
| 93 | + if (devReg != nullptr && regLength > 0) |
| 94 | + retVal = write(devReg, regLength); |
| 95 | + |
| 96 | + if(retVal != ksfTkErrOk) |
| 97 | + return retVal; |
| 98 | + |
| 99 | + // Read the data. |
| 100 | + retVal = read(data, numBytes, readBytes); |
| 101 | + |
| 102 | + return retVal; |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * @brief Get the type of the object |
| 107 | + * |
| 108 | + * @return uint8_t The type of the object |
| 109 | + */ |
| 110 | + virtual uint8_t type(void) override |
| 111 | + { |
| 112 | + return ksfTkBusTypeSerialBus; |
| 113 | + } |
| 114 | +}; |
0 commit comments