Oct-31-2020, 04:14 AM
(This post was last modified: Oct-31-2020, 08:57 AM by snippsat.
Edit Reason: Fix code tag
)
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?
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
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