pxt-ev3/editor/extension.ts

85 lines
2.9 KiB
TypeScript
Raw Normal View History

2017-07-06 18:34:31 +02:00
/// <reference path="../node_modules/pxt-core/built/pxteditor.d.ts" />
// When require()d from node, bind the global pxt namespace
namespace pxt {
export const dummyExport = 1;
}
eval("if (typeof process === 'object' && process + '' === '[object process]') pxt = global.pxt")
namespace pxt.editor {
// this comes from aux/pxt.lms
2017-07-07 14:08:36 +02:00
const rbfTemplate = `
4c45474f580000006d000100000000001c000000000000000e000000821b038405018130813e8053
74617274696e672e2e2e0084006080XX00448581644886488405018130813e80427965210084000a
2017-07-07 14:08:36 +02:00
`
2017-07-06 18:34:31 +02:00
function hf2Async() {
return pxt.HF2.mkPacketIOAsync()
.then(h => {
let w = new Ev3Wrapper(h)
return w.reconnectAsync(true)
.then(() => w)
})
}
let initPromise: Promise<Ev3Wrapper>
function initAsync() {
if (!initPromise)
initPromise = hf2Async()
.catch(err => {
initPromise = null
return Promise.reject(err)
})
return initPromise
}
export function deployCoreAsync(resp: pxtc.CompileResult, isCli = false) {
let w: Ev3Wrapper
let elfPath = "../prjs/BrkProg_SAVE/binary.elf"
let rbfPath = "../prjs/BrkProg_SAVE/pxt0.rbf"
return initAsync()
.then(w_ => {
w = w_
if (w.isStreaming)
U.userError("please stop the program first")
return w.rmAsync(elfPath)
2017-07-06 18:58:15 +02:00
}).then(() => {
2017-07-06 18:34:31 +02:00
let f = U.stringToUint8Array(atob(resp.outfiles[pxt.outputName()]))
return w.flashAsync(elfPath, f)
2017-07-06 18:58:15 +02:00
}).then(() => {
2017-07-07 14:08:36 +02:00
let rbfHex = rbfTemplate
.replace(/\s+/g, "")
.replace("XX", U.toHex(U.stringToUint8Array(elfPath)))
2017-07-06 18:34:31 +02:00
let rbf = U.fromHex(rbfHex)
HF2.write16(rbf, 4, rbf.length)
return w.flashAsync(rbfPath, rbf)
2017-07-06 18:58:15 +02:00
}).then(() => {
return w.runAsync(rbfPath)
2017-07-06 18:34:31 +02:00
}).then(() => {
if (isCli)
return w.disconnectAsync()
else
return Promise.resolve()
})
}
initExtensionsAsync = function (opts: pxt.editor.ExtensionOptions): Promise<pxt.editor.ExtensionResult> {
2017-07-07 14:08:36 +02:00
pxt.debug('loading pxt-ev3 target extensions...')
2017-07-06 18:34:31 +02:00
const res: pxt.editor.ExtensionResult = {
deployCoreAsync,
};
2017-07-07 14:08:36 +02:00
initAsync()
/*
2017-07-07 14:08:36 +02:00
.then(w => w.streamFileAsync("/tmp/serial.txt", buf => {
let str = Util.fromUTF8(Util.uint8ArrayToString(buf))
window.postMessage({
type: 'serial',
id: 'n/a', // TODO
data: str
}, "*")
}))
*/
2017-07-06 18:34:31 +02:00
return Promise.resolve<pxt.editor.ExtensionResult>(res);
}
2017-07-06 18:58:15 +02:00
}