Compare commits
4 Commits
Author | SHA1 | Date | |
---|---|---|---|
59ca9cf463 | |||
7da811246c | |||
69f8453947 | |||
39ba9b81af |
14
libs/behaviors/_locales/behaviors-jsdoc-strings.json
Normal file
14
libs/behaviors/_locales/behaviors-jsdoc-strings.json
Normal file
@ -0,0 +1,14 @@
|
||||
{
|
||||
"behaviors": "Behavior drive blocks",
|
||||
"behaviors.Behavior": "A behavior",
|
||||
"behaviors.BehaviorManager": "A manager for behaviors",
|
||||
"behaviors.BehaviorManager.add": "Adds a new behavior to the behavior manager",
|
||||
"behaviors.BehaviorManager.add|param|behavior": "the behavior to add",
|
||||
"behaviors.BehaviorManager.start": "Starts the behavior control loop",
|
||||
"behaviors.BehaviorManager.stop": "Stops the execution loop",
|
||||
"behaviors.addBehavior": "Adds the behavior and starts it",
|
||||
"behaviors.addBehavior|param|behavior": "a behavior",
|
||||
"behaviors.avoidCrash": "A behavior that stops all motors if the sensor distance get too short",
|
||||
"behaviors.driveForward": "A behavior that turns on the motors to the specified speed",
|
||||
"behaviors.driveForward|param|motors": "@param speed the desired speed, eg: 50"
|
||||
}
|
7
libs/behaviors/_locales/behaviors-strings.json
Normal file
7
libs/behaviors/_locales/behaviors-strings.json
Normal file
@ -0,0 +1,7 @@
|
||||
{
|
||||
"behaviors.addBehavior|block": "add behavior %behavior",
|
||||
"behaviors.avoidCrash|block": "avoid crash using %ultrasonic",
|
||||
"behaviors.driveForward|block": "drive %motors|forward at %speed|%",
|
||||
"behaviors|block": "behaviors",
|
||||
"{id:category}Behaviors": "Behaviors"
|
||||
}
|
6
libs/behaviors/pxt.json
Normal file
6
libs/behaviors/pxt.json
Normal file
@ -0,0 +1,6 @@
|
||||
{
|
||||
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/behaviors",
|
||||
"dependencies": {
|
||||
"core": "file:../ev3"
|
||||
}
|
||||
}
|
56
libs/behaviors/targetoverrides.ts
Normal file
56
libs/behaviors/targetoverrides.ts
Normal file
@ -0,0 +1,56 @@
|
||||
namespace behaviors {
|
||||
class AvoidCrashBehavior extends behaviors.Behavior {
|
||||
private ultrasonic: sensors.UltraSonicSensor;
|
||||
constructor(ultrasonic: sensors.UltraSonicSensor) {
|
||||
super();
|
||||
this.ultrasonic = ultrasonic;
|
||||
}
|
||||
|
||||
shouldRun(): boolean {
|
||||
return this.ultrasonic.distance() < 5;
|
||||
}
|
||||
|
||||
run(): void {
|
||||
motors.stopAllMotors();
|
||||
this.active = false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A behavior that stops all motors if the sensor distance get too short
|
||||
*/
|
||||
//% blockId=behaviorsAvoidCrash block="avoid crash using %ultrasonic"
|
||||
export function avoidCrash(ultrasonic: sensors.UltraSonicSensor) : behaviors.Behavior {
|
||||
return new AvoidCrashBehavior(ultrasonic);
|
||||
}
|
||||
|
||||
class DriveForwardBehavior extends behaviors.Behavior {
|
||||
private motors: motors.MotorBase;
|
||||
private speed: number;
|
||||
constructor(motors: motors.MotorBase, speed: number) {
|
||||
super();
|
||||
this.motors = motors;
|
||||
this.speed = speed;
|
||||
}
|
||||
|
||||
shouldRun(): boolean {
|
||||
return true;
|
||||
}
|
||||
|
||||
run(): void {
|
||||
this.motors.setSpeed(this.speed);
|
||||
pauseUntil(() => !this.active);
|
||||
this.motors.setSpeed(0);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* A behavior that turns on the motors to the specified speed
|
||||
* @param motors
|
||||
* @param speed the desired speed, eg: 50
|
||||
*/
|
||||
//% blockId=behaviorsDriveForward block="drive %motors|forward at %speed|%"
|
||||
export function driveForward(motors: motors.MotorBase, speed: number): behaviors.Behavior {
|
||||
return new DriveForwardBehavior(motors, speed);
|
||||
}
|
||||
}
|
@ -343,7 +343,7 @@ namespace motors {
|
||||
* Gets motor angle.
|
||||
* @param motor the port which connects to the motor
|
||||
*/
|
||||
//% blockId=motorTachoCount block="%motor|angle"
|
||||
//% blockId=motorAngle block="%motor|angle"
|
||||
//% weight=70
|
||||
//% group="Sensors"
|
||||
angle(): number {
|
||||
@ -445,7 +445,7 @@ namespace motors {
|
||||
private __setSpeed(speed: number) {
|
||||
syncMotors(this._port, {
|
||||
speed: speed,
|
||||
turnRatio: 0,
|
||||
turnRatio: 100, // same speed
|
||||
useBrake: !!this._brake
|
||||
})
|
||||
}
|
||||
|
@ -19,6 +19,10 @@ namespace sensors {
|
||||
namespace motors {
|
||||
}
|
||||
|
||||
//% labelLineWidth=0
|
||||
namespace behaviors {
|
||||
}
|
||||
|
||||
//% color="#D67923" weight=80 icon="\uf10e"
|
||||
namespace music {
|
||||
|
||||
|
@ -266,8 +266,10 @@ namespace music {
|
||||
//% blockId=music_play_sound_effect_until_done block="play sound effect %sound|until done"
|
||||
//% weight=98 blockGap=8
|
||||
export function playSoundEffectUntilDone(sound: Sound) {
|
||||
if (!sound) return;
|
||||
if (!sound || numSoundsPlaying >= soundsLimit) return;
|
||||
numSoundsPlaying++;
|
||||
sound.play();
|
||||
numSoundsPlaying--;
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -1,16 +1,6 @@
|
||||
{
|
||||
"name": "tests",
|
||||
"description": "A unit test library",
|
||||
"files": [
|
||||
"README.md",
|
||||
"tests.ts",
|
||||
"platformoverrides.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
],
|
||||
"public": true,
|
||||
"additionalFilePath": "../../node_modules/pxt-common-packages/libs/tests",
|
||||
"dependencies": {
|
||||
"core": "file:../core"
|
||||
"core": "file:../ev3"
|
||||
}
|
||||
}
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pxt-ev3",
|
||||
"version": "0.0.51",
|
||||
"version": "0.0.52",
|
||||
"description": "LEGO Mindstorms EV3 for Microsoft MakeCode",
|
||||
"private": true,
|
||||
"keywords": [
|
||||
@ -44,7 +44,7 @@
|
||||
"webfonts-generator": "^0.4.0"
|
||||
},
|
||||
"dependencies": {
|
||||
"pxt-common-packages": "0.15.1",
|
||||
"pxt-common-packages": "0.15.3",
|
||||
"pxt-core": "3.0.5"
|
||||
},
|
||||
"scripts": {
|
||||
|
@ -16,7 +16,8 @@
|
||||
"libs/infrared-sensor",
|
||||
"libs/gyro-sensor",
|
||||
"libs/ev3",
|
||||
"libs/tests"
|
||||
"libs/tests",
|
||||
"libs/behaviors"
|
||||
],
|
||||
"simulator": {
|
||||
"autoRun": true,
|
||||
|
@ -10,8 +10,6 @@ namespace pxsim.music {
|
||||
}
|
||||
|
||||
namespace pxsim.SoundMethods {
|
||||
let numSoundsPlaying = 0;
|
||||
const soundsLimit = 1;
|
||||
let audio: HTMLAudioElement;
|
||||
|
||||
export function buffer(buf: RefBuffer) {
|
||||
@ -27,18 +25,15 @@ namespace pxsim.SoundMethods {
|
||||
}
|
||||
|
||||
export function play(buf: RefBuffer, volume: number) {
|
||||
if (!buf || numSoundsPlaying >= soundsLimit) {
|
||||
if (!buf) {
|
||||
return Promise.resolve();
|
||||
}
|
||||
return new Promise<void>(resolve => {
|
||||
let url = "data:audio/wav;base64," + btoa(uint8ArrayToString(buf.data))
|
||||
audio = new Audio(url)
|
||||
audio.onended = () => {
|
||||
resolve();
|
||||
numSoundsPlaying--;
|
||||
}
|
||||
numSoundsPlaying++;
|
||||
audio.play()
|
||||
audio.onended = () => resolve();
|
||||
audio.onpause = () => resolve();
|
||||
audio.play();
|
||||
})
|
||||
}
|
||||
|
||||
@ -46,8 +41,8 @@ namespace pxsim.SoundMethods {
|
||||
return new Promise<void>(resolve => {
|
||||
if (audio) {
|
||||
audio.pause();
|
||||
numSoundsPlaying--;
|
||||
}
|
||||
resolve();
|
||||
})
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user