From ac3d99dc95ce64299440f1cc80b170a89dc53868 Mon Sep 17 00:00:00 2001
From: gamblor21 <mark.komus@gmail.com>
Date: Sat, 20 Nov 2021 13:41:18 -0600
Subject: [PATCH] Check map to display size

---
 locale/circuitpython.pot                | 4 ++++
 shared-bindings/is31fl3741/is31fl3741.c | 3 +++
 shared-module/is31fl3741/is31fl3741.c   | 5 ++++-
 3 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot
index ba8f7d3c07bf7..f6a442b77f088 100644
--- a/locale/circuitpython.pot
+++ b/locale/circuitpython.pot
@@ -1430,6 +1430,10 @@ msgstr ""
 msgid "Key must be 16, 24, or 32 bytes long"
 msgstr ""
 
+#: shared-module/is31fl3741/is31fl3741.c
+msgid "LED mappings must match display size"
+msgstr ""
+
 #: py/compile.c
 msgid "LHS of keyword arg must be an id"
 msgstr ""
diff --git a/shared-bindings/is31fl3741/is31fl3741.c b/shared-bindings/is31fl3741/is31fl3741.c
index 723687d227e46..aa98972b249c4 100644
--- a/shared-bindings/is31fl3741/is31fl3741.c
+++ b/shared-bindings/is31fl3741/is31fl3741.c
@@ -88,6 +88,9 @@ STATIC mp_obj_t is31fl3741_is31fl3741_make_new(const mp_obj_type_t *type, size_t
 
         self->scale_width = args[ARG_width].u_int / 3;
         self->scale_height = args[ARG_height].u_int / 3;
+    } else {
+        self->scale_width = args[ARG_width].u_int;
+        self->scale_height = args[ARG_height].u_int;
     }
 
     self->auto_gamma = args[ARG_gamma].u_bool;
diff --git a/shared-module/is31fl3741/is31fl3741.c b/shared-module/is31fl3741/is31fl3741.c
index 78d2d1efce339..c616d3a20c044 100644
--- a/shared-module/is31fl3741/is31fl3741.c
+++ b/shared-module/is31fl3741/is31fl3741.c
@@ -59,11 +59,14 @@ void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *sel
     // of the heap as well.
     gc_never_free(self->i2c);
 
-    // TODO mapping should be equal to height * width * 3
     mp_obj_t *items;
     size_t len;
     mp_obj_list_get(mapping, &len, &items);
 
+    if (len != (size_t)(self->scale_width * self->scale_height * 3)) {
+        mp_raise_ValueError(translate("LED mappings must match display size"));
+    }
+
     self->mapping = common_hal_is31fl3741_allocator_impl(sizeof(uint16_t) * len);
     for (size_t i = 0; i < len; i++) {
         mp_int_t value = mp_obj_get_int(items[i]);