Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 16 additions & 6 deletions app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,23 @@ namespace microcode {
basic.pause(500)

const buf = this.load(SAVESLOT_AUTO)
let prog: ProgramDefn = undefined
if (buf) {
const prog = ProgramDefn.fromBuffer(new BufferReader(buf))
runProgram(prog)
try {
prog = ProgramDefn.fromBuffer(new BufferReader(buf))
} catch (e) {
console.log("invalid auto-save, clearing")
this.save(SAVESLOT_AUTO, new ProgramDefn().toBuffer())
}
}

this.host_ = new AppHost(this)

this.openHome()
if (!__screenhelpers.displayPresent()) {
if (prog) runProgram(prog)
}
else {
this.host_ = new AppHost(this)
this.openHome()
}
}

public save(slot: string, buf: Buffer) {
Expand Down Expand Up @@ -70,8 +79,9 @@ namespace microcode {
const wasRunning = isProgramRunning()
if (wasRunning) stopProgram()
settings.writeBuffer(SAVESLOT_AUTO, buf)
if (wasRunning)
if (wasRunning) {
runProgram(ProgramDefn.fromBuffer(new BufferReader(buf)))
}
}

export function stopProgram() {
Expand Down
1 change: 0 additions & 1 deletion apphost.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace microcode {
id: string | number,
nullIfMissing?: boolean,
): Bitmap | undefined {
if (id == "wordLogo") return wordLogo
const bitmap = icons.get(id, !!nullIfMissing)
if (bitmap) return bitmap
if (nullIfMissing) return undefined
Expand Down
11 changes: 6 additions & 5 deletions assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,9 @@ namespace microcode {
return null
}

// Not packable: apphost.ts returns this const directly for "wordLogo".
export const wordLogo = bmp`
//% packable
//% whenUsed
export const microcodeLogo = bmp`
.111111.......111111...1111.......................................................1111111.................................1111..................
11bbbbbb.....11bbbbbb.11bbbb....................................................111bbbbbbb1..............................11bbbb.................
1bbbbbbbb...11bbbbbbbf1bbbbbf..................................................11bbbbbbbbbbb.............................1bbbbbf................
Expand Down Expand Up @@ -216,7 +217,7 @@ namespace icondb {
})
*/

// Not packable: carImages in robot.ts returns this const directly.
//% packable whenUsed
export const arm_open = bmp`
. . . . . . . . . . c c c . . .
. . . . . . . . c c b b b c c .
Expand All @@ -236,7 +237,7 @@ namespace icondb {
. . . . . . . . . . c c c . . .
`

// Not packable: carImages in robot.ts returns this const directly.
//% packable whenUsed
export const arm_close = bmp`
. . . . . . . . . . . . . . . .
. . . . . . . . . . . . . . . .
Expand Down Expand Up @@ -316,7 +317,7 @@ namespace icondb {

//TODO: I think this is actually the Jacdac logo?
// user-interface-base/coreAssets.ts has the real microbitLogo
// Use "microbitLogo" and at the end of this function it will check user-interface-base and fetch it.
// Use "cd" and at the end of this function it will check user-interface-base and fetch it.

// Not packable: web-only art; whenUsed drops it from the device build.
//% whenUsed
Expand Down
2 changes: 1 addition & 1 deletion docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ namespace docs {
appendImage(images, "image", "home", screen())
renderIcons(images)
const samples = renderSamples(images)
appendImage(images, "image", "microcode", microcode.wordLogo)
appendImage(images, "image", "microcode", microcode.microcodeLogo)
appendImage(images, "image", "microbit", microcode.microbitLogo)
appendImage(
images,
Expand Down
75 changes: 39 additions & 36 deletions host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,22 @@ namespace microcode {
Tid.TID_FILTER_PIN_2,
]

const line2tids: IdMap = {
[robot.robots.RobotLineState.None]: Tid.TID_FILTER_LINE_NEITHER,
[robot.robots.RobotLineState.Left]: Tid.TID_FILTER_LINE_LEFT,
[robot.robots.RobotLineState.Right]: Tid.TID_FILTER_LINE_RIGHT,
[robot.robots.RobotLineState.Both]: Tid.TID_FILTER_LINE_BOTH,
[robot.robots.RobotLineState.LostLeft]:
Tid.TID_FILTER_LINE_NEITHER_LEFT,
[robot.robots.RobotLineState.LostRight]:
Tid.TID_FILTER_LINE_NEITHER_RIGHT,
}

export class MicrobitHost implements RuntimeHost {
/** Monotonic token observed by cancellable LED display work. */
private displayVersion = 0
private carWallValue = 0
private carLineValue = 0

constructor() {
this._handler = (s: number, f: number) => {}
Expand Down Expand Up @@ -90,7 +103,7 @@ namespace microcode {
//TODO: This is a hack to fix the onGesture events not working.
// In GDB we see that the CPP functions onGesture, MicrobitAccelerometer constructor and the LSM303Accelerometer constructor are invoked.
// In spite of this the __handler is not invoked. For some reason this basic.pause(0) fixes this issue (possibly because of it briefly yielding?)
basic.pause(0)
basic.pause(0) // this is due to the queueing problem in CODAL. see: https://github.com/microsoft/pxt-microbit/issues/7083
input.onGesture(g, () => {
this._handler(
Tid.TID_SENSOR_ACCELEROMETER,
Expand All @@ -99,9 +112,8 @@ namespace microcode {
})
})

radio.onReceivedNumber(radioNum => {
this._handler(Tid.TID_SENSOR_RADIO_RECEIVE, radioNum)
})
radio.setGroup(1)
radio.onReceivedNumber(radioNum => this.handleRadioNumber(radioNum))

input.onSound(DetectedSound.Loud, () => {
this._handler(Tid.TID_SENSOR_MICROPHONE, Tid.TID_FILTER_LOUD)
Expand Down Expand Up @@ -135,11 +147,31 @@ namespace microcode {
value = input.soundLevel()
max = 255
break
case Tid.TID_SENSOR_LINE:
value = this.carLineValue
break
}
if (!normalized) return value
return Math.abs(value) / (Math.abs(min) + max)
}

private handleRadioNumber(radioValue: number) {
const obstacleState = robot.robots.RobotCompactCommand.ObstacleState
const lineState = robot.robots.RobotCompactCommand.LineState

if (radioValue > obstacleState && radioValue < lineState) {
this.carWallValue = radioValue - obstacleState
this._handler(Tid.TID_SENSOR_CAR_WALL, this.carWallValue)
} else if (radioValue >= lineState) {
this.carLineValue = radioValue - lineState
const lineTid = line2tids[this.carLineValue]
if (lineTid !== undefined)
this._handler(Tid.TID_SENSOR_LINE, lineTid)
} else {
this._handler(Tid.TID_SENSOR_RADIO_RECEIVE, radioValue)
}
}

private _handler: (sensorTid: number, filter: number) => void
registerOnSensorEvent(
handler: (sensorTid: number, filter: number) => void,
Expand Down Expand Up @@ -220,6 +252,9 @@ namespace microcode {
music.PlaybackMode.UntilDone,
)
return
case Tid.TID_ACTUATOR_CAR:
if (param !== undefined) radio.sendNumber(param)
return
case Tid.TID_ACTUATOR_MUSIC:
music.stopAllSounds()
music.play(
Expand Down Expand Up @@ -290,35 +325,3 @@ namespace microcode {

export const runtimeHost: RuntimeHost = new MicrobitHost()
}

/*

// TODO: convert this to external sensor with events and values
// TODO: and lift out
const radioVal = this.getRadioVal()
if (
sensor == Tid.TID_SENSOR_CAR_WALL ||
sensor == Tid.TID_SENSOR_LINE
) {
// this hack separates radio ranges used to communicate with robot car
if (
robot.robots.RobotCompactCommand.ObstacleState <
radioVal
)
if (sensor == Tid.TID_SENSOR_CAR_WALL)
return this.filterOnEvent(
radioVal -
robot.robots.RobotCompactCommand
.ObstacleState
)
else if (
robot.robots.RobotCompactCommand.LineState <=
radioVal
)
return this.filterOnEvent(radioVal)
} else if (
radioVal < robot.robots.RobotCompactCommand.ObstacleState
)
return this.filterViaCompare()

*/
Loading
Loading