From 3c5f2cf2c7d8880c39a785fcfdcf87976c423342 Mon Sep 17 00:00:00 2001 From: c0repwn3r Date: Tue, 23 May 2023 14:50:30 -0400 Subject: [PATCH] initial commit - patcher, couple patches --- .gitignore | 1 + .gitmodules | 3 + all_blocks.md | 553 +++++++++ .../0001-pyblocks-basic-framework.patch | 1090 +++++++++++++++++ patches/scratch-blocks/0002-imu-blocks.patch | 191 +++ pbt.sh | 100 ++ scratch-blocks | 1 + 7 files changed, 1939 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 all_blocks.md create mode 100644 patches/scratch-blocks/0001-pyblocks-basic-framework.patch create mode 100644 patches/scratch-blocks/0002-imu-blocks.patch create mode 100755 pbt.sh create mode 160000 scratch-blocks diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..485dee6 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +.idea diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..67ad1fd --- /dev/null +++ b/.gitmodules @@ -0,0 +1,3 @@ +[submodule "scratch-blocks"] + path = scratch-blocks + url = https://github.com/scratchfoundation/scratch-blocks diff --git a/all_blocks.md b/all_blocks.md new file mode 100644 index 0000000..717a062 --- /dev/null +++ b/all_blocks.md @@ -0,0 +1,553 @@ +# Notation + +- `(value)` +- `(d! dropdown)` +- `` +- `(m! display matrix picker)` + +# Hub + +- DONE `initialize hub (d! top_side = Axis.Z) (d! front_size = Axis.X)` + - transpiled: `hub = PrimeHub(top_side, front_side)` + +## Hub - Status Light + +- DONE `set button color to (d! color)` + - transpiled: `hub.light.on(color)` + +- DONE `turn off button light` + - transpiled: `hub.light.off()` + +todo: light.blink +todo: light.animate + +## Hub - Display + +- `set display orientation to (d! side)` + - transpiled: `hub.display.orientation(side)` + +- DONE `turn display off` + - transpiled: `hub.display.off()` + +- `set (m! row, col) to (brightness %) brightness` + - transpiled: `hub.display.pixel(row, col, brightness)` + +- `show (m! picutre) on display` + - transpiled: `hub.display.icon(picture)` + +todo: display.animate + +- DONE `show (number) on display` + - transpiled: `hub.display.number(number)` + +- `show (char) on display` + - transpiled: `hub.display.char(char)` + +- DONE `scroll (text) on display` + - transpiled: `hub.display.text(text)` + +- `scroll (text) on display, on for (on) off for (off)` + - transpiled: `hub.display.text(text, on, off)` + +## Hub - Buttons + +!- `(buttons pressed)` +! - transpiled: `hub.buttons.pressed()` + +- DONE `` + - transpiled: `button in hub.buttons.pressed()` + +## Hub - IMU + +- DONE `` + - transpiled: `hub.imu.ready()` + +- DONE `` + - transpiled: `!hub.imu.stationary()` + +- `(side facing up)` + - transpiled: `hub.imu.up()` + +- DONE `(pitch)` + - transpiled: `hub.imu.tilt()[0]` + +- DONE `(roll)` + - transpiled: `hub.imu.roll()[1]` + +- DONE `(acceleration (d! axis))` + - transpiled: `hub.imu.acceleration(axis)` + +- DONE `(angular velocity (d! axis))` + - transpiled: `hub.imu.angular_velocity(axis)` + +- DONE `(yaw)` + - transpiled: `hub.imu.heading()` + - warning: must be reset if lifted from table + - reset in prelude? + +- DONE `reset yaw angle` + - transpiled: `hub.imu.reset_heading(0)` + +- DONE `reset yaw angle to (degrees)` + - transpiled: `hub.imu.reset_heading(degrees)` + +todo: imu.rotation +todo: imu.orientation + +- DONE `(angular velocity threshold)` + - transpiled: `hub.imu.settings()[0]` +- DONE `(acceleration threshold)` + - transpiled: `hub.imu.settings()[1]` + +- DONE `set angular velocity threshold to (threshold)` + - transpiled: `hub.imu.settings(threshold, hub.imu.settings()[1])` +- DONE `set acceleration threshold to (threshold)` + - transpiled: `hub.imu.settings(hub.imu.settings()[0], threshold)` + +## Hub - Speaker + +- `(volume)` + - transpiled: `hub.speaker.volume()` + +- `set speaker volume to (volume)` + - transpiled: `hub.speaker.volume(volume)` + +- `play tone (frequency) for (ms) ms` + - transpiled: `hub.speaker.beep(frequency, ms)` + +- `start playing tone (frequency)` + - transpiled: `hub.speaker.beep(frequency, -1)` + +todo: speaker.play_notes + +## TODO: Hub - Bluetooth +Not legal for competition, so I skipped it. +Easy to design though, for late.r + +## Hub - Battery + +- `(battery voltage)` + - transpiled: `hub.battery.voltage()` + +- `(battery current)` + - transpiled: `hub.battery.current()` + +- `` + - transpiled: `hub.charger.connected()` + +- `(charging current)` + - transpiled: `hub.charger.current()` + +- `` + - transpiled: `hub.charger.status() == 1` + +- `` + - transpiled: `hub.charger.status() == 2` + +- `` + - transpiled: `hub.charger.status() == 3` + +## Hub - Control + +- `set stop button to (button)` + - transpiled: `hub.system.set_stop_button(button)` + +- `(hub name)` + - transpiled: `hub.system.name()` + +todo: system.storage + +- DONE `stop and exit program` - program terminator + - transpiled: prelude code, go back to selection screen + +- DONE `stop and restart program` - program terminator + - transpiled: prelude code, exit, run again + +- DONE `stop and shutdown system` - program terminator + - transpiled: `hub.system.shutdown()` + +- `` + - transpiled: `hub.system.reset_reason() == 0` + +- `` + - transpiled: `hub.system.reset_reason() == 1` + +- `` + - transpiled: `hub.system.reset_reason() == 2` + +# Motors + +## Motors - DC + +- `turn on dc motor (port) at (power) power` + - transpiled: `motor.dc(power)` + +- `stop dc motor (port)` + - transpiled: `motor.stop()` + +- `set max voltage of dc motor (port) to (voltage)` + - transpiled: `motor.settings(voltage)` +- `(max voltage of dc motor (port))` + - transpiled: `motor.settings()[0]` + +- `set positive direction of dc motor (port) to (d! direction)` + - transpiled: RESET motor variable with the direction + +## Motors + +- `set positive direction of motor (port) to (d! direction)` + - traspiled: RESET motor variable with the direction + +todo: geartrain +todo: reset_angle + +- `((port) rotation angle)` + - transpiled: `motor.angle()` +- `reset rotation angle of (port)` + - transpiled: `motor.reset_angle(0)` +- `reset rotation angle of (port) to (deg)` + - transpiled: `motor.reset_angle(deg)` + +- `((port) current speed)` + - transpiled: `motor.speed()` + +- `((port) current load)` + - transpiled: `motor.load()` + +- `<(port) is stalled?>` + - transpiled: `motor.stalled()` + +- `(port) stop moving` + - transpiled: `motor.stop()` + +- `(port) brake` + - transpiled: `motor.brake()` + +- `(port) stop and hold position` + - transpiled: `motor.hold()` + +- `(port) start moving at (speed)` + - transpiled: `motor.run(speed)` + +- `(port) start moving at (power) power` + - transpiled: `motor.dc(power)` + +- `(port) run for (time) seconds at (speed) speed, then (d! stop action)` + - transpiled: `motor.run_time(speed, time, stop action, True)` + +- `(port) start running for (time) seconds at (speed) speed, then (d! stop action)` + - transpiled: `motor.run_time(speed, time, stop action, False)` + +- `(port) run for (deg) degrees at (speed), then (d! stop action)` + - transpiled: `motor.run_angle(speed, deg, stop action, True)` + +- `(port) start running for (deg) degrees at (speed), then (d! stop action)` + - transpiled: `motor.run_angle(speed, deg, stop action, False)` + +- `(port) go to relative position (deg) at (speed), then (d! stop action)` + - transpiled: `motor.run_target(speed, deg, stop action, True)` + +- `(port) start moving to relative position (deg) at (speed), then (d! stop action)` + - transpiled: `motor.run_target(speed, deg, stop action, False)` + +- `(port) go to relative position (deg) immediately` + - transpiled: `motor.track_target(deg)` + +- `(port) run at (speed) speed until stalled, then (d! stop action)` + - transpiled: `motor.run_until_stalled(speed, stop action)` +- `(port) run at (speed) speed until stalled, then (d! stop action), with maximum power (power)` + - transpiled: `motor.run_until_stalled(speed, stop action, power)` + +- `<(port) is done moving?>` + - transpiled: `motor.done()` + +- `((port) max speed)` + - transpiled: `motor.limits()[0]` +- `((port) max acceleration)` + - transpiled: `motor.limits()[1]` +- `((port) max torque)` + - transpiled: `motor.limits()[2]` +- `set (port) max speed to (speed)` + - transpiled: `motor.limits(speed, motor.limits()[1], motor.limits()[2])` +- `set (port) max acceleration to (accel)` + - transpiled: `motor.limits(motor.limits()[0], accel, motor.limits()[2])` +- `set (port) max torque to (torq)` + - transpiled: `motor.limits(motor.limits()[0], motor.limits()[1], torq)` + +TODO: pid +TODO: tolerances +TODO: stall tolerances +TODO: scale +TODO: state +TODO: settings + +# Tilt Sensor + +- `((port) pitch)` + - transpiled: `sensor.tilt()[0]` +- `((port) roll)` + - transpiled: `sensor.tilt()[1]` + +# Infrared Sensor + +- `((port) infrared distance)` + - transpiled: `sensor.distance()` +- `((port) infrared reflection)` + - transpiled: `sensor.reflection()` +- `((port) object count)` + - transpiled: `sensor.count()` + +# Color and Distance Sensor + +- `<(port) color is (d! color)?>` + - transpiled: `sensor.color() == color` +- `((port) reflection)` + - transpiled: `sensor.reflection()` +- `((port) intensity)` + - transpiled: `sensor.ambient()` +- `((port) infrared distance)` + - transpiled: `sensor.distance()` + +TODO: detectable_colors + +- `set (port) light to (d! color RGB)` + - transpiled: `sensor.light.on(color)` +- `turn off (port) light` + - transpiled: `sensor.light.off()` + +# TODO - Power Functions Infrared Controls + +# Color Sensor + +- `<(port) color is (d! color)?>` + - transpiled: `sensor.color() == color` +- `((port) reflection)` + - transpiled: `sensor.reflection()` +- `((port) intensity)` + - transpiled: `sensor.ambient()` +- `((port) hue)` + - transpiled: `sensor.hsv().h` +- `((port) saturation)` + - transpiled: `sensor.hsv().s` +- `((port) brightness)` + - transpiled: `sensor.hsv().v` + +TODO: detectable_colors + +- `turn on (port) light at (power) brightness` + - transpiled: `sensor.lights.on(power)` +- `turn off (port) light` + - transpiled: `sensor.lights.off()` + +# Ultrasonic Sensor + +- `((port) distance)` + - transpiled: `sensor.distance()` +- `<(port) other ultrasonic sensors nearby?>` + - transpiled: `sensor.presence()` +- `turn on (port) light at (power) brightness` + - transpiled: `sensor.lights.on(power)` +- `turn off (port) light` + - transpiled: `sensor.lights.off()` + +# Force Sensor + +- `((port) force)` + - transpiled: `sensor.force()` +- `((port) distance travelled)` + - transpiled: `sensor.distance()` +- `<(port) pressed?>` + - transpiled: `sensor.pressed()` +- `<(port) touched?>` + - transpiled: `sensor.touched()` + +# TODO - Color Light Matrix + +# Light + +- `turn on (port) at (power) brightness` + - transpiled: `sensor.on(power)` +- `turn off (port)` + - trnaspiled: `sensor.off()` + +# TODO - Remote Control + +# Constants + +## Constants - Axis + +- `(x axis)` + - transpiled: `Axis.X` +- `(y axis)` + - transpiled: `Axis.Y` +- `(z axis)` + - transpiled: `Axis.Z` + +## Constants - Button + +- `(left button)` + - transpiled: `Button.LEFT` +- `(center button)` + - transpiled: `Button.CENTER` +- `(right button)` + - transpiled: `Button.RIGHT` +- `(bluetooth button)` + - transpiled: `Button.BLUETOOTH` + +## Constants - Stop Behavior + +- `coast` + - transpiled: `Stop.COAST` +- `preplanned coast` + - transpiled: `Stop.COAST_SMART` +- `brake` + - transpiled: `Stop.BRAKE` +- `hold position` + - transpiled: `Stop.HOLD` +- `do not stop` + - transpiled: `Stop.NONE` + +# Tools + +- `wait (ms) ms` + - transpiled: `tools.wait(ms)` + +## TODO - Tools - Stopwatch + +# Movement (Robotics) + +- `set movement motors to (left port) and (right port)` + - transpiled: `left_motor = left port; right_motor = right port; drive = DriveBase(left_motor, right_motor, wd, track)` +- `set wheel diameter to (mm) mm` + - transpiled: `wd = mm; drive = DriveBase(left_motor, right_motor, wd, track)` +- `set track width to (mm) mm` + - transpiled: `track = mm; drive = DriveBase(left_motor, right_motor, wd, track)` + +- `move (mm) mm, then (d! stop)` + - transpiled: `drive.straight(mm, stop, True)` +- `start moving (mm) mm, then (d! stop)` + - transpiled: `drive.straight(mm, stop, False)` +- `turn in place (deg) degrees, then (d! stop)` + - transpiled: `drive.turn(deg, stop, True)` +- `start turning in place (deg) degrees, then (d! stop)` + - transpiled: `drive.turn(deg, stop, False)` +- `turn (deg) degrees across a (rad) mm arc, then (d! stop)` + - transpiled: `drive.curve(rad, deg, stop, True)` +- `start turning (deg) degrees across a (rad) mm arc, then (d! stop)` + - transpiled: `drive.curve(rad, deg, stop, False)` + +- `(movement speed)` + - transpiled: `drive.settings()[0]` +- `(movement acceleration)` + - transpiled: `drive.settings()[1]` +- `(turn speed)` + - transpiled: `drive.settings()[2]` +- `(turn acceleration)` + - transpiled: `drive.settings()[3]` +- `set movement speed to (speed)` + - transpiled: `drive.settings(speed, drive.settings()[1], drive.settings()[2], drive.settings()[3])` +- `set movement acceleration to (speed)` + - transpiled: `drive.settings(drive.settings()[0], speed, drive.settings()[2], drive.settings()[3])` +- `set turn speed to (speed)` + - transpiled: `drive.settings(drive.settings()[0], drive.settings()[1], speed, drive.settings()[3])` +- `set turn acceleration to (speed)` + - transpiled: `drive.settings(drive.settings()[0], drive.settings()[1], drive.settings()[2], speed)` + +- `` + - transpiled: `drive.done()` + +- `start moving at (spd) speed` + - transpiled: `drive.drive(spd, 0)` +- `start moving at (spd) speed with turn rate (tr)` + - transpiled: `drive.drive(spd, tr)` +- `stop moving` + - transpiled: `drive.stop()` + +- `(relative distance)` + - transpiled: `drive.distance()` +- `(relative angle)` + - transpiled: `drive.angle()` + +- `(current drive speed)` + - transpiled: `drive.state()[1]` + +- `(current turn rate)` + - transpiled: `drive.state()[3]` + +- `reset angle and distance` + - transpiled: `drive.reset()` + +- `` + - transpiled: `drive.stalled()` + +- `use gyroscope for movement` + - transpiled: `drive = GyroDriveBase(left_motor, right_motor, wd, track)` +- `use math for movement` + - transpiled: `drive = DriveBase(left_motor, right_motor, wd, track)` +# Random + +- `(random number from (a) to (b))` + - transpiled: `urandom.randint(a, b)` +- `(random decimal from 0 to 1)` + - transpiled: `urandom.random()` + +# Control + +- `repeat (n)` + - transpiled: `for i in range(n):` +- `forever` + - transpiled: `while True:` +- `if then` + - transpiled: `if cond:` +- `if then else` + - transpiled: `if cond: else:` +- `wait until ` + - transpiled: `while !: pass` +- `repeat until ` + - transpiled: `while !cond:` +- `stop` + - transpiled: `raise SystemExit("Exited by program")` +# Operators + +- `((a) + (b))` + - transpiled: `a + b` +- `((a) - (b))` + - transpiled: `a - b` +- `((a) * (b))` + - transpiled: `a * b` +- `((a) / (b))` + - transpiled: `a / b` +- `<(a) > (b)>` + - transpiled: `a > b` +- `<(a) < (b)>` + - transpiled: `a < b` +- `<(a) = (b)>` + - transpiled: `a == b` +- `< and >` + - transpiled: `a and b` +- `< or >` + - transpiled: `a or b` +- `>` + - transpiled: `not a` +- `(join (a) (b))` + - transpiled: `a + b` +- `(letter (n) of (a))` + - transpiled: `a[n]` +- `(length of (a))` + - transpiled: `len(a)` +- `<(a) contains (b)?>` + - transpiled: `a in b` +- `((a) mod (b))` + - transpiled: `a % b` +- `(round (a))` + - transpiled: `round(a)` + +TODO: operators + +# Variables + +Variables are stored as python variables with the prefix `UVAR` - for example `UVAR_myvar` + +# My blocks + +re just functions diff --git a/patches/scratch-blocks/0001-pyblocks-basic-framework.patch b/patches/scratch-blocks/0001-pyblocks-basic-framework.patch new file mode 100644 index 0000000..b3cc6cd --- /dev/null +++ b/patches/scratch-blocks/0001-pyblocks-basic-framework.patch @@ -0,0 +1,1090 @@ +From 66eeefe02e5b4690beed014e863ddfa0c69b4a75 Mon Sep 17 00:00:00 2001 +From: c0repwn3r +Date: Tue, 23 May 2023 09:22:48 -0400 +Subject: [PATCH] pyblocks basics - framework, disable useless scratch blocks - + add a couple blocks from a few categories + +--- + .idea/.gitignore | 5 + + blocks_vertical/control.js | 20 +- + blocks_vertical/default_toolbox.js | 245 +++++++++++++++++++ + blocks_vertical/display.js | 63 +++++ + blocks_vertical/hub.js | 42 ++++ + blocks_vertical/hub_status_light.js | 43 ++++ + blocks_vertical/imu.js | 118 +++++++++ + blocks_vertical/input.js | 34 +++ + blocks_vertical/{ => upstream}/data.js | 0 + blocks_vertical/{ => upstream}/event.js | 0 + blocks_vertical/{ => upstream}/extensions.js | 0 + blocks_vertical/{ => upstream}/looks.js | 0 + blocks_vertical/{ => upstream}/motion.js | 0 + blocks_vertical/{ => upstream}/procedures.js | 0 + blocks_vertical/{ => upstream}/sensing.js | 0 + blocks_vertical/{ => upstream}/sound.js | 0 + blocks_vertical/vertical_extensions.js | 3 +- + core/colours.js | 70 +++++- + core/constants.js | 24 +- + msg/js/en.js | 12 + + msg/json/en.json | 14 +- + msg/messages.js | 67 +++++ + package.json | 2 +- + tests/custom_procedure_playground.html | 14 +- + tests/vertical_playground.html | 26 +- + tests/workspace_svg/index.html | 4 +- + 26 files changed, 764 insertions(+), 42 deletions(-) + create mode 100644 .idea/.gitignore + create mode 100644 blocks_vertical/display.js + create mode 100644 blocks_vertical/hub.js + create mode 100644 blocks_vertical/hub_status_light.js + create mode 100644 blocks_vertical/imu.js + create mode 100644 blocks_vertical/input.js + rename blocks_vertical/{ => upstream}/data.js (100%) + rename blocks_vertical/{ => upstream}/event.js (100%) + rename blocks_vertical/{ => upstream}/extensions.js (100%) + rename blocks_vertical/{ => upstream}/looks.js (100%) + rename blocks_vertical/{ => upstream}/motion.js (100%) + rename blocks_vertical/{ => upstream}/procedures.js (100%) + rename blocks_vertical/{ => upstream}/sensing.js (100%) + rename blocks_vertical/{ => upstream}/sound.js (100%) + +diff --git a/.idea/.gitignore b/.idea/.gitignore +new file mode 100644 +index 00000000..b58b603f +--- /dev/null ++++ b/.idea/.gitignore +@@ -0,0 +1,5 @@ ++# Default ignored files ++/shelf/ ++/workspace.xml ++# Editor-based HTTP Client requests ++/httpRequests/ +diff --git a/blocks_vertical/control.js b/blocks_vertical/control.js +index e5bb20b5..ac42c4bf 100644 +--- a/blocks_vertical/control.js ++++ b/blocks_vertical/control.js +@@ -175,27 +175,27 @@ Blockly.Blocks['control_stop'] = { + * @this Blockly.Block + */ + init: function() { +- var ALL_SCRIPTS = 'all'; +- var THIS_SCRIPT = 'this script'; +- var OTHER_SCRIPTS = 'other scripts in sprite'; ++ var AND_EXIT_PROGRAM = 'and exit program'; ++ var AND_RESTART_PROGRAM = 'and restart program'; ++ var AND_SHUTDOWN_HUB = 'and turn off hub'; + var stopDropdown = new Blockly.FieldDropdown(function() { + if (this.sourceBlock_ && + this.sourceBlock_.nextConnection && + this.sourceBlock_.nextConnection.isConnected()) { +- return [ +- [Blockly.Msg.CONTROL_STOP_OTHER, OTHER_SCRIPTS] +- ]; ++ return [[Blockly.Msg.CONTROL_STOP_EXIT, AND_EXIT_PROGRAM], ++ [Blockly.Msg.CONTROL_STOP_RESTART, AND_RESTART_PROGRAM], ++ [Blockly.Msg.CONTROL_STOP_SHUTDOWN, AND_SHUTDOWN_HUB]]; + } +- return [[Blockly.Msg.CONTROL_STOP_ALL, ALL_SCRIPTS], +- [Blockly.Msg.CONTROL_STOP_THIS, THIS_SCRIPT], +- [Blockly.Msg.CONTROL_STOP_OTHER, OTHER_SCRIPTS] ++ return [[Blockly.Msg.CONTROL_STOP_EXIT, AND_EXIT_PROGRAM], ++ [Blockly.Msg.CONTROL_STOP_RESTART, AND_RESTART_PROGRAM], ++ [Blockly.Msg.CONTROL_STOP_SHUTDOWN, AND_SHUTDOWN_HUB] + ]; + }, function(option) { + // Create an event group to keep field value and mutator in sync + // Return null at the end because setValue is called here already. + Blockly.Events.setGroup(true); + var oldMutation = Blockly.Xml.domToText(this.sourceBlock_.mutationToDom()); +- this.sourceBlock_.setNextStatement(option == OTHER_SCRIPTS); ++ this.sourceBlock_.setNextStatement(option == AND_SHUTDOWN_HUB); + var newMutation = Blockly.Xml.domToText(this.sourceBlock_.mutationToDom()); + Blockly.Events.fire(new Blockly.Events.BlockChange(this.sourceBlock_, + 'mutation', null, oldMutation, newMutation)); +diff --git a/blocks_vertical/default_toolbox.js b/blocks_vertical/default_toolbox.js +index 8c5624fa..8516e0db 100644 +--- a/blocks_vertical/default_toolbox.js ++++ b/blocks_vertical/default_toolbox.js +@@ -28,6 +28,248 @@ goog.require('Blockly.Blocks'); + * @fileoverview Provide a default toolbox XML. + */ + ++Blockly.Blocks.defaultToolbox = ''; ++ ++/* + Blockly.Blocks.defaultToolbox = '