Skip to content

Commit c6c9770

Browse files
committed
Merge remote-tracking branch 'micropython/master'
2 parents 09be96a + f7c4611 commit c6c9770

72 files changed

Lines changed: 1009 additions & 475 deletions

Some content is hidden

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

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ tests/*.out
3232
# Python cache files
3333
######################
3434
__pycache__/
35+
*.pyc
3536

3637
# Customized Makefile/project overrides
3738
######################

docs/esp8266/quickref.rst

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,14 @@ Tab-completion is useful to find out what methods an object has.
2323
Paste mode (ctrl-E) is useful to paste a large slab of Python code into
2424
the REPL.
2525

26-
The ``machine`` module::
26+
The :mod:`machine` module::
2727

2828
import machine
2929

3030
machine.freq() # get the current frequency of the CPU
3131
machine.freq(160000000) # set the CPU frequency to 160 MHz
3232

33-
The ``esp`` module::
33+
The :mod:`esp` module::
3434

3535
import esp
3636

@@ -40,7 +40,7 @@ The ``esp`` module::
4040
Networking
4141
----------
4242

43-
The ``network`` module::
43+
The :mod:`network` module::
4444

4545
import network
4646

@@ -69,13 +69,13 @@ A useful function for connecting to your local WiFi network is::
6969
pass
7070
print('network config:', wlan.ifconfig())
7171

72-
Once the network is established the ``socket`` module can be used
72+
Once the network is established the :mod:`socket <usocket>` module can be used
7373
to create and use TCP/UDP sockets as usual.
7474

7575
Delay and timing
7676
----------------
7777

78-
Use the ``time`` module::
78+
Use the :mod:`time <utime>` module::
7979

8080
import time
8181

@@ -165,14 +165,14 @@ Use the ``machine.ADC`` class::
165165
SPI bus
166166
-------
167167

168-
The SPI driver is implemented in software and works on all pins::
168+
There are two SPI drivers. One is implemented in software and works on all pins::
169169

170170
from machine import Pin, SPI
171171

172172
# construct an SPI bus on the given pins
173173
# polarity is the idle state of SCK
174174
# phase=0 means sample on the first edge of SCK, phase=1 means the second
175-
spi = SPI(baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
175+
spi = SPI(-1, baudrate=100000, polarity=1, phase=0, sck=Pin(0), mosi=Pin(2), miso=Pin(4))
176176

177177
spi.init(baudrate=200000) # set the baudrate
178178

@@ -194,13 +194,13 @@ Hardware SPI
194194
------------
195195

196196
The hardware SPI is faster (up to 80Mhz), but only works on following pins:
197-
``MISO`` is gpio2, ``MOSI`` is gpio13, and ``SCK`` is gpio14. It has the same
197+
``MISO`` is gpio12, ``MOSI`` is gpio13, and ``SCK`` is gpio14. It has the same
198198
methods as SPI, except for the pin parameters for the constructor and init
199199
(as those are fixed).
200200

201-
from machine import Pin, HSPI
201+
from machine import Pin, SPI
202202

203-
hspi = HSPI(baudrate=800000000, polarity=0, phase=0)
203+
hspi = SPI(0, baudrate=80000000, polarity=0, phase=0)
204204

205205

206206
I2C bus
@@ -253,15 +253,14 @@ The OneWire driver is implemented in software and works on all pins::
253253
ow.scan() # return a list of devices on the bus
254254
ow.reset() # reset the bus
255255
ow.readbyte() # read a byte
256-
ow.read(5) # read 5 bytes
257256
ow.writebyte(0x12) # write a byte on the bus
258257
ow.write('123') # write bytes on the bus
259258
ow.select_rom(b'12345678') # select a specific device by its ROM code
260259

261-
There is a specific driver for DS18B20 devices::
260+
There is a specific driver for DS18S20 and DS18B20 devices::
262261

263-
import time
264-
ds = onewire.DS18B20(ow)
262+
import time, ds18x20
263+
ds = ds18x20.DS18X20(ow)
265264
roms = ds.scan()
266265
ds.convert_temp()
267266
time.sleep_ms(750)

docs/esp8266/tutorial/onewire.rst

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,19 +6,19 @@ The 1-wire bus is a serial bus that uses just a single wire for communication
66
is a very popular 1-wire device, and here we show how to use the onewire module
77
to read from such a device.
88

9-
For the following code to work you need to have at least one DS18B20 temperature
9+
For the following code to work you need to have at least one DS18S20 or DS18B20 temperature
1010
sensor with its data line connected to GPIO12. You must also power the sensors
1111
and connect a 4.7k Ohm resistor between the data pin and the power pin. ::
1212

1313
import time
1414
import machine
15-
import onewire
15+
import onewire, ds18x20
1616

1717
# the device is on GPIO12
1818
dat = machine.Pin(12)
1919

2020
# create the onewire object
21-
ds = onewire.DS18B20(onewire.OneWire(dat))
21+
ds = ds18x20.DS18X20(onewire.OneWire(dat))
2222

2323
# scan for devices on the bus
2424
roms = ds.scan()

docs/library/pyb.USB_HID.rst

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
.. currentmodule:: pyb
2+
3+
class USB_HID -- USB Human Interface Device (HID)
4+
=================================================
5+
6+
The USB_HID class allows creation of an object representing the USB
7+
Human Interface Device (HID) interface. It can be used to emulate
8+
a peripheral such as a mouse or keyboard.
9+
10+
Before you can use this class, you need to use :meth:`pyb.usb_mode()` to set the USB mode to include the HID interface.
11+
12+
Constructors
13+
------------
14+
15+
.. class:: pyb.USB_HID()
16+
17+
Create a new USB_HID object.
18+
19+
20+
Methods
21+
-------
22+
23+
.. method:: USB_HID.send(data)
24+
25+
Send data over the USB HID interface:
26+
27+
- ``data`` is the data to send (a tuple/list of integers, or a
28+
bytearray).

docs/library/pyb.rst

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ Miscellaneous functions
188188
Takes a 4-tuple (or list) and sends it to the USB host (the PC) to
189189
signal a HID mouse-motion event.
190190

191-
.. note:: This function is deprecated. Use pyb.USB_HID().send(...) instead.
191+
.. note:: This function is deprecated. Use :meth:`pyb.USB_HID.send()` instead.
192192

193193
.. function:: info([dump_alloc_table])
194194

@@ -254,6 +254,33 @@ Miscellaneous functions
254254

255255
Returns a string of 12 bytes (96 bits), which is the unique ID of the MCU.
256256

257+
.. function:: usb_mode([modestr], vid=0xf055, pid=0x9801, hid=pyb.hid_mouse)
258+
259+
If called with no arguments, return the current USB mode as a string.
260+
261+
If called with ``modestr`` provided, attempts to set USB mode.
262+
This can only be done when called from ``boot.py`` before
263+
:meth:`pyb.main()` has been called. The following values of
264+
``modestr`` are understood:
265+
266+
- ``None``: disables USB
267+
- ``'VCP'``: enable with VCP (Virtual COM Port) interface
268+
- ``'VCP+MSC'``: enable with VCP and MSC (mass storage device class)
269+
- ``'VCP+HID'``: enable with VCP and HID (human interface device)
270+
271+
For backwards compatibility, ``'CDC'`` is understood to mean
272+
``'VCP'`` (and similarly for ``'CDC+MSC'`` and ``'CDC+HID'``).
273+
274+
The ``vid`` and ``pid`` parameters allow you to specify the VID
275+
(vendor id) and PID (product id).
276+
277+
If enabling HID mode, you may also specify the HID details by
278+
passing the ``hid`` keyword parameter. It takes a tuple of
279+
(subclass, protocol, max packet length, polling interval, report
280+
descriptor). By default it will set appropriate values for a USB
281+
mouse. There is also a ``pyb.hid_keyboard`` constant, which is an
282+
appropriate tuple for a USB keyboard.
283+
257284
Classes
258285
-------
259286

@@ -277,4 +304,5 @@ Classes
277304
pyb.Switch.rst
278305
pyb.Timer.rst
279306
pyb.UART.rst
307+
pyb.USB_HID.rst
280308
pyb.USB_VCP.rst

docs/pyboard/quickref.rst

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,12 @@
33
Quick reference for the pyboard
44
===============================
55

6+
The below pinout is for PYBv1.0. You can also view pinouts for
7+
other versions of the pyboard:
8+
`PYBv1.1 <http://micropython.org/resources/pybv11-pinout.jpg>`__
9+
or `PYBLITEv1.0-AC <http://micropython.org/resources/pyblitev10ac-pinout.jpg>`__
10+
or `PYBLITEv1.0 <http://micropython.org/resources/pyblitev10-pinout.jpg>`__.
11+
612
.. image:: http://micropython.org/resources/pybv10-pinout.jpg
713
:alt: PYBv1.0 pinout
814
:width: 700px
@@ -14,14 +20,25 @@ See :mod:`pyb`. ::
1420

1521
import pyb
1622

17-
pyb.delay(50) # wait 50 milliseconds
18-
pyb.millis() # number of milliseconds since bootup
1923
pyb.repl_uart(pyb.UART(1, 9600)) # duplicate REPL on UART(1)
2024
pyb.wfi() # pause CPU, waiting for interrupt
2125
pyb.freq() # get CPU and bus frequencies
2226
pyb.freq(60000000) # set CPU freq to 60MHz
2327
pyb.stop() # stop CPU, waiting for external interrupt
2428

29+
Delay and timing
30+
----------------
31+
32+
Use the :mod:`time <utime>` module::
33+
34+
import time
35+
36+
time.sleep(1) # sleep for 1 second
37+
time.sleep_ms(500) # sleep for 500 milliseconds
38+
time.sleep_us(10) # sleep for 10 microseconds
39+
start = time.ticks_ms() # get value of millisecond counter
40+
delta = time.ticks_diff(start, time.ticks_ms()) # compute time difference
41+
2542
LEDs
2643
----
2744

docs/pyboard/tutorial/usb_mouse.rst

Lines changed: 15 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,23 @@ will look something like this::
1313

1414
import pyb
1515
#pyb.main('main.py') # main script to run after this one
16-
#pyb.usb_mode('CDC+MSC') # act as a serial and a storage device
17-
#pyb.usb_mode('CDC+HID') # act as a serial device and a mouse
16+
#pyb.usb_mode('VCP+MSC') # act as a serial and a storage device
17+
#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
1818

1919
To enable the mouse mode, uncomment the last line of the file, to
2020
make it look like::
2121

22-
pyb.usb_mode('CDC+HID') # act as a serial device and a mouse
22+
pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
2323

2424
If you already changed your ``boot.py`` file, then the minimum code it
2525
needs to work is::
2626

2727
import pyb
28-
pyb.usb_mode('CDC+HID')
28+
pyb.usb_mode('VCP+HID')
2929

30-
This tells the pyboard to configure itself as a CDC (serial) and HID
31-
(human interface device, in our case a mouse) USB device when it boots
32-
up.
30+
This tells the pyboard to configure itself as a VCP (Virtual COM Port,
31+
ie serial port) and HID (human interface device, in our case a mouse)
32+
USB device when it boots up.
3333

3434
Eject/unmount the pyboard drive and reset it using the RST switch.
3535
Your PC should now detect the pyboard as a mouse!
@@ -41,7 +41,8 @@ To get the py-mouse to do anything we need to send mouse events to the PC.
4141
We will first do this manually using the REPL prompt. Connect to your
4242
pyboard using your serial program and type the following::
4343

44-
>>> pyb.hid((0, 10, 0, 0))
44+
>>> hid = pyb.USB_HID()
45+
>>> hid.send((0, 10, 0, 0))
4546

4647
Your mouse should move 10 pixels to the right! In the command above you
4748
are sending 4 pieces of information: button status, x, y and scroll. The
@@ -52,7 +53,7 @@ Let's make the mouse oscillate left and right::
5253
>>> import math
5354
>>> def osc(n, d):
5455
... for i in range(n):
55-
... pyb.hid((0, int(20 * math.sin(i / 10)), 0, 0))
56+
... hid.send((0, int(20 * math.sin(i / 10)), 0, 0))
5657
... pyb.delay(d)
5758
...
5859
>>> osc(100, 50)
@@ -100,9 +101,10 @@ In ``main.py`` put the following code::
100101

101102
switch = pyb.Switch()
102103
accel = pyb.Accel()
104+
hid = pyb.USB_HID()
103105

104106
while not switch():
105-
pyb.hid((0, accel.x(), accel.y(), 0))
107+
hid.send((0, accel.x(), accel.y(), 0))
106108
pyb.delay(20)
107109

108110
Save your file, eject/unmount your pyboard drive, and reset it using the RST
@@ -112,7 +114,7 @@ the mouse around. Try it out, and see if you can make the mouse stand still!
112114
Press the USR switch to stop the mouse motion.
113115

114116
You'll note that the y-axis is inverted. That's easy to fix: just put a
115-
minus sign in front of the y-coordinate in the ``pyb.hid()`` line above.
117+
minus sign in front of the y-coordinate in the ``hid.send()`` line above.
116118

117119
Restoring your pyboard to normal
118120
--------------------------------
@@ -121,9 +123,9 @@ If you leave your pyboard as-is, it'll behave as a mouse everytime you plug
121123
it in. You probably want to change it back to normal. To do this you need
122124
to first enter safe mode (see above), and then edit the ``boot.py`` file.
123125
In the ``boot.py`` file, comment out (put a # in front of) the line with the
124-
``CDC+HID`` setting, so it looks like::
126+
``VCP+HID`` setting, so it looks like::
125127

126-
#pyb.usb_mode('CDC+HID') # act as a serial device and a mouse
128+
#pyb.usb_mode('VCP+HID') # act as a serial device and a mouse
127129

128130
Save your file, eject/unmount the drive, and reset the pyboard. It is now
129131
back to normal operating mode.

0 commit comments

Comments
 (0)