Joel is trying to use a Raspberry Pi with a LEGO Toy Pad. He asked me about the ev3dev tutorial I wrote for using the EV3 with the LEGO DIMENSIONS NFC reader.
It was written a long long time ago in python2 so quick adapted it for python3:
#!/usr/bin/env python3
import usb.core
import usb.util
from time import sleep
# based on my old tutorial
# https://www.ev3dev.org/docs/tutorials/using-lego-dimensions-toy-pad/
#
TOYPAD_INIT = [0x55, 0x0f, 0xb0, 0x01, 0x28, 0x63, 0x29, 0x20, 0x4c, 0x45, 0x47, 0x4f, 0x20, 0x32, 0x30, 0x31, 0x34, 0xf7, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
PAD1_RED = [0x55, 0x0e, 0xc8, 0x06, 0x01, 0xff, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 51, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
PADS_OFF = [0x55, 0x06, 0xc0, 0x02, 0x00, 0x00, 0x00, 0x00, 29, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
dev = usb.core.find(idVendor=0x0e6f, idProduct=0x0241)
if dev is None:
print('Device not found')
else:
print(dev)
if dev.is_kernel_driver_active(0):
dev.detach_kernel_driver(0)
print(usb.util.get_string(dev, dev.iProduct))
dev.set_configuration()
dev.write(1,TOYPAD_INIT)
# pulse RED
dev.write(1,PAD1_RED)
sleep(0.5)
dev.write(1,PADS_OFF)
It works on my laptop running Ubuntu 22.10 but had not tested it yet with a RPi.
The most important part is not the script – there are two requirements: the ‘python3-usb’ library and a udev rule to allow python to access the LEGO USB device without root privileges:
sudo apt install python3-usb
sudo nano /etc/udev/rules.d/99-dimensions.rules
and then insert this udev rule:
SUBSYSTEM=="usb", ATTR{idVendor}=="0e6f", ATTR{idProduct}=="0241", MODE="0666"
The previous script just flashes a red light once. Meanwhile I’ve been using the Toy Pad as a controller for my robotic band, following my wife suggestion of using NFC tags to select the music to play instead of running commands in the shell (my wife is very good at bringing me to reality – at our LUG events, she is the one that demonstrates my gadgets when I am not present):
so the full code will follow soon (I hope).