Add more snippet types for better detection
This commit is contained in:
parent
9fd2a3a3e6
commit
9ebd9d4f04
@ -48,21 +48,21 @@ The micro:bit’s *scheduler* provides the capability to concurrently execute di
|
|||||||
|
|
||||||
The first job of the scheduler is to allow multiple *subprograms* to be queued up for later execution . For our purposes, a subprogram is just a statement or sequence of statements in the context of a larger program. Consider the Touch Develop program below for counting button presses.
|
The first job of the scheduler is to allow multiple *subprograms* to be queued up for later execution . For our purposes, a subprogram is just a statement or sequence of statements in the context of a larger program. Consider the Touch Develop program below for counting button presses.
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
export function countButtonPresses() {
|
export function countButtonPresses() {
|
||||||
|
let count = 0
|
||||||
input.onButtonPressed(Button.A, () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
})
|
})
|
||||||
count = 0
|
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
The program above contains three statements that execute in order from top to bottom. The first statement
|
The program above contains three statements that execute in order from top to bottom. The first statement
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
input.onButtonPressed(Button.A, () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
})
|
})
|
||||||
@ -70,7 +70,7 @@ input.onButtonPressed(Button.A, () => {
|
|||||||
|
|
||||||
informs the scheduler that on each and every event of the A button being pressed, a subprogram (called the event handler) should be queued for execution. The event handler is demarcated by the do/end keywords; it increments the global variable `count` by one. The second statement
|
informs the scheduler that on each and every event of the A button being pressed, a subprogram (called the event handler) should be queued for execution. The event handler is demarcated by the do/end keywords; it increments the global variable `count` by one. The second statement
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
basic.forever(() => {
|
basic.forever(() => {
|
||||||
basic.showNumber(count, 150)
|
basic.showNumber(count, 150)
|
||||||
})
|
})
|
||||||
@ -78,7 +78,7 @@ basic.forever(() => {
|
|||||||
|
|
||||||
queues a `forever` loop for later execution by the scheduler; the body of this loop (between the do/end keywords) displays the current value of global variable `count` on the LED screen. The third statement
|
queues a `forever` loop for later execution by the scheduler; the body of this loop (between the do/end keywords) displays the current value of global variable `count` on the LED screen. The third statement
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
count = 0
|
count = 0
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -128,7 +128,7 @@ Through this example, we have seen that the micro:bit scheduler enables you to c
|
|||||||
|
|
||||||
As a result, you can easily add a new capability to the micro:bit by just adding a new subprogram. For example, if you want to add a reset feature to the counter program, all you need to do is add a new event handler for a press of button B that sets the global variable "count" to zero, as shown below:
|
As a result, you can easily add a new capability to the micro:bit by just adding a new subprogram. For example, if you want to add a reset feature to the counter program, all you need to do is add a new event handler for a press of button B that sets the global variable "count" to zero, as shown below:
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
export function countButtonPressesWithReset() {
|
export function countButtonPressesWithReset() {
|
||||||
input.onButtonPressed(Button.A, () => {
|
input.onButtonPressed(Button.A, () => {
|
||||||
count = count + 1
|
count = count + 1
|
||||||
|
@ -34,8 +34,7 @@ Let's code the third part of Happy Birthday!
|
|||||||
|
|
||||||
To do this, you need to add blocks after the last line of the `play` blocks. We want to continue to adding musical chords with the `play` block. Then insert the appropriate chord blocks `G`, `E`, `C`, `B`, `A` to complete the third part of the song. Modify your code so that your code looks like this.
|
To do this, you need to add blocks after the last line of the `play` blocks. We want to continue to adding musical chords with the `play` block. Then insert the appropriate chord blocks `G`, `E`, `C`, `B`, `A` to complete the third part of the song. Modify your code so that your code looks like this.
|
||||||
|
|
||||||
``` blocks
|
```blocks
|
||||||
|
|
||||||
music.playTone(music.noteFrequency(Note.C), music.beat(BeatFraction.Quater));
|
music.playTone(music.noteFrequency(Note.C), music.beat(BeatFraction.Quater));
|
||||||
music.playTone(music.noteFrequency(Note.C), music.beat(BeatFraction.Quater));
|
music.playTone(music.noteFrequency(Note.C), music.beat(BeatFraction.Quater));
|
||||||
music.playTone(music.noteFrequency(Note.D), music.beat(BeatFraction.Quater));
|
music.playTone(music.noteFrequency(Note.D), music.beat(BeatFraction.Quater));
|
||||||
@ -57,7 +56,6 @@ music.playTone(music.noteFrequency(Note.F), music.beat(BeatFraction.Quater));
|
|||||||
music.playTone(music.noteFrequency(Note.E), music.beat(BeatFraction.Quater));
|
music.playTone(music.noteFrequency(Note.E), music.beat(BeatFraction.Quater));
|
||||||
music.playTone(music.noteFrequency(Note.D), music.beat(BeatFraction.Quater));
|
music.playTone(music.noteFrequency(Note.D), music.beat(BeatFraction.Quater));
|
||||||
basic.pause(100);
|
basic.pause(100);
|
||||||
|
|
||||||
```
|
```
|
||||||
|
|
||||||
* click *run * to see if the code works as expected.
|
* click *run * to see if the code works as expected.
|
||||||
|
@ -23,7 +23,7 @@ In this animation, each row is 15 spaces wide because
|
|||||||
there are three frames in the animation, and each frame is
|
there are three frames in the animation, and each frame is
|
||||||
five spaces wide, just like the screen on the BBC micro:bit.
|
five spaces wide, just like the screen on the BBC micro:bit.
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
basic.showAnimation(`
|
basic.showAnimation(`
|
||||||
. . # . . . # # # . . # # # .
|
. . # . . . # # # . . # # # .
|
||||||
. # # . . . . . # . . . . # .
|
. # # . . . . . # . . . . # .
|
||||||
@ -47,7 +47,7 @@ In this animation, each row is 30 spaces wide because
|
|||||||
there are six frames in the animation, and each frame is
|
there are six frames in the animation, and each frame is
|
||||||
five spaces wide, just like the screen.
|
five spaces wide, just like the screen.
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
basic.showAnimation(`
|
basic.showAnimation(`
|
||||||
. . . . . # . . . . . . . . . . . . . # . . . . . # . . . .
|
. . . . . # . . . . . . . . . . . . . # . . . . . # . . . .
|
||||||
. . # . . . . . . . . . # . . . . . . . . . # . . . . . . .
|
. . # . . . . . . . . . # . . . . . . . . . # . . . . . . .
|
||||||
|
@ -23,7 +23,7 @@ devices.onSignalStrengthChanged(() => {})
|
|||||||
|
|
||||||
Display the signal strength on screen:
|
Display the signal strength on screen:
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
devices.onSignalStrengthChanged(() => {
|
devices.onSignalStrengthChanged(() => {
|
||||||
basic.showNumber(devices.signalStrength(), 150)
|
basic.showNumber(devices.signalStrength(), 150)
|
||||||
})
|
})
|
||||||
|
@ -6,7 +6,7 @@ Turn off all the pixels in an [Image](/reference/images/image).
|
|||||||
|
|
||||||
### JavaScript
|
### JavaScript
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function clear(img: micro_bit.Image)
|
export function clear(img: micro_bit.Image)
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -18,7 +18,7 @@ export function clear(img: micro_bit.Image)
|
|||||||
|
|
||||||
The following example turns off the pixels of `img` when the A input button is pressed:
|
The following example turns off the pixels of `img` when the A input button is pressed:
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let img = images.createImage(`
|
let img = images.createImage(`
|
||||||
. . . . .
|
. . . . .
|
||||||
. # # # .
|
. # # # .
|
||||||
|
@ -2,12 +2,12 @@
|
|||||||
|
|
||||||
Reports the x position of a sprite on the LED screen
|
Reports the x position of a sprite on the LED screen
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function x(_this: micro_bitSprites.LedSprite) : number
|
export function x(_this: micro_bitSprites.LedSprite) : number
|
||||||
```
|
```
|
||||||
|
|
||||||
Reports the y position of a sprite on the LED screen
|
Reports the y position of a sprite on the LED screen
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function y(_this: micro_bitSprites.LedSprite) : number
|
export function y(_this: micro_bitSprites.LedSprite) : number
|
||||||
```
|
```
|
||||||
|
@ -4,24 +4,24 @@ Reports the x or y position, the current direction of a sprite, or the brightne
|
|||||||
|
|
||||||
Reports the x position of a sprite on the LED screen
|
Reports the x position of a sprite on the LED screen
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function x(_this: micro_bitSprites.LedSprite) : number
|
export function x(_this: micro_bitSprites.LedSprite) : number
|
||||||
```
|
```
|
||||||
|
|
||||||
Reports the y position of a sprite on the LED screen
|
Reports the y position of a sprite on the LED screen
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function y(_this: micro_bitSprites.LedSprite) : number
|
export function y(_this: micro_bitSprites.LedSprite) : number
|
||||||
```
|
```
|
||||||
|
|
||||||
Reports the brightness of a sprite on the LED screen
|
Reports the brightness of a sprite on the LED screen
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function brightness(_this: micro_bitSprites.LedSprite) : number
|
export function brightness(_this: micro_bitSprites.LedSprite) : number
|
||||||
```
|
```
|
||||||
|
|
||||||
Reports the current direction of a sprite on the LED screen
|
Reports the current direction of a sprite on the LED screen
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function direction(_this: micro_bitSprites.LedSprite) : number
|
export function direction(_this: micro_bitSprites.LedSprite) : number
|
||||||
```
|
```
|
||||||
|
@ -6,7 +6,7 @@ Get the state of a pixel in an [Image](/reference/images/image).
|
|||||||
|
|
||||||
### JavaScript
|
### JavaScript
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function pixel(_this: micro_bit.Image, x: number, y: number) : boolean
|
export function pixel(_this: micro_bit.Image, x: number, y: number) : boolean
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -29,7 +29,7 @@ This example gets the state of pixel `0, 0` in the `img` variable:
|
|||||||
|
|
||||||
### ~hide
|
### ~hide
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let img = images.createImage(`
|
let img = images.createImage(`
|
||||||
. . # . . . . . . .
|
. . # . . . . . . .
|
||||||
. # . # . . . # . .
|
. # . # . . . # . .
|
||||||
@ -41,7 +41,7 @@ let img = images.createImage(`
|
|||||||
|
|
||||||
### ~
|
### ~
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let state = img.pixel(0, 0)
|
let state = img.pixel(0, 0)
|
||||||
```
|
```
|
||||||
|
|
||||||
|
@ -6,7 +6,7 @@ Display an [Image](/reference/images/image) on the BBC micro:bit's [LED screen](
|
|||||||
|
|
||||||
### JavaScript
|
### JavaScript
|
||||||
|
|
||||||
```
|
```sig
|
||||||
export function showFrame(img: micro_bit.Image, frame: number)
|
export function showFrame(img: micro_bit.Image, frame: number)
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -20,7 +20,7 @@ The `show frame` function is the same as [plot frame](/reference/images/plot-fra
|
|||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let img = images.createImage(`
|
let img = images.createImage(`
|
||||||
# . . . # # . . . #
|
# . . . # # . . . #
|
||||||
. # . # . . # # # .
|
. # . # . . # # # .
|
||||||
|
@ -20,7 +20,7 @@ The following example gets the width of `img` and stores it in the `w` variable:
|
|||||||
|
|
||||||
### ~hide
|
### ~hide
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let img = images.createImage(`
|
let img = images.createImage(`
|
||||||
. . # . . . . . . .
|
. . # . . . . . . .
|
||||||
. # . # . . . # . .
|
. # . # . . . # . .
|
||||||
@ -32,7 +32,7 @@ let img = images.createImage(`
|
|||||||
|
|
||||||
### ~
|
### ~
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let w = img.width()
|
let w = img.width()
|
||||||
```
|
```
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ let w = img.width()
|
|||||||
|
|
||||||
The following example uses the `width` function with a [for](/blocks/loops/for) loop to show each image frame on the screen:
|
The following example uses the `width` function with a [for](/blocks/loops/for) loop to show each image frame on the screen:
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
let img2 = images.createImage(`
|
let img2 = images.createImage(`
|
||||||
. . # . . . # # # # . # # # .
|
. . # . . . # # # # . # # # .
|
||||||
. # # . . . . . . # . . . # .
|
. # # . . . . . . # . . . # .
|
||||||
|
@ -13,7 +13,7 @@ pins.analogPitch(440, 300)
|
|||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
pins.analogSetPitchPin("P0")
|
pins.analogSetPitchPin("P0")
|
||||||
let frequency1 = 440
|
let frequency1 = 440
|
||||||
let duration = 1000
|
let duration = 1000
|
||||||
|
@ -12,7 +12,7 @@ pins.analogSetPitchPin(AnalogPin.P0)
|
|||||||
|
|
||||||
### Example
|
### Example
|
||||||
|
|
||||||
```
|
```blocks
|
||||||
pins.analogSetPitchPin(AnalogPin.P0)
|
pins.analogSetPitchPin(AnalogPin.P0)
|
||||||
let frequency = 440
|
let frequency = 440
|
||||||
let duration = 1000
|
let duration = 1000
|
||||||
|
Loading…
Reference in New Issue
Block a user