Compare commits
12 Commits
Author | SHA1 | Date |
---|---|---|
Peli de Halleux | 4bea8e8712 | |
Peli de Halleux | 0af6182f23 | |
peli | 398e53bae4 | |
peli | 33e6372bf0 | |
Maciej Mroziński | 83bba14a4d | |
Maciej Mroziński | 0d7c1f0d50 | |
peli | 623e57a30e | |
Maciej Mroziński | 91235fb6d3 | |
Peli de Halleux | 4a73c5d700 | |
Peli de Halleux | a47a06ac19 | |
Peli | 3f598a3eee | |
Peli | ded2e9d82c |
|
@ -27,7 +27,6 @@ while (true) {
|
||||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
|
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
|
||||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
|
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
|
||||||
}
|
}
|
||||||
pause(1);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -43,6 +42,5 @@ while (true) {
|
||||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
|
music.playSoundEffectUntilDone(sounds.mechanicalMotorStart)
|
||||||
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
|
music.playSoundEffectUntilDone(sounds.mechanicalMotorIdle);
|
||||||
}
|
}
|
||||||
pause(1);
|
|
||||||
}
|
}
|
||||||
```
|
```
|
|
@ -1,3 +1,3 @@
|
||||||
{
|
{
|
||||||
"appref": "v1.2.22"
|
"appref": "v1.2.30"
|
||||||
}
|
}
|
||||||
|
|
|
@ -64,6 +64,7 @@ class WebSerialPackageIO implements pxt.HF2.PacketIO {
|
||||||
private _writer: any;
|
private _writer: any;
|
||||||
|
|
||||||
constructor(private port: SerialPort, private options: SerialOptions) {
|
constructor(private port: SerialPort, private options: SerialOptions) {
|
||||||
|
console.log(`serial: new io`)
|
||||||
}
|
}
|
||||||
|
|
||||||
async readSerialAsync() {
|
async readSerialAsync() {
|
||||||
|
@ -90,17 +91,24 @@ class WebSerialPackageIO implements pxt.HF2.PacketIO {
|
||||||
return !!(<any>navigator).serial;
|
return !!(<any>navigator).serial;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static portIos: WebSerialPackageIO[] = [];
|
||||||
static async mkPacketIOAsync(): Promise<pxt.HF2.PacketIO> {
|
static async mkPacketIOAsync(): Promise<pxt.HF2.PacketIO> {
|
||||||
const serial = (<any>navigator).serial;
|
const serial = (<any>navigator).serial;
|
||||||
if (serial) {
|
if (serial) {
|
||||||
try {
|
try {
|
||||||
const requestOptions: SerialPortRequestOptions = {};
|
const requestOptions: SerialPortRequestOptions = {};
|
||||||
const port = await serial.requestPort(requestOptions);
|
const port = await serial.requestPort(requestOptions);
|
||||||
const options: SerialOptions = {
|
|
||||||
baudrate: 460800,
|
let io = WebSerialPackageIO.portIos.filter(i => i.port == port)[0];
|
||||||
buffersize: 4096
|
if (!io) {
|
||||||
};
|
const options: SerialOptions = {
|
||||||
return new WebSerialPackageIO(port, options);
|
baudrate: 460800,
|
||||||
|
buffersize: 4096
|
||||||
|
};
|
||||||
|
io = new WebSerialPackageIO(port, options);
|
||||||
|
WebSerialPackageIO.portIos.push(io);
|
||||||
|
}
|
||||||
|
return io;
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.log(`connection error`, e)
|
console.log(`connection error`, e)
|
||||||
}
|
}
|
||||||
|
@ -125,11 +133,8 @@ class WebSerialPackageIO implements pxt.HF2.PacketIO {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private closeAsync() {
|
private async closeAsync() {
|
||||||
console.log(`serial: closing port`);
|
// don't close port
|
||||||
this.port.close();
|
|
||||||
this._reader = undefined;
|
|
||||||
this._writer = undefined;
|
|
||||||
return Promise.delay(500);
|
return Promise.delay(500);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -116,7 +116,7 @@ export class Ev3Wrapper {
|
||||||
if (this.dataDump)
|
if (this.dataDump)
|
||||||
log("TALK: " + U.toHex(buf))
|
log("TALK: " + U.toHex(buf))
|
||||||
return this.io.sendPacketAsync(buf)
|
return this.io.sendPacketAsync(buf)
|
||||||
.then(() => this.msgs.shiftAsync(1000))
|
.then(() => this.msgs.shiftAsync(5000))
|
||||||
.then(resp => {
|
.then(resp => {
|
||||||
if (resp[2] != buf[2] || resp[3] != buf[3])
|
if (resp[2] != buf[2] || resp[3] != buf[3])
|
||||||
U.userError("msg count de-sync")
|
U.userError("msg count de-sync")
|
||||||
|
|
|
@ -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();
|
const now = control.millis();
|
||||||
if (now - this.lastQuery >= this.interval * 2)
|
if (now - this.lastQuery >= this.interval * 2)
|
||||||
this.queryAndUpdate(); // sensor poller is not allowed to run
|
this.queryAndUpdate(); // sensor poller is not allowed to run
|
||||||
if (now - this.lastPause >= this.interval * 5)
|
control.cooperate(); // allow events to trigger
|
||||||
pause(1); // allow events to trigger
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private queryAndUpdate() {
|
private queryAndUpdate() {
|
||||||
|
|
|
@ -124,7 +124,7 @@ namespace motors {
|
||||||
export function stopAll() {
|
export function stopAll() {
|
||||||
const b = mkCmd(Output.ALL, DAL.opOutputStop, 0)
|
const b = mkCmd(Output.ALL, DAL.opOutputStop, 0)
|
||||||
writePWM(b);
|
writePWM(b);
|
||||||
pause(1);
|
control.cooperate();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -136,7 +136,7 @@ namespace motors {
|
||||||
//% help=motors/reset-all
|
//% help=motors/reset-all
|
||||||
export function resetAll() {
|
export function resetAll() {
|
||||||
reset(Output.ALL)
|
reset(Output.ALL)
|
||||||
pause(1);
|
control.cooperate();
|
||||||
}
|
}
|
||||||
|
|
||||||
interface MoveSchedule {
|
interface MoveSchedule {
|
||||||
|
@ -269,7 +269,7 @@ namespace motors {
|
||||||
if (this._brake && this._brakeSettleTime > 0)
|
if (this._brake && this._brakeSettleTime > 0)
|
||||||
pause(this._brakeSettleTime);
|
pause(this._brakeSettleTime);
|
||||||
else {
|
else {
|
||||||
pause(1);
|
control.cooperate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -280,7 +280,7 @@ namespace motors {
|
||||||
// allow robot to settle
|
// allow robot to settle
|
||||||
this.settle();
|
this.settle();
|
||||||
} else {
|
} else {
|
||||||
pause(1);
|
control.cooperate();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -357,7 +357,7 @@ namespace motors {
|
||||||
// special: 0 is infinity
|
// special: 0 is infinity
|
||||||
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
|
if (schedule.steps[0] + schedule.steps[1] + schedule.steps[2] == 0) {
|
||||||
this._run(schedule.speed);
|
this._run(schedule.speed);
|
||||||
pause(1);
|
control.cooperate();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -26,7 +26,8 @@
|
||||||
"icons.jres",
|
"icons.jres",
|
||||||
"ns.ts",
|
"ns.ts",
|
||||||
"platform.h",
|
"platform.h",
|
||||||
"integrator.ts"
|
"integrator.ts",
|
||||||
|
"cooperate.ts"
|
||||||
],
|
],
|
||||||
"testFiles": [
|
"testFiles": [
|
||||||
"test.ts"
|
"test.ts"
|
||||||
|
|
|
@ -12,4 +12,5 @@ music.setTempo(120)
|
||||||
music.noteFrequency(Note.C)
|
music.noteFrequency(Note.C)
|
||||||
music.beat()
|
music.beat()
|
||||||
music.setVolume(50)
|
music.setVolume(50)
|
||||||
```
|
music.volume()
|
||||||
|
```
|
||||||
|
|
|
@ -13,7 +13,22 @@
|
||||||
|
|
||||||
namespace music {
|
namespace music {
|
||||||
|
|
||||||
uint8_t currVolume = 50;
|
int _readSystemVolume() {
|
||||||
|
char ParBuf[8];
|
||||||
|
int volume;
|
||||||
|
|
||||||
|
int fd = open("../sys/settings/Volume.rtf", O_RDONLY);
|
||||||
|
read(fd, ParBuf, sizeof(ParBuf));
|
||||||
|
close(fd);
|
||||||
|
if (sscanf(ParBuf,"%d",&volume) > 0) {
|
||||||
|
if ((volume >= 0) && (volume <= 100)) {
|
||||||
|
return volume;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return 50;
|
||||||
|
}
|
||||||
|
|
||||||
|
uint8_t currVolume = _readSystemVolume();
|
||||||
uint8_t *lmsSoundMMap;
|
uint8_t *lmsSoundMMap;
|
||||||
|
|
||||||
int writeDev(void *data, int size) {
|
int writeDev(void *data, int size) {
|
||||||
|
@ -37,6 +52,18 @@ void setVolume(int volume) {
|
||||||
currVolume = max(0, min(100, volume));
|
currVolume = max(0, min(100, volume));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the output volume of the sound synthesizer.
|
||||||
|
*/
|
||||||
|
//% weight=96
|
||||||
|
//% blockId=synth_get_volume block="volume"
|
||||||
|
//% parts="speaker" blockGap=8
|
||||||
|
//% help=music/volume
|
||||||
|
//% weight=1
|
||||||
|
int volume() {
|
||||||
|
return currVolume;
|
||||||
|
}
|
||||||
|
|
||||||
#define SOUND_CMD_BREAK 0
|
#define SOUND_CMD_BREAK 0
|
||||||
#define SOUND_CMD_TONE 1
|
#define SOUND_CMD_TONE 1
|
||||||
#define SOUND_CMD_PLAY 2
|
#define SOUND_CMD_PLAY 2
|
||||||
|
@ -45,7 +72,7 @@ void setVolume(int volume) {
|
||||||
|
|
||||||
struct ToneCmd {
|
struct ToneCmd {
|
||||||
uint8_t cmd;
|
uint8_t cmd;
|
||||||
uint8_t vol;
|
uint8_t lvl;
|
||||||
uint16_t freq;
|
uint16_t freq;
|
||||||
uint16_t duration;
|
uint16_t duration;
|
||||||
};
|
};
|
||||||
|
@ -55,10 +82,26 @@ static void _stopSound() {
|
||||||
writeDev(&cmd, sizeof(cmd));
|
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) {
|
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;
|
ToneCmd cmd;
|
||||||
cmd.cmd = SOUND_CMD_TONE;
|
cmd.cmd = SOUND_CMD_TONE;
|
||||||
cmd.vol = volume;
|
cmd.lvl = level;
|
||||||
cmd.freq = frequency;
|
cmd.freq = frequency;
|
||||||
cmd.duration = duration;
|
cmd.duration = duration;
|
||||||
// (*SoundInstance.pSound).Busy = TRUE;
|
// (*SoundInstance.pSound).Busy = TRUE;
|
||||||
|
@ -122,7 +165,8 @@ void playSample(Buffer buf) {
|
||||||
stopUser();
|
stopUser();
|
||||||
pthread_mutex_lock(&pumpMutex);
|
pthread_mutex_lock(&pumpMutex);
|
||||||
*lmsSoundMMap = 1; // BUSY
|
*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);
|
writeDev(cmd, 2);
|
||||||
decrRC(currentSample);
|
decrRC(currentSample);
|
||||||
currentSample = buf;
|
currentSample = buf;
|
||||||
|
@ -201,4 +245,4 @@ Buffer buffer(Sound snd) {
|
||||||
void play(Sound snd) {
|
void play(Sound snd) {
|
||||||
music::playSample(snd);
|
music::playSample(snd);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -13,6 +13,16 @@ declare namespace music {
|
||||||
//% weight=1 shim=music::setVolume
|
//% weight=1 shim=music::setVolume
|
||||||
function setVolume(volume: int32): void;
|
function setVolume(volume: int32): void;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the output volume of the sound synthesizer.
|
||||||
|
*/
|
||||||
|
//% weight=96
|
||||||
|
//% blockId=synth_get_volume block="volume"
|
||||||
|
//% parts="speaker" blockGap=8
|
||||||
|
//% help=music/volume
|
||||||
|
//% weight=1 shim=music::volume
|
||||||
|
function volume(): int32;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Play a tone through the speaker for some amount of time.
|
* Play a tone through the speaker for some amount of time.
|
||||||
* @param frequency pitch of the tone to play in Hertz (Hz), eg: Note.C
|
* @param frequency pitch of the tone to play in Hertz (Hz), eg: Note.C
|
||||||
|
|
|
@ -5,3 +5,4 @@ music.playTone(1440, 500)
|
||||||
pause(500)
|
pause(500)
|
||||||
music.playTone(2440, 500)
|
music.playTone(2440, 500)
|
||||||
pause(500)
|
pause(500)
|
||||||
|
music.volume()
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "pxt-ev3",
|
"name": "pxt-ev3",
|
||||||
"version": "1.2.26",
|
"version": "1.2.31",
|
||||||
"description": "LEGO MINDSTORMS EV3 for Microsoft MakeCode",
|
"description": "LEGO MINDSTORMS EV3 for Microsoft MakeCode",
|
||||||
"private": false,
|
"private": false,
|
||||||
"keywords": [
|
"keywords": [
|
||||||
|
|
|
@ -108,7 +108,8 @@
|
||||||
"de",
|
"de",
|
||||||
"ja",
|
"ja",
|
||||||
"ru",
|
"ru",
|
||||||
"zh-CN"
|
"zh-CN",
|
||||||
|
"fr"
|
||||||
],
|
],
|
||||||
"highContrast": true,
|
"highContrast": true,
|
||||||
"lightToc": true,
|
"lightToc": true,
|
||||||
|
|
|
@ -7,6 +7,14 @@ namespace pxsim.music {
|
||||||
export function stopAllSounds() {
|
export function stopAllSounds() {
|
||||||
SoundMethods.stop()
|
SoundMethods.stop()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pxsim.music.setVolume = (volume: number): void => {
|
||||||
|
pxsim.getAudioState().volume = volume;
|
||||||
|
};
|
||||||
|
|
||||||
|
export function volume() {
|
||||||
|
return pxsim.getAudioState().volume;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
namespace pxsim.SoundMethods {
|
namespace pxsim.SoundMethods {
|
||||||
|
|
Loading…
Reference in New Issue