|
| 1 | +/** |
| 2 | + ************************************************** |
| 3 | + * |
| 4 | + * @file easyC.hpp |
| 5 | + * @brief Basic funtions for easyC libraries |
| 6 | + * |
| 7 | + * |
| 8 | + * @copyright GNU General Public License v3.0 |
| 9 | + * @authors Goran Juric @ soldered.com |
| 10 | + ***************************************************/ |
| 11 | + |
| 12 | +#ifndef __EASYC__ |
| 13 | +#define __EASYC__ |
| 14 | + |
| 15 | +#include "Arduino.h" |
| 16 | +#include "Wire.h" |
| 17 | + |
| 18 | +#define ANALOG_READ_REG 0 |
| 19 | +#define DIGITAL_READ_REG 1 |
| 20 | + |
| 21 | +class EasyC |
| 22 | +{ |
| 23 | +public: |
| 24 | + /** |
| 25 | + * @brief Main constructor for easyC version |
| 26 | + * |
| 27 | + */ |
| 28 | + EasyC() |
| 29 | + { |
| 30 | + native = 0; |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @brief Initializes sensors on native or easyC on default address |
| 35 | + */ |
| 36 | + void begin() |
| 37 | + { |
| 38 | + if (native) |
| 39 | + initializeNative(); |
| 40 | + else |
| 41 | + begin(defaultAddress); |
| 42 | + beginDone = 1; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * @brief Initializes sensors on supplied i2c address |
| 47 | + * |
| 48 | + * @param uint8_t _address Custom easyC sensor address |
| 49 | + */ |
| 50 | + void begin(uint8_t _address) |
| 51 | + { |
| 52 | + address = _address; |
| 53 | + |
| 54 | + Wire.begin(); |
| 55 | + |
| 56 | + beginDone = 1; |
| 57 | + } |
| 58 | + |
| 59 | + int native = 0; |
| 60 | + bool beginDone = 0; |
| 61 | + |
| 62 | + virtual void initializeNative() = 0; |
| 63 | + |
| 64 | + int err; |
| 65 | + |
| 66 | + char address; |
| 67 | + const char defaultAddress = 0x30; |
| 68 | + |
| 69 | + /** |
| 70 | + * @brief Private function to send a single byte to sensor |
| 71 | + * |
| 72 | + * @param char regAddr Address of register to access later |
| 73 | + * |
| 74 | + * @return int Standard endTransmission error codes |
| 75 | + */ |
| 76 | + int sendAddress(char regAddr) |
| 77 | + { |
| 78 | + Wire.beginTransmission(address); |
| 79 | + Wire.write(regAddr); |
| 80 | + |
| 81 | + return err = Wire.endTransmission(); |
| 82 | + } |
| 83 | + |
| 84 | + /** |
| 85 | + * @brief Private function to read n bytes over i2c |
| 86 | + * |
| 87 | + * @param char a[] Array to read data to |
| 88 | + * @param int n Number of bytes to read |
| 89 | + * |
| 90 | + * @return int Error code, always 0 |
| 91 | + */ |
| 92 | + int readData(char a[], int n) |
| 93 | + { |
| 94 | + Wire.requestFrom(address, n); |
| 95 | + Wire.readBytes(a, n); |
| 96 | + |
| 97 | + return 0; |
| 98 | + } |
| 99 | + |
| 100 | + /** |
| 101 | + * @brief Private function to send over i2c and then read n bytes |
| 102 | + * |
| 103 | + * @param char regAddr Address of register to access data from |
| 104 | + * @param char a Array to put data in |
| 105 | + * @param size_t n Size of data to read |
| 106 | + * |
| 107 | + * @return int 0 if read successfuly, error code from endTransmission if not |
| 108 | + */ |
| 109 | + int readRegister(char regAddr, char a[], size_t n) |
| 110 | + { |
| 111 | + if (sendAddress(regAddr)) |
| 112 | + return err; |
| 113 | + |
| 114 | + if (readData(a, n)) |
| 115 | + return err; |
| 116 | + |
| 117 | + return 0; |
| 118 | + } |
| 119 | + |
| 120 | + /** |
| 121 | + * @brief Private function to write n bytes over i2c |
| 122 | + * |
| 123 | + * @param char a[] Array to read data from |
| 124 | + * @param int n Number of bytes to read |
| 125 | + * |
| 126 | + * @return int Standard endTransmission error codes |
| 127 | + */ |
| 128 | + int sendData(const uint8_t *a, int n) |
| 129 | + { |
| 130 | + Wire.beginTransmission(address); |
| 131 | + Wire.write(a, n); |
| 132 | + |
| 133 | + return err = Wire.endTransmission(); |
| 134 | + } |
| 135 | +}; |
| 136 | + |
| 137 | +#endif |
0 commit comments