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.

MyHwESP8266.cpp 4.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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. #include "MyHwESP8266.h"
  20. #include <EEPROM.h>
  21. bool hwInit(void)
  22. {
  23. #if !defined(MY_DISABLED_SERIAL)
  24. MY_SERIALDEVICE.begin(MY_BAUD_RATE, SERIAL_8N1, MY_ESP8266_SERIAL_MODE, 1);
  25. MY_SERIALDEVICE.setDebugOutput(true);
  26. #if defined(MY_GATEWAY_SERIAL)
  27. while (!MY_SERIALDEVICE) {}
  28. #endif
  29. #endif
  30. EEPROM.begin(EEPROM_size);
  31. return true;
  32. }
  33. void hwReadConfigBlock(void *buf, void *addr, size_t length)
  34. {
  35. uint8_t *dst = static_cast<uint8_t *>(buf);
  36. int pos = reinterpret_cast<int>(addr);
  37. while (length-- > 0) {
  38. *dst++ = EEPROM.read(pos++);
  39. }
  40. }
  41. void hwWriteConfigBlock(void *buf, void *addr, size_t length)
  42. {
  43. uint8_t *src = static_cast<uint8_t *>(buf);
  44. int pos = reinterpret_cast<int>(addr);
  45. while (length-- > 0) {
  46. EEPROM.write(pos++, *src++);
  47. }
  48. // see implementation, commit only executed if diff
  49. EEPROM.commit();
  50. }
  51. uint8_t hwReadConfig(const int addr)
  52. {
  53. uint8_t value;
  54. hwReadConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
  55. return value;
  56. }
  57. void hwWriteConfig(const int addr, uint8_t value)
  58. {
  59. hwWriteConfigBlock(&value, reinterpret_cast<void *>(addr), 1);
  60. }
  61. bool hwUniqueID(unique_id_t *uniqueID)
  62. {
  63. // padding
  64. (void)memset((uint8_t *)uniqueID, MY_HWID_PADDING_BYTE, sizeof(unique_id_t));
  65. uint32_t val = ESP.getChipId();
  66. (void)memcpy((uint8_t *)uniqueID, &val, 4);
  67. val = ESP.getFlashChipId();
  68. (void)memcpy((uint8_t *)uniqueID + 4, &val, 4);
  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. // Start random number generator
  79. for (size_t i = 0; i < __length; i++) {
  80. dst[i] = (uint8_t)RANDOM_REG32;
  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(uint8_t interrupt, 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(uint8_t interrupt1, uint8_t mode1, uint8_t interrupt2, uint8_t mode2,
  99. uint32_t ms)
  100. {
  101. // TODO: Not supported!
  102. (void)interrupt1;
  103. (void)mode1;
  104. (void)interrupt2;
  105. (void)mode2;
  106. (void)ms;
  107. return MY_SLEEP_NOT_POSSIBLE;
  108. }
  109. #if defined(MY_SPECIAL_DEBUG)
  110. // settings for getVcc()
  111. ADC_MODE(ADC_VCC);
  112. #else
  113. // [default] settings for analogRead(A0)
  114. ADC_MODE(ADC_TOUT);
  115. #endif
  116. uint16_t hwCPUVoltage(void)
  117. {
  118. #if defined(MY_SPECIAL_DEBUG)
  119. // in mV, requires ADC_VCC set
  120. return ESP.getVcc();
  121. #else
  122. // not possible
  123. return FUNCTION_NOT_SUPPORTED;
  124. #endif
  125. }
  126. uint16_t hwCPUFrequency(void)
  127. {
  128. // in 1/10Mhz
  129. return ESP.getCpuFreqMHz()*10;
  130. }
  131. int8_t hwCPUTemperature(void)
  132. {
  133. return -127; // not available
  134. }
  135. uint16_t hwFreeMem(void)
  136. {
  137. return ESP.getFreeHeap();
  138. }
  139. void hwDebugPrint(const char *fmt, ... )
  140. {
  141. #ifndef MY_DISABLED_SERIAL
  142. char fmtBuffer[MY_SERIAL_OUTPUT_SIZE];
  143. #ifdef MY_GATEWAY_SERIAL
  144. // prepend debug message to be handled correctly by controller (C_INTERNAL, I_LOG_MESSAGE)
  145. snprintf_P(fmtBuffer, sizeof(fmtBuffer), PSTR("0;255;%" PRIu8 ";0;%" PRIu8 ";%" PRIu32 " "),
  146. C_INTERNAL, I_LOG_MESSAGE, hwMillis());
  147. MY_DEBUGDEVICE.print(fmtBuffer);
  148. #else
  149. // prepend timestamp
  150. MY_DEBUGDEVICE.print(hwMillis());
  151. MY_DEBUGDEVICE.print(" ");
  152. #endif
  153. va_list args;
  154. va_start(args, fmt);
  155. // cppcheck-suppress wrongPrintfScanfArgNum
  156. vsnprintf_P(fmtBuffer, sizeof(fmtBuffer), fmt, args);
  157. #ifdef MY_GATEWAY_SERIAL
  158. // Truncate message if this is gateway node
  159. fmtBuffer[sizeof(fmtBuffer) - 2] = '\n';
  160. fmtBuffer[sizeof(fmtBuffer) - 1] = '\0';
  161. #endif
  162. va_end(args);
  163. MY_DEBUGDEVICE.print(fmtBuffer);
  164. MY_DEBUGDEVICE.flush();
  165. #else
  166. (void)fmt;
  167. #endif
  168. }