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.

MyHwESP32.cpp 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174
  1. /*
  2. * The MySensors Arduino library handles the wireless radio link and protocol
  3. * between your home built sensors/actuators and HA controller of choice.
  4. * The sensors forms a self healing radio network with optional repeaters. Each
  5. * repeater and gateway builds a routing tables in EEPROM which keeps track of the
  6. * network topology allowing messages to be routed to nodes.
  7. *
  8. * Created by Henrik Ekblad <henrik.ekblad@mysensors.org>
  9. * Copyright (C) 2013-2018 Sensnology AB
  10. * Full contributor list: https://github.com/mysensors/MySensors/graphs/contributors
  11. *
  12. * Documentation: http://www.mysensors.org
  13. * Support Forum: http://forum.mysensors.org
  14. *
  15. * This program is free software; you can redistribute it and/or
  16. * modify it under the terms of the GNU General Public License
  17. * version 2 as published by the Free Software Foundation.
  18. *
  19. * Arduino core for ESP32: https://github.com/espressif/arduino-esp32
  20. *
  21. * MySensors ESP32 implementation, Copyright (C) 2017-2018 Olivier Mauti <olivier@mysensors.org>
  22. *
  23. */
  24. #include "MyHwESP32.h"
  25. bool hwInit(void)
  26. {
  27. #if !defined(MY_DISABLED_SERIAL)
  28. MY_SERIALDEVICE.begin(MY_BAUD_RATE, SERIAL_8N1);
  29. #if defined(MY_GATEWAY_SERIAL)
  30. while (!MY_SERIALDEVICE) {}
  31. #endif
  32. #endif
  33. return EEPROM.begin(MY_EEPROM_SIZE);
  34. }
  35. void hwReadConfigBlock(void *buf, void *addr, size_t length)
  36. {
  37. uint8_t *dst = static_cast<uint8_t *>(buf);
  38. int offs = reinterpret_cast<int>(addr);
  39. while (length-- > 0) {
  40. *dst++ = EEPROM.read(offs++);
  41. }
  42. }
  43. void hwWriteConfigBlock(void *buf, void *addr, size_t length)
  44. {
  45. uint8_t *src = static_cast<uint8_t *>(buf);
  46. int offs = reinterpret_cast<int>(addr);
  47. while (length-- > 0) {
  48. EEPROM.write(offs++, *src++);
  49. }
  50. EEPROM.commit();
  51. }
  52. uint8_t hwReadConfig(const int addr)
  53. {
  54. uint8_t value;
  55. hwReadConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
  56. return value;
  57. }
  58. void hwWriteConfig(const int addr, uint8_t value)
  59. {
  60. if (hwReadConfig(addr) != value) {
  61. hwWriteConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
  62. }
  63. }
  64. bool hwUniqueID(unique_id_t *uniqueID)
  65. {
  66. uint64_t val = ESP.getEfuseMac();
  67. (void)memcpy((void *)uniqueID, (void *)&val, 8);
  68. (void)memset((void *)(uniqueID + 8), MY_HWID_PADDING_BYTE, 8); // padding
  69. return true;
  70. }
  71. ssize_t hwGetentropy(void *__buffer, size_t __length)
  72. {
  73. // cut length if > 256
  74. if (__length > 256) {
  75. __length = 256;
  76. }
  77. uint8_t *dst = (uint8_t *)__buffer;
  78. // get random numbers
  79. for (size_t i = 0; i < __length; i++) {
  80. dst[i] = (uint8_t)esp_random();
  81. }
  82. return __length;
  83. }
  84. int8_t hwSleep(uint32_t ms)
  85. {
  86. // TODO: Not supported!
  87. (void)ms;
  88. return MY_SLEEP_NOT_POSSIBLE;
  89. }
  90. int8_t hwSleep(const uint8_t interrupt, const uint8_t mode, uint32_t ms)
  91. {
  92. // TODO: Not supported!
  93. (void)interrupt;
  94. (void)mode;
  95. (void)ms;
  96. return MY_SLEEP_NOT_POSSIBLE;
  97. }
  98. int8_t hwSleep(const uint8_t interrupt1, const uint8_t mode1, const uint8_t interrupt2,
  99. const uint8_t mode2,
  100. uint32_t ms)
  101. {
  102. // TODO: Not supported!
  103. (void)interrupt1;
  104. (void)mode1;
  105. (void)interrupt2;
  106. (void)mode2;
  107. (void)ms;
  108. return MY_SLEEP_NOT_POSSIBLE;
  109. }
  110. uint16_t hwCPUVoltage(void)
  111. {
  112. // in mV
  113. return FUNCTION_NOT_SUPPORTED;
  114. }
  115. uint16_t hwCPUFrequency(void)
  116. {
  117. // in 1/10Mhz
  118. return static_cast<uint16_t>(ESP.getCpuFreqMHz() * 10);
  119. }
  120. int8_t hwCPUTemperature(void)
  121. {
  122. // CPU temperature in °C
  123. return static_cast<int8_t>((temperatureRead() - MY_ESP32_TEMPERATURE_OFFSET) /
  124. MY_ESP32_TEMPERATURE_GAIN);
  125. }
  126. uint16_t hwFreeMem(void)
  127. {
  128. return static_cast<uint16_t>(ESP.getFreeHeap());
  129. }
  130. void hwDebugPrint(const char *fmt, ...)
  131. {
  132. #ifndef MY_DISABLED_SERIAL
  133. char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
  134. #ifdef MY_GATEWAY_SERIAL
  135. // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
  136. snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%" PRIu8 ";0;%" PRIu8 ";%" PRIu32 " "),
  137. C_INTERNAL, I_LOG_MESSAGE, hwMillis());
  138. MY_DEBUGDEVICE.print(fmtBuffer);
  139. #else
  140. // prepend timestamp
  141. MY_DEBUGDEVICE.print(hwMillis());
  142. MY_DEBUGDEVICE.print(F(" "));
  143. #endif
  144. va_list args;
  145. va_start(args, fmt);
  146. vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
  147. #ifdef MY_GATEWAY_SERIAL
  148. // Truncate message if this is gateway node
  149. fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
  150. fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
  151. #endif
  152. va_end(args);
  153. MY_DEBUGDEVICE.print(fmtBuffer);
  154. MY_DEBUGDEVICE.flush();
  155. #else
  156. (void)fmt;
  157. #endif
  158. }