Skip to content

Commit 5bd0649

Browse files
Initial commit.
Based on the V2 docs as of f3908e2c295ec9e4ad9da47541021af1c948b827 and upstream MicroPython docs for the few modules we don't have micro:bit docs for. Updated after merge of bbcmicrobit/micropython#725 Some notes on discrepancies in discrepancies.md. I'll make more tweaks as I get feedback on issues raised. `builtins` contain too many symbols for MicroPython. I'll fix that by comparing with the introspection results in a subsequent commit.
0 parents  commit 5bd0649

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+7808
-0
lines changed

.github/workflows/node.js.yml

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
2+
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
3+
4+
name: Node.js CI
5+
6+
on:
7+
push:
8+
branches: [ main ]
9+
pull_request:
10+
branches: [ main ]
11+
12+
jobs:
13+
build:
14+
15+
runs-on: ubuntu-latest
16+
17+
strategy:
18+
matrix:
19+
node-version: [16.x]
20+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
21+
22+
steps:
23+
- uses: actions/checkout@v2
24+
- name: Use Node.js ${{ matrix.node-version }}
25+
uses: actions/setup-node@v2
26+
with:
27+
node-version: ${{ matrix.node-version }}
28+
cache: 'npm'
29+
- run: npm ci
30+
- run: npm run build --if-present
31+
- run: npm test

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/typeshed.json
2+
/node_modules/
3+
*.rst

.vscode/settings.json

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"python.formatting.provider": "black",
3+
"python.analysis.typeshedPaths": [
4+
"/Users/mth/Development/microbit-foundation/micropython-microbit-stubs/typeshed/"
5+
],
6+
"python.analysis.typeCheckingMode": "basic"
7+
}

README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# micro:bit MicroPython stubs for editor support
2+
3+
## Credit and licensing
4+
5+
builtins and utility definitions are derived from [typeshed](https://github.com/python/typeshed). Further work is required to review these to ensure that we don't have stubs for definitions MicroPython does not support. These stubs don't provide documentation.
6+
7+
MicroPython and micro:bit stubs are based on the restructured text documentation for [micro:bit](https://github.com/bbcmicrobit/micropython/tree/v2-docs) and, where not otherwise covered in micro:bit-specific docs, [MicroPython](https://github.com/micropython/micropython/tree/master/docs/library). These stubs provide documentation.
8+
9+
Typeshed is Apache 2.0 licensed.
10+
11+
MicroPython is MIT Licensed and Copyright (c) 2013-2021 Damien P. George.
12+
MicroPython for the micro:bit is MIT licensed and Copyright (c) 2013-2016 The MicroPython-on-micro:bit Developers (see https://github.com/bbcmicrobit/micropython/blob/v2-docs/AUTHORS).
13+

browser-package.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env python3
2+
"""
3+
Outputs a JSON file containing all the stubs and a Pyright config file.
4+
5+
This is intended for web apps that need the stubs client side.
6+
"""
7+
import os
8+
import json
9+
10+
results = dict(files={})
11+
for (source, prefix) in (("typeshed", "/typeshed/"), ("config", "/src/")):
12+
for (dirpath, dirnames, filenames) in os.walk(source):
13+
for f in sorted(filenames):
14+
path = os.path.join(dirpath, f)
15+
destination = os.path.join(
16+
prefix, os.path.join(*path.split(os.path.sep)[1:])
17+
)
18+
print(f"{path} -> {destination}")
19+
text = open(path, "r").read()
20+
results["files"][destination] = text
21+
22+
with open("typeshed.json", "w") as f:
23+
f.write(json.dumps(results, indent=2))

config/pyrightconfig.json

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"pythonVersion": "3.6",
3+
"pythonPlatform": "Linux",
4+
"typeCheckingMode": "basic",
5+
"typeshedPath": "/typeshed/",
6+
"reportMissingModuleSource": false,
7+
"reportWildcardImportFromLibrary": false,
8+
"verboseOutput": true
9+
}

discrepancies.md

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
# Notes
2+
3+
These are working notes on discrepancies found when creating these stubs.
4+
5+
## Cross-cutting issues
6+
7+
Module docs throughout are a bit arbitrary and need review.
8+
9+
Various docs don't line up exactly because @overload is the best way to represent their typings so requires docs to be customised for each scenario.
10+
11+
## Module-specific issues
12+
13+
### gc
14+
15+
isenabled is undocumented in MicroPython docs (not added).
16+
17+
### audio
18+
19+
[AudioFrame.copyfrom](https://github.com/microbit-foundation/micropython-microbit-v2/blob/bca4dc5690998e8e5de07019a44185fc9b9ea080/src/codal_port/modaudio.c#L301) is undocumented (not added).
20+
21+
AudioFrame implements some operations that aren't clearly documented (e.g. you can add them, multiple by an integer). I added stubs for these as they're used in examples.
22+
23+
### uart
24+
25+
uart.readinto is misindented in the docs (minor).
26+
there's a method documented as having been removed so I've omitted it from the stubs
27+
28+
### microbit
29+
30+
- image - type of buffer in second __init__ option
31+
- text modified to split across __init__ definitions
32+
- "Same as" language is unhelpful.
33+
- __sub__ and __div__ added based on examples but not in docs.
34+
- microphone - the doc style here is a bit different to elsewhere, might be less good in Pyright?
35+
36+
### micropython
37+
38+
- schedule is undocumented (I've not added it)
39+
- micropython.schedule is missing from our docs. Why? It is on the device (checked on V2).
40+
41+
### neopixel
42+
43+
Has long but important module docstring with important warnings for the user.
44+
I've removed the images and reproduced it otherwise in full.
45+
46+
Some complication with write/show. Fine for stubs but will need revisiting for docs.
47+
48+
ws2812_write is undocumented (here and in microbit module)
49+
ORDER is undocumented
50+
51+
### pins (in microbit)
52+
53+
get_analog_period_microseconds isn't documented
54+
55+
pin_logo isn't really a pin... it just has:
56+
CAPACITIVE
57+
RESISTIVE
58+
is_touched
59+
set_touch_mode
60+
how to model this? currently incorrectly a touch pin.
61+
62+
pins need class docs
63+
64+
NO_PULL etc. aren't available on MicroBitDigitalPin. You need to use the instances
65+
to access. Can/should we model this?
66+
67+
### machine
68+
69+
mem16
70+
mem32
71+
mem8
72+
are undocumented
73+
74+
### os
75+
76+
ilistdir and stat are undocumented (not added)
77+
78+
### radio
79+
80+
In the docs and stubs but not on a V2 (V1 not checked):
81+
82+
```
83+
>>> radio.RATE_250KBIT
84+
Traceback (most recent call last):
85+
File "<stdin>", line 1, in <module>
86+
AttributeError: 'module' object has no attribute 'RATE_250KBIT'
87+
```
88+
89+
It's commented out in the source: https://github.com/microbit-foundation/micropython-microbit-v2/blame/eba8995843ebc7246765b364710543c9ffee344a/src/codal_port/modradio.c#L269
90+
91+
I've added a note to the docstring for now.
92+
93+
### microphone
94+
95+
```python
96+
>>> from microbit import *
97+
>>> repr(microphone.current_event())
98+
'None'
99+
>>> repr(microphone.current_event())
100+
"SoundEvent('loud')"
101+
```
102+
103+
but [docs](https://microbit-micropython.readthedocs.io/en/v2-docs/microphone.html#microbit.microphone.current_event) (and for now stubs) claim it cannot return None
104+
105+
Raised https://github.com/microbit-foundation/micropython-microbit-v2/issues/86
106+
107+
## Interesting code failures
108+
109+
In waveforms.py
110+
111+
```python
112+
frames = [ None ] * 32
113+
```
114+
115+
...is a problematic initialization pattern for arrays. Can we relax the rules?
116+
Is the pattern widespread? What options does Pyright have to relax behaviour around None?

examples/micropython/LICENSE

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2013-2016 The MicroPython-on-micro:bit Developers, as listed
4+
in the accompanying AUTHORS file
5+
6+
Permission is hereby granted, free of charge, to any person obtaining a copy
7+
of this software and associated documentation files (the "Software"), to deal
8+
in the Software without restriction, including without limitation the rights
9+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
copies of the Software, and to permit persons to whom the Software is
11+
furnished to do so, subject to the following conditions:
12+
13+
The above copyright notice and this permission notice shall be included in
14+
all copies or substantial portions of the Software.
15+
16+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
THE SOFTWARE.

examples/micropython/analog_watch.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
from microbit import *
2+
3+
hands = Image.ALL_CLOCKS
4+
5+
#A centre dot of brightness 2.
6+
ticker_image = Image("2\n").crop(-2,-2,5,5)
7+
8+
#Adjust these to taste
9+
MINUTE_BRIGHT = 0.1111
10+
HOUR_BRIGHT = 0.55555
11+
12+
#Generate hands for 5 minute intervals
13+
def fiveticks():
14+
fivemins = 0
15+
hours = 0
16+
while True:
17+
yield hands[fivemins]*MINUTE_BRIGHT + hands[hours]*HOUR_BRIGHT
18+
fivemins = (fivemins+1)%12
19+
hours = (hours + (fivemins == 0))%12
20+
21+
#Generate hands with ticker superimposed for 1 minute intervals.
22+
def ticks():
23+
on = True
24+
for face in fiveticks():
25+
for i in range(5):
26+
if on:
27+
yield face + ticker_image
28+
else:
29+
yield face - ticker_image
30+
on = not on
31+
32+
#Run a clock speeded up 60 times, so we can watch the animation.
33+
for tick in ticks():
34+
display.show(tick)
35+
sleep(1000)

examples/micropython/asmleds.py.todo

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
"""
2+
This script uses the inline assembler to make the LEDs light up
3+
in a pattern based on how they are multiplexed in rows/cols.
4+
"""
5+
6+
# row pins: 13, 14, 15
7+
# col pins: 4..12 inclusive
8+
# GPIO words starting at 0x50000500:
9+
# RESERVED, OUT, OUTSET, OUTCLR, IN, DIR, DIRSET, DIRCLR
10+
11+
@micropython.asm_thumb
12+
def led_cycle():
13+
b(START)
14+
15+
# DELAY routine
16+
label(DELAY)
17+
mov(r3, 0xa0)
18+
lsl(r3, r3, 11)
19+
label(delay_loop)
20+
sub(r3, 1)
21+
bne(delay_loop)
22+
bx(lr)
23+
24+
label(START)
25+
26+
cpsid('i') # disable interrupts so we control the display
27+
28+
mov(r0, 0x50) # r0=0x50
29+
lsl(r0, r0, 16) # r0=0x500000
30+
add(r0, 0x05) # r0=0x500005
31+
lsl(r0, r0, 8) # r0=0x50000500 -- this points to GPIO registers
32+
mov(r1, 0b111)
33+
lsl(r1, r1, 13) # r1=0xe000
34+
str(r1, [r0, 8]) # pull all rows high
35+
36+
mov(r1, 1 << 4) # r1 holds current col bit
37+
mov(r2, 9) # r2 holds number of cols left
38+
label(loop_on)
39+
str(r1, [r0, 12]) # pull col low to turn LEDs on
40+
bl(DELAY) # wait
41+
lsl(r1, r1, 1) # shift to next col
42+
sub(r2, 1) # decrease col counter
43+
bne(loop_on) # loop while there are still cols left
44+
45+
mov(r1, 1 << 4) # r1 holds current col bit
46+
mov(r2, 9) # r2 holds number of cols left
47+
label(loop_off)
48+
str(r1, [r0, 8]) # pull col high to turn LEDs off
49+
bl(DELAY) # wait
50+
lsl(r1, r1, 1) # shift to next col
51+
sub(r2, 1) # decrease col counter
52+
bne(loop_off) # loop while there are still cols left
53+
54+
cpsie('i') # enable interrupts
55+
56+
for i in range(4):
57+
led_cycle()

0 commit comments

Comments
 (0)