Explorar el Código

transmit: expect payload as bytes instead of List[int] to avoid "bytes" > 0xff

Fabian Peter Hammerle hace 5 años
padre
commit
ffdea3cbd0
Se han modificado 2 ficheros con 14 adiciones y 7 borrados
  1. 9 5
      cc1101/__init__.py
  2. 5 2
      examples/transmit.py

+ 9 - 5
cc1101/__init__.py

@@ -429,14 +429,14 @@ class CC1101:
         _LOGGER.debug("flushing tx fifo buffer")
         _LOGGER.debug("flushing tx fifo buffer")
         self._command_strobe(StrobeAddress.SFTX)
         self._command_strobe(StrobeAddress.SFTX)
 
 
-    def transmit(self, payload: typing.List[int]) -> None:
+    def transmit(self, payload: bytes) -> None:
         # see "15.2 Packet Format"
         # see "15.2 Packet Format"
         # > In variable packet length mode, [...]
         # > In variable packet length mode, [...]
         # > The first byte written to the TXFIFO must be different from 0.
         # > The first byte written to the TXFIFO must be different from 0.
         if payload[0] == 0:
         if payload[0] == 0:
             raise ValueError(
             raise ValueError(
                 "in variable packet length mode the first byte of payload must not be null"
                 "in variable packet length mode the first byte of payload must not be null"
-                + "\npayload: {}".format(payload)
+                + "\npayload: {!r}".format(payload)
             )
             )
         marcstate = self.get_main_radio_control_state_machine_state()
         marcstate = self.get_main_radio_control_state_machine_state()
         if marcstate != MainRadioControlStateMachineState.IDLE:
         if marcstate != MainRadioControlStateMachineState.IDLE:
@@ -451,11 +451,15 @@ class CC1101:
                 "payload exceeds maximum payload length of {} bytes".format(
                 "payload exceeds maximum payload length of {} bytes".format(
                     max_packet_length
                     max_packet_length
                 )
                 )
-                + "\npayload: {}".format(payload)
+                + "\npayload: {!r}".format(payload)
             )
             )
         self._flush_tx_fifo_buffer()
         self._flush_tx_fifo_buffer()
-        self._write_burst(FIFORegisterAddress.TX, payload)
-        _LOGGER.info("transmitting %s", payload)
+        self._write_burst(FIFORegisterAddress.TX, list(payload))
+        _LOGGER.info(
+            "transmitting 0x%s (%r)",
+            "".join("{:02x}".format(b) for b in payload),
+            payload,
+        )
         self._command_strobe(StrobeAddress.STX)
         self._command_strobe(StrobeAddress.STX)
 
 
     @contextlib.contextmanager
     @contextlib.contextmanager

+ 5 - 2
examples/transmit.py

@@ -17,6 +17,9 @@ with cc1101.CC1101() as transceiver:
     print("symbol rate", transceiver.get_symbol_rate_baud(), "Baud")
     print("symbol rate", transceiver.get_symbol_rate_baud(), "Baud")
     print("modulation format", transceiver.get_modulation_format().name)
     print("modulation format", transceiver.get_modulation_format().name)
     print("starting transmission")
     print("starting transmission")
+    transceiver.transmit(b"\x01\xff\x00 message")
+    time.sleep(1.0)
+    transceiver.transmit(bytes([0x01, 0b10101010, 0xFF]))
     while True:
     while True:
-        transceiver.transmit(list(range(1, 32)))
-        time.sleep(2.0)
+        time.sleep(1.0)
+        transceiver.transmit(bytes(range(1, 16)))