Compare commits

...

10 Commits

Author SHA1 Message Date
61da1032d6 0.3.3 2016-07-27 07:26:24 -07:00
36d455c693 Bump pxt-core to 0.3.5 2016-07-27 07:26:20 -07:00
42c766b6d7 updated shim definition 2016-07-26 16:17:54 -07:00
6f00384891 Merge branch 'master' of https://github.com/Microsoft/pxt-microbit 2016-07-26 13:52:01 -07:00
8440f7c763 adding "pin is pressed" block 2016-07-26 13:51:41 -07:00
8a8e864f99 fix reference section 2016-07-26 14:18:44 -04:00
7a3402b782 add FAQ for language 2016-07-26 14:10:41 -04:00
5e9a5b29f4 fix formatting problem 2016-07-26 14:10:41 -04:00
aff9d1ee60 clean up docs 2016-07-26 09:03:42 -07:00
8add7e8efb updated radio strings / docs page 2016-07-26 08:47:45 -07:00
14 changed files with 107 additions and 274 deletions

View File

@ -27,5 +27,10 @@ Visit the cards below to starting programming JavaScript and TypeScript with the
},{
"name": "Classes",
"url": "/js/classes"
}]
},{
"name": "FAQ",
"url": "/js/faq"
}
]
```

58
docs/js/faq.md Normal file
View File

@ -0,0 +1,58 @@
# Frequently asked questions
# What is the language supported for the micro:bit?
For the micro:bit, we support a "static" subset of TypeScript (itself a superset of JavaScript):
## Supported language features
* variables with `let`, `const`, and `var`
* functions with lexical scoping and recursion
* top-level code in the file; hello world really is `console.log("Hello world")`
* `if ... else if ... else` statements
* `while` and `do ... while` loops
* `for(;;)` loops (see below about `for ... in/of`)
* `break/continue`; also with labeled loops
* `switch` statement (on numbers only)
* `debugger` statement for breakpoints
* conditional operator `? :`; lazy boolean operators
* namespaces (a form of modules)
* all arithmetic operators (including bitwise operators); note that in microcontroller targets
all arithmetic is performed on integers, also when simulating in the browser
* strings (with a few common methods)
* [string templates](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Template_literals) (`` `x is ${x}` ``)
* arrow functions `() => ...`
* classes with fields, methods and constructors; `new` keyword
* array literals `[1, 2, 3]`
* enums
## Unsupported language features
We generally stay away from the more dynamic parts of JavaScript.
Things you may miss and we may implement:
* exceptions (`throw`, `try ... catch`, `try ... finally`)
* `for ... of` statements
* object literals `{ foo: 1, bar: "two" }`
* method-like properties (get/set accessors)
* class inheritance
For JS-only targets we may implement the following:
* regular expressions
* classes implementing interfaces
Things that we are not very likely to implement:
* file-based modules (`import * from ...`, `module.exports` etc); we do support namespaces
* spread operator
* `yield` expression and ``function*``
* `await` expression and `async function`
* `typeof` expression
* tagged templates ``tag `text ${expression} more text` ``; regular templates are supported
* binding with arrays or objects: `let [a, b] = ...; let { x, y } = ...`
* `with` statement
* `eval`
* `delete` statement
* `for ... in` statements
* JSX (HTML as part of JavaScript)

View File

@ -35,7 +35,7 @@ basic.showNumber(1);
basic.showNumber(2);
```
## The empty statement
### The empty statement
In JavaScript, there is the concept of an *empty statement*, which is whitespace followed by
a semicolon in the context where a statement is expected.

View File

@ -22,15 +22,3 @@ control.inBackground(() => {
});
```
## Advanced
```namespaces
devices.tellCameraTo(MesCameraEvent.TakePhoto);
bluetooth.onBluetoothConnected(() => {});
```
```package
microbit-devices
microbit-bluetooth
```

View File

@ -1,148 +0,0 @@
# Game Library
The game library supports simple single-player time-based games. The player has a **sprite**, number of **lives** and a **score**. The game has a sprite, number of **levels** and a **countdown clock**. The general goal of a game will be to move the sprite and achieve a top score before time runs out or the number of lives goes to zero.
The code below shows a simple game where the user gets to press the button ``A`` as much times as possible in 10 seconds.
```blocks
input.onButtonPressed(Button.A, () => {
game.addScore(1)
})
game.startCountdown(10000)
```
### [Create sprite](/reference/game/create-sprite)
Create sprite with x, y coordinates and returns a LED Sprite. Create a new LED sprite.
![](/static/mb/create-sprite-0.png)
```
export function createSprite(x: number, y: number) : micro_bitSprites.LedSprite
```
### [Move](/reference/game/move)
Sprite move by a certain number
![](/static/mb/game-library/move-0.png)
```
export function move(_this: micro_bitSprites.LedSprite, leds: number)
```
### [Turn](/reference/game/turn)
Rotates a sprite to the right by a certain number of degrees
![](/static/mb/game-library/turn-0.png)
```
export function turnRight(_this: micro_bitSprites.LedSprite, degrees: number)
```
Rotates a sprite to the left by a certain number of degrees
```
export function turnLeft(_this: micro_bitSprites.LedSprite, degrees: number)
```
### [Change](/reference/game/change)
Sprite will change the x position by this number
![](/static/mb/change-0.png)
```
export function changeXBy(_this: micro_bitSprites.LedSprite, x: number)
```
Sprite will change the y position by this number
```
export function changeYBy(_this: micro_bitSprites.LedSprite, y: number)
```
### [Set](/reference/game/set)
Sprite will change the x position by this number
```
export function setX(_this: micro_bitSprites.LedSprite, x: number)
```
Sprite will change the y position by this number
![](/static/mb/change-0.png)
```
export function changeYBy(_this: micro_bitSprites.LedSprite, y: number)
```
### [If on edge, bounce](/reference/game/if-on-edge-bounce)
Sprite - If the sprite is on the edge, the sprite will bounce
![](/static/mb/game-library/if-on-edge-bounce-0.png)
```
export function ifOnEdgeBounce(_this: micro_bitSprites.LedSprite)
```
### [Change score by](/reference/game/change-score-by)
When a player achieves a goal, you can increase the game score
* add score points to the current score
![](/static/mb/game-library/pic1.png)
```
export function addScore(points: number)
```
### [Score](/reference/game/score)
* set the current score to a particular value.
```
export function setScore(value: number)
```
* get the current score value
![](/static/mb/game-library/pic2.png)
```
export function score() : number
```
### [Countdown](/reference/game/start-countdown)
If your game has a time limit, you can start a countdown in which case `game->current time` returns the remaining time.
* start a countdown with the maximum duration of the game in milliseconds.
![](/static/mb/game-library/pic3.png)
```
export function startCountdown(ms: number)
```
### [Game over](/reference/game/game-over)
If the `life` reaches zero or the time expires (see countdown), the game enters the **game over** mode. When the game is over, `game->is running` returns false
* check if the game still running.
```
let running = game.isRunning()
```
You can also end the game by calling the `game -> game over` function:
![](/static/mb/game-library/pic0.png)
```
game.gameOver()
```

View File

@ -16,7 +16,7 @@ export function showFrame(img: micro_bit.Image, frame: number)
### Difference from `plot frame`
The `show frame` function is the same as [plot frame](/reference/image/plot-frame), but contains a built-in delay after the LED screen has been updated (whereas `plot frame` has no built-in delay)
The `show frame` function is the same as [plot frame](/reference/images/plot-frame), but contains a built-in delay after the LED screen has been updated (whereas `plot frame` has no built-in delay)
### Example

View File

@ -14,7 +14,7 @@ radio.receivedNumberAt(0);
radio.receiveString();
radio.receivedSignalStrength();
radio.setGroup(0);
radio.setTransmitPower(0);
radio.setTransmitPower(7);
radio.writeValueToSerial();
radio.setTransmitSerialNumber(true);
radio.setTransmitSerialNumber(false);
```

View File

@ -46,8 +46,6 @@ for
forever
game-library
game-over
if

View File

@ -1,101 +1,32 @@
{
"basic": "Provides access to basic micro:bit functionality.",
"basic.clearScreen": "Turn off all LEDs",
"basic.forever": "Repeats the code forever in the background. On each iteration, allows other codes to run.",
"basic.pause": "Pause for the specified time in milliseconds",
"basic.plotLeds": "Draws an image on the LED screen.",
"basic.showAnimation": "Shows a sequence of LED screens as an animation.",
"basic.showLeds": "Draws an image on the LED screen.",
"basic.showNumber": "Scroll a number on the screen. If the number fits on the screen (i.e. is a single digit), do not scroll.",
"basic.showString": "Display text on the display, one character at a time. If the string fits on the screen (i.e. is one letter), does not scroll.",
"control": "Runtime and event utilities.",
"control.inBackground": "Schedules code that run in the background.",
"control.reset": "Resets the BBC micro:bit.",
"game": "A single-LED sprite game engine",
"game.addScore": "Adds points to the current score",
"game.gameOver": "Displays a game over animation.",
"game.score": "Gets the current score",
"game.setScore": "Sets the current score value",
"game.startCountdown": "Starts a game countdown timer",
"images": "Creation, manipulation and display of LED images.",
"images.createBigImage": "Creates an image with 2 frames.",
"images.createImage": "Creates an image that fits on the LED screen.",
"input": "Events and data from sensors",
"input.acceleration": "Get the acceleration value in milli-gravitys (when the board is laying flat with the screen up, x=0, y=0 and z=-1024)",
"input.buttonIsPressed": "Get the button state (pressed or not) for ``A`` and ``B``.",
"input.calibrate": "Obsolete, compass calibration is automatic.",
"input.compassHeading": "Get the current compass compass heading in degrees.",
"input.lightLevel": "Reads the light level applied to the LED screen in a range from ``0`` (dark) to ``255`` bright.",
"input.magneticForce": "Get the magnetic force value in ``micro-Teslas`` (``µT``). This function is not supported in the simulator.",
"input.onButtonPressed": "Do something when a button (``A``, ``B`` or both ``A+B``) is pressed",
"input.onGesture": "Attaches code to run when the screen is facing up.",
"input.onLogoDown": "Attaches code to run when the logo is oriented downwards and the board is vertical.",
"input.onLogoUp": "Attaches code to run when the logo is oriented upwards and the board is vertical.",
"input.onPinPressed": "Do something when a pin(``P0``, ``P1`` or both ``P2``) is pressed.",
"input.onScreenDown": "Attaches code to run when the screen is facing down.",
"input.onScreenUp": "Attaches code to run when the screen is facing up.",
"input.onShake": "Attaches code to run when the device is shaken.",
"input.pinIsPressed": "Get the pin state (pressed or not). Requires to hold the ground to close the circuit.",
"input.rotation": "The pitch of the device, rotation along the ``x-axis``, in degrees.",
"input.runningTime": "Gets the number of milliseconds elapsed since power on.",
"input.setAccelerometerRange": "Sets the accelerometer sample range in gravities.",
"input.temperature": "Gets the temperature in Celsius degrees (°C).",
"led": "Control of the LED screen.",
"led.brightness": "Get the screen brightness from 0 (off) to 255 (full bright).",
"led.fadeIn": "Fades in the screen display.",
"led.fadeOut": "Fades out the screen brightness.",
"led.plot": "Turn on the specified LED using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.",
"led.plotAll": "Turns all LEDS on",
"led.plotBarGraph": "Displays a vertical bar graph based on the `value` and `high` value.\nIf `high` is 0, the chart gets adjusted automatically.",
"led.point": "Get the on/off state of the specified LED using x, y coordinates. (0,0) is upper left.",
"led.screenshot": "Takes a screenshot of the LED screen and returns an image.",
"led.setBrightness": "Set the screen brightness from 0 (off) to 255 (full bright).",
"led.setDisplayMode": "Sets the display mode between black and white and greyscale for rendering LEDs.",
"led.stopAnimation": "Cancels the current animation and clears other pending animations.",
"led.toggle": "Toggles a particular pixel",
"led.toggleAll": "Inverts the current LED display",
"led.unplot": "Turn off the specified LED using x, y coordinates (x is horizontal, y is vertical). (0,0) is upper left.",
"music": "Generation of music tones through pin ``P0``.",
"music.beat": "Returns the duration of a beat in milli-seconds",
"music.changeTempoBy": "Change the tempo by the specified amount",
"music.noteFrequency": "Gets the frequency of a note.",
"music.playTone": "Plays a tone through pin ``P0`` for the given duration.",
"music.rest": "Rests (plays nothing) for a specified time through pin ``P0``.",
"music.ringTone": "Plays a tone through pin ``P0``.",
"music.setTempo": "Sets the tempo to the specified amount",
"music.tempo": "Returns the tempo in beats per minute. Tempo is the speed (bpm = beats per minute) at which notes play. The larger the tempo value, the faster the notes will play.",
"pins": "Control currents in Pins for analog/digital signals, servos, i2c, ...",
"pins.analogPitch": "Emits a Pulse-width modulation (PWM) signal to the current pitch pin. Use `analog set pitch pin` to define the pitch pin.",
"pins.analogReadPin": "Read the connector value as analog, that is, as a value comprised between 0 and 1023.",
"pins.analogSetPeriod": "Configures the Pulse-width modulation (PWM) of the analog output to the given value in **microseconds** or `1/1000` milliseconds.\nIf this pin is not configured as an analog output (using `analog write pin`), the operation has no effect.",
"pins.analogSetPitchPin": "Sets the pin used when using `pins->analog pitch`.",
"pins.analogWritePin": "Set the connector value as analog. Value must be comprised between 0 and 1023.",
"pins.digitalReadPin": "Read the specified pin or connector as either 0 or 1",
"pins.digitalWritePin": "Set a pin or connector value to either 0 or 1.",
"pins.i2cReadNumber": "Read one number from 7-bit I2C address.",
"pins.i2cWriteNumber": "Write one number to a 7-bit I2C address.",
"pins.map": "Re-maps a number from one range to another. That is, a value of ``from low`` would get mapped to ``to low``, a value of ``from high`` to ``to high``, values in-between to values in-between, etc.",
"pins.onPulsed": "Configures this pin to a digital input, and generates events where the timestamp is the duration that this pin was either ``high`` or ``low``.",
"pins.pulseDuration": "Gets the duration of the last pulse in micro-seconds. This function should be called from a ``onPulse`` handler.",
"pins.servoSetPulse": "Configures this IO pin as an analog/pwm output, configures the period to be 20 ms, and sets the pulse width, based on the value it is given **microseconds** or `1/1000` milliseconds.",
"pins.servoWritePin": "Writes a value to the servo, controlling the shaft accordingly. On a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation. On a continuous rotation servo, this will set the speed of the servo (with ``0`` being full-speed in one direction, ``180`` being full speed in the other, and a value near ``90`` being no movement).",
"radio": "Communicate data using radio packets",
"radio.onDataReceived": "Registers code to run when a packet is received over radio.",
"radio.onDataReceived|block": "on data received",
"radio.receiveNumber": "Reads the next packet as a number from the radio queue.",
"radio.receiveNumber|block": "receive number",
"radio.receiveString": "Reads the next packet as a string and returns it.",
"radio.receiveString|block": "receive string",
"radio.receivedNumberAt": "Reads a number at a given index, between ``0`` and ``3``, from the packet received by ``receive number``. Not supported in simulator.",
"radio.receivedNumberAt|block": "receive number|at %VALUE",
"radio.receivedNumberAt|param|index": "index of the number to read from 0 to 3. 1 eg",
"radio.receivedSignalStrength": "Gets the received signal strength indicator (RSSI) from the packet received by ``receive number``. Not supported in simulator.\nnamespace=radio",
"radio.receivedSignalStrength|block": "received signal strength",
"radio.sendNumber": "Broadcasts a number over radio to any connected micro:bit in the group.",
"radio.sendNumber|block": "send number %value",
"radio.sendString": "Broadcasts a number over radio to any connected micro:bit in the group.",
"radio.sendString|block": "send string %msg",
"radio.sendValue": "Broadcasts a name / value pair along with the device serial number\nand running time to any connected BBC micro:bit in the group.",
"radio.sendValue|block": "send|value %name|= %value",
"radio.sendValue|param|name": "the field name (max 12 characters), eg: \"data\"",
"radio.sendValue|param|value": "the numberic value",
"radio.setGroup": "Sets the group id for radio communications. A micro:bit can only listen to one group ID at any time.\n@ param id the group id between ``0`` and ``255``, 1 eg",
"radio.setGroup|block": "set group %ID",
"radio.setTransmitPower": "Change the output power level of the transmitter to the given value.",
"radio.setTransmitPower|block": "set transmit power %power",
"radio.setTransmitPower|param|power": "a value in the range 0..7, where 0 is the lowest power and 7 is the highest. eg: 7",
"radio.setTransmitSerialNumber": "Set the radio to transmit the serial number in each message.",
"radio.setTransmitSerialNumber|block": "set tranmist serial number %transmit",
"radio.writeValueToSerial": "Reads a value sent with `stream value` and writes it\nto the serial stream as JSON",
"serial": "Reading and writing data over a serial connection.",
"serial.readLine": "Reads a line of text from the serial port.",
"serial.writeLine": "Prints a line of text to the serial",
"serial.writeNumber": "Prints a numeric value to the serial",
"serial.writeString": "Sends a piece of text through Serial connection.",
"serial.writeValue": "Writes a ``name: value`` pair line to the serial."
"radio.writeValueToSerial|block": "write value to serial",
"radio|block": "radio"
}

View File

@ -167,6 +167,17 @@ namespace input {
return false;
}
/**
* Get the pin state (pressed or not). Requires to hold the ground to close the circuit.
* @param name pin used to detect the touch
*/
//% help=input/pin-is-pressed weight=56
//% blockId="device_pin_is_pressed" block="pin %NAME|is pressed" icon="\uf094"
//% blockGap=8
bool pinIsPressed(TouchPin name) {
auto pin = getPin((int)name);
return pin && pin->isTouched();
}
/**
* Get the current compass compass heading in degrees.
@ -270,16 +281,6 @@ namespace input {
//% help=input/calibrate weight=0
void calibrate() { }
/**
* Get the pin state (pressed or not). Requires to hold the ground to close the circuit.
* @param name pin used to detect the touch
*/
//% help=input/pin-is-pressed weight=58 block="pin|%NAME|is pressed" icon="\uf094"
bool pinIsPressed(TouchPin name) {
auto pin = getPin((int)name);
return pin && pin->isTouched();
}
/**
* Sets the accelerometer sample range in gravities.
* @param range a value describe the maximum strengh of acceleration measured

View File

@ -230,6 +230,15 @@ declare namespace input {
//% icon="\uf192" blockGap=8 shim=input::buttonIsPressed
function buttonIsPressed(button: Button): boolean;
/**
* Get the pin state (pressed or not). Requires to hold the ground to close the circuit.
* @param name pin used to detect the touch
*/
//% help=input/pin-is-pressed weight=56
//% blockId="device_pin_is_pressed" block="pin %NAME|is pressed" icon="\uf094"
//% blockGap=8 shim=input::pinIsPressed
function pinIsPressed(name: TouchPin): boolean;
/**
* Get the current compass compass heading in degrees.
*/
@ -290,13 +299,6 @@ declare namespace input {
//% help=input/calibrate weight=0 shim=input::calibrate
function calibrate(): void;
/**
* Get the pin state (pressed or not). Requires to hold the ground to close the circuit.
* @param name pin used to detect the touch
*/
//% help=input/pin-is-pressed weight=58 block="pin|%NAME|is pressed" icon="\uf094" shim=input::pinIsPressed
function pinIsPressed(name: TouchPin): boolean;
/**
* Sets the accelerometer sample range in gravities.
* @param range a value describe the maximum strengh of acceleration measured

View File

@ -185,7 +185,6 @@ Functions in this category require to be connected to a remote device.
### Libraries
* [game library](/js/game-library)
* [serial library](/js/serial-library)
### ~

View File

@ -47,7 +47,6 @@ Learn how to create a guessing game with **global variables** `var str: "this is
* **on screen down** [read more...](/functions/on-screen-down)
* **on screen up** [read more...](/functions/on-screen-up)
* **math random** : [read more...](/js/math)
* **game library** [read more...](/js/game-library)
## Resources

View File

@ -1,6 +1,6 @@
{
"name": "pxt-microbit",
"version": "0.3.2",
"version": "0.3.3",
"description": "BBC micro:bit target for PXT",
"keywords": [
"JavaScript",
@ -29,6 +29,6 @@
"typescript": "^1.8.7"
},
"dependencies": {
"pxt-core": "0.3.2"
"pxt-core": "0.3.5"
}
}