Smart-Home am Beispiel der Präsenzerkennung im Raum Projektarbeit Lennart Heimbs, Johannes Krug, Sebastian Dohle und Kevin Holzschuh bei Prof. Oliver Hofmann SS2019
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

I2CEeprom.cpp 2.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. // Copyright (C) 2016 Krister W. <kisse66@hobbylabs.org>
  2. //
  3. // Original SPI flash driver this is based on:
  4. // Copyright (c) 2013-2015 by Felix Rusu, LowPowerLab.com
  5. // **********************************************************************************
  6. // License
  7. // **********************************************************************************
  8. // This program is free software; you can redistribute it
  9. // and/or modify it under the terms of the GNU General
  10. // Public License as published by the Free Software
  11. // Foundation; either version 3 of the License, or
  12. // (at your option) any later version.
  13. //
  14. // This program is distributed in the hope that it will
  15. // be useful, but WITHOUT ANY WARRANTY; without even the
  16. // implied warranty of MERCHANTABILITY or FITNESS FOR A
  17. // PARTICULAR PURPOSE. See the GNU General Public
  18. // License for more details.
  19. //
  20. // You should have received a copy of the GNU General
  21. // Public License along with this program.
  22. // If not, see <http://www.gnu.org/licenses/>.
  23. //
  24. // Licence can be viewed at
  25. // http://www.gnu.org/licenses/gpl-3.0.txt
  26. //
  27. // Please maintain this license information along with authorship
  28. // and copyright notices in any redistribution of this code
  29. #include <Wire.h>
  30. #include "I2CEeprom.h"
  31. I2CEeprom::I2CEeprom(uint8_t addr) : extEEPROM(I2CEEPROM_CHIP_SIZE, 1, I2CEEPROM_PAGE_SIZE)
  32. {
  33. m_addr = addr; // we only need this for busy()
  34. }
  35. /// setup
  36. bool I2CEeprom::initialize()
  37. {
  38. return extEEPROM::begin(I2CEEPROM_TWI_CLK) ? false : true;
  39. }
  40. /// read 1 byte
  41. uint8_t I2CEeprom::readByte(uint32_t addr)
  42. {
  43. uint8_t val;
  44. readBytes(addr, &val, 1);
  45. return val;
  46. }
  47. /// read multiple bytes
  48. void I2CEeprom::readBytes(uint32_t addr, void* buf, uint16_t len)
  49. {
  50. extEEPROM::read((unsigned long)addr, (byte *)buf, (unsigned int) len);
  51. }
  52. /// check if the chip is busy
  53. bool I2CEeprom::busy()
  54. {
  55. Wire.beginTransmission(m_addr);
  56. Wire.write(0);
  57. Wire.write(0);
  58. if (Wire.endTransmission() == 0) {
  59. return false;
  60. } else {
  61. return true; // busy
  62. }
  63. }
  64. /// Write 1 byte
  65. void I2CEeprom::writeByte(uint32_t addr, uint8_t byt)
  66. {
  67. writeBytes(addr, &byt, 1);
  68. }
  69. /// write multiple bytes
  70. void I2CEeprom::writeBytes(uint32_t addr, const void* buf, uint16_t len)
  71. {
  72. extEEPROM::write((unsigned long) addr, (byte *)buf, (unsigned int) len);
  73. }