Python Forum
Question on subprocess module.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question on subprocess module.
#1
OK, I am trying to integrate a few hardware pieces. I have some main code I have working fine, but I am trying to add some more hardware to get everything I want accomplished done with a wiimote. I have already gotten a pca9685 servo bonnet working and audio output via an hdmi to vgi adapter, push button status read, and 6/9 dof readings to control several servos, lights, and lasers.

As I started getting things dialed in and working it occurred to me that as I launch the script as an rc.local instance or with the push button with a status monitor. I found that the minipitft from adafruit was small enough to provide a status readout while the script ran.

The problem I an running into is that it appears that the subprocess.popen initialization is not suited to run segmented python code. initializing the display as below creates a substantial delay on input that is similar to the response delays I had with the prior Arduino setup. as far as I can tell subprocess is meant to work with os commands, not strictly python commands.

The problem is that the wiimote can only be initialized once, no more. Once it connects a while True: loop starts. Part of the goal is to have readouts display on the display so that I can tell it is being initialized. I can store temp variables in an array to pass to the subprocess and have it run every few cycles, that is the only way to keep the script from slowing down.

The only other option is to have the display run at initialization and then not be used. I would prefer to periodically push updates to it without delaying the main script.

Below is an earlier iteration of the code.

Is there something besides supprocess.popen I should be using?
# -*- coding: utf-8 -*-
import time
import subprocess
import digitalio
import board
from PIL import Image, ImageDraw, ImageFont
import adafruit_rgb_display.st7789 as st7789
cs_pin = digitalio.DigitalInOut(board.CE0)
dc_pin = digitalio.DigitalInOut(board.D25)
reset_pin = None
BAUDRATE = 64000000
spi = board.SPI()
disp = st7789.ST7789(    spi,    cs=cs_pin,    dc=dc_pin,    rst=reset_pin,    baudrate=BAUDRATE,    width=240,    height=240,    x_offset=0,    y_offset=80,)
# button i2c assy
import busio
from i2c_button import I2C_Button
i2c = busio.I2C(board.SCL, board.SDA)
button = I2C_Button(i2c)
height = disp.width  # we swap height/width to rotate it to landscape!
width = disp.height
image = Image.new("RGB", (width, height))
rotation = 180
draw = ImageDraw.Draw(image)
draw.rectangle((0, 0, width, height), outline=0, fill=(0, 0, 0))
disp.image(image, rotation)
padding = -2
top = padding
bottom = height - padding
x = 0
font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans.ttf", 20)
backlight = digitalio.DigitalInOut(board.D22)
backlight.switch_to_output()
backlight.value = True
while True:
    print(button.status)
    state = button.status[2]
    print(state)
    # Draw a black filled box to clear the image.
    draw.rectangle((0, 0, width, height), outline=0, fill=0)
    # Shell scripts for system monitoring from here:
    # https://unix.stackexchange.com/questions/119126/command-to-display-memory-usage-disk-usage-and-cpu-load
    cmd = "hostname -I | cut -d' ' -f1"
    IP = "IP: " + subprocess.check_output(cmd, shell=True).decode("utf-8")
    cmd = "cat /sys/class/thermal/thermal_zone0/temp |  awk '{printf \"CPU Temp: %.1f C\", $(NF-0) / 1000}'"  # pylint: disable=line-too-long
    Temp = subprocess.check_output(cmd, shell=True).decode("utf-8")
    text = "hello world!"
    raw = "but, bat, acc, nunchuck:, acc, stick, but"
    test = "echo button.status[2]"
    TEST =  subprocess.check_output(test, shell=True).decode("utf-8")
    print(IP)
    print(Temp)
    print(text)
    print(raw)
# Write four lines of text.
    y = top
    draw.text((x, y), IP, font=font, fill="#FFFFFF")
    y += font.getsize(IP)[1]
    draw.text((x, y), Temp, font=font, fill="#FF00FF")
    y += font.getsize(text)[1]
    draw.text((x, y), text, font=font, fill="#F0F0F0")
    y += font.getsize(raw)[1]
    draw.text((x, y), raw, font=font, fill="#ff1900")
    y += font.getsize("state")[1]
    draw.text((x, y), str(state), font=font, fill="#ff1900")
   # Display image.
    disp.image(image, rotation)
    time.sleep(0.1)
Does anyone have any thoughts on different ways to approach this? I am running to the limits of my knowledge and nomenclature as I am learning python just to do this.

As an FYI I am documenting the main build here and intend to share all of this with the community once I get this working as this setup is being built programattically to be extremely modular and easy to modify for other folks' projects:
http://forum.alienslegacy.com/viewtopic.php?f=3&t=18792
Reply
#2
I don't know if this can help but you have two subprocess calls to get a local IP address and a sensor temperature. These could probably obtained from functions in the psutil module if you are able to import it. For the third call, the echo button.status[2], I don't know what it does, so you could perhaps explain this with more details.
Reply
#3
FYI I abandoned this piece as the code update time on drawing this added a 4 second delay on the entire script.

I determined that I was not able to branch off the specific commands to a background thread. It was some sort of limit relating to python as far as I could find.
Reply
#4
(Oct-31-2020, 08:52 AM)Gribouillis Wrote: I don't know if this can help but you have two subprocess calls to get a local IP address and a sensor temperature. These could probably obtained from functions in the psutil module if you are able to import it. For the third call, the echo button.status[2], I don't know what it does, so you could perhaps explain this with more details.

As far as your question just to be complete and nice, the wiimote button state results in a tuple. I had to pull out individual elements from the tuple and assign them to set variables. I then used those variables as inputs and outputs to control servo movements and led on/off states.

I used button counters for each button and then added if == # statements for each button to achieve the desired number of functions for each button press in sequence of presses.

Though perhaps a video might help explain what I am doing better:
https://www.youtube.com/watch?v=ogiPiRoHa8w
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  datetime module question jacksfrustration 10 1,608 Jan-12-2024, 04:54 AM
Last Post: deanhystad
  problem in using subprocess module akbarza 5 944 Sep-24-2023, 02:02 PM
Last Post: snippsat
  Module not found question sighhhh12 0 1,448 Sep-09-2022, 05:43 AM
Last Post: sighhhh12
  keyboard module question DPaul 0 2,110 Mar-23-2021, 04:22 PM
Last Post: DPaul
  Getting error from subprocess module swechsler 2 4,636 Sep-16-2019, 11:04 PM
Last Post: snippsat
  Module googletrans 2.4.0 Question hlhp 0 2,552 Jun-17-2019, 03:50 PM
Last Post: hlhp
  A question about subprocess taking input from command line and returning output! Aurimas 8 5,107 May-15-2019, 04:02 PM
Last Post: Aurimas
  Question about the Random Module Exsul 1 1,966 Mar-13-2019, 02:06 AM
Last Post: ichabod801
  OS command via python subprocess module alinaveed786 21 10,634 Oct-23-2018, 05:40 AM
Last Post: alinaveed786
  How to get the program's pid which is running with subprocess module? purecode 1 11,939 Jan-24-2018, 04:22 AM
Last Post: wavic

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020