Skip to content
This repository has been archived by the owner on Nov 25, 2024. It is now read-only.

Commit

Permalink
dataRecorder.ts: added cancel recording menu, sensor.ts: modified sch…
Browse files Browse the repository at this point in the history
…eduler to have a .stop(), now basic.pause() is called in 100ms chunks
  • Loading branch information
KierPalin committed Nov 7, 2024
1 parent a6ca3e3 commit 169e771
Show file tree
Hide file tree
Showing 2 changed files with 137 additions and 4 deletions.
115 changes: 112 additions & 3 deletions dataRecorder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ namespace microcode {
/** For the currentSensorIndex */
private sensorBoxColor: number;

private showCancelRecordingScreen: boolean;
private currentlyCancelling: boolean
private yesBtn: Sprite // currentBtn = 0
private noBtn: Sprite // currentBtn = 1

constructor(app: App, sensors: Sensor[]) {
super(app, "dataRecorder")

Expand All @@ -36,6 +41,8 @@ namespace microcode {
this.sensorIndexOffset = 0
this.currentSensorIndex = 0
this.sensorBoxColor = 15
this.showCancelRecordingScreen = false;
this.currentlyCancelling = false;

//---------------
// User Controls:
Expand All @@ -46,16 +53,23 @@ namespace microcode {
ControllerButtonEvent.Pressed,
controller.B.id,
() => {
this.app.popScene()
this.app.pushScene(new Home(this.app))
this.showCancelRecordingScreen = !this.showCancelRecordingScreen
}
)

// Clear whatever A was previously bound to
control.onEvent(
ControllerButtonEvent.Pressed,
controller.A.id,
() => {}
() => {
if (this.showCancelRecordingScreen) {
this.currentlyCancelling = true
this.scheduler.stop()

this.app.popScene()
this.app.pushScene(new Home(this.app))
}
}
)

// Scroll Up
Expand Down Expand Up @@ -86,6 +100,25 @@ namespace microcode {
}
)


// For cancelling the current recording:

this.yesBtn = new Sprite({ img: icons.get("tile_button_a") })
this.yesBtn.bindXfrm(new Affine())
this.yesBtn.xfrm.parent = new Affine()
this.yesBtn.xfrm.worldPos.x = Screen.HALF_WIDTH
this.yesBtn.xfrm.worldPos.y = Screen.HALF_HEIGHT
this.yesBtn.xfrm.localPos.x = -40
this.yesBtn.xfrm.localPos.y = 12

this.noBtn = new Sprite({ img: icons.get("tile_button_b") })
this.noBtn.bindXfrm(new Affine())
this.noBtn.xfrm.parent = new Affine()
this.noBtn.xfrm.worldPos.x = Screen.HALF_WIDTH
this.noBtn.xfrm.worldPos.y = Screen.HALF_HEIGHT
this.noBtn.xfrm.localPos.x = 40
this.noBtn.xfrm.localPos.y = 12

this.log()
}

Expand Down Expand Up @@ -192,6 +225,82 @@ namespace microcode {

y += 14
}

if (this.showCancelRecordingScreen) {
const headerX = Screen.HALF_WIDTH // Log has data in it

// Outline:
screen().fillRect(
Screen.HALF_WIDTH - 65,
Screen.HALF_HEIGHT - 30,
130 + 2,
60 + 2,
15 // Black
)

screen().fillRect(
Screen.HALF_WIDTH - 65,
Screen.HALF_HEIGHT - 30,
130,
60,
4 // Orange
)

const tutorialTextLength = ("Cancel recording?".length * font.charWidth)
screen().print(
"Cancel recording?",
headerX - (tutorialTextLength / 2),
Screen.HALF_HEIGHT - 30 + 7,
15 // Black
)

// Underline the title:
screen().fillRect(
headerX - (tutorialTextLength / 2) - 1,
Screen.HALF_HEIGHT - 30 + 16,
tutorialTextLength,
2,
15 // Black
)

if (this.currentlyCancelling)
screen().printCenter("Cancelling...", Screen.HALF_HEIGHT - 9, 15)

// Draw button prompts:
screen().print(
"Yes",
Screen.HALF_WIDTH - 48,
Screen.HALF_HEIGHT + 20,
15
)

screen().print(
"No",
Screen.HALF_WIDTH + 33,
Screen.HALF_HEIGHT + 20,
15
)

// White boxes behind yes & no btns:
screen().fillRect(
Screen.HALF_WIDTH - 47,
Screen.HALF_HEIGHT + 6,
12,
12,
1
)

screen().fillRect(
Screen.HALF_WIDTH + 34,
Screen.HALF_HEIGHT + 6,
12,
12,
1
)

this.yesBtn.draw()
this.noBtn.draw()
}
}
}
}
Expand Down
26 changes: 25 additions & 1 deletion sensors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,8 @@ namespace microcode {
/** Should the information from the sensorWithMostTimeLeft be shown on the basic's 5x5 LED matrix? */
private showOnBasicScreen: boolean = false;

private continueLogging: boolean;

constructor(sensors: Sensor[], showOnBasicScreen?: boolean) {
this.schedule = []
this.sensors = sensors
Expand All @@ -230,6 +232,8 @@ namespace microcode {
}
})

this.continueLogging = true;

// Setup schedule so that periods are in order ascending
sensors.sort((a, b) => a.getPeriod() - b.getPeriod())
this.schedule = sensors.map((sensor) => {return {sensor, waitTime: sensor.getPeriod()}})
Expand All @@ -239,6 +243,11 @@ namespace microcode {
loggingComplete(): boolean {return !(this.schedule.length > 0)}


stop() {
this.continueLogging = false;
}


/**
* Schedules the sensors and orders them to .log()
* Runs within a separate fiber.
Expand Down Expand Up @@ -278,7 +287,22 @@ namespace microcode {
const nextLogTime = this.schedule[0].waitTime;
const sleepTime = nextLogTime - currentTime;

basic.pause(sleepTime + lastLogTime - input.runningTime()) // Discount for operation time

// Wait the required period, discount operation time, in 100ms chunks
// Check if there last been a request to stop logging each chunk

const pauseTime = sleepTime + lastLogTime - input.runningTime() // Discount for operation time
for (let i = 0; i < pauseTime; i+=100) {
if (!this.continueLogging) {
return
}
basic.pause(100)
}
basic.pause(pauseTime % 100)

if (!this.continueLogging)
break;

lastLogTime = input.runningTime()
currentTime += sleepTime

Expand Down

0 comments on commit 169e771

Please sign in to comment.