_cli.py 3.4 KB

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