about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMatthew Garrett <mjg59@google.com>2019-05-18T07·10-0700
committerGitHub <noreply@github.com>2019-05-18T07·10-0700
commit01928b091114d36508c15f7c103ecf8f7df9e2e4 (patch)
tree6cc60fd6f17c725766d64c5f85078f68039f8cbb
parent0db317962348907096a29bb8d3b85c6c1d0ee35e (diff)
parent3ad4a9f0f8b202863b6d33ff8a9f585ffacccf65 (diff)
Merge pull request #218 from sprilukin/rf_experiment_v0.9
RF support to v0.9 resolves #87
-rw-r--r--README.md21
-rw-r--r--broadlink/__init__.py32
-rwxr-xr-xcli/broadlink_cli52
3 files changed, 102 insertions, 3 deletions
diff --git a/README.md b/README.md
index 74473b8e3063..d5e0154db221 100644
--- a/README.md
+++ b/README.md
@@ -43,6 +43,27 @@ Enter learning mode:
 devices[0].enter_learning()
 ```
 
+Sweep RF frequencies:
+```
+devices[0].sweep_frequency()
+```
+
+Cancel sweep RF frequencies:
+```
+devices[0].cancel_sweep_frequency()
+```
+Check whether a frequency has been found:
+```
+found = devices[0].check_frequency()
+```
+(This will return True if the RM has locked onto a frequency, False otherwise)
+
+Attempt to learn an RF packet:
+```
+found = devices[0].find_rf_packet()
+```
+(This will return True if a packet has been found, False otherwise)
+
 Obtain an IR or RF packet while in learning mode:
 ```
 ir_packet = devices[0].check_data()
diff --git a/broadlink/__init__.py b/broadlink/__init__.py
index 98b6ba5958ae..271a1860bfc9 100644
--- a/broadlink/__init__.py
+++ b/broadlink/__init__.py
@@ -547,6 +547,38 @@ class rm(device):
     packet[0] = 3
     self.send_packet(0x6a, packet)
 
+  def sweep_frequency(self):
+    packet = bytearray(16)
+    packet[0] = 0x19
+    self.send_packet(0x6a, packet)
+
+  def cancel_sweep_frequency(self):
+    packet = bytearray(16)
+    packet[0] = 0x1e
+    self.send_packet(0x6a, packet)
+
+  def check_frequency(self):
+    packet = bytearray(16)
+    packet[0] = 0x1a
+    response = self.send_packet(0x6a, packet)
+    err = response[0x22] | (response[0x23] << 8)
+    if err == 0:
+      payload = self.decrypt(bytes(response[0x38:]))
+      if payload[0x04] == 1:
+          return True
+    return False
+
+  def find_rf_packet(self):
+    packet = bytearray(16)
+    packet[0] = 0x1b
+    response = self.send_packet(0x6a, packet)
+    err = response[0x22] | (response[0x23] << 8)
+    if err == 0:
+      payload = self.decrypt(bytes(response[0x38:]))
+      if payload[0x04] == 1:
+          return True
+    return False
+
   def check_temperature(self):
     packet = bytearray(16)
     packet[0] = 1
diff --git a/cli/broadlink_cli b/cli/broadlink_cli
index 9317cd197c21..1c3e231d69e5 100755
--- a/cli/broadlink_cli
+++ b/cli/broadlink_cli
@@ -1,4 +1,4 @@
-#!/usr/bin/env python
+#!/usr/bin/env python3
 
 import broadlink
 import sys
@@ -79,7 +79,8 @@ parser.add_argument("--switch", action="store_true", help="switch state from on
 parser.add_argument("--send", action="store_true", help="send command")
 parser.add_argument("--sensors", action="store_true", help="check all sensors")
 parser.add_argument("--learn", action="store_true", help="learn command")
-parser.add_argument("--learnfile", help="learn command and save to specified file")
+parser.add_argument("--rfscanlearn", action="store_true", help="rf scan learning")
+parser.add_argument("--learnfile", help="save learned command to a specified file")
 parser.add_argument("--durations", action="store_true", help="use durations in micro seconds instead of the Broadlink format")
 parser.add_argument("--convert", action="store_true", help="convert input data to durations")
 parser.add_argument("data", nargs='*', help="Data to send or convert")
@@ -119,7 +120,7 @@ if args.send:
     data = durations_to_broadlink(parse_durations(' '.join(args.data))) \
         if args.durations else bytearray.fromhex(''.join(args.data))
     dev.send_data(data)
-if args.learn or args.learnfile:
+if args.learn:
     dev.enter_learning()
     data = None
     print("Learning...")
@@ -182,3 +183,48 @@ if args.switch:
     else:
         dev.set_power(True)
         print('* Switch to ON *')
+if args.rfscanlearn:
+    dev.sweep_frequency()
+    print("Learning RF Frequency, press and hold the button to learn...")
+
+    timeout = 20
+
+    while (not dev.check_frequency()) and (timeout > 0):
+        time.sleep(1)
+        timeout -= 1
+
+    if timeout <= 0:
+        print("RF Frequency not found")
+        dev.cancel_sweep_frequency()
+        exit(1)
+
+    print("Found RF Frequency - 1 of 2!")
+    print("You can now let go of the button")
+
+    input("Press enter to continue...")
+
+    print("To complete learning, single press the button you want to learn")
+
+    dev.find_rf_packet()
+
+    data = None
+    timeout = 20
+
+    while (data is None) and (timeout > 0):
+        time.sleep(1)
+        timeout -= 1
+        data = dev.check_data()
+
+    if data:
+        print("Found RF Frequency - 2 of 2!")
+        learned = format_durations(to_microseconds(bytearray(data))) \
+            if args.durations \
+            else ''.join(format(x, '02x') for x in bytearray(data))
+        if args.learnfile is None:
+            print(learned)
+        if args.learnfile is not None:
+            print("Saving to {}".format(args.learnfile))
+            with open(args.learnfile, "w") as text_file:
+                text_file.write(learned)
+    else:
+        print("No data received...")