v1 stable
This commit is contained in:
parent
8226b18b3c
commit
d5fee5b19d
46
LinkBox/LinkBox.ino
Normal file
46
LinkBox/LinkBox.ino
Normal file
@ -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 = "";
|
||||
}
|
34
main.py
Normal file
34
main.py
Normal file
@ -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()
|
64
ns_detector.py
Normal file
64
ns_detector.py
Normal file
@ -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:
|
||||
...
|
Loading…
Reference in New Issue
Block a user