1
0

_cli.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. # python-cc1101 - Python Library to Transmit RF Signals via CC1101 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(
  40. "-p",
  41. "--output-power",
  42. metavar="SETTING",
  43. dest="output_power_settings",
  44. type=int,
  45. nargs="+",
  46. help="Configures output power levels by setting PATABLE and FREND0.PA_POWER."
  47. " Up to 8 bytes may be provided."
  48. # add when making _set_modulation_format() public
  49. # ' "[PATABLE] provides flexible PA power ramp up and ramp down'
  50. # " at the start and end of transmission when using 2-FSK, GFSK,"
  51. # ' 4-FSK, and MSK modulation as well as ASK modulation shaping."'
  52. " For OOK modulation, exactly 2 bytes must be provided:"
  53. " 0 to turn off the transmission for logical 0,"
  54. " and a level > 0 to turn on the transmission for logical 1"
  55. " (e.g., --output-power 0 198)."
  56. ' See "Table 39: Optimum PATABLE Settings for Various Output Power Levels [...]"'
  57. ' and section "24 Output Power Programming".',
  58. )
  59. argparser.add_argument("-d", "--debug", action="store_true")
  60. def _init_logging(args: argparse.Namespace) -> None:
  61. logging.basicConfig(
  62. level=logging.DEBUG if args.debug else logging.INFO,
  63. format=(
  64. "%(asctime)s:%(levelname)s:%(name)s:%(funcName)s:%(message)s"
  65. if args.debug
  66. else "%(message)s"
  67. ),
  68. datefmt="%Y-%m-%dT%H:%M:%S%z",
  69. )
  70. def _configure_via_args(
  71. *,
  72. transceiver: cc1101.CC1101,
  73. args: argparse.Namespace,
  74. packet_length_if_fixed: int | None,
  75. ) -> None:
  76. if args.base_frequency_hertz:
  77. transceiver.set_base_frequency_hertz(args.base_frequency_hertz)
  78. if args.symbol_rate_baud:
  79. transceiver.set_symbol_rate_baud(args.symbol_rate_baud)
  80. if args.sync_mode:
  81. transceiver.set_sync_mode(
  82. cc1101.options.SyncMode[args.sync_mode.upper().replace("-", "_")]
  83. )
  84. if args.packet_length_mode:
  85. packet_length_mode = cc1101.options.PacketLengthMode[
  86. args.packet_length_mode.upper()
  87. ]
  88. # default: variable length
  89. transceiver.set_packet_length_mode(packet_length_mode)
  90. # default: 255 (maximum)
  91. if (
  92. packet_length_if_fixed is not None
  93. and packet_length_mode == cc1101.options.PacketLengthMode.FIXED
  94. ):
  95. transceiver.set_packet_length_bytes(packet_length_if_fixed)
  96. if args.disable_checksum:
  97. transceiver.disable_checksum()
  98. if args.output_power_settings:
  99. transceiver.set_output_power(args.output_power_settings)
  100. def _export_config():
  101. argparser = argparse.ArgumentParser(
  102. description="Export values in CC1101's configuration registers"
  103. " after applying settings specified via command-line arguments & options",
  104. allow_abbrev=False,
  105. )
  106. _add_common_args(argparser)
  107. argparser.add_argument("--format", choices=["python-list"], default="python-list")
  108. args = argparser.parse_args()
  109. _init_logging(args)
  110. _LOGGER.debug("args=%r", args)
  111. with cc1101.CC1101(lock_spi_device=True) as transceiver:
  112. _configure_via_args(
  113. transceiver=transceiver, args=args, packet_length_if_fixed=None
  114. )
  115. _LOGGER.info("%s", transceiver)
  116. print("[")
  117. for register_index, (register, value) in enumerate(
  118. transceiver.get_configuration_register_values().items()
  119. ):
  120. assert register_index == register.value
  121. print(
  122. "0b{value:08b}, # 0x{value:02x} {register_name}".format(
  123. value=value, register_name=register.name
  124. )
  125. )
  126. print("]")
  127. print(
  128. # pylint: disable=protected-access; internal function & method
  129. "# PATABLE = "
  130. + cc1101._format_patable(transceiver._get_patable(), insert_spaces=True)
  131. )
  132. def _transmit():
  133. argparser = argparse.ArgumentParser(
  134. description="Transmits the payload provided via standard input (stdin)"
  135. " ASK/OOK-modulated in big-endian bit order.",
  136. allow_abbrev=False,
  137. )
  138. _add_common_args(argparser)
  139. args = argparser.parse_args()
  140. _init_logging(args)
  141. _LOGGER.debug("args=%r", args)
  142. payload = sys.stdin.buffer.read()
  143. # configure transceiver after reading from stdin
  144. # to avoid delay between configuration and transmission if pipe is slow
  145. with cc1101.CC1101(lock_spi_device=True) as transceiver:
  146. _configure_via_args(
  147. transceiver=transceiver, args=args, packet_length_if_fixed=len(payload)
  148. )
  149. _LOGGER.info("%s", transceiver)
  150. transceiver.transmit(payload)