summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorEnrique Casado <ecasado@bhdyn.com>2021-02-05 11:43:43 +0100
committerEnrique Casado <ecasado@bhdyn.com>2021-02-05 11:43:43 +0100
commit53f0bb3601fa5bcbb5e7a93306b80ab2acc78ff2 (patch)
tree94fb209199052595a7e9ebc04db7211f835468ce /shared-module
parentbff40fc1c9f3d7b0738185b17851a3ed67bba2d2 (diff)
parent984f1a1af5b732062439ea41e7718f16309ec0b5 (diff)
Merge remote-tracking branch 'origin/master' into main
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/_eve/__init__.c4
-rw-r--r--shared-module/adafruit_bus_device/I2CDevice.c79
-rw-r--r--shared-module/adafruit_bus_device/I2CDevice.h39
-rw-r--r--shared-module/adafruit_bus_device/SPIDevice.c85
-rw-r--r--shared-module/adafruit_bus_device/SPIDevice.h44
-rw-r--r--shared-module/adafruit_bus_device/__init__.c0
-rw-r--r--shared-module/audiocore/__init__.c60
-rw-r--r--shared-module/audiocore/__init__.h8
-rw-r--r--shared-module/displayio/ColorConverter.c9
-rw-r--r--shared-module/displayio/EPaperDisplay.c23
-rw-r--r--shared-module/displayio/FourWire.c4
-rw-r--r--shared-module/displayio/OnDiskBitmap.c5
-rw-r--r--shared-module/displayio/Palette.c3
-rw-r--r--shared-module/msgpack/__init__.c471
-rw-r--r--shared-module/msgpack/__init__.h34
-rw-r--r--shared-module/random/__init__.c2
-rw-r--r--shared-module/rgbmatrix/RGBMatrix.c25
-rw-r--r--shared-module/sharpdisplay/SharpMemoryFramebuffer.c57
-rw-r--r--shared-module/sharpdisplay/SharpMemoryFramebuffer.h1
-rw-r--r--shared-module/time/__init__.c2
-rw-r--r--shared-module/usb_midi/__init__.c8
21 files changed, 885 insertions, 78 deletions
diff --git a/shared-module/_eve/__init__.c b/shared-module/_eve/__init__.c
index 0f1e12d9f..d95c777dc 100644
--- a/shared-module/_eve/__init__.c
+++ b/shared-module/_eve/__init__.c
@@ -70,8 +70,8 @@ void common_hal__eve_Vertex2f(common_hal__eve_t *eve, mp_float_t x, mp_float_t y
void common_hal__eve_VertexFormat(common_hal__eve_t *eve, uint32_t frac)
{
- C4(eve, ((27 << 24) | ((frac & 7))));
- eve->vscale = 1 << eve->vscale;
+ C4(eve, ((39 << 24) | ((frac & 7))));
+ eve->vscale = 1 << frac;
}
diff --git a/shared-module/adafruit_bus_device/I2CDevice.c b/shared-module/adafruit_bus_device/I2CDevice.c
new file mode 100644
index 000000000..83abe80d6
--- /dev/null
+++ b/shared-module/adafruit_bus_device/I2CDevice.c
@@ -0,0 +1,79 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Mark Komus
+ *
+ * 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/adafruit_bus_device/I2CDevice.h"
+#include "shared-bindings/busio/I2C.h"
+#include "py/mperrno.h"
+#include "py/nlr.h"
+#include "py/runtime.h"
+#include "lib/utils/interrupt_char.h"
+
+void common_hal_adafruit_bus_device_i2cdevice_construct(adafruit_bus_device_i2cdevice_obj_t *self, busio_i2c_obj_t *i2c, uint8_t device_address) {
+ self->i2c = i2c;
+ self->device_address = device_address;
+}
+
+void common_hal_adafruit_bus_device_i2cdevice_lock(adafruit_bus_device_i2cdevice_obj_t *self) {
+ bool success = common_hal_busio_i2c_try_lock(self->i2c);
+
+ while (!success) {
+ RUN_BACKGROUND_TASKS;
+ if (mp_hal_is_interrupted()) {
+ break;
+ }
+
+ success = common_hal_busio_i2c_try_lock(self->i2c);
+ }
+}
+
+void common_hal_adafruit_bus_device_i2cdevice_unlock(adafruit_bus_device_i2cdevice_obj_t *self) {
+ common_hal_busio_i2c_unlock(self->i2c);
+}
+
+uint8_t common_hal_adafruit_bus_device_i2cdevice_readinto(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length) {
+ return common_hal_busio_i2c_read(self->i2c, self->device_address, buffer, length);
+}
+
+uint8_t common_hal_adafruit_bus_device_i2cdevice_write(adafruit_bus_device_i2cdevice_obj_t *self, mp_obj_t buffer, size_t length, bool transmit_stop_bit) {
+ return common_hal_busio_i2c_write(self->i2c, self->device_address, buffer, length, transmit_stop_bit);
+}
+
+void common_hal_adafruit_bus_device_i2cdevice_probe_for_device(adafruit_bus_device_i2cdevice_obj_t *self) {
+ common_hal_adafruit_bus_device_i2cdevice_lock(self);
+
+ mp_buffer_info_t bufinfo;
+ mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
+
+ mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_WRITE);
+
+ uint8_t status = common_hal_adafruit_bus_device_i2cdevice_readinto(self, (uint8_t*)bufinfo.buf, 1);
+ if (status != 0) {
+ common_hal_adafruit_bus_device_i2cdevice_unlock(self);
+ mp_raise_ValueError_varg(translate("No I2C device at address: %x"), self->device_address);
+ }
+
+ common_hal_adafruit_bus_device_i2cdevice_unlock(self);
+}
diff --git a/shared-module/adafruit_bus_device/I2CDevice.h b/shared-module/adafruit_bus_device/I2CDevice.h
new file mode 100644
index 000000000..d06adb9f5
--- /dev/null
+++ b/shared-module/adafruit_bus_device/I2CDevice.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft 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.
+ */
+
+#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_I2CDEVICE_H
+#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_I2CDEVICE_H
+
+#include "py/obj.h"
+#include "common-hal/busio/I2C.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ busio_i2c_obj_t *i2c;
+ uint8_t device_address;
+} adafruit_bus_device_i2cdevice_obj_t;
+
+#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_I2CDEVICE_H
diff --git a/shared-module/adafruit_bus_device/SPIDevice.c b/shared-module/adafruit_bus_device/SPIDevice.c
new file mode 100644
index 000000000..e489fc7c0
--- /dev/null
+++ b/shared-module/adafruit_bus_device/SPIDevice.c
@@ -0,0 +1,85 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2020 Mark Komus
+ *
+ * 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/adafruit_bus_device/SPIDevice.h"
+#include "shared-bindings/busio/SPI.h"
+#include "shared-bindings/digitalio/DigitalInOut.h"
+#include "py/mperrno.h"
+#include "py/nlr.h"
+#include "py/runtime.h"
+
+void common_hal_adafruit_bus_device_spidevice_construct(adafruit_bus_device_spidevice_obj_t *self, busio_spi_obj_t *spi, digitalio_digitalinout_obj_t *cs,
+ uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t extra_clocks) {
+ self->spi = spi;
+ self->baudrate = baudrate;
+ self->polarity = polarity;
+ self->phase = phase;
+ self->extra_clocks = extra_clocks;
+ self->chip_select = cs;
+}
+
+void common_hal_adafruit_bus_device_spidevice_enter(adafruit_bus_device_spidevice_obj_t *self) {
+ bool success = false;
+ while (!success) {
+ success = common_hal_busio_spi_try_lock(self->spi);
+ RUN_BACKGROUND_TASKS;
+ mp_handle_pending();
+ }
+
+ common_hal_busio_spi_configure(self->spi, self->baudrate, self->polarity, self->phase, 8);
+
+ if (self->chip_select != MP_OBJ_NULL) {
+ common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), false);
+ }
+}
+
+void common_hal_adafruit_bus_device_spidevice_exit(adafruit_bus_device_spidevice_obj_t *self) {
+ if (self->chip_select != MP_OBJ_NULL) {
+ common_hal_digitalio_digitalinout_set_value(MP_OBJ_TO_PTR(self->chip_select), true);
+ }
+
+ if (self->extra_clocks > 0) {
+
+ mp_buffer_info_t bufinfo;
+ mp_obj_t buffer = mp_obj_new_bytearray_of_zeros(1);
+
+ mp_get_buffer_raise(buffer, &bufinfo, MP_BUFFER_READ);
+ ((uint8_t*)bufinfo.buf)[0] = 0xFF;
+
+ uint8_t clocks = self->extra_clocks / 8;
+ if ((self->extra_clocks % 8) != 0)
+ clocks += 1;
+
+ while (clocks > 0) {
+ if (!common_hal_busio_spi_write(self->spi, ((uint8_t*)bufinfo.buf), 1)) {
+ mp_raise_OSError(MP_EIO);
+ }
+ clocks--;
+ }
+ }
+
+ common_hal_busio_spi_unlock(self->spi);
+}
diff --git a/shared-module/adafruit_bus_device/SPIDevice.h b/shared-module/adafruit_bus_device/SPIDevice.h
new file mode 100644
index 000000000..1a7c70fa7
--- /dev/null
+++ b/shared-module/adafruit_bus_device/SPIDevice.h
@@ -0,0 +1,44 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft 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.
+ */
+
+#ifndef MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H
+#define MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H
+
+#include "py/obj.h"
+#include "common-hal/busio/SPI.h"
+#include "common-hal/digitalio/DigitalInOut.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ busio_spi_obj_t *spi;
+ uint32_t baudrate;
+ uint8_t polarity;
+ uint8_t phase;
+ uint8_t extra_clocks;
+ digitalio_digitalinout_obj_t *chip_select;
+} adafruit_bus_device_spidevice_obj_t;
+
+#endif // MICROPY_INCLUDED_ATMEL_SAMD_SHARED_MODULE_BUSDEVICE_SPIDEVICE_H
diff --git a/shared-module/adafruit_bus_device/__init__.c b/shared-module/adafruit_bus_device/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/adafruit_bus_device/__init__.c
diff --git a/shared-module/audiocore/__init__.c b/shared-module/audiocore/__init__.c
index ac9d41b60..7bdfe460c 100644
--- a/shared-module/audiocore/__init__.c
+++ b/shared-module/audiocore/__init__.c
@@ -70,3 +70,63 @@ void audiosample_get_buffer_structure(mp_obj_t sample_obj, bool single_channel,
proto->get_buffer_structure(MP_OBJ_TO_PTR(sample_obj), single_channel, single_buffer,
samples_signed, max_buffer_length, spacing);
}
+
+void audiosample_convert_u8m_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = (*buffer_in++ - 0x80) << 8;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_u8s_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes) {
+ size_t nsamples = 2*nframes;
+ for(;nsamples--;) {
+ int16_t sample = (*buffer_in++ - 0x80) << 8;
+ *buffer_out++ = sample;
+ }
+}
+
+void audiosample_convert_s8m_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = (*buffer_in++) << 8;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_s8s_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes) {
+ size_t nsamples = 2*nframes;
+ for(;nsamples--;) {
+ int16_t sample = (*buffer_in++) << 8;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_u16m_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = *buffer_in++ - 0x8000;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
+
+
+void audiosample_convert_u16s_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes) {
+ size_t nsamples = 2*nframes;
+ for(;nsamples--;) {
+ int16_t sample = *buffer_in++ - 0x8000;
+ *buffer_out++ = sample;
+ }
+}
+
+void audiosample_convert_s16m_s16s(int16_t *buffer_out, const int16_t *buffer_in, size_t nframes) {
+ for(;nframes--;) {
+ int16_t sample = *buffer_in++;
+ *buffer_out++ = sample;
+ *buffer_out++ = sample;
+ }
+}
diff --git a/shared-module/audiocore/__init__.h b/shared-module/audiocore/__init__.h
index 7a56454b8..ec2cf1cfa 100644
--- a/shared-module/audiocore/__init__.h
+++ b/shared-module/audiocore/__init__.h
@@ -74,4 +74,12 @@ void audiosample_get_buffer_structure(mp_obj_t sample_obj, bool single_channel,
bool* single_buffer, bool* samples_signed,
uint32_t* max_buffer_length, uint8_t* spacing);
+void audiosample_convert_u8m_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
+void audiosample_convert_u8s_s16s(int16_t *buffer_out, const uint8_t *buffer_in, size_t nframes);
+void audiosample_convert_s8m_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes);
+void audiosample_convert_s8s_s16s(int16_t *buffer_out, const int8_t *buffer_in, size_t nframes);
+void audiosample_convert_u16m_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes);
+void audiosample_convert_u16s_s16s(int16_t *buffer_out, const uint16_t *buffer_in, size_t nframes);
+void audiosample_convert_s16m_s16s(int16_t *buffer_out, const int16_t *buffer_in, size_t nframes);
+
#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOCORE__INIT__H
diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c
index 80558d037..40dcd9d16 100644
--- a/shared-module/displayio/ColorConverter.c
+++ b/shared-module/displayio/ColorConverter.c
@@ -29,6 +29,8 @@
#include "py/misc.h"
#include "py/runtime.h"
+#define NO_TRANSPARENT_COLOR (0x1000000)
+
uint32_t displayio_colorconverter_dither_noise_1 (uint32_t n)
{
n = (n >> 13) ^ n;
@@ -42,6 +44,7 @@ uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y) {
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self, bool dither) {
self->dither = dither;
+ self->transparent_color = NO_TRANSPARENT_COLOR;
}
uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888) {
@@ -130,7 +133,7 @@ bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t*
}
void common_hal_displayio_colorconverter_make_transparent(displayio_colorconverter_t* self, uint32_t transparent_color) {
- if (self->transparent_color >= 0x1000000) {
+ if (self->transparent_color != NO_TRANSPARENT_COLOR) {
mp_raise_RuntimeError(translate("Only one color can be transparent at a time"));
}
self->transparent_color = transparent_color;
@@ -138,8 +141,8 @@ void common_hal_displayio_colorconverter_make_transparent(displayio_colorconvert
void common_hal_displayio_colorconverter_make_opaque(displayio_colorconverter_t* self, uint32_t transparent_color) {
(void) transparent_color;
- // 0x1000000 will never equal a valid color
- self->transparent_color = 0x1000000;
+ // NO_TRANSPARENT_COLOR will never equal a valid color
+ self->transparent_color = NO_TRANSPARENT_COLOR;
}
void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) {
diff --git a/shared-module/displayio/EPaperDisplay.c b/shared-module/displayio/EPaperDisplay.c
index 1b285b4b1..3fb37ff21 100644
--- a/shared-module/displayio/EPaperDisplay.c
+++ b/shared-module/displayio/EPaperDisplay.c
@@ -198,6 +198,29 @@ mp_obj_t common_hal_displayio_epaperdisplay_get_bus(displayio_epaperdisplay_obj_
return self->core.bus;
}
+void common_hal_displayio_epaperdisplay_set_rotation(displayio_epaperdisplay_obj_t* self, int rotation){
+ bool transposed = (self->core.rotation == 90 || self->core.rotation == 270);
+ bool will_transposed = (rotation == 90 || rotation == 270);
+ if(transposed != will_transposed) {
+ int tmp = self->core.width;
+ self->core.width = self->core.height;
+ self->core.height = tmp;
+ }
+ displayio_display_core_set_rotation(&self->core, rotation);
+ if (self == &displays[0].epaper_display) {
+ supervisor_stop_terminal();
+ supervisor_start_terminal(self->core.width, self->core.height);
+ }
+ if (self->core.current_group != NULL) {
+ displayio_group_update_transform(self->core.current_group, &self->core.transform);
+ }
+}
+
+uint16_t common_hal_displayio_epaperdisplay_get_rotation(displayio_epaperdisplay_obj_t* self){
+ return self->core.rotation;
+}
+
+
bool displayio_epaperdisplay_refresh_area(displayio_epaperdisplay_obj_t* self, const displayio_area_t* area) {
uint16_t buffer_size = 128; // In uint32_ts
diff --git a/shared-module/displayio/FourWire.c b/shared-module/displayio/FourWire.c
index 06f8a84e3..e993f2480 100644
--- a/shared-module/displayio/FourWire.c
+++ b/shared-module/displayio/FourWire.c
@@ -76,9 +76,7 @@ void common_hal_displayio_fourwire_deinit(displayio_fourwire_obj_t* self) {
common_hal_reset_pin(self->command.pin);
common_hal_reset_pin(self->chip_select.pin);
- if (self->reset.pin) {
- common_hal_reset_pin(self->reset.pin);
- }
+ common_hal_reset_pin(self->reset.pin);
}
bool common_hal_displayio_fourwire_reset(mp_obj_t obj) {
diff --git a/shared-module/displayio/OnDiskBitmap.c b/shared-module/displayio/OnDiskBitmap.c
index 993fc03de..c1a0ddeb7 100644
--- a/shared-module/displayio/OnDiskBitmap.c
+++ b/shared-module/displayio/OnDiskBitmap.c
@@ -57,7 +57,7 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
uint32_t compression = read_word(bmp_header, 15);
uint32_t number_of_colors = read_word(bmp_header, 23);
- bool indexed = ((bits_per_pixel <= 8) && (number_of_colors != 0));
+ bool indexed = bits_per_pixel <= 8;
self->bitfield_compressed = (compression == 3);
self->bits_per_pixel = bits_per_pixel;
self->width = read_word(bmp_header, 9);
@@ -75,6 +75,9 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self,
self->b_bitmask = 0x1f;
}
} else if (indexed && self->bits_per_pixel != 1) {
+ if (number_of_colors == 0) {
+ number_of_colors = 1 << bits_per_pixel;
+ }
uint16_t palette_size = number_of_colors * sizeof(uint32_t);
uint16_t palette_offset = 0xe + header_size;
diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c
index facb1fa73..ba5ff665c 100644
--- a/shared-module/displayio/Palette.c
+++ b/shared-module/displayio/Palette.c
@@ -83,7 +83,8 @@ bool displayio_palette_get_color(displayio_palette_t *self, const _displayio_col
uint8_t pixel_hue = self->colors[palette_index].hue;
displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, luma, color);
} else if (colorspace->grayscale) {
- *color = self->colors[palette_index].luma >> (8 - colorspace->depth);
+ size_t bitmask = (1 << colorspace->depth) - 1;
+ *color = (self->colors[palette_index].luma >> colorspace->grayscale_bit) & bitmask;
} else {
uint16_t packed = self->colors[palette_index].rgb565;
if (colorspace->reverse_bytes_in_word) {
diff --git a/shared-module/msgpack/__init__.c b/shared-module/msgpack/__init__.c
new file mode 100644
index 000000000..103003174
--- /dev/null
+++ b/shared-module/msgpack/__init__.c
@@ -0,0 +1,471 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Bernhard Boser
+ *
+ * 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 <stdio.h>
+#include <inttypes.h>
+
+#include "py/obj.h"
+#include "py/binary.h"
+#include "py/objarray.h"
+#include "py/objlist.h"
+#include "py/objstringio.h"
+#include "py/parsenum.h"
+#include "py/runtime.h"
+#include "py/stream.h"
+
+#include "supervisor/shared/translate.h"
+#include "shared-bindings/msgpack/ExtType.h"
+
+////////////////////////////////////////////////////////////////
+// stream management
+
+typedef struct _msgpack_stream_t {
+ mp_obj_t stream_obj;
+ mp_uint_t (*read)(mp_obj_t obj, void *buf, mp_uint_t size, int *errcode);
+ mp_uint_t (*write)(mp_obj_t obj, const void *buf, mp_uint_t size, int *errcode);
+ int errcode;
+} msgpack_stream_t;
+
+STATIC msgpack_stream_t get_stream(mp_obj_t stream_obj, int flags) {
+ const mp_stream_p_t *stream_p = mp_get_stream_raise(stream_obj, flags);
+ msgpack_stream_t s = {stream_obj, stream_p->read, stream_p->write, 0};
+ return s;
+}
+
+////////////////////////////////////////////////////////////////
+// readers
+
+STATIC void read(msgpack_stream_t *s, void *buf, mp_uint_t size) {
+ if (size == 0) return;
+ mp_uint_t ret = s->read(s->stream_obj, buf, size, &s->errcode);
+ if (s->errcode != 0) {
+ mp_raise_OSError(s->errcode);
+ }
+ if (ret == 0) {
+ mp_raise_msg(&mp_type_EOFError, NULL);
+ }
+ if (ret < size) {
+ mp_raise_ValueError(translate("short read"));
+ }
+}
+
+STATIC uint8_t read1(msgpack_stream_t *s) {
+ uint8_t res = 0;
+ read(s, &res, 1);
+ return res;
+}
+
+STATIC uint16_t read2(msgpack_stream_t *s) {
+ uint16_t res = 0;
+ read(s, &res, 2);
+ int n = 1;
+ if (*(char *)&n == 1) res = __builtin_bswap16(res);
+ return res;
+}
+
+STATIC uint32_t read4(msgpack_stream_t *s) {
+ uint32_t res = 0;
+ read(s, &res, 4);
+ int n = 1;
+ if (*(char *)&n == 1) res = __builtin_bswap32(res);
+ return res;
+}
+
+STATIC size_t read_size(msgpack_stream_t *s, uint8_t len_index) {
+ size_t res = 0;
+ switch (len_index) {
+ case 0: res = (size_t)read1(s); break;
+ case 1: res = (size_t)read2(s); break;
+ case 2: res = (size_t)read4(s); break;
+ }
+ return res;
+}
+
+////////////////////////////////////////////////////////////////
+// writers
+
+STATIC void write(msgpack_stream_t *s, const void *buf, mp_uint_t size) {
+ mp_uint_t ret = s->write(s->stream_obj, buf, size, &s->errcode);
+ if (s->errcode != 0) {
+ mp_raise_OSError(s->errcode);
+ }
+ if (ret == 0) {
+ mp_raise_msg(&mp_type_EOFError, NULL);
+ }
+}
+
+STATIC void write1(msgpack_stream_t *s, uint8_t obj) {
+ write(s, &obj, 1);
+}
+
+STATIC void write2(msgpack_stream_t *s, uint16_t obj) {
+ int n = 1;
+ if (*(char *)&n == 1) obj = __builtin_bswap16(obj);
+ write(s, &obj, 2);
+}
+
+STATIC void write4(msgpack_stream_t *s, uint32_t obj) {
+ int n = 1;
+ if (*(char *)&n == 1) obj = __builtin_bswap32(obj);
+ write(s, &obj, 4);
+}
+
+// compute and write msgpack size code (array structures)
+STATIC void write_size(msgpack_stream_t *s, uint8_t code, size_t size) {
+ if ((uint8_t)size == size) {
+ write1(s, code);
+ write1(s, size);
+ } else if ((uint16_t)size == size) {
+ write1(s, code+1);
+ write2(s, size);
+ } else {
+ write1(s, code+2);
+ write4(s, size);
+ }
+}
+
+////////////////////////////////////////////////////////////////
+// packers
+
+// This is a helper function to iterate through a dictionary. The state of
+// the iteration is held in *cur and should be initialised with zero for the
+// first call. Will return NULL when no more elements are available.
+STATIC mp_map_elem_t *dict_iter_next(mp_obj_dict_t *dict, size_t *cur) {
+ size_t max = dict->map.alloc;
+ mp_map_t *map = &dict->map;
+
+ for (size_t i = *cur; i < max; i++) {
+ if (MP_MAP_SLOT_IS_FILLED(map, i)) {
+ *cur = i + 1;
+ return &(map->table[i]);
+ }
+ }
+
+ return NULL;
+}
+
+STATIC void pack_int(msgpack_stream_t *s, int32_t x) {
+ if (x > -32 && x < 128) {
+ write1(s, x);
+ } else if ((int8_t)x == x) {
+ write1(s, 0xd0);
+ write1(s, x);
+ } else if ((int16_t)x == x) {
+ write1(s, 0xd1);
+ write2(s, x);
+ } else {
+ write1(s, 0xd2);
+ write4(s, x);
+ }
+}
+
+STATIC void pack_bin(msgpack_stream_t *s, const uint8_t* data, size_t len) {
+ write_size(s, 0xc4, len);
+ if (len > 0) write(s, data, len);
+}
+
+STATIC void pack_ext(msgpack_stream_t *s, int8_t code, const uint8_t* data, size_t len) {
+ if (len == 1) {
+ write1(s, 0xd4);
+ } else if (len == 2) {
+ write1(s, 0xd5);
+ } else if (len == 4) {
+ write1(s, 0xd6);
+ } else if (len == 8) {
+ write1(s, 0xd7);
+ } else if (len == 16) {
+ write1(s, 0xd8);
+ } else {
+ write_size(s, 0xc7, len);
+ }
+ write1(s, code); // type byte
+ if (len > 0) write(s, data, len);
+}
+
+STATIC void pack_str(msgpack_stream_t *s, const char* str, size_t len) {
+ if (len < 32) {
+ write1(s, 0b10100000 | (uint8_t)len);
+ } else {
+ write_size(s, 0xd9, len);
+ }
+ if (len > 0) write(s, str, len);
+}
+
+STATIC void pack_array(msgpack_stream_t *s, size_t len) {
+ // only writes the header, manually write the objects after calling pack_array!
+ if (len < 16) {
+ write1(s, 0b10010000 | (uint8_t)len);
+ } else if (len < 0x10000) {
+ write1(s, 0xdc);
+ write2(s, len);
+ } else {
+ write1(s, 0xdd);
+ write4(s, len);
+ }
+}
+
+STATIC void pack_dict(msgpack_stream_t *s, size_t len) {
+ // only writes the header, manually write the objects after calling pack_array!
+ if (len < 16) {
+ write1(s, 0b10000000 | (uint8_t)len);
+ } else if (len < 0x10000) {
+ write1(s, 0xde);
+ write2(s, len);
+ } else {
+ write1(s, 0xdf);
+ write4(s, len);
+ }
+}
+
+STATIC void pack(mp_obj_t obj, msgpack_stream_t *s, mp_obj_t default_handler) {
+ if (MP_OBJ_IS_SMALL_INT(obj)) {
+ // int
+ int32_t x = MP_OBJ_SMALL_INT_VALUE(obj);
+ pack_int(s, x);
+ } else if (MP_OBJ_IS_STR(obj)) {
+ // string
+ size_t len;
+ const char *data = mp_obj_str_get_data(obj, &len);
+ pack_str(s, data, len);
+ } else if (MP_OBJ_IS_TYPE(obj, &mod_msgpack_exttype_type)) {
+ mod_msgpack_extype_obj_t *ext = MP_OBJ_TO_PTR(obj);
+ mp_buffer_info_t bufinfo;
+ mp_get_buffer_raise(ext->data, &bufinfo, MP_BUFFER_READ);
+ pack_ext(s, ext->code, bufinfo.buf, bufinfo.len);
+ } else if (MP_OBJ_IS_TYPE(obj, &mp_type_tuple)) {
+ // tuple
+ mp_obj_tuple_t *self = MP_OBJ_TO_PTR(obj);
+ pack_array(s, self->len);
+ for (size_t i=0; i<self->len; i++) {
+ pack(self->items[i], s, default_handler);
+ }
+ } else if (MP_OBJ_IS_TYPE(obj, &mp_type_list)) {
+ // list (layout differs from tuple)
+ mp_obj_list_t *self = MP_OBJ_TO_PTR(obj);
+ pack_array(s, self->len);
+ for (size_t i=0; i<self->len; i++) {
+ pack(self->items[i], s, default_handler);
+ }
+ } else if (MP_OBJ_IS_TYPE(obj, &mp_type_dict)) {
+ // dict
+ mp_obj_dict_t *self = MP_OBJ_TO_PTR(obj);
+ pack_dict(s, self->map.used);
+ size_t cur = 0;
+ mp_map_elem_t *next = NULL;
+ while ((next = dict_iter_next(self, &cur)) != NULL) {
+ pack(next->key, s, default_handler);
+ pack(next->value, s, default_handler);
+ }
+ } else if (mp_obj_is_float(obj)) {
+ union Float { mp_float_t f; uint32_t u; };
+ union Float data;
+ data.f = mp_obj_float_get(obj);
+ write1(s, 0xca);
+ write4(s, data.u);
+ } else if (obj == mp_const_none) {
+ write1(s, 0xc0);
+ } else if (obj == mp_const_false) {
+ write1(s, 0xc2);
+ } else if (obj == mp_const_true) {
+ write1(s, 0xc3);
+ } else {
+ mp_buffer_info_t bufinfo;
+ if (mp_get_buffer(obj, &bufinfo, MP_BUFFER_READ)) {
+ // bytes (bin type)
+ pack_bin(s, bufinfo.buf, bufinfo.len);
+ } else if (default_handler != mp_const_none) {
+ // set default_handler to mp_const_none to avoid infinite recursion
+ // this also precludes some valid outputs
+ pack(mp_call_function_1(default_handler, obj), s, mp_const_none);
+ } else {
+ mp_raise_ValueError(translate("no default packer"));
+ }
+ }
+}
+
+////////////////////////////////////////////////////////////////
+// unpacker
+
+STATIC mp_obj_t unpack(msgpack_stream_t *s, mp_obj_t ext_hook, bool use_list);
+
+STATIC mp_obj_t unpack_array_elements(msgpack_stream_t *s, size_t size, mp_obj_t ext_hook, bool use_list) {
+ if (use_list) {
+ mp_obj_list_t *t = MP_OBJ_TO_PTR(mp_obj_new_list(size, NULL));
+ for (size_t i=0; i<size; i++) {
+ t->items[i] = unpack(s, ext_hook, use_list);
+ }
+ return MP_OBJ_FROM_PTR(t);
+ } else {
+ mp_obj_tuple_t *t = MP_OBJ_TO_PTR(mp_obj_new_tuple(size, NULL));
+ for (size_t i=0; i<size; i++) {
+ t->items[i] = unpack(s, ext_hook, use_list);
+ }
+ return MP_OBJ_FROM_PTR(t);
+ }
+}
+
+STATIC mp_obj_t unpack_bytes(msgpack_stream_t *s, size_t size) {
+ vstr_t vstr;
+ vstr_init_len(&vstr, size);
+ byte *p = (byte*)vstr.buf;
+ // read in chunks: (some drivers - e.g. UART) limit the
+ // maximum number of bytes that can be read at once
+ // read(s, p, size);
+ while (size > 0) {
+ int n = size > 256 ? 256 : size;
+ read(s, p, n);
+ size -= n;
+ p += n;
+ }
+ return mp_obj_new_str_from_vstr(&mp_type_bytes, &vstr);
+}
+
+STATIC mp_obj_t unpack_ext(msgpack_stream_t *s, size_t size, mp_obj_t ext_hook) {
+ int8_t code = read1(s);
+ mp_obj_t data = unpack_bytes(s, size);
+ if (ext_hook != mp_const_none) {
+ return mp_call_function_2(ext_hook, MP_OBJ_NEW_SMALL_INT(code), data);
+ } else {
+ mod_msgpack_extype_obj_t *o = m_new_obj(mod_msgpack_extype_obj_t);
+ o->base.type = &mod_msgpack_exttype_type;
+ o->code = code;
+ o->data = data;
+ return MP_OBJ_FROM_PTR(o);
+ }
+}
+
+STATIC mp_obj_t unpack(msgpack_stream_t *s, mp_obj_t ext_hook, bool use_list) {
+ uint8_t code = read1(s);
+ if (((code & 0b10000000) == 0) || ((code & 0b11100000) == 0b11100000)) {
+ // int
+ return MP_OBJ_NEW_SMALL_INT((int8_t)code);
+ }
+ if ((code & 0b11100000) == 0b10100000) {
+ // str
+ size_t len = code & 0b11111;
+ // allocate on stack; len < 32
+ char str[len];
+ read(s, &str, len);
+ return mp_obj_new_str(str, len);
+ }
+ if ((code & 0b11110000) == 0b10010000) {
+ // array (list / tuple)
+ return unpack_array_elements(s, code & 0b1111, ext_hook, use_list);
+ }
+ if ((code & 0b11110000) == 0b10000000) {
+ // map (dict)
+ size_t len = code & 0b1111;
+ mp_obj_dict_t *d = MP_OBJ_TO_PTR(mp_obj_new_dict(len));
+ for (size_t i=0; i<len; i++) {
+ mp_obj_dict_store(d, unpack(s, ext_hook, use_list), unpack(s, ext_hook, use_list));
+ }
+ return MP_OBJ_FROM_PTR(d);
+ }
+ switch (code) {
+ case 0xc0: return mp_const_none;
+ case 0xc2: return mp_const_false;
+ case 0xc3: return mp_const_true;
+ case 0xc4:
+ case 0xc5:
+ case 0xc6: {
+ // bin 8, 16, 32
+ return unpack_bytes(s, read_size(s, code-0xc4));
+ }
+ case 0xcc: // uint8
+ case 0xd0: // int8
+ return MP_OBJ_NEW_SMALL_INT((int8_t)read1(s));
+ case 0xcd: // uint16
+ case 0xd1: // int16
+ return MP_OBJ_NEW_SMALL_INT((int16_t)read2(s));
+ case 0xce: // uint32
+ case 0xd2: // int32
+ return MP_OBJ_NEW_SMALL_INT((int32_t)read4(s));
+ case 0xca: {
+ union Float { mp_float_t f; uint32_t u; };
+ union Float data;
+ data.u = read4(s);
+ return mp_obj_new_float(data.f);
+ }
+ case 0xd9:
+ case 0xda:
+ case 0xdb: {
+ // str 8, 16, 32
+ size_t size = read_size(s, code-0xd9);
+ vstr_t vstr;
+ vstr_init_len(&vstr, size);
+ byte *p = (byte*)vstr.buf;
+ read(s, p, size);
+ return mp_obj_new_str_from_vstr(&mp_type_str, &vstr);
+ }
+ case 0xde:
+ case 0xdf: {
+ // map 16 & 32
+ size_t len = read_size(s, code - 0xde + 1);
+ mp_obj_dict_t *d = MP_OBJ_TO_PTR(mp_obj_new_dict(len));
+ for (size_t i=0; i<len; i++) {
+ mp_obj_dict_store(d, unpack(s, ext_hook, use_list), unpack(s, ext_hook, use_list));
+ }
+ return MP_OBJ_FROM_PTR(d);
+ }
+ case 0xdc:
+ case 0xdd: {
+ // array 16 & 32
+ size_t size = read_size(s, code - 0xdc + 1);
+ return unpack_array_elements(s, size, ext_hook, use_list);
+ }
+ case 0xd4: // fixenxt 1
+ return unpack_ext(s, 1, ext_hook);
+ case 0xd5: // fixenxt 2
+ return unpack_ext(s, 2, ext_hook);
+ case 0xd6: // fixenxt 4
+ return unpack_ext(s, 4, ext_hook);
+ case 0xd7: // fixenxt 8
+ return unpack_ext(s, 8, ext_hook);
+ case 0xd8: // fixenxt 16
+ return unpack_ext(s, 16, ext_hook);
+ case 0xc7: // ext 8
+ case 0xc8: // ext 16
+ case 0xc9:
+ // ext 8, 16, 32
+ return unpack_ext(s, read_size(s, code-0xc7), ext_hook);
+ case 0xc1: // never used
+ case 0xcb: // float 64
+ case 0xcf: // uint 64
+ case 0xd3: // int 64
+ default:
+ mp_raise_NotImplementedError(translate("64 bit types"));
+ }
+}
+
+void common_hal_msgpack_pack(mp_obj_t obj, mp_obj_t stream_obj, mp_obj_t default_handler) {
+ msgpack_stream_t stream = get_stream(stream_obj, MP_STREAM_OP_WRITE);
+ pack(obj, &stream, default_handler);
+}
+
+mp_obj_t common_hal_msgpack_unpack(mp_obj_t stream_obj, mp_obj_t ext_hook, bool use_list) {
+ msgpack_stream_t stream = get_stream(stream_obj, MP_STREAM_OP_WRITE);
+ return unpack(&stream, ext_hook, use_list);
+}
diff --git a/shared-module/msgpack/__init__.h b/shared-module/msgpack/__init__.h
new file mode 100644
index 000000000..88b4809f9
--- /dev/null
+++ b/shared-module/msgpack/__init__.h
@@ -0,0 +1,34 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2017 Scott Shawcroft 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.
+ */
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_MSGPACK___INIT___H
+#define MICROPY_INCLUDED_SHARED_MODULE_MSGPACK___INIT___H
+
+#include "py/stream.h"
+
+void common_hal_msgpack_pack(mp_obj_t obj, mp_obj_t stream_obj, mp_obj_t default_handler);
+mp_obj_t common_hal_msgpack_unpack(mp_obj_t stream_obj, mp_obj_t ext_hook, bool use_list);
+
+#endif
diff --git a/shared-module/random/__init__.c b/shared-module/random/__init__.c
index 5cc20e5b5..42499bc0a 100644
--- a/shared-module/random/__init__.c
+++ b/shared-module/random/__init__.c
@@ -44,7 +44,7 @@ STATIC uint32_t yasmarang(void)
{
if (yasmarang_pad == 0xeda4baba) {
if (!common_hal_os_urandom((uint8_t *)&yasmarang_pad, sizeof(uint32_t))) {
- yasmarang_pad = common_hal_time_monotonic() & 0xffffffff;
+ yasmarang_pad = common_hal_time_monotonic_ms() & 0xffffffff;
}
}
yasmarang_pad += yasmarang_dat + yasmarang_d * yasmarang_n;
diff --git a/shared-module/rgbmatrix/RGBMatrix.c b/shared-module/rgbmatrix/RGBMatrix.c
index 94c3eda27..a09767b62 100644
--- a/shared-module/rgbmatrix/RGBMatrix.c
+++ b/shared-module/rgbmatrix/RGBMatrix.c
@@ -78,10 +78,10 @@ void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t* self,
// verify that the matrix is big enough
mp_get_index(mp_obj_get_type(self->framebuffer), self->bufinfo.len, MP_OBJ_NEW_SMALL_INT(self->bufsize-1), false);
} else {
- _PM_free(self->bufinfo.buf);
- _PM_free(self->protomatter.rgbPins);
- _PM_free(self->protomatter.addr);
- _PM_free(self->protomatter.screenData);
+ common_hal_rgbmatrix_free_impl(self->bufinfo.buf);
+ common_hal_rgbmatrix_free_impl(self->protomatter.rgbPins);
+ common_hal_rgbmatrix_free_impl(self->protomatter.addr);
+ common_hal_rgbmatrix_free_impl(self->protomatter.screenData);
self->framebuffer = NULL;
self->bufinfo.buf = common_hal_rgbmatrix_allocator_impl(self->bufsize);
@@ -180,9 +180,6 @@ void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
void rgbmatrix_rgbmatrix_collect_ptrs(rgbmatrix_rgbmatrix_obj_t* self) {
gc_collect_ptr(self->framebuffer);
- gc_collect_ptr(self->protomatter.rgbPins);
- gc_collect_ptr(self->protomatter.addr);
- gc_collect_ptr(self->protomatter.screenData);
}
void common_hal_rgbmatrix_rgbmatrix_set_paused(rgbmatrix_rgbmatrix_obj_t* self, bool paused) {
@@ -217,18 +214,10 @@ int common_hal_rgbmatrix_rgbmatrix_get_height(rgbmatrix_rgbmatrix_obj_t* self) {
}
void *common_hal_rgbmatrix_allocator_impl(size_t sz) {
- if (gc_alloc_possible()) {
- return m_malloc_maybe(sz + sizeof(void*), true);
- } else {
- supervisor_allocation *allocation = allocate_memory(align32_size(sz), false);
- return allocation ? allocation->ptr : NULL;
- }
+ supervisor_allocation *allocation = allocate_memory(align32_size(sz), false, true);
+ return allocation ? allocation->ptr : NULL;
}
void common_hal_rgbmatrix_free_impl(void *ptr_in) {
- supervisor_allocation *allocation = allocation_from_ptr(ptr_in);
-
- if (allocation) {
- free_memory(allocation);
- }
+ free_memory(allocation_from_ptr(ptr_in));
}
diff --git a/shared-module/sharpdisplay/SharpMemoryFramebuffer.c b/shared-module/sharpdisplay/SharpMemoryFramebuffer.c
index b199e98d6..4b92bd637 100644
--- a/shared-module/sharpdisplay/SharpMemoryFramebuffer.c
+++ b/shared-module/sharpdisplay/SharpMemoryFramebuffer.c
@@ -34,32 +34,10 @@
#include "shared-module/sharpdisplay/SharpMemoryFramebuffer.h"
#include "supervisor/memory.h"
-#include "supervisor/shared/safe_mode.h"
#define SHARPMEM_BIT_WRITECMD_LSB (0x80)
#define SHARPMEM_BIT_VCOM_LSB (0x40)
-static void *hybrid_alloc(size_t sz) {
- supervisor_allocation *allocation = allocate_memory(align32_size(sz), false);
- if (allocation) {
- memset(allocation->ptr, 0, sz);
- return allocation->ptr;
- }
- if (gc_alloc_possible()) {
- return m_malloc(sz, true);
- }
- reset_into_safe_mode(MEM_MANAGE);
- return NULL; // unreached
-}
-
-static inline void hybrid_free(void *ptr_in) {
- supervisor_allocation *allocation = allocation_from_ptr(ptr_in);
-
- if (allocation) {
- free_memory(allocation);
- }
-}
-
STATIC uint8_t bitrev(uint8_t n) {
uint8_t r = 0;
for(int i=0;i<8;i++) r |= ((n>>i) & 1)<<(7-i);
@@ -102,9 +80,9 @@ void common_hal_sharpdisplay_framebuffer_reset(sharpdisplay_framebuffer_obj_t *s
}
void common_hal_sharpdisplay_framebuffer_reconstruct(sharpdisplay_framebuffer_obj_t *self) {
- if (!allocation_from_ptr(self->bufinfo.buf)) {
- self->bufinfo.buf = NULL;
- }
+ // Look up the allocation by the old pointer and get the new pointer from it.
+ supervisor_allocation* alloc = allocation_from_ptr(self->bufinfo.buf);
+ self->bufinfo.buf = alloc ? alloc->ptr : NULL;
}
void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_obj_t *self, mp_buffer_info_t *bufinfo) {
@@ -112,7 +90,12 @@ void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_ob
int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
int height = common_hal_sharpdisplay_framebuffer_get_height(self);
self->bufinfo.len = row_stride * height + 2;
- self->bufinfo.buf = hybrid_alloc(self->bufinfo.len);
+ supervisor_allocation* alloc = allocate_memory(align32_size(self->bufinfo.len), false, true);
+ if (alloc == NULL) {
+ m_malloc_fail(self->bufinfo.len);
+ }
+ self->bufinfo.buf = alloc->ptr;
+ memset(alloc->ptr, 0, self->bufinfo.len);
uint8_t *data = self->bufinfo.buf;
*data++ = SHARPMEM_BIT_WRITECMD_LSB;
@@ -123,7 +106,9 @@ void common_hal_sharpdisplay_framebuffer_get_bufinfo(sharpdisplay_framebuffer_ob
}
self->full_refresh = true;
}
- *bufinfo = self->bufinfo;
+ if (bufinfo) {
+ *bufinfo = self->bufinfo;
+ }
}
void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *self) {
@@ -137,7 +122,7 @@ void common_hal_sharpdisplay_framebuffer_deinit(sharpdisplay_framebuffer_obj_t *
common_hal_reset_pin(self->chip_select.pin);
- hybrid_free(self->bufinfo.buf);
+ free_memory(allocation_from_ptr(self->bufinfo.buf));
memset(self, 0, sizeof(*self));
}
@@ -154,19 +139,7 @@ void common_hal_sharpdisplay_framebuffer_construct(sharpdisplay_framebuffer_obj_
self->height = height;
self->baudrate = baudrate;
- int row_stride = common_hal_sharpdisplay_framebuffer_get_row_stride(self);
- self->bufinfo.len = row_stride * height + 2;
- // re-use a supervisor allocation if possible
- self->bufinfo.buf = hybrid_alloc(self->bufinfo.len);
-
- uint8_t *data = self->bufinfo.buf;
- *data++ = SHARPMEM_BIT_WRITECMD_LSB;
-
- for(int y=0; y<self->height; y++) {
- *data = bitrev(y+1);
- data += row_stride;
- }
- self->full_refresh = true;
+ common_hal_sharpdisplay_framebuffer_get_bufinfo(self, NULL);
}
void common_hal_sharpdisplay_framebuffer_swapbuffers(sharpdisplay_framebuffer_obj_t *self, uint8_t *dirty_row_bitmask) {
@@ -271,7 +244,5 @@ const framebuffer_p_t sharpdisplay_framebuffer_proto = {
};
void common_hal_sharpdisplay_framebuffer_collect_ptrs(sharpdisplay_framebuffer_obj_t *self) {
- gc_collect_ptr(self->framebuffer);
gc_collect_ptr(self->bus);
- gc_collect_ptr(self->bufinfo.buf);
}
diff --git a/shared-module/sharpdisplay/SharpMemoryFramebuffer.h b/shared-module/sharpdisplay/SharpMemoryFramebuffer.h
index 8acacc94e..08966a89c 100644
--- a/shared-module/sharpdisplay/SharpMemoryFramebuffer.h
+++ b/shared-module/sharpdisplay/SharpMemoryFramebuffer.h
@@ -33,7 +33,6 @@
typedef struct {
mp_obj_base_t base;
- mp_obj_t framebuffer;
busio_spi_obj_t* bus;
busio_spi_obj_t inline_bus;
digitalio_digitalinout_obj_t chip_select;
diff --git a/shared-module/time/__init__.c b/shared-module/time/__init__.c
index 347e68ae0..c79a5f3ae 100644
--- a/shared-module/time/__init__.c
+++ b/shared-module/time/__init__.c
@@ -28,7 +28,7 @@
#include "supervisor/port.h"
#include "supervisor/shared/tick.h"
-uint64_t common_hal_time_monotonic(void) {
+uint64_t common_hal_time_monotonic_ms(void) {
return supervisor_ticks_ms64();
}
diff --git a/shared-module/usb_midi/__init__.c b/shared-module/usb_midi/__init__.c
index 73a314b99..3fb3f836c 100644
--- a/shared-module/usb_midi/__init__.c
+++ b/shared-module/usb_midi/__init__.c
@@ -40,12 +40,12 @@ supervisor_allocation* usb_midi_allocation;
void usb_midi_init(void) {
// TODO(tannewt): Make this dynamic.
- uint16_t tuple_size = align32_size(sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t*) * 2);
- uint16_t portin_size = align32_size(sizeof(usb_midi_portin_obj_t));
- uint16_t portout_size = align32_size(sizeof(usb_midi_portout_obj_t));
+ size_t tuple_size = align32_size(sizeof(mp_obj_tuple_t) + sizeof(mp_obj_t*) * 2);
+ size_t portin_size = align32_size(sizeof(usb_midi_portin_obj_t));
+ size_t portout_size = align32_size(sizeof(usb_midi_portout_obj_t));
// For each embedded MIDI Jack in the descriptor we create a Port
- usb_midi_allocation = allocate_memory(tuple_size + portin_size + portout_size, false);
+ usb_midi_allocation = allocate_memory(tuple_size + portin_size + portout_size, false, false);
mp_obj_tuple_t *ports = (mp_obj_tuple_t *) usb_midi_allocation->ptr;
ports->base.type = &mp_type_tuple;