Device.h 4.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. /*
  2. Device is an I2C/SPI compatible base class library.
  3. Copyright (C) Martin Lindupp 2019
  4. V1.0.0 -- Initial release
  5. V1.0.1 -- Added ESP32 HSPI support
  6. V1.0.2 -- Modification to allow external creation of HSPI object on ESP32
  7. V1.0.3 -- Addition of SPI write and read byte masks
  8. V1.0.4 -- Modification to allow user-defined pins for I2C operation on the ESP8266
  9. V1.0.5 -- Modification to allow user-defined pins for I2C operation on the ESP32
  10. V1.0.6 -- Initialise "device" constructor member variables in the same order they are declared
  11. The MIT License (MIT)
  12. Permission is hereby granted, free of charge, to any person obtaining a copy
  13. of this software and associated documentation files (the "Software"), to deal
  14. in the Software without restriction, including without limitation the rights
  15. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  16. copies of the Software, and to permit persons to whom the Software is
  17. furnished to do so, subject to the following conditions:
  18. The above copyright notice and this permission notice shall be included in all
  19. copies or substantial portions of the Software.
  20. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  21. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  22. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  23. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  24. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  25. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  26. SOFTWARE.
  27. */
  28. #ifndef Device_h
  29. #define Device_h
  30. #include <Arduino.h>
  31. #include <Wire.h>
  32. #include <SPI.h>
  33. ////////////////////////////////////////////////////////////////////////////////
  34. // Device Communications
  35. ////////////////////////////////////////////////////////////////////////////////
  36. #if defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_ESP32
  37. enum Comms { I2C_COMMS, SPI_COMMS, I2C_COMMS_DEFINED_PINS };
  38. #else
  39. enum Comms { I2C_COMMS, SPI_COMMS };
  40. #endif
  41. ////////////////////////////////////////////////////////////////////////////////
  42. // Device Class definition
  43. ////////////////////////////////////////////////////////////////////////////////
  44. class Device{
  45. public:
  46. Device(); // Device object for I2C operation
  47. #ifdef ARDUINO_ARCH_ESP8266
  48. Device(uint8_t sda, uint8_t scl); // Device object for ESP8266 I2C operation with user-defined pins
  49. #endif
  50. Device(uint8_t cs); // Device object for SPI operation
  51. #ifdef ARDUINO_ARCH_ESP32
  52. Device(uint8_t sda, uint8_t scl); // Device object for ESP32 I2C operation with user-defined pins
  53. Device(uint8_t cs, uint8_t spiPort, SPIClass& spiClass); // Device object for ESP32 HSPI operation with supplied SPI object
  54. #endif
  55. void setClock(uint32_t clockSpeed); // Set the I2C/SPI clock speed
  56. protected:
  57. void initialise(); // Initialise communications
  58. void setI2CAddress(uint8_t addr); // Set the Device I2C address
  59. void writeByte(uint8_t subAddress, uint8_t data); // I2C and SPI write byte wrapper function
  60. uint8_t readByte(uint8_t subAddress); // I2C and SPI read byte wrapper function
  61. void readBytes(uint8_t subAddress, uint8_t* dest, uint16_t count); // I2C and SPI read bytes wrapper function
  62. private:
  63. Comms comms; // Communications bus: I2C or SPI
  64. uint8_t address; // The device I2C address
  65. uint8_t cs; // The SPI chip select pin
  66. #ifdef ARDUINO_ARCH_ESP32
  67. uint8_t spiPort; // SPI port type VSPI or HSPI
  68. #endif
  69. SPIClass* spi; // Pointer to the SPI class
  70. uint32_t spiClockSpeed; // The SPI clock speed
  71. const uint8_t WRITE_MASK = 0x7F; // Sub-address write mask for SPI communications
  72. const uint8_t READ_MASK = 0x80; // Sub-address read mask for SPI communications
  73. #if defined ARDUINO_ARCH_ESP8266 || defined ARDUINO_ARCH_ESP32
  74. uint8_t sda, scl; // Software I2C SDA and SCL pins
  75. #endif
  76. };
  77. #endif