diff options
| author | Dan Halbert <halbert@halwitz.org> | 2018-01-02 21:25:41 -0500 |
|---|---|---|
| committer | Dan Halbert <halbert@halwitz.org> | 2018-01-02 21:25:41 -0500 |
| commit | 065e82015f0d2bd6732c62b7375fcc7af87fa0cf (patch) | |
| tree | e48fa8caa5675d529d335a2b154e69eba8199cff /shared-module | |
| parent | ce81c8dda9686d3cd78d9c5383b6982c148ac7ec (diff) | |
merge from 2.2.0 + fix up board defs
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/_stage/Layer.c | 105 | ||||
| -rw-r--r-- | shared-module/_stage/Layer.h | 48 | ||||
| -rw-r--r-- | shared-module/_stage/Text.c | 67 | ||||
| -rw-r--r-- | shared-module/_stage/Text.h | 46 | ||||
| -rw-r--r-- | shared-module/_stage/__init__.c | 76 | ||||
| -rw-r--r-- | shared-module/_stage/__init__.h | 42 |
6 files changed, 384 insertions, 0 deletions
diff --git a/shared-module/_stage/Layer.c b/shared-module/_stage/Layer.c new file mode 100644 index 000000000..4c9c46d7c --- /dev/null +++ b/shared-module/_stage/Layer.c @@ -0,0 +1,105 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 "Layer.h" +#include "__init__.h" + + +// Get the color of the pixel on the layer. +uint16_t get_layer_pixel(layer_obj_t *layer, int16_t x, uint16_t y) { + + // Shift by the layer's position offset. + x -= layer->x; + y -= layer->y; + + // Bounds check. + if ((x < 0) || (x >= layer->width << 4) || + (y < 0) || (y >= layer->height << 4)) { + return TRANSPARENT; + } + + // Get the tile from the grid location or from sprite frame. + uint8_t frame = layer->frame; + if (layer->map) { + uint8_t tx = x >> 4; + uint8_t ty = y >> 4; + + frame = layer->map[(ty * layer->width + tx) >> 1]; + if (tx & 0x01) { + frame &= 0x0f; + } else { + frame >>= 4; + } + } + + // Get the position within the tile. + x &= 0x0f; + y &= 0x0f; + + // Rotate the image. + uint8_t ty = y; // Temporary variable for swapping. + switch (layer->rotation) { + case 1: // 90 degrees clockwise + y = 15 - x; + x = ty; + break; + case 2: // 180 degrees + y = 15 - ty; + x = 15 - x; + break; + case 3: // 90 degrees counter-clockwise + y = x; + x = 15 - ty; + break; + case 4: // 0 degrees, mirrored + x = 15 - x; + break; + case 5: // 90 degrees clockwise, mirrored + y = x; + x = ty; + break; + case 6: // 180 degrees, mirrored + y = 15 - ty; + break; + case 7: // 90 degrees counter-clockwise, mirrored + y = 15 - x; + x = 15 - ty; + break; + default: // 0 degrees + break; + } + + // Get the value of the pixel. + uint8_t pixel = layer->graphic[(frame << 7) + (y << 3) + (x >> 1)]; + if (x & 0x01) { + pixel &= 0x0f; + } else { + pixel >>= 4; + } + + // Convert to 16-bit color using the palette. + return layer->palette[pixel << 1] | layer->palette[(pixel << 1) + 1] << 8; +} diff --git a/shared-module/_stage/Layer.h b/shared-module/_stage/Layer.h new file mode 100644 index 000000000..c46f71349 --- /dev/null +++ b/shared-module/_stage/Layer.h @@ -0,0 +1,48 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE__STAGE_LAYER_H +#define MICROPY_INCLUDED_SHARED_MODULE__STAGE_LAYER_H + +#include <stdint.h> +#include <stdbool.h> + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t *map; + uint8_t *graphic; + uint8_t *palette; + int16_t x, y; + uint8_t width, height; + uint8_t frame; + uint8_t rotation; +} layer_obj_t; + +uint16_t get_layer_pixel(layer_obj_t *layer, int16_t x, uint16_t y); + +#endif // MICROPY_INCLUDED_SHARED_MODULE__STAGE_LAYER diff --git a/shared-module/_stage/Text.c b/shared-module/_stage/Text.c new file mode 100644 index 000000000..307f9bbfa --- /dev/null +++ b/shared-module/_stage/Text.c @@ -0,0 +1,67 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 "Text.h" +#include "__init__.h" + + +// Get the color of the pixel on the text. +uint16_t get_text_pixel(text_obj_t *text, int16_t x, uint16_t y) { + + // Shift by the text's position offset. + x -= text->x; + y -= text->y; + + // Bounds check. + if ((x < 0) || (x >= text->width << 3) || + (y < 0) || (y >= text->height << 3)) { + return TRANSPARENT; + } + + // Get the tile from the grid location or from sprite frame. + uint8_t tx = x >> 3; + uint8_t ty = y >> 3; + uint8_t c = text->chars[ty * text->width + tx]; + uint8_t color_offset = 0; + if (c & 0x80) { + color_offset = 4; + } + c &= 0x7f; + if (!c) { + return TRANSPARENT; + } + + // Get the position within the char. + x &= 0x07; + y &= 0x07; + + // Get the value of the pixel. + uint8_t pixel = text->font[(c << 4) + (y << 1) + (x >> 2)]; + pixel = ((pixel >> ((x & 0x03) << 1)) & 0x03) + color_offset; + + // Convert to 16-bit color using the palette. + return text->palette[pixel << 1] | text->palette[(pixel << 1) + 1] << 8; +} diff --git a/shared-module/_stage/Text.h b/shared-module/_stage/Text.h new file mode 100644 index 000000000..bc111e49c --- /dev/null +++ b/shared-module/_stage/Text.h @@ -0,0 +1,46 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE__STAGE_TEXT_H +#define MICROPY_INCLUDED_SHARED_MODULE__STAGE_TEXT_H + +#include <stdint.h> +#include <stdbool.h> + +#include "py/obj.h" + +typedef struct { + mp_obj_base_t base; + uint8_t *chars; + uint8_t *font; + uint8_t *palette; + int16_t x, y; + uint8_t width, height; +} text_obj_t; + +uint16_t get_text_pixel(text_obj_t *text, int16_t x, uint16_t y); + +#endif // MICROPY_INCLUDED_SHARED_MODULE__STAGE_TEXT diff --git a/shared-module/_stage/__init__.c b/shared-module/_stage/__init__.c new file mode 100644 index 000000000..1931b8940 --- /dev/null +++ b/shared-module/_stage/__init__.c @@ -0,0 +1,76 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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 "Layer.h" +#include "Text.h" +#include "__init__.h" +#include "shared-bindings/_stage/Layer.h" +#include "shared-bindings/_stage/Text.h" + + +bool render_stage(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, + mp_obj_t *layers, size_t layers_size, + uint16_t *buffer, size_t buffer_size, + busio_spi_obj_t *spi) { + + // TODO(deshipu): Do a collision check of each layer with the + // rectangle, and only process the layers that overlap with it. + + size_t index = 0; + for (uint8_t y = y0; y < y1; ++y) { + for (uint8_t x = x0; x < x1; ++x) { + for (size_t layer = 0; layer < layers_size; ++layer) { + uint16_t c = TRANSPARENT; + layer_obj_t *obj = MP_OBJ_TO_PTR(layers[layer]); + if (obj->base.type == &mp_type_layer) { + c = get_layer_pixel(obj, x, y); + } else if (obj->base.type == &mp_type_text) { + c = get_text_pixel((text_obj_t *)obj, x, y); + } + if (c != TRANSPARENT) { + buffer[index] = c; + break; + } + } + index += 1; + // The buffer is full, send it. + if (index >= buffer_size) { + if (!common_hal_busio_spi_write(spi, + ((uint8_t*)buffer), buffer_size * 2)) { + return false; + } + index = 0; + } + } + } + // Send the remaining data. + if (index) { + if (!common_hal_busio_spi_write(spi, ((uint8_t*)buffer), index * 2)) { + return false; + } + } + return true; +} diff --git a/shared-module/_stage/__init__.h b/shared-module/_stage/__init__.h new file mode 100644 index 000000000..326c72559 --- /dev/null +++ b/shared-module/_stage/__init__.h @@ -0,0 +1,42 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017 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. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE__STAGE_H +#define MICROPY_INCLUDED_SHARED_MODULE__STAGE_H + +#include "shared-bindings/busio/SPI.h" +#include <stdint.h> +#include <stdbool.h> +#include "py/obj.h" + +#define TRANSPARENT (0x1ff8) + +bool render_stage(uint8_t x0, uint8_t y0, uint8_t x1, uint8_t y1, + mp_obj_t *layers, size_t layers_size, + uint16_t *buffer, size_t buffer_size, + busio_spi_obj_t *spi); + +#endif // MICROPY_INCLUDED_SHARED_MODULE__STAGE |
