Building dummy package using pyCharm - python

I am writing a GUI for my Raspberry pi, but I'd like to work on the code on PC, then drop it into the pi, just cos the tools are better and I mostly have remote access to the pi etc etc. My problem is that the GUI does some calls to change IO pins on the Pi, using the RPi.GPIO library, which doesn't exist on PC. Of course I can comment out the lines of pi-specific code, but that's really messy especially if I start going back and forth. My idea is to set up a dummy/mirror library for the PC, then the code picks the dummy library on PC and the real library on the pi. Seems simple, but I'm getting really bogged down creating my own library. So, to my question, in summary - what is the easiest way to create a quick library using PyCharm that my code would pick up...
Here is some quick code for context...
import RPi.GPIO as GPIO # this is the library I want to mirror
# sets pin numbering on pi, does completely nothing in
#  the dummy I want to call in on on PC
GPIO.setmode(GPIO.BOARD)
GPIO.output(self.reset_pin, 1) # also does stuff on pi, nothing on PC

There is the fake-rpi package on pypi:
So, does this simulate everything on a Raspberry Pi? No! Right now it simulates what I use and need. Over time, more will be added. You are also welcome to submit pull requests for things I haven't added yet.
But it looks like it simulates GPIO pins

Related

Fast data transfer between a python program (server) and a Java program (client) on the same singleboard Linux computer (Raspberry Pi 3)

I know relativ good java and created a videogame using it for raspberry pi. The game must be controlled via an USB joystick. I searched and found many variant to read joystick data, but it seems that the best way is to use evdev written on python. How can I transfer data from a python script to a Java application? They are on the same raspberry pi with raspberry pi OS.
It seams that the best way is to create files using python and read they or the their names from my Java game. The files must be named so: 4_up_pressed.txt or
5_x+_035.txt
6_y-_075.txt
And so on. The first number is the number of a command. But I think that this way is not fast. Can I direct and fast transfer data from python into Java on the same Raspberry pi?
The best was works perfect is the library :
simple-evdev-java
Thanks you gOOse.
But it is only a simple implementation of the python library. It has no ability to access the name of the connected devices (it must have low level access and an another library with c/c++ dependencies, I think). I can set up my app with my single Raspbery PI and my gamepad, but for every other device in the production batch I need to write a difficult agroritm to test which connected device from 10 is actually the joystick. It depends also from the number of connected devices. I think I will use the simple-evdev-java library for the "in game events", but I will run the original python evdev script to get the list of all the connected devices with their names. Than I save this data in a TXT file and I read it from the main java game at the game start. I think it must work if I can make the python script with the integrated evdev library executable.
The code should be so. It works from the PyCharm:
from evdev import InputDevice, categorize, ecodes, KeyEvent
path = '/dev/input/event'
i = 0
end = 20
dataForSave = ["" for x in range(end)]
for i in range(end):
actualPath = path + str(i)
try:
gamepad = InputDevice(actualPath)
stringForSaving = gamepad.name+'_x_'+actualPath
dataForSave[i]=stringForSaving
except:
with open('devices.txt', 'w') as output_file:
for i in range(len(dataForSave)):
if (len(dataForSave[i]) > 1):
output_file.write(dataForSave[i])
output_file.write('\r')
print('ended')

How does a program distinguish between real key presses and those sent by pyautogui, pywinauto etc.?

I've been trying to write a script that sends specific key presses to the game, however they just get "ignored", while the script can send the exact same key presses to any other window without a problem. I'm using WIN 10 x64, python 3.9 and pyautogui and pywinatuo.
Is there an easy way around this? Please consider my inexperience with coding when answering.
I had a similar problem and ended up sending the desired keystroke out to a serial port, using the 'serial' module the writing some code on a 'pro micro' using an arduino ide which would accept any character coming in on the serial port and would then output said character or string to a the usb. The neat thing about the pro micro is the serial port and the usb can be same physical connection. While I had to make exceptions for some special characters like \r and \n it works quite well. Where my application that would not recognize keyboard or mouse inputs from pyautogui or pywinauto, it now does recognize them when coming from the usb as it looks like any other piece of hardware. If you are interested I will send/post the code for the pro micro and some examples of how I used it.
For normal GUI apps there is no easy way to distinguish real clicks vs pywinauto's ones. If you set monitoring keyboard/mouse hooks, there is no such info in HOOK structures. So the answer is no.

Raspberry Pi 4 controlling a Servo without shaking

I've been trying to control a mg995 Servo by changing the duty Cycle with the RPi.GPIO library, but the Servo ended up shaking a lot. I've been reading a lot of Threads about this issue and I know, that using the RPi.GPIO library is causing the issue.
I then tried to use the pigpio library but it's unfortunately not available for the RP4.
I know that buying a specific hardware could help out but I want to try it with software first.
Is there another way to controll a Servo without the shaking? I want to run the Servo through python code btw
If the driving is not done at a too high PWM frequency then you can create a solution by using SYSFS driver, if you need high frequency the problem is maybe hardware and not software to control at the oscilloscope. If you are able and want to do without any lib then you can write directly the gpio registers of the SOC through memory-map with mmap
As I tested before, software PWM is unstable and the servos can shake a lot, hardward PWM is quite acurrate, but the pi has only 2 hardware PWM channels.
The best way may be add an external PWM module (PCA9685 for example).

Receiving data from HID game controller in Python

I am trying to communicate with a game controller Human Interface Device (HID) with a Python script, but I'm having trouble receiving data from the controller. I am using pywinusb 0.3.2 and Python 3.3. The game controller is a Logitech Gamepad F310.Link Here to pywinusb.
I have been able to get python to recognize the HID, but I am stuck as on where to go from there. My code for recognizing the controller is:
import pywinusb.hid as hid
target = hid.HidDeviceFilter(vendor_id = target_vendor_id, product_id = target_product_id)
allitems = target.get_devices()
device = allitems[0]
print(device)
This code returns something like this: HID device (vID=0x00a, pID=0xa000, v=0x0000); Logitech; Logitech Dual Action, Path: \(numbers and letters in a long sequence here)
I'm not sure if the approach here would work with a controller, or whether there is another library that is preferable to pywinusb. If I have to switch libraries, I would prefer to switch to one that is cross platform. The small amount of documentation in the examples section included was not enough for me to figure this out with.
My question is: How do I receive the data from the controller? Is there a command? I've worked with the Arduino a little bit and hooking it up to a Wii Nunchuk was a similar process. Will I have to be sending initialization sequences, similar to the Arduino? I need to know the x and y values of both sticks on the controller as well as whether the buttons are pressed.
My apologies if this is obvious or has already been answered. I have never worked with an HID in python before.
Take a look to how HID usages work (google around).
Use the show_hids.py example script to review if the device exposes standard usages.
If not, your only hope is to use raw reports handlers (check the example folders from the source, I recommend you to work with github's code).

PS3 controller driver -> uinput-> python? somehow?

I'm trying to read from a PS3 controller in python on Ubuntu and I'm not having much luck. I started with the ps3joy driver from Willow Garage (http://www.ros.org/wiki/ps3joy) which supposedly publishes all the important bits of the PS3 controller to something I had never heard of called "uinput". Apparently it's a linux feature that allows userspace drivers to provide system events. ...Why the WG driver requires root access given that it's supposedly a userspace driver is beyond me, but that's not my question.
Anyway, the current state of me trying to get it to work is that I've got the driver working, and I've verified that it responds to button presses on the controller, but I don't know how to pull any of that data out so I can use it.
My first guess was to use pygame to (hopefully) read from /dev/uinput (which I'm pretty sure is where the driver sends the data):
from pygame import joystick
if not joystick.get_init():
joystick.init()
js = joystick.Joystick(0) # there is only one joystick... even if the driver isn't running(!)
js.init()
print js.get_numbuttons() # perhaps coincidentally correctly prints 17 which is the number of buttons on a PS3 controller
for i in range(js.get_numaxes()):
print js.get_axis(i) # always prints 0, no matter what I'm doing with the controller
but it didn't work. The most telling part of the problem is that it does the same thing if I don't have the WG driver running at all.
I'm sure this is something easy, that I'm just not reading the right information, but googling has not helped me find what the right information is and I'm getting tired and desperate.
You don't need the driver. Assuming the controller exposes itself as a HID, you can use the event subsystem to read controller events directly from the device.
I know it's too late, but if anyone will ever need the code or is struggling with it, you can use mine. I've wrote a script in python that gets ps3 data from USB and sends it to specific a MAC address via PC's bluetooth (you can use ps3controller.py only for data).
This was made for my quadcopter project.
https://github.com/urbanzrim/ps3controller
Try
pygame.event.pump()
before you read the joystick. I needed it to work with the 360 controller
I believe you need the following at the very least:
from pygame import joystick, event, display
display.init()
joystick.init()
js=joystick.Joystick(0)
js.init()
...
for foo in bar:
event.pump()
...
if foo:
event.pump()
...
while bar:
event.pump()
...
I believe that display.init() has to be there because it is needed for event handling...
Also, you can skip a lot of that with
import pygame
pygame.init()
js=pygame.joystick.Joystick(0)
js.init()
...
for foo in bar:
pygame.event.pump()
...
if foo:
pygame.event.pump()
...
while bar:
pygame.event.pump()
....
I could be wrong, but I think your issues are:
A) No event.pump in your if/while/for clauses
B) No display.init()
Sources:
http://webcache.googleusercontent.com/search?q=cache:I56GyE7I4CkJ:iamtherockstar.com/archive/making-hid-devices-easier-using-pygame-joysticks/+&cd=1&hl=en&ct=clnk&gl=us
and
http://www.pygame.org/docs/ref/event.html
"The input queue is heavily dependent on the pygame display module."
Solving similar problem right now: communicate/receive data from PS3 bluetooth remote with python in GNU/Linux.
What i found helpful:
Debugging PS3 Controller http://www.pabr.org/sixlinux/sixlinux.en.html
Looks like working project, for PS3 Remote http://kitlaan.twinaxis.com/projects/bluez-ps3remote/ (it requires to patch bluez 1st) did not tested, yet.
pybluez BT wrapper http://code.google.com/p/pybluez/ (checking it right now)

Categories

Resources