I am trying to create a HID that controls the up and down arrow keys with the raspberry pi. I don't know the simplest/best way to connect the pi to pc for live data exchange.
My idea is that the pi has an ultrasonic sensor that is set up vertically, that reads the distance. My hand would reflect its waves and when I was approaching the sensor it would push a key in one direction, and when I was getting further away from it, it would push the other key.
I have the ultrasonic sensor, and know how to use it and create the code that would see if I was approaching or departing. I also have a code on my pc that can make keystrokes. (ctypes library)
My issue is how do I link the 2. How do I send live data from my pi to my pc?
All code is in python
Update-
Okay. First of all let me explain what I am trying to do. My goal is to make a HID that is an ultrasonic sensor that reads how far my hand is from it. It will be placed on my desk pointing upward. When my hand approaches it, it should trigger the down arrow key. When my hand is growing further away, it should trigger the up arrow key. No hand (5 ft distance) would indicate for no movement.
Second, let me tell you what I have so far. I created the circuitry , and made a script to test distance from the sensor. It works great. I also made a script on my PC that can make keystrokes and control my PC. Afterwards I linked my Raspberry Pi and PC via SSH. With SSH I can control my Pi from my PC through the pi's terminal.
Third, the product and the problem. I can run the sensor file on my PC through the ssh and get readings in the terminal. I tried to do the same with my keystroke program to see if I could combine the 2 for a finished product, but I soon found out that I was unable to run it due to the keystroke program being made for Windows and not Raspbian. I am stuck and have no clue what to do. I was wondering if any of you wonderful people in the community could help.
Thanks!
Here is the keystroke code.
import ctypes
from ctypes import wintypes
import time
user32 = ctypes.WinDLL('user32', use_last_error=True)
INPUT_MOUSE = 0
INPUT_KEYBOARD = 1
INPUT_HARDWARE = 2
KEYEVENTF_EXTENDEDKEY = 0x0001
KEYEVENTF_KEYUP = 0x0002
KEYEVENTF_UNICODE = 0x0004
KEYEVENTF_SCANCODE = 0x0008
MAPVK_VK_TO_VSC = 0
# msdn.microsoft.com/en-us/library/dd375731
VK_TAB = 0x09
VK_MENU = 0x12
# C struct definitions
wintypes.ULONG_PTR = wintypes.WPARAM
class MOUSEINPUT(ctypes.Structure):
_fields_ = (("dx", wintypes.LONG),
("dy", wintypes.LONG),
("mouseData", wintypes.DWORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
class KEYBDINPUT(ctypes.Structure):
_fields_ = (("wVk", wintypes.WORD),
("wScan", wintypes.WORD),
("dwFlags", wintypes.DWORD),
("time", wintypes.DWORD),
("dwExtraInfo", wintypes.ULONG_PTR))
def __init__(self, *args, **kwds):
super(KEYBDINPUT, self).__init__(*args, **kwds)
# some programs use the scan code even if KEYEVENTF_SCANCODE
# isn't set in dwFflags, so attempt to map the correct code.
if not self.dwFlags & KEYEVENTF_UNICODE:
self.wScan = user32.MapVirtualKeyExW(self.wVk,
MAPVK_VK_TO_VSC, 0)
class HARDWAREINPUT(ctypes.Structure):
_fields_ = (("uMsg", wintypes.DWORD),
("wParamL", wintypes.WORD),
("wParamH", wintypes.WORD))
class INPUT(ctypes.Structure):
class _INPUT(ctypes.Union):
_fields_ = (("ki", KEYBDINPUT),
("mi", MOUSEINPUT),
("hi", HARDWAREINPUT))
_anonymous_ = ("_input",)
_fields_ = (("type", wintypes.DWORD),
("_input", _INPUT))
LPINPUT = ctypes.POINTER(INPUT)
def _check_count(result, func, args):
if result == 0:
raise ctypes.WinError(ctypes.get_last_error())
return args
user32.SendInput.errcheck = _check_count
user32.SendInput.argtypes = (wintypes.UINT, # nInputs
LPINPUT, # pInputs
ctypes.c_int) # cbSize
# Functions
def PressKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def ReleaseKey(hexKeyCode):
x = INPUT(type=INPUT_KEYBOARD,
ki=KEYBDINPUT(wVk=hexKeyCode,
dwFlags=KEYEVENTF_KEYUP))
user32.SendInput(1, ctypes.byref(x), ctypes.sizeof(x))
def AltTab():
time.sleep(2)
PressKey(0x43 )
ReleaseKey(0x43)
PressKey(0x4C )
ReleaseKey(0x4C)
PressKey(0x49 )
ReleaseKey(0x49)
PressKey(0x4E )
ReleaseKey(0x4E)
PressKey(0x54 )
ReleaseKey(0x54)
if __name__ == "__main__":
AltTab()
And here is the code I am using for the ultrasonic sensor.
#!/usr/bin/python
import RPi.GPIO as GPIO
import time
def checkdist():
GPIO.output(16, GPIO.HIGH)
time.sleep(0.000015)
GPIO.output(16, GPIO.LOW)
while not GPIO.input(18):
pass
t1 = time.time()
while GPIO.input(18):
pass
t2 = time.time()
return (t2 - t1) * 340 / 2 * 3.28084
GPIO.setmode(GPIO.BOARD)
GPIO.setup(16, GPIO.OUT, initial=GPIO.LOW)
GPIO.setup(18, GPIO.IN)
time.sleep(2)
try:
while True:
dist = checkdist()
if dist < 30:
print ('Distance: %0.2f ft' % dist)
time.sleep(0.01)
except KeyboardInterrupt:
GPIO.cleanup()
For those who are calling it a duplicate- A its not a motion sensor, that is infrared. This is a distance sensor, that is ultrasonic. B they were staying on the pi. My goal is to use it on windows through the pi. If I was wanting it just on py I could have had it done and wouldn't be here.