Skip to content

Instantly share code, notes, and snippets.

@bricklife
bricklife / vscode-spike-extension-test.py
Last active October 18, 2022 00:51
A sample code of `Python (advanced)` for lego-spikeprime-mindstorms-vscode
# LEGO type:advanced slot:0 autostart
from runtime.virtualmachine import VirtualMachine # Needed for newer hub versions (3.1.43+)
import hub
from util.rotation import rotate_hub_display_to_value
rotates = ["1", "3", "4", "2"]
rotate_index = 0
async def on_left_button_pressed(vm, stack):
print("Left button pressed")
//
// ViewController.swift
// WebSocketInjection
//
// Created by Shinichiro Oba on 2021/03/18.
//
import UIKit
import WebKit
@bricklife
bricklife / main.py
Last active July 4, 2020 22:32
Autonomous running train in Pybricks
from pybricks.hubs import CityHub
from pybricks.pupdevices import DCMotor, ColorDistanceSensor
from pybricks.parameters import Port, Color
from pybricks.tools import wait
hub = CityHub()
motor = DCMotor(Port.A)
color_sensor = ColorDistanceSensor(Port.B)
def wait_for_color(color):
@bricklife
bricklife / LEGO SPIKE Prime JSON command examples.md
Last active June 15, 2023 11:55
LEGO SPIKE Prime JSON command examples

System

{"i":"EMWW","m":"get_firmware_info","p":{}}
{"m":"trigger_current_state","p":{}}
{"m":"program_modechange","p":{"mode":"play"}} // "mode": "play" or "download"
{"i":"ESGe","m":"set_hub_name","p":{"name":"U1BJS0U="}} // "name": Base64 string

Motor

#include <BleKeyboard.h> // https://github.com/T-vK/ESP32-BLE-Keyboard
BleKeyboard bleKeyboard;
bool pressing = false;
void setup() {
pinMode(25, INPUT_PULLUP); // A1 pin
bleKeyboard.begin();
}
#include "DigiKeyboard.h"
bool pressing = false;
void setup() {
pinMode(0, INPUT);
digitalWrite(0, HIGH); // Pull Up
}
void loop() {
#include <Bounce.h>
Bounce button = Bounce(1, 10);
void setup() {
pinMode(1, INPUT_PULLUP);
}
void loop() {
button.update();
@bricklife
bricklife / spike_send.py
Created May 21, 2020 07:01
Send command to SPIKE from EV3
#!/usr/bin/env pybricks-micropython
from pybricks.hubs import EV3Brick
from pybricks.tools import wait
from pybricks.parameters import Button
from connection import SpikePrimeStreamReader
# Add send_command method into SpikePrimeStreamReader class
def send_command(self, command):
self.sock.send((command + '\r').encode())
@bricklife
bricklife / SPIKE_Prime_REPL.txt
Last active January 18, 2023 22:05
REPL mode on LEGO Education SPIKE Prime hub
MicroPython v1.9.4-1146-gca9944357 on 2019-10-03; LEGO Technic Large Hub with STM32F413xx
Type "help()" for more information.
>>> help()
Welcome to MicroPython!
For online help please visit http://micropython.org/help/.
Quick overview of commands for the board:
hub.info() -- print some general information
hub.status() -- print sensor data
@bricklife
bricklife / main.py
Last active January 16, 2020 03:15
main.py in LEGO Education SPIKE Prime Hub
import gc
import micropython
import hub_runtime
micropython.alloc_emergency_exception_buf(256)
hub_runtime.start()