Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
623e57a30e | |||
91235fb6d3 | |||
4a73c5d700 | |||
a47a06ac19 | |||
3f598a3eee | |||
ded2e9d82c |
@ -27,7 +27,6 @@ while (true) {
|
||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
|
||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
|
||||
}
|
||||
pause(1);
|
||||
}
|
||||
```
|
||||
|
||||
@ -43,6 +42,5 @@ while (true) {
|
||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
|
||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
|
||||
}
|
||||
pause(1);
|
||||
}
|
||||
```
|
@ -64,6 +64,7 @@ class WebSerialPackageIO implements pxt.HF2.PacketIO {
|
||||
private _writer: any;
|
||||
|
||||
constructor(private port: SerialPort, private options: SerialOptions) {
|
||||
console.log(`serial: new io`)
|
||||
}
|
||||
|
||||
async readSerialAsync() {
|
||||
@ -90,17 +91,24 @@ class WebSerialPackageIO implements pxt.HF2.PacketIO {
|
||||
return !!(<any>navigator).serial;
|
||||
}
|
||||
|
||||
static portIos: WebSerialPackageIO[] = [];
|
||||
static async mkPacketIOAsync(): Promise<pxt.HF2.PacketIO> {
|
||||
const serial = (<any>navigator).serial;
|
||||
if (serial) {
|
||||
try {
|
||||
const requestOptions: SerialPortRequestOptions = {};
|
||||
const port = await serial.requestPort(requestOptions);
|
||||
const options: SerialOptions = {
|
||||
baudrate: 460800,
|
||||
buffersize: 4096
|
||||
};
|
||||
return new WebSerialPackageIO(port, options);
|
||||
|
||||
let io = WebSerialPackageIO.portIos.filter(i => i.port == port)[0];
|
||||
if (!io) {
|
||||
const options: SerialOptions = {
|
||||
baudrate: 460800,
|
||||
buffersize: 4096
|
||||
};
|
||||
io = new WebSerialPackageIO(port, options);
|
||||
WebSerialPackageIO.portIos.push(io);
|
||||
}
|
||||
return io;
|
||||
} catch (e) {
|
||||
console.log(`connection error`, e)
|
||||
}
|
||||
@ -125,11 +133,8 @@ class WebSerialPackageIO implements pxt.HF2.PacketIO {
|
||||
});
|
||||
}
|
||||
|
||||
private closeAsync() {
|
||||
console.log(`serial: closing port`);
|
||||
this.port.close();
|
||||
this._reader = undefined;
|
||||
this._writer = undefined;
|
||||
private async closeAsync() {
|
||||
// don't close port
|
||||
return Promise.delay(500);
|
||||
}
|
||||
|
||||
|
11
libs/core/cooperate.ts
Normal file
11
libs/core/cooperate.ts
Normal file
@ -0,0 +1,11 @@
|
||||
namespace control {
|
||||
let lastPause = 0;
|
||||
const COOPERATION_INTERVAL = 20
|
||||
export function cooperate() {
|
||||
const now = control.millis()
|
||||
if (now - lastPause > COOPERATION_INTERVAL) {
|
||||
lastPause = now
|
||||
pause(1)
|
||||
}
|
||||
}
|
||||
}
|
@ -21,8 +21,7 @@ namespace sensors.internal {
|
||||
const now = control.millis();
|
||||
if (now - this.lastQuery >= this.interval * 2)
|
||||
this.queryAndUpdate(); // sensor poller is not allowed to run
|
||||
if (now - this.lastPause >= this.interval * 5)
|
||||
pause(1); // allow events to trigger
|
||||
control.cooperate(); // allow events to trigger
|
||||
}
|
||||
|
||||
private queryAndUpdate() {
|
||||
|
@ -124,7 +124,7 @@ namespace motors {
|
||||
export function stopAll() {
|
||||
const b = mkCmd(Output.ALL, DAL.opOutputStop, 0)
|
||||
writePWM(b);
|
||||
pause(1);
|
||||
control.cooperate();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -136,7 +136,7 @@ namespace motors {
|
||||
//% help=motors/reset-all
|
||||
export function resetAll() {
|
||||
reset(Output.ALL)
|
||||
pause(1);
|
||||
control.cooperate();
|
||||
}
|
||||
|
||||
interface MoveSchedule {
|
||||
@ -269,7 +269,7 @@ namespace motors {
|
||||
if (this._brake && this._brakeSettleTime > 0)
|
||||
pause(this._brakeSettleTime);
|
||||
else {
|
||||
pause(1);
|
||||
control.cooperate();
|
||||
}
|
||||
}
|
||||
|
||||
@ -280,7 +280,7 @@ namespace motors {
|
||||
// allow robot to settle
|
||||
this.settle();
|
||||
} else {
|
||||
pause(1);
|
||||
control.cooperate();
|
||||
}
|
||||
}
|
||||
|
||||
@ -357,7 +357,7 @@ namespace motors {
|
||||
// special: 0 is infinity
|
||||
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
|
||||
this._run(schedule.speed);
|
||||
pause(1);
|
||||
control.cooperate();
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,8 @@
|
||||
"icons.jres",
|
||||
"ns.ts",
|
||||
"platform.h",
|
||||
"integrator.ts"
|
||||
"integrator.ts",
|
||||
"cooperate.ts"
|
||||
],
|
||||
"testFiles": [
|
||||
"test.ts"
|
||||
|
@ -45,7 +45,7 @@ void setVolume(int volume) {
|
||||
|
||||
struct ToneCmd {
|
||||
uint8_t cmd;
|
||||
uint8_t vol;
|
||||
uint8_t lvl;
|
||||
uint16_t freq;
|
||||
uint16_t duration;
|
||||
};
|
||||
@ -55,10 +55,26 @@ static void _stopSound() {
|
||||
writeDev(&cmd, sizeof(cmd));
|
||||
}
|
||||
|
||||
static uint8_t _getVolumeLevel(uint8_t volume, uint8_t levels) {
|
||||
uint8_t level;
|
||||
uint8_t step = (uint8_t) (100 / (levels - 1));
|
||||
if (volume < step) {
|
||||
level = 0;
|
||||
} else if (volume > step * (levels - 1)) {
|
||||
level = levels;
|
||||
} else {
|
||||
level = (uint8_t) (volume / step);
|
||||
}
|
||||
return level;
|
||||
}
|
||||
|
||||
static void _playTone(uint16_t frequency, uint16_t duration, uint8_t volume) {
|
||||
// https://github.com/mindboards/ev3sources/blob/78ebaf5b6f8fe31cc17aa5dce0f8e4916a4fc072/lms2012/c_sound/source/c_sound.c#L471
|
||||
uint8_t level = _getVolumeLevel(volume, 13);
|
||||
|
||||
ToneCmd cmd;
|
||||
cmd.cmd = SOUND_CMD_TONE;
|
||||
cmd.vol = volume;
|
||||
cmd.lvl = level;
|
||||
cmd.freq = frequency;
|
||||
cmd.duration = duration;
|
||||
// (*SoundInstance.pSound).Busy = TRUE;
|
||||
@ -122,7 +138,8 @@ void playSample(Buffer buf) {
|
||||
stopUser();
|
||||
pthread_mutex_lock(&pumpMutex);
|
||||
*lmsSoundMMap = 1; // BUSY
|
||||
uint8_t cmd[] = {SOUND_CMD_PLAY, (uint8_t)((currVolume / 33) + 1)};
|
||||
// https://github.com/mindboards/ev3sources/blob/78ebaf5b6f8fe31cc17aa5dce0f8e4916a4fc072/lms2012/c_sound/source/c_sound.c#L605
|
||||
uint8_t cmd[] = {SOUND_CMD_PLAY, _getVolumeLevel(currVolume, 8)};
|
||||
writeDev(cmd, 2);
|
||||
decrRC(currentSample);
|
||||
currentSample = buf;
|
||||
|
5105
package-lock.json
generated
Normal file
5105
package-lock.json
generated
Normal file
File diff suppressed because it is too large
Load Diff
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pxt-ev3",
|
||||
"version": "1.2.26",
|
||||
"version": "1.2.29",
|
||||
"description": "LEGO MINDSTORMS EV3 for Microsoft MakeCode",
|
||||
"private": false,
|
||||
"keywords": [
|
||||
|
Reference in New Issue
Block a user