Compare commits
74 Commits
Author | SHA1 | Date | |
---|---|---|---|
0a746f5bbe | |||
066408c317 | |||
90f7b229d1 | |||
c54e0b0e9d | |||
ea088ac828 | |||
29cf4d3e53 | |||
6d637eefd7 | |||
6310e96ac2 | |||
dbb1e31822 | |||
5376bc2dce | |||
57bd293df8 | |||
e570d46755 | |||
d37f284fce | |||
b716226889 | |||
1bd19cc1a0 | |||
ac8c9c5e85 | |||
057260c483 | |||
568ae32f81 | |||
ae605ff829 | |||
f25ddb1085 | |||
f175f973f0 | |||
c7e5fa490e | |||
61a29f7c67 | |||
50293fc654 | |||
a9ecadaf09 | |||
6e56107eb5 | |||
1f077beb65 | |||
8fff762156 | |||
cebded2526 | |||
13f53d730a | |||
8adcab1cab | |||
8cb5f442f9 | |||
96448d5237 | |||
30f01bb0ac | |||
3d0b397de2 | |||
e0de55d689 | |||
7637a98f07 | |||
95f94e0886 | |||
9a2367cf8e | |||
15fecb77c4 | |||
4f69bbabfb | |||
8351ed0513 | |||
c34b0a1aeb | |||
9bcd44d7e4 | |||
3403da8ce8 | |||
39c146329f | |||
8d0d0a7e9a | |||
61b3783dd4 | |||
da16428842 | |||
359c456577 | |||
b3d9c167e1 | |||
78398b220f | |||
a656fbbd7b | |||
e681cc8c97 | |||
03beec9c49 | |||
1600644be5 | |||
a3dd64eb4c | |||
b865cce44e | |||
dc42900c7f | |||
dcc3bd95fe | |||
61e63831aa | |||
5651c3b9ba | |||
a562557bca | |||
d890c05229 | |||
4340e1f7ed | |||
e2e4c68f85 | |||
3f6a3089f1 | |||
84a0c0fe56 | |||
5237b86cf5 | |||
4070d4e691 | |||
7048156b46 | |||
232758805b | |||
211d4e5538 | |||
374d8c590d |
@ -53,11 +53,6 @@ pxt update
|
||||
|
||||
More instructions at https://github.com/Microsoft/pxt#running-a-target-from-localhost
|
||||
|
||||
## Universal Windows App
|
||||
|
||||
The Windows 10 app is a [Universal Windows Hosted Web App](https://microsoftedge.github.io/WebAppsDocs/en-US/win10/CreateHWA.htm)
|
||||
that wraps ``codethemicrobit.com`` and provides additional features.
|
||||
|
||||
### Building
|
||||
|
||||
* Install Visual Studio 2015 Update 2 or higher. Make sure the Windows 10 templates are installed.
|
||||
|
@ -28,7 +28,7 @@ export function deployCoreAsync(res: ts.pxtc.CompileResult) {
|
||||
|
||||
function getBitDrivesAsync(): Promise<string[]> {
|
||||
if (process.platform == "win32") {
|
||||
let rx = new RegExp("^([A-Z]:).* " + pxt.appTarget.compile.deployDrives)
|
||||
const rx = new RegExp("^([A-Z]:).* " + pxt.appTarget.compile.deployDrives)
|
||||
return execAsync("wmic PATH Win32_LogicalDisk get DeviceID, VolumeName, FileSystem")
|
||||
.then(buf => {
|
||||
let res: string[] = []
|
||||
@ -42,9 +42,14 @@ function getBitDrivesAsync(): Promise<string[]> {
|
||||
})
|
||||
}
|
||||
else if (process.platform == "darwin") {
|
||||
let rx = new RegExp(pxt.appTarget.compile.deployDrives)
|
||||
const rx = new RegExp(pxt.appTarget.compile.deployDrives)
|
||||
return readDirAsync("/Volumes")
|
||||
.then(lst => lst.filter(s => rx.test(s)).map(s => "/Volumes/" + s + "/"))
|
||||
} else if (process.platform == "linux") {
|
||||
const rx = new RegExp(pxt.appTarget.compile.deployDrives)
|
||||
const user = process.env["USER"]
|
||||
return readDirAsync(`/media/${user}`)
|
||||
.then(lst => lst.filter(s => rx.test(s)).map(s => `/media/${user}/${s}/`))
|
||||
} else {
|
||||
return Promise.resolve([])
|
||||
}
|
||||
|
@ -1,37 +1,36 @@
|
||||
# Math
|
||||
|
||||
[Numeric](/reference/types/number) values: 0, 1, 2, ...
|
||||
### [Numeric](/reference/types/number) values: 0, 1, 2, ...
|
||||
|
||||
```blocks
|
||||
```block
|
||||
0;
|
||||
1;
|
||||
2;
|
||||
```
|
||||
|
||||
Arithmetic binary operation (+, -, *, /)
|
||||
### Arithmetic binary operation (+, -, *, /)
|
||||
|
||||
```blocks
|
||||
```block
|
||||
0+1;
|
||||
0-1;
|
||||
1*2;
|
||||
3/4;
|
||||
```
|
||||
|
||||
Absolute value
|
||||
### Absolute value
|
||||
|
||||
```blocks
|
||||
```block
|
||||
Math.abs(-5);
|
||||
```
|
||||
|
||||
Minimum/maximum of two values
|
||||
### Minimum/maximum of two values
|
||||
|
||||
```blocks
|
||||
```block
|
||||
Math.min(0, 1);
|
||||
Math.max(0, 1);
|
||||
```
|
||||
|
||||
Random value
|
||||
### Random value
|
||||
|
||||
```blocks
|
||||
```block
|
||||
Math.random(5);
|
||||
```
|
||||
|
@ -1,42 +0,0 @@
|
||||
# Math functions
|
||||
|
||||
### @parent blocks/language
|
||||
|
||||
The math library includes math related functions that you can use with [Numbers](/reference/types/number).
|
||||
|
||||
### abs
|
||||
|
||||
math `->` abs (x : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
|
||||
|
||||
returns the absolute value of input parameter `x`
|
||||
|
||||

|
||||
|
||||
### max
|
||||
|
||||
math `->` max (x : [Number](/reference/types/number), y : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
|
||||
|
||||
returns the larger of two input numbers (`x` and `y`)
|
||||
|
||||

|
||||
|
||||
### min
|
||||
|
||||
math `->` min (x : [Number](/reference/types/number), y : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
|
||||
|
||||
returns the smaller of two input numbers (`x` and `y`)
|
||||
|
||||

|
||||
|
||||
### random
|
||||
|
||||
math `->` random (limit : [Number](/reference/types/number)) *returns* [Number](/reference/types/number)
|
||||
|
||||
returns a random [Number](/reference/types/number) between 0 and the parameter *limit*
|
||||
|
||||

|
||||
|
||||
### See also
|
||||
|
||||
[Number](/reference/types/number)
|
||||
|
@ -1,19 +1,24 @@
|
||||
# Run Scripts on your micro:bit
|
||||
# Running programs on your micro:bit
|
||||
|
||||
How to compile, transfer, and run a script on your micro:bit.
|
||||
How to compile, transfer, and run a program on your micro:bit.
|
||||
|
||||
While you're writing and testing your scripts, you'll mostly be running scripts in your browser by clicking the `PLay` button
|
||||
(see [run code in your browser](/device/simulator) for info about this).
|
||||
While you're writing and testing your programs, you'll mostly be [running them
|
||||
in the simulator](/device/simulator), but once you've finished your program you
|
||||
can **compile** it and run it on your micro:bit.
|
||||
|
||||
Once your masterpiece is complete, you can compile your script and run it on your micro:bit.
|
||||
The basic steps are:
|
||||
|
||||
1. Connect your micro:bit to your computer via USB
|
||||
2. Click **Download** and download the `.hex` file
|
||||
3. Copy the `.hex` file from your computer onto the micro:bit drive
|
||||
|
||||
## Requirements
|
||||
|
||||
You need the following things to transfer and run a script on your micro:bit:
|
||||
|
||||
* A-Male to Micro USB cable to connect your computer to your micro:bit. This is the same cable that is commonly used to connect a smart phone to a computer.
|
||||
* a PC running Windows 7 of later, or a Mac running OS X 10.6 or later
|
||||
* access to the Internet
|
||||
* A-Male to Micro USB cable to connect your computer to your micro:bit. This is
|
||||
the same cable that is commonly used to connect a smart phone to a computer.
|
||||
* A PC running Windows 7 or later, or a Mac running OS X 10.6 or later
|
||||
|
||||
## Step 1: Connect your micro:bit to your computer
|
||||
|
||||
@ -23,108 +28,151 @@ First, connect the micro:bit:
|
||||
|
||||
2. Connect the other end of the USB cable to a USB port on your computer.
|
||||
|
||||
Your computer should recognise your micro:bit as a new drive. On computers running Windows, MICROBIT appears as a drive under Devices and drives. On a Mac it appears as a new drive under Devices.
|
||||
Your computer should recognise your micro:bit as a new drive. On computers
|
||||
running Windows, `MICROBIT` appears as a drive under Devices and drives. On a Mac
|
||||
it appears as a new drive under Devices.
|
||||
|
||||
Windows
|
||||
**Windows**
|
||||
|
||||

|
||||

|
||||
|
||||
Mac (picture bvabdbco)
|
||||
WARN: unknown picture: bvabdbco:5x3
|
||||
**Mac**
|
||||
|
||||
## Step 2: Compile your script
|
||||

|
||||
|
||||
Next, compile your script:
|
||||
## Step 2: Download your program
|
||||
|
||||
1. Sign in to Touch Develop on your computer.
|
||||
|
||||
2. Open your script (find the script in **My Scripts** and click `Edit`).
|
||||
|
||||
3. Click **Download**. Your script is converted into a hex file that you can transfer and run on your micro:bit.
|
||||
|
||||
4. When prompted, choose to save the compiled file on your computer (or anywhere other than the micro:bit). Depending on which browser you are using, the download will adopt the download behaviour of that particular browser.
|
||||
1. Open your project on [codethemicrobit.com](https://codethemicrobit.com)
|
||||
2. Click **Download**
|
||||
3. When prompted, choose to **save** the compiled file onto your computer. The
|
||||
prompt will be different depending on which browser you are using, or
|
||||
whether you are using a Windows computer or a Mac
|
||||
|
||||
### Windows
|
||||
|
||||
** Chrome**
|
||||
#### Chrome
|
||||
|
||||
Your .hex file appears as a download at the bottom of the browser. Open up your windows file explorer. Your micro:bit appears as a drive called MICROBIT.
|
||||
Your `.hex` file appears as a download at the bottom of the browser. Click on
|
||||
the arrow next to the name of the file and then click **Show in folder**.
|
||||
|
||||
**Right click** on the download and select **show in folder**. Drag and drop the hex file from the download folder onto the MICROBIT drive.
|
||||

|
||||
|
||||
Alternatively, you can drag and drop the downloaded hex file from the bottom of the browser onto the file explorer and onto the MICROBIT drive.
|
||||
Drag and drop the `.hex` file from the download folder onto the `MICROBIT` drive.
|
||||
|
||||

|
||||
#### Firefox
|
||||
|
||||
**Firefox**
|
||||
A window will appear asking whether you want to save or open the `.hex` file.
|
||||
Select **Save File** and then select **OK**.
|
||||
|
||||
A dialogue box will appear, asking whether you would like to open or save your hex file. Select **Save**, then **OK** and the file will appear in your downloads in the top right of your browser. Select the **blue arrow**, select the relevant file and drag and drop it onto your Windows Explorer and onto your MICROBIT drive.
|
||||

|
||||
|
||||

|
||||
The file will then appear in your downloads in the top right of your browser.
|
||||
Click the **folder icon** next to the filename to open it in Windows Explorer.
|
||||
|
||||

|
||||

|
||||
|
||||
**IE10**
|
||||
Drag and drop the `.hex` file from the download folder onto the `MICROBIT` drive.
|
||||
|
||||
Click on **Download**. You will see a message “Do you want to save this .hex file.” Select **Save**.
|
||||
#### Microsoft Edge
|
||||
|
||||
A message will appear at the bottom of the browser asking what you want to do
|
||||
with the file. Click **Save**:
|
||||
|
||||

|
||||
|
||||
Then click **Open folder** and drag and drop the file from your Downloads to
|
||||
your `MICROBIT` drive.
|
||||
|
||||

|
||||
|
||||
#### Internet Explorer
|
||||
|
||||
A message will appear at the bottom of the browser asking what you want to do
|
||||
with the file. Click **Save**:
|
||||
|
||||

|
||||
|
||||
Then click **Open folder** and drag and drop the file from your Downloads to
|
||||
your `MICROBIT` drive.
|
||||
|
||||

|
||||
|
||||
### Mac
|
||||
|
||||
** Safari**
|
||||
#### Safari
|
||||
|
||||
When you select **Download** in Safari on Mac, your file will be downloaded to your downloads folder. Go to your downloads folder and open the file. In Safari the file will appear as unknown.txt rather than a named .hex file. Drag and drop it onto your MICROBIT drive.
|
||||
When you select **Download** in Safari a file called `Unknown` will be
|
||||
downloaded into your Downloads folder. Open your Downloads folder and drag and
|
||||
drop the file onto your `MICROBIT` drive, under Devices:
|
||||
|
||||

|
||||

|
||||
|
||||
**Firefox**
|
||||
#### Firefox
|
||||
|
||||
A dialogue box will appear, asking whether you would like to open or save your hex file. Select **Save** and **OK** and the file will then appear in your downloads in the top right of your browser. Click on **Show in Finder** and the file will appear in your downloads folder. Select the file and drag and drop it onto your MICROBIT drive.
|
||||
A dialogue box will appear, asking whether you would like to open or save your
|
||||
hex file. Select **Save file** and click **OK** and the file will then appear in
|
||||
your downloads in the top right of your browser. Right click on the file and
|
||||
click on **Show in Finder** and the file will appear in your downloads folder.
|
||||
Select the file and drag and drop it onto your `MICROBIT` drive.
|
||||
|
||||

|
||||

|
||||
|
||||

|
||||

|
||||
|
||||
**Chrome**
|
||||
#### Chrome
|
||||
|
||||
When you select **Download** in Chrome, the file will be downloaded to the bottom of the browser in .hex format. Click on the small arrow and select **Show in Finder**. This will show the file in your download folder. Drag and drop the file onto your MICROBIT drive.
|
||||
When you select **Download** in Chrome, the file will appear at the bottom of
|
||||
the browser. Click on the small arrow and select **Show in Finder**. This will
|
||||
show the file in your download folder. Drag and drop the file onto your
|
||||
`MICROBIT` drive.
|
||||
|
||||

|
||||

|
||||
|
||||
## Step 3: Transfer the file to your micro:bit
|
||||
|
||||
1. The file will transfer onto your micro:bit.
|
||||
|
||||
2. If you're using Windows, you can use **Send to** as described below.
|
||||
|
||||
3. The LED on the back of your micro:bit flashes during the transfer (which should only take a few seconds).
|
||||
|
||||
4. Once transferred, the code will run automatically on your micro:bit. To rerun your program, press the reset button on the back of your micro:bit. The reset button automatically runs the newest file on the micro:bit.
|
||||
* Once you've found the folder containing your `.hex` file, drag and drop it
|
||||
onto your `MICROBIT` drive
|
||||
* If you're using Windows, you can use **Send to** as described below
|
||||
* The LED on the back of your micro:bit flashes during the transfer (which
|
||||
should only take a few seconds).
|
||||
* Once transferred, the code will run automatically on your micro:bit. To rerun
|
||||
your program, press the reset button on the back of your micro:bit. The reset
|
||||
button automatically runs the newest file on the micro:bit.
|
||||
|
||||
**Send to**: If you're using Windows you use *Send to* in File Explorer:
|
||||
|
||||
- In File Explorer, right-click on the hex file (created in Step 2 above), choose **Send to**, and then **MICROBIT**.
|
||||
|
||||

|
||||

|
||||
|
||||
By copying the script onto the 'MICROBIT' drive, you have programmed it into the flash memory on the micro:bit, which means even after you unplug the micro:bit, your script will still run if the micro:bit is powered by battery.
|
||||
By copying the script onto the `MICROBIT` drive, you have programmed it into the
|
||||
flash memory on the micro:bit, which means even after you unplug the micro:bit,
|
||||
your program will still run if the micro:bit is powered by battery.
|
||||
|
||||
## Troubleshooting
|
||||
|
||||
You can’t drag and drop more than one hex file at once onto your micro:bit. If you try to drag and drop a second hex file onto your micro:bit before the first file has finished downloading, then the second file may fail in different ways.
|
||||
You can’t drag and drop more than one hex file at once onto your micro:bit. If
|
||||
you try to drag and drop a second hex file onto your micro:bit before the first
|
||||
file has finished downloading, then the second file may fail in different ways.
|
||||
|
||||
When the first program has been written to the micro:bit, the drive will disengage. If you drag and drop a second file at this point it may not find the drive and the second write will fail.
|
||||
When the first program has been written to the micro:bit, the drive will
|
||||
disengage. If you drag and drop a second file at this point it may not find the
|
||||
drive and the second write will fail.
|
||||
|
||||
The errors may look like this:
|
||||
|
||||
**Windows**
|
||||
|
||||

|
||||

|
||||
|
||||
**Mac**
|
||||
|
||||

|
||||

|
||||
|
||||
Or it may appear that there are two hex files on your micro:bit so the micro:bit won’t be able to run multiple files. To rectify this, unplug your micro:bit and plug it in again. Make sure that your micro:bit appears as MICROBIT and not MAINTENANCE.
|
||||
Or it may appear that there are two hex files on your micro:bit so the micro:bit
|
||||
won’t be able to run multiple files. To rectify this, unplug your micro:bit and
|
||||
plug it in again. Make sure that your micro:bit appears as `MICROBIT` and not
|
||||
`MAINTENANCE`.
|
||||
|
||||
### See also
|
||||
|
||||
|
@ -4,9 +4,11 @@ An introduction to conditions for the Block Editor.
|
||||
|
||||
## Introduction to conditions
|
||||
|
||||
In the introduction to code, we made the BBC micro:bit automatically shows the message ‘hello, world!’:
|
||||
In the introduction to code, we made the BBC micro:bit automatically shows the message ‘hello world!’:
|
||||
|
||||

|
||||
```blocks
|
||||
basic.showString("hello world!")
|
||||
```
|
||||
|
||||
This statement, or code, will happen as soon as the BBC micro:bit is activated. This means it is unconditional. We can add a condition to make code function in certain ways:
|
||||
|
||||
@ -16,11 +18,13 @@ This statement, or code, will happen as soon as the BBC micro:bit is activated.
|
||||
|
||||
In programming we use an ‘if’ statement: if this condition is met, do something. Lets add an if statement to the code we had before; the BBC Micro:bit will wait for the user to press a button before showing the image.
|
||||
|
||||
### Write the code
|
||||
|
||||
Click the **if** category and drag an `if/do` block. Drag the`show string` block we wrote previously into the `do` section of the block. Next click the **input** tab and drag a `button pressed` block, connect it to the open jigsaw of the `if` block. This is our criteria: `if A button is pressed`. We can change which button (button A or B) by clicking the arrow next to ‘A’ and changing the value. This means our BBC micro:bit is waiting for button A (the left button) to be pressed. Finally go to the **basic** tab and drag a `forever` block, and attach all our code inside. We add this block to ensure the BBC micro:bit is always waiting to show us this message, not just once. Your code should look like this:
|
||||
|
||||

|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
if (input.buttonIsPressed(Button.A)) {
|
||||
basic.showString("hello world!")
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Again, test the code in the simulator. Try clicking **Button A** to display the "hello, world!" message every time the `button is pressed`.
|
||||
|
||||
@ -40,7 +44,15 @@ For example, we could make it so our BBC Micro:bit tells us to press the A butto
|
||||
|
||||
We want the message "Press A!" to scroll across the BBC micro:bit, so right-click the `show string` block and select **Duplicate**. Drag this new block into the `else` section and replace the “hello, world!” with "Press A!". Your code should look like this:
|
||||
|
||||

|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
if (input.buttonIsPressed(Button.A)) {
|
||||
basic.showString("hello world!")
|
||||
} else {
|
||||
basic.showString("PRESS A")
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
So, to recap: the `forever` block makes sure our code runs forever. The BBC micro:bit checks if the user is pressing the left button, if the user is not then the “Press the button!” message will scroll across the LEDs. If the user is pressing the button then the “hello, world!” message will scroll across the screen. Check this in the simulator or attach the BBC micro:bit to the computer then click **Download** to send the code onto the BBC micro:bit.
|
||||
|
||||
|
@ -1,88 +0,0 @@
|
||||
# blocks - challenges
|
||||
|
||||
Extra stuff for the Block Editor - an introduction to GPIO
|
||||
|
||||
## Before we get started
|
||||
|
||||
This section details challenges for the BBC micro:bit. Ensure you have completed all other sections of the Microsoft Block Editor tutorials before attempting these challenges!
|
||||
|
||||
## Quiz Challenge [1]
|
||||
|
||||
Using if statements, try to add more statements to create a simple quiz. The user will be told if the question is right or not, and will have two options (button A and button B).
|
||||
|
||||
Here is some sample code for a simple quiz:
|
||||
|
||||

|
||||
|
||||
## Timer Challenge [2]
|
||||
|
||||
Create a timer that runs out after a certain amount of time (using the *count* loop). For an extra challenge, let the user input the amount of seconds they want the timer to run for using variables and the buttons as input. The solution is below.
|
||||
|
||||

|
||||
|
||||
## Graphics Challenges [3]
|
||||
|
||||
Using the knowledge you have learnt from the [rendering graphics](/lessons/graphics) section, try creating an algorithm to draw these shapes. Before you write the code try to figure out how the BBC micro:bit will be thinking to plot these points. For example, with our diagonal line – “count up from 0 to 4 by 1, and plot points x=i and y=i”.
|
||||
|
||||
* Another diagonal line
|
||||
* A square going around the board
|
||||
* A filled square
|
||||
* A square which unplots itself after
|
||||
* A filled square which then unplots itself
|
||||
|
||||
The solutions are below.
|
||||
|
||||
### Square [3.1]
|
||||
|
||||

|
||||
|
||||
### Filled square [3.2]
|
||||
|
||||

|
||||
|
||||
### Vanishing square [3.3]
|
||||
|
||||
Use the same code and algorithm for the square solution, only use the ‘unplot’ block to make this LED turn off again. You could also reverse the algorithm.
|
||||
|
||||
### Vanishing filled square [3.4]
|
||||
|
||||
Use the same code and algorithm for the filled square solution, only use the `unplot` block to make this LED turn off again. You could also reverse the algorithm.
|
||||
|
||||
## Animation Challenge [4]
|
||||
|
||||
Use your new knowledge of animations and algorithms to program your BBC micro:bit to act human: for example, you could make your BBC micro:bit smile and wink. Remember you can display images with the `show image` and `create image` blocks. Sample code is below.
|
||||
|
||||

|
||||
|
||||
## Electronic Dice Challenge [5]
|
||||
|
||||
Using the code in the Random Numbers tutorial in Section 6, or your own algorithm, create an electronic dice that displays the values appropriate for a dice (so 1 shows a single LED on in the center, two shows two LEDs on at each corner, etc.). You may want to declare image variables to do this, then check what it is equal to using an ‘if’ statement. Sample code is below.
|
||||
|
||||

|
||||
|
||||
## Calculator Challenge [6]
|
||||
|
||||
Using your knowledge of loops, counters and math, create a calculator.
|
||||
|
||||
The calculator should:
|
||||
|
||||
* Count the amount of times the user presses the left button before pressing the right button (this is the first value, or valueOne)
|
||||
* Count the amount of times the user presses the left button before the right button again (this is the second value, or valueTwo)
|
||||
* Scroll through operations (+,-, x and divide) until the user presses the right button to make a choice
|
||||
* Perform the calculation
|
||||
* Show the entire calculation, for example: 5 + 10 = 15
|
||||
|
||||
Sample code is below.
|
||||
|
||||

|
||||
|
||||
## Smart watch Challenge [8]
|
||||
|
||||
Create a smart watch using the BBC micro:bit. Create a menu where the user presses one button to cycle through options and another button to choose this option. Add applications to this smart watch:
|
||||
|
||||
* Calculators
|
||||
* Games
|
||||
* Random number generators
|
||||
|
||||
And any other applications you can think of.
|
||||
|
@ -6,7 +6,9 @@ An introduction to graphics for the Block Editor.
|
||||
|
||||
Ensure you have completed the 'Hello, world!' and Loop tutorials and tested them on a simulator or on BBC micro:bit.
|
||||
|
||||

|
||||
```blocks
|
||||
basic.showString("HI!");
|
||||
```
|
||||
|
||||
The BBC micro:bit has a grid of 25 LEDs, so we can use these to display images.
|
||||
|
||||
@ -24,9 +26,16 @@ We can also code our bug to plot a point by giving an x (horizontal) and y (vert
|
||||
|
||||
We can also unplot a point (turn the LED off again) using the `unplot` block. So we could create a flashing LED program, using the `pause` block to create a delay.
|
||||
|
||||

|
||||
```blocks
|
||||
basic.forever(() => {
|
||||
led.plot(2,2)
|
||||
basic.pause(100)
|
||||
led.unplot(2,2)
|
||||
basic.pause(100)
|
||||
})
|
||||
```
|
||||
|
||||
We can also use the `clear screen` block to turn off all LEDs.
|
||||
We can also use the `basic.clearScreen` block to turn off all LEDs.
|
||||
|
||||
## Tip
|
||||
|
||||
@ -34,26 +43,35 @@ The pause block is in milliseconds, so setting it to 1000 will have a pause of a
|
||||
|
||||
### Devising algorithms for shapes
|
||||
|
||||
An algorithm is a set of steps to follow to solve a problem. We can begin to draw shapes on the BBC micro:bit using an algorithm. For example, we could draw a straight line with this code:
|
||||
An algorithm is a set of steps to follow to solve a problem. We can begin to draw shapes on the BBC micro:bit using an algorithm.
|
||||
For example, we could draw a straight line with this code:
|
||||
|
||||

|
||||
```blocks
|
||||
for(let i = 0; i <=4; i++) {
|
||||
led.plot(i, 0);
|
||||
basic.pause(200)
|
||||
}
|
||||
```
|
||||
|
||||
Our algorithm is: increase **i** by 1 **from 0** to **4**, and **plot** the point **x=i**, **y=0**. The pause block allows this line to be animated (drawn frame by frame).
|
||||
|
||||
Try devising an algorithm for a diagonal line using the code above and the variable **i**. Your code should look like this; as our variable increases, so does the location that the BBC micro:bit is plotting at:
|
||||
|
||||

|
||||
|
||||
We can create more complex algorithms for more complex shapes, too. See the [challenges](/lessons/challenges) section for additional graphical challenges and solutions.
|
||||
Try devising an algorithm for a diagonal line using the code above and the variable **i**.
|
||||
```sim
|
||||
basic.forever(() => {
|
||||
for(let i = 0; i <=4; i++) {
|
||||
led.plot(i, i);
|
||||
basic.pause(200)
|
||||
}
|
||||
basic.clearScreen();
|
||||
})
|
||||
```
|
||||
|
||||
### Animations
|
||||
|
||||
Animations are changes happening at a certain rate. For example, we could add the `delay` block from the **Basic** drawer with our square algorithm – this will slowly draw a square (as an animation).
|
||||
Animations are changes happening at a certain rate. For example, we could add the `pause` block from the **Basic** drawer with our square algorithm – this will slowly draw a square (as an animation).
|
||||
|
||||
We could create more complex animations, for example we could make our BBC micro:bit display an explosion or fireworks.
|
||||
|
||||
See the [challenges](/lessons/challenges) section for some animation tasks.
|
||||
|
||||
### Image variables
|
||||
|
||||
We can create image variables so we can easily display an image at a later point. For example:
|
||||
|
@ -1,59 +0,0 @@
|
||||
# blocks - loops
|
||||
|
||||
An introduction to Loops for the Block Editor.
|
||||
|
||||
We may want to handle the user’s input multiple times or remain waiting for their input for a long time. We use loops to make sure that our code runs multiple times. These can be found in the **Loops** drawer.
|
||||
|
||||
### Forever loops
|
||||
|
||||
In the Variables tutorial we utilised a forever loop to create a counter:
|
||||
|
||||

|
||||
|
||||
This allows our BBC micro:bit to wait for the user to do something forever, for example wait for the user to press the correct button as the example above shows. If you were creating a quiz, you may want to loop forever until the user presses the correct button or answers the question.
|
||||
|
||||
### Repeat Loops
|
||||
|
||||
Repeat loops allow code to happen a certain amount of times. You may want to create a quiz that only gives the user a few tries to get the correct answer, for example. The number can be changed to facilitate your code.
|
||||
|
||||

|
||||
|
||||
The code above will scroll the message, “Hello world” three times.
|
||||
|
||||
### While & Until loops
|
||||
|
||||
The ‘repeat while’ loop allows you to continue looping some code until a condition is met. The empty socket next to the while loop allows you to connect some Logic and construct a statement.
|
||||
|
||||

|
||||
|
||||
The code above will scroll the message, “Press it!”, while the user hasn’t pressed the button.
|
||||
|
||||
* Drag a `set item` block from the **Variables** drawer. Click the **down arrow** and click **New Variable**, and type "pressed". Drag a `0` block from **Maths** to set the variable **pressed** to 0.
|
||||
* Drag a `repeat while` block from the **Loops** drawer and attach an `=` block from the **Logic** drawer. Drag `item` from the **Variables** drawer and click the **down arrow**, select ‘pressed’. Drag a `0` block from Maths and connect it to the other side of the equals. This will carry out the code until ‘pressed’ does not equal 0.
|
||||
* Add a `show string` block from the **Basic** drawer and change the message to "Press it!"
|
||||
* Add an `if` block from the **Logic** drawer, connect a `button pressed` block from the **Input** drawer, and add text from the **Basic** drawer. Change this to A to show we are waiting for button A.
|
||||
* Inside the ‘do’ part of the if statement, add a `set` block from the Variables drawer, click the **down arrow** to change it to **pressed** and drag a `1` from the Maths drawer
|
||||
* Lastly underneath the while loop, add another `show string` block and fill in the gaps.
|
||||
|
||||
Test the code above on actual hardware or on the simulator window.
|
||||
|
||||
We can also change the code in subtle ways to have a completely different effect:
|
||||
|
||||

|
||||
|
||||
This time we have to press the button three times to leave the while loop.
|
||||
|
||||
## Tip
|
||||
|
||||
You can press the arrow next to a word in a block to change it. For example, you can change Math functions or change a Logic statement.
|
||||
|
||||
### Count or for loops
|
||||
|
||||
A count loop allows you to loop a certain amount of times and to change a variable as you do so. For example, we can create a simple counting program:
|
||||
|
||||

|
||||
|
||||
The count loop will repeat a certain amount of times whilst changing a variable. You can click the arrow next to **i** to replace it with any of your own variables. So this program will display numbers 1 to 10.
|
||||
|
||||
This loop allows you to repeat code for the amount of times you want to without worrying about manually changing variables. You could use this for a counting program or a timer.
|
||||
|
BIN
docs/static/mb/blocks/comment-0.png
vendored
Before Width: | Height: | Size: 23 KiB |
BIN
docs/static/mb/blocks/crocodile-clips-1.jpg
vendored
Before Width: | Height: | Size: 302 KiB |
BIN
docs/static/mb/blocks/game-library/pic0.png
vendored
Before Width: | Height: | Size: 12 KiB |
BIN
docs/static/mb/blocks/lessons-0.png
vendored
Before Width: | Height: | Size: 54 KiB |
BIN
docs/static/mb/blocks/lessons-1.png
vendored
Before Width: | Height: | Size: 80 KiB |
BIN
docs/static/mb/blocks/lessons-2.png
vendored
Before Width: | Height: | Size: 69 KiB |
BIN
docs/static/mb/blocks/lessons-3.png
vendored
Before Width: | Height: | Size: 72 KiB |
BIN
docs/static/mb/blocks/lessons-4.png
vendored
Before Width: | Height: | Size: 48 KiB |
BIN
docs/static/mb/blocks/lessons-5.png
vendored
Before Width: | Height: | Size: 138 KiB |
Before Width: | Height: | Size: 38 KiB |
Before Width: | Height: | Size: 18 KiB |
Before Width: | Height: | Size: 26 KiB |
BIN
docs/static/mb/blocks/lessons/crocodile-clip-0.jpg
vendored
Before Width: | Height: | Size: 294 KiB |
BIN
docs/static/mb/blocks/lessons/glowing-pendulum-1.png
vendored
Before Width: | Height: | Size: 6.9 KiB |
BIN
docs/static/mb/blocks/lessons/glowing-pendulum-2.png
vendored
Before Width: | Height: | Size: 17 KiB |
BIN
docs/static/mb/blocks/lessons/glowing-pendulum-3.png
vendored
Before Width: | Height: | Size: 24 KiB |
BIN
docs/static/mb/blocks/lessons/glowing-pendulum-4.png
vendored
Before Width: | Height: | Size: 29 KiB |
BIN
docs/static/mb/blocks/lessons/glowing-pendulum-5.png
vendored
Before Width: | Height: | Size: 51 KiB |
BIN
docs/static/mb/blocks/lessons/graphics-1.png
vendored
Before Width: | Height: | Size: 43 KiB |
BIN
docs/static/mb/blocks/lessons/graphics-2.png
vendored
Before Width: | Height: | Size: 14 KiB |
BIN
docs/static/mb/blocks/lessons/graphics-3.png
vendored
Before Width: | Height: | Size: 14 KiB |
BIN
docs/static/mb/blocks/math-0.png
vendored
Before Width: | Height: | Size: 4.4 KiB |
BIN
docs/static/mb/blocks/math-1.png
vendored
Before Width: | Height: | Size: 5.7 KiB |
BIN
docs/static/mb/blocks/math-2.png
vendored
Before Width: | Height: | Size: 5.4 KiB |
BIN
docs/static/mb/blocks/math-3.png
vendored
Before Width: | Height: | Size: 4.3 KiB |
BIN
docs/static/mb/blocks/number-0.png
vendored
Before Width: | Height: | Size: 3.9 KiB |
BIN
docs/static/mb/blocks/number-1.png
vendored
Before Width: | Height: | Size: 2.6 KiB |
BIN
docs/static/mb/blocks/number-2.png
vendored
Before Width: | Height: | Size: 3.6 KiB |
BIN
docs/static/mb/blocks/number-3.png
vendored
Before Width: | Height: | Size: 4.2 KiB |
BIN
docs/static/mb/blocks/string-0.png
vendored
Before Width: | Height: | Size: 4.5 KiB |
BIN
docs/static/mb/blocks/string-1.png
vendored
Before Width: | Height: | Size: 5.7 KiB |
BIN
docs/static/mb/blocks/string-2.png
vendored
Before Width: | Height: | Size: 5.3 KiB |
BIN
docs/static/mb/blocks/to-td-0.png
vendored
Before Width: | Height: | Size: 11 KiB |
BIN
docs/static/mb/blocks/to-td-1.png
vendored
Before Width: | Height: | Size: 9.2 KiB |
BIN
docs/static/mb/blocks/to-td-2.png
vendored
Before Width: | Height: | Size: 22 KiB |
BIN
docs/static/mb/blocks/to-td-3.png
vendored
Before Width: | Height: | Size: 30 KiB |
BIN
docs/static/mb/blocks/to-td-4.png
vendored
Before Width: | Height: | Size: 7.3 KiB |
BIN
docs/static/mb/blocks/to-td-5.png
vendored
Before Width: | Height: | Size: 10 KiB |
BIN
docs/static/mb/blocks/to-td-6.png
vendored
Before Width: | Height: | Size: 24 KiB |
BIN
docs/static/mb/blocks/var-0.png
vendored
Before Width: | Height: | Size: 3.2 KiB |
BIN
docs/static/mb/blocks/var-1.png
vendored
Before Width: | Height: | Size: 5.1 KiB |
BIN
docs/static/mb/blocks/var-10.png
vendored
Before Width: | Height: | Size: 28 KiB |
BIN
docs/static/mb/blocks/var-2.png
vendored
Before Width: | Height: | Size: 3.5 KiB |
BIN
docs/static/mb/blocks/var-3.png
vendored
Before Width: | Height: | Size: 3.7 KiB |
BIN
docs/static/mb/blocks/var-4.png
vendored
Before Width: | Height: | Size: 10 KiB |
BIN
docs/static/mb/blocks/var-5.png
vendored
Before Width: | Height: | Size: 5.0 KiB |
BIN
docs/static/mb/blocks/var-6.png
vendored
Before Width: | Height: | Size: 7.2 KiB |
BIN
docs/static/mb/blocks/var-7.png
vendored
Before Width: | Height: | Size: 9.5 KiB |
BIN
docs/static/mb/blocks/var-8.png
vendored
Before Width: | Height: | Size: 16 KiB |
BIN
docs/static/mb/blocks/var-9.png
vendored
Before Width: | Height: | Size: 7.6 KiB |
BIN
docs/static/mb/device/pins-0.png
vendored
Before Width: | Height: | Size: 220 KiB After Width: | Height: | Size: 77 KiB |
BIN
docs/static/mb/device/usb-1.jpg
vendored
Before Width: | Height: | Size: 391 KiB |
BIN
docs/static/mb/device/usb-10.png
vendored
Before Width: | Height: | Size: 25 KiB |
BIN
docs/static/mb/device/usb-2.jpg
vendored
Before Width: | Height: | Size: 289 KiB |
BIN
docs/static/mb/device/usb-3.jpg
vendored
Before Width: | Height: | Size: 385 KiB |
BIN
docs/static/mb/device/usb-4.jpg
vendored
Before Width: | Height: | Size: 180 KiB |
BIN
docs/static/mb/device/usb-5.jpg
vendored
Before Width: | Height: | Size: 121 KiB |
BIN
docs/static/mb/device/usb-6.jpg
vendored
Before Width: | Height: | Size: 336 KiB |
BIN
docs/static/mb/device/usb-7.jpg
vendored
Before Width: | Height: | Size: 324 KiB |
BIN
docs/static/mb/device/usb-osx-chrome.png
vendored
Normal file
After Width: | Height: | Size: 186 KiB |
BIN
docs/static/mb/device/usb-osx-copy-file-error.png
vendored
Normal file
After Width: | Height: | Size: 10 KiB |
BIN
docs/static/mb/device/usb-osx-device.png
vendored
Normal file
After Width: | Height: | Size: 31 KiB |
BIN
docs/static/mb/device/usb-osx-dnd.png
vendored
Normal file
After Width: | Height: | Size: 36 KiB |
BIN
docs/static/mb/device/usb-osx-firefox-1.png
vendored
Normal file
After Width: | Height: | Size: 102 KiB |
BIN
docs/static/mb/device/usb-osx-firefox-2.png
vendored
Normal file
After Width: | Height: | Size: 63 KiB |
BIN
docs/static/mb/device/usb-windows-chrome.png
vendored
Normal file
After Width: | Height: | Size: 162 KiB |
Before Width: | Height: | Size: 164 KiB After Width: | Height: | Size: 164 KiB |
Before Width: | Height: | Size: 27 KiB After Width: | Height: | Size: 27 KiB |
BIN
docs/static/mb/device/usb-windows-edge-1.png
vendored
Normal file
After Width: | Height: | Size: 156 KiB |
BIN
docs/static/mb/device/usb-windows-edge-2.png
vendored
Normal file
After Width: | Height: | Size: 135 KiB |
BIN
docs/static/mb/device/usb-windows-firefox-1.png
vendored
Normal file
After Width: | Height: | Size: 4.9 KiB |
BIN
docs/static/mb/device/usb-windows-firefox-2.png
vendored
Normal file
After Width: | Height: | Size: 120 KiB |
BIN
docs/static/mb/device/usb-windows-ie11-1.png
vendored
Normal file
After Width: | Height: | Size: 164 KiB |
BIN
docs/static/mb/device/usb-windows-ie11-2.png
vendored
Normal file
After Width: | Height: | Size: 159 KiB |
Before Width: | Height: | Size: 160 KiB After Width: | Height: | Size: 160 KiB |
BIN
docs/static/mb/js/basicFuns.png
vendored
Before Width: | Height: | Size: 47 KiB After Width: | Height: | Size: 15 KiB |
BIN
docs/static/mb/js/basicIntell.png
vendored
Before Width: | Height: | Size: 128 KiB After Width: | Height: | Size: 22 KiB |
BIN
docs/static/mb/js/forIntell.png
vendored
Before Width: | Height: | Size: 30 KiB After Width: | Height: | Size: 7.9 KiB |
@ -101,38 +101,18 @@ namespace bluetooth {
|
||||
uart = new MicroBitUARTService(*uBit.ble, 61, 60);
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.
|
||||
*/
|
||||
//% help=bluetooth/uart-write
|
||||
//% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
//%
|
||||
void uartWrite(StringData *data) {
|
||||
startUartService();
|
||||
uart->send(ManagedString(data));
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
||||
*/
|
||||
//% help=bluetooth/uart-read
|
||||
//% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8
|
||||
//% parts="bluetooth"
|
||||
//%
|
||||
StringData* uartRead(StringData *del) {
|
||||
startUartService();
|
||||
return uart->readUntil(ManagedString(del)).leakData();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the delimiter corresponding string
|
||||
*/
|
||||
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
|
||||
//% weight=1
|
||||
//% parts="bluetooth"
|
||||
StringData* delimiters(Delimiters del) {
|
||||
ManagedString c("\n\n,$:.#"[max(0, min(6, (int)del))]);
|
||||
return c.leakData();
|
||||
}
|
||||
/**
|
||||
* Register code to run when the micro:bit is connected to over Bluetooth
|
||||
* @param body Code to run when a Bluetooth connection is established
|
||||
|
43
libs/microbit-bluetooth/bluetooth.ts
Normal file
@ -0,0 +1,43 @@
|
||||
namespace bluetooth {
|
||||
/**
|
||||
* Returns the delimiter corresponding string
|
||||
*/
|
||||
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
|
||||
//% weight=1 parts="bluetooth"
|
||||
export function delimiters(del: Delimiters): string {
|
||||
// even though it might not look like, this is more
|
||||
// (memory) efficient than the C++ implementation, because the
|
||||
// strings are statically allocated and take no RAM
|
||||
switch (del) {
|
||||
case Delimiters.NewLine: return "\n"
|
||||
case Delimiters.Comma: return ","
|
||||
case Delimiters.Dollar: return "$"
|
||||
case Delimiters.Colon: return ":"
|
||||
case Delimiters.Fullstop: return "."
|
||||
case Delimiters.Hash: return "#"
|
||||
default: return "\n"
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.
|
||||
*/
|
||||
//% help=bluetooth/uart-write
|
||||
//% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::uartWrite
|
||||
export function uartWrite(data: string): void {
|
||||
// dummy implementation for simulator
|
||||
console.log("UART Write: " + data)
|
||||
}
|
||||
|
||||
/**
|
||||
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
||||
*/
|
||||
//% help=bluetooth/uart-read
|
||||
//% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::uartRead
|
||||
export function uartRead(del: string): string {
|
||||
// dummy implementation for simulator
|
||||
return "???"
|
||||
}
|
||||
}
|
@ -5,6 +5,7 @@
|
||||
"README.md",
|
||||
"enums.d.ts",
|
||||
"shims.d.ts",
|
||||
"bluetooth.ts",
|
||||
"bluetooth.cpp"
|
||||
],
|
||||
"public": true,
|
||||
|
24
libs/microbit-bluetooth/shims.d.ts
vendored
@ -63,30 +63,6 @@ declare namespace bluetooth {
|
||||
//% parts="bluetooth" shim=bluetooth::startUartService
|
||||
function startUartService(): void;
|
||||
|
||||
/**
|
||||
* Writes to the Bluetooth UART service buffer. From there the data is transmitted over Bluetooth to a connected device.
|
||||
*/
|
||||
//% help=bluetooth/uart-write
|
||||
//% blockId=bluetooth_uart_write block="bluetooth uart write %data" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::uartWrite
|
||||
function uartWrite(data: string): void;
|
||||
|
||||
/**
|
||||
* Reads from the Bluetooth UART service buffer, returning its contents when the specified delimiter character is encountered.
|
||||
*/
|
||||
//% help=bluetooth/uart-read
|
||||
//% blockId=bluetooth_uart_read block="bluetooth uart read %del=bluetooth_uart_delimiter_conv" blockGap=8
|
||||
//% parts="bluetooth" shim=bluetooth::uartRead
|
||||
function uartRead(del: string): string;
|
||||
|
||||
/**
|
||||
* Returns the delimiter corresponding string
|
||||
*/
|
||||
//% blockId="bluetooth_uart_delimiter_conv" block="%del"
|
||||
//% weight=1
|
||||
//% parts="bluetooth" shim=bluetooth::delimiters
|
||||
function delimiters(del: Delimiters): string;
|
||||
|
||||
/**
|
||||
* Register code to run when the micro:bit is connected to over Bluetooth
|
||||
* @param body Code to run when a Bluetooth connection is established
|
||||
|
@ -37,4 +37,11 @@ namespace control {
|
||||
panic(98)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Display warning in the simulator.
|
||||
*/
|
||||
//% shim=pxtrt::runtimeWarning
|
||||
export function runtimeWarning(message: string) {
|
||||
}
|
||||
}
|
||||
|
@ -316,4 +316,9 @@ namespace pxtrt {
|
||||
void* getGlobalsPtr() {
|
||||
return globals;
|
||||
}
|
||||
|
||||
//%
|
||||
void runtimeWarning(StringData *s) {
|
||||
// noop for now
|
||||
}
|
||||
}
|
||||
|
Before Width: | Height: | Size: 3.1 KiB After Width: | Height: | Size: 3.1 KiB |
@ -27,6 +27,8 @@
|
||||
"serial.cpp",
|
||||
"serial.ts",
|
||||
"buffer.cpp",
|
||||
"pxtparts.json",
|
||||
"parts/speaker.svg",
|
||||
"_locales/fr/microbit-jsdoc-strings.json"
|
||||
],
|
||||
"public": true,
|
||||
|
78
libs/microbit/pxtparts.json
Normal file
@ -0,0 +1,78 @@
|
||||
{
|
||||
"ledmatrix": {
|
||||
"visual": "ledmatrix",
|
||||
"breadboardColumnsNeeded": 8,
|
||||
"breadboardStartRow": "h",
|
||||
"pinAllocation": {
|
||||
"type": "auto",
|
||||
"gpioPinsNeeded": [5, 5]
|
||||
},
|
||||
"assemblyStep": 0,
|
||||
"wires": [
|
||||
{"start": ["breadboard", "j", 0], "end": ["GPIO", 5], "color": "purple", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "j", 1], "end": ["GPIO", 6], "color": "purple", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "j", 2], "end": ["GPIO", 7], "color": "purple", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "j", 3], "end": ["GPIO", 8], "color": "purple", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "a", 7], "end": ["GPIO", 9], "color": "purple", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "a", 0], "end": ["GPIO", 0], "color": "green", "assemblyStep": 2},
|
||||
{"start": ["breadboard", "a", 1], "end": ["GPIO", 1], "color": "green", "assemblyStep": 2},
|
||||
{"start": ["breadboard", "a", 2], "end": ["GPIO", 2], "color": "green", "assemblyStep": 2},
|
||||
{"start": ["breadboard", "a", 3], "end": ["GPIO", 3], "color": "green", "assemblyStep": 2},
|
||||
{"start": ["breadboard", "j", 4], "end": ["GPIO", 4], "color": "green", "assemblyStep": 2}
|
||||
]
|
||||
},
|
||||
"buttonpair": {
|
||||
"visual": "buttonpair",
|
||||
"breadboardColumnsNeeded": 6,
|
||||
"breadboardStartRow": "f",
|
||||
"pinAllocation": {
|
||||
"type": "predefined",
|
||||
"pins": ["P13", "P12"]
|
||||
},
|
||||
"assemblyStep": 0,
|
||||
"wires": [
|
||||
{"start": ["breadboard", "j", 0], "end": ["GPIO", 0], "color": "yellow", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "a", 2], "end": "ground", "color": "blue", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "j", 3], "end": ["GPIO", 1], "color": "orange", "assemblyStep": 2},
|
||||
{"start": ["breadboard", "a", 5], "end": "ground", "color": "blue", "assemblyStep": 2}
|
||||
]
|
||||
},
|
||||
"neopixel": {
|
||||
"visual": "neopixel",
|
||||
"breadboardColumnsNeeded": 5,
|
||||
"breadboardStartRow": "h",
|
||||
"pinAllocation": {
|
||||
"type": "factoryfunction",
|
||||
"functionName": "neopixel.create",
|
||||
"pinArgPositions": [0],
|
||||
"otherArgPositions": [1]
|
||||
},
|
||||
"assemblyStep": 0,
|
||||
"wires": [
|
||||
{"start": ["breadboard", "j", 1], "end": "ground", "color": "blue", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "j", 2], "end": "threeVolt", "color": "red", "assemblyStep": 2},
|
||||
{"start": ["breadboard", "j", 3], "end": ["GPIO", 0], "color": "green", "assemblyStep": 2}
|
||||
]
|
||||
},
|
||||
"speaker": {
|
||||
"visual": {
|
||||
"image": "/parts/speaker.svg",
|
||||
"width": 500,
|
||||
"height": 500,
|
||||
"firstPin": [180, 135],
|
||||
"pinDist": 70,
|
||||
"extraColumnOffset": 1
|
||||
},
|
||||
"breadboardColumnsNeeded": 5,
|
||||
"breadboardStartRow": "f",
|
||||
"pinAllocation": {
|
||||
"type": "auto",
|
||||
"gpioPinsNeeded": 1
|
||||
},
|
||||
"assemblyStep": 0,
|
||||
"wires": [
|
||||
{"start": ["breadboard", "j", 1], "end": ["GPIO", 0], "color": "#ff80fa", "assemblyStep": 1},
|
||||
{"start": ["breadboard", "j", 3], "end": "ground", "color": "blue", "assemblyStep": 1}
|
||||
]
|
||||
}
|
||||
}
|
@ -1,141 +0,0 @@
|
||||
# From Block Editor to Touch Develop
|
||||
|
||||
#docs
|
||||
|
||||
The Block Editor and Touch Develop programming languages provide similar features, but are not identical in their functionality. This presents a learning opportunity for teachers and students: to understand a few basic concepts and how they are expressed in different programming languages. The objective is to make students better able to navigate the sea of programming languages they will encounter later.
|
||||
|
||||
## Concept 1: Inclusive and exclusive intervals
|
||||
|
||||
In mathematics, numeric intervals are a useful shorthand for expressing a sequence of numbers. For example, the notation [0,9] represents the sequence of ten numbers 0,1,2,3,4,5,6,7,8,9. This is known as an "inclusive" interval because the sequence includes the endpoints of the interval, namely 0 and 9. On the other hand, the interval (0,9) represents the sequence of eight numbers 1,2,3,4,5,6,7,8 and is known as "exclusive".
|
||||
|
||||
In the interval notation, the brackets "[" and "]" represent inclusive endpoints and the parentheses "(" and ")" represent exclusive endpoints. Brackets can be mixed and matched, so [0,9) represents the sequence of nine numbers 0,1,2,3,4,5,6,7,8 while (0,9] represents the sequence of nine numbers 1,2,3,4,5,6,7,8,9. Let's call the former interval "inclusive-exclusive" and the latter interval "exclusive-inclusive".
|
||||
|
||||
### Block Editor for loop uses a 0-based inclusive interval
|
||||
|
||||
Numeric intervals arise in the context of for loops, both in the Block Editor and Touch Develop. Here's a Block Editor for loop to draw a diagonal line from the top-left corner of the [LED screen](/device/screen) to the bottom-right corner. The loop iteration variable *i* ranges "from 0 to 4":
|
||||
|
||||

|
||||
|
||||
What interval does "from 0 to 4" represent? The answer is the inclusive interval [0,4], meaning that the loop iteration variable `i` will take on the values 0,1,2,3,4 over the *five* iterations of the loop. Experiments have shown that the *inclusive internal* is most familiar to students with no previous programming experience.
|
||||
|
||||
### TouchDevelop for loop uses a 0-based inclusive-exclusive interval
|
||||
|
||||
To achieve the same result in Touch Develop, we write the for loop slightly differently because the upper bound of the 0-based loop is *exclusive* rather than inclusive:
|
||||
|
||||
```
|
||||
for (let i = 0; i < 5; i++) {
|
||||
led.plot(i, i)
|
||||
}
|
||||
```
|
||||
|
||||
If we translated the Block Editor loop directly into Touch Develop, we would have:
|
||||
|
||||
```
|
||||
for (let i1 = 0; i1 < 4; i1++) {
|
||||
led.plot(i1, i1)
|
||||
}
|
||||
```
|
||||
|
||||
which would result in the loop iteration variable taking on values in the interval [0,4), namely 0,1,2,3.
|
||||
|
||||
### ~hint
|
||||
|
||||
The use of an exclusive upper-bound in for loops is standard practice in most programming languages. The basic reason for this is that with a 0-based inclusive lower bound, an exclusive upper bound U conveniently happens to be the length of the sequence represented by [0,U). This, of course, begs the question of why we count by starting with zero (0) in programming whereas we learn to count with one (1) in math.
|
||||
|
||||
### ~
|
||||
|
||||
## Concept 2: variable scope
|
||||
|
||||
A variable's *scope* is defined by two other concepts: its *lifetime* and *visibility*. Imagine program execution like a timeline with each point in the timeline being a step in the program's execution.
|
||||
|
||||
* A variable's *lifetime* can be thought of as an interval [birth, death) in the program execution during which the variable exists and has a value. Within that interval of the program execution, we say the variable is *alive*.
|
||||
* A variable is visible if its value can be read/written at a point in program execution. Visibility often is based on program structure and where the current point of program execution is. Imagine program structure like a house with only doors and no windows: if you are outside the house, you cannot see the objects (variables) inside the house - they are not visible to you even though they may exist; however, if you enter the house through the door, you can see the variables inside the house. The house itself may be divided into rooms, each of which defines another space in which certain variables are visible and others are not visible.
|
||||
|
||||
A variable is "in scope" at a program point if it is both alive and visible at that point. A variable is "not in scope" if it is not alive or if it is not visible.
|
||||
|
||||
### The Block Editor has variables with only global scope
|
||||
|
||||
In the Block Editor, all variables are *global* variables, which means that all variables are alive and visible during the entire program execution. Consider the following Block Editor program:
|
||||
|
||||

|
||||
|
||||
This program will draw a diagonal line, wait for one second and then show the value of global variable `i`. What number will be shown on the LED screen? After the fifth iteration of the for loop, the value of variable `i` is 4. At the end of this iteration, the variable `i` is incremented and takes on the value 5. Since 5 is greater than the upper (inclusive) endpoint of 4, the loop terminates with the value of `i` at 5.
|
||||
|
||||
### Problems with global variables: unintended interference
|
||||
|
||||
The Block Editor program belows shows a problem with having only variables with global scope. The intent of the program below is fairly clear: if the user presses button A, slowly draw a diagonal line from top-left to lower-right; if the user presses button B, slowly draw a diagonal line from top-right to lower-left. Pressing both buttons should lead to an X being displayed on the screen.
|
||||
|
||||

|
||||
|
||||
The problem with the above program is that we have two loops using the same global variable *i* as the loop iteration variable. If the user first presses button A and then quickly presses button B, the loops execute concurrently, both reading and writing global variable *i* - this can cause unexpected results (in particular, you won't necessarily end up with an X displayed on the screen). You can see this more clearly by pressing the convert button in the Block Editor and examining the Touch Develop code that implements the Block Editor semantics:
|
||||
|
||||

|
||||
|
||||
### JavaScript has variables with both local and global scope
|
||||
|
||||
In Touch Develop, in contrast to the Block Editor, the for-loop iteration variable has scope that is local to the loop:
|
||||
|
||||
```
|
||||
for (let i2 = 0; i2 < 5; i2++) {
|
||||
led.plot(i2, i2)
|
||||
}
|
||||
```
|
||||
|
||||
This means that:
|
||||
|
||||
1. the loop iteration variable *i* comes into existence just before the loop begins and goes out of existence just after the loop terminates.
|
||||
|
||||
2. the variable `i` only is visible from the code that appears textually between the `do` and `end` keywords of the for loop (this is known as lexical scoping).
|
||||
|
||||
The value of the loop iteration variable is completely determined by the semantics of the for loop. As such, Touch Develop doesn't allow the programmer to overwrite the value of a loop iteration variable, as shown below:
|
||||
|
||||
```
|
||||
for (let i3 = 0; i3 < 5; i3++) {
|
||||
led.plot(i3, i3)
|
||||
i3 = 42
|
||||
}
|
||||
```
|
||||
|
||||
### Why is local scope useful?
|
||||
|
||||
Local scope allows you to use the same variable name in different parts of a program without concern about interference (as with variables with global scope). Here's the Touch Develop program that implements the "X" program without interference:
|
||||
|
||||
```
|
||||
input.onButtonPressed(Button.A, () => {
|
||||
for (let i4 = 0; i4 < 5; i4++) {
|
||||
led.plot(i4, i4)
|
||||
basic.pause(1000)
|
||||
}
|
||||
})
|
||||
input.onButtonPressed(Button.B, () => {
|
||||
for (let i5 = 0; i5 < 5; i5++) {
|
||||
led.plot(4 - i5, i5)
|
||||
basic.pause(1000)
|
||||
}
|
||||
})
|
||||
```
|
||||
|
||||
Even though the same variable name (i) appears in both loops, these are different variables, each with their own lifetime and visibility (as defined by the for-loop).
|
||||
|
||||
## Concept 3: static types
|
||||
|
||||
A variable has a *static type* if it holds the same kind of value (integer, string, Boolean) everywhere that it is in scope. If a variable can hold values of different types at different program locations, then it does not have a static type.
|
||||
|
||||
### Block Editor blocks not plugged to an event will run
|
||||
|
||||
Blocks not plugged to an event will run. Blocks are running even if they are not inside of an `event`. As shown below, ``show string`` *Hello* will show a string on the LED screen one character at a time (scrolling from left to right).
|
||||
|
||||

|
||||
|
||||
### Google's Blockly variables do not have static types
|
||||
|
||||
In Blockly, a variable can hold different types of values at different program locations. As shown below, the global variable *Count* can be first set to a number and later to a string:
|
||||
|
||||

|
||||
|
||||
### Block Editor and Touch Develop variables have static types
|
||||
|
||||
In the Block Editor (based on Blockly) and Touch Develop, each variable has a static type. This means that some programs don't make sense, such as:
|
||||
|
||||

|
||||
|
@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "pxt-microbit",
|
||||
"version": "0.3.64",
|
||||
"version": "0.3.78",
|
||||
"description": "micro:bit target for PXT",
|
||||
"keywords": [
|
||||
"JavaScript",
|
||||
@ -29,6 +29,6 @@
|
||||
"typescript": "^1.8.7"
|
||||
},
|
||||
"dependencies": {
|
||||
"pxt-core": "0.3.74"
|
||||
"pxt-core": "0.3.92"
|
||||
}
|
||||
}
|
||||
|
188
sim/allocator.ts
@ -55,6 +55,63 @@ namespace pxsim {
|
||||
gpioNeeded: number,
|
||||
gpioAssigned: string[]
|
||||
}
|
||||
interface PowerUsage {
|
||||
topGround: boolean,
|
||||
topThreeVolt: boolean,
|
||||
bottomGround: boolean,
|
||||
bottomThreeVolt: boolean,
|
||||
singleGround: boolean,
|
||||
singleThreeVolt: boolean,
|
||||
}
|
||||
function isOnBreadboardBottom(location: WireLocationDefinition) {
|
||||
let isBot = false;
|
||||
if (location[0] === "breadboard") {
|
||||
let row = <string>location[1];
|
||||
isBot = 0 <= ["a", "b", "c", "d", "e"].indexOf(row);
|
||||
}
|
||||
return isBot;
|
||||
}
|
||||
const arrCount = (a: boolean[]) => a.reduce((p, n) => p + (n ? 1 : 0), 0);
|
||||
const arrAny = (a: boolean[]) => arrCount(a) > 0;
|
||||
function computePowerUsage(wireDef: WireDefinition): PowerUsage {
|
||||
let ends = [wireDef.start, wireDef.end];
|
||||
let endIsGround = ends.map(e => e === "ground");
|
||||
let endIsThreeVolt = ends.map(e => e === "threeVolt");
|
||||
let endIsBot = ends.map(e => isOnBreadboardBottom(e));
|
||||
let hasGround = arrAny(endIsGround);
|
||||
let hasThreeVolt = arrAny(endIsThreeVolt);
|
||||
let hasBot = arrAny(endIsBot);
|
||||
return {
|
||||
topGround: hasGround && !hasBot,
|
||||
topThreeVolt: hasThreeVolt && !hasBot,
|
||||
bottomGround: hasGround && hasBot,
|
||||
bottomThreeVolt: hasThreeVolt && hasBot,
|
||||
singleGround: hasGround,
|
||||
singleThreeVolt: hasThreeVolt
|
||||
};
|
||||
}
|
||||
function mergePowerUsage(powerUsages: PowerUsage[]) {
|
||||
let finalPowerUsage = powerUsages.reduce((p, n) => ({
|
||||
topGround: p.topGround || n.topGround,
|
||||
topThreeVolt: p.topThreeVolt || n.topThreeVolt,
|
||||
bottomGround: p.bottomGround || n.bottomGround,
|
||||
bottomThreeVolt: p.bottomThreeVolt || n.bottomThreeVolt,
|
||||
singleGround: n.singleGround ? p.singleGround === null : p.singleGround,
|
||||
singleThreeVolt: n.singleThreeVolt ? p.singleThreeVolt === null : p.singleThreeVolt,
|
||||
}), {
|
||||
topGround: false,
|
||||
topThreeVolt: false,
|
||||
bottomGround: false,
|
||||
bottomThreeVolt: false,
|
||||
singleGround: null,
|
||||
singleThreeVolt: null,
|
||||
});
|
||||
if (finalPowerUsage.singleGround)
|
||||
finalPowerUsage.topGround = finalPowerUsage.bottomGround = false;
|
||||
if (finalPowerUsage.singleThreeVolt)
|
||||
finalPowerUsage.topThreeVolt = finalPowerUsage.bottomThreeVolt = false;
|
||||
return finalPowerUsage;
|
||||
}
|
||||
function copyDoubleArray(a: string[][]) {
|
||||
return a.map(b => b.map(p => p));
|
||||
}
|
||||
@ -90,6 +147,7 @@ namespace pxsim {
|
||||
ground: mkRange(1, 26).map(n => <BBRowCol>["-", `${n}`]),
|
||||
},
|
||||
};
|
||||
private powerUsage: PowerUsage;
|
||||
|
||||
constructor(opts: AllocatorOpts) {
|
||||
this.opts = opts;
|
||||
@ -97,9 +155,17 @@ namespace pxsim {
|
||||
|
||||
private allocateLocation(location: WireLocationDefinition, opts: AllocLocOpts): Loc {
|
||||
if (location === "ground" || location === "threeVolt") {
|
||||
//special case if there is only a single ground or three volt pin in the whole build
|
||||
if (location === "ground" && this.powerUsage.singleGround) {
|
||||
let boardGroundPin = this.getBoardGroundPin();
|
||||
return {type: "dalboard", pin: boardGroundPin};
|
||||
} else if (location === "threeVolt" && this.powerUsage.singleThreeVolt) {
|
||||
let boardThreeVoltPin = this.getBoardThreeVoltPin();
|
||||
return {type: "dalboard", pin: boardThreeVoltPin};
|
||||
}
|
||||
|
||||
U.assert(!!opts.nearestBBPin);
|
||||
let nearLoc = opts.nearestBBPin;
|
||||
let nearestCoord = this.opts.getBBCoord(nearLoc);
|
||||
let nearestCoord = this.opts.getBBCoord(opts.nearestBBPin);
|
||||
let firstTopAndBot = [
|
||||
this.availablePowerPins.top.ground[0] || this.availablePowerPins.top.threeVolt[0],
|
||||
this.availablePowerPins.bottom.ground[0] || this.availablePowerPins.bottom.threeVolt[0]
|
||||
@ -111,31 +177,31 @@ namespace pxsim {
|
||||
//TODO
|
||||
}
|
||||
let nearTop = visuals.findClosestCoordIdx(nearestCoord, firstTopAndBot) == 0;
|
||||
let pins: BBRowCol[];
|
||||
let barPins: BBRowCol[];
|
||||
if (nearTop) {
|
||||
if (location === "ground") {
|
||||
pins = this.availablePowerPins.top.ground;
|
||||
barPins = this.availablePowerPins.top.ground;
|
||||
} else if (location === "threeVolt") {
|
||||
pins = this.availablePowerPins.top.threeVolt;
|
||||
barPins = this.availablePowerPins.top.threeVolt;
|
||||
}
|
||||
} else {
|
||||
if (location === "ground") {
|
||||
pins = this.availablePowerPins.bottom.ground;
|
||||
barPins = this.availablePowerPins.bottom.ground;
|
||||
} else if (location === "threeVolt") {
|
||||
pins = this.availablePowerPins.bottom.threeVolt;
|
||||
barPins = this.availablePowerPins.bottom.threeVolt;
|
||||
}
|
||||
}
|
||||
let pinCoords = pins.map(rowCol => {
|
||||
let pinCoords = barPins.map(rowCol => {
|
||||
return this.opts.getBBCoord(rowCol);
|
||||
});
|
||||
let pinIdx = visuals.findClosestCoordIdx(nearestCoord, pinCoords);
|
||||
let pin = pins[pinIdx];
|
||||
let closestPinIdx = visuals.findClosestCoordIdx(nearestCoord, pinCoords);
|
||||
let pin = barPins[closestPinIdx];
|
||||
if (nearTop) {
|
||||
this.availablePowerPins.top.ground.splice(pinIdx, 1);
|
||||
this.availablePowerPins.top.threeVolt.splice(pinIdx, 1);
|
||||
this.availablePowerPins.top.ground.splice(closestPinIdx, 1);
|
||||
this.availablePowerPins.top.threeVolt.splice(closestPinIdx, 1);
|
||||
} else {
|
||||
this.availablePowerPins.bottom.ground.splice(pinIdx, 1);
|
||||
this.availablePowerPins.bottom.threeVolt.splice(pinIdx, 1);
|
||||
this.availablePowerPins.bottom.ground.splice(closestPinIdx, 1);
|
||||
this.availablePowerPins.bottom.threeVolt.splice(closestPinIdx, 1);
|
||||
}
|
||||
return {type: "breadboard", rowCol: pin};
|
||||
} else if (location[0] === "breadboard") {
|
||||
@ -148,23 +214,41 @@ namespace pxsim {
|
||||
let idx = <number>location[1];
|
||||
let pin = opts.cmpGPIOPins[idx];
|
||||
return {type: "dalboard", pin: pin};
|
||||
} else if (location === "MOSI" || location === "MISO" || location === "SCK") {
|
||||
if (!this.opts.boardDef.spiPins)
|
||||
console.debug("No SPI pin mappings found!");
|
||||
let pin = (<any>this.opts.boardDef.spiPins)[location as string] as string;
|
||||
return {type: "dalboard", pin: pin};
|
||||
} else if (location === "SDA" || location === "SCL") {
|
||||
if (!this.opts.boardDef.i2cPins)
|
||||
console.debug("No I2C pin mappings found!");
|
||||
let pin = (<any>this.opts.boardDef.i2cPins)[location as string] as string;
|
||||
return {type: "dalboard", pin: pin};
|
||||
} else {
|
||||
//TODO
|
||||
U.assert(false);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
private allocatePowerWires(): WireInst[] {
|
||||
private getBoardGroundPin() {
|
||||
let boardGround = this.opts.boardDef.groundPins[0] || null;
|
||||
if (!boardGround) {
|
||||
console.log("No available ground pin on board!");
|
||||
//TODO
|
||||
}
|
||||
return boardGround;
|
||||
}
|
||||
private getBoardThreeVoltPin() {
|
||||
let threeVoltPin = this.opts.boardDef.threeVoltPins[0] || null;
|
||||
if (!threeVoltPin) {
|
||||
console.log("No available 3.3V pin on board!");
|
||||
//TODO
|
||||
}
|
||||
return threeVoltPin;
|
||||
}
|
||||
private allocatePowerWires(powerUsage: PowerUsage): WireInst[] {
|
||||
let boardGroundPin = this.getBoardGroundPin();
|
||||
let threeVoltPin = this.getBoardThreeVoltPin();
|
||||
let topLeft: BBRowCol = ["-", "26"];
|
||||
let botLeft: BBRowCol = ["-", "1"];
|
||||
let topRight: BBRowCol = ["-", "50"];
|
||||
@ -179,34 +263,71 @@ namespace pxsim {
|
||||
}
|
||||
const GROUND_COLOR = "blue";
|
||||
const POWER_COLOR = "red";
|
||||
const wires: WireInst[] = [
|
||||
{start: this.allocateLocation("ground", {nearestBBPin: top}),
|
||||
end: this.allocateLocation("ground", {nearestBBPin: bot}),
|
||||
color: GROUND_COLOR, assemblyStep: 0},
|
||||
{start: this.allocateLocation("ground", {nearestBBPin: top}),
|
||||
end: {type: "dalboard", pin: boardGround},
|
||||
color: GROUND_COLOR, assemblyStep: 0},
|
||||
{start: this.allocateLocation("threeVolt", {nearestBBPin: top}),
|
||||
end: this.allocateLocation("threeVolt", {nearestBBPin: bot}),
|
||||
color: POWER_COLOR, assemblyStep: 1},
|
||||
{start: this.allocateLocation("threeVolt", {nearestBBPin: top}),
|
||||
end: {type: "dalboard", pin: threeVoltPin},
|
||||
color: POWER_COLOR, assemblyStep: 1},
|
||||
];
|
||||
const wires: WireInst[] = [];
|
||||
let groundStep = 0;
|
||||
let threeVoltStep = (powerUsage.bottomGround || powerUsage.topGround) ? 1 : 0;
|
||||
if (powerUsage.bottomGround && powerUsage.topGround) {
|
||||
//bb top - <==> bb bot -
|
||||
wires.push({
|
||||
start: this.allocateLocation("ground", {nearestBBPin: top}),
|
||||
end: this.allocateLocation("ground", {nearestBBPin: bot}),
|
||||
color: GROUND_COLOR, assemblyStep: groundStep
|
||||
});
|
||||
}
|
||||
if (powerUsage.topGround) {
|
||||
//board - <==> bb top -
|
||||
wires.push({
|
||||
start: this.allocateLocation("ground", {nearestBBPin: top}),
|
||||
end: {type: "dalboard", pin: boardGroundPin},
|
||||
color: GROUND_COLOR, assemblyStep: groundStep
|
||||
});
|
||||
} else if (powerUsage.bottomGround) {
|
||||
//board - <==> bb bot -
|
||||
wires.push({
|
||||
start: this.allocateLocation("ground", {nearestBBPin: bot}),
|
||||
end: {type: "dalboard", pin: boardGroundPin},
|
||||
color: GROUND_COLOR, assemblyStep: groundStep
|
||||
});
|
||||
}
|
||||
if (powerUsage.bottomThreeVolt && powerUsage.bottomGround) {
|
||||
//bb top + <==> bb bot +
|
||||
wires.push({
|
||||
start: this.allocateLocation("threeVolt", {nearestBBPin: top}),
|
||||
end: this.allocateLocation("threeVolt", {nearestBBPin: bot}),
|
||||
color: POWER_COLOR, assemblyStep: threeVoltStep
|
||||
});
|
||||
}
|
||||
if (powerUsage.topThreeVolt) {
|
||||
//board + <==> bb top +
|
||||
wires.push({
|
||||
start: this.allocateLocation("threeVolt", {nearestBBPin: top}),
|
||||
end: {type: "dalboard", pin: threeVoltPin},
|
||||
color: POWER_COLOR, assemblyStep: threeVoltStep
|
||||
});
|
||||
} else if (powerUsage.bottomThreeVolt) {
|
||||
//board + <==> bb bot +
|
||||
wires.push({
|
||||
start: this.allocateLocation("threeVolt", {nearestBBPin: bot}),
|
||||
end: {type: "dalboard", pin: threeVoltPin},
|
||||
color: POWER_COLOR, assemblyStep: threeVoltStep
|
||||
});
|
||||
}
|
||||
return wires;
|
||||
}
|
||||
private allocateWire(wireDef: WireDefinition, opts: AllocWireOpts): WireInst {
|
||||
let ends = [wireDef.start, wireDef.end];
|
||||
let endIsPower = ends.map(e => e === "ground" || e === "threeVolt");
|
||||
//allocate non-power first so we know the nearest pin for the power end
|
||||
let endInsts = ends.map((e, idx) => !endIsPower[idx] ? this.allocateLocation(e, opts) : null)
|
||||
//allocate power pins closest to the other end of the wire
|
||||
endInsts = endInsts.map((e, idx) => {
|
||||
if (e)
|
||||
return e;
|
||||
let locInst = <BBLoc>endInsts[1 - idx];
|
||||
let locInst = <BBLoc>endInsts[1 - idx]; // non-power end
|
||||
let l = this.allocateLocation(ends[idx], {
|
||||
nearestBBPin: locInst.rowCol,
|
||||
startColumn: opts.startColumn,
|
||||
cmpGPIOPins: opts.cmpGPIOPins
|
||||
cmpGPIOPins: opts.cmpGPIOPins,
|
||||
});
|
||||
return l;
|
||||
});
|
||||
@ -404,8 +525,11 @@ namespace pxsim {
|
||||
let basicWires: WireInst[] = [];
|
||||
let cmpsAndWires: CmpAndWireInst[] = [];
|
||||
if (cmpList.length > 0) {
|
||||
basicWires = this.allocatePowerWires();
|
||||
let partialCmps = this.allocatePartialCmps();
|
||||
let allWireDefs = partialCmps.map(p => p.def.wires).reduce((p, n) => p.concat(n), []);
|
||||
let allPowerUsage = allWireDefs.map(w => computePowerUsage(w));
|
||||
this.powerUsage = mergePowerUsage(allPowerUsage);
|
||||
basicWires = this.allocatePowerWires(this.powerUsage);
|
||||
let cmpGPIOPins = this.allocateGPIOPins(partialCmps);
|
||||
let reverseMap = mkReverseMap(this.opts.boardDef.gpioPinMap);
|
||||
let cmpMicrobitPins = cmpGPIOPins.map(pins => pins.map(p => reverseMap[p]));
|
||||
|
@ -1,3 +1,5 @@
|
||||
/// <reference path="../node_modules/pxt-core/built/pxtsim.d.ts"/>
|
||||
|
||||
namespace pxsim {
|
||||
export class DalBoard extends BaseBoard {
|
||||
id: string;
|
||||
@ -69,12 +71,14 @@ namespace pxsim {
|
||||
}
|
||||
|
||||
initAsync(msg: SimulatorRunMessage): Promise<void> {
|
||||
super.initAsync(msg);
|
||||
|
||||
let options = (msg.options || {}) as RuntimeOptions;
|
||||
|
||||
let boardDef = CURRENT_BOARD; //TODO: read from pxt.json/pxttarget.json
|
||||
|
||||
let cmpsList = msg.parts;
|
||||
let cmpDefs = PART_DEFINITIONS; //TODO: read from pxt.json/pxttarget.json
|
||||
let cmpDefs = msg.partDefinitions || {}; //TODO: read from pxt.json/pxttarget.json
|
||||
let fnArgs = msg.fnArgs;
|
||||
|
||||
let viewHost = new visuals.BoardHost({
|
||||
|