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.

MyGatewayTransportSerial.cpp 2.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  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 "MyConfig.h"
  20. #include "MyProtocol.h"
  21. #include "MyGatewayTransport.h"
  22. #include "MyMessage.h"
  23. #include "MyProtocol.h"
  24. // global variables
  25. extern MyMessage _msgTmp;
  26. char _serialInputString[MY_GATEWAY_MAX_RECEIVE_LENGTH]; // A buffer for incoming commands from serial interface
  27. uint8_t _serialInputPos;
  28. MyMessage _serialMsg;
  29. bool gatewayTransportSend(MyMessage &message)
  30. {
  31. setIndication(INDICATION_GW_TX);
  32. MY_SERIALDEVICE.print(protocolFormat(message));
  33. // Serial print is always successful
  34. return true;
  35. }
  36. bool gatewayTransportInit(void)
  37. {
  38. (void)gatewayTransportSend(buildGw(_msgTmp, I_GATEWAY_READY).set(MSG_GW_STARTUP_COMPLETE));
  39. // Send presentation of locally attached sensors (and node if applicable)
  40. presentNode();
  41. return true;
  42. }
  43. bool gatewayTransportAvailable(void)
  44. {
  45. while (MY_SERIALDEVICE.available()) {
  46. // get the new byte:
  47. const char inChar = (char)MY_SERIALDEVICE.read();
  48. // if the incoming character is a newline, set a flag
  49. // so the main loop can do something about it:
  50. if (_serialInputPos < MY_GATEWAY_MAX_RECEIVE_LENGTH - 1) {
  51. if (inChar == '\n') {
  52. _serialInputString[_serialInputPos] = 0;
  53. const bool ok = protocolParse(_serialMsg, _serialInputString);
  54. if (ok) {
  55. setIndication(INDICATION_GW_RX);
  56. }
  57. _serialInputPos = 0;
  58. return ok;
  59. } else {
  60. // add it to the inputString:
  61. _serialInputString[_serialInputPos] = inChar;
  62. _serialInputPos++;
  63. }
  64. } else {
  65. // Incoming message too long. Throw away
  66. _serialInputPos = 0;
  67. }
  68. }
  69. return false;
  70. }
  71. MyMessage & gatewayTransportReceive(void)
  72. {
  73. // Return the last parsed message
  74. return _serialMsg;
  75. }