On start support (#324)

* annotating APIs to support onstart

* missed file

* updated templates

* adding new lines in main.ts

* fixing docs

* removing onstart

* updated docs

* updated various docs

* more docs

* adding upgrade policies for blocks

* updated pxt reference

* placing on start under "basic"
This commit is contained in:
Peli de Halleux 2016-12-19 14:09:35 -08:00 committed by GitHub
parent de9f2d7e67
commit 92b60a251d
17 changed files with 189 additions and 239 deletions

View File

@ -11,4 +11,4 @@ Math.random(5);
## See Also
[logic](/blocks/logic), [loops](/blocks/loops), [math](/blocks/math), [variables](/blocks/variables)
[logic](/blocks/logic), [loops](/blocks/loops), [math](/blocks/math), [variables](/blocks/variables), [on-start](/blocks/on-start)

View File

@ -86,12 +86,14 @@ if (led.point(1,1) && led.point(2,2)) {
When you compare two Numbers, you get a Boolean value, such as the comparison `x < 5` in the code below:
```blocks
let x = Math.random(5)
if(x < 5) {
basic.showString("low");
} else {
basic.showString("high");
}
input.onButtonPressed(Button.A, () => {
let x = Math.random(5)
if(x < 5) {
basic.showString("low");
} else {
basic.showString("high");
}
})
```
See the documentation on [Numbers](/reference/types/number) for more information on comparing two Numbers. You can also [compare strings](/reference/types/string-functions) using the `equals` function.

View File

@ -14,13 +14,15 @@ Click on the dark blue gear icon (see above) to add an *else* or *if* to the cur
### Example: adjusting screen brightness
```blocks
if(input.lightLevel()<100){
led.setBrightness(255);
}
```
If the [light level](/reference/input/light-level) is `< 100`, this code sets the brightness to `255` when the button A is pressed:
If the [light level](/reference/input/light-level) is `< 100`, this code sets the brightness to `255`:
```blocks
input.onButtonPressed(Button.A, () => {
if(input.lightLevel()<100){
led.setBrightness(255);
}
})
```
### See also

View File

@ -4,14 +4,21 @@
Run part of the program the number of times you say.
```block
for(let i = 0; i <= 4; ++i) {
}
```
### Example: Count to 4
This program will show the numbers 0, 1, 2, 3, and 4 one after another on the LED screen.
```blocks
for(let i = 0; i < 5; ++i) {
basic.showNumber(i)
}
input.onButtonPressed(Button.A, () => {
for(let i = 0; i < 5; ++i) {
basic.showNumber(i)
}
})
```
### See also

View File

@ -2,7 +2,7 @@
Repeat code while a [Boolean](/blocks/logic/boolean) `condition` is true.
```blocks
```block
while(true) {
}
```
@ -16,11 +16,13 @@ The condition is tested before any code runs. Which means that if the condition
The following example uses a while loop to make a diagonal line on the LED screen (points `0, 0`, `1, 1`, `2, 2`, `3, 3`, `4, 4`).
```blocks
let index = 4;
while(index >= 0) {
led.plot(index, index);
index--;
}
input.onButtonPressed(Button.A, () => {
let index = 4;
while(index >= 0) {
led.plot(index, index);
index--;
}
})
```
### See also

22
docs/blocks/on-start.md Normal file
View File

@ -0,0 +1,22 @@
# On Start
An event that runs when the program starts.
The ``on start`` is a special event that runs when the program starts, before any other event.
Use this event to initialize your program.
## Example
In this example, ``on start`` sets a dimmer brightness on the screen and the button handler shows a string.
```blocks
input.onButtonPressed(Button.A, () => {
basic.showString("Hello!")
})
led.setBrightness(50)
```
## What about JavaScript?
``on-start`` only exists in the block editor. In JavaScript, all code executes sequentially from the first line.

View File

@ -10,7 +10,9 @@ Let's start by adding a variable where you can store data. Then rename the varia
```blocks
let light = input.lightLevel();
input.onButtonPressed(Button.A, () => {
let light = input.lightLevel();
});
```
We want to play music on button pressed in order to register an event handler that will execute whenever when you run a script and click on button pressed on the simulator. We must start by opening the Input drawer and adding `on button pressed` A. Then add a block `rest` to plays nothing for a `1/16` beat. Modify your code so that your code looks like this.

View File

@ -37,7 +37,7 @@ input.onButtonPressed(Button.B, () => {
We will use the @boardname@'s compass to detect the magnet. Compass's tell us what direction we are pointing by detecting the Earth's magnetic field but they can also detect any other magnet nearby. We will use that to check if our magnet is next to the @boardname@ by using the [magnetic force](/reference/input/magnetic-force) block found in the input menu's 'more' section. As we only want to measure the strength we change the drop down to select 'strength':
```blocks
```block
input.magneticForce(Dimension.Strength)
```
@ -50,16 +50,19 @@ If you have ever played with magnets you know they have two ends, often called a
So in the code below we will check if the absolute value of our magnetic field strength reading is more than 100 and save the result of that check in a new variable called 'isSwitched':
```blocks
let isSwitched = Math.abs(input.magneticForce(Dimension.Strength)) > 100
let force = Math.abs(input.magneticForce(Dimension.Strength));
let isSwitched = force > 100
```
## Step 4: running our 'magnet nearby' check all the time
At the moment our code to detect the magnet being nearby will only run once so we need to put it into a [forever](/reference/basic/forever) block so that it keeps getting run again and again checking for the magnet to come near to the @boardname@. We should also make sure 'isSwitched' is false when our program starts.
```blocks
let force = 0;
let isSwitched = false;
basic.forever(() => {
let isSwitched = Math.abs(input.magneticForce(Dimension.Strength)) > 100
force = Math.abs(input.magneticForce(Dimension.Strength));
isSwitched = force > 100
})
```
@ -68,10 +71,11 @@ basic.forever(() => {
Now we can check the value of our variable 'isSwitched' whenever we want and we will know that the magnet is nearby if it's value is 'true'. Let's use that to change how the buttons work and complete the code for our trick. We will add an 'if, else' block to each button's code and check if we should swap over what each button displays because 'isSwitched' is equal to true:
```blocks
let force = 0;
let isSwitched = false;
basic.forever(() => {
isSwitched = Math.abs(input.magneticForce(Dimension.Strength)) > 100
force = Math.abs(input.magneticForce(Dimension.Strength));
isSwitched = force > 100
})
input.onButtonPressed(Button.A, () => {

View File

@ -1,62 +1,3 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="device_forever">
<statement name="HANDLER">
<block type="device_show_leds">
<field name="LED00">FALSE</field>
<field name="LED10">FALSE</field>
<field name="LED20">FALSE</field>
<field name="LED30">FALSE</field>
<field name="LED40">FALSE</field>
<field name="LED01">FALSE</field>
<field name="LED11">TRUE</field>
<field name="LED21">FALSE</field>
<field name="LED31">TRUE</field>
<field name="LED41">FALSE</field>
<field name="LED02">FALSE</field>
<field name="LED12">FALSE</field>
<field name="LED22">FALSE</field>
<field name="LED32">FALSE</field>
<field name="LED42">FALSE</field>
<field name="LED03">TRUE</field>
<field name="LED13">FALSE</field>
<field name="LED23">FALSE</field>
<field name="LED33">FALSE</field>
<field name="LED43">TRUE</field>
<field name="LED04">FALSE</field>
<field name="LED14">TRUE</field>
<field name="LED24">TRUE</field>
<field name="LED34">TRUE</field>
<field name="LED44">FALSE</field>
<next>
<block type="device_show_leds">
<field name="LED00">FALSE</field>
<field name="LED10">FALSE</field>
<field name="LED20">FALSE</field>
<field name="LED30">FALSE</field>
<field name="LED40">FALSE</field>
<field name="LED01">FALSE</field>
<field name="LED11">FALSE</field>
<field name="LED21">FALSE</field>
<field name="LED31">FALSE</field>
<field name="LED41">FALSE</field>
<field name="LED02">FALSE</field>
<field name="LED12">FALSE</field>
<field name="LED22">FALSE</field>
<field name="LED32">FALSE</field>
<field name="LED42">FALSE</field>
<field name="LED03">FALSE</field>
<field name="LED13">FALSE</field>
<field name="LED23">FALSE</field>
<field name="LED33">FALSE</field>
<field name="LED43">FALSE</field>
<field name="LED04">FALSE</field>
<field name="LED14">FALSE</field>
<field name="LED24">FALSE</field>
<field name="LED34">FALSE</field>
<field name="LED44">FALSE</field>
</block>
</next>
</block>
</statement>
</block>
<block type="pxtStart"></block>
</xml>

View File

@ -1,16 +1 @@
basic.forever(() => {
basic.showLeds(`
. # . # .
# . # . #
# . . . #
. # . # .
. . # . .
`)
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})

View File

@ -1,5 +1,5 @@
{
"bluetooth": "Support for additional Bluetooth services.",
"bluetooth": "Support for additional Bluetooth services.\n\nSupport for additional Bluetooth services.",
"bluetooth.advertiseUrl": "Advertise an Eddystone URL",
"bluetooth.advertiseUrl|param|connectable": "true to keep bluetooth connectable for other services, false otherwise.",
"bluetooth.advertiseUrl|param|power": "power level between 0 and 7, eg: 7",

View File

@ -1,62 +1,3 @@
<xml xmlns="http://www.w3.org/1999/xhtml">
<block type="device_forever">
<statement name="HANDLER">
<block type="device_show_leds">
<field name="LED00">FALSE</field>
<field name="LED10">FALSE</field>
<field name="LED20">FALSE</field>
<field name="LED30">FALSE</field>
<field name="LED40">FALSE</field>
<field name="LED01">FALSE</field>
<field name="LED11">TRUE</field>
<field name="LED21">FALSE</field>
<field name="LED31">TRUE</field>
<field name="LED41">FALSE</field>
<field name="LED02">FALSE</field>
<field name="LED12">FALSE</field>
<field name="LED22">FALSE</field>
<field name="LED32">FALSE</field>
<field name="LED42">FALSE</field>
<field name="LED03">TRUE</field>
<field name="LED13">FALSE</field>
<field name="LED23">FALSE</field>
<field name="LED33">FALSE</field>
<field name="LED43">TRUE</field>
<field name="LED04">FALSE</field>
<field name="LED14">TRUE</field>
<field name="LED24">TRUE</field>
<field name="LED34">TRUE</field>
<field name="LED44">FALSE</field>
<next>
<block type="device_show_leds">
<field name="LED00">FALSE</field>
<field name="LED10">FALSE</field>
<field name="LED20">FALSE</field>
<field name="LED30">FALSE</field>
<field name="LED40">FALSE</field>
<field name="LED01">FALSE</field>
<field name="LED11">FALSE</field>
<field name="LED21">FALSE</field>
<field name="LED31">FALSE</field>
<field name="LED41">FALSE</field>
<field name="LED02">FALSE</field>
<field name="LED12">FALSE</field>
<field name="LED22">FALSE</field>
<field name="LED32">FALSE</field>
<field name="LED42">FALSE</field>
<field name="LED03">FALSE</field>
<field name="LED13">FALSE</field>
<field name="LED23">FALSE</field>
<field name="LED33">FALSE</field>
<field name="LED43">FALSE</field>
<field name="LED04">FALSE</field>
<field name="LED14">FALSE</field>
<field name="LED24">FALSE</field>
<field name="LED34">FALSE</field>
<field name="LED44">FALSE</field>
</block>
</next>
</block>
</statement>
</block>
<block type="pxtStart"></block>
</xml>

View File

@ -1,16 +1 @@
basic.forever(() => {
basic.showLeds(`
. # . # .
# . # . #
# . . . #
. # . # .
. . # . .
`)
basic.showLeds(`
. . . . .
. . . . .
. . . . .
. . . . .
. . . . .
`)
})

View File

@ -31,7 +31,7 @@ namespace control {
/**
* If the condition is false, display msg on serial console, and panic with code 098.
*/
export function assert(condition: boolean, msg ?: string) {
export function assert(condition: boolean, msg?: string) {
if (!condition) {
console.log("ASSERTION FAILED")
if (msg != null) {

View File

@ -34,6 +34,6 @@
"semantic-ui-less": "^2.2.4"
},
"dependencies": {
"pxt-core": "0.6.7"
"pxt-core": "0.7.1"
}
}

View File

@ -57,6 +57,12 @@
"bluetooth\\s*\\.uartRead\\s*\\((.*?)\\)": "bluetooth.uartReadUntil($1)",
"bluetooth\\s*\\.uartWrite\\s*\\((.*?)\\)": "bluetooth.uartWriteUntil($1)"
}
},
{
"type": "blockId",
"map": {
"device_get_acceleration": "device_acceleration"
}
}
]
},
@ -64,7 +70,9 @@
"mathBlocks": true,
"loopsBlocks": true,
"logicBlocks": true,
"variablesBlocks": true
"variablesBlocks": true,
"onStartColor": "#0078D7",
"onStartNamespace": "basic"
},
"simulator": {
"autoRun": true,

View File

@ -11908,62 +11908,111 @@
:10071000F19501004198010039B1010099B901003A
:10072000D5C901009D26020061270200C180010099
:10073000708E3B92C615A841C49866C975EE519754
:100740002D0CF6CB4FCF31AA3852319F38B256E537
:100740002D0CF6CB4FCF31AA187FF87FFC58A9E9C2
:100750000000000000000000000000000000000099
:1007600000B5002001B4002001B4002001B4002035
:1007700000028C30024602BC01BCE8F745FC01B423
:10078000E8F7CAFC01BCF9F731FA0098F9F72EFA3C
:1007900001B000BD00B5002001B4069801B4069870
:1007A000014601BCF8F759FE01B4039801B405985D
:1007B000014601BCF8F751FE014601BCF8F753FEB3
:1007C00001B4059801B40798014601BCF8F745FE4D
:1007D000014601BCF8F743FE01B40498014601BC90
:1007E000F8F739FE0090FFE7009801B000BD00B5B2
:1007F000002001B4002000900298002802D0FF20C1
:100800000090FFE7059801B4F9F7C9F9059801B41C
:10081000059801B40398034604BC02BC0098F9F79C
:100820000AF901BCF9F7E2F90598F9F7DFF901B027
:1008300000BD00B5002001B40020E8F77DFB00906A
:10084000FFE7009801B000BDFFFF000020B50D4696
:1008500000200002A83001B40120000290300146BF
:1008600001BCE8F70FFA00200002B83001B4012003
:1008700000029030014601BCE8F704FA20BD0000F8
:10088000FFFF05000500000000000000010001005E
:100890000000000000000000000000010101002A2B
:1008A000FFFF050005000000000000000000000040
:1008B0000000000000000000000000000000002A0E
:1008C00041140E2FB82FA2BB4C00A3010000000062
:1008D0007B22636F6D7072657373696F6E223A224B
:1008E0004C5A4D41222C2268656164657253697AC5
:1008F00065223A3133392C227465787453697A65EC
:10090000223A323439342C226E616D65223A2262E9
:100910006C6F636B73626C696E6B227D5D0000802F
:1009200000490A000000000000003D888867041CA0
:10093000BCC1C8A25578869B4D69261C354501422D
:100940001948E37B0692B2DBE3A99A218E18EFA542
:100950006421FF3A50DF5CB84D25B831BC1D9BD0F7
:10096000A3720BD23F36166C9FFADA84711D2EB338
:100970003291D1DEB646B0BE0FFADE6FD7AD7C1134
:10098000573353851403078AD6AEE9A5C8D021B2E0
:100990007FFAB45E1D9AF8C10C1AEF12B167A5C2B6
:1009A000329D2DC02570F67BAF4E1FC2923CE042B7
:1009B00028E478417276160D1F06CE6E0CCBD84C0B
:1009C000B456FF79CC0DE99E98FE0D65BED6F7842E
:1009D000890D2B69552360DED35DCD22F579D8E4EE
:1009E000F214F5717F1C2CEF0D59E583D995AC807D
:1009F0000624B660B3E376A4C5A4C62BC0B1766066
:100A00005B5E6DBBD94336CEF28C58753EF5E6176A
:100A10000D454E5EA91A60E7F9BAA6413CFDB93210
:100A2000CC8DC16D25BCBA7ECF28A19D88036FAD4A
:100A30008EF21F6085E68BBD74D011870E15C5320E
:100A4000CAB0F268C6FF6A8E3DB568FF545C98A5CF
:100A5000041DFD9090BB3978F3995097661EE126EE
:100A60000993D095AB665AE4626AC471DC13A6D4CC
:100A700012F078262A98D778E53BE920E252773DB4
:100A8000206C0C9FA0E8589826BD5F95814AC5BE92
:100A90007BC997717460085C806C1CD9B85CCD33DD
:100AA0000016DD6FD865555605505938E15B941135
:100AB000D2FAC2F686783310D2E2FF86A151000046
:1007600000B5002001B401B401B401B401B401200A
:1007700001B4002001B4002001B400200002F630D2
:10078000024602BC01BCE8F73FFC014601BC02B4D2
:10079000F9F7B0FC01BCF9F729FA022001B40020F6
:1007A00001B4002001B4012000021A30024602BC4C
:1007B00001BCE8F729FC014601BC02B4F9F79AFC38
:1007C00001BCF9F713FA002001B4002001B40120A4
:1007D00000024030024602BC01BCE8F715FC01B43F
:1007E000E8F79AFC01BCF9F701FA02209621E7F735
:1007F00021FF01200002483001B4012000029030A6
:10080000014601BCE8F73EFA012000025830E8F743
:10081000CDF801B4962001460098F9F72FF901BCF4
:10082000F9F7E4F90120E7F73FFEE7F785FDE8F780
:1008300065FAE7F7AFFE0020E7F71CFEE7F784FD57
:100840000020E7F7F9FD0020E7F714FE0020E7F7A6
:1008500083FD0098F9F7CAF90298F9F7C7F90498E7
:10086000F9F7C4F905B000BD00B5002001B4069841
:1008700001B40698014601BCF8F7EFFD01B40398F6
:1008800001B40598014601BCF8F7E7FD014601BC3B
:10089000F8F7E9FD01B4059801B40798014601BCD9
:1008A000F8F7DBFD014601BCF8F7D9FD01B4049867
:1008B000014601BCF8F7CFFD0090FFE7009801B0BA
:1008C00000BD00B5002001B400200090029800286F
:1008D00002D0FF200090FFE7059801B4F9F75FF917
:1008E000059801B4059801B40398034604BC02BC02
:1008F0000098F9F7A0F801BCF9F778F90598F9F72D
:1009000075F901B000BD00B5002001B40020E8F782
:1009100013FB0090FFE7009801B000BDFFFF00004F
:1009200020B50D46002001B401B404200190002040
:100930000090009801B40298014601BCF8F767FDE9
:10094000002805D000980121F8F785FD0090F0E718
:1009500000200021F8F75BFD002800D0FFE702B07F
:1009600020BD0000FFFF000020B50D46002001B4AF
:1009700001B40120002819D004200121F8F76BFDF3
:10098000019000200090009801B40298014601BC3B
:10099000F8F73DFD002805D000980121F8F75BFD30
:1009A0000090F0E71420E7F79BFDE2E702B020BDDE
:1009B000FFFF000020B50D466420E7F791FD20BD44
:1009C000FFFF05000500000000000000010001001D
:1009D0000000000000010000000100010101002AE8
:1009E000FFFF060048656C6C6F21000000000000EE
:1009F00041140E2FB82FA2BB55006F03000000005A
:100A00007B22636F6D7072657373696F6E223A2219
:100A10004C5A4D41222C2268656164657253697A93
:100A200065223A3133352C227465787453697A65BE
:100A3000223A343236362C226E616D65223A2261BA
:100A400077652D696E73706972696E672073637262
:100A5000697074227D5D000080003111000000008B
:100A60000000003D888867041CBCC1C8A255788678
:100A70009B4D69261C354501421948E37B0692B21D
:100A8000DBE3A99A218E18EFA56421FF3A50DF5CC1
:100A9000B84D25B831BC1D9BD0A3720BD23F361682
:100AA0006C9FFADA84711D2EC9AC893737C3C8E24E
:100AB0003144571FA844D7A3963E762405463739BC
:100AC000562C2741749E7ADF8947F2C0F16D27E6E4
:100AD000671D21D7B68962CD4A32EB0C6D831DCEDE
:100AE000D938679D3E575063D676C5D1AD67714AF8
:100AF000DF5E908FB74BF1BA71BE31AF0D068F211B
:100B000042E1428B1A26AEB037DFB75E85844F6F65
:100B10007EFD07E78A8E10F0509394D2D3D10F89CF
:100B200079E24994B05EC5B4191F8F8732BA2EADF1
:100B3000BDA33AEA6B799E8F238650EED8A213129A
:100B40004CE8DBDF1CBC54075FAB965AB71F196437
:100B500057356BE317D9558961C31B5E7A253D8BE9
:100B6000101FCAEA6E9334AFDEB378209D13B0B283
:100B7000BBD03BC203BE98A78036163F9B529787D7
:100B800047E2EBB8DB41A6C2DE8D0437572CBDC867
:100B900070C5D57E332A3633384DAC966B5EE0F89F
:100BA0001A4FFBEE3BD5FE46C1FF358742209871B8
:100BB000585520F96EAAEF9324E9B1252895D54719
:100BC0006CFC464A13EB3A3899E7D46BA3C2027B1C
:100BD0008A502E8AA3F91A7AFD6DD225F44D72102F
:100BE00013FB3F888E41786D06AA9C6CBC12DB4DCE
:100BF000FCEAB9619A1C30A75D2426231876ED041F
:100C0000E1F6A1976F1202A06C6DA23FB9ED17B982
:100C10004661EAE80727E90B820F1648E858286B77
:100C2000C4312974DA972D1A5681A72E31DA22FFA2
:100C3000C68CE7BBCBACAA9EBD6FF5C91D62E4B9FB
:100C40009C1B6E664633ECFCC88C169A8DF048539C
:100C50009687FAB48B5DD1A1CC7568CBA05DBDC27F
:100C6000E485A7A1BA020C34F54A5876D5793EF24C
:100C7000240921FA568D22B801545A7140A289D50F
:100C8000C96E7F553E45B8911C762CA743E18DDF98
:100C90007FDB0D260FF7DC95B1454CB8B4A06BEEA9
:100CA000BBAB7A21557231ADDA411E05EDAD8E7ABE
:100CB000FCE220D686E36BD35922FC6BDA31BDD43B
:100CC000FF06648ED8A908BADFF63CE91A3B5874CF
:100CD00070836F6C9461A68A19299865D6BA06B894
:100CE00063C874A840820066F1BD40FC0816A4F8F1
:100CF000B8505B1E18047413CFA130569896E37950
:100D0000941201C53A972167887F1258FB9D85B3DD
:100D1000D1111EFAFB63E87B849CE3166143E8F281
:100D2000B3EA7C8B03B40E241F13F43DD24309ECC9
:100D30000B7F066F94CC1DFC0606F9B2284BA2BDB2
:100D4000BBBA5A03F54A5646DB56956B1D5CEC9FC1
:100D50005112F313C27CED0D7921A38A36C35D01D4
:100D6000485A30A244E32C82C52EEB6C9F260F51CB
:100D700076E9E1C367F794A852FDAC99CA3B1364C6
:100D80006F4F839EA694170E8339279977896845FC
:100D900011BD5F9646D7966E61E47FCF282FBD4C7C
:100DA0001709BFF597D59E441D6CDB5B74C44A1DC3
:100DB000C29F9A9190DFE13600061A2252F094FF0A
:100DC000BEF48D7000000000000000000000000074
:10C00000903C002061DC030073DC030075DC03005E
:10C010000000000000000000000000000000000020
:10C0200000000000000000000000000069C10300E3