Compare commits

..

3 Commits

Author SHA1 Message Date
40aaf0fb18 1.4.22 2020-09-08 23:54:42 -07:00
b26cf289c3 Change sound volume to level (#986) 2020-09-08 23:53:33 -07:00
f0821f8d6c cherry-pick Cooperate (#985)
* cooperate pause

* fix math

* update lastPause before pausing

* faster cooperation

Co-authored-by: Peli de Halleux <peli@DESKTOP-5B7QRAM.corp.microsoft.com>
2020-08-21 06:09:01 +02:00
8 changed files with 50 additions and 30 deletions

View File

@ -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);
}
```

10
libs/base/shims.d.ts vendored
View File

@ -87,12 +87,6 @@ declare interface Buffer {
*/
//% shim=BufferMethods::write
write(dstOffset: int32, src: Buffer): void;
/**
* Compute k-bit FNV-1 non-cryptographic hash of the buffer.
*/
//% shim=BufferMethods::hash
hash(bits: int32): uint32;
}
declare namespace control {
@ -100,14 +94,14 @@ declare namespace control {
* Create a new zero-initialized buffer.
* @param size number of bytes in the buffer
*/
//% deprecated=1 shim=control::createBuffer
//% shim=control::createBuffer
function createBuffer(size: int32): Buffer;
/**
* Create a new buffer with UTF8-encoded string
* @param str the string to put in the buffer
*/
//% deprecated=1 shim=control::createBufferFromUTF8
//% shim=control::createBufferFromUTF8
function createBufferFromUTF8(str: string): Buffer;
}
declare namespace loops {

11
libs/core/cooperate.ts Normal file
View 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)
}
}
}

View File

@ -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() {

View File

@ -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;
}

View File

@ -28,7 +28,8 @@
"platform.h",
"platform.cpp",
"dmesg.cpp",
"integrator.ts"
"integrator.ts",
"cooperate.ts"
],
"testFiles": [
"test.ts"

View File

@ -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;

View File

@ -1,6 +1,6 @@
{
"name": "pxt-ev3",
"version": "1.4.21",
"version": "1.4.22",
"description": "LEGO MINDSTORMS EV3 for Microsoft MakeCode",
"private": false,
"keywords": [
@ -32,21 +32,21 @@
"docs/*/*/*.md"
],
"devDependencies": {
"typescript": "2.6.1",
"react": "16.8.3",
"semantic-ui-less": "2.2.14",
"@types/bluebird": "2.0.33",
"@types/marked": "0.3.0",
"@types/node": "8.0.53",
"webfonts-generator": "^0.4.0",
"@types/jquery": "3.2.16",
"@types/react": "16.0.25",
"@types/react-dom": "16.0.3",
"@types/web-bluetooth": "0.0.4",
"@types/bluebird": "^2.0.33",
"@types/marked": "^0.3.0",
"@types/node": "^9.3.0",
"semantic-ui-less": "2.2.14",
"typescript": "^3.7.5"
"@types/web-bluetooth": "0.0.4"
},
"dependencies": {
"pxt-common-packages": "8.1.3",
"pxt-core": "6.2.8"
"pxt-common-packages": "6.18.2",
"pxt-core": "5.32.3"
},
"scripts": {
"test": "node node_modules/pxt-core/built/pxt.js travis"