summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/displayio/ColorConverter.c41
-rw-r--r--shared-module/displayio/ColorConverter.h39
-rw-r--r--shared-module/displayio/Group.c16
-rw-r--r--shared-module/displayio/Group.h1
-rw-r--r--shared-module/displayio/OnDiskBitmap.c93
-rw-r--r--shared-module/displayio/OnDiskBitmap.h47
-rw-r--r--shared-module/displayio/Sprite.c33
-rw-r--r--shared-module/displayio/Sprite.h3
8 files changed, 259 insertions, 14 deletions
diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c
new file mode 100644
index 000000000..d20b24c01
--- /dev/null
+++ b/shared-module/displayio/ColorConverter.c
@@ -0,0 +1,41 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/displayio/ColorConverter.h"
+
+void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self) {
+}
+
+bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, uint32_t input_color, uint16_t* output_color) {
+ // TODO(tannewt): Validate the color input against the input format.
+ uint32_t r5 = (input_color >> 19);
+ uint32_t g6 = (input_color >> 10) & 0x3f;
+ uint32_t b5 = (input_color >> 3) & 0x1f;
+ uint32_t packed = r5 << 11 | g6 << 5 | b5;
+ // swap bytes
+ *output_color = __builtin_bswap16(packed);
+ return true;
+}
diff --git a/shared-module/displayio/ColorConverter.h b/shared-module/displayio/ColorConverter.h
new file mode 100644
index 000000000..62ef2eaab
--- /dev/null
+++ b/shared-module/displayio/ColorConverter.h
@@ -0,0 +1,39 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H
+#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "py/obj.h"
+
+typedef struct {
+ mp_obj_base_t base;
+} displayio_colorconverter_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H
diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c
index 885131d47..52b89b5ae 100644
--- a/shared-module/displayio/Group.c
+++ b/shared-module/displayio/Group.c
@@ -40,6 +40,18 @@ void common_hal_displayio_group_append(displayio_group_t* self, mp_obj_t layer)
}
self->children[self->size] = layer;
self->size++;
+ self->needs_refresh = true;
+}
+
+mp_obj_t common_hal_displayio_group_pop(displayio_group_t* self) {
+ if (self->size == 0) {
+ mp_raise_IndexError(translate("Group empty"));
+ }
+ self->size--;
+ mp_obj_t item = self->children[self->size];
+ self->children[self->size] = NULL;
+ self->needs_refresh = true;
+ return item;
}
void displayio_group_construct(displayio_group_t* self, mp_obj_t* child_array, uint32_t max_size) {
@@ -65,6 +77,9 @@ bool displayio_group_get_pixel(displayio_group_t *self, int16_t x, int16_t y, ui
}
bool displayio_group_needs_refresh(displayio_group_t *self) {
+ if (self->needs_refresh) {
+ return true;
+ }
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i];
if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) {
@@ -78,6 +93,7 @@ bool displayio_group_needs_refresh(displayio_group_t *self) {
}
void displayio_group_finish_refresh(displayio_group_t *self) {
+ self->needs_refresh = false;
for (int32_t i = self->size - 1; i >= 0 ; i--) {
mp_obj_t layer = self->children[i];
if (MP_OBJ_IS_TYPE(layer, &displayio_sprite_type)) {
diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h
index 7f3e5646a..272f3114c 100644
--- a/shared-module/displayio/Group.h
+++ b/shared-module/displayio/Group.h
@@ -39,6 +39,7 @@ typedef struct {
uint16_t size;
uint16_t max_size;
mp_obj_t* children;
+ bool needs_refresh;
} displayio_group_t;
diff --git a/shared-module/displayio/OnDiskBitmap.c b/shared-module/displayio/OnDiskBitmap.c
new file mode 100644
index 000000000..6e4a4c6f0
--- /dev/null
+++ b/shared-module/displayio/OnDiskBitmap.c
@@ -0,0 +1,93 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#include "shared-bindings/displayio/OnDiskBitmap.h"
+
+#include <string.h>
+
+#include "py/mperrno.h"
+#include "py/runtime.h"
+
+static uint32_t read_word(uint16_t* bmp_header, uint16_t index) {
+ return bmp_header[index] | bmp_header[index + 1] << 16;
+}
+
+void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self, pyb_file_obj_t* file) {
+ // Load the wave
+ self->file = file;
+ uint16_t bmp_header[24];
+ f_rewind(&self->file->fp);
+ UINT bytes_read;
+ if (f_read(&self->file->fp, bmp_header, 48, &bytes_read) != FR_OK) {
+ mp_raise_OSError(MP_EIO);
+ }
+ if (bytes_read != 48 ||
+ memcmp(bmp_header, "BM", 2) != 0) {
+ mp_raise_ValueError(translate("Invalid BMP file"));
+ }
+
+ // We can't cast because we're not aligned.
+ self->data_offset = read_word(bmp_header, 5);
+
+ uint32_t header_size = read_word(bmp_header, 7);
+ uint32_t compression = read_word(bmp_header, 15);
+ if (!(header_size == 12 || header_size == 40 || header_size == 108 || header_size == 124) ||
+ !(compression == 0)) {
+ mp_raise_ValueError_varg(translate("Only Windows format, uncompressed BMP supported %d"), header_size);
+ }
+ // TODO(tannewt): Support bitfield compressed colors since RGB565 can be produced by the GIMP.
+ uint16_t bits_per_pixel = bmp_header[14];
+ if (bits_per_pixel < 24) {
+ mp_raise_ValueError_varg(translate("Only true color (24 bpp or higher) BMP supported %x"), bits_per_pixel);
+ }
+ self->bytes_per_pixel = bits_per_pixel / 8;
+ self->width = read_word(bmp_header, 9);
+ self->height = read_word(bmp_header, 11);
+ uint32_t byte_width = self->width * self->bytes_per_pixel;
+ self->stride = byte_width;
+ // Rows are word aligned.
+ if (self->stride % 4 != 0) {
+ self->stride += 4 - self->stride % 4;
+ }
+}
+
+
+uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *self,
+ int16_t x, int16_t y) {
+ if (x < 0 || x >= self->width || y < 0 || y >= self->height) {
+ return 0;
+ }
+ uint32_t location = self->data_offset + (self->height - y) * self->stride + x * self->bytes_per_pixel;
+ // We don't cache here because the underlying FS caches sectors.
+ f_lseek(&self->file->fp, location);
+ UINT bytes_read;
+ uint32_t pixel = 0;
+ uint32_t result = f_read(&self->file->fp, &pixel, self->bytes_per_pixel, &bytes_read);
+ if (result == FR_OK) {
+ return pixel;
+ }
+ return 0;
+}
diff --git a/shared-module/displayio/OnDiskBitmap.h b/shared-module/displayio/OnDiskBitmap.h
new file mode 100644
index 000000000..31d6c9550
--- /dev/null
+++ b/shared-module/displayio/OnDiskBitmap.h
@@ -0,0 +1,47 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
+ * of this software and associated documentation files (the "Software"), to deal
+ * in the Software without restriction, including without limitation the rights
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
+ * copies of the Software, and to permit persons to whom the Software is
+ * furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be included in
+ * all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
+ * THE SOFTWARE.
+ */
+
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKBITMAP_H
+#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKBITMAP_H
+
+#include <stdbool.h>
+#include <stdint.h>
+
+#include "py/obj.h"
+
+#include "extmod/vfs_fat.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ uint16_t width;
+ uint16_t height;
+ uint16_t data_offset;
+ uint16_t stride;
+ pyb_file_obj_t* file;
+ uint8_t bytes_per_pixel;
+} displayio_ondiskbitmap_t;
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKBITMAP_H
diff --git a/shared-module/displayio/Sprite.c b/shared-module/displayio/Sprite.c
index b584c0569..4fe8d1ff6 100644
--- a/shared-module/displayio/Sprite.c
+++ b/shared-module/displayio/Sprite.c
@@ -26,15 +26,17 @@
#include "shared-bindings/displayio/Sprite.h"
-
#include "shared-bindings/displayio/Bitmap.h"
+#include "shared-bindings/displayio/ColorConverter.h"
+#include "shared-bindings/displayio/OnDiskBitmap.h"
+#include "shared-bindings/displayio/Palette.h"
void common_hal_displayio_sprite_construct(displayio_sprite_t *self, mp_obj_t bitmap,
- mp_obj_t palette, uint16_t width, uint16_t height, uint16_t x, uint16_t y) {
+ mp_obj_t pixel_shader, uint16_t width, uint16_t height, uint16_t x, uint16_t y) {
self->width = width;
self->height = height;
self->bitmap = bitmap;
- self->palette = palette;
+ self->pixel_shader = pixel_shader;
self->x = x;
self->y = y;
}
@@ -51,12 +53,12 @@ void common_hal_displayio_sprite_set_position(displayio_sprite_t *self, int16_t
}
-displayio_palette_t* common_hal_displayio_sprite_get_palette(displayio_sprite_t *self) {
- return self->palette;
+mp_obj_t common_hal_displayio_sprite_get_pixel_shader(displayio_sprite_t *self) {
+ return self->pixel_shader;
}
-void common_hal_displayio_sprite_set_palette(displayio_sprite_t *self, displayio_palette_t* palette) {
- self->palette = palette;
+void common_hal_displayio_sprite_set_pixel_shader(displayio_sprite_t *self, mp_obj_t pixel_shader) {
+ self->pixel_shader = pixel_shader;
self->needs_refresh = true;
}
@@ -66,12 +68,19 @@ bool displayio_sprite_get_pixel(displayio_sprite_t *self, int16_t x, int16_t y,
if (y < 0 || y >= self->height || x >= self->width || x < 0) {
return false;
}
+ uint32_t value;
+ if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) {
+ value = common_hal_displayio_bitmap_get_pixel(self->bitmap, x, y);
+ } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) {
+ value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, x, y);
+ }
- uint32_t value = common_hal_displayio_bitmap_get_pixel(self->bitmap, x, y);
- if (self->palette == mp_const_none) {
+ if (self->pixel_shader == mp_const_none) {
*pixel = value;
return true;
- } else if (MP_OBJ_IS_TYPE(self->palette, &displayio_palette_type) && displayio_palette_get_color(self->palette, value, pixel)) {
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type) && displayio_palette_get_color(self->pixel_shader, value, pixel)) {
+ return true;
+ } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) && common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) {
return true;
}
@@ -79,12 +88,12 @@ bool displayio_sprite_get_pixel(displayio_sprite_t *self, int16_t x, int16_t y,
}
bool displayio_sprite_needs_refresh(displayio_sprite_t *self) {
- return self->needs_refresh || displayio_palette_needs_refresh(self->palette);
+ return self->needs_refresh || displayio_palette_needs_refresh(self->pixel_shader);
}
void displayio_sprite_finish_refresh(displayio_sprite_t *self) {
self->needs_refresh = false;
- displayio_palette_finish_refresh(self->palette);
+ displayio_palette_finish_refresh(self->pixel_shader);
// TODO(tannewt): We could double buffer changes to position and move them over here.
// That way they won't change during a refresh and tear.
}
diff --git a/shared-module/displayio/Sprite.h b/shared-module/displayio/Sprite.h
index 213130af8..dc368c4b7 100644
--- a/shared-module/displayio/Sprite.h
+++ b/shared-module/displayio/Sprite.h
@@ -31,12 +31,11 @@
#include <stdint.h>
#include "py/obj.h"
-#include "shared-bindings/displayio/Palette.h"
typedef struct {
mp_obj_base_t base;
mp_obj_t bitmap;
- displayio_palette_t* palette;
+ mp_obj_t pixel_shader;
uint16_t x;
uint16_t y;
uint16_t width;