Skip to content

Commit

Permalink
better makecode blocks
Browse files Browse the repository at this point in the history
  • Loading branch information
pelikhan committed Oct 14, 2023
1 parent df719c0 commit e8f3ecd
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 15 deletions.
28 changes: 25 additions & 3 deletions robot/blocks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,34 @@ namespace microcode {
robot.runDrift = Math.clamp(-25, 25, drift)
}

/**
* Gets the distance reported by the distance sensor
*/
//% block="robot obstacle distance"
//% blockId=microcoderobotobstacledistance
//% group="Input"
export function obstacleDistance() {
checkRobotDriver()
return robot.currentUltrasonicDistance
}

/**
* Gets the distance reported by the distance sensor
*/
//% block="robot on obstacle changed"
//% blockId=microcoderobotobstacledistancechanged
//% group="Input"
export function onObstacleChanged(handler: () => void) {
checkRobotDriver()
microcode.robots.onEvent(microcode.robots.RobotCompactCommand.ObstacleState, handler)
}

/**
* Checks the state of lines
*/
//% block="robot detect lines $state"
//% blockId=microcoderobotdetectlines
//% group="Sensors"
//% group="Input"
export function detectLines(state: RobotLineState): boolean {
checkRobotDriver()
return robot.currentLineState === state
Expand All @@ -67,10 +89,10 @@ namespace microcode {
*/
//% block="robot on line $state detected"
//% blockId=microcoderobotondetectlines
//% group="Sensors"
//% group="Input"
export function onLineDetected(state: RobotLineState, handler: () => void) {
const msg = microcode.robots.RobotCompactCommand.LineState | state
microcode.onEvent(msg, handler)
microcode.robots.onEvent(msg, handler)
}

/**
Expand Down
10 changes: 9 additions & 1 deletion robot/messages.ts
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
// obsolete
namespace microcode.robots {
const ROBOT_EVENT_ID = 7325
export function raiseEvent(event: robots.RobotCompactCommand) {
control.raiseEvent(ROBOT_EVENT_ID, event & 0xffff)
}
export function onEvent(event: robots.RobotCompactCommand, handler: () => void) {
control.onEvent(ROBOT_EVENT_ID, event & 0xffff, handler)
}
}
17 changes: 6 additions & 11 deletions robot/robotdriver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* Microcode Robot
*/
//% color="#ff6800" icon="\uf1b9" weight=15
//% groups=['Move', 'Input', 'Configuration']
namespace microcode {
const RUN_STOP_THRESHOLD = 2
const TARGET_SPEED_THRESHOLD = 4
Expand All @@ -12,15 +13,6 @@ namespace microcode {
const ULTRASONIC_MIN_READING = 1
const LINE_ASSIST_LOST_THRESHOLD = 72

const ROBOT_EVENT_ID = 7325
function raiseEvent(event: robots.RobotCompactCommand) {
microcode.robots.sendCompactCommand(event)
control.raiseEvent(ROBOT_EVENT_ID, event)
}
export function onEvent(event: robots.RobotCompactCommand, handler: () => void) {
control.onEvent(ROBOT_EVENT_ID, event, handler)
}

/**
*
*/
Expand Down Expand Up @@ -102,6 +94,7 @@ namespace microcode {
//% block="robot start %this"
//% blockId=microcoderobotstart
//% weight=100
//% group="Configuration"
start() {
if (this.running) return

Expand Down Expand Up @@ -264,7 +257,8 @@ namespace microcode {
this.lastSonarValue = d
this.playTone(2400 - d * 400, 200 + d * 25)
const msg = microcode.robots.RobotCompactCommand.ObstacleState | d
raiseEvent(msg)
microcode.robots.sendCompactCommand(msg)
microcode.robots.raiseEvent(microcode.robots.RobotCompactCommand.ObstacleState)
}
}
}
Expand Down Expand Up @@ -313,7 +307,8 @@ namespace microcode {
this.currentLineState = ls
this.currentLineStateCounter = 0
const msg = microcode.robots.RobotCompactCommand.LineState | this.currentLineState
raiseEvent(msg)
microcode.robots.sendCompactCommand(msg)
microcode.robots.raiseEvent(msg)
}
this.currentLineStateCounter++
return ls
Expand Down

0 comments on commit e8f3ecd

Please sign in to comment.