_cli.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  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 typing
  21. import cc1101
  22. import cc1101.options
  23. _LOGGER = logging.getLogger(__name__)
  24. def _add_common_args(argparser: argparse.ArgumentParser) -> None:
  25. argparser.add_argument("-f", "--base-frequency-hertz", type=int)
  26. argparser.add_argument("-r", "--symbol-rate-baud", type=int)
  27. argparser.add_argument(
  28. "-s",
  29. "--sync-mode",
  30. type=str,
  31. choices=[m.name.lower().replace("_", "-") for m in cc1101.options.SyncMode],
  32. )
  33. argparser.add_argument(
  34. "-l",
  35. "--packet-length-mode",
  36. type=str,
  37. choices=[m.name.lower() for m in cc1101.options.PacketLengthMode],
  38. )
  39. argparser.add_argument("--disable-checksum", action="store_true")
  40. argparser.add_argument("-d", "--debug", action="store_true")
  41. def _init_logging(args: argparse.Namespace) -> None:
  42. logging.basicConfig(
  43. level=logging.DEBUG if args.debug else logging.INFO,
  44. format="%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s"
  45. if args.debug
  46. else "%(message)s",
  47. datefmt="%Y-%m-%dT%H:%M:%S%z",
  48. )
  49. def _configure_via_args(
  50. transceiver: cc1101.CC1101,
  51. args: argparse.Namespace,
  52. packet_length_if_fixed: typing.Optional[int],
  53. ) -> None:
  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. if args.packet_length_mode:
  63. packet_length_mode = cc1101.options.PacketLengthMode[
  64. args.packet_length_mode.upper()
  65. ]
  66. # default: variable length
  67. transceiver.set_packet_length_mode(packet_length_mode)
  68. # default: 255 (maximum)
  69. if (
  70. packet_length_if_fixed is not None
  71. and packet_length_mode == cc1101.options.PacketLengthMode.FIXED
  72. ):
  73. transceiver.set_packet_length_bytes(packet_length_if_fixed)
  74. if args.disable_checksum:
  75. transceiver.disable_checksum()
  76. def _transmit():
  77. argparser = argparse.ArgumentParser(
  78. description="Transmits the payload provided via standard input (stdin)"
  79. " OOK-modulated in big-endian bit order.",
  80. allow_abbrev=False,
  81. )
  82. _add_common_args(argparser)
  83. args = argparser.parse_args()
  84. _init_logging(args)
  85. _LOGGER.debug("args=%r", args)
  86. payload = sys.stdin.buffer.read()
  87. # configure transceiver after reading from stdin
  88. # to avoid delay between configuration and transmission if pipe is slow
  89. with cc1101.CC1101(lock_spi_device=True) as transceiver:
  90. _configure_via_args(
  91. transceiver=transceiver, args=args, packet_length_if_fixed=len(payload)
  92. )
  93. _LOGGER.info("%s", transceiver)
  94. transceiver.transmit(payload)