PlatformIO package of the Teensy core framework compatible with GCC 10 & C++20
Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

109 lines
3.2KB

  1. /**
  2. * Copyright (c) 2009 Andrew Rapp. All rights reserved.
  3. *
  4. * This file is part of XBee-Arduino.
  5. *
  6. * XBee-Arduino is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * XBee-Arduino is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with XBee-Arduino. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <XBee.h>
  20. /*
  21. This example is for Series 2 XBee
  22. Receives a ZB RX packet and sets a PWM value based on packet data.
  23. Error led is flashed if an unexpected packet is received
  24. */
  25. XBee xbee = XBee();
  26. XBeeResponse response = XBeeResponse();
  27. // create reusable response objects for responses we expect to handle
  28. ZBRxResponse rx = ZBRxResponse();
  29. ModemStatusResponse msr = ModemStatusResponse();
  30. int statusLed = 13;
  31. int errorLed = 13;
  32. int dataLed = 13;
  33. void flashLed(int pin, int times, int wait) {
  34. for (int i = 0; i < times; i++) {
  35. digitalWrite(pin, HIGH);
  36. delay(wait);
  37. digitalWrite(pin, LOW);
  38. if (i + 1 < times) {
  39. delay(wait);
  40. }
  41. }
  42. }
  43. void setup() {
  44. pinMode(statusLed, OUTPUT);
  45. pinMode(errorLed, OUTPUT);
  46. pinMode(dataLed, OUTPUT);
  47. // start serial
  48. Serial.begin(9600);
  49. xbee.begin(Serial);
  50. flashLed(statusLed, 3, 50);
  51. }
  52. // continuously reads packets, looking for ZB Receive or Modem Status
  53. void loop() {
  54. xbee.readPacket();
  55. if (xbee.getResponse().isAvailable()) {
  56. // got something
  57. if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) {
  58. // got a zb rx packet
  59. // now fill our zb rx class
  60. xbee.getResponse().getZBRxResponse(rx);
  61. if (rx.getOption() == ZB_PACKET_ACKNOWLEDGED) {
  62. // the sender got an ACK
  63. flashLed(statusLed, 10, 10);
  64. } else {
  65. // we got it (obviously) but sender didn't get an ACK
  66. flashLed(errorLed, 2, 20);
  67. }
  68. // set dataLed PWM to value of the first byte in the data
  69. analogWrite(dataLed, rx.getData(0));
  70. } else if (xbee.getResponse().getApiId() == MODEM_STATUS_RESPONSE) {
  71. xbee.getResponse().getModemStatusResponse(msr);
  72. // the local XBee sends this response on certain events, like association/dissociation
  73. if (msr.getStatus() == ASSOCIATED) {
  74. // yay this is great. flash led
  75. flashLed(statusLed, 10, 10);
  76. } else if (msr.getStatus() == DISASSOCIATED) {
  77. // this is awful.. flash led to show our discontent
  78. flashLed(errorLed, 10, 10);
  79. } else {
  80. // another status
  81. flashLed(statusLed, 5, 10);
  82. }
  83. } else {
  84. // not something we were expecting
  85. flashLed(errorLed, 1, 25);
  86. }
  87. } else if (xbee.getResponse().isError()) {
  88. //nss.print("Error reading packet. Error code: ");
  89. //nss.println(xbee.getResponse().getErrorCode());
  90. }
  91. }