_cli.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. # python-cc1101 - Python Library to Transmit RF Signals via C1101 Transceivers
  2. #
  3. # Copyright (C) 2020 Fabian Peter Hammerle <fabian@hammerle.me>
  4. #
  5. # This program is free software: you can redistribute it and/or modify
  6. # it under the terms of the GNU General Public License as published by
  7. # the Free Software Foundation, either version 3 of the License, or
  8. # any later version.
  9. #
  10. # This program is distributed in the hope that it will be useful,
  11. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. # GNU General Public License for more details.
  14. #
  15. # You should have received a copy of the GNU General Public License
  16. # along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. import argparse
  18. import logging
  19. import sys
  20. import cc1101
  21. import cc1101.options
  22. _LOGGER = logging.getLogger(__name__)
  23. def _transmit():
  24. argparser = argparse.ArgumentParser(
  25. description="Transmits the payload provided via standard input (stdin)"
  26. " OOK-modulated in big-endian bit order.",
  27. allow_abbrev=False,
  28. )
  29. argparser.add_argument("-f", "--base-frequency-hertz", type=int)
  30. argparser.add_argument("-r", "--symbol-rate-baud", type=int)
  31. argparser.add_argument(
  32. "-s",
  33. "--sync-mode",
  34. type=str,
  35. choices=[m.name.lower().replace("_", "-") for m in cc1101.options.SyncMode],
  36. )
  37. argparser.add_argument(
  38. "-l",
  39. "--packet-length-mode",
  40. type=str,
  41. choices=[m.name.lower() for m in cc1101.options.PacketLengthMode],
  42. )
  43. argparser.add_argument("--disable-checksum", action="store_true")
  44. argparser.add_argument("-d", "--debug", action="store_true")
  45. args = argparser.parse_args()
  46. logging.basicConfig(
  47. level=logging.DEBUG if args.debug else logging.INFO,
  48. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s"
  49. if args.debug
  50. else "%(message)s",
  51. datefmt="%Y-%m-%dT%H:%M:%S%z",
  52. )
  53. _LOGGER.debug("args=%r", args)
  54. with cc1101.CC1101() as transceiver:
  55. if args.base_frequency_hertz:
  56. transceiver.set_base_frequency_hertz(args.base_frequency_hertz)
  57. if args.symbol_rate_baud:
  58. transceiver.set_symbol_rate_baud(args.symbol_rate_baud)
  59. if args.sync_mode:
  60. transceiver.set_sync_mode(
  61. cc1101.options.SyncMode[args.sync_mode.upper().replace("-", "_")]
  62. )
  63. payload = sys.stdin.buffer.read()
  64. if args.packet_length_mode:
  65. packet_length_mode = cc1101.options.PacketLengthMode[
  66. args.packet_length_mode.upper()
  67. ]
  68. # default: variable length
  69. transceiver.set_packet_length_mode(packet_length_mode)
  70. # default: 255 (maximum)
  71. if packet_length_mode == cc1101.options.PacketLengthMode.FIXED:
  72. transceiver.set_packet_length_bytes(len(payload))
  73. if args.disable_checksum:
  74. transceiver.disable_checksum()
  75. _LOGGER.info("%s", transceiver)
  76. transceiver.transmit(payload)