Skip to content

Instantly share code, notes, and snippets.

@dlech
dlech / main.py
Created April 14, 2024 21:42
Resolver test bench
#!/usr/bin/env pybricks-micropython
# SPDX-License-Identifier: MIT
# Copyright (c) 2024 David Lechner <dlechner@baylibre.com>
import math
from pybricks.hubs import EV3Brick
from pybricks.ev3devices import Motor, InfraredSensor
from pybricks.parameters import Port, Direction, Button
from pybricks.tools import wait
@dlech
dlech / fullscreen.js
Last active July 26, 2023 17:13
Pybricks Code Full Screen Editor TamperMonkey Script
// ==UserScript==
// @name Pybricks Code Full Screen Editor
// @namespace https://pybricks.com/tampermonkey/full-screen-editor
// @version 1.0.0
// @description Full screen text editor for Pybricks Code.
// @author David Lechner
// @match https://code.pybricks.com
// @match https://beta.pybricks.com
// @icon https://pybricks.com/favion.ico
// @grant none
@dlech
dlech / square-names.js
Last active February 8, 2023 22:17
Chess.com square names
// ==UserScript==
// @name Square names
// @source https://gist.github.com/dlech/2e0e9304458b9f7b816a59e3f187a04b
// @namespace com.lechnology.chess.squares
// @version 1.0.0
// @description Display the square names on each square.
// @author David Lechner
// @match https://www.chess.com/*
// @grant none
// ==/UserScript==
@dlech
dlech / spike-prime-large-drive-base-remote.py
Last active June 2, 2021 23:30
Example Pybricks remote control program for SPIKE Prime large drive base (experimental)
from pybricks.hubs import PrimeHub
from pybricks.experimental import Remote
from pybricks.pupdevices import Motor
from pybricks.parameters import Button, Port, Direction
from pybricks.robotics import DriveBase
from pybricks.tools import wait
DRIVE_SPEED = 400
hub = PrimeHub()
@dlech
dlech / gelo.py
Last active September 19, 2023 07:40
A program for LEGO MINDSTORMS Robot Inventor - Gelo model
# SPDX-License-Identifier: MIT
# Copyright (c) 2021 David Lechner <david@pybricks.com>
# A program for LEGO MINDSTORMS Robot Inventor - Gelo
# Developed using MINDSTORMS App v1.3.4 (10.1.0), hub firmware v1.2.01.0103
# Building instructions: https://www.lego.com/cdn/product-assets/product.bi.additional.main.pdf/51515_Gelo.pdf
# Hub API: https://lego.github.io/MINDSTORMS-Robot-Inventor-hub-API/
@dlech
dlech / _websocket.py
Created December 25, 2020 01:16
WebSocket connection for Pybricks v2.x mailboxes
"""WebSocket server compatible with Pybricks v2.x mailboxes."""
# Ref: https://developer.mozilla.org/en-US/docs/Web/API/WebSockets_API/Writing_WebSocket_servers
# Ref: https://tools.ietf.org/html/rfc6455
# Ref: https://github.com/pybricks/pybricks-micropython/blob/v2/bricks/ev3dev/modules/pybricks/messaging.py
# Ref: https://github.com/pybricks/pybricks-micropython/blob/v2/bricks/ev3dev/modules/pybricks/bluetooth.py
# Ref: https://github.com/python/cpython/blob/3.9/Lib/socketserver.py
from ubinascii import b2a_base64
from uhashlib import sha1
@dlech
dlech / music.py
Created December 18, 2020 19:12
background music
from pybricks.hubs import InventorHub
from pybricks.pupdevices import Motor, ColorSensor, UltrasonicSensor
from pybricks.parameters import Port, Stop, Color, Button
from pybricks.tools import wait, StopWatch
JINGLE_BELLS = (
["B3/4", "B3/4", "B3/2"] * 2 + ["B3/4", "D4/4", "G3/4.", "A3/8", "B3/2.", "R/4"] +
["C4/4", "C4/4", "C4/4.", "C4/8", "C4/4", "B3/4", "B3/4", "B3/8", "B3/8",
"D4/4", "D4/4", "C4/4", "A3/4", "G3/2.", "R/4"]
)
@dlech
dlech / multitask.py
Last active March 28, 2023 14:09
Example of LEGO SPIKE Prime scratch program translated to MicroPython using generator functions as coroutines
# spike.control.wait_for_seconds doesn't allow decimal points
# so we replace it with the standard MicroPython sleep function instead
from utime import sleep as wait_for_seconds
from utime import ticks_diff, ticks_ms
# See https://docs.micropython.org/en/latest/library/utime.html for more info on the utime module
from spike import (PrimeHub, LightMatrix, Button, StatusLight, ForceSensor, MotionSensor,
Speaker, ColorSensor, App, DistanceSensor, Motor, MotorPair)
from spike.control import wait_until
@dlech
dlech / nsrunloop_asyncio.py
Created June 25, 2020 21:19
NSRunLoop integration with Python asyncio
"""
NSRunLoop integration with Python asyncio.
Created on 2020-06-25 by David Lechner <david@pybricks.com>
"""
import asyncio
import selectors
import objc
from Foundation import NSDate, NSDefaultRunLoopMode, NSFileHandle, NSRunLoop
@dlech
dlech / Program.cs
Created June 14, 2020 00:47
Pybricks BLE UART C#
using System;
using System.Linq;
using System.Threading.Tasks;
using Windows.Devices.Bluetooth;
using Windows.Devices.Bluetooth.GenericAttributeProfile;
using Windows.Devices.Bluetooth.Advertisement;
using Windows.Security.Cryptography;
using Windows.Storage.Streams;
using Buffer = Windows.Storage.Streams.Buffer;
using System.IO;