Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1# STUB: do NOT upload to the ESP, it's used to run the tests 

2def delay(ms): 

3 """ 

4 Delay for the given number of milliseconds. 

5 """ 

6 

7 

8def udelay(us): 

9 """ 

10 Delay for the given number of microseconds. 

11 """ 

12 

13def millis(): 

14 """ 

15 Returns the number of milliseconds since the board was last reset. 

16 """ 

17 

18 

19 

20def micros(): 

21 """ 

22 Returns the number of microseconds since the board was last reset. 

23 """ 

24 

25 

26 

27def elapsed_millis(start): 

28 """ 

29 Returns the number of milliseconds which have elapsed since ``start``. 

30 """ 

31 

32 

33 

34def elapsed_micros(start): 

35 """ 

36 Returns the number of microseconds which have elapsed since ``start``. 

37 """ 

38 

39 

40 

41def hard_reset(): 

42 """ 

43 Resets the pyboard in a manner similar to pushing the external RESET 

44 button. 

45 """ 

46 

47 

48def bootloader(): 

49 """ 

50 Activate the bootloader without BOOT\* pins. 

51 """ 

52 

53 

54 

55def disable_irq(): 

56 """ 

57 Disable interrupt requests. 

58 """ 

59 

60 

61 

62def enable_irq(state=True): 

63 """ 

64 Enable interrupt requests. 

65 """ 

66 

67 

68 

69def freq(self, sysclk=None, hclk=None, pclk1=None, pclk2=None): 

70 """ 

71 If given no arguments, returns a tuple of clock frequencies. 

72 """ 

73 

74 

75 

76def wfi(): 

77 """ 

78 Wait for an internal or external interrupt. 

79 """ 

80 

81 

82 

83def stop(): 

84 """ 

85 Put the pyboard in a "sleeping" state. 

86 """ 

87 

88 

89def standby(): 

90 """ 

91 Put the pyboard into a "deep sleep" state. 

92 """ 

93 

94 

95 

96def info(dump_alloc_table): 

97 """ 

98 Print out lots of information about the board. 

99 """ 

100 

101 

102 

103def main(filename): 

104 """ 

105 Set the filename of the main script to run after boot.py is finished. 

106 """ 

107 

108 

109 

110def mount(device, mountpoint, readonly=False, mkfs=False): 

111 """ 

112 Mount a block device and make it available as part of the filesystem. 

113 """ 

114 

115 

116 

117def repl_uart(uart): 

118 """ 

119 Get or set the UART object where the REPL is repeated on. 

120 """ 

121 

122 

123 

124def rng(): 

125 """ 

126 Return a 30-bit hardware generated random number. 

127 """ 

128 

129 

130 

131def sync(): 

132 """ 

133 Sync all file systems. 

134 """ 

135 

136 

137 

138def unique_id(): 

139 """ 

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

141 """ 

142 

143 

144 

145def usb_mode(modestr, vid=0xf055, pid=0x9801, hid=0): 

146 """ 

147 If called with no arguments, return the current USB mode as a string. 

148 If called with modestr provided, attempts to set USB mode. 

149 """ 

150 

151 

152 

153class Accel: 

154 

155 def filtered_xyz(self): 

156 """ 

157 Get a 3-tuple of filtered x, y and z values. 

158 """ 

159 

160 

161 def tilt(self): 

162 """ 

163 Get the tilt register. 

164 """ 

165 

166 

167 def x(self): 

168 """ 

169 Get the x-axis value. 

170 """ 

171 

172 

173 def y(self): 

174 """ 

175 Get the y-axis value. 

176 """ 

177 

178 

179 def z(self): 

180 """ 

181 Get the z-axis value. 

182 """ 

183 

184 

185 def write(self, register, value): 

186 """write.""" 

187 

188 def read(self, register): 

189 """read.""" 

190 

191 

192class ADC: 

193 ATTN_0DB = 0 

194 ATTN_2_5DB = 1 

195 ATTN_6DB = 2 

196 ATTN_11DB = 3 

197 

198 def __init__(self, pin): 

199 """ 

200 Create an ADC object associated with the given pin. 

201 This allows you to then read analog values on that pin. 

202 """ 

203 

204 

205 def read_timed(self): 

206 """read timeout.""" 

207 

208 def atten(self, num): 

209 """attenutaiton.""" 

210 

211 def read_timed_stop(self): 

212 """read timed.""" 

213 

214 def read(self): 

215 """ 

216 Read the value on the analog pin and return it. The returned value 

217 will be between 0 and 4095. 

218 """ 

219 return 0 

220 

221 def read_timed(buf, timer): 

222 """ 

223 Read analog values into ``buf`` at a rate set by the ``timer`` object. 

224 """ 

225 

226 

227 

228class CAN: 

229 NORMAL = "NORMAL" 

230 LOOPBACK = "LOOPBACK" 

231 SILENT = "SILENT" 

232 SILENT_LOOPBACK = "SILENT_LOOPBACK" 

233 

234 LIST16 = "LIST16" 

235 MASK16 = "MASK16" 

236 LIST32 = "LIST32" 

237 MASK32 = "MASK32" 

238 

239 def __init__(self, bus, mode=None, extframe=False, prescaler=100, sjw=1, bs1=6, bs2=8): 

240 """ 

241 Construct a CAN object on the given bus. 

242 """ 

243 

244 

245 @classmethod 

246 def initfilterbanks(cls, nr): 

247 """ 

248 Reset and disable all filter banks and assign how many banks should be available for CAN(1). 

249 """ 

250 

251 

252 def init(self, mode, extframe=False, prescaler=100, sjw=1, bs1=6, bs2=8): 

253 """ 

254 Initialise the CAN bus with the given parameters 

255 """ 

256 

257 

258 def deinit(self): 

259 """ 

260 Turn off the CAN bus. 

261 """ 

262 

263 

264 def setfilter(self, bank, mode, fifo, params, rtr): 

265 """ 

266 Configure a filter bank 

267 """ 

268 

269 

270 def clearfilter(self, bank): 

271 """ 

272 Clear and disables a filter bank. 

273 """ 

274 

275 

276 def any(self, fifo): 

277 """ 

278 Return True if any message waiting on the FIFO, else False. 

279 """ 

280 

281 

282 def recv(self, fifo, timeout=5000): 

283 """ 

284 Receive data on the bus. 

285 """ 

286 

287 

288 def send(self, data, id, timeout=0, rtr=False): 

289 """ 

290 Send a message on the bus. 

291 """ 

292 

293 

294 def rxcallback(self, fifo, fun): 

295 """ 

296 Register a function to be called when a message is accepted into a empty fifo: 

297 """ 

298 

299 

300 

301class DAC: 

302 NORMAL = "NORMAL" 

303 CIRCULAR = "CIRCULAR" 

304 

305 def __init__(self, port, bits=8): 

306 """ 

307 Construct a new DAC object. 

308 """ 

309 

310 

311 def init(self, bits=8): 

312 """ 

313 Reinitialise the DAC. ``bits`` can be 8 or 12. 

314 """ 

315 

316 

317 def deinit(self): 

318 """ 

319 De - initialise the DAC making its pin available for other uses. 

320 """ 

321 

322 def noise(self, freq): 

323 """ 

324 Generate a pseudo-random noise signal. 

325 """ 

326 

327 

328 def triangle(self, freq): 

329 """ 

330 Generate a triangle wave. 

331 """ 

332 

333 

334 def write(self, value): 

335 """ 

336 Direct access to the DAC output. 

337 """ 

338 

339 

340 def write_timed(self, data, freq, mode=NORMAL): 

341 """ 

342 Initiates a burst of RAM to DAC using a DMA transfer. 

343 """ 

344 

345 

346 

347class ExtInt: 

348 IRQ_FALLING = "IRQ_FALLING" 

349 IRQ_RISING = "IRQ_RISING" 

350 IRQ_RISING_FALLING = "IRQ_RISING_FALLING" 

351 

352 def __init__(self, pin, mode, pull, callback): 

353 """ 

354 Create an ExtInt object 

355 """ 

356 

357 

358 @classmethod 

359 def regs(cls): 

360 """ 

361 Dump the values of the EXTI registers. 

362 """ 

363 

364 def disable(self, ): 

365 """ 

366 Disable the interrupt associated with the ExtInt object. 

367 This could be useful for debouncing. 

368 """ 

369 

370 

371 def enable(self, ): 

372 """ 

373 Enable a disabled interrupt. 

374 """ 

375 

376 

377 def line(self, ): 

378 """ 

379 Return the line number that the pin is mapped to. 

380 """ 

381 

382 

383 def swint(self, ): 

384 """ 

385 Trigger the callback from software. 

386 """ 

387 

388 

389 

390class I2C: 

391 MASTER = "MASTER" 

392 SLAVE = "SLAVE" 

393 

394 def __init__(self, *args, **kwargs): 

395 """ 

396 Construct an I2C object on the given bus. 

397 """ 

398 

399 

400 def deinit(self): 

401 """ 

402 Turn off the I2C bus. 

403 """ 

404 

405 

406 def init(self, mode, addr=0x12, baudrate=400000, gencall=False): 

407 """ 

408 Initialise the I2C bus with the given parameters. 

409 """ 

410 

411 

412 def is_ready(self, addr): 

413 """ 

414 Check if an I2C device responds to the given address. Only valid when in master mode. 

415 """ 

416 

417 

418 def mem_read(self, data, addr, memaddr, timeout=5000, addr_size=8): 

419 """ 

420 Read from the memory of an I2C device. 

421 """ 

422 

423 

424 def mem_write(self, data, addr, memaddr, timeout=5000, addr_size=8): 

425 """ 

426 Write to the memory of an I2C device. 

427 """ 

428 

429 

430 def recv(self, recv, addr=0x00, timeout=5000): 

431 """ 

432 Receive data on the bus. 

433 """ 

434 

435 

436 def send(self, send, addr=0x00, timeout=5000): 

437 """ 

438 Send data on the bus. 

439 """ 

440 

441 

442 def scan(self): 

443 """ 

444 Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond. 

445 """ 

446 

447 

448 

449class LCD: 

450 

451 def __init__(self, skin_position): 

452 """ 

453 Construct an LCD object in the given skin position. ``skin_position`` can be 'X' or 'Y', and 

454 should match the position where the LCD pyskin is plugged in. 

455 """ 

456 

457 

458 def command(self, instr_data, buf): 

459 """ 

460 Send an arbitrary command to the LCD. 0 for ``instr_data`` to send an 

461 instruction, otherwise 1 to send data. ``buf`` is a buffer with the 

462 instructions/data to send. 

463 """ 

464 

465 def contrast(self, value): 

466 """ 

467 Set the contrast of the LCD. Valid values are between 0 and 47. 

468 """ 

469 

470 

471 def fill(self, colour): 

472 """ 

473 Fill the screen with the given colour (0 or 1 for white or black). 

474 """ 

475 

476 

477 def get(self, x, y): 

478 """ 

479 Get the pixel at the position ``(x, y)``. Returns 0 or 1. 

480 """ 

481 

482 

483 def light(self, value): 

484 """ 

485 Turn the backlight on/off. True or 1 turns it on, False or 0 turns it off. 

486 """ 

487 

488 

489 def pixel(self, x, y, colour): 

490 """ 

491 Set the pixel at ``(x, y)`` to the given colour (0 or 1). 

492 """ 

493 

494 

495 def show(self, ): 

496 """ 

497 Show the hidden buffer on the screen. 

498 """ 

499 

500 

501 def text(self, str, x, y, colour): 

502 """ 

503 Draw the given text to the position ``(x, y)`` using the given colour (0 or 1). 

504 """ 

505 

506 

507 def write(self, str): 

508 """ 

509 Write the string ``str`` to the screen. It will appear immediately. 

510 """ 

511 

512 

513 

514class LED: 

515 def __init__(self, id): 

516 """ 

517 Create an LED object associated with the given LED 

518 """ 

519 

520 

521 def intensity(self, value): 

522 """ 

523 Get or set the LED intensity. Intensity ranges between 0 (off) and 255 (full on). 

524 """ 

525 

526 def off(self, ): 

527 """ 

528 Turn the LED off. 

529 """ 

530 

531 

532 def on(self, ): 

533 """ 

534 Turn the LED on, to maximum intensity. 

535 """ 

536 

537 

538 def toggle(self, ): 

539 """ 

540 Toggle the LED between on (maximum intensity) and off. 

541 """ 

542 

543 

544 

545class _board(object): 

546 """object has any attribute, returns 1 for requested attribute's value.""" 

547 

548 def __getattr__(self, *args, **kwargs): 

549 return 1 

550 

551 

552class Pin: 

553 AF_OD = "AF_OD" 

554 AF_PP = "AF_PP" 

555 ANALOG = "ANALOG" 

556 IN = "IN" 

557 OUT = "OUT" 

558 OUT_OD = "OUT_OD" 

559 OUT_PP = "OUT_PP" 

560 PULL_DOWN = "PULL_DOWN" 

561 PULL_NONE = "PULL_NONE" 

562 PULL_UP = "PULL_UP" 

563 board = _board() 

564 cpu = _board() 

565 

566 def __init__(self, *args, **kwargs): 

567 """ 

568 Create a new Pin object associated with the id. 

569 """ 

570 

571 

572 @classmethod 

573 def debug(cls, state): 

574 """ 

575 Get or set the debugging state (``True`` or ``False`` for on or off). 

576 """ 

577 

578 

579 @classmethod 

580 def dict(cls, dict): 

581 """ 

582 Get or set the pin mapper dictionary. 

583 """ 

584 

585 

586 @classmethod 

587 def mapper(cls, fun): 

588 """ 

589 Get or set the pin mapper function. 

590 """ 

591 

592 

593 def init(self, mode, pull=PULL_NONE, af=-1): 

594 """ 

595 Initialise the pin: 

596 """ 

597 

598 

599 def value(self, value=None): 

600 """ 

601 Get or set the digital logic level of the pin. 

602 """ 

603 return 0 

604 

605 def __str__(self): 

606 """ 

607 Return a string describing the pin object. 

608 """ 

609 

610 

611 def af(self): 

612 """ 

613 Returns the currently configured alternate-function of the pin. 

614 """ 

615 

616 

617 def af_list(self, cls): 

618 """ 

619 Returns an array of alternate functions available for this pin. 

620 """ 

621 

622 

623 def gpio(self): 

624 """ 

625 Returns the base address of the GPIO block associated with this pin. 

626 """ 

627 

628 

629 def mode(self): 

630 """ 

631 Returns the currently configured mode of the pin. 

632 """ 

633 

634 

635 def name(self): 

636 """ 

637 Get the pin name. 

638 """ 

639 

640 

641 def names(self): 

642 """ 

643 Returns the cpu and board names for this pin. 

644 """ 

645 

646 

647 def pin(self): 

648 """ 

649 Get the pin number. 

650 """ 

651 

652 

653 def port(self): 

654 """ 

655 Get the pin port. 

656 """ 

657 

658 

659 def pull(self): 

660 """ 

661 Returns the currently configured pull of the pin. 

662 """ 

663 

664 

665 def on(self): 

666 """on.""" 

667 

668 def off(self): 

669 """off.""" 

670 

671 

672class PinAF: 

673 

674 def __str__(self): 

675 """ 

676 Return a string describing the alternate function. 

677 """ 

678 

679 

680 def index(self): 

681 """ 

682 Return the alternate function index. 

683 """ 

684 

685 

686 def name(self): 

687 """Return the name of the alternate function.""" 

688 

689 

690 def reg(self): 

691 """ 

692 Return the base register associated with the peripheral assigned to this 

693 alternate function. 

694 """ 

695 

696 

697 

698class RTC: 

699 

700 def __init__(self): 

701 """ 

702 Create an RTC object. 

703 """ 

704 

705 

706 def datetime(self, datetimetuple): 

707 """ 

708 Get or set the date and time of the RTC. 

709 """ 

710 

711 

712 def wakeup(self, timeout, callback=None): 

713 """ 

714 Set the RTC wakeup timer to trigger repeatedly at every ``timeout`` 

715 milliseconds. 

716 """ 

717 

718 

719 def info(self): 

720 """ 

721 Get information about the startup time and reset source. 

722 """ 

723 

724 

725 def calibration(self, cal): 

726 """ 

727 Get or set RTC calibration. 

728 """ 

729 

730 

731 

732class Servo: 

733 

734 def __init__(self, id): 

735 """ 

736 Create a servo object. ``id`` is 1-4, and corresponds to pins X1 through X4. 

737 """ 

738 

739 

740 def angle(self, angle, time=0): 

741 """ 

742 If no arguments are given, this function returns the current angle. 

743 """ 

744 

745 

746 def speed(self, speed, time=0): 

747 """ 

748 If no arguments are given, this function returns the current speed. 

749 """ 

750 

751 

752 def pulse_width(self, value): 

753 """ 

754 If no arguments are given, this function returns the current raw pulse-width 

755 value. 

756 """ 

757 

758 

759 def calibration(self, pulse_min, pulse_max, pulse_centre, pulse_angle_90, pulse_speed_100): 

760 """ 

761 If no arguments are given, this function returns the current calibration 

762 data, as a 5-tuple. 

763 """ 

764 

765 

766 

767class SPI: 

768 MASTER = "MASTER" 

769 SLAVE = "SLAVE" 

770 LSB = "LSB" 

771 MSB = "MSB" 

772 

773 def __init__(self, bus): 

774 """ 

775 Construct an SPI object on the given bus. 

776 """ 

777 

778 

779 def deinit(self): 

780 """ 

781 Turn off the SPI bus. 

782 """ 

783 

784 

785 def init(self, mode, prescaler, baudrate=328125, polarity=1, phase=0, bits=8, firstbit=MSB, ti=False, crc=None): 

786 """ 

787 Initialise the SPI bus with the given parameters: 

788 """ 

789 

790 

791 def recv(self, recv, timeout=5000): 

792 """ 

793 Receive data on the bus: 

794 """ 

795 

796 

797 def send(self, send, timeout=5000): 

798 """ 

799 Send data on the bus: 

800 """ 

801 

802 

803 def send_recv(self, send, recv=None, timeout=5000): 

804 """ 

805 Send and receive data on the bus at the same time: 

806 """ 

807 

808 

809 

810class Switch: 

811 

812 def __init__(self): 

813 """ 

814 Create and return a switch object. 

815 """ 

816 

817 

818 def __call__(self): 

819 """ 

820 Call switch object directly to get its state: True if pressed down, False otherwise. 

821 """ 

822 

823 

824 def callback(self, fun): 

825 """ 

826 Register the given function to be called when the switch is pressed down. 

827 """ 

828 

829 

830 

831class Timer: 

832 

833 def __init__(self, *args, **kwargs): 

834 """ 

835 Construct a new timer object of the given id. 

836 """ 

837 

838 

839 def init(self, freq, prescaler, period): 

840 """ 

841 Initialise the timer. 

842 """ 

843 

844 

845 def deinit(self): 

846 """ 

847 Deinitialises the timer. 

848 """ 

849 

850 

851 def callback(self, fun): 

852 """ 

853 Set the function to be called when the timer triggers. 

854 """ 

855 

856 

857 def channel(self, channel, mode): 

858 """ 

859 If only a channel number is ed, then a previously initialized channel 

860 object is returned (or ``None`` if there is no previous_sent_perc channel). 

861 """ 

862 

863 

864 def counter(self, value): 

865 """ 

866 Get or set the timer counter. 

867 """ 

868 

869 def freq(self, value): 

870 """ 

871 Get or set the frequency for the timer (changes prescaler and period if set). 

872 """ 

873 

874 

875 def period(self, value): 

876 """ 

877 Get or set the period of the timer. 

878 """ 

879 

880 

881 def prescaler(self, value): 

882 """ 

883 Get or set the prescaler for the timer. 

884 """ 

885 

886 

887 def source_freq(self): 

888 """ 

889 Get the frequency of the source of the timer. 

890 """ 

891 

892 

893 

894class TimerChannel: 

895 

896 def callback(self, fun): 

897 """ 

898 Set the function to be called when the timer channel triggers. 

899 """ 

900 

901 

902 def capture(self, value): 

903 """ 

904 Get or set the capture value associated with a channel. 

905 """ 

906 

907 

908 def compare(self, value): 

909 """ 

910 Get or set the compare value associated with a channel. 

911 """ 

912 

913 

914 def pulse_width(self, value): 

915 """ 

916 Get or set the pulse width value associated with a channel. 

917 """ 

918 

919 def pulse_width_percent(self, value): 

920 """ 

921 Get or set the pulse width percentage associated with a channel. 

922 """ 

923 

924 

925 

926class UART: 

927 RTS = "RTS" 

928 CTS = "CTS" 

929 

930 def __init__(self, bus): 

931 """ 

932 Construct a UART object on the given bus. 

933 """ 

934 

935 

936 def init(self, baudrate, bits=8, parity=None, stop=1, timeout=1000, flow=None, timeout_char=0, read_buf_len=64): 

937 """ 

938 Initialise the UART bus with the given parameters: 

939 """ 

940 

941 

942 def deinit(self): 

943 """ 

944 Turn off the UART bus. 

945 """ 

946 

947 

948 def any(self): 

949 """ 

950 Return ``True`` if any characters waiting, else ``False``. 

951 """ 

952 

953 

954 def writechar(self, char): 

955 """ 

956 Write a single character on the bus. 

957 """ 

958 

959 

960 def read(self, nbytes): 

961 """ 

962 Read characters. 

963 """ 

964 

965 

966 def readchar(self): 

967 """ 

968 Receive a single character on the bus. 

969 """ 

970 

971 

972 def readinto(self, buf, nbytes): 

973 """ 

974 Read bytes into the ``buf``. 

975 """ 

976 

977 

978 def readline(self): 

979 """ 

980 Read a line, ending in a newline character. 

981 """ 

982 

983 

984 def write(self, buf): 

985 """ 

986 Write the buffer of bytes to the bus. 

987 """ 

988 

989 

990 def sendbreak(self): 

991 """ 

992 Send a break condition on the bus. 

993 """ 

994 

995 

996 

997class USB_HID: 

998 """Create a new USB_HID object.""" 

999 

1000 def recv(self, data, timeout=5000): 

1001 """ 

1002 Receive data on the bus. 

1003 """ 

1004 

1005 

1006 def send(self, data): 

1007 """ 

1008 Send data over the USB HID interface: 

1009 """ 

1010 

1011 

1012 

1013class USB_VCP: 

1014 

1015 def __init__(self): 

1016 """ 

1017 Create a new USB_VCP object. 

1018 """ 

1019 

1020 

1021 def setinterrupt(self, chr): 

1022 """ 

1023 Set the character which interrupts running Python code. 

1024 """ 

1025 

1026 

1027 def isconnected(self): 

1028 """ 

1029 Return ``True`` if USB is connected as a serial device, else ``False``. 

1030 """ 

1031 

1032 

1033 def any(self): 

1034 """ 

1035 Return ``True`` if any characters waiting, else ``False``. 

1036 """ 

1037 

1038 

1039 def close(self): 

1040 """ 

1041 This method does nothing. It exists so the USB_VCP object can act as a file. 

1042 """ 

1043 

1044 

1045 def read(self, nbytes): 

1046 """ 

1047 Read at most ``nbytes`` from the serial device and return them as a bytes object. 

1048 """ 

1049 

1050 

1051 def readinto(self, buf, maxlen): 

1052 """ 

1053 Read bytes from the serial device and store them into ``buf``, which 

1054 should be a buffer-like object. 

1055 """ 

1056 

1057 

1058 def readline(self): 

1059 """ 

1060 Read a whole line from the serial device. 

1061 """ 

1062 

1063 

1064 def readlines(self): 

1065 """ 

1066 Read as much data as possible from the serial device, breaking it into lines. 

1067 """ 

1068 

1069 

1070 def write(self, buf): 

1071 """ 

1072 Write the bytes from ``buf`` to the serial device. 

1073 """ 

1074 

1075 

1076 def recv(self, data, timeout=5000): 

1077 """ 

1078 Receive data on the bus. 

1079 """ 

1080 

1081 

1082 def send(self, data, timeout=5000): 

1083 """ 

1084 Send data over the USB VCP. 

1085 """ 

1086 

1087 

1088 

1089class PWM: 

1090 def __init__(self, pin, freq): 

1091 """ 

1092 Create PWM object associated with the given pin and frequency. 

1093 """ 

1094 

1095 def duty(self, angle): 

1096 """move to angle."""