summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorScott Shawcroft <scott@adafruit.com>2020-07-02 13:56:09 -0700
committerGitHub <noreply@github.com>2020-07-02 13:56:09 -0700
commitc33542f978cc61f0466ff60ea769e3067baca95f (patch)
tree071e8f8a86971f7e947053c3b4fb2ff7da95d223 /shared-module
parentf572b723060382bbc9ca7a3edc88cb7c30fdcc30 (diff)
parenteec42d4cb53536a75e88f7f9321515bc6dcfeaa7 (diff)
Merge branch 'main' into patch-1
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/_bleio/ScanEntry.c14
-rw-r--r--shared-module/_bleio/ScanResults.c10
-rw-r--r--shared-module/_eve/__init__.c1
-rw-r--r--shared-module/_pew/PewPew.c125
-rw-r--r--shared-module/_pew/__init__.c87
-rw-r--r--shared-module/_pixelbuf/PixelBuf.c28
-rw-r--r--shared-module/aesio/__init__.c58
-rw-r--r--shared-module/aesio/__init__.h (renamed from shared-module/_pew/__init__.h)37
-rw-r--r--shared-module/aesio/aes.c608
-rw-r--r--shared-module/aesio/aes.h105
-rw-r--r--shared-module/audiomixer/Mixer.c21
-rw-r--r--shared-module/bitbangio/SPI.c6
-rw-r--r--shared-module/board/__init__.c8
-rw-r--r--shared-module/displayio/Bitmap.c21
-rw-r--r--shared-module/displayio/ColorConverter.c16
-rw-r--r--shared-module/displayio/Display.c38
-rw-r--r--shared-module/displayio/Display.h5
-rw-r--r--shared-module/displayio/EPaperDisplay.c4
-rw-r--r--shared-module/displayio/EPaperDisplay.h1
-rw-r--r--shared-module/displayio/FourWire.c9
-rw-r--r--shared-module/displayio/Group.c50
-rw-r--r--shared-module/displayio/I2CDisplay.c2
-rw-r--r--shared-module/displayio/Palette.c9
-rw-r--r--shared-module/displayio/Palette.h1
-rw-r--r--shared-module/displayio/TileGrid.c2
-rw-r--r--shared-module/displayio/__init__.c84
-rw-r--r--shared-module/displayio/__init__.h15
-rw-r--r--shared-module/displayio/display_core.c50
-rw-r--r--shared-module/displayio/display_core.h2
-rw-r--r--shared-module/framebufferio/FramebufferDisplay.c312
-rw-r--r--shared-module/framebufferio/FramebufferDisplay.h92
-rw-r--r--shared-module/framebufferio/__init__.c0
-rw-r--r--shared-module/framebufferio/__init__.h0
-rw-r--r--shared-module/gamepad/GamePad.c2
-rw-r--r--shared-module/gamepad/__init__.c4
-rw-r--r--shared-module/gamepadshift/GamePadShift.c2
-rw-r--r--shared-module/network/__init__.c6
-rw-r--r--shared-module/rgbmatrix/RGBMatrix.c208
-rw-r--r--shared-module/rgbmatrix/RGBMatrix.h0
-rw-r--r--shared-module/rgbmatrix/__init__.c0
-rw-r--r--shared-module/rgbmatrix/__init__.h0
-rw-r--r--shared-module/rgbmatrix/allocator.h29
-rw-r--r--shared-module/sdcardio/SDCard.c466
-rw-r--r--shared-module/sdcardio/SDCard.h51
-rw-r--r--shared-module/sdcardio/__init__.c0
-rw-r--r--shared-module/sdcardio/__init__.h0
-rw-r--r--shared-module/time/__init__.c (renamed from shared-module/_pew/PewPew.h)37
-rw-r--r--shared-module/usb_hid/Device.c2
-rw-r--r--shared-module/ustack/__init__.c1
-rw-r--r--shared-module/vectorio/Circle.c55
-rw-r--r--shared-module/vectorio/Circle.h16
-rw-r--r--shared-module/vectorio/Polygon.c146
-rw-r--r--shared-module/vectorio/Polygon.h17
-rw-r--r--shared-module/vectorio/Rectangle.c34
-rw-r--r--shared-module/vectorio/Rectangle.h14
-rw-r--r--shared-module/vectorio/VectorShape.c334
-rw-r--r--shared-module/vectorio/VectorShape.h53
-rw-r--r--shared-module/vectorio/__init__.c2
-rw-r--r--shared-module/vectorio/__init__.h14
-rw-r--r--shared-module/wiznet/wiznet5k.c4
-rw-r--r--shared-module/wiznet/wiznet5k.h2
61 files changed, 2988 insertions, 332 deletions
diff --git a/shared-module/_bleio/ScanEntry.c b/shared-module/_bleio/ScanEntry.c
index 785209c4a..af9e4b347 100644
--- a/shared-module/_bleio/ScanEntry.c
+++ b/shared-module/_bleio/ScanEntry.c
@@ -56,11 +56,16 @@ bool bleio_scanentry_data_matches(const uint8_t* data, size_t len, const uint8_t
if (prefixes_length == 0) {
return true;
}
+ if (len == 0) {
+ // Prefixes exist, but no data.
+ return false;
+ }
size_t i = 0;
while(i < prefixes_length) {
uint8_t prefix_length = prefixes[i];
i += 1;
size_t j = 0;
+ bool prefix_matched = false;
while (j < len) {
uint8_t structure_length = data[j];
j += 1;
@@ -71,13 +76,18 @@ bool bleio_scanentry_data_matches(const uint8_t* data, size_t len, const uint8_t
if (any) {
return true;
}
- } else if (!any) {
- return false;
+ prefix_matched = true;
+ break;
}
j += structure_length;
}
+ // If all (!any), the current prefix must have matched at least one field.
+ if (!prefix_matched && !any) {
+ return false;
+ }
i += prefix_length;
}
+ // All prefixes matched some field (if !any), or none did (if any).
return !any;
}
diff --git a/shared-module/_bleio/ScanResults.c b/shared-module/_bleio/ScanResults.c
index 7ea0c165f..cb48d636d 100644
--- a/shared-module/_bleio/ScanResults.c
+++ b/shared-module/_bleio/ScanResults.c
@@ -45,10 +45,10 @@ bleio_scanresults_obj_t* shared_module_bleio_new_scanresults(size_t buffer_size,
}
mp_obj_t common_hal_bleio_scanresults_next(bleio_scanresults_obj_t *self) {
- while (ringbuf_count(&self->buf) == 0 && !self->done && !mp_hal_is_interrupted()) {
+ while (ringbuf_num_filled(&self->buf) == 0 && !self->done && !mp_hal_is_interrupted()) {
RUN_BACKGROUND_TASKS;
}
- if (ringbuf_count(&self->buf) == 0 || mp_hal_is_interrupted()) {
+ if (ringbuf_num_filled(&self->buf) == 0 || mp_hal_is_interrupted()) {
return mp_const_none;
}
@@ -56,7 +56,7 @@ mp_obj_t common_hal_bleio_scanresults_next(bleio_scanresults_obj_t *self) {
uint8_t type = ringbuf_get(&self->buf);
bool connectable = (type & (1 << 0)) != 0;
bool scan_response = (type & (1 << 1)) != 0;
- uint64_t ticks_ms;
+ uint64_t ticks_ms;
ringbuf_get_n(&self->buf, (uint8_t*) &ticks_ms, sizeof(ticks_ms));
uint8_t rssi = ringbuf_get(&self->buf);
uint8_t peer_addr[NUM_BLEIO_ADDRESS_BYTES];
@@ -81,7 +81,7 @@ mp_obj_t common_hal_bleio_scanresults_next(bleio_scanresults_obj_t *self) {
entry->time_received = ticks_ms;
entry->connectable = connectable;
entry->scan_response = scan_response;
-
+
return MP_OBJ_FROM_PTR(entry);
}
@@ -97,7 +97,7 @@ void shared_module_bleio_scanresults_append(bleio_scanresults_obj_t* self,
uint16_t len) {
int32_t packet_size = sizeof(uint8_t) + sizeof(ticks_ms) + sizeof(rssi) + NUM_BLEIO_ADDRESS_BYTES +
sizeof(addr_type) + sizeof(len) + len;
- int32_t empty_space = self->buf.size - ringbuf_count(&self->buf);
+ int32_t empty_space = self->buf.size - ringbuf_num_filled(&self->buf);
if (packet_size >= empty_space) {
// We can't fit the packet so skip it.
return;
diff --git a/shared-module/_eve/__init__.c b/shared-module/_eve/__init__.c
index 2c93eb69f..0f1e12d9f 100644
--- a/shared-module/_eve/__init__.c
+++ b/shared-module/_eve/__init__.c
@@ -314,4 +314,3 @@ void common_hal__eve_VertexTranslateY(common_hal__eve_t *eve, uint32_t y) {
void common_hal__eve_Vertex2ii(common_hal__eve_t *eve, uint32_t x, uint32_t y, uint32_t handle, uint32_t cell) {
C4(eve, ((2 << 30) | (((x) & 511) << 21) | (((y) & 511) << 12) | (((handle) & 31) << 7) | (((cell) & 127) << 0)));
}
-
diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c
deleted file mode 100644
index 568dc6425..000000000
--- a/shared-module/_pew/PewPew.c
+++ /dev/null
@@ -1,125 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2019 Radomir Dopieralski
- *
- * 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 <stdbool.h>
-
-#include "py/mpstate.h"
-#include "py/runtime.h"
-#include "__init__.h"
-#include "PewPew.h"
-
-#include "shared-bindings/digitalio/Pull.h"
-#include "shared-bindings/digitalio/DigitalInOut.h"
-#include "shared-bindings/util.h"
-#include "samd/timers.h"
-#include "supervisor/shared/translate.h"
-#include "timer_handler.h"
-
-
-static uint8_t pewpew_tc_index = 0xff;
-
-
-void pewpew_interrupt_handler(uint8_t index) {
- if (index != pewpew_tc_index) {
- return;
- }
- Tc* tc = tc_insts[index];
- if (!tc->COUNT16.INTFLAG.bit.MC0) {
- return;
- }
-
- pew_tick();
-
- // Clear the interrupt bit.
- tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
-}
-
-void pew_init() {
- pew_obj_t* pew = MP_STATE_VM(pew_singleton);
-
- common_hal_digitalio_digitalinout_switch_to_input(pew->buttons, PULL_UP);
-
- for (size_t i = 0; i < pew->rows_size; ++i) {
- digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pew->rows[i]);
- common_hal_digitalio_digitalinout_switch_to_output(pin, false,
- DRIVE_MODE_PUSH_PULL);
- }
- for (size_t i = 0; i < pew->cols_size; ++i) {
- digitalio_digitalinout_obj_t *pin = MP_OBJ_TO_PTR(pew->cols[i]);
- common_hal_digitalio_digitalinout_switch_to_output(pin, true,
- DRIVE_MODE_OPEN_DRAIN);
- }
- if (pewpew_tc_index == 0xff) {
- // Find a spare timer.
- uint8_t index = find_free_timer();
- if (index == 0xff) {
- mp_raise_RuntimeError(translate("All timers in use"));
- }
- Tc *tc = tc_insts[index];
-
- pewpew_tc_index = index;
- set_timer_handler(true, index, TC_HANDLER_PEW);
-
- // We use GCLK0 for SAMD21 and GCLK1 for SAMD51 because they both run
- // at 48mhz making our math the same across the boards.
- #ifdef SAMD21
- turn_on_clocks(true, index, 0);
- #endif
- #ifdef SAMD51
- turn_on_clocks(true, index, 1);
- #endif
-
-
- #ifdef SAMD21
- tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16 |
- TC_CTRLA_PRESCALER_DIV64 |
- TC_CTRLA_WAVEGEN_MFRQ;
- #endif
- #ifdef SAMD51
- tc_reset(tc);
- tc_set_enable(tc, false);
- tc->COUNT16.CTRLA.reg = TC_CTRLA_MODE_COUNT16
- | TC_CTRLA_PRESCALER_DIV64;
- tc->COUNT16.WAVE.reg = TC_WAVE_WAVEGEN_MFRQ;
- #endif
-
- tc_set_enable(tc, true);
- tc->COUNT16.CC[0].reg = 64;
-
- // Clear our interrupt in case it was set earlier
- tc->COUNT16.INTFLAG.reg = TC_INTFLAG_MC0;
- tc->COUNT16.INTENSET.reg = TC_INTENSET_MC0;
- tc_enable_interrupts(pewpew_tc_index);
- }
-}
-
-void pew_reset(void) {
- if (pewpew_tc_index != 0xff) {
- tc_reset(tc_insts[pewpew_tc_index]);
- pewpew_tc_index = 0xff;
- }
- MP_STATE_VM(pew_singleton) = NULL;
-}
diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c
deleted file mode 100644
index f3b23c606..000000000
--- a/shared-module/_pew/__init__.c
+++ /dev/null
@@ -1,87 +0,0 @@
-/*
- * This file is part of the MicroPython project, http://micropython.org/
- *
- * The MIT License (MIT)
- *
- * Copyright (c) 2019 Radomir Dopieralski
- *
- * 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 <stdbool.h>
-
-#include "py/mpstate.h"
-#include "__init__.h"
-#include "PewPew.h"
-
-#include "shared-bindings/digitalio/DigitalInOut.h"
-
-
-void pew_tick(void) {
- static uint8_t col = 0;
- static uint8_t turn = 0;
- static uint8_t pressed = 0;
- static uint8_t last_pressed = 0;
- digitalio_digitalinout_obj_t *pin;
-
- pew_obj_t* pew = MP_STATE_VM(pew_singleton);
- if (!pew) { return; }
-
- pin = MP_OBJ_TO_PTR(pew->cols[col]);
- ++col;
- if (col >= pew->cols_size) {
- pew->pressed |= last_pressed & pressed;
- last_pressed = pressed;
- pressed = 0;
- col = 0;
- ++turn;
- if (turn > 11) {
- turn = 0;
- }
- }
- if (!common_hal_digitalio_digitalinout_get_value(pew->buttons)) {
- pressed |= 1 << col;
- }
- common_hal_digitalio_digitalinout_set_value(pin, true);
- for (size_t x = 0; x < pew->rows_size; ++x) {
- pin = MP_OBJ_TO_PTR(pew->rows[x]);
- uint8_t color = pew->buffer[col * (pew->rows_size) + x];
- bool value = false;
- switch (color & 0x03) {
- case 3:
- value = true;
- break;
- case 2:
- if (turn == 2 || turn == 5 || turn == 8 || turn == 11) {
- value = true;
- }
- break;
- case 1:
- if (turn == 0) {
- value = true;
- }
- break;
- case 0:
- break;
- }
- common_hal_digitalio_digitalinout_set_value(pin, value);
- }
- pin = MP_OBJ_TO_PTR(pew->cols[col]);
- common_hal_digitalio_digitalinout_set_value(pin, false);
-}
diff --git a/shared-module/_pixelbuf/PixelBuf.c b/shared-module/_pixelbuf/PixelBuf.c
index bad8539ea..9d671e454 100644
--- a/shared-module/_pixelbuf/PixelBuf.c
+++ b/shared-module/_pixelbuf/PixelBuf.c
@@ -147,18 +147,11 @@ void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_
*r = value >> 16 & 0xff;
*g = (value >> 8) & 0xff;
*b = value & 0xff;
- // Int colors can't set white directly so convert to white when all components are equal.
- if (!byteorder->is_dotstar && byteorder->bpp == 4 && byteorder->has_white && *r == *g && *r == *b) {
- *w = *r;
- *r = 0;
- *g = 0;
- *b = 0;
- }
} else {
mp_obj_t *items;
size_t len;
mp_obj_get_array(color, &len, &items);
- if (len != byteorder->bpp && !byteorder->is_dotstar) {
+ if (len < 3 || len > 4) {
mp_raise_ValueError_varg(translate("Expected tuple of length %d, got %d"), byteorder->bpp, len);
}
@@ -171,8 +164,17 @@ void _pixelbuf_parse_color(pixelbuf_pixelbuf_obj_t* self, mp_obj_t color, uint8_
} else {
*w = mp_obj_get_int_truncated(items[PIXEL_W]);
}
+ return;
}
}
+ // Int colors can't set white directly so convert to white when all components are equal.
+ // Also handles RGBW values assigned an RGB tuple.
+ if (!byteorder->is_dotstar && byteorder->bpp == 4 && byteorder->has_white && *r == *g && *r == *b) {
+ *w = *r;
+ *r = 0;
+ *g = 0;
+ *b = 0;
+ }
}
void _pixelbuf_set_pixel_color(pixelbuf_pixelbuf_obj_t* self, size_t index, uint8_t r, uint8_t g, uint8_t b, uint8_t w) {
@@ -216,12 +218,11 @@ void _pixelbuf_set_pixel(pixelbuf_pixelbuf_obj_t* self, size_t index, mp_obj_t v
_pixelbuf_set_pixel_color(self, index, r, g, b, w);
}
-void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, size_t stop, size_t step, mp_obj_t* values) {
+void common_hal__pixelbuf_pixelbuf_set_pixels(mp_obj_t self_in, size_t start, mp_int_t step, size_t slice_len, mp_obj_t* values) {
pixelbuf_pixelbuf_obj_t* self = native_pixelbuf(self_in);
- size_t source_i = 0;
- for (size_t target_i = start; target_i < stop; target_i += step) {
- _pixelbuf_set_pixel(self, target_i, values[source_i]);
- source_i++;
+ for (size_t i = 0; i < slice_len; i++) {
+ _pixelbuf_set_pixel(self, start, values[i]);
+ start+=step;
}
if (self->auto_write) {
common_hal__pixelbuf_pixelbuf_show(self_in);
@@ -243,6 +244,7 @@ mp_obj_t common_hal__pixelbuf_pixelbuf_get_pixel(mp_obj_t self_in, size_t index)
if (self->pre_brightness_buffer != NULL) {
pixel_buffer = self->pre_brightness_buffer;
}
+ pixel_buffer += self->byteorder.bpp * index;
pixelbuf_rgbw_t *rgbw_order = &self->byteorder.byteorder;
elems[0] = MP_OBJ_NEW_SMALL_INT(pixel_buffer[rgbw_order->r]);
diff --git a/shared-module/aesio/__init__.c b/shared-module/aesio/__init__.c
new file mode 100644
index 000000000..2cacaeb66
--- /dev/null
+++ b/shared-module/aesio/__init__.c
@@ -0,0 +1,58 @@
+#include <string.h>
+
+#include "py/runtime.h"
+
+#include "shared-bindings/aesio/__init__.h"
+#include "shared-module/aesio/__init__.h"
+
+void common_hal_aesio_aes_construct(aesio_aes_obj_t *self, const uint8_t *key,
+ uint32_t key_length, const uint8_t *iv,
+ int mode, int counter) {
+ self->mode = mode;
+ self->counter = counter;
+ common_hal_aesio_aes_rekey(self, key, key_length, iv);
+}
+
+void common_hal_aesio_aes_rekey(aesio_aes_obj_t *self, const uint8_t *key,
+ uint32_t key_length, const uint8_t *iv) {
+ memset(&self->ctx, 0, sizeof(self->ctx));
+ if (iv != NULL) {
+ AES_init_ctx_iv(&self->ctx, key, key_length, iv);
+ } else {
+ AES_init_ctx(&self->ctx, key, key_length);
+ }
+}
+
+void common_hal_aesio_aes_set_mode(aesio_aes_obj_t *self, int mode) {
+ self->mode = mode;
+}
+
+void common_hal_aesio_aes_encrypt(aesio_aes_obj_t *self, uint8_t *buffer,
+ size_t length) {
+ switch (self->mode) {
+ case AES_MODE_ECB:
+ AES_ECB_encrypt(&self->ctx, buffer);
+ break;
+ case AES_MODE_CBC:
+ AES_CBC_encrypt_buffer(&self->ctx, buffer, length);
+ break;
+ case AES_MODE_CTR:
+ AES_CTR_xcrypt_buffer(&self->ctx, buffer, length);
+ break;
+ }
+}
+
+void common_hal_aesio_aes_decrypt(aesio_aes_obj_t *self, uint8_t *buffer,
+ size_t length) {
+ switch (self->mode) {
+ case AES_MODE_ECB:
+ AES_ECB_decrypt(&self->ctx, buffer);
+ break;
+ case AES_MODE_CBC:
+ AES_CBC_decrypt_buffer(&self->ctx, buffer, length);
+ break;
+ case AES_MODE_CTR:
+ AES_CTR_xcrypt_buffer(&self->ctx, buffer, length);
+ break;
+ }
+}
diff --git a/shared-module/_pew/__init__.h b/shared-module/aesio/__init__.h
index 4f953c610..1a0bb8696 100644
--- a/shared-module/_pew/__init__.h
+++ b/shared-module/aesio/__init__.h
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (c) 2019 Radomir Dopieralski
+ * Copyright (c) 2018 Dan Halbert 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
@@ -24,9 +24,36 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_PEW_H
-#define MICROPY_INCLUDED_PEW_H
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_AESIO__INIT__H
+#define MICROPY_INCLUDED_SHARED_MODULE_AESIO__INIT__H
-void pew_tick(void);
+#include <stdbool.h>
+#include <stdint.h>
-#endif // MICROPY_INCLUDED_PEW_H
+#include "py/obj.h"
+#include "py/proto.h"
+
+#include "shared-module/aesio/aes.h"
+
+// These values were chosen to correspond with the values
+// present in pycrypto.
+enum AES_MODE {
+ AES_MODE_ECB = 1,
+ AES_MODE_CBC = 2,
+ AES_MODE_CTR = 6,
+};
+
+typedef struct {
+ mp_obj_base_t base;
+
+ // The tinyaes context
+ struct AES_ctx ctx;
+
+ // Which AES mode this instance of the object is configured to use
+ enum AES_MODE mode;
+
+ // Counter for running in CTR mode
+ uint32_t counter;
+} aesio_aes_obj_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_AESIO__INIT__H
diff --git a/shared-module/aesio/aes.c b/shared-module/aesio/aes.c
new file mode 100644
index 000000000..b62b5afc4
--- /dev/null
+++ b/shared-module/aesio/aes.c
@@ -0,0 +1,608 @@
+/*
+
+This is an implementation of the AES algorithm, specifically ECB, CTR and CBC mode.
+Block size can be chosen in aes.h - available choices are AES128, AES192, AES256.
+
+The implementation is verified against the test vectors in:
+ National Institute of Standards and Technology Special Publication 800-38A 2001 ED
+
+ECB-AES128
+----------
+
+ plain-text:
+ 6bc1bee22e409f96e93d7e117393172a
+ ae2d8a571e03ac9c9eb76fac45af8e51
+ 30c81c46a35ce411e5fbc1191a0a52ef
+ f69f2445df4f9b17ad2b417be66c3710
+
+ key:
+ 2b7e151628aed2a6abf7158809cf4f3c
+
+ resulting cipher
+ 3ad77bb40d7a3660a89ecaf32466ef97
+ f5d3d58503b9699de785895a96fdbaaf
+ 43b1cd7f598ece23881b00e3ed030688
+ 7b0c785e27e8ad3f8223207104725dd4
+
+
+NOTE: String length must be evenly divisible by 16byte (str_len % 16 == 0)
+ You should pad the end of the string with zeros if this is not the case.
+ For AES192/256 the key size is proportionally larger.
+
+*/
+
+/*****************************************************************************/
+/* Includes: */
+/*****************************************************************************/
+#include <string.h> // CBC mode, for memset
+#include "aes.h"
+
+/*****************************************************************************/
+/* Defines: */
+/*****************************************************************************/
+// The number of columns comprising a state in AES. This is a constant in AES.
+// Value=4
+#define Nb 4UL
+
+#if defined(AES256) && (AES256 == 1)
+ #define Nk256 8UL
+ #define Nr256 14UL
+#endif
+#if defined(AES192) && (AES192 == 1)
+ #define Nk192 6UL
+ #define Nr192 12UL
+#endif
+#if defined(AES128) && (AES128 == 1)
+ #define Nk128 4UL // The number of 32 bit words in a key.
+ #define Nr128 10UL // The number of rounds in AES Cipher.
+#endif
+
+// jcallan@github points out that declaring Multiply as a function reduces code
+// size considerably with the Keil ARM compiler. See this link for more
+// information: https://github.com/kokke/tiny-AES-C/pull/3
+#ifndef MULTIPLY_AS_A_FUNCTION
+ #define MULTIPLY_AS_A_FUNCTION 0
+#endif
+
+
+
+
+/*****************************************************************************/
+/* Private variables: */
+/*****************************************************************************/
+// state - array holding the intermediate results during decryption.
+typedef uint8_t state_t[4][4];
+
+
+
+// The lookup-tables are marked const so they can be placed in read-only storage
+// instead of RAM The numbers below can be computed dynamically trading ROM for
+// RAM - This can be useful in (embedded) bootloader applications, where ROM is
+// often limited.
+static const uint8_t sbox[256] = {
+ //0 1 2 3 4 5 6 7 8 9 A B C D E F
+ 0x63, 0x7c, 0x77, 0x7b, 0xf2, 0x6b, 0x6f, 0xc5, 0x30, 0x01, 0x67, 0x2b, 0xfe, 0xd7, 0xab, 0x76,
+ 0xca, 0x82, 0xc9, 0x7d, 0xfa, 0x59, 0x47, 0xf0, 0xad, 0xd4, 0xa2, 0xaf, 0x9c, 0xa4, 0x72, 0xc0,
+ 0xb7, 0xfd, 0x93, 0x26, 0x36, 0x3f, 0xf7, 0xcc, 0x34, 0xa5, 0xe5, 0xf1, 0x71, 0xd8, 0x31, 0x15,
+ 0x04, 0xc7, 0x23, 0xc3, 0x18, 0x96, 0x05, 0x9a, 0x07, 0x12, 0x80, 0xe2, 0xeb, 0x27, 0xb2, 0x75,
+ 0x09, 0x83, 0x2c, 0x1a, 0x1b, 0x6e, 0x5a, 0xa0, 0x52, 0x3b, 0xd6, 0xb3, 0x29, 0xe3, 0x2f, 0x84,
+ 0x53, 0xd1, 0x00, 0xed, 0x20, 0xfc, 0xb1, 0x5b, 0x6a, 0xcb, 0xbe, 0x39, 0x4a, 0x4c, 0x58, 0xcf,
+ 0xd0, 0xef, 0xaa, 0xfb, 0x43, 0x4d, 0x33, 0x85, 0x45, 0xf9, 0x02, 0x7f, 0x50, 0x3c, 0x9f, 0xa8,
+ 0x51, 0xa3, 0x40, 0x8f, 0x92, 0x9d, 0x38, 0xf5, 0xbc, 0xb6, 0xda, 0x21, 0x10, 0xff, 0xf3, 0xd2,
+ 0xcd, 0x0c, 0x13, 0xec, 0x5f, 0x97, 0x44, 0x17, 0xc4, 0xa7, 0x7e, 0x3d, 0x64, 0x5d, 0x19, 0x73,
+ 0x60, 0x81, 0x4f, 0xdc, 0x22, 0x2a, 0x90, 0x88, 0x46, 0xee, 0xb8, 0x14, 0xde, 0x5e, 0x0b, 0xdb,
+ 0xe0, 0x32, 0x3a, 0x0a, 0x49, 0x06, 0x24, 0x5c, 0xc2, 0xd3, 0xac, 0x62, 0x91, 0x95, 0xe4, 0x79,
+ 0xe7, 0xc8, 0x37, 0x6d, 0x8d, 0xd5, 0x4e, 0xa9, 0x6c, 0x56, 0xf4, 0xea, 0x65, 0x7a, 0xae, 0x08,
+ 0xba, 0x78, 0x25, 0x2e, 0x1c, 0xa6, 0xb4, 0xc6, 0xe8, 0xdd, 0x74, 0x1f, 0x4b, 0xbd, 0x8b, 0x8a,
+ 0x70, 0x3e, 0xb5, 0x66, 0x48, 0x03, 0xf6, 0x0e, 0x61, 0x35, 0x57, 0xb9, 0x86, 0xc1, 0x1d, 0x9e,
+ 0xe1, 0xf8, 0x98, 0x11, 0x69, 0xd9, 0x8e, 0x94, 0x9b, 0x1e, 0x87, 0xe9, 0xce, 0x55, 0x28, 0xdf,
+ 0x8c, 0xa1, 0x89, 0x0d, 0xbf, 0xe6, 0x42, 0x68, 0x41, 0x99, 0x2d, 0x0f, 0xb0, 0x54, 0xbb, 0x16 };
+
+static const uint8_t rsbox[256] = {
+ 0x52, 0x09, 0x6a, 0xd5, 0x30, 0x36, 0xa5, 0x38, 0xbf, 0x40, 0xa3, 0x9e, 0x81, 0xf3, 0xd7, 0xfb,
+ 0x7c, 0xe3, 0x39, 0x82, 0x9b, 0x2f, 0xff, 0x87, 0x34, 0x8e, 0x43, 0x44, 0xc4, 0xde, 0xe9, 0xcb,
+ 0x54, 0x7b, 0x94, 0x32, 0xa6, 0xc2, 0x23, 0x3d, 0xee, 0x4c, 0x95, 0x0b, 0x42, 0xfa, 0xc3, 0x4e,
+ 0x08, 0x2e, 0xa1, 0x66, 0x28, 0xd9, 0x24, 0xb2, 0x76, 0x5b, 0xa2, 0x49, 0x6d, 0x8b, 0xd1, 0x25,
+ 0x72, 0xf8, 0xf6, 0x64, 0x86, 0x68, 0x98, 0x16, 0xd4, 0xa4, 0x5c, 0xcc, 0x5d, 0x65, 0xb6, 0x92,
+ 0x6c, 0x70, 0x48, 0x50, 0xfd, 0xed, 0xb9, 0xda, 0x5e, 0x15, 0x46, 0x57, 0xa7, 0x8d, 0x9d, 0x84,
+ 0x90, 0xd8, 0xab, 0x00, 0x8c, 0xbc, 0xd3, 0x0a, 0xf7, 0xe4, 0x58, 0x05, 0xb8, 0xb3, 0x45, 0x06,
+ 0xd0, 0x2c, 0x1e, 0x8f, 0xca, 0x3f, 0x0f, 0x02, 0xc1, 0xaf, 0xbd, 0x03, 0x01, 0x13, 0x8a, 0x6b,
+ 0x3a, 0x91, 0x11, 0x41, 0x4f, 0x67, 0xdc, 0xea, 0x97, 0xf2, 0xcf, 0xce, 0xf0, 0xb4, 0xe6, 0x73,
+ 0x96, 0xac, 0x74, 0x22, 0xe7, 0xad, 0x35, 0x85, 0xe2, 0xf9, 0x37, 0xe8, 0x1c, 0x75, 0xdf, 0x6e,
+ 0x47, 0xf1, 0x1a, 0x71, 0x1d, 0x29, 0xc5, 0x89, 0x6f, 0xb7, 0x62, 0x0e, 0xaa, 0x18, 0xbe, 0x1b,
+ 0xfc, 0x56, 0x3e, 0x4b, 0xc6, 0xd2, 0x79, 0x20, 0x9a, 0xdb, 0xc0, 0xfe, 0x78, 0xcd, 0x5a, 0xf4,
+ 0x1f, 0xdd, 0xa8, 0x33, 0x88, 0x07, 0xc7, 0x31, 0xb1, 0x12, 0x10, 0x59, 0x27, 0x80, 0xec, 0x5f,
+ 0x60, 0x51, 0x7f, 0xa9, 0x19, 0xb5, 0x4a, 0x0d, 0x2d, 0xe5, 0x7a, 0x9f, 0x93, 0xc9, 0x9c, 0xef,
+ 0xa0, 0xe0, 0x3b, 0x4d, 0xae, 0x2a, 0xf5, 0xb0, 0xc8, 0xeb, 0xbb, 0x3c, 0x83, 0x53, 0x99, 0x61,
+ 0x17, 0x2b, 0x04, 0x7e, 0xba, 0x77, 0xd6, 0x26, 0xe1, 0x69, 0x14, 0x63, 0x55, 0x21, 0x0c, 0x7d };
+
+// The round constant word array, Rcon[i], contains the values given by x to the
+// power (i-1) being powers of x (x is denoted as {02}) in the field GF(2^8)
+static const uint8_t Rcon[11] = {
+ 0x8d, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36 };
+
+/*
+ * Jordan Goulder points out in PR #12
+ * (https://github.com/kokke/tiny-AES-C/pull/12), that you can remove most of
+ * the elements in the Rcon array, because they are unused.
+ *
+ * From Wikipedia's article on the Rijndael key schedule @
+ * https://en.wikipedia.org/wiki/Rijndael_key_schedule#Rcon
+ *
+ * "Only the first some of these constants are actually used – up to rcon[10]
+ * for AES-128 (as 11 round keys are needed), up to rcon[8] for AES-192, up to
+ * rcon[7] for AES-256. rcon[0] is not used in AES algorithm."
+ */
+
+
+/*****************************************************************************/
+/* Private functions: */
+/*****************************************************************************/
+static const uint8_t *GetRoundKey(const struct AES_ctx *ctx) {
+ switch (ctx->KeyLength) {
+#if defined(AES128) && (AES128 == 1)
+ case 16: return ctx->RoundKey128;
+#endif
+#if defined(AES192) && (AES192 == 1)
+ case 24: return ctx->RoundKey192;
+#endif
+#if defined(AES256) && (AES256 == 1)
+ case 32: return ctx->RoundKey256;
+#endif
+ }
+ return NULL;
+}
+
+
+/*
+static uint8_t getSBoxValue(uint8_t num)
+{
+ return sbox[num];
+}
+*/
+#define getSBoxValue(num) (sbox[(num)])
+/*
+static uint8_t getSBoxInvert(uint8_t num)
+{
+ return rsbox[num];
+}
+*/
+#define getSBoxInvert(num) (rsbox[(num)])
+
+// This function produces Nb(Nr+1) round keys. The round keys are used in each
+// round to decrypt the states.
+static void KeyExpansion(struct AES_ctx* ctx, const uint8_t* Key)
+{
+ uint8_t* RoundKey = (uint8_t *)GetRoundKey(ctx);
+
+ unsigned i, j, k;
+ uint8_t tempa[4]; // Used for the column/row operations
+
+ // The first round key is the key itself.
+ for (i = 0; i < ctx->Nk; ++i)
+ {
+ RoundKey[(i * 4) + 0] = Key[(i * 4) + 0];
+ RoundKey[(i * 4) + 1] = Key[(i * 4) + 1];
+ RoundKey[(i * 4) + 2] = Key[(i * 4) + 2];
+ RoundKey[(i * 4) + 3] = Key[(i * 4) + 3];
+ }
+
+ // All other round keys are found from the previous round keys.
+ for (i = ctx->Nk; i < Nb * (ctx->Nr + 1); ++i)
+ {
+ {
+ k = (i - 1) * 4;
+ tempa[0]=RoundKey[k + 0];
+ tempa[1]=RoundKey[k + 1];
+ tempa[2]=RoundKey[k + 2];
+ tempa[3]=RoundKey[k + 3];
+
+ }
+
+ if (i % ctx->Nk == 0)
+ {
+ // This function shifts the 4 bytes in a word to the left once.
+ // [a0,a1,a2,a3] becomes [a1,a2,a3,a0]
+
+ // Function RotWord()
+ {
+ const uint8_t u8tmp = tempa[0];
+ tempa[0] = tempa[1];
+ tempa[1] = tempa[2];
+ tempa[2] = tempa[3];
+ tempa[3] = u8tmp;
+ }
+
+ // SubWord() is a function that takes a four-byte input word and applies
+ // the S-box to each of the four bytes to produce an output word.
+
+ // Function Subword()
+ {
+ tempa[0] = getSBoxValue(tempa[0]);
+ tempa[1] = getSBoxValue(tempa[1]);
+ tempa[2] = getSBoxValue(tempa[2]);
+ tempa[3] = getSBoxValue(tempa[3]);
+ }
+
+ tempa[0] = tempa[0] ^ Rcon[i/ctx->Nk];
+ }
+#if defined(AES256) && (AES256 == 1)
+ if (ctx->KeyLength == 32) {
+ if (i % ctx->Nk == 4)
+ {
+ // Function Subword()
+ {
+ tempa[0] = getSBoxValue(tempa[0]);
+ tempa[1] = getSBoxValue(tempa[1]);
+ tempa[2] = getSBoxValue(tempa[2]);
+ tempa[3] = getSBoxValue(tempa[3]);
+ }
+ }
+ }
+#endif
+ j = i * 4; k=(i - ctx->Nk) * 4;
+ RoundKey[j + 0] = RoundKey[k + 0] ^ tempa[0];
+ RoundKey[j + 1] = RoundKey[k + 1] ^ tempa[1];
+ RoundKey[j + 2] = RoundKey[k + 2] ^ tempa[2];
+ RoundKey[j + 3] = RoundKey[k + 3] ^ tempa[3];
+ }
+}
+
+void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen)
+{
+ ctx->KeyLength = keylen;
+ switch (ctx->KeyLength) {
+#if defined(AES128) && (AES128 == 1)
+ case 16: ctx->Nr = Nr128; ctx->Nk = Nk128; break;
+#endif
+#if defined(AES192) && (AES192 == 1)
+ case 24: ctx->Nr = Nr192; ctx->Nk = Nk192; break;
+#endif
+#if defined(AES256) && (AES256 == 1)
+ case 32: ctx->Nr = Nr256; ctx->Nk = Nk256; break;
+#endif
+ default: ctx->Nr = 0; ctx->Nk = 0; break;
+ }
+ KeyExpansion(ctx, key);
+}
+#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
+void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen, const uint8_t* iv)
+{
+ AES_init_ctx(ctx, key, keylen);
+ memcpy (ctx->Iv, iv, AES_BLOCKLEN);
+}
+void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv)
+{
+ memcpy (ctx->Iv, iv, AES_BLOCKLEN);
+}
+#endif
+
+// This function adds the round key to state. The round key is added to the
+// state by an XOR function.
+static void AddRoundKey(uint8_t round, state_t* state, const uint8_t* RoundKey)
+{
+ uint8_t i,j;
+ for (i = 0; i < 4; ++i)
+ {
+ for (j = 0; j < 4; ++j)
+ {
+ (*state)[i][j] ^= RoundKey[(round * Nb * 4) + (i * Nb) + j];
+ }
+ }
+}
+
+// The SubBytes Function Substitutes the values in the state matrix with values
+// in an S-box.
+static void SubBytes(state_t* state)
+{
+ uint8_t i, j;
+ for (i = 0; i < 4; ++i)
+ {
+ for (j = 0; j < 4; ++j)
+ {
+ (*state)[j][i] = getSBoxValue((*state)[j][i]);
+ }
+ }
+}
+
+// The ShiftRows() function shifts the rows in the state to the left. Each row
+// is shifted with different offset. Offset = Row number. So the first row is
+// not shifted.
+static void ShiftRows(state_t* state)
+{
+ uint8_t temp;
+
+ // Rotate first row 1 columns to left
+ temp = (*state)[0][1];
+ (*state)[0][1] = (*state)[1][1];
+ (*state)[1][1] = (*state)[2][1];
+ (*state)[2][1] = (*state)[3][1];
+ (*state)[3][1] = temp;
+
+ // Rotate second row 2 columns to left
+ temp = (*state)[0][2];
+ (*state)[0][2] = (*state)[2][2];
+ (*state)[2][2] = temp;
+
+ temp = (*state)[1][2];
+ (*state)[1][2] = (*state)[3][2];
+ (*state)[3][2] = temp;
+
+ // Rotate third row 3 columns to left
+ temp = (*state)[0][3];
+ (*state)[0][3] = (*state)[3][3];
+ (*state)[3][3] = (*state)[2][3];
+ (*state)[2][3] = (*state)[1][3];
+ (*state)[1][3] = temp;
+}
+
+static uint8_t xtime(uint8_t x)
+{
+ return ((x<<1) ^ (((x>>7) & 1) * 0x1b));
+}
+
+// MixColumns function mixes the columns of the state matrix
+static void MixColumns(state_t* state)
+{
+ uint8_t i;
+ uint8_t Tmp, Tm, t;
+ for (i = 0; i < 4; ++i)
+ {
+ t = (*state)[i][0];
+ Tmp = (*state)[i][0] ^ (*state)[i][1] ^ (*state)[i][2] ^ (*state)[i][3] ;
+ Tm = (*state)[i][0] ^ (*state)[i][1] ; Tm = xtime(Tm); (*state)[i][0] ^= Tm ^ Tmp ;
+ Tm = (*state)[i][1] ^ (*state)[i][2] ; Tm = xtime(Tm); (*state)[i][1] ^= Tm ^ Tmp ;
+ Tm = (*state)[i][2] ^ (*state)[i][3] ; Tm = xtime(Tm); (*state)[i][2] ^= Tm ^ Tmp ;
+ Tm = (*state)[i][3] ^ t ; Tm = xtime(Tm); (*state)[i][3] ^= Tm ^ Tmp ;
+ }
+}
+
+// Multiply is used to multiply numbers in the field GF(2^8)
+// Note: The last call to xtime() is unneeded, but often ends up generating a smaller binary
+// The compiler seems to be able to vectorize the operation better this way.
+// See https://github.com/kokke/tiny-AES-c/pull/34
+#if MULTIPLY_AS_A_FUNCTION
+static uint8_t Multiply(uint8_t x, uint8_t y)
+{
+ return (((y & 1) * x) ^
+ ((y>>1 & 1) * xtime(x)) ^
+ ((y>>2 & 1) * xtime(xtime(x))) ^
+ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^
+ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))); /* this last call to xtime() can be omitted */
+ }
+#else
+#define Multiply(x, y) \
+ ( ((y & 1) * x) ^ \
+ ((y>>1 & 1) * xtime(x)) ^ \
+ ((y>>2 & 1) * xtime(xtime(x))) ^ \
+ ((y>>3 & 1) * xtime(xtime(xtime(x)))) ^ \
+ ((y>>4 & 1) * xtime(xtime(xtime(xtime(x)))))) \
+
+#endif
+
+#if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
+// MixColumns function mixes the columns of the state matrix. The method used to
+// multiply may be difficult to understand for the inexperienced. Please use the
+// references to gain more information.
+static void InvMixColumns(state_t* state)
+{
+ int i;
+ uint8_t a, b, c, d;
+ for (i = 0; i < 4; ++i)
+ {
+ a = (*state)[i][0];
+ b = (*state)[i][1];
+ c = (*state)[i][2];
+ d = (*state)[i][3];
+
+ (*state)[i][0] = Multiply(a, 0x0e) ^ Multiply(b, 0x0b) ^ Multiply(c, 0x0d) ^ Multiply(d, 0x09);
+ (*state)[i][1] = Multiply(a, 0x09) ^ Multiply(b, 0x0e) ^ Multiply(c, 0x0b) ^ Multiply(d, 0x0d);
+ (*state)[i][2] = Multiply(a, 0x0d) ^ Multiply(b, 0x09) ^ Multiply(c, 0x0e) ^ Multiply(d, 0x0b);
+ (*state)[i][3] = Multiply(a, 0x0b) ^ Multiply(b, 0x0d) ^ Multiply(c, 0x09) ^ Multiply(d, 0x0e);
+ }
+}
+
+
+// The SubBytes Function Substitutes the values in the state matrix with values
+// in an S-box.
+static void InvSubBytes(state_t* state)
+{
+ uint8_t i, j;
+ for (i = 0; i < 4; ++i)
+ {
+ for (j = 0; j < 4; ++j)
+ {
+ (*state)[j][i] = getSBoxInvert((*state)[j][i]);
+ }
+ }
+}
+
+static void InvShiftRows(state_t* state)
+{
+ uint8_t temp;
+
+ // Rotate first row 1 columns to right
+ temp = (*state)[3][1];
+ (*state)[3][1] = (*state)[2][1];
+ (*state)[2][1] = (*state)[1][1];
+ (*state)[1][1] = (*state)[0][1];
+ (*state)[0][1] = temp;
+
+ // Rotate second row 2 columns to right
+ temp = (*state)[0][2];
+ (*state)[0][2] = (*state)[2][2];
+ (*state)[2][2] = temp;
+
+ temp = (*state)[1][2];
+ (*state)[1][2] = (*state)[3][2];
+ (*state)[3][2] = temp;
+
+ // Rotate third row 3 columns to right
+ temp = (*state)[0][3];
+ (*state)[0][3] = (*state)[1][3];
+ (*state)[1][3] = (*state)[2][3];
+ (*state)[2][3] = (*state)[3][3];
+ (*state)[3][3] = temp;
+}
+#endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
+
+// Cipher is the main function that encrypts the PlainText.
+static void Cipher(state_t* state, const struct AES_ctx* ctx)
+{
+ const uint8_t* RoundKey = GetRoundKey(ctx);
+ uint8_t round = 0;
+
+ // Add the First round key to the state before starting the rounds.
+ AddRoundKey(0, state, RoundKey);
+
+ // There will be Nr rounds. The first Nr-1 rounds are identical. These Nr
+ // rounds are executed in the loop below. Last one without MixColumns()
+ for (round = 1; ; ++round)
+ {
+ SubBytes(state);
+ ShiftRows(state);
+ if (round == ctx->Nr) {
+ break;
+ }
+ MixColumns(state);
+ AddRoundKey(round, state, RoundKey);
+ }
+ // Add round key to last round
+ AddRoundKey(ctx->Nr, state, RoundKey);
+}
+
+#if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
+static void InvCipher(state_t* state, const struct AES_ctx* ctx)
+{
+ const uint8_t* RoundKey = GetRoundKey(ctx);
+ uint8_t round = 0;
+
+ // Add the First round key to the state before starting the rounds.
+ AddRoundKey(ctx->Nr, state, RoundKey);
+
+ // There will be Nr rounds. The first Nr-1 rounds are identical. These Nr
+ // rounds are executed in the loop below. Last one without InvMixColumn()
+ for (round = (ctx->Nr - 1); ; --round)
+ {
+ InvShiftRows(state);
+ InvSubBytes(state);
+ AddRoundKey(round, state, RoundKey);
+ if (round == 0) {
+ break;
+ }
+ InvMixColumns(state);
+ }
+
+}
+#endif // #if (defined(CBC) && CBC == 1) || (defined(ECB) && ECB == 1)
+
+/*****************************************************************************/
+/* Public functions: */
+/*****************************************************************************/
+#if defined(ECB) && (ECB == 1)
+
+
+void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf)
+{
+ // The next function call encrypts the PlainText with the Key using AES
+ // algorithm.
+ Cipher((state_t*)buf, ctx);
+}
+
+void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf)
+{
+ // The next function call decrypts the PlainText with the Key using AES
+ // algorithm.
+ InvCipher((state_t*)buf, ctx);
+}
+
+
+#endif // #if defined(ECB) && (ECB == 1)
+
+
+
+
+
+#if defined(CBC) && (CBC == 1)
+
+
+static void XorWithIv(uint8_t* buf, const uint8_t* Iv)
+{
+ uint8_t i;
+ for (i = 0; i < AES_BLOCKLEN; ++i) // The block in AES is always 128bit no matter the key size
+ {
+ buf[i] ^= Iv[i];
+ }
+}
+
+void AES_CBC_encrypt_buffer(struct AES_ctx *ctx, uint8_t* buf, uint32_t length)
+{
+ uintptr_t i;
+ uint8_t *Iv = ctx->Iv;
+ for (i = 0; i < length; i += AES_BLOCKLEN)
+ {
+ XorWithIv(buf, Iv);
+ Cipher((state_t*)buf, ctx);
+ Iv = buf;
+ buf += AES_BLOCKLEN;
+ }
+ /* store Iv in ctx for next call */
+ memcpy(ctx->Iv, Iv, AES_BLOCKLEN);
+}
+
+void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
+{
+ uintptr_t i;
+ uint8_t storeNextIv[AES_BLOCKLEN];
+ for (i = 0; i < length; i += AES_BLOCKLEN)
+ {
+ memcpy(storeNextIv, buf, AES_BLOCKLEN);
+ InvCipher((state_t*)buf, ctx);
+ XorWithIv(buf, ctx->Iv);
+ memcpy(ctx->Iv, storeNextIv, AES_BLOCKLEN);
+ buf += AES_BLOCKLEN;
+ }
+
+}
+
+#endif // #if defined(CBC) && (CBC == 1)
+
+
+
+#if defined(CTR) && (CTR == 1)
+
+/* Symmetrical operation: same function for encrypting as for decrypting. Note
+any IV/nonce should never be reused with the same key */
+void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length)
+{
+ uint8_t buffer[AES_BLOCKLEN];
+
+ unsigned i;
+ int bi;
+ for (i = 0, bi = AES_BLOCKLEN; i < length; ++i, ++bi)
+ {
+ if (bi == AES_BLOCKLEN) /* we need to regen xor compliment in buffer */
+ {
+ memcpy(buffer, ctx->Iv, AES_BLOCKLEN);
+ Cipher((state_t*)buffer, ctx);
+
+ /* Increment Iv and handle overflow */
+ for (bi = (AES_BLOCKLEN - 1); bi >= 0; --bi)
+ {
+ /* inc will overflow */
+ if (ctx->Iv[bi] == 255)
+ {
+ ctx->Iv[bi] = 0;
+ continue;
+ }
+ ctx->Iv[bi] += 1;
+ break;
+ }
+ bi = 0;
+ }
+
+ buf[i] = (buf[i] ^ buffer[bi]);
+ }
+}
+
+#endif // #if defined(CTR) && (CTR == 1)
diff --git a/shared-module/aesio/aes.h b/shared-module/aesio/aes.h
new file mode 100644
index 000000000..a87fa5be7
--- /dev/null
+++ b/shared-module/aesio/aes.h
@@ -0,0 +1,105 @@
+#ifndef _AES_H_
+#define _AES_H_
+
+#include <stdint.h>
+
+// #define the macros below to 1/0 to enable/disable the mode of operation.
+//
+// CBC enables AES encryption in CBC-mode of operation.
+// CTR enables encryption in counter-mode.
+// ECB enables the basic ECB 16-byte block algorithm. All can be enabled simultaneously.
+
+// The #ifndef-guard allows it to be configured before #include'ing or at compile time.
+#ifndef CBC
+ #define CBC 1
+#endif
+
+#ifndef ECB
+ #define ECB 1
+#endif
+
+#ifndef CTR
+ #define CTR 1
+#endif
+
+
+#define AES128 1
+#define AES192 1
+#define AES256 1
+
+#define AES_BLOCKLEN 16 // Block length in bytes - AES is 128b block only
+
+#if defined(AES256) && (AES256 == 1)
+ #define AES_KEYLEN256 32
+ #define AES_keyExpSize256 240
+#endif
+#if defined(AES192) && (AES192 == 1)
+ #define AES_KEYLEN192 24
+ #define AES_keyExpSize192 208
+#endif
+#if defined(AES128) && (AES128 == 1)
+ #define AES_KEYLEN128 16 // Key length in bytes
+ #define AES_keyExpSize128 176
+#endif
+
+struct AES_ctx
+{
+ union {
+#if defined(AES256) && (AES256 == 1)
+ uint8_t RoundKey256[AES_keyExpSize256];
+#endif
+#if defined(AES192) && (AES192 == 1)
+ uint8_t RoundKey192[AES_keyExpSize192];
+#endif
+#if defined(AES128) && (AES128 == 1)
+ uint8_t RoundKey128[AES_keyExpSize128];
+#endif
+ };
+#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
+ uint8_t Iv[AES_BLOCKLEN];
+#endif
+ uint32_t KeyLength;
+ uint8_t Nr;
+ uint8_t Nk;
+};
+
+void AES_init_ctx(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen);
+#if (defined(CBC) && (CBC == 1)) || (defined(CTR) && (CTR == 1))
+void AES_init_ctx_iv(struct AES_ctx* ctx, const uint8_t* key, uint32_t keylen, const uint8_t* iv);
+void AES_ctx_set_iv(struct AES_ctx* ctx, const uint8_t* iv);
+#endif
+
+#if defined(ECB) && (ECB == 1)
+// buffer size is exactly AES_BLOCKLEN bytes;
+// you need only AES_init_ctx as IV is not used in ECB
+// NB: ECB is considered insecure for most uses
+void AES_ECB_encrypt(const struct AES_ctx* ctx, uint8_t* buf);
+void AES_ECB_decrypt(const struct AES_ctx* ctx, uint8_t* buf);
+
+#endif // #if defined(ECB) && (ECB == !)
+
+
+#if defined(CBC) && (CBC == 1)
+// buffer size MUST be mutile of AES_BLOCKLEN;
+// Suggest https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
+// NOTES: you need to set IV in ctx via AES_init_ctx_iv() or AES_ctx_set_iv()
+// no IV should ever be reused with the same key
+void AES_CBC_encrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+void AES_CBC_decrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+
+#endif // #if defined(CBC) && (CBC == 1)
+
+
+#if defined(CTR) && (CTR == 1)
+
+// Same function for encrypting as for decrypting.
+// IV is incremented for every block, and used after encryption as XOR-compliment for output
+// Suggesting https://en.wikipedia.org/wiki/Padding_(cryptography)#PKCS7 for padding scheme
+// NOTES: you need to set IV in ctx with AES_init_ctx_iv() or AES_ctx_set_iv()
+// no IV should ever be reused with the same key
+void AES_CTR_xcrypt_buffer(struct AES_ctx* ctx, uint8_t* buf, uint32_t length);
+
+#endif // #if defined(CTR) && (CTR == 1)
+
+
+#endif // _AES_H_
diff --git a/shared-module/audiomixer/Mixer.c b/shared-module/audiomixer/Mixer.c
index 4a72dab28..6c1ba1973 100644
--- a/shared-module/audiomixer/Mixer.c
+++ b/shared-module/audiomixer/Mixer.c
@@ -143,19 +143,17 @@ static inline uint32_t pack8(uint32_t val) {
static void mix_down_one_voice(audiomixer_mixer_obj_t* self,
audiomixer_mixervoice_obj_t* voice, bool voices_active,
uint32_t* word_buffer, uint32_t length) {
- bool voice_done = voice->sample == NULL;
- while (!voice_done && length != 0) {
+ while (length != 0) {
if (voice->buffer_length == 0) {
if (!voice->more_data) {
if (voice->loop) {
audiosample_reset_buffer(voice->sample, false, 0);
} else {
voice->sample = NULL;
- voice_done = true;
break;
}
}
- if (!voice_done) {
+ if (voice->sample) {
// Load another buffer
audioio_get_buffer_result_t result = audiosample_get_buffer(voice->sample, false, 0, (uint8_t**) &voice->remaining_buffer, &voice->buffer_length);
// Track length in terms of words.
@@ -230,10 +228,8 @@ static void mix_down_one_voice(audiomixer_mixer_obj_t* self,
}
if (length && !voices_active) {
- uint32_t sample_value = self->bits_per_sample == 8
- ? 0x80808080 : 0x80008000;
for (uint32_t i = 0; i<length; i++) {
- word_buffer[i] = sample_value;
+ word_buffer[i] = 0;
}
}
}
@@ -269,9 +265,16 @@ audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t*
for (int32_t v = 0; v < self->voice_count; v++) {
audiomixer_mixervoice_obj_t* voice = MP_OBJ_TO_PTR(self->voice[v]);
+ if(voice->sample) {
+ mix_down_one_voice(self, voice, voices_active, word_buffer, length);
+ voices_active = true;
+ }
+ }
- mix_down_one_voice(self, voice, voices_active, word_buffer, length);
- voices_active = true;
+ if (!voices_active) {
+ for (uint32_t i = 0; i<length; i++) {
+ word_buffer[i] = 0;
+ }
}
if (!self->samples_signed) {
diff --git a/shared-module/bitbangio/SPI.c b/shared-module/bitbangio/SPI.c
index d02adf34f..e0ff1184f 100644
--- a/shared-module/bitbangio/SPI.c
+++ b/shared-module/bitbangio/SPI.c
@@ -45,7 +45,7 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
}
common_hal_digitalio_digitalinout_switch_to_output(&self->clock, self->polarity == 1, DRIVE_MODE_PUSH_PULL);
- if (mosi != mp_const_none) {
+ if (mosi != NULL) {
result = common_hal_digitalio_digitalinout_construct(&self->mosi, mosi);
if (result != DIGITALINOUT_OK) {
common_hal_digitalio_digitalinout_deinit(&self->clock);
@@ -55,12 +55,12 @@ void shared_module_bitbangio_spi_construct(bitbangio_spi_obj_t *self,
common_hal_digitalio_digitalinout_switch_to_output(&self->mosi, false, DRIVE_MODE_PUSH_PULL);
}
- if (miso != mp_const_none) {
+ if (miso != NULL) {
// Starts out as input by default, no need to change.
result = common_hal_digitalio_digitalinout_construct(&self->miso, miso);
if (result != DIGITALINOUT_OK) {
common_hal_digitalio_digitalinout_deinit(&self->clock);
- if (mosi != mp_const_none) {
+ if (mosi != NULL) {
common_hal_digitalio_digitalinout_deinit(&self->mosi);
}
mp_raise_ValueError(translate("MISO pin init failed."));
diff --git a/shared-module/board/__init__.c b/shared-module/board/__init__.c
index bbf177384..2c9139d8a 100644
--- a/shared-module/board/__init__.c
+++ b/shared-module/board/__init__.c
@@ -103,12 +103,12 @@ mp_obj_t common_hal_board_create_uart(void) {
#ifdef DEFAULT_UART_BUS_RTS
const mcu_pin_obj_t* rts = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RTS);
#else
- const mcu_pin_obj_t* rts = mp_const_none;
+ const mcu_pin_obj_t* rts = NULL;
#endif
#ifdef DEFAULT_UART_BUS_CTS
const mcu_pin_obj_t* cts = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_CTS);
#else
- const mcu_pin_obj_t* cts = mp_const_none;
+ const mcu_pin_obj_t* cts = NULL;
#endif
#ifdef DEFAULT_UART_IS_RS485
const mcu_pin_obj_t* rs485_dir = MP_OBJ_TO_PTR(DEFAULT_UART_BUS_RS485DIR);
@@ -118,12 +118,12 @@ mp_obj_t common_hal_board_create_uart(void) {
const bool rs485_invert = false;
#endif
#else
- const mcu_pin_obj_t* rs485_dir = mp_const_none;
+ const mcu_pin_obj_t* rs485_dir = NULL;
const bool rs485_invert = false;
#endif
common_hal_busio_uart_construct(self, tx, rx, rts, cts, rs485_dir, rs485_invert,
- 9600, 8, PARITY_NONE, 1, 1.0f, 64);
+ 9600, 8, BUSIO_UART_PARITY_NONE, 1, 1.0f, 64, NULL, false);
MP_STATE_VM(shared_uart_bus) = MP_OBJ_FROM_PTR(self);
return MP_STATE_VM(shared_uart_bus);
}
diff --git a/shared-module/displayio/Bitmap.c b/shared-module/displayio/Bitmap.c
index 59971d25c..8bcda6086 100644
--- a/shared-module/displayio/Bitmap.c
+++ b/shared-module/displayio/Bitmap.c
@@ -162,3 +162,24 @@ void displayio_bitmap_finish_refresh(displayio_bitmap_t *self) {
self->dirty_area.x1 = 0;
self->dirty_area.x2 = 0;
}
+
+void common_hal_displayio_bitmap_fill(displayio_bitmap_t *self, uint32_t value) {
+ if (self->read_only) {
+ mp_raise_RuntimeError(translate("Read-only object"));
+ }
+ // Update the dirty area.
+ self->dirty_area.x1 = 0;
+ self->dirty_area.x2 = self->width;
+ self->dirty_area.y1 = 0;
+ self->dirty_area.y2 = self->height;
+
+ // build the packed word
+ uint32_t word = 0;
+ for (uint8_t i=0; i<32 / self->bits_per_value; i++) {
+ word |= (value & self->bitmask) << (32 - ((i+1)*self->bits_per_value));
+ }
+ // copy it in
+ for (uint32_t i=0; i<self->stride * self->height; i++) {
+ self->data[i] = word;
+ }
+}
diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c
index a5d7f0b05..c2c3214aa 100644
--- a/shared-module/displayio/ColorConverter.c
+++ b/shared-module/displayio/ColorConverter.c
@@ -47,9 +47,7 @@ uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888) {
uint32_t r5 = (color_rgb888 >> 19);
uint32_t g6 = (color_rgb888 >> 10) & 0x3f;
uint32_t b5 = (color_rgb888 >> 3) & 0x1f;
- uint32_t packed = r5 << 11 | g6 << 5 | b5;
- // swap bytes
- return __builtin_bswap16(packed);
+ return r5 << 11 | g6 << 5 | b5;
}
uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888) {
@@ -112,7 +110,7 @@ void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *sel
displayio_input_pixel_t input_pixel;
input_pixel.pixel = input_color;
input_pixel.x = input_pixel.y = input_pixel.tile = input_pixel.tile_x = input_pixel.tile_y = 0;
-
+
displayio_output_pixel_t output_pixel;
output_pixel.pixel = 0;
output_pixel.opaque = false;
@@ -132,7 +130,7 @@ bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t*
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) {
uint32_t pixel = input_pixel->pixel;
-
+
if (self->dither){
uint8_t randr = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y));
uint8_t randg = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x+33,input_pixel->tile_y));
@@ -156,7 +154,12 @@ void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d
}
if (colorspace->depth == 16) {
- output_color->pixel = displayio_colorconverter_compute_rgb565(pixel);
+ uint16_t packed = displayio_colorconverter_compute_rgb565(pixel);
+ if (colorspace->reverse_bytes_in_word) {
+ // swap bytes
+ packed = __builtin_bswap16(packed);
+ }
+ output_color->pixel = packed;
output_color->opaque = true;
return;
} else if (colorspace->tricolor) {
@@ -190,4 +193,3 @@ bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self) {
void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self) {
}
-
diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c
index 299c8ad35..a3d877f1a 100644
--- a/shared-module/displayio/Display.c
+++ b/shared-module/displayio/Display.c
@@ -41,16 +41,14 @@
#include <stdint.h>
#include <string.h>
-#include "tick.h"
-
void common_hal_displayio_display_construct(displayio_display_obj_t* self,
mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart,
uint16_t rotation, uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row,
- uint8_t bytes_per_cell, bool reverse_pixels_in_byte, uint8_t set_column_command,
+ uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word, 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 single_byte_bounds, bool data_as_commands, bool auto_refresh, uint16_t native_frames_per_second, bool backlight_on_high) {
// Turn off auto-refresh as we init.
self->auto_refresh = false;
uint16_t ram_width = 0x100;
@@ -60,7 +58,7 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
ram_height = 0xff;
}
displayio_display_core_construct(&self->core, bus, width, height, ram_width, ram_height, colstart, rowstart, rotation,
- color_depth, grayscale, pixels_in_byte_share_row, bytes_per_cell, reverse_pixels_in_byte);
+ color_depth, grayscale, pixels_in_byte_share_row, bytes_per_cell, reverse_pixels_in_byte, reverse_bytes_in_word);
self->set_column_command = set_column_command;
self->set_row_command = set_row_command;
@@ -69,6 +67,7 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
self->auto_brightness = auto_brightness;
self->first_manual_refresh = !auto_refresh;
self->data_as_commands = data_as_commands;
+ self->backlight_on_high = backlight_on_high;
self->native_frames_per_second = native_frames_per_second;
self->native_ms_per_frame = 1000 / native_frames_per_second;
@@ -110,6 +109,8 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
// Always set the backlight type in case we're reusing memory.
self->backlight_inout.base.type = &mp_type_NoneType;
if (backlight_pin != NULL && common_hal_mcu_pin_is_free(backlight_pin)) {
+ // Avoid PWM types and functions when the module isn't enabled
+ #if (CIRCUITPY_PULSEIO)
pwmout_result_t result = common_hal_pulseio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, 50000, false);
if (result != PWMOUT_OK) {
self->backlight_inout.base.type = &digitalio_digitalinout_type;
@@ -119,6 +120,12 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
self->backlight_pwm.base.type = &pulseio_pwmout_type;
common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm);
}
+ #else
+ // Otherwise default to digital
+ self->backlight_inout.base.type = &digitalio_digitalinout_type;
+ common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin);
+ common_hal_never_reset_pin(backlight_pin);
+ #endif
}
if (!self->auto_brightness && (self->backlight_inout.base.type != &mp_type_NoneType ||
brightness_command != NO_BRIGHTNESS_COMMAND)) {
@@ -159,10 +166,25 @@ mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t*
bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness) {
self->updating_backlight = true;
+ if (!self->backlight_on_high){
+ brightness = 1.0-brightness;
+ }
bool ok = false;
- if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
+
+ // Avoid PWM types and functions when the module isn't enabled
+ #if (CIRCUITPY_PULSEIO)
+ bool ispwm = (self->backlight_pwm.base.type == &pulseio_pwmout_type) ? true : false;
+ #else
+ bool ispwm = false;
+ #endif
+
+ if (ispwm) {
+ #if (CIRCUITPY_PULSEIO)
common_hal_pulseio_pwmout_set_duty_cycle(&self->backlight_pwm, (uint16_t) (0xffff * brightness));
ok = true;
+ #else
+ ok = false;
+ #endif
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99);
ok = true;
@@ -388,12 +410,16 @@ void displayio_display_background(displayio_display_obj_t* self) {
void release_display(displayio_display_obj_t* self) {
release_display_core(&self->core);
+ #if (CIRCUITPY_PULSEIO)
if (self->backlight_pwm.base.type == &pulseio_pwmout_type) {
common_hal_pulseio_pwmout_reset_ok(&self->backlight_pwm);
common_hal_pulseio_pwmout_deinit(&self->backlight_pwm);
} else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) {
common_hal_digitalio_digitalinout_deinit(&self->backlight_inout);
}
+ #else
+ common_hal_digitalio_digitalinout_deinit(&self->backlight_inout);
+ #endif
}
void reset_display(displayio_display_obj_t* self) {
diff --git a/shared-module/displayio/Display.h b/shared-module/displayio/Display.h
index e5fe0a708..861a38fd7 100644
--- a/shared-module/displayio/Display.h
+++ b/shared-module/displayio/Display.h
@@ -29,7 +29,9 @@
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/displayio/Group.h"
+#if CIRCUITPY_PULSEIO
#include "shared-bindings/pulseio/PWMOut.h"
+#endif
#include "shared-module/displayio/area.h"
#include "shared-module/displayio/display_core.h"
@@ -39,7 +41,9 @@ typedef struct {
displayio_display_core_t core;
union {
digitalio_digitalinout_obj_t backlight_inout;
+ #if CIRCUITPY_PULSEIO
pulseio_pwmout_obj_t backlight_pwm;
+ #endif
};
uint64_t last_backlight_refresh;
uint64_t last_refresh_call;
@@ -55,6 +59,7 @@ typedef struct {
bool data_as_commands;
bool auto_brightness;
bool updating_backlight;
+ bool backlight_on_high;
} displayio_display_obj_t;
void displayio_display_background(displayio_display_obj_t* self);
diff --git a/shared-module/displayio/EPaperDisplay.c b/shared-module/displayio/EPaperDisplay.c
index 91d4bb7f9..6a55687b9 100644
--- a/shared-module/displayio/EPaperDisplay.c
+++ b/shared-module/displayio/EPaperDisplay.c
@@ -42,8 +42,6 @@
#include <stdint.h>
#include <string.h>
-#include "tick.h"
-
void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t* self,
mp_obj_t bus, uint8_t* start_sequence, uint16_t start_sequence_len, uint8_t* stop_sequence, uint16_t stop_sequence_len,
uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height,
@@ -58,7 +56,7 @@ void common_hal_displayio_epaperdisplay_construct(displayio_epaperdisplay_obj_t*
self->core.colorspace.tricolor_luma = displayio_colorconverter_compute_luma(highlight_color);
}
- displayio_display_core_construct(&self->core, bus, width, height, ram_width, ram_height, colstart, rowstart, rotation, 1, true, true, 1, true);
+ displayio_display_core_construct(&self->core, bus, width, height, ram_width, ram_height, colstart, rowstart, rotation, 1, true, true, 1, true, true);
self->set_column_window_command = set_column_window_command;
self->set_row_window_command = set_row_window_command;
diff --git a/shared-module/displayio/EPaperDisplay.h b/shared-module/displayio/EPaperDisplay.h
index d08bed546..3b9f6e368 100644
--- a/shared-module/displayio/EPaperDisplay.h
+++ b/shared-module/displayio/EPaperDisplay.h
@@ -29,7 +29,6 @@
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/displayio/Group.h"
-#include "shared-bindings/pulseio/PWMOut.h"
#include "shared-module/displayio/area.h"
#include "shared-module/displayio/display_core.h"
diff --git a/shared-module/displayio/FourWire.c b/shared-module/displayio/FourWire.c
index 256c29642..17e8cfa1d 100644
--- a/shared-module/displayio/FourWire.c
+++ b/shared-module/displayio/FourWire.c
@@ -36,11 +36,10 @@
#include "shared-bindings/time/__init__.h"
#include "shared-module/displayio/display_core.h"
-#include "tick.h"
-
void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
busio_spi_obj_t* spi, const mcu_pin_obj_t* command,
- const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint32_t baudrate) {
+ const mcu_pin_obj_t* chip_select, const mcu_pin_obj_t* reset, uint32_t baudrate,
+ uint8_t polarity, uint8_t phase) {
self->bus = spi;
common_hal_busio_spi_never_reset(self->bus);
@@ -49,8 +48,8 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self,
gc_never_free(self->bus);
self->frequency = baudrate;
- self->polarity = 0;
- self->phase = 0;
+ self->polarity = polarity;
+ self->phase = phase;
common_hal_digitalio_digitalinout_construct(&self->command, command);
common_hal_digitalio_digitalinout_switch_to_output(&self->command, true, DRIVE_MODE_PUSH_PULL);
diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c
index d69e9f585..15cf5b8e4 100644
--- a/shared-module/displayio/Group.c
+++ b/shared-module/displayio/Group.c
@@ -29,6 +29,11 @@
#include "py/runtime.h"
#include "shared-bindings/displayio/TileGrid.h"
+#if CIRCUITPY_VECTORIO
+#include "shared-bindings/vectorio/VectorShape.h"
+#endif
+
+
void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y) {
displayio_group_child_t* children = m_new(displayio_group_child_t, max_size);
displayio_group_construct(self, children, max_size, scale, x, y);
@@ -117,6 +122,12 @@ static void _update_child_transforms(displayio_group_t* self) {
}
for (size_t i = 0; i < self->size; i++) {
mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ vectorio_vector_shape_update_transform(layer, &self->absolute_transform);
+ }
+ else
+#endif
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
displayio_tilegrid_update_transform(layer, &self->absolute_transform);
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
@@ -200,7 +211,15 @@ void common_hal_displayio_group_set_y(displayio_group_t* self, mp_int_t y) {
}
static mp_obj_t _add_layer(displayio_group_t* self, mp_obj_t layer) {
- mp_obj_t native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
+ mp_obj_t native_layer;
+#if CIRCUITPY_VECTORIO
+ native_layer = mp_instance_cast_to_native_base(layer, &vectorio_vector_shape_type);
+ if (native_layer != MP_OBJ_NULL) {
+ vectorio_vector_shape_update_transform(native_layer, &self->absolute_transform);
+ return native_layer;
+ }
+#endif
+ native_layer = mp_instance_cast_to_native_base(layer, &displayio_group_type);
if (native_layer == MP_OBJ_NULL) {
native_layer = mp_instance_cast_to_native_base(layer, &displayio_tilegrid_type);
if (native_layer == MP_OBJ_NULL) {
@@ -229,6 +248,14 @@ static void _remove_layer(displayio_group_t* self, size_t index) {
mp_obj_t layer = self->children[index].native;
displayio_area_t layer_area;
bool rendered_last_frame = false;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ bool has_dirty_area = vectorio_vector_shape_get_dirty_area(layer, &layer_area);
+ rendered_last_frame = has_dirty_area;
+ vectorio_vector_shape_update_transform(layer, NULL);
+ }
+ else
+#endif
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
displayio_tilegrid_t* tilegrid = layer;
rendered_last_frame = displayio_tilegrid_get_previous_area(tilegrid, &layer_area);
@@ -317,6 +344,15 @@ bool displayio_group_fill_area(displayio_group_t *self, const _displayio_colorsp
bool full_coverage = false;
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ if (vectorio_vector_shape_fill_area(layer, colorspace, area, mask, buffer)) {
+ full_coverage = true;
+ break;
+ }
+ }
+ else
+#endif
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
if (displayio_tilegrid_fill_area(layer, colorspace, area, mask, buffer)) {
full_coverage = true;
@@ -336,6 +372,12 @@ void displayio_group_finish_refresh(displayio_group_t *self) {
self->item_removed = false;
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ vectorio_vector_shape_finish_refresh(layer);
+ }
+ else
+#endif
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
displayio_tilegrid_finish_refresh(layer);
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
@@ -352,6 +394,12 @@ displayio_area_t* displayio_group_get_refresh_areas(displayio_group_t *self, dis
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i].native;
+#if CIRCUITPY_VECTORIO
+ if (MP_OBJ_IS_TYPE(layer, &vectorio_vector_shape_type)) {
+ tail = vectorio_vector_shape_get_refresh_areas(layer, tail);
+ }
+ else
+#endif
if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
tail = displayio_tilegrid_get_refresh_areas(layer, tail);
} else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
diff --git a/shared-module/displayio/I2CDisplay.c b/shared-module/displayio/I2CDisplay.c
index 280476f19..0c8f2e69a 100644
--- a/shared-module/displayio/I2CDisplay.c
+++ b/shared-module/displayio/I2CDisplay.c
@@ -38,8 +38,6 @@
#include "shared-bindings/time/__init__.h"
#include "shared-module/displayio/display_core.h"
-#include "tick.h"
-
void common_hal_displayio_i2cdisplay_construct(displayio_i2cdisplay_obj_t* self,
busio_i2c_obj_t* i2c, uint16_t device_address, const mcu_pin_obj_t* reset) {
diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c
index 3bce86f48..facb1fa73 100644
--- a/shared-module/displayio/Palette.c
+++ b/shared-module/displayio/Palette.c
@@ -35,10 +35,12 @@ void common_hal_displayio_palette_construct(displayio_palette_t* self, uint16_t
void common_hal_displayio_palette_make_opaque(displayio_palette_t* self, uint32_t palette_index) {
self->colors[palette_index].transparent = false;
+ self->needs_refresh = true;
}
void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t palette_index) {
self->colors[palette_index].transparent = true;
+ self->needs_refresh = true;
}
uint32_t common_hal_displayio_palette_get_len(displayio_palette_t* self) {
@@ -83,7 +85,12 @@ bool displayio_palette_get_color(displayio_palette_t *self, const _displayio_col
} else if (colorspace->grayscale) {
*color = self->colors[palette_index].luma >> (8 - colorspace->depth);
} else {
- *color = self->colors[palette_index].rgb565;
+ uint16_t packed = self->colors[palette_index].rgb565;
+ if (colorspace->reverse_bytes_in_word) {
+ // swap bytes
+ packed = __builtin_bswap16(packed);
+ }
+ *color = packed;
}
return true;
diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h
index 758fd43fc..da72f250f 100644
--- a/shared-module/displayio/Palette.h
+++ b/shared-module/displayio/Palette.h
@@ -41,6 +41,7 @@ typedef struct {
bool tricolor;
bool pixels_in_byte_share_row;
bool reverse_pixels_in_byte;
+ bool reverse_bytes_in_word;
bool dither;
} _displayio_colorspace_t;
diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c
index c4855c333..2766cbecd 100644
--- a/shared-module/displayio/TileGrid.c
+++ b/shared-module/displayio/TileGrid.c
@@ -435,7 +435,7 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c
} else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
input_pixel.pixel = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y);
}
-
+
output_pixel.opaque = true;
if (self->pixel_shader == mp_const_none) {
output_pixel.pixel = input_pixel.pixel;
diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c
index efa61265e..c898bbb98 100644
--- a/shared-module/displayio/__init__.c
+++ b/shared-module/displayio/__init__.c
@@ -21,6 +21,20 @@
primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT];
+#if CIRCUITPY_RGBMATRIX
+STATIC bool any_display_uses_this_rgbmatrix(rgbmatrix_rgbmatrix_obj_t* pm) {
+ for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+ if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
+ framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display;
+ if (display->framebuffer == pm) {
+ return true;
+ }
+ }
+ }
+ return false;
+}
+#endif
+
// Check for recursive calls to displayio_background.
bool displayio_background_in_progress = false;
@@ -47,6 +61,10 @@ void displayio_background(void) {
}
if (displays[i].display.base.type == &displayio_display_type) {
displayio_display_background(&displays[i].display);
+#if CIRCUITPY_FRAMEBUFFERIO
+ } else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
+ framebufferio_framebufferdisplay_background(&displays[i].framebuffer_display);
+#endif
} else if (displays[i].epaper_display.base.type == &displayio_epaperdisplay_type) {
displayio_epaperdisplay_background(&displays[i].epaper_display);
}
@@ -67,6 +85,10 @@ void common_hal_displayio_release_displays(void) {
release_display(&displays[i].display);
} else if (display_type == &displayio_epaperdisplay_type) {
release_epaperdisplay(&displays[i].epaper_display);
+#if CIRCUITPY_FRAMEBUFFERIO
+ } else if (display_type == &framebufferio_framebufferdisplay_type) {
+ release_framebufferdisplay(&displays[i].framebuffer_display);
+#endif
}
displays[i].display.base.type = &mp_type_NoneType;
}
@@ -80,6 +102,10 @@ void common_hal_displayio_release_displays(void) {
common_hal_displayio_i2cdisplay_deinit(&displays[i].i2cdisplay_bus);
} else if (bus_type == &displayio_parallelbus_type) {
common_hal_displayio_parallelbus_deinit(&displays[i].parallel_bus);
+#if CIRCUITPY_FRAMEBUFFERIO
+ } else if (bus_type == &rgbmatrix_RGBMatrix_type) {
+ common_hal_rgbmatrix_rgbmatrix_deinit(&displays[i].rgbmatrix);
+#endif
}
displays[i].fourwire_bus.base.type = &mp_type_NoneType;
}
@@ -141,6 +167,13 @@ void reset_displays(void) {
}
}
}
+#if CIRCUITPY_RGBMATRIX
+ } else if (displays[i].rgbmatrix.base.type == &rgbmatrix_RGBMatrix_type) {
+ rgbmatrix_rgbmatrix_obj_t * pm = &displays[i].rgbmatrix;
+ if(!any_display_uses_this_rgbmatrix(pm)) {
+ common_hal_rgbmatrix_rgbmatrix_deinit(pm);
+ }
+#endif
} else {
// Not an active display bus.
continue;
@@ -155,12 +188,24 @@ void reset_displays(void) {
} else if (displays[i].epaper_display.base.type == &displayio_epaperdisplay_type) {
displayio_epaperdisplay_obj_t* display = &displays[i].epaper_display;
common_hal_displayio_epaperdisplay_show(display, NULL);
+#if CIRCUITPY_FRAMEBUFFERIO
+ } else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
+ framebufferio_framebufferdisplay_obj_t* display = &displays[i].framebuffer_display;
+ display->auto_refresh = true;
+ common_hal_framebufferio_framebufferdisplay_show(display, NULL);
+#endif
}
}
}
void displayio_gc_collect(void) {
for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+#if CIRCUITPY_RGBMATRIX
+ if (displays[i].rgbmatrix.base.type == &rgbmatrix_RGBMatrix_type) {
+ rgbmatrix_rgbmatrix_collect_ptrs(&displays[i].rgbmatrix);
+ }
+#endif
+
if (displays[i].display.base.type == NULL) {
continue;
}
@@ -169,6 +214,10 @@ void displayio_gc_collect(void) {
// but this is more precise, and is the only field that needs marking.
if (displays[i].display.base.type == &displayio_display_type) {
displayio_display_collect_ptrs(&displays[i].display);
+#if CIRCUITPY_FRAMEBUFFERIO
+ } else if (displays[i].framebuffer_display.base.type == &framebufferio_framebufferdisplay_type) {
+ framebufferio_framebufferdisplay_collect_ptrs(&displays[i].framebuffer_display);
+#endif
} else if (displays[i].epaper_display.base.type == &displayio_epaperdisplay_type) {
displayio_epaperdisplay_collect_ptrs(&displays[i].epaper_display);
}
@@ -308,3 +357,38 @@ void displayio_area_transform_within(bool mirror_x, bool mirror_y, bool transpos
transformed->x1 = whole->x1 + (y1 - whole->y1);
}
}
+
+primary_display_t *allocate_display(void) {
+ for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+ mp_const_obj_t display_type = displays[i].display.base.type;
+ if (display_type == NULL || display_type == &mp_type_NoneType) {
+ return &displays[i];
+ }
+ }
+ return NULL;
+}
+
+primary_display_t *allocate_display_or_raise(void) {
+ primary_display_t *result = allocate_display();
+ if (result) {
+ return result;
+ }
+ mp_raise_RuntimeError(translate("Too many displays"));
+}
+primary_display_t *allocate_display_bus(void) {
+ for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) {
+ mp_const_obj_t display_type = displays[i].display.base.type;
+ if (display_type == NULL || display_type == &mp_type_NoneType) {
+ return &displays[i];
+ }
+ }
+ return NULL;
+}
+
+primary_display_t *allocate_display_bus_or_raise(void) {
+ primary_display_t *result = allocate_display_bus();
+ if (result) {
+ return result;
+ }
+ mp_raise_RuntimeError(translate("Too many display busses"));
+}
diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h
index e78bc61ce..9eb10c28b 100644
--- a/shared-module/displayio/__init__.h
+++ b/shared-module/displayio/__init__.h
@@ -29,20 +29,30 @@
#include "shared-bindings/displayio/Display.h"
#include "shared-bindings/displayio/EPaperDisplay.h"
+#if CIRCUITPY_FRAMEBUFFERIO
+#include "shared-bindings/framebufferio/FramebufferDisplay.h"
+#endif
#include "shared-bindings/displayio/FourWire.h"
#include "shared-bindings/displayio/Group.h"
#include "shared-bindings/displayio/I2CDisplay.h"
#include "shared-bindings/displayio/ParallelBus.h"
+#include "shared-bindings/rgbmatrix/RGBMatrix.h"
typedef struct {
union {
displayio_fourwire_obj_t fourwire_bus;
displayio_i2cdisplay_obj_t i2cdisplay_bus;
displayio_parallelbus_obj_t parallel_bus;
+#if CIRCUITPY_RGBMATRIX
+ rgbmatrix_rgbmatrix_obj_t rgbmatrix;
+#endif
};
union {
displayio_display_obj_t display;
displayio_epaperdisplay_obj_t epaper_display;
+#if CIRCUITPY_FRAMEBUFFERIO
+ framebufferio_framebufferdisplay_obj_t framebuffer_display;
+#endif
};
} primary_display_t;
@@ -54,4 +64,9 @@ void displayio_background(void);
void reset_displays(void);
void displayio_gc_collect(void);
+primary_display_t *allocate_display(void);
+primary_display_t *allocate_display_or_raise(void);
+primary_display_t *allocate_display_bus(void);
+primary_display_t *allocate_display_bus_or_raise(void);
+
#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO___INIT___H
diff --git a/shared-module/displayio/display_core.c b/shared-module/displayio/display_core.c
index 120b70f76..43f2d1937 100644
--- a/shared-module/displayio/display_core.c
+++ b/shared-module/displayio/display_core.c
@@ -40,42 +40,47 @@
#include <stdint.h>
#include <string.h>
-#include "tick.h"
+#define DISPLAYIO_CORE_DEBUG(...) (void)0
+// #define DISPLAYIO_CORE_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
void displayio_display_core_construct(displayio_display_core_t* self,
mp_obj_t bus, uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height, int16_t colstart, int16_t rowstart, uint16_t rotation,
- uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte) {
+ uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word) {
self->colorspace.depth = color_depth;
self->colorspace.grayscale = grayscale;
self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row;
self->colorspace.bytes_per_cell = bytes_per_cell;
self->colorspace.reverse_pixels_in_byte = reverse_pixels_in_byte;
+ self->colorspace.reverse_bytes_in_word = reverse_bytes_in_word;
self->colorspace.dither = false;
self->current_group = NULL;
self->colstart = colstart;
self->rowstart = rowstart;
self->last_refresh = 0;
- if (MP_OBJ_IS_TYPE(bus, &displayio_parallelbus_type)) {
- self->bus_reset = common_hal_displayio_parallelbus_reset;
- self->bus_free = common_hal_displayio_parallelbus_bus_free;
- self->begin_transaction = common_hal_displayio_parallelbus_begin_transaction;
- self->send = common_hal_displayio_parallelbus_send;
- self->end_transaction = common_hal_displayio_parallelbus_end_transaction;
- } else if (MP_OBJ_IS_TYPE(bus, &displayio_fourwire_type)) {
- self->bus_reset = common_hal_displayio_fourwire_reset;
- self->bus_free = common_hal_displayio_fourwire_bus_free;
- self->begin_transaction = common_hal_displayio_fourwire_begin_transaction;
- self->send = common_hal_displayio_fourwire_send;
- self->end_transaction = common_hal_displayio_fourwire_end_transaction;
- } else if (MP_OBJ_IS_TYPE(bus, &displayio_i2cdisplay_type)) {
- self->bus_reset = common_hal_displayio_i2cdisplay_reset;
- self->bus_free = common_hal_displayio_i2cdisplay_bus_free;
- self->begin_transaction = common_hal_displayio_i2cdisplay_begin_transaction;
- self->send = common_hal_displayio_i2cdisplay_send;
- self->end_transaction = common_hal_displayio_i2cdisplay_end_transaction;
- } else {
- mp_raise_ValueError(translate("Unsupported display bus type"));
+ // (framebufferdisplay already validated its 'bus' is a buffer-protocol object)
+ if (bus) {
+ if (MP_OBJ_IS_TYPE(bus, &displayio_parallelbus_type)) {
+ self->bus_reset = common_hal_displayio_parallelbus_reset;
+ self->bus_free = common_hal_displayio_parallelbus_bus_free;
+ self->begin_transaction = common_hal_displayio_parallelbus_begin_transaction;
+ self->send = common_hal_displayio_parallelbus_send;
+ self->end_transaction = common_hal_displayio_parallelbus_end_transaction;
+ } else if (MP_OBJ_IS_TYPE(bus, &displayio_fourwire_type)) {
+ self->bus_reset = common_hal_displayio_fourwire_reset;
+ self->bus_free = common_hal_displayio_fourwire_bus_free;
+ self->begin_transaction = common_hal_displayio_fourwire_begin_transaction;
+ self->send = common_hal_displayio_fourwire_send;
+ self->end_transaction = common_hal_displayio_fourwire_end_transaction;
+ } else if (MP_OBJ_IS_TYPE(bus, &displayio_i2cdisplay_type)) {
+ self->bus_reset = common_hal_displayio_i2cdisplay_reset;
+ self->bus_free = common_hal_displayio_i2cdisplay_bus_free;
+ self->begin_transaction = common_hal_displayio_i2cdisplay_begin_transaction;
+ self->send = common_hal_displayio_i2cdisplay_send;
+ self->end_transaction = common_hal_displayio_i2cdisplay_end_transaction;
+ } else {
+ mp_raise_ValueError(translate("Unsupported display bus type"));
+ }
}
self->bus = bus;
@@ -296,6 +301,7 @@ void displayio_display_core_start_refresh(displayio_display_core_t* self) {
void displayio_display_core_finish_refresh(displayio_display_core_t* self) {
if (self->current_group != NULL) {
+ DISPLAYIO_CORE_DEBUG("displayiocore group_finish_refresh\n");
displayio_group_finish_refresh(self->current_group);
}
self->full_refresh = false;
diff --git a/shared-module/displayio/display_core.h b/shared-module/displayio/display_core.h
index 1c3d0f09c..e4fd62d4a 100644
--- a/shared-module/displayio/display_core.h
+++ b/shared-module/displayio/display_core.h
@@ -58,7 +58,7 @@ typedef struct {
void displayio_display_core_construct(displayio_display_core_t* self,
mp_obj_t bus, uint16_t width, uint16_t height, uint16_t ram_width, uint16_t ram_height, int16_t colstart, int16_t rowstart, uint16_t rotation,
- uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte);
+ uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t bytes_per_cell, bool reverse_pixels_in_byte, bool reverse_bytes_in_word);
bool displayio_display_core_show(displayio_display_core_t* self, displayio_group_t* root_group);
diff --git a/shared-module/framebufferio/FramebufferDisplay.c b/shared-module/framebufferio/FramebufferDisplay.c
new file mode 100644
index 000000000..f296da409
--- /dev/null
+++ b/shared-module/framebufferio/FramebufferDisplay.c
@@ -0,0 +1,312 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ * 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/framebufferio/FramebufferDisplay.h"
+
+#include "py/gc.h"
+#include "py/runtime.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "shared-bindings/time/__init__.h"
+#include "shared-module/displayio/__init__.h"
+#include "shared-module/displayio/display_core.h"
+#include "supervisor/shared/display.h"
+#include "supervisor/shared/tick.h"
+#include "supervisor/usb.h"
+
+#include <stdint.h>
+#include <string.h>
+
+void common_hal_framebufferio_framebufferdisplay_construct(framebufferio_framebufferdisplay_obj_t* self,
+ mp_obj_t framebuffer,
+ uint16_t rotation,
+ bool auto_refresh) {
+ // Turn off auto-refresh as we init.
+ self->auto_refresh = false;
+ self->framebuffer = framebuffer;
+ self->framebuffer_protocol = mp_proto_get_or_throw(MP_QSTR_protocol_framebuffer, framebuffer);
+
+ uint16_t ram_width = 0x100;
+ uint16_t ram_height = 0x100;
+
+ displayio_display_core_construct(
+ &self->core,
+ NULL,
+ self->framebuffer_protocol->get_width(self->framebuffer),
+ self->framebuffer_protocol->get_height(self->framebuffer),
+ ram_width,
+ ram_height,
+ 0,
+ 0,
+ rotation,
+ self->framebuffer_protocol->get_color_depth(self->framebuffer),
+ false,
+ false,
+ self->framebuffer_protocol->get_bytes_per_cell(self->framebuffer),
+ false,
+ false);
+
+ self->first_manual_refresh = !auto_refresh;
+
+ self->native_frames_per_second = self->framebuffer_protocol->get_native_frames_per_second(self->framebuffer);
+ self->native_ms_per_frame = 1000 / self->native_frames_per_second;
+
+ supervisor_start_terminal(self->core.width, self->core.height);
+
+ // Set the group after initialization otherwise we may send pixels while we delay in
+ // initialization.
+ common_hal_framebufferio_framebufferdisplay_show(self, &circuitpython_splash);
+ self->auto_refresh = auto_refresh;
+}
+
+bool common_hal_framebufferio_framebufferdisplay_show(framebufferio_framebufferdisplay_obj_t* self, displayio_group_t* root_group) {
+ return displayio_display_core_show(&self->core, root_group);
+}
+
+uint16_t common_hal_framebufferio_framebufferdisplay_get_width(framebufferio_framebufferdisplay_obj_t* self){
+ return displayio_display_core_get_width(&self->core);
+}
+
+uint16_t common_hal_framebufferio_framebufferdisplay_get_height(framebufferio_framebufferdisplay_obj_t* self){
+ return displayio_display_core_get_height(&self->core);
+}
+
+bool common_hal_framebufferio_framebufferdisplay_get_auto_brightness(framebufferio_framebufferdisplay_obj_t* self) {
+ if (self->framebuffer_protocol->get_auto_brightness) {
+ return self->framebuffer_protocol->get_auto_brightness(self->framebuffer);
+ }
+ return true;
+}
+
+bool common_hal_framebufferio_framebufferdisplay_set_auto_brightness(framebufferio_framebufferdisplay_obj_t* self, bool auto_brightness) {
+ if (self->framebuffer_protocol->set_auto_brightness) {
+ return self->framebuffer_protocol->set_auto_brightness(self->framebuffer, auto_brightness);
+ }
+ return false;
+}
+
+mp_float_t common_hal_framebufferio_framebufferdisplay_get_brightness(framebufferio_framebufferdisplay_obj_t* self) {
+ if (self->framebuffer_protocol->set_brightness) {
+ return self->framebuffer_protocol->get_brightness(self->framebuffer);
+ }
+ return -1;
+}
+
+bool common_hal_framebufferio_framebufferdisplay_set_brightness(framebufferio_framebufferdisplay_obj_t* self, mp_float_t brightness) {
+ bool ok = false;
+ if (self->framebuffer_protocol->set_brightness) {
+ self->framebuffer_protocol->set_brightness(self->framebuffer, brightness);
+ ok = true;
+ }
+ return ok;
+}
+
+mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebufferio_framebufferdisplay_obj_t* self) {
+ return self->framebuffer;
+}
+
+STATIC const displayio_area_t* _get_refresh_areas(framebufferio_framebufferdisplay_obj_t *self) {
+ if (self->core.full_refresh) {
+ self->core.area.next = NULL;
+ return &self->core.area;
+ } else if (self->core.current_group != NULL) {
+ return displayio_group_get_refresh_areas(self->core.current_group, NULL);
+ }
+ return NULL;
+}
+
+STATIC bool _refresh_area(framebufferio_framebufferdisplay_obj_t* self, const displayio_area_t* area) {
+ uint16_t buffer_size = 128; // In uint32_ts
+
+ displayio_area_t clipped;
+ // Clip the area to the display by overlapping the areas. If there is no overlap then we're done.
+ if (!displayio_display_core_clip_area(&self->core, area, &clipped)) {
+ return true;
+ }
+ uint16_t subrectangles = 1;
+ uint16_t rows_per_buffer = displayio_area_height(&clipped);
+ uint8_t pixels_per_word = (sizeof(uint32_t) * 8) / self->core.colorspace.depth;
+ uint16_t pixels_per_buffer = displayio_area_size(&clipped);
+ if (displayio_area_size(&clipped) > buffer_size * pixels_per_word) {
+ rows_per_buffer = buffer_size * pixels_per_word / displayio_area_width(&clipped);
+ if (rows_per_buffer == 0) {
+ rows_per_buffer = 1;
+ }
+ // If pixels are packed by column then ensure rows_per_buffer is on a byte boundary.
+ if (self->core.colorspace.depth < 8 && !self->core.colorspace.pixels_in_byte_share_row) {
+ uint8_t pixels_per_byte = 8 / self->core.colorspace.depth;
+ if (rows_per_buffer % pixels_per_byte != 0) {
+ rows_per_buffer -= rows_per_buffer % pixels_per_byte;
+ }
+ }
+ subrectangles = displayio_area_height(&clipped) / rows_per_buffer;
+ if (displayio_area_height(&clipped) % rows_per_buffer != 0) {
+ subrectangles++;
+ }
+ pixels_per_buffer = rows_per_buffer * displayio_area_width(&clipped);
+ buffer_size = pixels_per_buffer / pixels_per_word;
+ if (pixels_per_buffer % pixels_per_word) {
+ buffer_size += 1;
+ }
+ }
+
+ // Allocated and shared as a uint32_t array so the compiler knows the
+ // alignment everywhere.
+ uint32_t buffer[buffer_size];
+ uint32_t mask_length = (pixels_per_buffer / 32) + 1;
+ uint32_t mask[mask_length];
+ uint16_t remaining_rows = displayio_area_height(&clipped);
+
+ for (uint16_t j = 0; j < subrectangles; j++) {
+ displayio_area_t subrectangle = {
+ .x1 = clipped.x1,
+ .y1 = clipped.y1 + rows_per_buffer * j,
+ .x2 = clipped.x2,
+ .y2 = clipped.y1 + rows_per_buffer * (j + 1)
+ };
+ if (remaining_rows < rows_per_buffer) {
+ subrectangle.y2 = subrectangle.y1 + remaining_rows;
+ }
+ remaining_rows -= rows_per_buffer;
+
+ memset(mask, 0, mask_length * sizeof(mask[0]));
+ memset(buffer, 0, buffer_size * sizeof(buffer[0]));
+
+ displayio_display_core_fill_area(&self->core, &subrectangle, mask, buffer);
+
+ // COULDDO: this arithmetic only supports multiple-of-8 bpp
+ uint8_t *dest = self->bufinfo.buf + (subrectangle.y1 * self->core.width + subrectangle.x1) * (self->core.colorspace.depth / 8);
+ uint8_t *src = (uint8_t*)buffer;
+ size_t rowsize = (subrectangle.x2 - subrectangle.x1) * (self->core.colorspace.depth / 8);
+ size_t rowstride = self->core.width * (self->core.colorspace.depth/8);
+ for (uint16_t i = subrectangle.y1; i < subrectangle.y2; i++) {
+ memcpy(dest, src, rowsize);
+ dest += rowstride;
+ src += rowsize;
+ }
+
+ // TODO(tannewt): Make refresh displays faster so we don't starve other
+ // background tasks.
+ usb_background();
+ }
+ return true;
+}
+
+STATIC void _refresh_display(framebufferio_framebufferdisplay_obj_t* self) {
+ displayio_display_core_start_refresh(&self->core);
+ self->framebuffer_protocol->get_bufinfo(self->framebuffer, &self->bufinfo);
+ const displayio_area_t* current_area = _get_refresh_areas(self);
+ while (current_area != NULL) {
+ _refresh_area(self, current_area);
+ current_area = current_area->next;
+ }
+ displayio_display_core_finish_refresh(&self->core);
+ self->framebuffer_protocol->swapbuffers(self->framebuffer);
+}
+
+void common_hal_framebufferio_framebufferdisplay_set_rotation(framebufferio_framebufferdisplay_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);
+ 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_framebufferio_framebufferdisplay_get_rotation(framebufferio_framebufferdisplay_obj_t* self){
+ return self->core.rotation;
+}
+
+
+bool common_hal_framebufferio_framebufferdisplay_refresh(framebufferio_framebufferdisplay_obj_t* self, uint32_t target_ms_per_frame, uint32_t maximum_ms_per_real_frame) {
+ if (!self->auto_refresh && !self->first_manual_refresh) {
+ uint64_t current_time = supervisor_ticks_ms64();
+ uint32_t current_ms_since_real_refresh = current_time - self->core.last_refresh;
+ // Test to see if the real frame time is below our minimum.
+ if (current_ms_since_real_refresh > maximum_ms_per_real_frame) {
+ mp_raise_RuntimeError(translate("Below minimum frame rate"));
+ }
+ uint32_t current_ms_since_last_call = current_time - self->last_refresh_call;
+ self->last_refresh_call = current_time;
+ // Skip the actual refresh to help catch up.
+ if (current_ms_since_last_call > target_ms_per_frame) {
+ return false;
+ }
+ uint32_t remaining_time = target_ms_per_frame - (current_ms_since_real_refresh % target_ms_per_frame);
+ // We're ahead of the game so wait until we align with the frame rate.
+ while (supervisor_ticks_ms64() - self->last_refresh_call < remaining_time) {
+ RUN_BACKGROUND_TASKS;
+ }
+ }
+ self->first_manual_refresh = false;
+ _refresh_display(self);
+ return true;
+}
+
+bool common_hal_framebufferio_framebufferdisplay_get_auto_refresh(framebufferio_framebufferdisplay_obj_t* self) {
+ return self->auto_refresh;
+}
+
+void common_hal_framebufferio_framebufferdisplay_set_auto_refresh(framebufferio_framebufferdisplay_obj_t* self,
+ bool auto_refresh) {
+ self->first_manual_refresh = !auto_refresh;
+ self->auto_refresh = auto_refresh;
+}
+
+STATIC void _update_backlight(framebufferio_framebufferdisplay_obj_t* self) {
+ // TODO(tannewt): Fade the backlight based on it's existing value and a target value. The target
+ // should account for ambient light when possible.
+}
+
+void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self) {
+ _update_backlight(self);
+
+ if (self->auto_refresh && (supervisor_ticks_ms64() - self->core.last_refresh) > self->native_ms_per_frame) {
+ _refresh_display(self);
+ }
+}
+
+void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self) {
+ release_display_core(&self->core);
+ self->framebuffer_protocol->deinit(self->framebuffer);
+}
+
+void reset_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self) {
+ self->auto_refresh = true;
+ common_hal_framebufferio_framebufferdisplay_show(self, NULL);
+}
+
+void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t* self) {
+ gc_collect_ptr(self->framebuffer);
+ displayio_display_core_collect_ptrs(&self->core);
+}
diff --git a/shared-module/framebufferio/FramebufferDisplay.h b/shared-module/framebufferio/FramebufferDisplay.h
new file mode 100644
index 000000000..1b68d2ab0
--- /dev/null
+++ b/shared-module/framebufferio/FramebufferDisplay.h
@@ -0,0 +1,92 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries
+ * 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.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FRAMEBUFFERDISPLAY_H
+#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FRAMEBUFFERDISPLAY_H
+
+#include "py/obj.h"
+#include "py/proto.h"
+
+#include "shared-bindings/digitalio/DigitalInOut.h"
+#include "shared-bindings/displayio/Group.h"
+#include "shared-bindings/pulseio/PWMOut.h"
+
+#include "shared-module/displayio/area.h"
+#include "shared-module/displayio/display_core.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ displayio_display_core_t core;
+ mp_obj_t framebuffer;
+ const struct _framebuffer_p_t *framebuffer_protocol;
+ mp_buffer_info_t bufinfo;
+ uint64_t last_backlight_refresh;
+ uint64_t last_refresh_call;
+ uint16_t native_frames_per_second;
+ uint16_t native_ms_per_frame;
+ bool auto_refresh;
+ bool first_manual_refresh;
+} framebufferio_framebufferdisplay_obj_t;
+
+void framebufferio_framebufferdisplay_background(framebufferio_framebufferdisplay_obj_t* self);
+void release_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self);
+void reset_framebufferdisplay(framebufferio_framebufferdisplay_obj_t* self);
+
+void framebufferio_framebufferdisplay_collect_ptrs(framebufferio_framebufferdisplay_obj_t* self);
+
+mp_obj_t common_hal_framebufferio_framebufferdisplay_get_framebuffer(framebufferio_framebufferdisplay_obj_t* self);
+
+typedef void (*framebuffer_get_bufinfo_fun)(mp_obj_t, mp_buffer_info_t *bufinfo);
+typedef void (*framebuffer_swapbuffers_fun)(mp_obj_t);
+typedef void (*framebuffer_deinit_fun)(mp_obj_t);
+typedef bool (*framebuffer_set_brightness_fun)(mp_obj_t, mp_float_t);
+typedef mp_float_t (*framebuffer_get_brightness_fun)(mp_obj_t);
+typedef bool (*framebuffer_set_auto_brightness_fun)(mp_obj_t, bool);
+typedef bool (*framebuffer_get_auto_brightness_fun)(mp_obj_t);
+typedef int (*framebuffer_get_width_fun)(mp_obj_t);
+typedef int (*framebuffer_get_height_fun)(mp_obj_t);
+typedef int (*framebuffer_get_color_depth_fun)(mp_obj_t);
+typedef int (*framebuffer_get_bytes_per_cell_fun)(mp_obj_t);
+typedef int (*framebuffer_get_native_frames_per_second_fun)(mp_obj_t);
+
+typedef struct _framebuffer_p_t {
+ MP_PROTOCOL_HEAD // MP_QSTR_protocol_framebuffer
+ framebuffer_get_bufinfo_fun get_bufinfo;
+ framebuffer_swapbuffers_fun swapbuffers;
+ framebuffer_deinit_fun deinit;
+ framebuffer_get_width_fun get_width;
+ framebuffer_get_height_fun get_height;
+ framebuffer_get_color_depth_fun get_color_depth;
+ framebuffer_get_bytes_per_cell_fun get_bytes_per_cell;
+ framebuffer_get_native_frames_per_second_fun get_native_frames_per_second;
+ framebuffer_get_brightness_fun get_brightness;
+ framebuffer_set_brightness_fun set_brightness;
+ framebuffer_get_auto_brightness_fun get_auto_brightness;
+ framebuffer_set_auto_brightness_fun set_auto_brightness;
+} framebuffer_p_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FRAMEBUFFERDISPLAY_H
diff --git a/shared-module/framebufferio/__init__.c b/shared-module/framebufferio/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/framebufferio/__init__.c
diff --git a/shared-module/framebufferio/__init__.h b/shared-module/framebufferio/__init__.h
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/framebufferio/__init__.h
diff --git a/shared-module/gamepad/GamePad.c b/shared-module/gamepad/GamePad.c
index fdce0caab..7b4108eb2 100644
--- a/shared-module/gamepad/GamePad.c
+++ b/shared-module/gamepad/GamePad.c
@@ -27,6 +27,7 @@
#include "py/mpstate.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-bindings/gamepad/GamePad.h"
+#include "supervisor/shared/tick.h"
void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad,
const mp_obj_t pins[], size_t n_pins) {
@@ -54,4 +55,5 @@ void common_hal_gamepad_gamepad_init(gamepad_obj_t *gamepad,
void common_hal_gamepad_gamepad_deinit(gamepad_obj_t *self) {
MP_STATE_VM(gamepad_singleton) = NULL;
+ supervisor_disable_tick();
}
diff --git a/shared-module/gamepad/__init__.c b/shared-module/gamepad/__init__.c
index 3e17a0fb5..9874b2752 100644
--- a/shared-module/gamepad/__init__.c
+++ b/shared-module/gamepad/__init__.c
@@ -29,6 +29,7 @@
#include "py/mpstate.h"
#include "shared-bindings/gamepad/__init__.h"
#include "shared-bindings/gamepad/GamePad.h"
+#include "supervisor/shared/tick.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
@@ -59,5 +60,8 @@ void gamepad_tick(void) {
}
void gamepad_reset(void) {
+ if (MP_STATE_VM(gamepad_singleton)) {
+ supervisor_disable_tick();
+ }
MP_STATE_VM(gamepad_singleton) = NULL;
}
diff --git a/shared-module/gamepadshift/GamePadShift.c b/shared-module/gamepadshift/GamePadShift.c
index b8fb4d3f4..51da61702 100644
--- a/shared-module/gamepadshift/GamePadShift.c
+++ b/shared-module/gamepadshift/GamePadShift.c
@@ -27,6 +27,7 @@
#include "py/mpstate.h"
#include "shared-bindings/digitalio/DigitalInOut.h"
#include "shared-module/gamepadshift/GamePadShift.h"
+#include "supervisor/shared/tick.h"
void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift,
digitalio_digitalinout_obj_t *clock_pin,
@@ -46,4 +47,5 @@ void common_hal_gamepadshift_gamepadshift_init(gamepadshift_obj_t *gamepadshift,
void common_hal_gamepadshift_gamepadshift_deinit(gamepadshift_obj_t *gamepadshift) {
MP_STATE_VM(gamepad_singleton) = NULL;
+ supervisor_disable_tick();
}
diff --git a/shared-module/network/__init__.c b/shared-module/network/__init__.c
index 925e9a2a3..a45191d8c 100644
--- a/shared-module/network/__init__.c
+++ b/shared-module/network/__init__.c
@@ -37,7 +37,7 @@
#include "shared-module/network/__init__.h"
-// mod_network_nic_list needs to be declared in mpconfigport.h
+// mod_network_nic_list needs to be declared in mpconfigport.h
void network_module_init(void) {
@@ -47,7 +47,7 @@ void network_module_init(void) {
void network_module_deinit(void) {
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
- mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
+ mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
if (nic_type->deinit != NULL) nic_type->deinit(nic);
}
mp_obj_list_set_len(&MP_STATE_PORT(mod_network_nic_list), 0);
@@ -61,7 +61,7 @@ void network_module_background(void) {
for (mp_uint_t i = 0; i < MP_STATE_PORT(mod_network_nic_list).len; i++) {
mp_obj_t nic = MP_STATE_PORT(mod_network_nic_list).items[i];
- mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
+ mod_network_nic_type_t *nic_type = (mod_network_nic_type_t*)mp_obj_get_type(nic);
if (nic_type->timer_tick != NULL) nic_type->timer_tick(nic);
}
}
diff --git a/shared-module/rgbmatrix/RGBMatrix.c b/shared-module/rgbmatrix/RGBMatrix.c
new file mode 100644
index 000000000..6dad91679
--- /dev/null
+++ b/shared-module/rgbmatrix/RGBMatrix.c
@@ -0,0 +1,208 @@
+/*
+ * This file is part of the Micro Python 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 <string.h>
+
+#include "py/gc.h"
+#include "py/obj.h"
+#include "py/objarray.h"
+#include "py/objproperty.h"
+#include "py/runtime.h"
+
+#include "common-hal/rgbmatrix/RGBMatrix.h"
+#include "shared-module/rgbmatrix/allocator.h"
+#include "shared-bindings/rgbmatrix/RGBMatrix.h"
+#include "shared-bindings/microcontroller/Pin.h"
+#include "shared-bindings/microcontroller/__init__.h"
+#include "shared-bindings/util.h"
+#include "shared-module/framebufferio/FramebufferDisplay.h"
+
+extern Protomatter_core *_PM_protoPtr;
+
+void common_hal_rgbmatrix_rgbmatrix_construct(rgbmatrix_rgbmatrix_obj_t *self, int width, int bit_depth, uint8_t rgb_count, uint8_t *rgb_pins, uint8_t addr_count, uint8_t *addr_pins, uint8_t clock_pin, uint8_t latch_pin, uint8_t oe_pin, bool doublebuffer, mp_obj_t framebuffer, void *timer) {
+ self->width = width;
+ self->bit_depth = bit_depth;
+ self->rgb_count = rgb_count;
+ memcpy(self->rgb_pins, rgb_pins, rgb_count);
+ self->addr_count = addr_count;
+ memcpy(self->addr_pins, addr_pins, addr_count);
+ self->clock_pin = clock_pin;
+ self->oe_pin = oe_pin;
+ self->latch_pin = latch_pin;
+ self->doublebuffer = doublebuffer;
+
+ self->timer = timer ? timer : common_hal_rgbmatrix_timer_allocate();
+ if (self->timer == NULL) {
+ mp_raise_ValueError(translate("No timer available"));
+ }
+
+ self->width = width;
+ self->bufsize = 2 * width * rgb_count / 3 * (1 << addr_count);
+
+ common_hal_rgbmatrix_rgbmatrix_reconstruct(self, framebuffer);
+}
+
+void common_hal_rgbmatrix_rgbmatrix_reconstruct(rgbmatrix_rgbmatrix_obj_t* self, mp_obj_t framebuffer) {
+ if (framebuffer) {
+ self->framebuffer = framebuffer;
+ framebuffer = mp_obj_new_bytearray_of_zeros(self->bufsize);
+ mp_get_buffer_raise(self->framebuffer, &self->bufinfo, MP_BUFFER_READ);
+ if (mp_get_buffer(self->framebuffer, &self->bufinfo, MP_BUFFER_RW)) {
+ self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW;
+ } else {
+ self->bufinfo.typecode = 'H';
+ }
+ // 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->core.rgbPins);
+ _PM_FREE(self->core.addr);
+ _PM_FREE(self->core.screenData);
+
+ self->framebuffer = NULL;
+ self->bufinfo.buf = _PM_allocator_impl(self->bufsize);
+ self->bufinfo.len = self->bufsize;
+ self->bufinfo.typecode = 'H' | MP_OBJ_ARRAY_TYPECODE_FLAG_RW;
+ }
+
+ ProtomatterStatus stat = _PM_init(&self->core,
+ self->width, self->bit_depth,
+ self->rgb_count/6, self->rgb_pins,
+ self->addr_count, self->addr_pins,
+ self->clock_pin, self->latch_pin, self->oe_pin,
+ self->doublebuffer, self->timer);
+
+ if (stat == PROTOMATTER_OK) {
+ _PM_protoPtr = &self->core;
+ common_hal_mcu_disable_interrupts();
+ common_hal_rgbmatrix_timer_enable(self->timer);
+ stat = _PM_begin(&self->core);
+ _PM_convert_565(&self->core, self->bufinfo.buf, self->width);
+ common_hal_mcu_enable_interrupts();
+ _PM_swapbuffer_maybe(&self->core);
+ }
+
+ if (stat != PROTOMATTER_OK) {
+ // XXX this deinit() actually makes crashy-crashy
+ // can trigger it by sending inappropriate pins
+ common_hal_rgbmatrix_rgbmatrix_deinit(self);
+ switch (stat) {
+ case PROTOMATTER_ERR_PINS:
+ mp_raise_ValueError(translate("Invalid pin"));
+ break;
+ case PROTOMATTER_ERR_ARG:
+ mp_raise_ValueError(translate("Invalid argument"));
+ break;
+ case PROTOMATTER_ERR_MALLOC: /// should have already been signaled as NLR
+ default:
+ mp_raise_msg_varg(&mp_type_RuntimeError,
+ translate("Internal error #%d"), (int)stat);
+ break;
+ }
+ }
+
+ self->paused = 0;
+
+}
+
+STATIC void free_pin(uint8_t *pin) {
+ if (*pin != COMMON_HAL_MCU_NO_PIN) {
+ common_hal_mcu_pin_reset_number(*pin);
+ }
+ *pin = COMMON_HAL_MCU_NO_PIN;
+}
+
+STATIC void free_pin_seq(uint8_t *seq, int count) {
+ for (int i=0; i<count; i++) {
+ free_pin(&seq[i]);
+ }
+}
+
+void common_hal_rgbmatrix_rgbmatrix_deinit(rgbmatrix_rgbmatrix_obj_t* self) {
+ if (self->timer) {
+ common_hal_rgbmatrix_timer_free(self->timer);
+ self->timer = 0;
+ }
+
+ if (_PM_protoPtr == &self->core) {
+ _PM_protoPtr = NULL;
+ }
+
+ free_pin_seq(self->rgb_pins, self->rgb_count);
+ free_pin_seq(self->addr_pins, self->addr_count);
+ free_pin(&self->clock_pin);
+ free_pin(&self->latch_pin);
+ free_pin(&self->oe_pin);
+
+ if (self->core.rgbPins) {
+ _PM_free(&self->core);
+ }
+ memset(&self->core, 0, sizeof(self->core));
+
+ // If it was supervisor-allocated, it is supervisor-freed and the pointer
+ // is zeroed, otherwise the pointer is just zeroed
+ _PM_FREE(self->bufinfo.buf);
+ self->base.type = NULL;
+
+ // If a framebuffer was passed in to the constructor, NULL the reference
+ // here so that it will become GC'able
+ self->framebuffer = NULL;
+}
+
+void rgbmatrix_rgbmatrix_collect_ptrs(rgbmatrix_rgbmatrix_obj_t* self) {
+ gc_collect_ptr(self->framebuffer);
+ gc_collect_ptr(self->core.rgbPins);
+ gc_collect_ptr(self->core.addr);
+ gc_collect_ptr(self->core.screenData);
+}
+
+void common_hal_rgbmatrix_rgbmatrix_set_paused(rgbmatrix_rgbmatrix_obj_t* self, bool paused) {
+ if (paused && !self->paused) {
+ _PM_stop(&self->core);
+ } else if (!paused && self->paused) {
+ _PM_resume(&self->core);
+ }
+ self->paused = paused;
+}
+
+bool common_hal_rgbmatrix_rgbmatrix_get_paused(rgbmatrix_rgbmatrix_obj_t* self) {
+ return self->paused;
+}
+
+void common_hal_rgbmatrix_rgbmatrix_refresh(rgbmatrix_rgbmatrix_obj_t* self) {
+ _PM_convert_565(&self->core, self->bufinfo.buf, self->width);
+ _PM_swapbuffer_maybe(&self->core);
+}
+
+int common_hal_rgbmatrix_rgbmatrix_get_width(rgbmatrix_rgbmatrix_obj_t* self) {
+ return self->width;
+}
+
+int common_hal_rgbmatrix_rgbmatrix_get_height(rgbmatrix_rgbmatrix_obj_t* self) {
+ int computed_height = (self->rgb_count / 3) << (self->addr_count);
+ return computed_height;
+}
diff --git a/shared-module/rgbmatrix/RGBMatrix.h b/shared-module/rgbmatrix/RGBMatrix.h
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/rgbmatrix/RGBMatrix.h
diff --git a/shared-module/rgbmatrix/__init__.c b/shared-module/rgbmatrix/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/rgbmatrix/__init__.c
diff --git a/shared-module/rgbmatrix/__init__.h b/shared-module/rgbmatrix/__init__.h
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/rgbmatrix/__init__.h
diff --git a/shared-module/rgbmatrix/allocator.h b/shared-module/rgbmatrix/allocator.h
new file mode 100644
index 000000000..5e6f0b41d
--- /dev/null
+++ b/shared-module/rgbmatrix/allocator.h
@@ -0,0 +1,29 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_RGBMATRIX_ALLOCATOR_H
+#define MICROPY_INCLUDED_SHARED_MODULE_RGBMATRIX_ALLOCATOR_H
+
+#include <stdbool.h>
+#include "py/gc.h"
+#include "py/misc.h"
+#include "supervisor/memory.h"
+
+#define _PM_ALLOCATOR _PM_allocator_impl
+#define _PM_FREE(x) (_PM_free_impl((x)), (x)=NULL, (void)0)
+
+static inline void *_PM_allocator_impl(size_t sz) {
+ if (gc_alloc_possible()) {
+ return m_malloc(sz + sizeof(void*), true);
+ } else {
+ supervisor_allocation *allocation = allocate_memory(align32_size(sz), false);
+ return allocation ? allocation->ptr : NULL;
+ }
+}
+
+static inline void _PM_free_impl(void *ptr_in) {
+ supervisor_allocation *allocation = allocation_from_ptr(ptr_in);
+
+ if (allocation) {
+ free_memory(allocation);
+ }
+}
+
+#endif
diff --git a/shared-module/sdcardio/SDCard.c b/shared-module/sdcardio/SDCard.c
new file mode 100644
index 000000000..9e861279d
--- /dev/null
+++ b/shared-module/sdcardio/SDCard.c
@@ -0,0 +1,466 @@
+/*
+ * This file is part of the Micro Python 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.
+ */
+
+// This implementation largely follows the structure of adafruit_sdcard.py
+
+#include "shared-bindings/busio/SPI.h"
+#include "shared-bindings/digitalio/DigitalInOut.h"
+#include "shared-bindings/time/__init__.h"
+#include "shared-bindings/util.h"
+#include "shared-module/sdcardio/SDCard.h"
+
+#include "py/mperrno.h"
+
+#if 0
+#define DEBUG_PRINT(...) ((void)mp_printf(&mp_plat_print, ## __VA_ARGS__))
+#else
+#define DEBUG_PRINT(...) ((void)0)
+#endif
+
+#define CMD_TIMEOUT (200)
+
+#define R1_IDLE_STATE (1<<0)
+#define R1_ILLEGAL_COMMAND (1<<2)
+
+#define TOKEN_CMD25 (0xFC)
+#define TOKEN_STOP_TRAN (0xFD)
+#define TOKEN_DATA (0xFE)
+
+STATIC bool lock_and_configure_bus(sdcardio_sdcard_obj_t *self) {
+ if (!common_hal_busio_spi_try_lock(self->bus)) {
+ return false;
+ }
+ common_hal_busio_spi_configure(self->bus, self->baudrate, 0, 0, 8);
+ common_hal_digitalio_digitalinout_set_value(&self->cs, false);
+ return true;
+}
+
+STATIC void lock_bus_or_throw(sdcardio_sdcard_obj_t *self) {
+ if (!lock_and_configure_bus(self)) {
+ mp_raise_OSError(EAGAIN);
+ }
+}
+
+STATIC void clock_card(sdcardio_sdcard_obj_t *self, int bytes) {
+ uint8_t buf[] = {0xff};
+ common_hal_digitalio_digitalinout_set_value(&self->cs, true);
+ for (int i=0; i<bytes; i++) {
+ common_hal_busio_spi_write(self->bus, buf, 1);
+ }
+}
+
+STATIC void extraclock_and_unlock_bus(sdcardio_sdcard_obj_t *self) {
+ clock_card(self, 1);
+ common_hal_busio_spi_unlock(self->bus);
+}
+
+static uint8_t CRC7(const uint8_t* data, uint8_t n) {
+ uint8_t crc = 0;
+ for (uint8_t i = 0; i < n; i++) {
+ uint8_t d = data[i];
+ for (uint8_t j = 0; j < 8; j++) {
+ crc <<= 1;
+ if ((d & 0x80) ^ (crc & 0x80)) {
+ crc ^= 0x09;
+ }
+ d <<= 1;
+ }
+ }
+ return (crc << 1) | 1;
+}
+
+#define READY_TIMEOUT_NS (300 * 1000 * 1000) // 300ms
+STATIC void wait_for_ready(sdcardio_sdcard_obj_t *self) {
+ uint64_t deadline = common_hal_time_monotonic_ns() + READY_TIMEOUT_NS;
+ while (common_hal_time_monotonic_ns() < deadline) {
+ uint8_t b;
+ common_hal_busio_spi_read(self->bus, &b, 1, 0xff);
+ if (b == 0xff) {
+ break;
+ }
+ }
+}
+
+// In Python API, defaults are response=None, data_block=True, wait=True
+STATIC int cmd(sdcardio_sdcard_obj_t *self, int cmd, int arg, void *response_buf, size_t response_len, bool data_block, bool wait) {
+ DEBUG_PRINT("cmd % 3d [%02x] arg=% 11d [%08x] len=%d%s%s\n", cmd, cmd, arg, arg, response_len, data_block ? " data" : "", wait ? " wait" : "");
+ uint8_t cmdbuf[6];
+ cmdbuf[0] = cmd | 0x40;
+ cmdbuf[1] = (arg >> 24) & 0xff;
+ cmdbuf[2] = (arg >> 16) & 0xff;
+ cmdbuf[3] = (arg >> 8) & 0xff;
+ cmdbuf[4] = arg & 0xff;
+ cmdbuf[5] = CRC7(cmdbuf, 5);
+
+ if (wait) {
+ wait_for_ready(self);
+ }
+
+ common_hal_busio_spi_write(self->bus, cmdbuf, sizeof(cmdbuf));
+
+ // Wait for the response (response[7] == 0)
+ bool response_received = false;
+ for (int i=0; i<CMD_TIMEOUT; i++) {
+ common_hal_busio_spi_read(self->bus, cmdbuf, 1, 0xff);
+ if ((cmdbuf[0] & 0x80) == 0) {
+ response_received = true;
+ break;
+ }
+ }
+
+ if (!response_received) {
+ return -EIO;
+ }
+
+ if (response_buf) {
+
+ if (data_block) {
+ cmdbuf[1] = 0xff;
+ do {
+ // Wait for the start block byte
+ common_hal_busio_spi_read(self->bus, cmdbuf+1, 1, 0xff);
+ } while (cmdbuf[1] != 0xfe);
+ }
+
+ common_hal_busio_spi_read(self->bus, response_buf, response_len, 0xff);
+
+ if (data_block) {
+ // Read and discard the CRC-CCITT checksum
+ common_hal_busio_spi_read(self->bus, cmdbuf+1, 2, 0xff);
+ }
+
+ }
+
+ return cmdbuf[0];
+}
+
+STATIC int block_cmd(sdcardio_sdcard_obj_t *self, int cmd_, int block, void *response_buf, size_t response_len, bool data_block, bool wait) {
+ return cmd(self, cmd_, block * self->cdv, response_buf, response_len, true, true);
+}
+
+STATIC bool cmd_nodata(sdcardio_sdcard_obj_t* self, int cmd, int response) {
+ uint8_t cmdbuf[2] = {cmd, 0xff};
+
+ common_hal_busio_spi_write(self->bus, cmdbuf, sizeof(cmdbuf));
+
+ // Wait for the response (response[7] == response)
+ for (int i=0; i<CMD_TIMEOUT; i++) {
+ common_hal_busio_spi_read(self->bus, cmdbuf, 1, 0xff);
+ if (cmdbuf[0] == response) {
+ return 0;
+ }
+ }
+ return -EIO;
+}
+
+STATIC const compressed_string_t *init_card_v1(sdcardio_sdcard_obj_t *self) {
+ for (int i=0; i<CMD_TIMEOUT; i++) {
+ if (cmd(self, 41, 0, NULL, 0, true, true) == 0) {
+ return NULL;
+ }
+ }
+ return translate("timeout waiting for v1 card");
+}
+
+STATIC const compressed_string_t *init_card_v2(sdcardio_sdcard_obj_t *self) {
+ for (int i=0; i<CMD_TIMEOUT; i++) {
+ uint8_t ocr[4];
+ common_hal_time_delay_ms(50);
+ cmd(self, 58, 0, ocr, sizeof(ocr), false, true);
+ cmd(self, 55, 0, NULL, 0, true, true);
+ if (cmd(self, 41, 0x40000000, NULL, 0, true, true) == 0) {
+ cmd(self, 58, 0, ocr, sizeof(ocr), false, true);
+ if ((ocr[0] & 0x40) != 0) {
+ self->cdv = 1;
+ }
+ return NULL;
+ }
+ }
+ return translate("timeout waiting for v2 card");
+}
+
+STATIC const compressed_string_t *init_card(sdcardio_sdcard_obj_t *self) {
+ clock_card(self, 10);
+
+ common_hal_digitalio_digitalinout_set_value(&self->cs, false);
+
+ // CMD0: init card: should return _R1_IDLE_STATE (allow 5 attempts)
+ {
+ bool reached_idle_state = false;
+ for (int i=0; i<5; i++) {
+ if (cmd(self, 0, 0, NULL, 0, true, true) == R1_IDLE_STATE) {
+ reached_idle_state = true;
+ break;
+ }
+ }
+ if (!reached_idle_state) {
+ return translate("no SD card");
+ }
+ }
+
+ // CMD8: determine card version
+ {
+ uint8_t rb7[4];
+ int response = cmd(self, 8, 0x1AA, rb7, sizeof(rb7), false, true);
+ if (response == R1_IDLE_STATE) {
+ const compressed_string_t *result =init_card_v2(self);
+ if (result != NULL) {
+ return result;
+ }
+ } else if (response == (R1_IDLE_STATE | R1_ILLEGAL_COMMAND)) {
+ const compressed_string_t *result =init_card_v1(self);
+ if (result != NULL) {
+ return result;
+ }
+ } else {
+ return translate("couldn't determine SD card version");
+ }
+ }
+
+ // CMD9: get number of sectors
+ {
+ uint8_t csd[16];
+ int response = cmd(self, 9, 0, csd, sizeof(csd), true, true);
+ if (response != 0) {
+ return translate("no response from SD card");
+ }
+ int csd_version = (csd[0] & 0xC0) >> 6;
+ if (csd_version >= 2) {
+ return translate("SD card CSD format not supported");
+ }
+
+ if (csd_version == 1) {
+ self->sectors = ((csd[8] << 8 | csd[9]) + 1) * 1024;
+ } else {
+ uint32_t block_length = 1 << (csd[5] & 0xF);
+ uint32_t c_size = ((csd[6] & 0x3) << 10) | (csd[7] << 2) | ((csd[8] & 0xC) >> 6);
+ uint32_t mult = 1 << (((csd[9] & 0x3) << 1 | (csd[10] & 0x80) >> 7) + 2);
+ self->sectors = block_length / 512 * mult * (c_size + 1);
+ }
+ }
+
+ // CMD16: set block length to 512 bytes
+ {
+ int response = cmd(self, 16, 512, NULL, 0, true, true);
+ if (response != 0) {
+ return translate("can't set 512 block size");
+ }
+ }
+
+ return NULL;
+}
+
+void common_hal_sdcardio_sdcard_construct(sdcardio_sdcard_obj_t *self, busio_spi_obj_t *bus, mcu_pin_obj_t *cs, int baudrate) {
+ self->bus = bus;
+ common_hal_digitalio_digitalinout_construct(&self->cs, cs);
+ common_hal_digitalio_digitalinout_switch_to_output(&self->cs, true, DRIVE_MODE_PUSH_PULL);
+
+ self->cdv = 512;
+ self->sectors = 0;
+ self->baudrate = 250000;
+
+ lock_bus_or_throw(self);
+ const compressed_string_t *result = init_card(self);
+ extraclock_and_unlock_bus(self);
+
+ if (result != NULL) {
+ common_hal_digitalio_digitalinout_deinit(&self->cs);
+ mp_raise_OSError_msg(result);
+ }
+
+ self->baudrate = baudrate;
+}
+
+void common_hal_sdcardio_sdcard_deinit(sdcardio_sdcard_obj_t *self) {
+ if (!self->bus) {
+ return;
+ }
+ self->bus = 0;
+ common_hal_digitalio_digitalinout_deinit(&self->cs);
+}
+
+void common_hal_sdcardio_check_for_deinit(sdcardio_sdcard_obj_t *self) {
+ if (!self->bus) {
+ raise_deinited_error();
+ }
+}
+
+int common_hal_sdcardio_sdcard_get_blockcount(sdcardio_sdcard_obj_t *self) {
+ common_hal_sdcardio_check_for_deinit(self);
+ return self->sectors;
+}
+
+int readinto(sdcardio_sdcard_obj_t *self, void *buf, size_t size) {
+ uint8_t aux[2] = {0, 0};
+ while (aux[0] != 0xfe) {
+ common_hal_busio_spi_read(self->bus, aux, 1, 0xff);
+ }
+
+ common_hal_busio_spi_read(self->bus, buf, size, 0xff);
+
+ // Read checksum and throw it away
+ common_hal_busio_spi_read(self->bus, aux, sizeof(aux), 0xff);
+ return 0;
+}
+
+int readblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf) {
+ uint32_t nblocks = buf->len / 512;
+ if (nblocks == 1) {
+ // Use CMD17 to read a single block
+ return block_cmd(self, 17, start_block, buf->buf, buf->len, true, true);
+ } else {
+ // Use CMD18 to read multiple blocks
+ int r = block_cmd(self, 18, start_block, NULL, 0, true, true);
+ if (r < 0) {
+ return r;
+ }
+
+ uint8_t *ptr = buf->buf;
+ while (nblocks--) {
+ r = readinto(self, ptr, 512);
+ if (r < 0) {
+ return r;
+ }
+ ptr += 512;
+ }
+
+ // End the multi-block read
+ r = cmd(self, 12, 0, NULL, 0, true, false);
+
+ // Return first status 0 or last before card ready (0xff)
+ while (r != 0) {
+ uint8_t single_byte;
+ common_hal_busio_spi_read(self->bus, &single_byte, 1, 0xff);
+ if (single_byte & 0x80) {
+ return r;
+ }
+ r = single_byte;
+ }
+ }
+ return 0;
+}
+
+int common_hal_sdcardio_sdcard_readblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf) {
+ common_hal_sdcardio_check_for_deinit(self);
+ if (buf->len % 512 != 0) {
+ mp_raise_ValueError(translate("Buffer length must be a multiple of 512"));
+ }
+
+ lock_and_configure_bus(self);
+ int r = readblocks(self, start_block, buf);
+ extraclock_and_unlock_bus(self);
+ return r;
+}
+
+int _write(sdcardio_sdcard_obj_t *self, uint8_t token, void *buf, size_t size) {
+ wait_for_ready(self);
+
+ uint8_t cmd[2];
+ cmd[0] = token;
+
+ common_hal_busio_spi_write(self->bus, cmd, 1);
+ common_hal_busio_spi_write(self->bus, buf, size);
+
+ cmd[0] = cmd[1] = 0xff;
+ common_hal_busio_spi_write(self->bus, cmd, 2);
+
+ // Check the response
+ // This differs from the traditional adafruit_sdcard handling,
+ // but adafruit_sdcard also ignored the return value of SDCard._write(!)
+ // so nobody noticed
+ //
+ //
+ // Response is as follows:
+ // x x x 0 STAT 1
+ // 7 6 5 4 3..1 0
+ // with STATUS 010 indicating "data accepted", and other status bit
+ // combinations indicating failure.
+ // In practice, I was seeing cmd[0] as 0xe5, indicating success
+ for (int i=0; i<CMD_TIMEOUT; i++) {
+ common_hal_busio_spi_read(self->bus, cmd, 1, 0xff);
+ DEBUG_PRINT("i=%02d cmd[0] = 0x%02x\n", i, cmd[0]);
+ if ((cmd[0] & 0b00010001) == 0b00000001) {
+ if ((cmd[0] & 0x1f) != 0x5) {
+ return -EIO;
+ } else {
+ break;
+ }
+ }
+ }
+
+ // Wait for the write to finish
+ do {
+ common_hal_busio_spi_read(self->bus, cmd, 1, 0xff);
+ } while (cmd[0] == 0);
+
+ // Success
+ return 0;
+}
+
+int writeblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf) {
+ common_hal_sdcardio_check_for_deinit(self);
+ uint32_t nblocks = buf->len / 512;
+ if (nblocks == 1) {
+ // Use CMD24 to write a single block
+ int r = block_cmd(self, 24, start_block, NULL, 0, true, true);
+ if (r < 0) {
+ return r;
+ }
+ r = _write(self, TOKEN_DATA, buf->buf, buf->len);
+ if (r < 0) {
+ return r;
+ }
+ } else {
+ // Use CMD25 to write multiple block
+ int r = block_cmd(self, 25, start_block, NULL, 0, true, true);
+ if (r < 0) {
+ return r;
+ }
+
+ uint8_t *ptr = buf->buf;
+ while (nblocks--) {
+ r = _write(self, TOKEN_CMD25, ptr, 512);
+ if (r < 0) {
+ return r;
+ }
+ ptr += 512;
+ }
+
+ cmd_nodata(self, TOKEN_STOP_TRAN, 0);
+ }
+ return 0;
+}
+
+int common_hal_sdcardio_sdcard_writeblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf) {
+ common_hal_sdcardio_check_for_deinit(self);
+ if (buf->len % 512 != 0) {
+ mp_raise_ValueError(translate("Buffer length must be a multiple of 512"));
+ }
+ lock_and_configure_bus(self);
+ int r = writeblocks(self, start_block, buf);
+ extraclock_and_unlock_bus(self);
+ return r;
+}
diff --git a/shared-module/sdcardio/SDCard.h b/shared-module/sdcardio/SDCard.h
new file mode 100644
index 000000000..76c906029
--- /dev/null
+++ b/shared-module/sdcardio/SDCard.h
@@ -0,0 +1,51 @@
+/*
+ * This file is part of the Micro Python 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 "py/objproperty.h"
+#include "py/runtime.h"
+#include "py/objarray.h"
+
+#include "common-hal/busio/SPI.h"
+#include "common-hal/digitalio/DigitalInOut.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ busio_spi_obj_t *bus;
+ digitalio_digitalinout_obj_t cs;
+ int cdv;
+ int baudrate;
+ uint32_t sectors;
+} sdcardio_sdcard_obj_t;
+
+void common_hal_sdcardio_sdcard_construct(sdcardio_sdcard_obj_t *self, busio_spi_obj_t *spi, mcu_pin_obj_t *cs, int baudrate);
+void common_hal_sdcardio_sdcard_deinit(sdcardio_sdcard_obj_t *self);
+void common_hal_sdcardio_sdcard_check_for_deinit(sdcardio_sdcard_obj_t *self);
+int common_hal_sdcardio_sdcard_get_blockcount(sdcardio_sdcard_obj_t *self);
+int common_hal_sdcardio_sdcard_readblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf);
+int common_hal_sdcardio_sdcard_writeblocks(sdcardio_sdcard_obj_t *self, uint32_t start_block, mp_buffer_info_t *buf);
diff --git a/shared-module/sdcardio/__init__.c b/shared-module/sdcardio/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/sdcardio/__init__.c
diff --git a/shared-module/sdcardio/__init__.h b/shared-module/sdcardio/__init__.h
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/sdcardio/__init__.h
diff --git a/shared-module/_pew/PewPew.h b/shared-module/time/__init__.c
index da1cae0a2..347e68ae0 100644
--- a/shared-module/_pew/PewPew.h
+++ b/shared-module/time/__init__.c
@@ -3,7 +3,7 @@
*
* The MIT License (MIT)
*
- * Copyright (c) 2019 Radomir Dopieralski
+ * Copyright (c) 2016 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
@@ -24,25 +24,22 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_PEW_PEWPEW_H
-#define MICROPY_INCLUDED_PEW_PEWPEW_H
+#include "py/mphal.h"
+#include "supervisor/port.h"
+#include "supervisor/shared/tick.h"
-#include <stdint.h>
-#include "shared-bindings/digitalio/DigitalInOut.h"
+uint64_t common_hal_time_monotonic(void) {
+ return supervisor_ticks_ms64();
+}
-typedef struct {
- mp_obj_base_t base;
- uint8_t* buffer;
- mp_obj_t* rows;
- mp_obj_t* cols;
- digitalio_digitalinout_obj_t *buttons;
- uint8_t rows_size;
- uint8_t cols_size;
- uint8_t pressed;
-} pew_obj_t;
+uint64_t common_hal_time_monotonic_ns(void) {
+ uint8_t subticks = 0;
+ uint64_t ticks = port_get_raw_ticks(&subticks);
+ // A tick is 976562.5 nanoseconds so multiply it by the base and add half instead of doing float
+ // math.
+ return 976562 * ticks + ticks / 2 + 30518 * subticks;
+}
-void pew_init(void);
-void pewpew_interrupt_handler(uint8_t index);
-void pew_reset(void);
-
-#endif // MICROPY_INCLUDED_PEW_PEWPEW_H
+void common_hal_time_delay_ms(uint32_t delay) {
+ mp_hal_delay_ms(delay);
+}
diff --git a/shared-module/usb_hid/Device.c b/shared-module/usb_hid/Device.c
index 8744f2ed3..8e9df6f04 100644
--- a/shared-module/usb_hid/Device.c
+++ b/shared-module/usb_hid/Device.c
@@ -25,7 +25,7 @@
*/
#include <string.h>
-#include "tick.h"
+
#include "py/runtime.h"
#include "shared-bindings/usb_hid/Device.h"
#include "shared-module/usb_hid/Device.h"
diff --git a/shared-module/ustack/__init__.c b/shared-module/ustack/__init__.c
index 947f81f8e..1e168ad6a 100644
--- a/shared-module/ustack/__init__.c
+++ b/shared-module/ustack/__init__.c
@@ -49,4 +49,3 @@ uint32_t shared_module_ustack_stack_size() {
uint32_t shared_module_ustack_stack_usage() {
return mp_stack_usage();
}
-
diff --git a/shared-module/vectorio/Circle.c b/shared-module/vectorio/Circle.c
new file mode 100644
index 000000000..73629b8ce
--- /dev/null
+++ b/shared-module/vectorio/Circle.c
@@ -0,0 +1,55 @@
+
+#include "shared-bindings/vectorio/Circle.h"
+#include "shared-module/vectorio/__init__.h"
+#include "shared-module/displayio/area.h"
+
+#include "py/runtime.h"
+#include "stdlib.h"
+
+
+void common_hal_vectorio_circle_construct(vectorio_circle_t *self, uint16_t radius) {
+ self->radius = radius;
+ self->on_dirty.obj = NULL;
+}
+
+void common_hal_vectorio_circle_set_on_dirty(vectorio_circle_t *self, vectorio_event_t on_dirty) {
+ if (self->on_dirty.obj != NULL) {
+ mp_raise_TypeError(translate("circle can only be registered in one parent"));
+ }
+ self->on_dirty = on_dirty;
+}
+
+
+uint32_t common_hal_vectorio_circle_get_pixel(void *obj, int16_t x, int16_t y) {
+ vectorio_circle_t *self = obj;
+ int16_t radius = abs(self->radius);
+ x = abs(x);
+ y = abs(y);
+ if (x+y <= radius) return 1;
+ if (x > radius) return 0;
+ if (y > radius) return 0;
+ const bool pythagorasSmallerThanRadius = (int32_t)x*x + (int32_t)y*y <= (int32_t)radius*radius;
+ return pythagorasSmallerThanRadius ? 1 : 0;
+}
+
+
+void common_hal_vectorio_circle_get_area(void *circle, displayio_area_t *out_area) {
+ vectorio_circle_t *self = circle;
+ out_area->x1 = -1 * self->radius - 1;
+ out_area->y1 = -1 * self->radius - 1;
+ out_area->x2 = self->radius + 1;
+ out_area->y2 = self->radius + 1;
+}
+
+int16_t common_hal_vectorio_circle_get_radius(void *obj) {
+ vectorio_circle_t *self = obj;
+ return self->radius;
+}
+
+void common_hal_vectorio_circle_set_radius(void *obj, int16_t radius) {
+ vectorio_circle_t *self = obj;
+ self->radius = abs(radius);
+ if (self->on_dirty.obj != NULL) {
+ self->on_dirty.event(self->on_dirty.obj);
+ }
+}
diff --git a/shared-module/vectorio/Circle.h b/shared-module/vectorio/Circle.h
new file mode 100644
index 000000000..d6a77b166
--- /dev/null
+++ b/shared-module/vectorio/Circle.h
@@ -0,0 +1,16 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_CIRCLE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_CIRCLE_H
+
+#include <stdint.h>
+
+#include "py/obj.h"
+
+#include "shared-module/vectorio/__init__.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint16_t radius;
+ vectorio_event_t on_dirty;
+} vectorio_circle_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_CIRCLE_H
diff --git a/shared-module/vectorio/Polygon.c b/shared-module/vectorio/Polygon.c
new file mode 100644
index 000000000..0025d4bfc
--- /dev/null
+++ b/shared-module/vectorio/Polygon.c
@@ -0,0 +1,146 @@
+
+#include "shared-module/vectorio/__init__.h"
+#include "shared-bindings/vectorio/Polygon.h"
+#include "shared-module/displayio/area.h"
+
+#include "py/runtime.h"
+#include "py/gc.h"
+
+#include "stdlib.h"
+#include <stdio.h>
+
+
+#define VECTORIO_POLYGON_DEBUG(...) (void)0
+// #define VECTORIO_POLYGON_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
+
+// Converts a list of points tuples to a flat list of ints for speedier internal use.
+// Also validates the points.
+static void _clobber_points_list(vectorio_polygon_t *self, mp_obj_t points_tuple_list) {
+ size_t len = 0;
+ mp_obj_t *items;
+ mp_obj_list_get(points_tuple_list, &len, &items);
+ VECTORIO_POLYGON_DEBUG("polygon_points_list len: %d\n", len);
+
+ if ( len < 3 ) {
+ mp_raise_TypeError_varg(translate("Polygon needs at least 3 points"));
+ }
+
+ if ( self->len < 2*len ) {
+ if ( self->points_list != NULL ) {
+ gc_free( self->points_list );
+ }
+ self->points_list = gc_alloc( 2 * len * sizeof(int), false, false );
+ }
+ self->len = 2*len;
+
+ for ( size_t i = 0; i < len; ++i) {
+ size_t tuple_len = 0;
+ mp_obj_t *tuple_items;
+ mp_obj_tuple_get(items[i], &tuple_len, &tuple_items);
+
+ if (tuple_len != 2) {
+ mp_raise_ValueError_varg(translate("%q must be a tuple of length 2"), MP_QSTR_point);
+ }
+ if ( !mp_obj_get_int_maybe(tuple_items[ 0 ], &self->points_list[2*i ])
+ || !mp_obj_get_int_maybe(tuple_items[ 1 ], &self->points_list[2*i + 1])
+ ) {
+ self->len = 0;
+ gc_free( self->points_list );
+ self->points_list = NULL;
+ mp_raise_ValueError_varg(translate("unsupported %q type"), MP_QSTR_point);
+ }
+ }
+}
+
+
+
+void common_hal_vectorio_polygon_construct(vectorio_polygon_t *self, mp_obj_t points_list) {
+ VECTORIO_POLYGON_DEBUG("%p polygon_construct\n", self);
+ self->points_list = NULL;
+ self->len = 0;
+ self->on_dirty.obj = NULL;
+ _clobber_points_list( self, points_list );
+}
+
+
+mp_obj_t common_hal_vectorio_polygon_get_points(vectorio_polygon_t *self) {
+ return self->points_list;
+}
+void common_hal_vectorio_polygon_set_points(vectorio_polygon_t *self, mp_obj_t points_list) {
+ _clobber_points_list( self, points_list );
+ if (self->on_dirty.obj != NULL) {
+ self->on_dirty.event(self->on_dirty.obj);
+ }
+}
+
+void common_hal_vectorio_polygon_set_on_dirty(vectorio_polygon_t *self, vectorio_event_t notification) {
+ if ( self->on_dirty.obj != NULL ) {
+ mp_raise_TypeError(translate("polygon can only be registered in one parent"));
+ }
+ self->on_dirty = notification;
+}
+
+
+void common_hal_vectorio_polygon_get_area(void *polygon, displayio_area_t *area) {
+ vectorio_polygon_t *self = polygon;
+
+ area->x1 = SHRT_MAX;
+ area->y1 = SHRT_MAX;
+ area->x2 = SHRT_MIN;
+ area->y2 = SHRT_MIN;
+ for (size_t i=0; i < self->len; ++i) {
+ int x = self->points_list[i];
+ ++i;
+ int y = self->points_list[i];
+ if (x <= area->x1) area->x1 = x-1;
+ if (y <= area->y1) area->y1 = y-1;
+ if (x >= area->x2) area->x2 = x+1;
+ if (y >= area->y2) area->y2 = y+1;
+ }
+}
+
+
+// <0 if the point is to the left of the line vector
+// 0 if the point is on the line
+// >0 if the point is to the right of the line vector
+__attribute__((always_inline)) static inline int line_side( mp_int_t x1, mp_int_t y1, mp_int_t x2, mp_int_t y2, int16_t px, int16_t py ) {
+ return (px - x1) * (y2 - y1)
+ - (py - y1) * (x2 - x1);
+}
+
+
+uint32_t common_hal_vectorio_polygon_get_pixel(void *obj, int16_t x, int16_t y) {
+ VECTORIO_POLYGON_DEBUG("%p polygon get_pixel %d, %d\n", obj, x, y);
+ vectorio_polygon_t *self = obj;
+
+ if (self->len == 0) {
+ return 0;
+ }
+
+ int winding_number = 0;
+ int x1 = self->points_list[0];
+ int y1 = self->points_list[1];
+ for (size_t i=2; i <= self->len + 1; ++i) {
+ VECTORIO_POLYGON_DEBUG(" {(%3d, %3d),", x1, y1);
+ int x2 = self->points_list[i % self->len];
+ ++i;
+ int y2 = self->points_list[i % self->len];
+ VECTORIO_POLYGON_DEBUG(" (%3d, %3d)}\n", x2, y2);
+ if ( y1 <= y ) {
+ if ( y2 > y && line_side(x1, y1, x2, y2, x, y) > 0 ) {
+ // Wind up, point is to the right of the edge vector
+ ++winding_number;
+ VECTORIO_POLYGON_DEBUG(" wind:%2d winding_number:%2d\n", 1, winding_number);
+ }
+ } else if ( y2 <= y && line_side(x1, y1, x2, y2, x, y) < 0 ) {
+ // Wind down, point is to the left of the edge vector
+ --winding_number;
+ VECTORIO_POLYGON_DEBUG(" wind:%2d winding_number:%2d\n", -1, winding_number);
+ }
+
+ x1 = x2;
+ y1 = y2;
+ }
+ return winding_number == 0 ? 0 : 1;
+}
diff --git a/shared-module/vectorio/Polygon.h b/shared-module/vectorio/Polygon.h
new file mode 100644
index 000000000..70de9036d
--- /dev/null
+++ b/shared-module/vectorio/Polygon.h
@@ -0,0 +1,17 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_POLYGON_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_POLYGON_H
+
+#include <stdint.h>
+
+#include "py/obj.h"
+#include "shared-module/vectorio/__init__.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ // An int array[ x, y, ... ]
+ int *points_list;
+ size_t len;
+ vectorio_event_t on_dirty;
+} vectorio_polygon_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_POLYGON_H
diff --git a/shared-module/vectorio/Rectangle.c b/shared-module/vectorio/Rectangle.c
new file mode 100644
index 000000000..5d48cc648
--- /dev/null
+++ b/shared-module/vectorio/Rectangle.c
@@ -0,0 +1,34 @@
+#include "shared-bindings/vectorio/Rectangle.h"
+#include "shared-module/displayio/area.h"
+
+#include "py/runtime.h"
+
+
+void common_hal_vectorio_rectangle_construct(vectorio_rectangle_t *self, uint32_t width, uint32_t height) {
+ self->width = width;
+ self->height = height;
+}
+
+
+uint32_t common_hal_vectorio_rectangle_get_pixel(void *obj, int16_t x, int16_t y) {
+ vectorio_rectangle_t *self = obj;
+ if (x < 0 || x > self->width || y > self->height || y < 0) {
+ return 0;
+ }
+ return 1;
+}
+
+
+void common_hal_vectorio_rectangle_get_area(void *rectangle, displayio_area_t *out_area) {
+ vectorio_rectangle_t *self = rectangle;
+ out_area->x1 = -1;
+ out_area->y1 = -1;
+ out_area->x2 = self->width;
+ out_area->y2 = self->height;
+}
+
+
+uint32_t common_hal_vectorio_rectangle_get_height(void *rectangle) {
+ vectorio_rectangle_t *self = rectangle;
+ return self->height;
+}
diff --git a/shared-module/vectorio/Rectangle.h b/shared-module/vectorio/Rectangle.h
new file mode 100644
index 000000000..56342a6d7
--- /dev/null
+++ b/shared-module/vectorio/Rectangle.h
@@ -0,0 +1,14 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_RECTANGLE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_RECTANGLE_H
+
+#include <stdint.h>
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint16_t width;
+ uint16_t height;
+} vectorio_rectangle_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_RECTANGLE_H
diff --git a/shared-module/vectorio/VectorShape.c b/shared-module/vectorio/VectorShape.c
new file mode 100644
index 000000000..bfe8a7b25
--- /dev/null
+++ b/shared-module/vectorio/VectorShape.c
@@ -0,0 +1,334 @@
+
+#include "stdlib.h"
+
+#include "shared-module/vectorio/__init__.h"
+#include "shared-bindings/vectorio/VectorShape.h"
+
+#include "py/runtime.h"
+#include "shared-bindings/time/__init__.h"
+#include "shared-bindings/displayio/ColorConverter.h"
+#include "shared-bindings/displayio/Palette.h"
+
+#include "shared-bindings/vectorio/Circle.h"
+#include "shared-bindings/vectorio/Polygon.h"
+#include "shared-bindings/vectorio/Rectangle.h"
+
+// Lifecycle actions.
+#define VECTORIO_SHAPE_DEBUG(...) (void)0
+// #define VECTORIO_SHAPE_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
+
+// Used in both logging and ifdefs, for extra variables
+// #define VECTORIO_PERF(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
+
+// Really verbose.
+#define VECTORIO_SHAPE_PIXEL_DEBUG(...) (void)0
+// #define VECTORIO_SHAPE_PIXEL_DEBUG(...) mp_printf(&mp_plat_print __VA_OPT__(,) __VA_ARGS__)
+
+
+inline __attribute__((always_inline))
+static int32_t max(int32_t a, int32_t b) {
+ return a > b ? a : b;
+}
+
+
+inline __attribute__((always_inline))
+static void _get_screen_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) {
+ VECTORIO_SHAPE_DEBUG("%p get_screen_area tform:{x:%d y:%d dx:%d dy:%d scl:%d w:%d h:%d mx:%d my:%d tr:%d}", self,
+ self->absolute_transform->x, self->absolute_transform->y, self->absolute_transform->dx, self->absolute_transform->dy, self->absolute_transform->scale,
+ self->absolute_transform->width, self->absolute_transform->height, self->absolute_transform->mirror_x, self->absolute_transform->mirror_y, self->absolute_transform->transpose_xy
+ );
+ self->ishape.get_area(self->ishape.shape, out_area);
+ VECTORIO_SHAPE_DEBUG(" in:{(%5d,%5d), (%5d,%5d)}", out_area->x1, out_area->y1, out_area->x2, out_area->y2);
+ if (self->absolute_transform->transpose_xy) {
+ int16_t swap = out_area->x1;
+ out_area->x1 = (out_area->y1 + self->y) * self->absolute_transform->dx + self->absolute_transform->x;
+ out_area->y1 = (swap + self->x) * self->absolute_transform->dy + self->absolute_transform->y;
+ swap = out_area->x2;
+ out_area->x2 = (out_area->y2 + self->y) * self->absolute_transform->dx + self->absolute_transform->x;
+ out_area->y2 = (swap + self->x) * self->absolute_transform->dy + self->absolute_transform->y;
+ } else {
+ out_area->x1 = (out_area->x1 + self->x) * self->absolute_transform->dx + self->absolute_transform->x;
+ out_area->y1 = (out_area->y1 + self->y) * self->absolute_transform->dy + self->absolute_transform->y;
+ out_area->x2 = (out_area->x2 + self->x) * self->absolute_transform->dx + self->absolute_transform->x;
+ out_area->y2 = (out_area->y2 + self->y) * self->absolute_transform->dy + self->absolute_transform->y;
+ }
+ // We might have mirrored due to dx
+ if (out_area->x2 < out_area->x1) {
+ int16_t swap = out_area->x1;
+ out_area->x1 = out_area->x2;
+ out_area->x2 = swap;
+ }
+ if (out_area->y2 < out_area->y1) {
+ int16_t swap = out_area->y1;
+ out_area->y1 = out_area->y2;
+ out_area->y2 = swap;
+ }
+ VECTORIO_SHAPE_DEBUG(" out:{(%5d,%5d), (%5d,%5d)}\n", out_area->x1, out_area->y1, out_area->x2, out_area->y2);
+}
+
+
+// For use by Group to know where it needs to redraw on layer removal.
+bool vectorio_vector_shape_get_dirty_area(vectorio_vector_shape_t *self, displayio_area_t *out_area) {
+ displayio_area_copy(&self->ephemeral_dirty_area, out_area);
+ return true; // For now just always redraw.
+}
+
+
+// This must be invoked each time a shape changes its position or its shape in any way.
+void common_hal_vectorio_vector_shape_set_dirty(void *vector_shape) {
+ vectorio_vector_shape_t *self = vector_shape;
+ // In screen space. Need to offset the shape space.
+ displayio_area_t current_area;
+ _get_screen_area(self, &current_area);
+ VECTORIO_SHAPE_DEBUG("%p shape_dirty current:{(%3d,%3d), (%3d,%3d)} dirty:{(%3d,%3d), (%3d,%3d)}",
+ self,
+ current_area.x1, current_area.y1, current_area.x2, current_area.y2,
+ self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+ self->dirty = true;
+ // Dirty area tracks the shape's footprint between draws. It's reset on refresh finish,
+ displayio_area_expand(&self->ephemeral_dirty_area, &current_area);
+ VECTORIO_SHAPE_DEBUG(" -> expanded:{(%3d,%3d), (%3d,%3d)}\n", self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+}
+
+
+static displayio_buffer_transform_t null_transform = {
+ .x = 0,
+ .y = 0,
+ .dx = 1,
+ .dy = 1,
+ .scale = 1,
+ .width = 0,
+ .height = 0,
+ .mirror_x = false,
+ .mirror_y = false,
+ .transpose_xy = false
+};
+
+
+void common_hal_vectorio_vector_shape_construct(vectorio_vector_shape_t *self,
+ vectorio_ishape_t ishape,
+ mp_obj_t pixel_shader, uint16_t x, uint16_t y) {
+ VECTORIO_SHAPE_DEBUG("%p vector_shape_construct x:%3d, y:%3d\n", self, x, y);
+ self->x = x;
+ self->y = y;
+ self->pixel_shader = pixel_shader;
+ self->ishape = ishape;
+ self->dirty = true;
+ self->absolute_transform = &null_transform; // Critical to have a valid transform before getting screen area.
+ _get_screen_area(self, &self->ephemeral_dirty_area);
+ self->ephemeral_dirty_area.next = NULL;
+}
+
+
+mp_int_t common_hal_vectorio_vector_shape_get_x(vectorio_vector_shape_t *self) {
+ VECTORIO_SHAPE_DEBUG("%p get_x\n", self);
+ return self->x;
+}
+
+
+void common_hal_vectorio_vector_shape_set_x(vectorio_vector_shape_t *self, mp_int_t x) {
+ VECTORIO_SHAPE_DEBUG("%p set_x %d\n", self, x);
+ if (self->x == x) {
+ return;
+ }
+ self->x = x;
+ common_hal_vectorio_vector_shape_set_dirty(self);
+}
+
+
+mp_int_t common_hal_vectorio_vector_shape_get_y(vectorio_vector_shape_t *self) {
+ VECTORIO_SHAPE_DEBUG("%p get_y\n", self);
+ return self->y;
+}
+
+
+void common_hal_vectorio_vector_shape_set_y(vectorio_vector_shape_t *self, mp_int_t y) {
+ VECTORIO_SHAPE_DEBUG("%p set_y %d\n", self, y);
+ if (self->y == y) {
+ return;
+ }
+ self->y = y;
+ common_hal_vectorio_vector_shape_set_dirty(self);
+}
+
+
+mp_obj_t common_hal_vectorio_vector_shape_get_pixel_shader(vectorio_vector_shape_t *self) {
+ VECTORIO_SHAPE_DEBUG("%p get_pixel_shader\n", self);
+ return self->pixel_shader;
+}
+
+void common_hal_vectorio_vector_shape_set_pixel_shader(vectorio_vector_shape_t *self, mp_obj_t pixel_shader) {
+ VECTORIO_SHAPE_DEBUG("%p set_pixel_shader\n", self);
+ self->pixel_shader = pixel_shader;
+ common_hal_vectorio_vector_shape_set_dirty(self);
+}
+
+
+bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer) {
+ // Shape areas are relative to 0,0. This will allow rotation about a known axis.
+ // The consequence is that the area reported by the shape itself is _relative_ to 0,0.
+ // To make it relative to the VectorShape position, we must shift it.
+ // Pixels are drawn on the screen_area (shifted) coordinate space, while pixels are _determined_ from
+ // the shape_area (unshifted) space.
+#ifdef VECTORIO_PERF
+ uint64_t start = common_hal_time_monotonic_ns();
+ uint64_t pixel_time = 0;
+#endif
+ displayio_area_t overlap;
+ VECTORIO_SHAPE_DEBUG("%p fill_area dirty:%d fill: {(%5d,%5d), (%5d,%5d)} dirty: {(%5d,%5d), (%5d,%5d)}",
+ self, self->dirty,
+ area->x1, area->y1, area->x2, area->y2,
+ self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2
+ );
+ if (!displayio_area_compute_overlap(area, &self->ephemeral_dirty_area, &overlap)) {
+ VECTORIO_SHAPE_DEBUG(" no overlap\n");
+ return false;
+ }
+ VECTORIO_SHAPE_DEBUG(", overlap: {(%3d,%3d), (%3d,%3d)}", overlap.x1, overlap.y1, overlap.x2, overlap.y2);
+
+ bool full_coverage = displayio_area_equal(area, &overlap);
+
+ uint8_t pixels_per_byte = 8 / colorspace->depth;
+
+ uint32_t linestride_px = displayio_area_width(area);
+ uint32_t line_dirty_offset_px = (overlap.y1 - area->y1) * linestride_px;
+ uint32_t column_dirty_offset_px = overlap.x1 - area->x1;
+ VECTORIO_SHAPE_DEBUG(", linestride:%3d line_offset:%3d col_offset:%3d depth:%2d ppb:%2d shape:%s",
+ linestride_px, line_dirty_offset_px, column_dirty_offset_px, colorspace->depth, pixels_per_byte, mp_obj_get_type_str(self->ishape.shape));
+
+ displayio_input_pixel_t input_pixel;
+ displayio_output_pixel_t output_pixel;
+
+ uint32_t mask_start_px = line_dirty_offset_px;
+ for (input_pixel.y = overlap.y1; input_pixel.y < overlap.y2; ++input_pixel.y) {
+ mask_start_px += column_dirty_offset_px;
+ for (input_pixel.x = overlap.x1; input_pixel.x < overlap.x2; ++input_pixel.x) {
+ // Check the mask first to see if the pixel has already been set.
+ uint32_t pixel_index = mask_start_px + (input_pixel.x - overlap.x1);
+ uint32_t *mask_doubleword = &(mask[pixel_index / 32]);
+ uint8_t mask_bit = pixel_index % 32;
+ VECTORIO_SHAPE_PIXEL_DEBUG("%p pixel_index: %5u mask_bit: %2u", self, pixel_index, mask_bit);
+ if ((*mask_doubleword & (1u << mask_bit)) != 0) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" masked\n");
+ continue;
+ }
+ output_pixel.pixel = 0;
+
+ // Get the target pixel based on the shape's coordinate space
+ int16_t pixel_to_get_x;
+ int16_t pixel_to_get_y;
+ if (self->absolute_transform->transpose_xy) {
+ pixel_to_get_x = (input_pixel.y - self->absolute_transform->dy * self->x - self->absolute_transform->y) / self->absolute_transform->dy;
+ pixel_to_get_y = (input_pixel.x - self->absolute_transform->dx * self->y - self->absolute_transform->x) / self->absolute_transform->dx;
+ } else {
+ pixel_to_get_x = (input_pixel.x - self->absolute_transform->dx * self->x) / self->absolute_transform->dx;
+ pixel_to_get_y = (input_pixel.y - self->absolute_transform->dy * self->y) / self->absolute_transform->dy;
+ }
+ VECTORIO_SHAPE_PIXEL_DEBUG(" get_pixel %p (%3d, %3d) -> ( %3d, %3d )", self->ishape.shape, input_pixel.x, input_pixel.y, pixel_to_get_x, pixel_to_get_y);
+#ifdef VECTORIO_PERF
+ uint64_t pre_pixel = common_hal_time_monotonic_ns();
+#endif
+ input_pixel.pixel = self->ishape.get_pixel(self->ishape.shape, pixel_to_get_x, pixel_to_get_y);
+#ifdef VECTORIO_PERF
+ uint64_t post_pixel = common_hal_time_monotonic_ns();
+ pixel_time += post_pixel - pre_pixel;
+#endif
+ VECTORIO_SHAPE_PIXEL_DEBUG(" -> %d", input_pixel.pixel);
+
+ output_pixel.opaque = true;
+ if (self->pixel_shader == mp_const_none) {
+ output_pixel.pixel = input_pixel.pixel;
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
+ output_pixel.opaque = displayio_palette_get_color(self->pixel_shader, colorspace, input_pixel.pixel, &output_pixel.pixel);
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
+ displayio_colorconverter_convert(self->pixel_shader, colorspace, &input_pixel, &output_pixel);
+ }
+ if (!output_pixel.opaque) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" (encountered transparent pixel; input area is not fully covered)\n");
+ full_coverage = false;
+ } else {
+ *mask_doubleword |= 1u << mask_bit;
+ if (colorspace->depth == 16) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %04x 16\n", output_pixel.pixel);
+ *(((uint16_t*) buffer) + pixel_index) = output_pixel.pixel;
+ } else if (colorspace->depth == 8) {
+ VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %02x 8\n", output_pixel.pixel);
+ *(((uint8_t*) buffer) + pixel_index) = output_pixel.pixel;
+ } else if (colorspace->depth < 8) {
+ // Reorder the offsets to pack multiple rows into a byte (meaning they share a column).
+ if (!colorspace->pixels_in_byte_share_row) {
+ uint16_t width = linestride_px;
+ uint16_t row = pixel_index / width;
+ uint16_t col = pixel_index % width;
+ pixel_index = col * pixels_per_byte + (row / pixels_per_byte) * pixels_per_byte * width + row % pixels_per_byte;
+ }
+ uint8_t shift = (pixel_index % pixels_per_byte) * colorspace->depth;
+ if (colorspace->reverse_pixels_in_byte) {
+ // Reverse the shift by subtracting it from the leftmost shift.
+ shift = (pixels_per_byte - 1) * colorspace->depth - shift;
+ }
+ VECTORIO_SHAPE_PIXEL_DEBUG(" buffer = %2d %d\n", output_pixel.pixel, colorspace->depth);
+ ((uint8_t*)buffer)[pixel_index / pixels_per_byte] |= output_pixel.pixel << shift;
+ }
+ }
+ }
+ mask_start_px += linestride_px - column_dirty_offset_px;
+ }
+#ifdef VECTORIO_PERF
+ uint64_t end = common_hal_time_monotonic_ns();
+ uint32_t pixels = (overlap.x2 - overlap.x1) * (overlap.y2 - overlap.y1);
+ VECTORIO_PERF("draw %16s -> shape:{%4dpx, %4.1fms,%9.1fpps fill} shape_pixels:{%6.1fus total, %4.1fus/px}\n",
+ mp_obj_get_type_str(self->ishape.shape),
+ (overlap.x2 - overlap.x1) * (overlap.y2 - overlap.y1),
+ (double)((end - start) / 1000000.0),
+ (double)(max(1, pixels * (1000000000.0 / (end - start)))),
+ (double)(pixel_time / 1000.0),
+ (double)(pixel_time / 1000.0 / pixels)
+ );
+#endif
+ VECTORIO_SHAPE_DEBUG(" -> pixels:%4d\n");
+ return full_coverage;
+}
+
+
+void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self) {
+ if ( !self->dirty ) {
+ return;
+ }
+ VECTORIO_SHAPE_DEBUG("%p finish_refresh was:{(%3d,%3d), (%3d,%3d)}\n", self, self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+ self->dirty = false;
+ // Reset dirty area tracking to current footprint
+ _get_screen_area(self, &self->ephemeral_dirty_area);
+ self->ephemeral_dirty_area.next = NULL;
+ VECTORIO_SHAPE_DEBUG("%p finish_refresh now:{(%3d,%3d), (%3d,%3d)}\n", self, self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+
+ if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) {
+ displayio_palette_finish_refresh(self->pixel_shader);
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) {
+ displayio_colorconverter_finish_refresh(self->pixel_shader);
+ }
+}
+
+
+// Assembles a singly linked list of dirty areas from all components on the display.
+displayio_area_t* vectorio_vector_shape_get_refresh_areas(vectorio_vector_shape_t *self, displayio_area_t* tail) {
+ if (self->dirty
+ || (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type) && displayio_palette_needs_refresh(self->pixel_shader))
+ || (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) && displayio_colorconverter_needs_refresh(self->pixel_shader))
+ ) {
+ VECTORIO_SHAPE_DEBUG("%p get_refresh_area dirty:%d {(%3d,%3d), (%3d,%3d)}", self, self->dirty, self->ephemeral_dirty_area.x1, self->ephemeral_dirty_area.y1, self->ephemeral_dirty_area.x2, self->ephemeral_dirty_area.y2);
+ common_hal_vectorio_vector_shape_set_dirty(self);
+ // vector.add_to_head
+ self->ephemeral_dirty_area.next = tail;
+ VECTORIO_SHAPE_DEBUG(" this_area: %p next: %p after: %p\n", &self->ephemeral_dirty_area, tail, tail == NULL ? NULL : tail->next);
+ return &self->ephemeral_dirty_area;
+ }
+ return tail;
+}
+
+void vectorio_vector_shape_update_transform(vectorio_vector_shape_t *self, displayio_buffer_transform_t *group_transform) {
+ self->absolute_transform = group_transform == NULL ? &null_transform : group_transform;
+ common_hal_vectorio_vector_shape_set_dirty(self);
+}
diff --git a/shared-module/vectorio/VectorShape.h b/shared-module/vectorio/VectorShape.h
new file mode 100644
index 000000000..56eb3d8a5
--- /dev/null
+++ b/shared-module/vectorio/VectorShape.h
@@ -0,0 +1,53 @@
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "py/obj.h"
+#include "shared-module/displayio/area.h"
+#include "shared-module/displayio/Palette.h"
+
+typedef void get_area_function(mp_obj_t shape, displayio_area_t *out_area);
+typedef uint32_t get_pixel_function(mp_obj_t shape, int16_t x, int16_t y);
+
+// This struct binds a shape's common Shape support functions (its vector shape interface)
+// to its instance pointer. We only check at construction time what the type of the
+// associated shape is and link the correct functions up.
+// Later when using the shape for drawing logic these functions may be invoked
+// unconditionally. This simplifies the addition of new types and restricts the
+// respective responsibilities of VectorShape and actual shape implementations.
+typedef struct {
+ mp_obj_t shape;
+ get_area_function *get_area;
+ get_pixel_function *get_pixel;
+} vectorio_ishape_t;
+
+typedef struct {
+ mp_obj_base_t base;
+ vectorio_ishape_t ishape;
+ mp_obj_t pixel_shader;
+ int16_t x;
+ int16_t y;
+ displayio_buffer_transform_t *absolute_transform;
+ bool dirty; // True if we need to draw
+ // Tracks current shape footprint and expands outward as the shape dirties and changes.
+ // This is suboptimal if you move your shape far. Could add more state to only redraw
+ // exactly what we left behind.
+ displayio_area_t ephemeral_dirty_area;
+} vectorio_vector_shape_t;
+
+displayio_area_t* vectorio_vector_shape_get_refresh_areas(vectorio_vector_shape_t *self, displayio_area_t *tail);
+
+bool vectorio_vector_shape_get_dirty_area(vectorio_vector_shape_t *self, displayio_area_t *current_dirty_area);
+
+// Area is always in absolute screen coordinates.
+bool vectorio_vector_shape_fill_area(vectorio_vector_shape_t *self, const _displayio_colorspace_t *colorspace, const displayio_area_t *area, uint32_t *mask, uint32_t *buffer);
+
+// Fills in out_area with the maximum bounds of all related pixels in the last rendered frame. Returns
+// false if the vector shape wasn't rendered in the last frame.
+bool vectorio_vector_shape_get_previous_area(vectorio_vector_shape_t *self, displayio_area_t *out_area);
+void vectorio_vector_shape_finish_refresh(vectorio_vector_shape_t *self);
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_SHAPE_H
diff --git a/shared-module/vectorio/__init__.c b/shared-module/vectorio/__init__.c
new file mode 100644
index 000000000..f5227ef01
--- /dev/null
+++ b/shared-module/vectorio/__init__.c
@@ -0,0 +1,2 @@
+
+// Don't need anything in here yet
diff --git a/shared-module/vectorio/__init__.h b/shared-module/vectorio/__init__.h
new file mode 100644
index 000000000..8da85bba4
--- /dev/null
+++ b/shared-module/vectorio/__init__.h
@@ -0,0 +1,14 @@
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_INIT_H
+#define MICROPY_INCLUDED_SHARED_MODULE_VECTORIO_INIT_H
+
+#include "py/obj.h"
+
+typedef void event_function(mp_obj_t obj);
+
+typedef struct {
+ mp_obj_t obj;
+ event_function *event;
+} vectorio_event_t;
+
+
+#endif
diff --git a/shared-module/wiznet/wiznet5k.c b/shared-module/wiznet/wiznet5k.c
index 3c5c5f0c8..a603a5593 100644
--- a/shared-module/wiznet/wiznet5k.c
+++ b/shared-module/wiznet/wiznet5k.c
@@ -379,12 +379,12 @@ void wiznet5k_socket_deinit(mod_network_socket_obj_t *socket) {
}
/// Create and return a WIZNET5K object.
-mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in) {
+mp_obj_t wiznet5k_create(busio_spi_obj_t *spi_in, const mcu_pin_obj_t *cs_in, const mcu_pin_obj_t *rst_in) {
// init the wiznet5k object
wiznet5k_obj.base.type = (mp_obj_type_t*)&mod_network_nic_type_wiznet5k;
wiznet5k_obj.cris_state = 0;
- wiznet5k_obj.spi = MP_OBJ_TO_PTR(spi_in);
+ wiznet5k_obj.spi = spi_in;
wiznet5k_obj.socket_used = 0;
wiznet5k_obj.dhcp_socket = -1;
diff --git a/shared-module/wiznet/wiznet5k.h b/shared-module/wiznet/wiznet5k.h
index 0154831c7..19823ae55 100644
--- a/shared-module/wiznet/wiznet5k.h
+++ b/shared-module/wiznet/wiznet5k.h
@@ -59,7 +59,7 @@ int wiznet5k_socket_ioctl(mod_network_socket_obj_t *socket, mp_uint_t request, m
void wiznet5k_socket_timer_tick(mod_network_socket_obj_t *socket);
void wiznet5k_socket_deinit(mod_network_socket_obj_t *socket);
mp_obj_t wiznet5k_socket_disconnect(mp_obj_t self_in);
-mp_obj_t wiznet5k_create(mp_obj_t spi_in, mp_obj_t cs_in, mp_obj_t rst_in);
+mp_obj_t wiznet5k_create(busio_spi_obj_t *spi_in, const mcu_pin_obj_t *cs_in, const mcu_pin_obj_t *rst_in);
int wiznet5k_start_dhcp(void);
int wiznet5k_stop_dhcp(void);