Krolyxon
Commit 1d55752

Branches

Tags

Refs →

orion-rf.piolibdepsesp32-s3-n16r8Adafruit BusIOAdafruit_GenericDevice.cpp

90 lines · 3.2 KB · C++

1/*
2 Written with help by Claude!
3 https://claude.ai/chat/335f50b1-3dd8-435e-9139-57ec7ca26a3c (at this time
4 chats are not shareable :(
5*/
6
7#include "Adafruit_GenericDevice.h"
8
9/*!
10 * @brief Create a Generic device with the provided read/write functions
11 * @param obj Pointer to object instance
12 * @param read_func Function pointer for reading raw data
13 * @param write_func Function pointer for writing raw data
14 * @param readreg_func Function pointer for reading registers (optional)
15 * @param writereg_func Function pointer for writing registers (optional) */
16Adafruit_GenericDevice::Adafruit_GenericDevice(
17 void *obj, busio_genericdevice_read_t read_func,
18 busio_genericdevice_write_t write_func,
19 busio_genericdevice_readreg_t readreg_func,
20 busio_genericdevice_writereg_t writereg_func) {
21 _obj = obj;
22 _read_func = read_func;
23 _write_func = write_func;
24 _readreg_func = readreg_func;
25 _writereg_func = writereg_func;
26 _begun = false;
27}
28
29/*! @brief Simple begin function (doesn't do much at this time)
30 @return true always
31*/
32bool Adafruit_GenericDevice::begin(void) {
33 _begun = true;
34 return true;
35}
36
37/*!
38@brief Marks the GenericDevice as no longer in use.
39@note: Since this is a GenericDevice, if you are using this with a Serial
40object, this does NOT disable serial communication or release the RX/TX pins.
41That must be done manually by calling Serial.end().
42*/
43void Adafruit_GenericDevice::end(void) { _begun = false; }
44
45/*! @brief Write a buffer of data
46 @param buffer Pointer to buffer of data to write
47 @param len Number of bytes to write
48 @return true if write was successful, otherwise false */
49bool Adafruit_GenericDevice::write(const uint8_t *buffer, size_t len) {
50 if (!_begun)
51 return false;
52 return _write_func(_obj, buffer, len);
53}
54
55/*! @brief Read data into a buffer
56 @param buffer Pointer to buffer to read data into
57 @param len Number of bytes to read
58 @return true if read was successful, otherwise false */
59bool Adafruit_GenericDevice::read(uint8_t *buffer, size_t len) {
60 if (!_begun)
61 return false;
62 return _read_func(_obj, buffer, len);
63}
64
65/*! @brief Read from a register location
66 @param addr_buf Buffer containing register address
67 @param addrsiz Size of register address in bytes
68 @param buf Buffer to store read data
69 @param bufsiz Size of data to read in bytes
70 @return true if read was successful, otherwise false */
71bool Adafruit_GenericDevice::readRegister(uint8_t *addr_buf, uint8_t addrsiz,
72 uint8_t *buf, uint16_t bufsiz) {
73 if (!_begun || !_readreg_func)
74 return false;
75 return _readreg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
76}
77
78/*! @brief Write to a register location
79 @param addr_buf Buffer containing register address
80 @param addrsiz Size of register address in bytes
81 @param buf Buffer containing data to write
82 @param bufsiz Size of data to write in bytes
83 @return true if write was successful, otherwise false */
84bool Adafruit_GenericDevice::writeRegister(uint8_t *addr_buf, uint8_t addrsiz,
85 const uint8_t *buf,
86 uint16_t bufsiz) {
87 if (!_begun || !_writereg_func)
88 return false;
89 return _writereg_func(_obj, addr_buf, addrsiz, buf, bufsiz);
90}