summaryrefslogtreecommitdiff
path: root/shared-bindings
diff options
context:
space:
mode:
Diffstat (limited to 'shared-bindings')
-rw-r--r--shared-bindings/_bleio/Address.c2
-rw-r--r--shared-bindings/_bleio/CharacteristicBuffer.c2
-rw-r--r--shared-bindings/_bleio/Connection.c2
-rw-r--r--shared-bindings/_bleio/PacketBuffer.c2
-rw-r--r--shared-bindings/_bleio/__init__.c15
-rw-r--r--shared-bindings/_bleio/__init__.h2
-rw-r--r--shared-bindings/board/__init__.c10
-rw-r--r--shared-bindings/busio/I2C.c4
-rw-r--r--shared-bindings/busio/SPI.c4
-rw-r--r--shared-bindings/camera/Camera.c132
-rw-r--r--shared-bindings/camera/Camera.h40
-rw-r--r--shared-bindings/camera/ImageFormat.c93
-rw-r--r--shared-bindings/camera/ImageFormat.h49
-rw-r--r--shared-bindings/camera/__init__.c50
-rw-r--r--shared-bindings/canio/CAN.c387
-rw-r--r--shared-bindings/canio/CAN.h52
-rw-r--r--shared-bindings/canio/Listener.c178
-rw-r--r--shared-bindings/canio/Listener.h43
-rw-r--r--shared-bindings/canio/Match.c139
-rw-r--r--shared-bindings/canio/Match.h37
-rw-r--r--shared-bindings/canio/Message.c159
-rw-r--r--shared-bindings/canio/Message.h42
-rw-r--r--shared-bindings/canio/RemoteTransmissionRequest.c156
-rw-r--r--shared-bindings/canio/RemoteTransmissionRequest.h42
-rw-r--r--shared-bindings/canio/__init__.c125
-rw-r--r--shared-bindings/canio/__init__.h33
-rw-r--r--shared-bindings/displayio/Bitmap.c4
-rw-r--r--shared-bindings/displayio/ColorConverter.c28
-rw-r--r--shared-bindings/displayio/ColorConverter.h3
-rw-r--r--shared-bindings/displayio/Display.c19
-rw-r--r--shared-bindings/displayio/Display.h3
-rw-r--r--shared-bindings/displayio/EPaperDisplay.c35
-rw-r--r--shared-bindings/displayio/EPaperDisplay.h2
-rw-r--r--shared-bindings/ipaddress/IPv4Address.c2
-rw-r--r--shared-bindings/microcontroller/Pin.c6
-rw-r--r--shared-bindings/rgbmatrix/RGBMatrix.c8
-rw-r--r--shared-bindings/rgbmatrix/RGBMatrix.h2
-rw-r--r--shared-bindings/socket/__init__.c15
-rw-r--r--shared-bindings/socketpool/Socket.c11
-rw-r--r--shared-bindings/socketpool/SocketPool.c17
-rw-r--r--shared-bindings/ssl/SSLContext.c2
-rwxr-xr-xshared-bindings/supervisor/Runtime.c8
-rw-r--r--shared-bindings/time/__init__.c2
-rw-r--r--shared-bindings/wifi/Network.c18
-rw-r--r--shared-bindings/wifi/Network.h1
-rw-r--r--shared-bindings/wifi/Radio.c138
-rw-r--r--shared-bindings/wifi/Radio.h10
-rw-r--r--shared-bindings/wifi/ScannedNetworks.c4
48 files changed, 2053 insertions, 85 deletions
diff --git a/shared-bindings/_bleio/Address.c b/shared-bindings/_bleio/Address.c
index 1f168d31a..04c667db4 100644
--- a/shared-bindings/_bleio/Address.c
+++ b/shared-bindings/_bleio/Address.c
@@ -86,7 +86,7 @@ STATIC mp_obj_t bleio_address_make_new(const mp_obj_type_t *type, size_t n_args,
//| or use `str()` on the :py:class:`~_bleio.Attribute` object itself, the address will be printed
//| in the expected order. For example:
//|
-//| .. code-block:: pycon
+//| .. code-block:: python
//|
//| >>> import _bleio
//| >>> _bleio.adapter.address
diff --git a/shared-bindings/_bleio/CharacteristicBuffer.c b/shared-bindings/_bleio/CharacteristicBuffer.c
index 7bcbc807e..4d6c836c1 100644
--- a/shared-bindings/_bleio/CharacteristicBuffer.c
+++ b/shared-bindings/_bleio/CharacteristicBuffer.c
@@ -37,7 +37,7 @@
STATIC void raise_error_if_not_connected(bleio_characteristic_buffer_obj_t *self) {
if (!common_hal_bleio_characteristic_buffer_connected(self)) {
- mp_raise_bleio_ConnectionError(translate("Not connected"));
+ mp_raise_ConnectionError(translate("Not connected"));
}
}
diff --git a/shared-bindings/_bleio/Connection.c b/shared-bindings/_bleio/Connection.c
index b5eeaa906..fe82af098 100644
--- a/shared-bindings/_bleio/Connection.c
+++ b/shared-bindings/_bleio/Connection.c
@@ -63,7 +63,7 @@
void bleio_connection_ensure_connected(bleio_connection_obj_t *self) {
if (!common_hal_bleio_connection_get_connected(self)) {
- mp_raise_bleio_ConnectionError(translate("Connection has been disconnected and can no longer be used. Create a new connection."));
+ mp_raise_ConnectionError(translate("Connection has been disconnected and can no longer be used. Create a new connection."));
}
}
diff --git a/shared-bindings/_bleio/PacketBuffer.c b/shared-bindings/_bleio/PacketBuffer.c
index 8c8970939..f2a3cbce0 100644
--- a/shared-bindings/_bleio/PacketBuffer.c
+++ b/shared-bindings/_bleio/PacketBuffer.c
@@ -161,7 +161,7 @@ STATIC mp_obj_t bleio_packet_buffer_write(mp_uint_t n_args, const mp_obj_t *pos_
// gatts write events, which may not have been sent yet.
//
// IDEAL:
- // mp_raise_bleio_ConnectionError(translate("Not connected"));
+ // mp_raise_ConnectionError(translate("Not connected"));
// TEMPORARY:
num_bytes_written = 0;
}
diff --git a/shared-bindings/_bleio/__init__.c b/shared-bindings/_bleio/__init__.c
index 3d9084dd5..2ef70a64f 100644
--- a/shared-bindings/_bleio/__init__.c
+++ b/shared-bindings/_bleio/__init__.c
@@ -63,7 +63,6 @@
//| """Catchall exception for Bluetooth related errors."""
//| ...
MP_DEFINE_BLEIO_EXCEPTION(BluetoothError, Exception)
-
NORETURN void mp_raise_bleio_BluetoothError(const compressed_string_t* fmt, ...) {
va_list argptr;
va_start(argptr,fmt);
@@ -71,18 +70,6 @@ NORETURN void mp_raise_bleio_BluetoothError(const compressed_string_t* fmt, ...)
va_end(argptr);
nlr_raise(exception);
}
-//| class ConnectionError(BluetoothError):
-//| """Raised when a connection is unavailable."""
-//| ...
-//|
-MP_DEFINE_BLEIO_EXCEPTION(ConnectionError, bleio_BluetoothError)
-NORETURN void mp_raise_bleio_ConnectionError(const compressed_string_t* fmt, ...) {
- va_list argptr;
- va_start(argptr,fmt);
- mp_obj_t exception = mp_obj_new_exception_msg_vlist(&mp_type_bleio_ConnectionError, fmt, argptr);
- va_end(argptr);
- nlr_raise(exception);
-}
//| class RoleError(BluetoothError):
//| """Raised when a resource is used as the mismatched role. For example, if a local CCCD is
@@ -93,6 +80,7 @@ MP_DEFINE_BLEIO_EXCEPTION(RoleError, bleio_BluetoothError)
NORETURN void mp_raise_bleio_RoleError(const compressed_string_t* msg) {
mp_raise_msg(&mp_type_bleio_RoleError, msg);
}
+
//| class SecurityError(BluetoothError):
//| """Raised when a security related error occurs."""
//| ...
@@ -183,7 +171,6 @@ STATIC const mp_rom_map_elem_t bleio_module_globals_table[] = {
// Errors
{ MP_ROM_QSTR(MP_QSTR_BluetoothError), OBJ_FROM_PTR(&mp_type_bleio_BluetoothError) },
- { MP_ROM_QSTR(MP_QSTR_ConnectionError), OBJ_FROM_PTR(&mp_type_bleio_ConnectionError) },
{ MP_ROM_QSTR(MP_QSTR_RoleError), OBJ_FROM_PTR(&mp_type_bleio_RoleError) },
{ MP_ROM_QSTR(MP_QSTR_SecurityError), OBJ_FROM_PTR(&mp_type_bleio_SecurityError) },
diff --git a/shared-bindings/_bleio/__init__.h b/shared-bindings/_bleio/__init__.h
index 588b1f197..65ba9b3a5 100644
--- a/shared-bindings/_bleio/__init__.h
+++ b/shared-bindings/_bleio/__init__.h
@@ -51,14 +51,12 @@ const mp_obj_type_t mp_type_bleio_ ## exc_name = { \
};
extern const mp_obj_type_t mp_type_bleio_BluetoothError;
-extern const mp_obj_type_t mp_type_bleio_ConnectionError;
extern const mp_obj_type_t mp_type_bleio_RoleError;
extern const mp_obj_type_t mp_type_bleio_SecurityError;
extern mp_obj_t bleio_set_adapter(mp_obj_t adapter_obj);
NORETURN void mp_raise_bleio_BluetoothError(const compressed_string_t* msg, ...);
-NORETURN void mp_raise_bleio_ConnectionError(const compressed_string_t* msg, ...);
NORETURN void mp_raise_bleio_RoleError(const compressed_string_t* msg);
NORETURN void mp_raise_bleio_SecurityError(const compressed_string_t* msg, ...);
diff --git a/shared-bindings/board/__init__.c b/shared-bindings/board/__init__.c
index 6837fc41c..717698408 100644
--- a/shared-bindings/board/__init__.c
+++ b/shared-bindings/board/__init__.c
@@ -28,6 +28,12 @@
#include "py/runtime.h"
#include "shared-bindings/board/__init__.h"
+#if BOARD_I2C
+#include "shared-bindings/busio/I2C.h"
+#endif
+#if BOARD_SPI
+#include "shared-bindings/busio/SPI.h"
+#endif
//| """Board specific pin names
//|
@@ -45,7 +51,7 @@
#if BOARD_I2C
mp_obj_t board_i2c(void) {
mp_obj_t singleton = common_hal_board_get_i2c();
- if (singleton != NULL) {
+ if (singleton != NULL && !common_hal_busio_i2c_deinited(singleton)) {
return singleton;
}
assert_pin_free(DEFAULT_I2C_BUS_SDA);
@@ -69,7 +75,7 @@ MP_DEFINE_CONST_FUN_OBJ_0(board_i2c_obj, board_i2c);
#if BOARD_SPI
mp_obj_t board_spi(void) {
mp_obj_t singleton = common_hal_board_get_spi();
- if (singleton != NULL) {
+ if (singleton != NULL && !common_hal_busio_spi_deinited(singleton)) {
return singleton;
}
assert_pin_free(DEFAULT_SPI_BUS_SCK);
diff --git a/shared-bindings/busio/I2C.c b/shared-bindings/busio/I2C.c
index 157e779fd..8b456dbd3 100644
--- a/shared-bindings/busio/I2C.c
+++ b/shared-bindings/busio/I2C.c
@@ -39,7 +39,7 @@
//| class I2C:
//| """Two wire serial protocol"""
//|
-//| def __init__(self, scl: microcontroller.Pin, sda: microcontroller.Pin, *, frequency: int = 400000, timeout: int = 255) -> None:
+//| def __init__(self, scl: microcontroller.Pin, sda: microcontroller.Pin, *, frequency: int = 100000, timeout: int = 255) -> None:
//|
//| """I2C is a two-wire protocol for communicating between devices. At the
//| physical level it consists of 2 wires: SCL and SDA, the clock and data
@@ -70,7 +70,7 @@ STATIC mp_obj_t busio_i2c_make_new(const mp_obj_type_t *type, size_t n_args, con
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_scl, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_sda, MP_ARG_REQUIRED | MP_ARG_OBJ },
- { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 400000} },
+ { MP_QSTR_frequency, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 100000} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 255} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
diff --git a/shared-bindings/busio/SPI.c b/shared-bindings/busio/SPI.c
index aefe4f5a7..e47564c8c 100644
--- a/shared-bindings/busio/SPI.c
+++ b/shared-bindings/busio/SPI.c
@@ -96,6 +96,10 @@ STATIC mp_obj_t busio_spi_make_new(const mp_obj_type_t *type, size_t n_args, con
const mcu_pin_obj_t* mosi = validate_obj_is_free_pin_or_none(args[ARG_MOSI].u_obj);
const mcu_pin_obj_t* miso = validate_obj_is_free_pin_or_none(args[ARG_MISO].u_obj);
+ if (!miso && !mosi) {
+ mp_raise_ValueError(translate("Must provide MISO or MOSI pin"));
+ }
+
common_hal_busio_spi_construct(self, clock, mosi, miso);
return MP_OBJ_FROM_PTR(self);
}
diff --git a/shared-bindings/camera/Camera.c b/shared-bindings/camera/Camera.c
new file mode 100644
index 000000000..fd88ccab0
--- /dev/null
+++ b/shared-bindings/camera/Camera.c
@@ -0,0 +1,132 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+#include "shared-bindings/camera/Camera.h"
+#include "shared-bindings/util.h"
+
+//| class Camera:
+//| """The class to control camera.
+//|
+//| Usage::
+//|
+//| import board
+//| import sdioio
+//| import storage
+//| import camera
+//|
+//| sd = sdioio.SDCard(
+//| clock=board.SDIO_CLOCK,
+//| command=board.SDIO_COMMAND,
+//| data=board.SDIO_DATA,
+//| frequency=25000000)
+//| vfs = storage.VfsFat(sd)
+//| storage.mount(vfs, '/sd')
+//|
+//| cam = camera.Camera()
+//|
+//| buffer = bytearray(512 * 1024)
+//| file = open("/sd/image.jpg","wb")
+//| size = cam.take_picture(buffer, width=1920, height=1080, format=camera.ImageFormat.JPG)
+//| file.write(buffer, size)
+//| file.close()"""
+//|
+
+//| def __init__(self) -> None:
+//| """Initialize camera."""
+//| ...
+//|
+STATIC mp_obj_t camera_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ camera_obj_t *self = m_new_obj(camera_obj_t);
+ self->base.type = &camera_type;
+ // No arguments
+ mp_arg_check_num(n_args, kw_args, 0, 0, false);
+
+ common_hal_camera_construct(self);
+ return MP_OBJ_FROM_PTR(self);
+}
+
+//| def deinit(self) -> None:
+//| """De-initialize camera."""
+//| ...
+//|
+STATIC mp_obj_t camera_obj_deinit(mp_obj_t self_in) {
+ camera_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_camera_deinit(self);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_1(camera_deinit_obj, camera_obj_deinit);
+
+STATIC void check_for_deinit(camera_obj_t *self) {
+ if (common_hal_camera_deinited(self)) {
+ raise_deinited_error();
+ }
+}
+
+//| def take_picture(self, buf: WriteableBuffer, format: ImageFormat) -> int:
+//| """Take picture and save to ``buf`` in the given ``format``. The size of the picture
+//| taken is ``width`` by ``height`` in pixels.
+//|
+//| :return: the number of bytes written into buf
+//| :rtype: int"""
+//| ...
+//|
+STATIC mp_obj_t camera_obj_take_picture(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_buffer, ARG_width, ARG_height, ARG_format };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_buffer, MP_ARG_REQUIRED | MP_ARG_OBJ },
+ { MP_QSTR_width, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
+ { MP_QSTR_height, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_INT },
+ { MP_QSTR_format, MP_ARG_KW_ONLY | MP_ARG_REQUIRED | MP_ARG_OBJ },
+ };
+ camera_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
+ check_for_deinit(self);
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(args[ARG_buffer].u_obj, &bufinfo, MP_BUFFER_WRITE);
+
+ camera_imageformat_t format = camera_imageformat_obj_to_type(args[ARG_format].u_obj);
+
+ return MP_OBJ_NEW_SMALL_INT(common_hal_camera_take_picture(self, (uint8_t *)bufinfo.buf, bufinfo.len, args[ARG_width].u_int, args[ARG_height].u_int, format));
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(camera_take_picture_obj, 2, camera_obj_take_picture);
+
+STATIC const mp_rom_map_elem_t camera_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&camera_deinit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_take_picture), MP_ROM_PTR(&camera_take_picture_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(camera_locals_dict, camera_locals_dict_table);
+
+const mp_obj_type_t camera_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Camera,
+ .make_new = camera_make_new,
+ .locals_dict = (mp_obj_dict_t*)&camera_locals_dict,
+};
diff --git a/shared-bindings/camera/Camera.h b/shared-bindings/camera/Camera.h
new file mode 100644
index 000000000..8102672e4
--- /dev/null
+++ b/shared-bindings/camera/Camera.h
@@ -0,0 +1,40 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
+
+#include "common-hal/camera/Camera.h"
+#include "shared-bindings/camera/ImageFormat.h"
+
+extern const mp_obj_type_t camera_type;
+
+void common_hal_camera_construct(camera_obj_t *self);
+void common_hal_camera_deinit(camera_obj_t *self);
+bool common_hal_camera_deinited(camera_obj_t *self);
+size_t common_hal_camera_take_picture(camera_obj_t *self, uint8_t *buffer, size_t len, uint16_t width, uint16_t height, camera_imageformat_t format);
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_CAMERA_H
diff --git a/shared-bindings/camera/ImageFormat.c b/shared-bindings/camera/ImageFormat.c
new file mode 100644
index 000000000..d4bdddc56
--- /dev/null
+++ b/shared-bindings/camera/ImageFormat.c
@@ -0,0 +1,93 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/camera/ImageFormat.h"
+
+//| class ImageFormat:
+//| """Image format"""
+//|
+//| def __init__(self) -> None:
+//| """Enum-like class to define the image format."""
+//|
+//| JPG: ImageFormat
+//| """JPG format."""
+//|
+//| RGB565: ImageFormat
+//| """RGB565 format."""
+//|
+const mp_obj_type_t camera_imageformat_type;
+
+const camera_imageformat_obj_t camera_imageformat_jpg_obj = {
+ { &camera_imageformat_type },
+};
+
+const camera_imageformat_obj_t camera_imageformat_rgb565_obj = {
+ { &camera_imageformat_type },
+};
+
+camera_imageformat_t camera_imageformat_obj_to_type(mp_obj_t obj) {
+ if (obj == MP_ROM_PTR(&camera_imageformat_jpg_obj)) {
+ return IMAGEFORMAT_JPG;
+ } else if (obj == MP_ROM_PTR(&camera_imageformat_rgb565_obj)) {
+ return IMAGEFORMAT_RGB565;
+ }
+ return IMAGEFORMAT_NONE;
+}
+
+mp_obj_t camera_imageformat_type_to_obj(camera_imageformat_t format) {
+ switch (format) {
+ case IMAGEFORMAT_JPG:
+ return (mp_obj_t)MP_ROM_PTR(&camera_imageformat_jpg_obj);
+ case IMAGEFORMAT_RGB565:
+ return (mp_obj_t)MP_ROM_PTR(&camera_imageformat_rgb565_obj);
+ case IMAGEFORMAT_NONE:
+ default:
+ return (mp_obj_t)MP_ROM_PTR(&mp_const_none_obj);
+ }
+}
+
+STATIC const mp_rom_map_elem_t camera_imageformat_locals_dict_table[] = {
+ {MP_ROM_QSTR(MP_QSTR_JPG), MP_ROM_PTR(&camera_imageformat_jpg_obj)},
+ {MP_ROM_QSTR(MP_QSTR_RGB565), MP_ROM_PTR(&camera_imageformat_rgb565_obj)},
+};
+STATIC MP_DEFINE_CONST_DICT(camera_imageformat_locals_dict, camera_imageformat_locals_dict_table);
+
+STATIC void camera_imageformat_print(const mp_print_t *print, mp_obj_t self_in, mp_print_kind_t kind) {
+ qstr format = MP_QSTR_None;
+ if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imageformat_jpg_obj)) {
+ format = MP_QSTR_JPG;
+ } else if (MP_OBJ_TO_PTR(self_in) == MP_ROM_PTR(&camera_imageformat_rgb565_obj)) {
+ format = MP_QSTR_RGB565;
+ }
+ mp_printf(print, "%q.%q.%q", MP_QSTR_camera, MP_QSTR_ImageSize, format);
+}
+
+const mp_obj_type_t camera_imageformat_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_ImageFormat,
+ .print = camera_imageformat_print,
+ .locals_dict = (mp_obj_t)&camera_imageformat_locals_dict,
+};
diff --git a/shared-bindings/camera/ImageFormat.h b/shared-bindings/camera/ImageFormat.h
new file mode 100644
index 000000000..8abc88438
--- /dev/null
+++ b/shared-bindings/camera/ImageFormat.h
@@ -0,0 +1,49 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
+#define MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
+
+#include "py/obj.h"
+
+typedef enum {
+ IMAGEFORMAT_NONE,
+ IMAGEFORMAT_JPG,
+ IMAGEFORMAT_RGB565,
+} camera_imageformat_t;
+
+const mp_obj_type_t camera_imageformat_type;
+
+camera_imageformat_t camera_imageformat_obj_to_type(mp_obj_t obj);
+mp_obj_t camera_imageformat_type_to_obj(camera_imageformat_t mode);
+
+typedef struct {
+ mp_obj_base_t base;
+} camera_imageformat_obj_t;
+extern const camera_imageformat_obj_t camera_imageformat_jpg_obj;
+extern const camera_imageformat_obj_t camera_imageformat_rgb565_obj;
+
+#endif // MICROPY_INCLUDED_SHARED_BINDINGS_CAMERA_IMAGEFORMAT_H
diff --git a/shared-bindings/camera/__init__.c b/shared-bindings/camera/__init__.c
new file mode 100644
index 000000000..080516d51
--- /dev/null
+++ b/shared-bindings/camera/__init__.c
@@ -0,0 +1,50 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright 2020 Sony Semiconductor Solutions Corporation
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/obj.h"
+#include "py/runtime.h"
+#include "py/mphal.h"
+#include "shared-bindings/camera/Camera.h"
+#include "shared-bindings/util.h"
+
+//| """Support for camera input
+//|
+//| The `camera` module contains classes to control the camera and take pictures."""
+//|
+STATIC const mp_rom_map_elem_t camera_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR_camera) },
+ { MP_ROM_QSTR(MP_QSTR_Camera), MP_ROM_PTR(&camera_type) },
+
+ // Enum-like Classes.
+ { MP_ROM_QSTR(MP_QSTR_ImageFormat), MP_ROM_PTR(&camera_imageformat_type) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(camera_module_globals, camera_module_globals_table);
+
+const mp_obj_module_t camera_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&camera_module_globals,
+};
diff --git a/shared-bindings/canio/CAN.c b/shared-bindings/canio/CAN.c
new file mode 100644
index 000000000..13066764e
--- /dev/null
+++ b/shared-bindings/canio/CAN.c
@@ -0,0 +1,387 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "py/enum.h"
+#include "common-hal/canio/CAN.h"
+#include "common-hal/canio/Listener.h"
+#include "shared-bindings/canio/__init__.h"
+#include "shared-bindings/canio/CAN.h"
+#include "shared-bindings/canio/Listener.h"
+#include "shared-bindings/canio/Match.h"
+#include "shared-bindings/canio/Message.h"
+#include "shared-bindings/microcontroller/Pin.h"
+
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+//|
+//| class CAN:
+//| """CAN bus protocol"""
+//|
+//| def __init__(self,
+//| tx: microcontroller.Pin,
+//| rx: microcontroller.Pin,
+//| *,
+//| baudrate: int = 250000,
+//| loopback: bool = False,
+//| silent: bool = False,
+//| auto_restart: bool = False,
+//| ) -> None:
+//| """A common shared-bus protocol. The rx and tx pins are generally
+//| connected to a transceiver which controls the H and L pins on a
+//| shared bus.
+//|
+//| :param ~microcontroller.Pin rx: the pin to receive with
+//| :param ~microcontroller.Pin tx: the pin to transmit with
+//| :param int baudrate: The bit rate of the bus in Hz. All devices on the bus must agree on this value.
+//| :param bool loopback: When True the ``rx`` pin's value is ignored, and the device receives the packets it sends.
+//| :param bool silent: When True the ``tx`` pin is always driven to the high logic level. This mode can be used to "sniff" a CAN bus without interfering.
+//| :param bool auto_restart: If True, will restart communications after entering bus-off state
+//| """
+//| ...
+//|
+STATIC mp_obj_t canio_can_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_tx, ARG_rx, ARG_baudrate, ARG_loopback, ARG_silent, ARG_auto_restart, NUM_ARGS };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_tx, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ { MP_QSTR_rx, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ { MP_QSTR_baudrate, MP_ARG_INT, {.u_int = 250000} },
+ { MP_QSTR_loopback, MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_silent, MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_auto_restart, MP_ARG_BOOL, {.u_bool = false} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
+
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ mcu_pin_obj_t *rx_pin = validate_obj_is_free_pin_or_none(args[ARG_rx].u_obj);
+ mcu_pin_obj_t *tx_pin = validate_obj_is_free_pin_or_none(args[ARG_tx].u_obj);
+ if (!rx_pin && !tx_pin) {
+ mp_raise_ValueError(translate("tx and rx cannot both be None"));
+ }
+
+ canio_can_obj_t *self = m_new_obj(canio_can_obj_t);
+ self->base.type = &canio_can_type;
+ common_hal_canio_can_construct(self, tx_pin, rx_pin, args[ARG_baudrate].u_int, args[ARG_loopback].u_bool, args[ARG_silent].u_bool);
+
+ common_hal_canio_can_auto_restart_set(self, args[ARG_auto_restart].u_bool);
+
+ return MP_OBJ_FROM_PTR(self);
+}
+
+
+//| auto_restart: bool
+//| """If True, will restart communications after entering bus-off state"""
+//|
+STATIC mp_obj_t canio_can_auto_restart_get(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return mp_obj_new_bool(common_hal_canio_can_auto_restart_get(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_can_auto_restart_get_obj, canio_can_auto_restart_get);
+
+STATIC mp_obj_t canio_can_auto_restart_set(mp_obj_t self_in, mp_obj_t flag_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ common_hal_canio_can_auto_restart_set(self, mp_obj_is_true(flag_in));
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_can_auto_restart_set_obj, canio_can_auto_restart_set);
+
+STATIC const mp_obj_property_t canio_can_auto_restart_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_can_auto_restart_get_obj,
+ (mp_obj_t)&canio_can_auto_restart_set_obj,
+ (mp_obj_t)mp_const_none},
+};
+
+
+//| baudrate: int
+//| """The baud rate (read-only)"""
+//|
+STATIC mp_obj_t canio_can_baudrate_get(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_can_baudrate_get(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_can_baudrate_get_obj, canio_can_baudrate_get);
+
+STATIC const mp_obj_property_t canio_can_baudrate_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_can_baudrate_get_obj,
+ (mp_obj_t)mp_const_none,
+ (mp_obj_t)mp_const_none},
+};
+
+//| transmit_error_count: int
+//| """The number of transmit errors (read-only). Increased for a detected transmission error, decreased for successful transmission. Limited to the range from 0 to 255 inclusive. Also called TEC."""
+//|
+STATIC mp_obj_t canio_can_transmit_error_count_get(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_can_transmit_error_count_get(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_can_transmit_error_count_get_obj, canio_can_transmit_error_count_get);
+
+STATIC const mp_obj_property_t canio_can_transmit_error_count_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_can_transmit_error_count_get_obj,
+ (mp_obj_t)mp_const_none,
+ (mp_obj_t)mp_const_none},
+};
+
+//| receive_error_count: int
+//| """The number of receive errors (read-only). Increased for a detected reception error, decreased for successful reception. Limited to the range from 0 to 255 inclusive. Also called REC."""
+//|
+STATIC mp_obj_t canio_can_receive_error_count_get(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_can_receive_error_count_get(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_can_receive_error_count_get_obj, canio_can_receive_error_count_get);
+
+STATIC const mp_obj_property_t canio_can_receive_error_count_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_can_receive_error_count_get_obj,
+ (mp_obj_t)mp_const_none,
+ (mp_obj_t)mp_const_none},
+};
+
+//| state: BusState
+//| """The current state of the bus. (read-only)"""
+STATIC mp_obj_t canio_can_state_get(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return cp_enum_find(&canio_bus_state_type, common_hal_canio_can_state_get(self));
+}
+
+MP_DEFINE_CONST_FUN_OBJ_1(canio_can_state_get_obj, canio_can_state_get);
+
+STATIC const mp_obj_property_t canio_can_state_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_can_state_get_obj,
+ (mp_obj_t)mp_const_none,
+ (mp_obj_t)mp_const_none},
+};
+
+
+//| def restart(self) -> None:
+//| """If the device is in the bus off state, restart it."""
+//| ...
+//|
+STATIC mp_obj_t canio_can_restart(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ common_hal_canio_can_restart(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_can_restart_obj, canio_can_restart);
+
+//| def listen(self, matches: Optional[Sequence[Match]]=None, *, timeout: float=10) -> Listener:
+//| """Start receiving messages that match any one of the filters.
+//|
+//| Creating a listener is an expensive operation and can interfere with reception of messages by other listeners.
+//|
+//| There is an implementation-defined maximum number of listeners and limit to the complexity of the filters.
+//|
+//| If the hardware cannot support all the requested matches, a ValueError is raised. Note that generally there are some number of hardware filters shared among all fifos.
+//|
+//| A message can be received by at most one Listener. If more than one listener matches a message, it is undefined which one actually receives it.
+//|
+//| An empty filter list causes all messages to be accepted.
+//|
+//| Timeout dictates how long receive() and next() will block.
+//|
+//| Platform specific notes:
+//|
+//| SAM E5x supports two Listeners. Filter blocks are shared between the two
+//| listeners. There are 4 standard filter blocks and 4 extended filter blocks.
+//| Each block can either match 2 single addresses or a mask of addresses.
+//| The number of filter blocks can be increased, up to a hardware maximum, by
+//| rebuilding CircuitPython, but this decreases the CircuitPython free
+//| memory even if canio is not used.
+//|
+//| STM32F405 supports two Listeners. Filter blocks are shared between the two listeners.
+//| There are 14 filter blocks. Each block can match 2 standard addresses with
+//| mask or 1 extended address with mask.
+//|
+//| ESP32S2 supports one Listener. There is a single filter block, which can either match a
+//| standard address with mask or an extended address with mask.
+//| """
+//| ...
+//|
+STATIC mp_obj_t canio_can_listen(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(pos_args[0]);
+ common_hal_canio_can_check_for_deinit(self);
+
+ enum { ARG_matches, ARG_timeout, NUM_ARGS };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_matches, MP_ARG_OBJ, {.u_obj = 0} },
+ { MP_QSTR_timeout, MP_ARG_OBJ, {.u_obj = 0} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
+
+ mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ size_t nmatch = 0;
+ mp_obj_t *match_objects = NULL;
+
+ if (args[ARG_matches].u_obj) {
+ mp_obj_get_array(args[ARG_matches].u_obj, &nmatch, &match_objects);
+ }
+
+ canio_match_obj_t *matches[nmatch];
+ for (size_t i=0; i<nmatch; i++) {
+ mp_obj_type_t *type = mp_obj_get_type(match_objects[i]);
+ if (type != &canio_match_type) {
+ mp_raise_TypeError_varg(translate("expected '%q' but got '%q'"), MP_QSTR_Match, type->name);
+ }
+ matches[i] = MP_OBJ_TO_PTR(match_objects[i]);
+ }
+
+ float timeout = args[ARG_timeout].u_obj ? mp_obj_get_float(args[ARG_timeout].u_obj) : 10.0f;
+ canio_listener_obj_t *listener = m_new_obj(canio_listener_obj_t);
+ listener->base.type = &canio_listener_type;
+ common_hal_canio_listener_construct(listener, self, nmatch, matches, timeout);
+ return listener;
+}
+MP_DEFINE_CONST_FUN_OBJ_KW(canio_can_listen_obj, 1, canio_can_listen);
+
+//| loopback: bool
+//| """True if the device was created in loopback mode, False
+//| otherwise (read-only)"""
+//|
+STATIC mp_obj_t canio_can_loopback_get(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return mp_obj_new_bool(common_hal_canio_can_loopback_get(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_can_loopback_get_obj, canio_can_loopback_get);
+
+STATIC const mp_obj_property_t canio_can_loopback_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_can_loopback_get_obj,
+ (mp_obj_t)mp_const_none,
+ (mp_obj_t)mp_const_none},
+};
+
+
+//| def send(self, message: Union[RemoteTransmissionRequest, Message]) -> None:
+//| """Send a message on the bus with the given data and id.
+//| If the message could not be sent due to a full fifo or a bus error condition, RuntimeError is raised.
+//| """
+//| ...
+//|
+STATIC mp_obj_t canio_can_send(mp_obj_t self_in, mp_obj_t message_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ mp_obj_type_t *message_type = mp_obj_get_type(message_in);
+ if (message_type != &canio_message_type && message_type != &canio_remote_transmission_request_type) {
+ mp_raise_TypeError_varg(translate("expected '%q' or '%q' but got '%q'"), MP_QSTR_Message, MP_QSTR_RemoteTransmissionRequest, message_type->name);
+ }
+
+ canio_message_obj_t *message = message_in;
+ common_hal_canio_can_send(self, message);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_can_send_obj, canio_can_send);
+
+//| silent: bool
+//| """True if the device was created in silent mode, False
+//| otherwise (read-only)"""
+//|
+STATIC mp_obj_t canio_can_silent_get(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return mp_obj_new_bool(common_hal_canio_can_silent_get(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_can_silent_get_obj, canio_can_silent_get);
+
+STATIC const mp_obj_property_t canio_can_silent_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_can_silent_get_obj,
+ (mp_obj_t)mp_const_none,
+ (mp_obj_t)mp_const_none},
+};
+
+
+//| def deinit(self) -> None:
+//| """Deinitialize this object, freeing its hardware resources"""
+//| ...
+//|
+STATIC mp_obj_t canio_can_deinit(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_deinit(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_can_deinit_obj, canio_can_deinit);
+
+//| def __enter__(self) -> CAN:
+//| """Returns self, to allow the object to be used in a `with` statement for resource control"""
+//| ...
+//|
+STATIC mp_obj_t canio_can_enter(mp_obj_t self_in) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_can_check_for_deinit(self);
+ return self_in;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_can_enter_obj, canio_can_enter);
+
+//| def __exit__(self, unused1: Optional[Type[BaseException]], unused2: Optional[BaseException], unused3: Optional[TracebackType]) -> None:
+//| """Calls deinit()"""
+//| ...
+STATIC mp_obj_t canio_can_exit(size_t num_args, const mp_obj_t args[]) {
+ canio_can_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+ common_hal_canio_can_deinit(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(canio_can_exit_obj, 4, 4, canio_can_exit);
+
+STATIC const mp_rom_map_elem_t canio_can_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&canio_can_enter_obj) },
+ { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&canio_can_exit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_auto_restart), MP_ROM_PTR(&canio_can_auto_restart_obj) },
+ { MP_ROM_QSTR(MP_QSTR_baudrate), MP_ROM_PTR(&canio_can_baudrate_obj) },
+ { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&canio_can_deinit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_listen), MP_ROM_PTR(&canio_can_listen_obj) },
+ { MP_ROM_QSTR(MP_QSTR_loopback), MP_ROM_PTR(&canio_can_loopback_obj) },
+ { MP_ROM_QSTR(MP_QSTR_receive_error_count), MP_ROM_PTR(&canio_can_receive_error_count_obj) },
+ { MP_ROM_QSTR(MP_QSTR_restart), MP_ROM_PTR(&canio_can_restart_obj) },
+ { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&canio_can_send_obj) },
+ { MP_ROM_QSTR(MP_QSTR_silent), MP_ROM_PTR(&canio_can_silent_obj) },
+ { MP_ROM_QSTR(MP_QSTR_state), MP_ROM_PTR(&canio_can_state_obj) },
+ { MP_ROM_QSTR(MP_QSTR_transmit_error_count), MP_ROM_PTR(&canio_can_transmit_error_count_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(canio_can_locals_dict, canio_can_locals_dict_table);
+
+const mp_obj_type_t canio_can_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_CAN,
+ .make_new = canio_can_make_new,
+ .locals_dict = (mp_obj_t)&canio_can_locals_dict,
+};
diff --git a/shared-bindings/canio/CAN.h b/shared-bindings/canio/CAN.h
new file mode 100644
index 000000000..968b71f14
--- /dev/null
+++ b/shared-bindings/canio/CAN.h
@@ -0,0 +1,52 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "py/obj.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "shared-bindings/canio/__init__.h"
+#include "shared-bindings/canio/Message.h"
+
+extern const mp_obj_type_t canio_can_type;
+
+typedef struct canio_can_obj canio_can_obj_t;
+
+void common_hal_canio_can_construct(canio_can_obj_t *self, mcu_pin_obj_t *tx, mcu_pin_obj_t *rx, int baudrate, bool loopback, bool silent);
+bool common_hal_canio_can_auto_restart_get(canio_can_obj_t *self);
+bool common_hal_canio_can_deinited(canio_can_obj_t *self);
+int common_hal_canio_can_baudrate_get(canio_can_obj_t *self);
+bool common_hal_canio_can_loopback_get(canio_can_obj_t *self);
+int common_hal_canio_can_receive_error_count_get(canio_can_obj_t *self);
+canio_bus_state_t common_hal_canio_can_state_get(canio_can_obj_t *self);
+bool common_hal_canio_can_silent_get(canio_can_obj_t *self);
+int common_hal_canio_can_transmit_error_count_get(canio_can_obj_t *self);
+void common_hal_canio_can_auto_restart_set(canio_can_obj_t *self, bool auto_restart);
+void common_hal_canio_can_check_for_deinit(canio_can_obj_t *self);
+void common_hal_canio_can_deinit(canio_can_obj_t *self);
+void common_hal_canio_can_restart(canio_can_obj_t *self);
+void common_hal_canio_can_send(canio_can_obj_t *self, mp_obj_t message);
+void common_hal_canio_reset(void);
diff --git a/shared-bindings/canio/Listener.c b/shared-bindings/canio/Listener.c
new file mode 100644
index 000000000..8a39f0f2a
--- /dev/null
+++ b/shared-bindings/canio/Listener.c
@@ -0,0 +1,178 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/canio/Listener.h"
+#include "shared-bindings/canio/Message.h"
+#include "common-hal/canio/Listener.h"
+
+#include "py/runtime.h"
+#include "py/objproperty.h"
+
+//| class Listener:
+//| """Listens for CAN message
+//|
+//| `canio.Listener` is not constructed directly, but instead by calling
+//| `canio.CAN.listen`.
+//|
+//| In addition to using the `receive` method to retrieve a message or
+//| the `in_waiting` method to check for an available message, a
+//| listener can be used as an iterable, yielding messages until no
+//| message arrives within ``self.timeout`` seconds."""
+//|
+
+//| def receive(self) -> Optional[Union[RemoteTransmissionRequest,Message]]:
+//| """Reads a message, after waiting up to ``self.timeout`` seconds
+//|
+//| If no message is received in time, `None` is returned. Otherwise,
+//| a `Message` or `RemoteTransmissionRequest` is returned."""
+//| ...
+//|
+STATIC mp_obj_t canio_listener_receive(mp_obj_t self_in) {
+ canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_listener_check_for_deinit(self);
+
+ mp_obj_t message = common_hal_canio_listener_receive(self);
+ // note: receive fills out the type field of the message
+
+ if (message) {
+ return message;
+ }
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_receive_obj, canio_listener_receive);
+
+//| def in_waiting(self) -> int:
+//| """Returns the number of messages (including remote
+//| transmission requests) waiting"""
+//| ...
+//|
+STATIC mp_obj_t canio_listener_in_waiting(mp_obj_t self_in) {
+ canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_listener_check_for_deinit(self);
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_listener_in_waiting(self));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_in_waiting_obj, canio_listener_in_waiting);
+
+//| def __iter__(self) -> Listener:
+//| """Returns self
+//|
+//| This method exists so that `Listener` can be used as an
+//| iterable"""
+//| ...
+//|
+//| def __next__(self) -> Union[RemoteTransmissionRequest,Message]:
+//| """Reads a message, after waiting up to self.timeout seconds
+//|
+//| If no message is received in time, raises StopIteration. Otherwise,
+//| a Message or is returned.
+//|
+//| This method enables the `Listener` to be used as an
+//| iterable, for instance in a for-loop."""
+//| ...
+//|
+STATIC mp_obj_t canio_iternext(mp_obj_t self_in) {
+ mp_obj_t result = canio_listener_receive(self_in);
+ if (result == mp_const_none) {
+ return MP_OBJ_STOP_ITERATION;
+ }
+ return result;
+}
+
+//| def deinit(self) -> None:
+//| """Deinitialize this object, freeing its hardware resources"""
+//| ...
+//|
+STATIC mp_obj_t canio_listener_deinit(mp_obj_t self_in) {
+ canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_listener_deinit(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_deinit_obj, canio_listener_deinit);
+
+//| def __enter__(self) -> CAN:
+//| """Returns self, to allow the object to be used in a `with` statement for resource control"""
+//| ...
+//|
+STATIC mp_obj_t canio_listener_enter(mp_obj_t self_in) {
+ canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_listener_check_for_deinit(self);
+ return self_in;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_enter_obj, canio_listener_enter);
+
+//| def __exit__(self, unused1: Optional[Type[BaseException]], unused2: Optional[BaseException], unused3: Optional[TracebackType]) -> None:
+//| """Calls deinit()"""
+//| ...
+STATIC mp_obj_t canio_listener_exit(size_t num_args, const mp_obj_t args[]) {
+ canio_listener_obj_t *self = MP_OBJ_TO_PTR(args[0]);
+ common_hal_canio_listener_deinit(self);
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(canio_listener_exit_obj, 4, 4, canio_listener_exit);
+
+
+//| timeout : float
+STATIC mp_obj_t canio_listener_timeout_get(mp_obj_t self_in) {
+ canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_listener_check_for_deinit(self);
+ return mp_obj_new_float(common_hal_canio_listener_get_timeout(self));
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_1(canio_listener_timeout_get_obj, canio_listener_timeout_get);
+
+STATIC mp_obj_t canio_listener_timeout_set(mp_obj_t self_in, mp_obj_t timeout_in) {
+ canio_listener_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_canio_listener_check_for_deinit(self);
+ common_hal_canio_listener_set_timeout(self, mp_obj_get_float(timeout_in));
+ return mp_const_none;
+}
+STATIC MP_DEFINE_CONST_FUN_OBJ_2(canio_listener_timeout_set_obj, canio_listener_timeout_set);
+
+STATIC const mp_obj_property_t canio_listener_timeout_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_listener_timeout_get_obj,
+ (mp_obj_t)&canio_listener_timeout_set_obj,
+ (mp_obj_t)mp_const_none},
+};
+
+
+
+STATIC const mp_rom_map_elem_t canio_listener_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR___enter__), MP_ROM_PTR(&canio_listener_enter_obj) },
+ { MP_ROM_QSTR(MP_QSTR___exit__), MP_ROM_PTR(&canio_listener_exit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_deinit), MP_ROM_PTR(&canio_listener_deinit_obj) },
+ { MP_ROM_QSTR(MP_QSTR_in_waiting), MP_ROM_PTR(&canio_listener_in_waiting_obj) },
+ { MP_ROM_QSTR(MP_QSTR_receive), MP_ROM_PTR(&canio_listener_receive_obj) },
+ { MP_ROM_QSTR(MP_QSTR_timeout), MP_ROM_PTR(&canio_listener_timeout_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(canio_listener_locals_dict, canio_listener_locals_dict_table);
+
+const mp_obj_type_t canio_listener_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Listener,
+ .getiter = mp_identity_getiter,
+ .iternext = canio_iternext,
+ .locals_dict = (mp_obj_dict_t*)&canio_listener_locals_dict,
+};
diff --git a/shared-bindings/canio/Listener.h b/shared-bindings/canio/Listener.h
new file mode 100644
index 000000000..527ffe4cb
--- /dev/null
+++ b/shared-bindings/canio/Listener.h
@@ -0,0 +1,43 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "py/obj.h"
+#include "shared-bindings/canio/CAN.h"
+#include "shared-bindings/canio/Match.h"
+
+extern const mp_obj_type_t canio_listener_type;
+
+typedef struct canio_listener_obj canio_listener_obj_t;
+
+void common_hal_canio_listener_construct(canio_listener_obj_t *self, canio_can_obj_t *can, size_t nmatch, canio_match_obj_t **matches, float timeout);
+void common_hal_canio_listener_check_for_deinit(canio_listener_obj_t *self);
+void common_hal_canio_listener_deinit(canio_listener_obj_t *self);
+mp_obj_t common_hal_canio_listener_receive(canio_listener_obj_t *self);
+int common_hal_canio_listener_in_waiting(canio_listener_obj_t *self);
+float common_hal_canio_listener_get_timeout(canio_listener_obj_t *self);
+void common_hal_canio_listener_set_timeout(canio_listener_obj_t *self, float timeout);
diff --git a/shared-bindings/canio/Match.c b/shared-bindings/canio/Match.c
new file mode 100644
index 000000000..6039631fa
--- /dev/null
+++ b/shared-bindings/canio/Match.c
@@ -0,0 +1,139 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/canio/Match.h"
+
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+//| class Match:
+//| """Describe CAN bus messages to match"""
+//|
+//|
+//| def __init__(self, id: int, *, mask: Optional[int] = None, extended: bool = False) -> None:
+//| """Construct a Match with the given properties.
+//|
+//| If mask is not None, then the filter is for any id which matches all
+//| the nonzero bits in mask. Otherwise, it matches exactly the given id.
+//| If extended is true then only extended ids are matched, otherwise
+//| only standard ids are matched."""
+//|
+
+STATIC mp_obj_t canio_match_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_id, ARG_mask, ARG_extended, NUM_ARGS };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
+ { MP_QSTR_mask, MP_ARG_OBJ, {.u_obj = mp_const_none } },
+ { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
+
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ int id_bits = args[ARG_extended].u_bool ? 0x1fffffff : 0x7ff;
+ int id = args[ARG_id].u_int;
+ int mask = args[ARG_mask].u_obj == mp_const_none ? id_bits : mp_obj_get_int(args[ARG_mask].u_obj);
+
+ if (id & ~id_bits) {
+ mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_id);
+ }
+
+ if (mask & ~id_bits) {
+ mp_raise_ValueError_varg(translate("%q out of range"), MP_QSTR_mask);
+ }
+
+ canio_match_obj_t *self = m_new_obj(canio_match_obj_t);
+ self->base.type = &canio_match_type;
+ common_hal_canio_match_construct(self, id, mask, args[ARG_extended].u_bool);
+ return self;
+}
+
+//| id: int
+//| """The id to match"""
+//|
+
+STATIC mp_obj_t canio_match_id_get(mp_obj_t self_in) {
+ canio_match_obj_t *self = self_in;
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_match_get_id(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_match_id_get_obj, canio_match_id_get);
+
+const mp_obj_property_t canio_match_id_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_match_id_get_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//|
+//| mask: int
+//| """The optional mask of ids to match"""
+//|
+
+STATIC mp_obj_t canio_match_mask_get(mp_obj_t self_in) {
+ canio_match_obj_t *self = self_in;
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_match_get_mask(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_match_mask_get_obj, canio_match_mask_get);
+
+const mp_obj_property_t canio_match_mask_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_match_mask_get_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| extended: bool
+//| """True to match extended ids, False to match standard ides"""
+//|
+
+STATIC mp_obj_t canio_match_extended_get(mp_obj_t self_in) {
+ canio_match_obj_t *self = self_in;
+ return mp_obj_new_bool(common_hal_canio_match_get_extended(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_match_extended_get_obj, canio_match_extended_get);
+
+const mp_obj_property_t canio_match_extended_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_match_extended_get_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+STATIC const mp_rom_map_elem_t canio_match_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_match_id_obj) },
+ { MP_ROM_QSTR(MP_QSTR_mask), MP_ROM_PTR(&canio_match_mask_obj) },
+ { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_match_extended_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(canio_match_locals_dict, canio_match_locals_dict_table);
+
+const mp_obj_type_t canio_match_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Match,
+ .make_new = canio_match_make_new,
+ .locals_dict = (mp_obj_dict_t*)&canio_match_locals_dict,
+};
diff --git a/shared-bindings/canio/Match.h b/shared-bindings/canio/Match.h
new file mode 100644
index 000000000..01a75fd1c
--- /dev/null
+++ b/shared-bindings/canio/Match.h
@@ -0,0 +1,37 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "py/obj.h"
+#include "shared-module/canio/Match.h"
+
+extern const mp_obj_type_t canio_match_type;
+
+void common_hal_canio_match_construct(canio_match_obj_t *self, int id, int mask, bool extended);
+int common_hal_canio_match_get_id(const canio_match_obj_t *self);
+int common_hal_canio_match_get_mask(const canio_match_obj_t *self);
+bool common_hal_canio_match_get_extended(const canio_match_obj_t *self);
diff --git a/shared-bindings/canio/Message.c b/shared-bindings/canio/Message.c
new file mode 100644
index 000000000..04fa8fc6c
--- /dev/null
+++ b/shared-bindings/canio/Message.c
@@ -0,0 +1,159 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/canio/Message.h"
+
+#include "py/obj.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+//| class Message:
+//| def __init__(self, id: int, data: bytes, *, extended: bool = False) -> None:
+//| """Construct a Message to send on a CAN bus.
+//|
+//| :param int id: The numeric ID of the message
+//| :param bytes data: The content of the message
+//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier
+//|
+//| In CAN, messages can have a length from 0 to 8 bytes.
+//| """
+//| ...
+//|
+STATIC mp_obj_t canio_message_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_id, ARG_data, ARG_extended, NUM_ARGS };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
+ { MP_QSTR_data, MP_ARG_OBJ | MP_ARG_REQUIRED },
+ { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
+
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ mp_buffer_info_t data;
+ mp_get_buffer_raise(args[ARG_data].u_obj, &data, MP_BUFFER_READ);
+
+ if (data.len > 8) {
+ mp_raise_ValueError(translate("Messages limited to 8 bytes"));
+ }
+
+ canio_message_obj_t *self = m_new_obj(canio_message_obj_t);
+ self->base.type = &canio_message_type;
+ common_hal_canio_message_construct(self, args[ARG_id].u_int, data.buf, data.len, args[ARG_extended].u_bool);
+ return self;
+}
+
+//| id: int
+//| """The numeric ID of the message"""
+//|
+STATIC mp_obj_t canio_message_id_get(const mp_obj_t self_in) {
+ canio_message_obj_t *self = self_in;
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_message_get_id(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_message_id_get_obj, canio_message_id_get);
+
+STATIC mp_obj_t canio_message_id_set(const mp_obj_t self_in, const mp_obj_t id) {
+ canio_message_obj_t *self = self_in;
+ common_hal_canio_message_set_id(self, mp_obj_get_int(id));
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_message_id_set_obj, canio_message_id_set);
+
+STATIC const mp_obj_property_t canio_message_id_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_message_id_get_obj,
+ (mp_obj_t)&canio_message_id_set_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| data: bytes
+//| """The content of the message"""
+//|
+STATIC mp_obj_t canio_message_data_get(const mp_obj_t self_in) {
+ canio_message_obj_t *self = self_in;
+ return mp_obj_new_bytes((const byte*)common_hal_canio_message_get_data(self), common_hal_canio_message_get_length(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_message_data_get_obj, canio_message_data_get);
+
+STATIC mp_obj_t canio_message_data_set(const mp_obj_t self_in, const mp_obj_t data_in) {
+ canio_message_obj_t *self = self_in;
+ mp_buffer_info_t data;
+ mp_get_buffer_raise(data_in, &data, MP_BUFFER_READ);
+ if (data.len > 8) {
+ mp_raise_ValueError(translate("Messages limited to 8 bytes"));
+ }
+ common_hal_canio_message_set_data(self, data.buf, data.len);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_message_data_set_obj, canio_message_data_set);
+
+
+STATIC const mp_obj_property_t canio_message_data_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_message_data_get_obj,
+ (mp_obj_t)&canio_message_data_set_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+
+//| extended: bool
+//| """True if the message's id is an extended id"""
+//|
+STATIC mp_obj_t canio_message_extended_get(const mp_obj_t self_in) {
+ canio_message_obj_t *self = self_in;
+ return mp_obj_new_bool(common_hal_canio_message_get_extended(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_message_extended_get_obj, canio_message_extended_get);
+
+STATIC mp_obj_t canio_message_extended_set(const mp_obj_t self_in, const mp_obj_t extended) {
+ canio_message_obj_t *self = self_in;
+ common_hal_canio_message_set_extended(self, mp_obj_is_true(extended));
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_message_extended_set_obj, canio_message_extended_set);
+
+
+STATIC const mp_obj_property_t canio_message_extended_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_message_extended_get_obj,
+ (mp_obj_t)&canio_message_extended_set_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+STATIC const mp_rom_map_elem_t canio_message_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_message_id_obj) },
+ { MP_ROM_QSTR(MP_QSTR_data), MP_ROM_PTR(&canio_message_data_obj) },
+ { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_message_extended_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(canio_message_locals_dict, canio_message_locals_dict_table);
+
+const mp_obj_type_t canio_message_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_Message,
+ .make_new = canio_message_make_new,
+ .locals_dict = (mp_obj_t)&canio_message_locals_dict,
+};
diff --git a/shared-bindings/canio/Message.h b/shared-bindings/canio/Message.h
new file mode 100644
index 000000000..b76535939
--- /dev/null
+++ b/shared-bindings/canio/Message.h
@@ -0,0 +1,42 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "py/obj.h"
+#include "shared-module/canio/Message.h"
+
+extern const mp_obj_type_t canio_message_type;
+extern const mp_obj_type_t canio_remote_transmission_request_type;
+
+void common_hal_canio_message_construct(canio_message_obj_t *self, int id, void *data, size_t size, bool extended);
+const void *common_hal_canio_message_get_data(const canio_message_obj_t *self);
+void common_hal_canio_message_set_data(canio_message_obj_t *self, const void *data, size_t size);
+bool common_hal_canio_message_get_extended(const canio_message_obj_t *self);
+void common_hal_canio_message_set_extended(canio_message_obj_t *self, bool extended);
+int common_hal_canio_message_get_id(const canio_message_obj_t *self);
+void common_hal_canio_message_set_id(canio_message_obj_t *self, int id);
+size_t common_hal_canio_message_get_length(const canio_message_obj_t *self);
diff --git a/shared-bindings/canio/RemoteTransmissionRequest.c b/shared-bindings/canio/RemoteTransmissionRequest.c
new file mode 100644
index 000000000..fcd259034
--- /dev/null
+++ b/shared-bindings/canio/RemoteTransmissionRequest.c
@@ -0,0 +1,156 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/canio/RemoteTransmissionRequest.h"
+
+#include "py/obj.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+//| class RemoteTransmissionRequest:
+//| def __init__(self, id: int, length: int, *, extended: bool = False) -> None:
+//| """Construct a RemoteTransmissionRequest to send on a CAN bus.
+//|
+//| :param int id: The numeric ID of the requested message
+//| :param int length: The length of the requested message
+//| :param bool extended: True if the message has an extended identifier, False if it has a standard identifier
+//|
+//| In CAN, messages can have a length from 0 to 8 bytes.
+//| """
+//| ...
+//|
+STATIC mp_obj_t canio_remote_transmission_request_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_id, ARG_length, ARG_extended, NUM_ARGS };
+ static const mp_arg_t allowed_args[] = {
+ { MP_QSTR_id, MP_ARG_INT | MP_ARG_REQUIRED },
+ { MP_QSTR_length, MP_ARG_INT | MP_ARG_REQUIRED },
+ { MP_QSTR_extended, MP_ARG_BOOL, {.u_bool = false} },
+ };
+ mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
+ MP_STATIC_ASSERT( MP_ARRAY_SIZE(allowed_args) == NUM_ARGS );
+
+ mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
+
+ int length = args[ARG_length].u_int;
+ if (length < 0 || length > 8) {
+ mp_raise_ValueError(translate("RemoteTransmissionRequests limited to 8 bytes"));
+ }
+
+ canio_remote_transmission_request_obj_t *self = m_new_obj(canio_remote_transmission_request_obj_t);
+ self->base.type = &canio_remote_transmission_request_type;
+ common_hal_canio_remote_transmission_request_construct(self, args[ARG_id].u_int, length, args[ARG_extended].u_bool);
+ return self;
+}
+
+
+//| id: int
+//| """The numeric ID of the message"""
+//|
+STATIC mp_obj_t canio_remote_transmission_request_id_get(const mp_obj_t self_in) {
+ canio_remote_transmission_request_obj_t *self = self_in;
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_remote_transmission_request_get_id(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_id_get_obj, canio_remote_transmission_request_id_get);
+
+STATIC mp_obj_t canio_remote_transmission_request_id_set(const mp_obj_t self_in, const mp_obj_t id) {
+ canio_remote_transmission_request_obj_t *self = self_in;
+ common_hal_canio_remote_transmission_request_set_id(self, mp_obj_get_int(id));
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_id_set_obj, canio_remote_transmission_request_id_set);
+
+STATIC const mp_obj_property_t canio_remote_transmission_request_id_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_remote_transmission_request_id_get_obj,
+ (mp_obj_t)&canio_remote_transmission_request_id_set_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| extended: bool
+//| """True if the message's id is an extended id"""
+//|
+STATIC mp_obj_t canio_remote_transmission_request_extended_get(const mp_obj_t self_in) {
+ canio_remote_transmission_request_obj_t *self = self_in;
+ return mp_obj_new_bool(common_hal_canio_remote_transmission_request_get_extended(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_extended_get_obj, canio_remote_transmission_request_extended_get);
+
+STATIC mp_obj_t canio_remote_transmission_request_extended_set(const mp_obj_t self_in, const mp_obj_t extended) {
+ canio_remote_transmission_request_obj_t *self = self_in;
+ common_hal_canio_remote_transmission_request_set_extended(self, mp_obj_is_true(extended));
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_extended_set_obj, canio_remote_transmission_request_extended_set);
+
+
+STATIC const mp_obj_property_t canio_remote_transmission_request_extended_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_remote_transmission_request_extended_get_obj,
+ (mp_obj_t)&canio_remote_transmission_request_extended_set_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+//| length: int
+//| """The length of the requested message."""
+//|
+STATIC mp_obj_t canio_remote_transmission_request_length_get(const mp_obj_t self_in) {
+ canio_remote_transmission_request_obj_t *self = self_in;
+ return MP_OBJ_NEW_SMALL_INT(common_hal_canio_remote_transmission_request_get_length(self));
+}
+MP_DEFINE_CONST_FUN_OBJ_1(canio_remote_transmission_request_length_get_obj, canio_remote_transmission_request_length_get);
+
+STATIC mp_obj_t canio_remote_transmission_request_length_set(const mp_obj_t self_in, const mp_obj_t length_in) {
+ canio_remote_transmission_request_obj_t *self = self_in;
+ int length = mp_obj_get_int(length_in);
+ if (length < 0 || length > 8) {
+ mp_raise_ValueError(translate("RemoteTransmissionRequests limited to 8 bytes"));
+ }
+ common_hal_canio_remote_transmission_request_set_length(self, length);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(canio_remote_transmission_request_length_set_obj, canio_remote_transmission_request_length_set);
+
+
+STATIC const mp_obj_property_t canio_remote_transmission_request_length_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&canio_remote_transmission_request_length_get_obj,
+ (mp_obj_t)&canio_remote_transmission_request_length_set_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
+STATIC const mp_rom_map_elem_t canio_remote_transmission_request_locals_dict_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_id), MP_ROM_PTR(&canio_remote_transmission_request_id_obj) },
+ { MP_ROM_QSTR(MP_QSTR_length), MP_ROM_PTR(&canio_remote_transmission_request_length_obj) },
+ { MP_ROM_QSTR(MP_QSTR_extended), MP_ROM_PTR(&canio_remote_transmission_request_extended_obj) },
+};
+STATIC MP_DEFINE_CONST_DICT(canio_remote_transmission_request_locals_dict, canio_remote_transmission_request_locals_dict_table);
+
+const mp_obj_type_t canio_remote_transmission_request_type = {
+ { &mp_type_type },
+ .name = MP_QSTR_RemoteTransmissionRequest,
+ .make_new = canio_remote_transmission_request_make_new,
+ .locals_dict = (mp_obj_t)&canio_remote_transmission_request_locals_dict,
+};
diff --git a/shared-bindings/canio/RemoteTransmissionRequest.h b/shared-bindings/canio/RemoteTransmissionRequest.h
new file mode 100644
index 000000000..8956587b3
--- /dev/null
+++ b/shared-bindings/canio/RemoteTransmissionRequest.h
@@ -0,0 +1,42 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+#include "py/obj.h"
+#include "shared-module/canio/RemoteTransmissionRequest.h"
+
+extern const mp_obj_type_t canio_remote_transmission_request_type;
+
+void common_hal_canio_remote_transmission_request_construct(canio_remote_transmission_request_obj_t *self, int id, size_t size, bool extended);
+const void *common_hal_canio_remote_transmission_request_get_data(const canio_remote_transmission_request_obj_t *self);
+void common_hal_canio_remote_transmission_request_set_data(canio_remote_transmission_request_obj_t *self, const void *data, size_t size);
+bool common_hal_canio_remote_transmission_request_get_extended(const canio_remote_transmission_request_obj_t *self);
+void common_hal_canio_remote_transmission_request_set_extended(canio_remote_transmission_request_obj_t *self, bool extended);
+int common_hal_canio_remote_transmission_request_get_id(const canio_remote_transmission_request_obj_t *self);
+void common_hal_canio_remote_transmission_request_set_id(canio_remote_transmission_request_obj_t *self, int id);
+size_t common_hal_canio_remote_transmission_request_get_length(const canio_remote_transmission_request_obj_t *self);
+void common_hal_canio_remote_transmission_request_set_length(canio_remote_transmission_request_obj_t *self, size_t length);
diff --git a/shared-bindings/canio/__init__.c b/shared-bindings/canio/__init__.c
new file mode 100644
index 000000000..f29d3ab8a
--- /dev/null
+++ b/shared-bindings/canio/__init__.c
@@ -0,0 +1,125 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+//| """CAN bus access
+//|
+//| The `canio` module contains low level classes to support the CAN bus
+//| protocol.
+//|
+//| CAN and Listener classes change hardware state and should be deinitialized when they
+//| are no longer needed if the program continues after use. To do so, either
+//| call :py:meth:`!deinit` or use a context manager. See
+//| :ref:`lifetime-and-contextmanagers` for more info.
+//|
+//| For example::
+//|
+//| import canio
+//| from board import *
+//|
+//| can = canio.CAN(board.CAN_RX, board.CAN_TX, baudrate=1000000)
+//| message = canio.Message(id=0x0408, data=b"adafruit")
+//| can.send(message)
+//| can.deinit()
+//|
+//| This example will write the data 'adafruit' onto the CAN bus to any
+//| device listening for message id 0x0408.
+//|
+//| A CAN bus involves a transceiver, which is often a separate chip with a "standby" pin.
+//| If your board has a CAN_STANDBY pin, ensure to set it to an output with the value False
+//| to enable the transceiver.
+//|
+//| Other implementations of the CAN device may exist (for instance, attached
+//| via an SPI bus). If so their constructor arguments may differ, but
+//| otherwise we encourage implementors to follow the API that the core uses.
+//| """
+//|
+
+#include "py/obj.h"
+#include "py/enum.h"
+
+#include "shared-bindings/canio/__init__.h"
+#include "shared-bindings/canio/CAN.h"
+#include "shared-bindings/canio/Match.h"
+#include "shared-bindings/canio/Message.h"
+#include "shared-bindings/canio/Listener.h"
+
+MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, ERROR_ACTIVE, BUS_STATE_ERROR_ACTIVE);
+MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, ERROR_PASSIVE, BUS_STATE_ERROR_PASSIVE);
+MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, ERROR_WARNING, BUS_STATE_ERROR_WARNING);
+MAKE_ENUM_VALUE(canio_bus_state_type, bus_state, BUS_OFF, BUS_STATE_OFF);
+
+//| class BusState:
+//| """The state of the CAN bus"""
+//|
+//| ERROR_ACTIVE: object
+//| """The bus is in the normal (active) state"""
+//|
+//| ERROR_WARNING: object
+//| """The bus is in the normal (active) state, but a moderate number of errors have occurred recently.
+//|
+//| NOTE: Not all implementations may use ERROR_WARNING. Do not rely on seeing ERROR_WARNING before ERROR_PASSIVE."""
+//|
+//| ERROR_PASSIVE: object
+//| """The bus is in the passive state due to the number of errors that have occurred recently.
+//|
+//| This device will acknowledge packets it receives, but cannot transmit messages.
+//| If additional errors occur, this device may progress to BUS_OFF.
+//| If it successfully acknowledges other packets on the bus, it can return to ERROR_WARNING or ERROR_ACTIVE and transmit packets.
+//| """
+//|
+//| BUS_OFF: object
+//| """The bus has turned off due to the number of errors that have
+//| occurred recently. It must be restarted before it will send or receive
+//| packets. This device will neither send or acknowledge packets on the bus."""
+//|
+MAKE_ENUM_MAP(canio_bus_state) {
+ MAKE_ENUM_MAP_ENTRY(bus_state, ERROR_ACTIVE),
+ MAKE_ENUM_MAP_ENTRY(bus_state, ERROR_PASSIVE),
+ MAKE_ENUM_MAP_ENTRY(bus_state, ERROR_WARNING),
+ MAKE_ENUM_MAP_ENTRY(bus_state, BUS_OFF),
+};
+STATIC MP_DEFINE_CONST_DICT(canio_bus_state_locals_dict, canio_bus_state_locals_table);
+
+MAKE_PRINTER(canio, canio_bus_state);
+
+MAKE_ENUM_TYPE(canio, BusState, canio_bus_state);
+
+STATIC const mp_rom_map_elem_t canio_module_globals_table[] = {
+ { MP_ROM_QSTR(MP_QSTR_BusState), MP_ROM_PTR(&canio_bus_state_type) },
+ { MP_ROM_QSTR(MP_QSTR_CAN), MP_ROM_PTR(&canio_can_type) },
+ { MP_ROM_QSTR(MP_QSTR_Listener), MP_ROM_PTR(&canio_listener_type) },
+ { MP_ROM_QSTR(MP_QSTR_Match), MP_ROM_PTR(&canio_match_type) },
+ { MP_ROM_QSTR(MP_QSTR_Message), MP_ROM_PTR(&canio_message_type) },
+ { MP_ROM_QSTR(MP_QSTR_RemoteTransmissionRequest), MP_ROM_PTR(&canio_remote_transmission_request_type) },
+ { MP_ROM_QSTR(MP_QSTR___name__), MP_ROM_QSTR(MP_QSTR__canio) },
+};
+
+STATIC MP_DEFINE_CONST_DICT(canio_module_globals, canio_module_globals_table);
+
+const mp_obj_module_t canio_module = {
+ .base = { &mp_type_module },
+ .globals = (mp_obj_dict_t*)&canio_module_globals,
+};
diff --git a/shared-bindings/canio/__init__.h b/shared-bindings/canio/__init__.h
new file mode 100644
index 000000000..e24eba92c
--- /dev/null
+++ b/shared-bindings/canio/__init__.h
@@ -0,0 +1,33 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Jeff Epler for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#pragma once
+
+typedef enum {
+ BUS_STATE_ERROR_ACTIVE, BUS_STATE_ERROR_PASSIVE, BUS_STATE_ERROR_WARNING, BUS_STATE_OFF
+} canio_bus_state_t;
+
+extern const mp_obj_type_t canio_bus_state_type;
diff --git a/shared-bindings/displayio/Bitmap.c b/shared-bindings/displayio/Bitmap.c
index 5a2fc785f..b9f06fe14 100644
--- a/shared-bindings/displayio/Bitmap.c
+++ b/shared-bindings/displayio/Bitmap.c
@@ -172,7 +172,7 @@ STATIC mp_obj_t bitmap_subscr(mp_obj_t self_in, mp_obj_t index_obj, mp_obj_t val
return mp_const_none;
}
-//| def blit(self, x: int, y: int, source_bitmap: bitmap, *, x1: int, y1: int, x2: int, y2: int, skip_index: int) -> None:
+//| def blit(self, x: int, y: int, source_bitmap: Bitmap, *, x1: int, y1: int, x2: int, y2: int, skip_index: int) -> None:
//| """Inserts the source_bitmap region defined by rectangular boundaries
//| (x1,y1) and (x2,y2) into the bitmap at the specified (x,y) location.
//|
@@ -274,7 +274,7 @@ STATIC mp_obj_t displayio_bitmap_obj_blit(size_t n_args, const mp_obj_t *pos_arg
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_bitmap_blit_obj, 4, displayio_bitmap_obj_blit);
// `displayio_bitmap_obj_blit` requires at least 4 arguments
-//| def fill(self, value: Any) -> None:
+//| def fill(self, value: int) -> None:
//| """Fills the bitmap with the supplied palette index value."""
//| ...
//|
diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c
index a2b6699ef..0e6e9bfd9 100644
--- a/shared-bindings/displayio/ColorConverter.c
+++ b/shared-bindings/displayio/ColorConverter.c
@@ -52,7 +52,7 @@ STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, siz
enum { ARG_dither};
static const mp_arg_t allowed_args[] = {
- { MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} },
+ { MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -110,9 +110,35 @@ const mp_obj_property_t displayio_colorconverter_dither_obj = {
(mp_obj_t)&mp_const_none_obj},
};
+//| def make_transparent(self, pixel: int) -> None:
+//| """Sets a pixel to not opaque."""
+//|
+STATIC mp_obj_t displayio_colorconverter_make_transparent(mp_obj_t self_in, mp_obj_t transparent_color_obj) {
+ displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in);
+
+ mp_int_t transparent_color = mp_obj_get_int(&transparent_color);
+ common_hal_displayio_colorconverter_make_transparent(self, transparent_color);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_make_transparent_obj, displayio_colorconverter_make_transparent);
+
+//| def make_opaque(self, pixel: int) -> None:
+//| """Sets a pixel to opaque."""
+//|
+STATIC mp_obj_t displayio_colorconverter_make_opaque(mp_obj_t self_in, mp_obj_t transparent_color_obj) {
+ displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in);
+
+ mp_int_t transparent_color = mp_obj_get_int(&transparent_color);
+ common_hal_displayio_colorconverter_make_opaque(self, transparent_color);
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_make_opaque_obj, displayio_colorconverter_make_opaque);
+
STATIC const mp_rom_map_elem_t displayio_colorconverter_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_convert), MP_ROM_PTR(&displayio_colorconverter_convert_obj) },
{ MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&displayio_colorconverter_dither_obj) },
+ { MP_ROM_QSTR(MP_QSTR_make_transparent), MP_ROM_PTR(&displayio_colorconverter_make_transparent_obj) },
+ { MP_ROM_QSTR(MP_QSTR_make_opaque), MP_ROM_PTR(&displayio_colorconverter_make_opaque_obj) },
};
STATIC MP_DEFINE_CONST_DICT(displayio_colorconverter_locals_dict, displayio_colorconverter_locals_dict_table);
diff --git a/shared-bindings/displayio/ColorConverter.h b/shared-bindings/displayio/ColorConverter.h
index d550d81be..441f7c7ae 100644
--- a/shared-bindings/displayio/ColorConverter.h
+++ b/shared-bindings/displayio/ColorConverter.h
@@ -39,4 +39,7 @@ void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *col
void common_hal_displayio_colorconverter_set_dither(displayio_colorconverter_t* self, bool dither);
bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t* self);
+void common_hal_displayio_colorconverter_make_transparent(displayio_colorconverter_t* self, uint32_t transparent_color);
+void common_hal_displayio_colorconverter_make_opaque(displayio_colorconverter_t* self, uint32_t transparent_color);
+
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H
diff --git a/shared-bindings/displayio/Display.c b/shared-bindings/displayio/Display.c
index f36aeed18..1ed59f233 100644
--- a/shared-bindings/displayio/Display.c
+++ b/shared-bindings/displayio/Display.c
@@ -39,7 +39,7 @@
#include "shared-module/displayio/__init__.h"
#include "supervisor/shared/translate.h"
-//| _DisplayBus = Union[FourWire, ParallelBus, I2CDisplay]
+//| _DisplayBus = Union['FourWire', 'ParallelBus', 'I2CDisplay']
//| """:py:class:`FourWire`, :py:class:`ParallelBus` or :py:class:`I2CDisplay`"""
//|
@@ -105,13 +105,22 @@
//| :param bool auto_brightness: If True, brightness is controlled via an ambient light sensor or other mechanism.
//| :param bool single_byte_bounds: Display column and row commands use single bytes
//| :param bool data_as_commands: Treat all init and boundary data as SPI commands. Certain displays require this.
+//| :param bool SH1107_addressing: Special quirk for SH1107, use upper/lower column set and page set
//| :param bool auto_refresh: Automatically refresh the screen
//| :param int native_frames_per_second: Number of display refreshes per second that occur with the given init_sequence.
//| :param bool backlight_on_high: If True, pulling the backlight pin high turns the backlight on."""
//| ...
//|
-STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_color_depth, ARG_grayscale, ARG_pixels_in_byte_share_row, ARG_bytes_per_cell, ARG_reverse_pixels_in_byte, ARG_reverse_bytes_in_word, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command, ARG_set_vertical_scroll, ARG_backlight_pin, ARG_brightness_command, ARG_brightness, ARG_auto_brightness, ARG_single_byte_bounds, ARG_data_as_commands, ARG_auto_refresh, ARG_native_frames_per_second, ARG_backlight_on_high };
+STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_args,
+ const mp_obj_t *pos_args, mp_map_t *kw_args) {
+ enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart,
+ ARG_rotation, ARG_color_depth, ARG_grayscale, ARG_pixels_in_byte_share_row,
+ ARG_bytes_per_cell, ARG_reverse_pixels_in_byte, ARG_reverse_bytes_in_word,
+ ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command,
+ ARG_set_vertical_scroll, ARG_backlight_pin, ARG_brightness_command,
+ ARG_brightness, ARG_auto_brightness, ARG_single_byte_bounds, ARG_data_as_commands,
+ ARG_auto_refresh, ARG_native_frames_per_second, ARG_backlight_on_high,
+ ARG_SH1107_addressing };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_display_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -139,6 +148,7 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
{ MP_QSTR_auto_refresh, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
{ MP_QSTR_native_frames_per_second, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 60} },
{ MP_QSTR_backlight_on_high, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
+ { MP_QSTR_SH1107_addressing, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -180,7 +190,8 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a
args[ARG_data_as_commands].u_bool,
args[ARG_auto_refresh].u_bool,
args[ARG_native_frames_per_second].u_int,
- args[ARG_backlight_on_high].u_bool
+ args[ARG_backlight_on_high].u_bool,
+ args[ARG_SH1107_addressing].u_bool
);
return self;
diff --git a/shared-bindings/displayio/Display.h b/shared-bindings/displayio/Display.h
index e69c5b6b5..f9fbc157c 100644
--- a/shared-bindings/displayio/Display.h
+++ b/shared-bindings/displayio/Display.h
@@ -45,7 +45,8 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
uint8_t set_column_command, uint8_t set_row_command, uint8_t write_ram_command, uint8_t set_vertical_scroll,
uint8_t* init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t* backlight_pin, uint16_t brightness_command,
mp_float_t brightness, bool auto_brightness,
- bool single_byte_bounds, bool data_as_commands, bool auto_refresh, uint16_t native_frames_per_second, bool backlight_on_high);
+ bool single_byte_bounds, bool data_as_commands, bool auto_refresh, uint16_t native_frames_per_second,
+ bool backlight_on_high, bool SH1107_addressing);
bool common_hal_displayio_display_show(displayio_display_obj_t* self,
displayio_group_t* root_group);
diff --git a/shared-bindings/displayio/EPaperDisplay.c b/shared-bindings/displayio/EPaperDisplay.c
index 8be4ee4c4..e0326d9c8 100644
--- a/shared-bindings/displayio/EPaperDisplay.c
+++ b/shared-bindings/displayio/EPaperDisplay.c
@@ -49,7 +49,19 @@
//| Most people should not use this class directly. Use a specific display driver instead that will
//| contain the startup and shutdown sequences at minimum."""
//|
-//| def __init__(self, display_bus: _DisplayBus, start_sequence: ReadableBuffer, stop_sequence: ReadableBuffer, *, width: int, height: int, ram_width: int, ram_height: int, colstart: int = 0, rowstart: int = 0, rotation: int = 0, set_column_window_command: Optional[int] = None, set_row_window_command: Optional[int] = None, single_byte_bounds: bool = False, write_black_ram_command: int, black_bits_inverted: bool = False, write_color_ram_command: Optional[int] = None, color_bits_inverted: bool = False, highlight_color: int = 0x000000, refresh_display_command: int, refresh_time: float = 40, busy_pin: Optional[microcontroller.Pin] = None, busy_state: bool = True, seconds_per_frame: float = 180, always_toggle_chip_select: bool = False) -> None:
+//| def __init__(self, display_bus: _DisplayBus,
+//| start_sequence: ReadableBuffer, stop_sequence: ReadableBuffer, *,
+//| width: int, height: int, ram_width: int, ram_height: int,
+//| colstart: int = 0, rowstart: int = 0, rotation: int = 0,
+//| set_column_window_command: Optional[int] = None,
+//| set_row_window_command: Optional[int] = None, single_byte_bounds: bool = False,
+//| write_black_ram_command: int, black_bits_inverted: bool = False,
+//| write_color_ram_command: Optional[int] = None,
+//| color_bits_inverted: bool = False, highlight_color: int = 0x000000,
+//| refresh_display_command: int, refresh_time: float = 40,
+//| busy_pin: Optional[microcontroller.Pin] = None, busy_state: bool = True,
+//| seconds_per_frame: float = 180, always_toggle_chip_select: bool = False,
+//| grayscale: bool = False) -> None:
//| """Create a EPaperDisplay object on the given display bus (`displayio.FourWire` or `displayio.ParallelBus`).
//|
//| The ``start_sequence`` and ``stop_sequence`` are bitpacked to minimize the ram impact. Every
@@ -84,11 +96,18 @@
//| :param microcontroller.Pin busy_pin: Pin used to signify the display is busy
//| :param bool busy_state: State of the busy pin when the display is busy
//| :param float seconds_per_frame: Minimum number of seconds between screen refreshes
-//| :param bool always_toggle_chip_select: When True, chip select is toggled every byte"""
+//| :param bool always_toggle_chip_select: When True, chip select is toggled every byte
+//| :param bool grayscale: When true, the color ram is the low bit of 2-bit grayscale"""
//| ...
//|
STATIC mp_obj_t displayio_epaperdisplay_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_display_bus, ARG_start_sequence, ARG_stop_sequence, ARG_width, ARG_height, ARG_ram_width, ARG_ram_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_set_column_window_command, ARG_set_row_window_command, ARG_set_current_column_command, ARG_set_current_row_command, ARG_write_black_ram_command, ARG_black_bits_inverted, ARG_write_color_ram_command, ARG_color_bits_inverted, ARG_highlight_color, ARG_refresh_display_command, ARG_refresh_time, ARG_busy_pin, ARG_busy_state, ARG_seconds_per_frame, ARG_always_toggle_chip_select };
+ enum { ARG_display_bus, ARG_start_sequence, ARG_stop_sequence, ARG_width, ARG_height,
+ ARG_ram_width, ARG_ram_height, ARG_colstart, ARG_rowstart, ARG_rotation,
+ ARG_set_column_window_command, ARG_set_row_window_command, ARG_set_current_column_command,
+ ARG_set_current_row_command, ARG_write_black_ram_command, ARG_black_bits_inverted,
+ ARG_write_color_ram_command, ARG_color_bits_inverted, ARG_highlight_color,
+ ARG_refresh_display_command, ARG_refresh_time, ARG_busy_pin, ARG_busy_state,
+ ARG_seconds_per_frame, ARG_always_toggle_chip_select, ARG_grayscale };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_display_bus, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_start_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ },
@@ -115,6 +134,7 @@ STATIC mp_obj_t displayio_epaperdisplay_make_new(const mp_obj_type_t *type, size
{ MP_QSTR_busy_state, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} },
{ MP_QSTR_seconds_per_frame, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(180)} },
{ MP_QSTR_always_toggle_chip_select, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
+ { MP_QSTR_grayscale, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} },
};
mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)];
mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
@@ -151,11 +171,14 @@ STATIC mp_obj_t displayio_epaperdisplay_make_new(const mp_obj_type_t *type, size
self,
display_bus,
start_bufinfo.buf, start_bufinfo.len, stop_bufinfo.buf, stop_bufinfo.len,
- args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_ram_width].u_int, args[ARG_ram_height].u_int, args[ARG_colstart].u_int, args[ARG_rowstart].u_int, rotation,
+ args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_ram_width].u_int, args[ARG_ram_height].u_int,
+ args[ARG_colstart].u_int, args[ARG_rowstart].u_int, rotation,
args[ARG_set_column_window_command].u_int, args[ARG_set_row_window_command].u_int,
args[ARG_set_current_column_command].u_int, args[ARG_set_current_row_command].u_int,
- args[ARG_write_black_ram_command].u_int, args[ARG_black_bits_inverted].u_bool, write_color_ram_command, args[ARG_color_bits_inverted].u_bool, highlight_color, args[ARG_refresh_display_command].u_int, refresh_time,
- busy_pin, args[ARG_busy_state].u_bool, seconds_per_frame, args[ARG_always_toggle_chip_select].u_bool
+ args[ARG_write_black_ram_command].u_int, args[ARG_black_bits_inverted].u_bool, write_color_ram_command,
+ args[ARG_color_bits_inverted].u_bool, highlight_color, args[ARG_refresh_display_command].u_int, refresh_time,
+ busy_pin, args[ARG_busy_state].u_bool, seconds_per_frame,
+ args[ARG_always_toggle_chip_select].u_bool, args[ARG_grayscale].u_bool
);
return self;
diff --git a/shared-bindings/displayio/EPaperDisplay.h b/shared-bindings/displayio/EPaperDisplay.h
index e4b81c883..352de899a 100644
--- a/shared-bindings/displayio/EPaperDisplay.h
+++ b/shared-bindings/displayio/EPaperDisplay.h
@@ -44,7 +44,7 @@ void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t*
uint16_t set_column_window_command, uint16_t set_row_window_command,
uint16_t set_current_column_command, uint16_t set_current_row_command,
uint16_t write_black_ram_command, bool black_bits_inverted, uint16_t write_color_ram_command, bool color_bits_inverted, uint32_t highlight_color, uint16_t refresh_display_command, mp_float_t refresh_time,
- const mcu_pin_obj_t* busy_pin, bool busy_state, mp_float_t seconds_per_frame, bool always_toggle_chip_select);
+ const mcu_pin_obj_t* busy_pin, bool busy_state, mp_float_t seconds_per_frame, bool always_toggle_chip_select, bool grayscale);
bool common_hal_displayio_epaperdisplay_refresh(displayio_epaperdisplay_obj_t* self);
diff --git a/shared-bindings/ipaddress/IPv4Address.c b/shared-bindings/ipaddress/IPv4Address.c
index b2a10158a..e027f32d6 100644
--- a/shared-bindings/ipaddress/IPv4Address.c
+++ b/shared-bindings/ipaddress/IPv4Address.c
@@ -126,7 +126,7 @@ const mp_obj_property_t ipaddress_ipv4address_version_obj = {
(mp_obj_t)&mp_const_none_obj},
};
-//| def __eq__(self, other: IPv4Address) -> bool:
+//| def __eq__(self, other: object) -> bool:
//| """Two Address objects are equal if their addresses and address types are equal."""
//| ...
//|
diff --git a/shared-bindings/microcontroller/Pin.c b/shared-bindings/microcontroller/Pin.c
index 1294b78f0..6ba02a0e7 100644
--- a/shared-bindings/microcontroller/Pin.c
+++ b/shared-bindings/microcontroller/Pin.c
@@ -71,7 +71,11 @@ STATIC void mcu_pin_print(const mp_print_t *print, mp_obj_t self_in, mp_print_ki
qstr name;
get_pin_name(self, &package, &module, &name);
- mp_printf(print, "%q.%q.%q", MP_QSTR_microcontroller, MP_QSTR_pin, name);
+ if (package){
+ mp_printf(print, "%q.%q.%q", package, module, name);
+ } else {
+ mp_printf(print, "%q.%q", module , name);
+ }
}
const mp_obj_type_t mcu_pin_type = {
diff --git a/shared-bindings/rgbmatrix/RGBMatrix.c b/shared-bindings/rgbmatrix/RGBMatrix.c
index e4683af92..753c1c920 100644
--- a/shared-bindings/rgbmatrix/RGBMatrix.c
+++ b/shared-bindings/rgbmatrix/RGBMatrix.c
@@ -73,6 +73,10 @@ STATIC void preflight_pins_or_throw(uint8_t clock_pin, uint8_t *rgb_pins, uint8_
uint32_t port = clock_pin / 32;
uint32_t bit_mask = 1 << (clock_pin % 32);
+ if (rgb_pin_count <= 0 || rgb_pin_count % 6 != 0 || rgb_pin_count > 30) {
+ mp_raise_ValueError_varg(translate("The length of rgb_pins must be 6, 12, 18, 24, or 30"));
+ }
+
for (uint8_t i = 0; i < rgb_pin_count; i++) {
uint32_t pin_port = rgb_pins[i] / 32;
@@ -210,6 +214,10 @@ STATIC mp_obj_t rgbmatrix_rgbmatrix_make_new(const mp_obj_type_t *type, size_t n
}
}
+ if (args[ARG_width].u_int <= 0) {
+ mp_raise_ValueError(translate("width must be greater than zero"));
+ }
+
preflight_pins_or_throw(clock_pin, rgb_pins, rgb_count, true);
mp_obj_t framebuffer = args[ARG_framebuffer].u_obj;
diff --git a/shared-bindings/rgbmatrix/RGBMatrix.h b/shared-bindings/rgbmatrix/RGBMatrix.h
index 1dc5a406c..bfe37c340 100644
--- a/shared-bindings/rgbmatrix/RGBMatrix.h
+++ b/shared-bindings/rgbmatrix/RGBMatrix.h
@@ -27,7 +27,7 @@
#pragma once
#include "shared-module/rgbmatrix/RGBMatrix.h"
-#include "lib/protomatter/core.h"
+#include "lib/protomatter/src/core.h"
extern const mp_obj_type_t rgbmatrix_RGBMatrix_type;
diff --git a/shared-bindings/socket/__init__.c b/shared-bindings/socket/__init__.c
index 38840da5e..799bf28af 100644
--- a/shared-bindings/socket/__init__.c
+++ b/shared-bindings/socket/__init__.c
@@ -49,7 +49,14 @@ STATIC const mp_obj_type_t socket_type;
//| class socket:
//|
-//| def __init__(self, family: int, type: int, proto: int) -> None:
+//| AF_INET: int
+//| AF_INET6: int
+//| SOCK_STREAM: int
+//| SOCK_DGRAM: int
+//| SOCK_RAW: int
+//| IPPROTO_TCP: int
+//|
+//| def __init__(self, family: int = AF_INET, type: int = SOCK_STREAM, proto: int = IPPROTO_TCP) -> None:
//| """Create a new socket
//|
//| :param int family: AF_INET or AF_INET6
@@ -57,12 +64,6 @@ STATIC const mp_obj_type_t socket_type;
//| :param int proto: IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW (ignored)"""
//| ...
//|
-//| AF_INET: int
-//| AF_INET6: int
-//| SOCK_STREAM: int
-//| SOCK_DGRAM: int
-//| SOCK_RAW: int
-//|
STATIC mp_obj_t socket_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *args, mp_map_t *kw_args) {
mp_arg_check_num(n_args, kw_args, 0, 4, false);
diff --git a/shared-bindings/socketpool/Socket.c b/shared-bindings/socketpool/Socket.c
index 250e9c874..362e4d7e8 100644
--- a/shared-bindings/socketpool/Socket.c
+++ b/shared-bindings/socketpool/Socket.c
@@ -36,9 +36,6 @@
#include "py/runtime.h"
#include "py/mperrno.h"
-#include "esp_log.h"
-static const char* TAG = "socket binding";
-
//| class Socket:
//| """TCP, UDP and RAW socket. Cannot be created directly. Instead, call
//| `SocketPool.socket()`.
@@ -164,7 +161,7 @@ STATIC mp_obj_t socketpool_socket_close(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(socketpool_socket_close_obj, socketpool_socket_close);
-//| def connect(self, address: tuple) -> None:
+//| def connect(self, address: Tuple[str, int]) -> None:
//| """Connect a socket to a remote address
//|
//| :param ~tuple address: tuple of (remote_address, remote_port)"""
@@ -183,7 +180,6 @@ STATIC mp_obj_t socketpool_socket_connect(mp_obj_t self_in, mp_obj_t addr_in) {
bool ok = common_hal_socketpool_socket_connect(self, host, hostlen, port);
if (!ok) {
- ESP_EARLY_LOGW(TAG, "socket connect failed");
mp_raise_OSError(0);
}
@@ -261,7 +257,10 @@ STATIC mp_obj_t socketpool_socket_recv_into(size_t n_args, const mp_obj_t *args)
mp_int_t len = bufinfo.len;
if (n_args == 3) {
mp_int_t given_len = mp_obj_get_int(args[2]);
- if (given_len < len) {
+ if (given_len > len) {
+ mp_raise_ValueError(translate("buffer too small for requested bytes"));
+ }
+ if (given_len > 0 && given_len < len) {
len = given_len;
}
}
diff --git a/shared-bindings/socketpool/SocketPool.c b/shared-bindings/socketpool/SocketPool.c
index 0eeebd691..73eeed265 100644
--- a/shared-bindings/socketpool/SocketPool.c
+++ b/shared-bindings/socketpool/SocketPool.c
@@ -37,9 +37,6 @@
#include "shared-bindings/socketpool/Socket.h"
#include "shared-bindings/socketpool/SocketPool.h"
-#include "esp_log.h"
-static const char* TAG = "socketpool binding";
-
//| class SocketPool:
//| """A pool of socket resources available for the given radio. Only one
//| SocketPool can be created for each radio.
@@ -60,8 +57,14 @@ STATIC mp_obj_t socketpool_socketpool_make_new(const mp_obj_type_t *type, size_t
return MP_OBJ_FROM_PTR(s);
}
-
-//| def socket(self, family: int = AF_INET, type: int = SOCK_STREAM, proto: int = IPPROTO_TCP) -> None:
+//| AF_INET: int
+//| AF_INET6: int
+//| SOCK_STREAM: int
+//| SOCK_DGRAM: int
+//| SOCK_RAW: int
+//| IPPROTO_TCP: int
+//|
+//| def socket(self, family: int = AF_INET, type: int = SOCK_STREAM, proto: int = IPPROTO_TCP) -> socketpool.Socket:
//| """Create a new socket
//|
//| :param ~int family: AF_INET or AF_INET6
@@ -69,6 +72,7 @@ STATIC mp_obj_t socketpool_socketpool_make_new(const mp_obj_type_t *type, size_t
//| :param ~int proto: IPPROTO_TCP, IPPROTO_UDP or IPPROTO_RAW (ignored)"""
//| ...
//|
+
STATIC mp_obj_t socketpool_socketpool_socket(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
mp_arg_check_num(n_args, kw_args, 0, 5, false);
@@ -85,7 +89,7 @@ STATIC mp_obj_t socketpool_socketpool_socket(size_t n_args, const mp_obj_t *pos_
}
MP_DEFINE_CONST_FUN_OBJ_KW(socketpool_socketpool_socket_obj, 1, socketpool_socketpool_socket);
-//| def getaddrinfo(host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> tuple:
+//| def getaddrinfo(host: str, port: int, family: int = 0, type: int = 0, proto: int = 0, flags: int = 0) -> Tuple[int, int, int, str, Tuple[str, int]]:
//| """Gets the address information for a hostname and port
//|
//| Returns the appropriate family, socket type, socket protocol and
@@ -122,7 +126,6 @@ STATIC mp_obj_t socketpool_socketpool_getaddrinfo(size_t n_args, const mp_obj_t
}
if (ip_str == mp_const_none) {
- ESP_EARLY_LOGW(TAG, "no ip str");
mp_raise_OSError(0);
}
diff --git a/shared-bindings/ssl/SSLContext.c b/shared-bindings/ssl/SSLContext.c
index d2c236d3b..9d4df7261 100644
--- a/shared-bindings/ssl/SSLContext.c
+++ b/shared-bindings/ssl/SSLContext.c
@@ -51,7 +51,7 @@ STATIC mp_obj_t ssl_sslcontext_make_new(const mp_obj_type_t *type, size_t n_args
return MP_OBJ_FROM_PTR(s);
}
-//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: str = None) -> socketpool.Socket:
+//| def wrap_socket(sock: socketpool.Socket, *, server_side: bool = False, server_hostname: Optional[str] = None) -> socketpool.Socket:
//| """Wraps the socket into a socket-compatible class that handles SSL negotiation.
//| The socket must be of type SOCK_STREAM."""
//| ...
diff --git a/shared-bindings/supervisor/Runtime.c b/shared-bindings/supervisor/Runtime.c
index 84d8370dd..bfca6e7b1 100755
--- a/shared-bindings/supervisor/Runtime.c
+++ b/shared-bindings/supervisor/Runtime.c
@@ -46,13 +46,7 @@
//|
//| serial_connected: bool
-//| """Returns the USB serial communication status (read-only).
-//|
-//| .. note::
-//|
-//| SAMD: Will return ``True`` if the USB serial connection
-//| has been established at any point. Will not reset if
-//| USB is disconnected but power remains (e.g. battery connected)"""
+//| """Returns the USB serial communication status (read-only)."""
//|
STATIC mp_obj_t supervisor_get_serial_connected(mp_obj_t self){
diff --git a/shared-bindings/time/__init__.c b/shared-bindings/time/__init__.c
index 0a78b377c..44f82c62e 100644
--- a/shared-bindings/time/__init__.c
+++ b/shared-bindings/time/__init__.c
@@ -215,7 +215,7 @@ STATIC mp_obj_t time_time(void) {
MP_DEFINE_CONST_FUN_OBJ_0(time_time_obj, time_time);
//| def monotonic_ns() -> int:
-//| """Return the time of the specified clock clk_id in nanoseconds.
+//| """Return the time of the monotonic clock, cannot go backward, in nanoseconds.
//|
//| :return: the current time
//| :rtype: int"""
diff --git a/shared-bindings/wifi/Network.c b/shared-bindings/wifi/Network.c
index b6d5e0901..bf970a9c0 100644
--- a/shared-bindings/wifi/Network.c
+++ b/shared-bindings/wifi/Network.c
@@ -58,6 +58,23 @@ const mp_obj_property_t wifi_network_ssid_obj = {
};
+//| bssid: bytes
+//| """BSSID of the network (usually the AP's MAC address)"""
+//|
+STATIC mp_obj_t wifi_network_get_bssid(mp_obj_t self) {
+ return common_hal_wifi_network_get_bssid(self);
+
+}
+MP_DEFINE_CONST_FUN_OBJ_1(wifi_network_get_bssid_obj, wifi_network_get_bssid);
+
+const mp_obj_property_t wifi_network_bssid_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_network_get_bssid_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+
//| rssi: int
//| """Signal strength of the network"""
//|
@@ -94,6 +111,7 @@ const mp_obj_property_t wifi_network_channel_obj = {
STATIC const mp_rom_map_elem_t wifi_network_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_ssid), MP_ROM_PTR(&wifi_network_ssid_obj) },
+ { MP_ROM_QSTR(MP_QSTR_bssid), MP_ROM_PTR(&wifi_network_bssid_obj) },
{ MP_ROM_QSTR(MP_QSTR_rssi), MP_ROM_PTR(&wifi_network_rssi_obj) },
{ MP_ROM_QSTR(MP_QSTR_channel), MP_ROM_PTR(&wifi_network_channel_obj) },
};
diff --git a/shared-bindings/wifi/Network.h b/shared-bindings/wifi/Network.h
index 6a7f7be4a..c9bbc685e 100644
--- a/shared-bindings/wifi/Network.h
+++ b/shared-bindings/wifi/Network.h
@@ -36,6 +36,7 @@
const mp_obj_type_t wifi_network_type;
extern mp_obj_t common_hal_wifi_network_get_ssid(wifi_network_obj_t *self);
+extern mp_obj_t common_hal_wifi_network_get_bssid(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_rssi(wifi_network_obj_t *self);
extern mp_obj_t common_hal_wifi_network_get_channel(wifi_network_obj_t *self);
diff --git a/shared-bindings/wifi/Radio.c b/shared-bindings/wifi/Radio.c
index 329dcb1b5..e81e8793c 100644
--- a/shared-bindings/wifi/Radio.c
+++ b/shared-bindings/wifi/Radio.c
@@ -24,11 +24,13 @@
* THE SOFTWARE.
*/
+#include "shared-bindings/wifi/__init__.h"
+
+#include <regex.h>
#include <string.h>
-#include "py/objproperty.h"
#include "py/runtime.h"
-#include "shared-bindings/wifi/__init__.h"
+#include "py/objproperty.h"
//| class Radio:
//| """Native wifi radio.
@@ -50,7 +52,6 @@
//|
STATIC mp_obj_t wifi_radio_get_enabled(mp_obj_t self) {
return mp_obj_new_bool(common_hal_wifi_radio_get_enabled(self));
-
}
MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_enabled_obj, wifi_radio_get_enabled);
@@ -78,7 +79,7 @@ const mp_obj_property_t wifi_radio_mac_address_obj = {
};
-//| def start_scanning_networks(self, *, start_channel=1, stop_channel=11) -> Iterable[Network]:
+//| def start_scanning_networks(self, *, start_channel: int = 1, stop_channel: int = 11) -> Iterable[Network]:
//| """Scans for available wifi networks over the given channel range. Make sure the channels are allowed in your country."""
//| ...
//|
@@ -102,17 +103,57 @@ STATIC mp_obj_t wifi_radio_stop_scanning_networks(mp_obj_t self_in) {
}
STATIC MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_stop_scanning_networks_obj, wifi_radio_stop_scanning_networks);
+//| hostname: ReadableBuffer
+//| """Hostname for wifi interface. When the hostname is altered after interface started/connected
+//| the changes would only be reflected once the interface restarts/reconnects."""
+//|
+STATIC mp_obj_t wifi_radio_get_hostname(mp_obj_t self_in) {
+ wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ return common_hal_wifi_radio_get_hostname(self);
+}
+MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_hostname_obj, wifi_radio_get_hostname);
+
+STATIC mp_obj_t wifi_radio_set_hostname(mp_obj_t self_in, mp_obj_t hostname_in) {
+ mp_buffer_info_t hostname;
+ mp_get_buffer_raise(hostname_in, &hostname, MP_BUFFER_READ);
+
+ if (hostname.len < 1 || hostname.len > 253) {
+ mp_raise_ValueError(translate("Hostname must be between 1 and 253 characters"));
+ }
+
+ regex_t regex; //validate hostname according to RFC 1123
+ regcomp(&regex,"^(([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])\\.)*([a-z0-9]|[a-z0-9][a-z0-9\\-]{0,61}[a-z0-9])$", REG_EXTENDED | REG_ICASE | REG_NOSUB);
+ if (regexec(&regex, hostname.buf, 0, NULL, 0)) {
+ mp_raise_ValueError(translate("invalid hostname"));
+ }
+ regfree(&regex);
+
+ wifi_radio_obj_t *self = MP_OBJ_TO_PTR(self_in);
+ common_hal_wifi_radio_set_hostname(self, hostname.buf);
+
+ return mp_const_none;
+}
+MP_DEFINE_CONST_FUN_OBJ_2(wifi_radio_set_hostname_obj, wifi_radio_set_hostname);
+
+const mp_obj_property_t wifi_radio_hostname_obj = {
+ .base.type = &mp_type_property,
+ .proxy = {(mp_obj_t)&wifi_radio_get_hostname_obj,
+ (mp_obj_t)&wifi_radio_set_hostname_obj,
+ (mp_obj_t)&mp_const_none_obj},
+};
+
//| def connect(self, ssid: ReadableBuffer, password: ReadableBuffer = b"", *, channel: Optional[int] = 0, timeout: Optional[float] = None) -> bool:
//| """Connects to the given ssid and waits for an ip address. Reconnections are handled
//| automatically once one connection succeeds."""
//| ...
//|
STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
- enum { ARG_ssid, ARG_password, ARG_channel, ARG_timeout };
+ enum { ARG_ssid, ARG_password, ARG_channel, ARG_bssid, ARG_timeout };
static const mp_arg_t allowed_args[] = {
{ MP_QSTR_ssid, MP_ARG_REQUIRED | MP_ARG_OBJ },
{ MP_QSTR_password, MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_channel, MP_ARG_KW_ONLY | MP_ARG_INT, {.u_int = 0} },
+ { MP_QSTR_bssid, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = MP_OBJ_NULL} },
{ MP_QSTR_timeout, MP_ARG_KW_ONLY | MP_ARG_OBJ, {.u_obj = mp_const_none} },
};
@@ -125,7 +166,6 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
timeout = mp_obj_get_float(args[ARG_timeout].u_obj);
}
-
mp_buffer_info_t ssid;
mp_get_buffer_raise(args[ARG_ssid].u_obj, &ssid, MP_BUFFER_READ);
@@ -138,7 +178,19 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
}
}
- wifi_radio_error_t error = common_hal_wifi_radio_connect(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, timeout);
+ #define MAC_ADDRESS_LENGTH 6
+
+ mp_buffer_info_t bssid;
+ bssid.len = 0;
+ // Should probably make sure bssid is just bytes and not something else too
+ if (args[ARG_bssid].u_obj != MP_OBJ_NULL) {
+ mp_get_buffer_raise(args[ARG_bssid].u_obj, &bssid, MP_BUFFER_READ);
+ if (bssid.len != MAC_ADDRESS_LENGTH) {
+ mp_raise_ValueError(translate("Invalid BSSID"));
+ }
+ }
+
+ wifi_radio_error_t error = common_hal_wifi_radio_connect(self, ssid.buf, ssid.len, password.buf, password.len, args[ARG_channel].u_int, timeout, bssid.buf, bssid.len);
if (error == WIFI_RADIO_ERROR_AUTH) {
mp_raise_ConnectionError(translate("Authentication failure"));
} else if (error == WIFI_RADIO_ERROR_NO_AP_FOUND) {
@@ -151,6 +203,38 @@ STATIC mp_obj_t wifi_radio_connect(size_t n_args, const mp_obj_t *pos_args, mp_m
}
STATIC MP_DEFINE_CONST_FUN_OBJ_KW(wifi_radio_connect_obj, 1, wifi_radio_connect);
+//| ipv4_gateway: Optional[ipaddress.IPv4Address]
+//| """IP v4 Address of the gateway when connected to an access point. None otherwise."""
+//|
+STATIC mp_obj_t wifi_radio_get_ipv4_gateway(mp_obj_t self) {
+ return common_hal_wifi_radio_get_ipv4_gateway(self);
+
+}
+MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_gateway_obj, wifi_radio_get_ipv4_gateway);
+
+const mp_obj_property_t wifi_radio_ipv4_gateway_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ipv4_gateway_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| ipv4_subnet: Optional[ipaddress.IPv4Address]
+//| """IP v4 Address of the subnet when connected to an access point. None otherwise."""
+//|
+STATIC mp_obj_t wifi_radio_get_ipv4_subnet(mp_obj_t self) {
+ return common_hal_wifi_radio_get_ipv4_subnet(self);
+
+}
+MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_subnet_obj, wifi_radio_get_ipv4_subnet);
+
+const mp_obj_property_t wifi_radio_ipv4_subnet_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ipv4_subnet_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
//| ipv4_address: Optional[ipaddress.IPv4Address]
//| """IP v4 Address of the radio when connected to an access point. None otherwise."""
//|
@@ -167,7 +251,39 @@ const mp_obj_property_t wifi_radio_ipv4_address_obj = {
(mp_obj_t)&mp_const_none_obj },
};
-//| def ping(self, ip, *, timeout: float = 0.5) -> float:
+//| ipv4_dns: Optional[ipaddress.IPv4Address]
+//| """IP v4 Address of the DNS server in use when connected to an access point. None otherwise."""
+//|
+STATIC mp_obj_t wifi_radio_get_ipv4_dns(mp_obj_t self) {
+ return common_hal_wifi_radio_get_ipv4_dns(self);
+
+}
+MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ipv4_dns_obj, wifi_radio_get_ipv4_dns);
+
+const mp_obj_property_t wifi_radio_ipv4_dns_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ipv4_dns_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| ap_info: Optional[Network]
+//| """Network object containing BSSID, SSID, channel, and RSSI when connected to an access point. None otherwise."""
+//|
+STATIC mp_obj_t wifi_radio_get_ap_info(mp_obj_t self) {
+ return common_hal_wifi_radio_get_ap_info(self);
+
+}
+MP_DEFINE_CONST_FUN_OBJ_1(wifi_radio_get_ap_info_obj, wifi_radio_get_ap_info);
+
+const mp_obj_property_t wifi_radio_ap_info_obj = {
+ .base.type = &mp_type_property,
+ .proxy = { (mp_obj_t)&wifi_radio_get_ap_info_obj,
+ (mp_obj_t)&mp_const_none_obj,
+ (mp_obj_t)&mp_const_none_obj },
+};
+
+//| def ping(self, ip: ipaddress.IPv4Address, *, timeout: Optional[float] = 0.5) -> float:
//| """Ping an IP to test connectivity. Returns echo time in seconds.
//| Returns None when it times out."""
//| ...
@@ -204,9 +320,15 @@ STATIC const mp_rom_map_elem_t wifi_radio_locals_dict_table[] = {
{ MP_ROM_QSTR(MP_QSTR_start_scanning_networks), MP_ROM_PTR(&wifi_radio_start_scanning_networks_obj) },
{ MP_ROM_QSTR(MP_QSTR_stop_scanning_networks), MP_ROM_PTR(&wifi_radio_stop_scanning_networks_obj) },
+ { MP_ROM_QSTR(MP_QSTR_hostname), MP_ROM_PTR(&wifi_radio_hostname_obj) },
+
{ MP_ROM_QSTR(MP_QSTR_connect), MP_ROM_PTR(&wifi_radio_connect_obj) },
// { MP_ROM_QSTR(MP_QSTR_connect_to_enterprise), MP_ROM_PTR(&wifi_radio_connect_to_enterprise_obj) },
+ { MP_ROM_QSTR(MP_QSTR_ap_info), MP_ROM_PTR(&wifi_radio_ap_info_obj) },
+ { MP_ROM_QSTR(MP_QSTR_ipv4_dns), MP_ROM_PTR(&wifi_radio_ipv4_dns_obj) },
+ { MP_ROM_QSTR(MP_QSTR_ipv4_gateway), MP_ROM_PTR(&wifi_radio_ipv4_gateway_obj) },
+ { MP_ROM_QSTR(MP_QSTR_ipv4_subnet), MP_ROM_PTR(&wifi_radio_ipv4_subnet_obj) },
{ MP_ROM_QSTR(MP_QSTR_ipv4_address), MP_ROM_PTR(&wifi_radio_ipv4_address_obj) },
// { MP_ROM_QSTR(MP_QSTR_access_point_active), MP_ROM_PTR(&wifi_radio_access_point_active_obj) },
diff --git a/shared-bindings/wifi/Radio.h b/shared-bindings/wifi/Radio.h
index 812814f9e..1e8f362e3 100644
--- a/shared-bindings/wifi/Radio.h
+++ b/shared-bindings/wifi/Radio.h
@@ -35,7 +35,6 @@
const mp_obj_type_t wifi_radio_type;
-
typedef enum {
WIFI_RADIO_ERROR_NONE,
WIFI_RADIO_ERROR_UNKNOWN,
@@ -46,13 +45,20 @@ typedef enum {
extern bool common_hal_wifi_radio_get_enabled(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_set_enabled(wifi_radio_obj_t *self, bool enabled);
+extern mp_obj_t common_hal_wifi_radio_get_hostname(wifi_radio_obj_t *self);
+extern void common_hal_wifi_radio_set_hostname(wifi_radio_obj_t *self, const char *hostname);
+
extern mp_obj_t common_hal_wifi_radio_get_mac_address(wifi_radio_obj_t *self);
extern mp_obj_t common_hal_wifi_radio_start_scanning_networks(wifi_radio_obj_t *self);
extern void common_hal_wifi_radio_stop_scanning_networks(wifi_radio_obj_t *self);
-extern wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout);
+extern wifi_radio_error_t common_hal_wifi_radio_connect(wifi_radio_obj_t *self, uint8_t* ssid, size_t ssid_len, uint8_t* password, size_t password_len, uint8_t channel, mp_float_t timeout, uint8_t* bssid, size_t bssid_len);
+extern mp_obj_t common_hal_wifi_radio_get_ap_info(wifi_radio_obj_t *self);
+extern mp_obj_t common_hal_wifi_radio_get_ipv4_dns(wifi_radio_obj_t *self);
+extern mp_obj_t common_hal_wifi_radio_get_ipv4_gateway(wifi_radio_obj_t *self);
+extern mp_obj_t common_hal_wifi_radio_get_ipv4_subnet(wifi_radio_obj_t *self);
extern mp_obj_t common_hal_wifi_radio_get_ipv4_address(wifi_radio_obj_t *self);
extern mp_int_t common_hal_wifi_radio_ping(wifi_radio_obj_t *self, mp_obj_t ip_address, mp_float_t timeout);
diff --git a/shared-bindings/wifi/ScannedNetworks.c b/shared-bindings/wifi/ScannedNetworks.c
index c927d7282..9316faa45 100644
--- a/shared-bindings/wifi/ScannedNetworks.c
+++ b/shared-bindings/wifi/ScannedNetworks.c
@@ -32,9 +32,6 @@
#include "py/runtime.h"
#include "shared-bindings/wifi/ScannedNetworks.h"
-#include "esp_log.h"
-static const char *TAG = "cp iternext";
-
//| class ScannedNetworks:
//| """Iterates over all `wifi.Network` objects found while scanning. This object is always created
//| by a `wifi.Radio`: it has no user-visible constructor."""
@@ -47,7 +44,6 @@ STATIC mp_obj_t scannednetworks_iternext(mp_obj_t self_in) {
return network;
}
- ESP_EARLY_LOGI(TAG, "stop iteration");
return MP_OBJ_STOP_ITERATION;
}