From d5fee5b19dcbb25e4370817164800e92dc20dace Mon Sep 17 00:00:00 2001 From: Bigsk Date: Wed, 28 Dec 2022 16:49:23 +0800 Subject: [PATCH] v1 stable --- LinkBox/LinkBox.ino | 46 ++++++++++++++++++++++++++++++++ main.py | 34 ++++++++++++++++++++++++ ns_detector.py | 64 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 144 insertions(+) create mode 100644 LinkBox/LinkBox.ino create mode 100644 main.py create mode 100644 ns_detector.py diff --git a/LinkBox/LinkBox.ino b/LinkBox/LinkBox.ino new file mode 100644 index 0000000..ed9afe5 --- /dev/null +++ b/LinkBox/LinkBox.ino @@ -0,0 +1,46 @@ +const int busyPin = A5; +const int PTTPin = 9; + +String PTTValue = ""; +bool busyStatus = false; + +void setup() { + // initialize serial + Serial.begin(9600); + // initialize pin + pinMode(PTTPin, OUTPUT); + digitalWrite(PTTPin, LOW); +} + +void loop() { + // Read busy status + int busyValue = analogRead(busyPin); + //Serial.println(busyValue); + if (busyValue > 400 && !busyStatus) { + busyStatus = true; + Serial.write("t"); + delay(1000); + } else if (busyValue < 400 && busyStatus) { + busyStatus = false; + Serial.write("f"); + delay(1000); + } + + // Read PTT status + while (Serial.available() > 0) { + PTTValue += char(Serial.read()); + delay(20); + } + + if (PTTValue.length() > 0) { + + if(PTTValue == "t\n"){ + digitalWrite(PTTPin, HIGH); + } + if(PTTValue == "f\n"){ + digitalWrite(PTTPin, LOW); + } + + } + PTTValue = ""; +} diff --git a/main.py b/main.py new file mode 100644 index 0000000..86928d1 --- /dev/null +++ b/main.py @@ -0,0 +1,34 @@ +import ns_detector +import serial, keyboard +import threading, time + +D = ns_detector.ns_detector() + +def communicator(): + last = False + portx = "COM7" + bps = 9600 + timex = 0.1 + ser = serial.Serial(portx, bps, timeout=timex) + while True: + busy = ser.read().decode() + if busy == "t": + keyboard.press("home") + elif busy == "f": + keyboard.release("home") + if D.RECV: + if not last: + ser.write("t\n".encode()) + last = True + else: + if last: + ser.write("f\n".encode()) + last = False + +def main(): + communication = threading.Thread(target=communicator, name="Communicator") + communication.daemon = True + communication.start() + + +main() \ No newline at end of file diff --git a/ns_detector.py b/ns_detector.py new file mode 100644 index 0000000..59406c1 --- /dev/null +++ b/ns_detector.py @@ -0,0 +1,64 @@ +import threading +import win32con, win32gui +from PIL import ImageGrab + +class ns_detector(object): + def __init__(self, debug = None) -> None: + self.RECV = False + self.SEND = False + self.__debug = True if debug is not None else False + monitor = threading.Thread(target=self.monitor) + monitor.daemon + monitor.start() + + def get_window_pos(self, name): + name = name + handle = win32gui.FindWindow(0, name) + # Get handle + if handle == 0: + return None + else: + # return position and handle + return win32gui.GetWindowRect(handle), handle + + def monitor(self): + while True: + (x1, y1, x2, y2), handle = self.get_window_pos('南山对讲') + try: + # Set Foreground + win32gui.SendMessage(handle, win32con.WM_SYSCOMMAND, win32con.SC_RESTORE, 0) + # Highlight window + win32gui.SetForegroundWindow(handle) + except: + pass + # Grab picture + img_ready = ImageGrab.grab((x1, y1, x2, y2)) + + try: + if img_ready.getpixel((343, 125)) == (110, 189, 83): + if not self.RECV and self.__debug: + print("RECV ON") + self.RECV = True + if self.SEND and self.__debug: + print("SEND OFF (Busy)") + self.SEND = False + elif img_ready.getpixel((343, 125)) == (61, 67, 83): + if self.RECV and self.__debug: + print("RECV OFF") + self.RECV = False + if self.SEND and self.__debug: + print("SEND OFF") + self.SEND = False + elif img_ready.getpixel((343, 125)) == (206, 82, 82): + if self.RECV and self.__debug: + print("RECV OFF (Busy)") + self.RECV = False + if not self.SEND and self.__debug: + print("SEND ON") + self.SEND = True + else: + if self.__debug: + print("RECV/SEND ERROR") + print(img_ready.getpixel((343, 125))) + except: + ... \ No newline at end of file