pybricks / support

Pybricks support and general discussion
MIT License
109 stars 7 forks source link

Switching mode of PUPDevice only works with read, not with write method #1213

Closed ste7anste7an closed 1 year ago

ste7anste7an commented 1 year ago

Describing the bug The bug only appears to be present in the beta pybricks firmware (Pybricks MicroPython v1.20.0-23-g6c633a8dd on 2023-07-07; SPIKE Prime Hub with STM32F413VG).

When writing to a PUPDevice, the write(mode,data) method is used, with the mode the mode of the sensor to write data to. E.g. the Distance sensor uses mode 5 to set the values of LEDS around the two sensors with 4 segments representing the 4 bytes of the data. When I read another mode first, and write immediately to mode 5, I get an error. Using the latest pybricks firmware (Pybricks MicroPython v3.2.3 on 2023-02-17; SPIKE Prime Hub with STM32F413VG) this works OK, and the program below does not result in an error.

To reproduce Steps to reproduce the behavior:

  1. Flash Spike Prime hub with beta firmware (Pybricks MicroPython v1.20.0-23-g6c633a8dd on 2023-07-07; SPIKE Prime Hub with STM32F413VG)
  2. Connect the distance sensor to port A
  3. Run the following program:
from pybricks.hubs import PrimeHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor, ForceSensor
from pybricks.parameters import Button, Color, Direction, Port, Side, Stop
from pybricks.robotics import DriveBase
from pybricks.tools import wait, StopWatch

hub = PrimeHub()

from pybricks.iodevices import PUPDevice
p=PUPDevice(Port.A)
#correct
print("read mode 1=",p.read(1))
print("read mode 5=",p.read(5))
p.write(5,[1,1,1,1])
print("success")
# failure
print("read mode 1=",p.read(1))
p.write(5,[1,1,1,1])
  1. See error
    
    read mode 1= (91,)
    read mode 5= (1, 1, 1, 1)
    success
    read mode 1= (91,)
    Traceback (most recent call last):
    File "test_mode.py", line 18, in <module>
    OSError: [Errno 1] EPERM: 

The requested operation is not valid in the current state: --> Check the documentation for required conditions. --> Check the line in your script that matches the line number given in the 'Traceback' above.


**Expected behavior**
The program should run without errors.

**Screenshots**
![image](https://github.com/pybricks/support/assets/51531682/2cbee501-cfde-43ff-9a60-3c82ccd4fa95)
laurensvalk commented 1 year ago

Was able to reproduce and just pushed a fix. Thanks for reporting!