Coverage for src/main/python/machine.py : 99%
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 """
8def udelay(us):
9 """
10 Delay for the given number of microseconds.
11 """
13def millis():
14 """
15 Returns the number of milliseconds since the board was last reset.
16 """
20def micros():
21 """
22 Returns the number of microseconds since the board was last reset.
23 """
27def elapsed_millis(start):
28 """
29 Returns the number of milliseconds which have elapsed since ``start``.
30 """
34def elapsed_micros(start):
35 """
36 Returns the number of microseconds which have elapsed since ``start``.
37 """
41def hard_reset():
42 """
43 Resets the pyboard in a manner similar to pushing the external RESET
44 button.
45 """
48def bootloader():
49 """
50 Activate the bootloader without BOOT\* pins.
51 """
55def disable_irq():
56 """
57 Disable interrupt requests.
58 """
62def enable_irq(state=True):
63 """
64 Enable interrupt requests.
65 """
69def freq(self, sysclk=None, hclk=None, pclk1=None, pclk2=None):
70 """
71 If given no arguments, returns a tuple of clock frequencies.
72 """
76def wfi():
77 """
78 Wait for an internal or external interrupt.
79 """
83def stop():
84 """
85 Put the pyboard in a "sleeping" state.
86 """
89def standby():
90 """
91 Put the pyboard into a "deep sleep" state.
92 """
96def info(dump_alloc_table):
97 """
98 Print out lots of information about the board.
99 """
103def main(filename):
104 """
105 Set the filename of the main script to run after boot.py is finished.
106 """
110def mount(device, mountpoint, readonly=False, mkfs=False):
111 """
112 Mount a block device and make it available as part of the filesystem.
113 """
117def repl_uart(uart):
118 """
119 Get or set the UART object where the REPL is repeated on.
120 """
124def rng():
125 """
126 Return a 30-bit hardware generated random number.
127 """
131def sync():
132 """
133 Sync all file systems.
134 """
138def unique_id():
139 """
140 Returns a string of 12 bytes (96 bits), which is the unique ID of the MCU.
141 """
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 """
153class Accel:
155 def filtered_xyz(self):
156 """
157 Get a 3-tuple of filtered x, y and z values.
158 """
161 def tilt(self):
162 """
163 Get the tilt register.
164 """
167 def x(self):
168 """
169 Get the x-axis value.
170 """
173 def y(self):
174 """
175 Get the y-axis value.
176 """
179 def z(self):
180 """
181 Get the z-axis value.
182 """
185 def write(self, register, value):
186 """write."""
188 def read(self, register):
189 """read."""
192class ADC:
193 ATTN_0DB = 0
194 ATTN_2_5DB = 1
195 ATTN_6DB = 2
196 ATTN_11DB = 3
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 """
205 def read_timed(self):
206 """read timeout."""
208 def atten(self, num):
209 """attenutaiton."""
211 def read_timed_stop(self):
212 """read timed."""
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
221 def read_timed(buf, timer):
222 """
223 Read analog values into ``buf`` at a rate set by the ``timer`` object.
224 """
228class CAN:
229 NORMAL = "NORMAL"
230 LOOPBACK = "LOOPBACK"
231 SILENT = "SILENT"
232 SILENT_LOOPBACK = "SILENT_LOOPBACK"
234 LIST16 = "LIST16"
235 MASK16 = "MASK16"
236 LIST32 = "LIST32"
237 MASK32 = "MASK32"
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 """
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 """
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 """
258 def deinit(self):
259 """
260 Turn off the CAN bus.
261 """
264 def setfilter(self, bank, mode, fifo, params, rtr):
265 """
266 Configure a filter bank
267 """
270 def clearfilter(self, bank):
271 """
272 Clear and disables a filter bank.
273 """
276 def any(self, fifo):
277 """
278 Return True if any message waiting on the FIFO, else False.
279 """
282 def recv(self, fifo, timeout=5000):
283 """
284 Receive data on the bus.
285 """
288 def send(self, data, id, timeout=0, rtr=False):
289 """
290 Send a message on the bus.
291 """
294 def rxcallback(self, fifo, fun):
295 """
296 Register a function to be called when a message is accepted into a empty fifo:
297 """
301class DAC:
302 NORMAL = "NORMAL"
303 CIRCULAR = "CIRCULAR"
305 def __init__(self, port, bits=8):
306 """
307 Construct a new DAC object.
308 """
311 def init(self, bits=8):
312 """
313 Reinitialise the DAC. ``bits`` can be 8 or 12.
314 """
317 def deinit(self):
318 """
319 De - initialise the DAC making its pin available for other uses.
320 """
322 def noise(self, freq):
323 """
324 Generate a pseudo-random noise signal.
325 """
328 def triangle(self, freq):
329 """
330 Generate a triangle wave.
331 """
334 def write(self, value):
335 """
336 Direct access to the DAC output.
337 """
340 def write_timed(self, data, freq, mode=NORMAL):
341 """
342 Initiates a burst of RAM to DAC using a DMA transfer.
343 """
347class ExtInt:
348 IRQ_FALLING = "IRQ_FALLING"
349 IRQ_RISING = "IRQ_RISING"
350 IRQ_RISING_FALLING = "IRQ_RISING_FALLING"
352 def __init__(self, pin, mode, pull, callback):
353 """
354 Create an ExtInt object
355 """
358 @classmethod
359 def regs(cls):
360 """
361 Dump the values of the EXTI registers.
362 """
364 def disable(self, ):
365 """
366 Disable the interrupt associated with the ExtInt object.
367 This could be useful for debouncing.
368 """
371 def enable(self, ):
372 """
373 Enable a disabled interrupt.
374 """
377 def line(self, ):
378 """
379 Return the line number that the pin is mapped to.
380 """
383 def swint(self, ):
384 """
385 Trigger the callback from software.
386 """
390class I2C:
391 MASTER = "MASTER"
392 SLAVE = "SLAVE"
394 def __init__(self, *args, **kwargs):
395 """
396 Construct an I2C object on the given bus.
397 """
400 def deinit(self):
401 """
402 Turn off the I2C bus.
403 """
406 def init(self, mode, addr=0x12, baudrate=400000, gencall=False):
407 """
408 Initialise the I2C bus with the given parameters.
409 """
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 """
418 def mem_read(self, data, addr, memaddr, timeout=5000, addr_size=8):
419 """
420 Read from the memory of an I2C device.
421 """
424 def mem_write(self, data, addr, memaddr, timeout=5000, addr_size=8):
425 """
426 Write to the memory of an I2C device.
427 """
430 def recv(self, recv, addr=0x00, timeout=5000):
431 """
432 Receive data on the bus.
433 """
436 def send(self, send, addr=0x00, timeout=5000):
437 """
438 Send data on the bus.
439 """
442 def scan(self):
443 """
444 Scan all I2C addresses from 0x01 to 0x7f and return a list of those that respond.
445 """
449class LCD:
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 """
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 """
465 def contrast(self, value):
466 """
467 Set the contrast of the LCD. Valid values are between 0 and 47.
468 """
471 def fill(self, colour):
472 """
473 Fill the screen with the given colour (0 or 1 for white or black).
474 """
477 def get(self, x, y):
478 """
479 Get the pixel at the position ``(x, y)``. Returns 0 or 1.
480 """
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 """
489 def pixel(self, x, y, colour):
490 """
491 Set the pixel at ``(x, y)`` to the given colour (0 or 1).
492 """
495 def show(self, ):
496 """
497 Show the hidden buffer on the screen.
498 """
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 """
507 def write(self, str):
508 """
509 Write the string ``str`` to the screen. It will appear immediately.
510 """
514class LED:
515 def __init__(self, id):
516 """
517 Create an LED object associated with the given LED
518 """
521 def intensity(self, value):
522 """
523 Get or set the LED intensity. Intensity ranges between 0 (off) and 255 (full on).
524 """
526 def off(self, ):
527 """
528 Turn the LED off.
529 """
532 def on(self, ):
533 """
534 Turn the LED on, to maximum intensity.
535 """
538 def toggle(self, ):
539 """
540 Toggle the LED between on (maximum intensity) and off.
541 """
545class _board(object):
546 """object has any attribute, returns 1 for requested attribute's value."""
548 def __getattr__(self, *args, **kwargs):
549 return 1
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()
566 def __init__(self, *args, **kwargs):
567 """
568 Create a new Pin object associated with the id.
569 """
572 @classmethod
573 def debug(cls, state):
574 """
575 Get or set the debugging state (``True`` or ``False`` for on or off).
576 """
579 @classmethod
580 def dict(cls, dict):
581 """
582 Get or set the pin mapper dictionary.
583 """
586 @classmethod
587 def mapper(cls, fun):
588 """
589 Get or set the pin mapper function.
590 """
593 def init(self, mode, pull=PULL_NONE, af=-1):
594 """
595 Initialise the pin:
596 """
599 def value(self, value=None):
600 """
601 Get or set the digital logic level of the pin.
602 """
603 return 0
605 def __str__(self):
606 """
607 Return a string describing the pin object.
608 """
611 def af(self):
612 """
613 Returns the currently configured alternate-function of the pin.
614 """
617 def af_list(self, cls):
618 """
619 Returns an array of alternate functions available for this pin.
620 """
623 def gpio(self):
624 """
625 Returns the base address of the GPIO block associated with this pin.
626 """
629 def mode(self):
630 """
631 Returns the currently configured mode of the pin.
632 """
635 def name(self):
636 """
637 Get the pin name.
638 """
641 def names(self):
642 """
643 Returns the cpu and board names for this pin.
644 """
647 def pin(self):
648 """
649 Get the pin number.
650 """
653 def port(self):
654 """
655 Get the pin port.
656 """
659 def pull(self):
660 """
661 Returns the currently configured pull of the pin.
662 """
665 def on(self):
666 """on."""
668 def off(self):
669 """off."""
672class PinAF:
674 def __str__(self):
675 """
676 Return a string describing the alternate function.
677 """
680 def index(self):
681 """
682 Return the alternate function index.
683 """
686 def name(self):
687 """Return the name of the alternate function."""
690 def reg(self):
691 """
692 Return the base register associated with the peripheral assigned to this
693 alternate function.
694 """
698class RTC:
700 def __init__(self):
701 """
702 Create an RTC object.
703 """
706 def datetime(self, datetimetuple):
707 """
708 Get or set the date and time of the RTC.
709 """
712 def wakeup(self, timeout, callback=None):
713 """
714 Set the RTC wakeup timer to trigger repeatedly at every ``timeout``
715 milliseconds.
716 """
719 def info(self):
720 """
721 Get information about the startup time and reset source.
722 """
725 def calibration(self, cal):
726 """
727 Get or set RTC calibration.
728 """
732class Servo:
734 def __init__(self, id):
735 """
736 Create a servo object. ``id`` is 1-4, and corresponds to pins X1 through X4.
737 """
740 def angle(self, angle, time=0):
741 """
742 If no arguments are given, this function returns the current angle.
743 """
746 def speed(self, speed, time=0):
747 """
748 If no arguments are given, this function returns the current speed.
749 """
752 def pulse_width(self, value):
753 """
754 If no arguments are given, this function returns the current raw pulse-width
755 value.
756 """
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 """
767class SPI:
768 MASTER = "MASTER"
769 SLAVE = "SLAVE"
770 LSB = "LSB"
771 MSB = "MSB"
773 def __init__(self, bus):
774 """
775 Construct an SPI object on the given bus.
776 """
779 def deinit(self):
780 """
781 Turn off the SPI bus.
782 """
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 """
791 def recv(self, recv, timeout=5000):
792 """
793 Receive data on the bus:
794 """
797 def send(self, send, timeout=5000):
798 """
799 Send data on the bus:
800 """
803 def send_recv(self, send, recv=None, timeout=5000):
804 """
805 Send and receive data on the bus at the same time:
806 """
810class Switch:
812 def __init__(self):
813 """
814 Create and return a switch object.
815 """
818 def __call__(self):
819 """
820 Call switch object directly to get its state: True if pressed down, False otherwise.
821 """
824 def callback(self, fun):
825 """
826 Register the given function to be called when the switch is pressed down.
827 """
831class Timer:
833 def __init__(self, *args, **kwargs):
834 """
835 Construct a new timer object of the given id.
836 """
839 def init(self, freq, prescaler, period):
840 """
841 Initialise the timer.
842 """
845 def deinit(self):
846 """
847 Deinitialises the timer.
848 """
851 def callback(self, fun):
852 """
853 Set the function to be called when the timer triggers.
854 """
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 """
864 def counter(self, value):
865 """
866 Get or set the timer counter.
867 """
869 def freq(self, value):
870 """
871 Get or set the frequency for the timer (changes prescaler and period if set).
872 """
875 def period(self, value):
876 """
877 Get or set the period of the timer.
878 """
881 def prescaler(self, value):
882 """
883 Get or set the prescaler for the timer.
884 """
887 def source_freq(self):
888 """
889 Get the frequency of the source of the timer.
890 """
894class TimerChannel:
896 def callback(self, fun):
897 """
898 Set the function to be called when the timer channel triggers.
899 """
902 def capture(self, value):
903 """
904 Get or set the capture value associated with a channel.
905 """
908 def compare(self, value):
909 """
910 Get or set the compare value associated with a channel.
911 """
914 def pulse_width(self, value):
915 """
916 Get or set the pulse width value associated with a channel.
917 """
919 def pulse_width_percent(self, value):
920 """
921 Get or set the pulse width percentage associated with a channel.
922 """
926class UART:
927 RTS = "RTS"
928 CTS = "CTS"
930 def __init__(self, bus):
931 """
932 Construct a UART object on the given bus.
933 """
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 """
942 def deinit(self):
943 """
944 Turn off the UART bus.
945 """
948 def any(self):
949 """
950 Return ``True`` if any characters waiting, else ``False``.
951 """
954 def writechar(self, char):
955 """
956 Write a single character on the bus.
957 """
960 def read(self, nbytes):
961 """
962 Read characters.
963 """
966 def readchar(self):
967 """
968 Receive a single character on the bus.
969 """
972 def readinto(self, buf, nbytes):
973 """
974 Read bytes into the ``buf``.
975 """
978 def readline(self):
979 """
980 Read a line, ending in a newline character.
981 """
984 def write(self, buf):
985 """
986 Write the buffer of bytes to the bus.
987 """
990 def sendbreak(self):
991 """
992 Send a break condition on the bus.
993 """
997class USB_HID:
998 """Create a new USB_HID object."""
1000 def recv(self, data, timeout=5000):
1001 """
1002 Receive data on the bus.
1003 """
1006 def send(self, data):
1007 """
1008 Send data over the USB HID interface:
1009 """
1013class USB_VCP:
1015 def __init__(self):
1016 """
1017 Create a new USB_VCP object.
1018 """
1021 def setinterrupt(self, chr):
1022 """
1023 Set the character which interrupts running Python code.
1024 """
1027 def isconnected(self):
1028 """
1029 Return ``True`` if USB is connected as a serial device, else ``False``.
1030 """
1033 def any(self):
1034 """
1035 Return ``True`` if any characters waiting, else ``False``.
1036 """
1039 def close(self):
1040 """
1041 This method does nothing. It exists so the USB_VCP object can act as a file.
1042 """
1045 def read(self, nbytes):
1046 """
1047 Read at most ``nbytes`` from the serial device and return them as a bytes object.
1048 """
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 """
1058 def readline(self):
1059 """
1060 Read a whole line from the serial device.
1061 """
1064 def readlines(self):
1065 """
1066 Read as much data as possible from the serial device, breaking it into lines.
1067 """
1070 def write(self, buf):
1071 """
1072 Write the bytes from ``buf`` to the serial device.
1073 """
1076 def recv(self, data, timeout=5000):
1077 """
1078 Receive data on the bus.
1079 """
1082 def send(self, data, timeout=5000):
1083 """
1084 Send data over the USB VCP.
1085 """
1089class PWM:
1090 def __init__(self, pin, freq):
1091 """
1092 Create PWM object associated with the given pin and frequency.
1093 """
1095 def duty(self, angle):
1096 """move to angle."""