Compare commits
6 Commits
Author | SHA1 | Date | |
---|---|---|---|
f6b392356c | |||
f4896f8d7c | |||
4dbd691146 | |||
2742dba0c4 | |||
5bea47a094 | |||
835a4b5cf0 |
@ -1,75 +1,15 @@
|
|||||||
# JavaScript
|
# JavaScript
|
||||||
|
|
||||||
You can write micro:bit programs in a subset of [TypeScript](https://www.typescriptlang.org), a superset of JavaScript.
|
If you already know some JavaScript, you might be interested in [the JavaScript and TypeScript languages](/js/lang).
|
||||||
Many micro:bit programs, especially at the beginner's level, are just plain JavaScript. TypeScript introduces class-based
|
Otherwise, visit the cards below to starting programming JavaScript with the micro:bit:
|
||||||
object-oriented programming, such as:
|
|
||||||
|
|
||||||
```typescript
|
```codecard
|
||||||
class Greeter {
|
[{
|
||||||
greeting: string;
|
"name": "Calling Functions",
|
||||||
constructor(message: string) {
|
"url":"/js/call.md"
|
||||||
this.greeting = message;
|
},{
|
||||||
}
|
"name": "Sequencing Commands",
|
||||||
greet() {
|
"url":"/js/sequence.md"
|
||||||
return "Hello, " + this.greeting;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
]
|
||||||
let greeter = new Greeter("world");
|
```
|
||||||
basic.showString(greeter.greet())
|
|
||||||
```
|
|
||||||
|
|
||||||
This site is meant for teaching programming first, and JavaScript second. For this
|
|
||||||
reason, we have stayed away from concepts that are specific to JavaScript (for
|
|
||||||
example, prototype inheritance), and instead focused on ones common to most
|
|
||||||
modern programming languages (for example, loops, lexically scoped variables,
|
|
||||||
functions, classes, lambdas).
|
|
||||||
|
|
||||||
We leverage TypeScript's [type inference](http://www.typescriptlang.org/docs/handbook/type-inference.html) so that
|
|
||||||
students need not specify types when clear from context.
|
|
||||||
|
|
||||||
## Supported language features
|
|
||||||
|
|
||||||
* top-level code in the file: "Hello world!" really is just `basic.showString("Hello world!")`
|
|
||||||
* [basic types](http://www.typescriptlang.org/docs/handbook/basic-types.html)
|
|
||||||
* [variable declarations](http://www.typescriptlang.org/docs/handbook/variable-declarations.html): `let`, `const`, and `var`
|
|
||||||
* [functions](http://www.typescriptlang.org/docs/handbook/functions.html) with lexical scoping and recursion
|
|
||||||
|
|
||||||
### User-defined types and modules
|
|
||||||
|
|
||||||
* [classes](http://www.typescriptlang.org/docs/handbook/classes.html) with fields, methods and constructors; `new` keyword
|
|
||||||
* [enums](http://www.typescriptlang.org/docs/handbook/enums.html)
|
|
||||||
* [namespaces](http://www.typescriptlang.org/docs/handbook/namespaces.html) (a form of modules)
|
|
||||||
|
|
||||||
### Control-flow constructs
|
|
||||||
|
|
||||||
* `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
|
|
||||||
|
|
||||||
### Expressions
|
|
||||||
|
|
||||||
* conditional operator `? :`; lazy boolean operators
|
|
||||||
* 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 `() => ...`
|
|
||||||
* array literals `[1, 2, 3]`
|
|
||||||
|
|
||||||
|
|
||||||
## 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
|
|
||||||
|
|
||||||
If there is something you'd like to see, please file an issue at [GitHub](http://github.com/microsoft/pxt/issues).
|
|
@ -15,44 +15,40 @@ followed by `.`; a list of all the functions will appear.
|
|||||||
|
|
||||||

|

|
||||||
|
|
||||||
Continue typing to select one of the functions, or click on one of the functions
|
We call this feature "Intellisense". Continue typing to select one of the functions,
|
||||||
to select. You also narrow down the set of functions by typing, as below:
|
or click on one of the functions to select. You also narrow down the set of functions by typing, as below:
|
||||||
|
|
||||||

|

|
||||||
|
|
||||||
# Function parameters
|
## Function parameter values
|
||||||
|
|
||||||
You might have noticed that the call `showString` above takes one value,
|
You might have noticed that the call `showString` above takes one parameter value,
|
||||||
the string to be scrolled on the LED screen. There is a second (optional)
|
the string to be scrolled on the LED screen. There is a second (optional)
|
||||||
parameter that controls the speed of the the scroll. Try this:
|
parameter that controls the speed of the scroll. Try this:
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
basic.showString("Hello!",50)
|
basic.showString("Hello!",50)
|
||||||
```
|
```
|
||||||
|
|
||||||
You might have noticed that the function list above shows all
|
Intellisense shows all the available parameters for a function.
|
||||||
the available parameters for each function.
|
|
||||||
|
|
||||||
|
## Left and right parentheses, please!
|
||||||
|
|
||||||
|
Whenever you want to call a function, you give the name of the function
|
||||||
|
followed by `(` and ending with `)`. Inbetween the left and right
|
||||||
|
parentheses go the function arguments. If a function has zero arguments, you still
|
||||||
|
need the parentheses in order to call the function. For example
|
||||||
|
|
||||||
```typescript
|
```typescript
|
||||||
basic.showString("Hello!")
|
|
||||||
basic.showLeds(`
|
|
||||||
. # . # .
|
|
||||||
. . . . .
|
|
||||||
. . # . .
|
|
||||||
# . . . #
|
|
||||||
. # # # .
|
|
||||||
`)
|
|
||||||
basic.pause(1000)
|
|
||||||
basic.clearScreen()
|
basic.clearScreen()
|
||||||
basic.showString("Goodbye!")
|
```
|
||||||
basic.showLeds(`
|
|
||||||
. # . # .
|
It's a syntax error to have a left parenthesis without the "closing" right parenthesis:
|
||||||
. . . . .
|
|
||||||
. . . . .
|
```typescript
|
||||||
. # # # .
|
basic.clearScreen(
|
||||||
# . . . #
|
```
|
||||||
`)
|
|
||||||
basic.pause(1000)
|
### ~button /js/sequence
|
||||||
basic.clearScreen()
|
NEXT: Sequencing Commands
|
||||||
```
|
### ~
|
||||||
|
75
docs/js/lang.md
Normal file
75
docs/js/lang.md
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
# JavaScript and TypeScript
|
||||||
|
|
||||||
|
You can write micro:bit programs in a subset of [TypeScript](https://www.typescriptlang.org), a superset of JavaScript.
|
||||||
|
Many micro:bit programs, especially at the beginner's level, are just plain JavaScript. TypeScript introduces class-based
|
||||||
|
object-oriented programming, such as:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
class Greeter {
|
||||||
|
greeting: string;
|
||||||
|
constructor(message: string) {
|
||||||
|
this.greeting = message;
|
||||||
|
}
|
||||||
|
greet() {
|
||||||
|
return "Hello, " + this.greeting;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
let greeter = new Greeter("world");
|
||||||
|
basic.showString(greeter.greet())
|
||||||
|
```
|
||||||
|
|
||||||
|
This site is meant for teaching programming first, and JavaScript second. For this
|
||||||
|
reason, we have stayed away from concepts that are specific to JavaScript (for
|
||||||
|
example, prototype inheritance), and instead focused on ones common to most
|
||||||
|
modern programming languages (for example, loops, lexically scoped variables,
|
||||||
|
functions, classes, lambdas).
|
||||||
|
|
||||||
|
We leverage TypeScript's [type inference](http://www.typescriptlang.org/docs/handbook/type-inference.html) so that
|
||||||
|
students need not specify types when clear from context.
|
||||||
|
|
||||||
|
## Supported language features
|
||||||
|
|
||||||
|
* top-level code in the file: "Hello world!" really is just `basic.showString("Hello world!")`
|
||||||
|
* [basic types](http://www.typescriptlang.org/docs/handbook/basic-types.html)
|
||||||
|
* [variable declarations](http://www.typescriptlang.org/docs/handbook/variable-declarations.html): `let`, `const`, and `var`
|
||||||
|
* [functions](http://www.typescriptlang.org/docs/handbook/functions.html) with lexical scoping and recursion
|
||||||
|
|
||||||
|
### User-defined types and modules
|
||||||
|
|
||||||
|
* [classes](http://www.typescriptlang.org/docs/handbook/classes.html) with fields, methods and constructors; `new` keyword
|
||||||
|
* [enums](http://www.typescriptlang.org/docs/handbook/enums.html)
|
||||||
|
* [namespaces](http://www.typescriptlang.org/docs/handbook/namespaces.html) (a form of modules)
|
||||||
|
|
||||||
|
### Control-flow constructs
|
||||||
|
|
||||||
|
* `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
|
||||||
|
|
||||||
|
### Expressions
|
||||||
|
|
||||||
|
* conditional operator `? :`; lazy boolean operators
|
||||||
|
* 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 `() => ...`
|
||||||
|
* array literals `[1, 2, 3]`
|
||||||
|
|
||||||
|
|
||||||
|
## 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
|
||||||
|
|
||||||
|
If there is something you'd like to see, please file an issue at [GitHub](http://github.com/microsoft/pxt/issues).
|
25
docs/js/sequence.md
Normal file
25
docs/js/sequence.md
Normal file
@ -0,0 +1,25 @@
|
|||||||
|
## Sequencing commands
|
||||||
|
|
||||||
|
By calling one function after another, you can create an animation:
|
||||||
|
|
||||||
|
```typescript
|
||||||
|
basic.showLeds(`
|
||||||
|
. # . # .
|
||||||
|
. . . . .
|
||||||
|
. . # . .
|
||||||
|
# . . . #
|
||||||
|
. # # # .
|
||||||
|
`)
|
||||||
|
basic.showLeds(`
|
||||||
|
. # . # .
|
||||||
|
. . . . .
|
||||||
|
. . . . .
|
||||||
|
. # # # .
|
||||||
|
# . . . #
|
||||||
|
`)
|
||||||
|
```
|
||||||
|
|
||||||
|
## The Semicolon
|
||||||
|
|
||||||
|
Coming soon...
|
||||||
|
|
@ -1,9 +1,12 @@
|
|||||||
# Servo Write Pin
|
# Servo Write Pin
|
||||||
|
|
||||||
Writes a value to the servo on to the specified [pin](/device/pins) (``P0``, ``P1``, ``P2``), controlling the shaft accordingly.
|
Write a value to the servo on the specified [pin](/device/pins)
|
||||||
|
and control the shaft.
|
||||||
|
|
||||||
* on a standard servo, this will set the angle of the shaft (in degrees), moving the shaft to that orientation.
|
This function will move the shaft of a standard servo to the specified
|
||||||
* 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).
|
angle, or set the speed of a continuous rotation servo. (`0` specifies
|
||||||
|
full speed in one direction, `180` specifies full speed in the other,
|
||||||
|
and approximately `90` specifies no movement.)
|
||||||
|
|
||||||
```sig
|
```sig
|
||||||
pins.servoWritePin(AnalogPin.P0, 180)
|
pins.servoWritePin(AnalogPin.P0, 180)
|
||||||
@ -11,18 +14,18 @@ pins.servoWritePin(AnalogPin.P0, 180)
|
|||||||
|
|
||||||
### Parameters
|
### Parameters
|
||||||
|
|
||||||
* `name` - [String](/reference/types/string); the pin name ("P0", "P1", or "P2")
|
* a [string](/reference/types/string) that specifies the pin name (`P0` through `P4`, or `P10`)
|
||||||
* `value` - a [Number](/reference/types/number) between 0 and 180 included
|
* a [number](/reference/types/number) from `0` through `180`
|
||||||
|
|
||||||
### Examples
|
### Examples
|
||||||
|
|
||||||
* setting the shaft angle to mid point on a servo
|
#### Setting the shaft angle to midpoint on a servo
|
||||||
|
|
||||||
```blocks
|
```blocks
|
||||||
pins.servoWritePin(AnalogPin.P0, 90)
|
pins.servoWritePin(AnalogPin.P0, 90)
|
||||||
```
|
```
|
||||||
|
|
||||||
* control the shaft by using the tilt information of the accelerometer
|
#### Controlling the shaft by using the tilt information of the accelerometer
|
||||||
|
|
||||||
```blocks
|
```blocks
|
||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
@ -33,7 +36,7 @@ basic.forever(() => {
|
|||||||
})
|
})
|
||||||
```
|
```
|
||||||
|
|
||||||
* setting the full speed on a continuous servo
|
#### Setting the full speed on a continuous servo
|
||||||
|
|
||||||
```blocks
|
```blocks
|
||||||
pins.servoWritePin(AnalogPin.P0, 0)
|
pins.servoWritePin(AnalogPin.P0, 0)
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "pxt-microbit",
|
"name": "pxt-microbit",
|
||||||
"version": "0.2.183",
|
"version": "0.2.184",
|
||||||
"description": "BBC micro:bit target for PXT",
|
"description": "BBC micro:bit target for PXT",
|
||||||
"keywords": [
|
"keywords": [
|
||||||
"JavaScript",
|
"JavaScript",
|
||||||
|
Reference in New Issue
Block a user