_cli.py 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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. format="%(name)s:%(funcName)s:%(message)s",
  50. datefmt="%Y-%m-%dT%H:%M:%S%z",
  51. )
  52. _LOGGER.debug("args=%r", args)
  53. with cc1101.CC1101() as transceiver:
  54. if args.base_frequency_hertz:
  55. transceiver.set_base_frequency_hertz(args.base_frequency_hertz)
  56. if args.symbol_rate_baud:
  57. transceiver.set_symbol_rate_baud(args.symbol_rate_baud)
  58. if args.sync_mode:
  59. transceiver.set_sync_mode(
  60. cc1101.options.SyncMode[args.sync_mode.upper().replace("-", "_")]
  61. )
  62. payload = sys.stdin.buffer.read()
  63. if args.packet_length_mode:
  64. packet_length_mode = cc1101.options.PacketLengthMode[
  65. args.packet_length_mode.upper()
  66. ]
  67. # default: variable length
  68. transceiver.set_packet_length_mode(packet_length_mode)
  69. # default: 255 (maximum)
  70. if packet_length_mode == cc1101.options.PacketLengthMode.FIXED:
  71. transceiver.set_packet_length_bytes(len(payload))
  72. if args.disable_checksum:
  73. transceiver.disable_checksum()
  74. _LOGGER.debug("transceiver=%r", transceiver)
  75. transceiver.transmit(payload)