adding a few more blocks

This commit is contained in:
Peli de Halleux 2017-10-24 05:30:05 -07:00
parent 8fa6cf41ca
commit fe46461c4c
5 changed files with 71 additions and 28 deletions

View File

@ -4,34 +4,34 @@
*/ */
const enum LightsPattern { const enum LightsPattern {
//% block=Off enumval=0 //% block=Off enumval=0
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
Off = 0, Off = 0,
//% block=Green enumval=1 //% block=Green enumval=1
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
Green = 1, Green = 1,
//% block=Red enumval=2 //% block=Red enumval=2
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
Red = 2, Red = 2,
//% block=Orange enumval=3 //% block=Orange enumval=3
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
Orange = 3, Orange = 3,
//% block="Flashing Green" enumval=4 //% block="Flashing Green" enumval=4
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
GreenFlash = 4, GreenFlash = 4,
//% block="Flashing Red" enumval=5 //% block="Flashing Red" enumval=5
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
RedFlash = 5, RedFlash = 5,
//% block="Flashing Orange" enumval=6 //% block="Flashing Orange" enumval=6
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
OrangeFlash = 6, OrangeFlash = 6,
//% block="Pulsing Green" enumval=7 //% block="Pulsing Green" enumval=7
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
GreenPulse = 7, GreenPulse = 7,
//% block="Pulsing Red" enumval=8 //% block="Pulsing Red" enumval=8
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
RedPulse = 8, RedPulse = 8,
//% block="Pulsing Orange" enumval=9 //% block="Pulsing Orange" enumval=9
//% blockIdentity=output.getPattern //% blockIdentity=output.pattern
OrangePulse = 9, OrangePulse = 9,
} }
@ -241,7 +241,7 @@ namespace output {
*/ */
//% blockId=setLights block="set status light %pattern=led_pattern" //% blockId=setLights block="set status light %pattern=led_pattern"
//% weight=100 group="Lights" //% weight=100 group="Lights"
export function setLights(pattern: number): void { export function setStatusLight(pattern: number): void {
if (currPattern === pattern) if (currPattern === pattern)
return return
currPattern = pattern currPattern = pattern
@ -258,7 +258,7 @@ namespace output {
//% blockId=led_pattern block="%pattern" //% blockId=led_pattern block="%pattern"
//% shim=TD_ID colorSecondary="#6e9a36" //% shim=TD_ID colorSecondary="#6e9a36"
//% blockHidden=true useEnumVal=1 pattern.fieldOptions.decompileLiterals=1 //% blockHidden=true useEnumVal=1 pattern.fieldOptions.decompileLiterals=1
export function getPattern(pattern: LightsPattern): number { export function pattern(pattern: LightsPattern): number {
return pattern; return pattern;
} }
} }

View File

@ -5,6 +5,7 @@ const enum GyroSensorMode {
} }
namespace input { namespace input {
//% fixedInstances
export class GyroSensor extends internal.UartSensor { export class GyroSensor extends internal.UartSensor {
constructor() { constructor() {
super() super()
@ -18,12 +19,32 @@ namespace input {
this._setMode(m) this._setMode(m)
} }
getAngle() { /**
* Get the current angle from the gyroscope.
* @param color the gyroscope to query the request
*/
//% help=input/gyro/angle
//% block="%color|angle"
//% blockId=gyroGetAngle
//% parts="gyroscope"
//% blockNamespace=input
//% weight=65 blockGap=8
angle() {
this.setMode(GyroSensorMode.Angle) this.setMode(GyroSensorMode.Angle)
return this.getNumber(NumberFormat.Int16LE, 0) return this.getNumber(NumberFormat.Int16LE, 0)
} }
getRate() { /**
* Get the current rotation rate from the gyroscope.
* @param color the gyroscope to query the request
*/
//% help=input/gyro/rate
//% block="%color|rotation rate"
//% blockId=gyroGetRate
//% parts="gyroscope"
//% blockNamespace=input
//% weight=65 blockGap=8
rate() {
this.setMode(GyroSensorMode.Rate) this.setMode(GyroSensorMode.Rate)
return this.getNumber(NumberFormat.Int16LE, 0) return this.getNumber(NumberFormat.Int16LE, 0)
} }

View File

@ -40,6 +40,7 @@ namespace input {
} }
} }
//% fixedInstance
export class IrSensor extends internal.UartSensor { export class IrSensor extends internal.UartSensor {
private channel: IrRemoteChannel private channel: IrRemoteChannel
private buttons: Button[]; private buttons: Button[];
@ -92,16 +93,37 @@ namespace input {
this._setMode(m) this._setMode(m)
} }
getDistance() { /**
* Get the distance measured by the infrared sensor.
* @param ir the infrared sensor
*/
//% help=input/infrared/distance
//% block="%infrared|distance"
//% blockId=infraredGetDistance
//% parts="infrared"
//% blockNamespace=input
//% weight=65 blockGap=8
distance() {
this.setMode(IrSensorMode.Proximity) this.setMode(IrSensorMode.Proximity)
return this.getNumber(NumberFormat.UInt8LE, 0) return this.getNumber(NumberFormat.UInt8LE, 0)
} }
getRemoteCommand() { /**
* Get the remote commandreceived the infrared sensor.
* @param ir the infrared sensor
*/
//% help=input/infrared/remote-command
//% block="%infrared|remote command"
//% blockId=infraredGetRemoteCommand
//% parts="infrared"
//% blockNamespace=input
//% weight=65 blockGap=8
remoteCommand() {
this.setMode(IrSensorMode.RemoteControl) this.setMode(IrSensorMode.RemoteControl)
return this.getNumber(NumberFormat.UInt8LE, this.channel) return this.getNumber(NumberFormat.UInt8LE, this.channel)
} }
// TODO
getDirectionAndDistance() { getDirectionAndDistance() {
this.setMode(IrSensorMode.Seek) this.setMode(IrSensorMode.Seek)
return this.getNumber(NumberFormat.UInt16LE, this.channel * 2) return this.getNumber(NumberFormat.UInt16LE, this.channel * 2)

View File

@ -92,10 +92,10 @@ namespace screen {
* @param x the starting position's x coordinate, eg: 0 * @param x the starting position's x coordinate, eg: 0
* @param y the starting position's x coordinate, eg: 0 * @param y the starting position's x coordinate, eg: 0
*/ */
//% blockId=screen_drawText block="print %text| at x: %x| y: %y" //% blockId=screen_print block="print %text| at x: %x| y: %y"
//% weight=99 group="Screen" blockNamespace=output inlineInputMode="inline" //% weight=99 group="Screen" blockNamespace=output inlineInputMode="inline"
//% x.min=0 x.max=178 y.min=0 y.max=128 //% x.min=0 x.max=178 y.min=0 y.max=128
export function drawText(text: string, x: number, y: number, mode = Draw.Normal) { export function print(text: string, x: number, y: number, mode = Draw.Normal) {
x |= 0 x |= 0
y |= 0 y |= 0
if (!currFont) currFont = defaultFont() if (!currFont) currFont = defaultFont()

View File

@ -1,8 +1,8 @@
screen.clear() screen.clear()
screen.drawText("PXT!", 10, 30, Draw.Quad) screen.print("PXT!", 10, 30, Draw.Quad)
screen.drawRect(40, 40, 20, 10, Draw.Fill) screen.drawRect(40, 40, 20, 10, Draw.Fill)
output.setLights(LightsPattern.Orange) output.setStatusLight(LightsPattern.Orange)
screen.drawIcon(100, 50, screen.doubleIcon(screen.heart), Draw.Double | Draw.Transparent) screen.drawIcon(100, 50, screen.doubleIcon(screen.heart), Draw.Double | Draw.Transparent)
@ -12,37 +12,37 @@ input.buttonEnter.onEvent(ButtonEvent.Click, () => {
input.buttonLeft.onEvent(ButtonEvent.Click, () => { input.buttonLeft.onEvent(ButtonEvent.Click, () => {
screen.drawRect(10, 70, 20, 10, Draw.Fill) screen.drawRect(10, 70, 20, 10, Draw.Fill)
output.setLights(LightsPattern.Red) output.setStatusLight(LightsPattern.Red)
screen.setFont(screen.microbitFont()) screen.setFont(screen.microbitFont())
}) })
input.buttonRight.onEvent(ButtonEvent.Click, () => { input.buttonRight.onEvent(ButtonEvent.Click, () => {
screen.drawText("Right!", 10, 60) screen.print("Right!", 10, 60)
}) })
input.buttonDown.onEvent(ButtonEvent.Click, () => { input.buttonDown.onEvent(ButtonEvent.Click, () => {
screen.drawText("Down! ", 10, 60) screen.print("Down! ", 10, 60)
}) })
input.buttonUp.onEvent(ButtonEvent.Click, () => { input.buttonUp.onEvent(ButtonEvent.Click, () => {
screen.drawText("Up! ", 10, 60) screen.print("Up! ", 10, 60)
}) })
let num = 0 let num = 0
input.touchSensor.onEvent(ButtonEvent.Click, () => { input.touchSensor.onEvent(ButtonEvent.Click, () => {
screen.drawText("Click! " + num, 10, 60) screen.print("Click! " + num, 10, 60)
num++ num++
}) })
input.remoteTopLeft.onEvent(ButtonEvent.Click, () => { input.remoteTopLeft.onEvent(ButtonEvent.Click, () => {
screen.drawText("TOPLEFT " + num, 10, 60) screen.print("TOPLEFT " + num, 10, 60)
num++ num++
}) })
input.remoteTopRight.onEvent(ButtonEvent.Down, () => { input.remoteTopRight.onEvent(ButtonEvent.Down, () => {
screen.drawText("TOPRIGH " + num, 10, 60) screen.print("TOPRIGH " + num, 10, 60)
num++ num++
}) })
@ -54,7 +54,7 @@ loops.forever(() => {
/* /*
loops.forever(() => { loops.forever(() => {
let v = input.color.getColor() let v = input.color.getColor()
screen.drawText(10, 60, v + " ") screen.print(10, 60, v + " ")
loops.pause(200) loops.pause(200)
}) })
*/ */