diff options
| author | sommersoft <sommersoft@gmail.com> | 2019-03-07 18:51:14 -0600 |
|---|---|---|
| committer | sommersoft <sommersoft@gmail.com> | 2019-03-07 18:51:14 -0600 |
| commit | 3a738af5104fa334951427d1a446882a75f8eb71 (patch) | |
| tree | df9317398ef9c8684a932efec063ea412fbffd7d /shared-module | |
| parent | c4d627a877f8470122c1a73e9de94430749b3cb1 (diff) | |
| parent | 2de8236979ad987193a9a729dc379b612e442b84 (diff) | |
Merge branch 'master' of https://github.com/adafruit/circuitpython into en_ARR
Diffstat (limited to 'shared-module')
| -rw-r--r-- | shared-module/_pew/PewPew.c | 125 | ||||
| -rw-r--r-- | shared-module/_pew/PewPew.h | 48 | ||||
| -rw-r--r-- | shared-module/_pew/__init__.c | 85 | ||||
| -rw-r--r-- | shared-module/_pew/__init__.h | 32 | ||||
| -rw-r--r-- | shared-module/displayio/Display.c | 37 | ||||
| -rw-r--r-- | shared-module/displayio/OnDiskBitmap.c | 127 | ||||
| -rw-r--r-- | shared-module/displayio/OnDiskBitmap.h | 7 | ||||
| -rw-r--r-- | shared-module/displayio/__init__.c | 66 | ||||
| -rw-r--r-- | shared-module/storage/__init__.c | 5 |
9 files changed, 479 insertions, 53 deletions
diff --git a/shared-module/_pew/PewPew.c b/shared-module/_pew/PewPew.c new file mode 100644 index 000000000..c1d6aa5c3 --- /dev/null +++ b/shared-module/_pew/PewPew.c @@ -0,0 +1,125 @@ +/* + * 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("")); + } + 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 = 160; + + // 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/PewPew.h b/shared-module/_pew/PewPew.h new file mode 100644 index 000000000..da1cae0a2 --- /dev/null +++ b/shared-module/_pew/PewPew.h @@ -0,0 +1,48 @@ +/* + * 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. + */ + +#ifndef MICROPY_INCLUDED_PEW_PEWPEW_H +#define MICROPY_INCLUDED_PEW_PEWPEW_H + +#include <stdint.h> +#include "shared-bindings/digitalio/DigitalInOut.h" + +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; + +void pew_init(void); +void pewpew_interrupt_handler(uint8_t index); +void pew_reset(void); + +#endif // MICROPY_INCLUDED_PEW_PEWPEW_H diff --git a/shared-module/_pew/__init__.c b/shared-module/_pew/__init__.c new file mode 100644 index 000000000..90fba3990 --- /dev/null +++ b/shared-module/_pew/__init__.c @@ -0,0 +1,85 @@ +/* + * 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 >= 8) { + 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 == 4 || turn == 6) { + value = true; + } + case 1: + if (turn == 0) { + value = true; + } + 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/_pew/__init__.h b/shared-module/_pew/__init__.h new file mode 100644 index 000000000..4f953c610 --- /dev/null +++ b/shared-module/_pew/__init__.h @@ -0,0 +1,32 @@ +/* + * 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. + */ + +#ifndef MICROPY_INCLUDED_PEW_H +#define MICROPY_INCLUDED_PEW_H + +void pew_tick(void); + +#endif // MICROPY_INCLUDED_PEW_H diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c index f14a27b78..0e76a868f 100644 --- a/shared-module/displayio/Display.c +++ b/shared-module/displayio/Display.c @@ -69,7 +69,11 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self, self->bus = bus; uint32_t i = 0; - self->begin_transaction(self->bus); + while (!self->begin_transaction(self->bus)) { +#ifdef MICROPY_VM_HOOK_LOOP + MICROPY_VM_HOOK_LOOP ; +#endif + } while (i < init_sequence_len) { uint8_t *cmd = init_sequence + i; uint8_t data_size = *(cmd + 1); @@ -121,7 +125,7 @@ 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)) { - pwmout_result_t result = common_hal_pulseio_pwmout_construct(&self->backlight_pwm, backlight_pin, 0, 5000, false); + 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; common_hal_digitalio_digitalinout_construct(&self->backlight_inout, backlight_pin); @@ -147,7 +151,8 @@ void common_hal_displayio_display_refresh_soon(displayio_display_obj_t* self) { int32_t common_hal_displayio_display_wait_for_frame(displayio_display_obj_t* self) { uint64_t last_refresh = self->last_refresh; - while (last_refresh == self->last_refresh) { + // Don't try to refresh if we got an exception. + while (last_refresh == self->last_refresh && MP_STATE_VM(mp_pending_exception) == NULL) { MICROPY_VM_HOOK_LOOP } return 0; @@ -157,6 +162,14 @@ bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t* s return self->auto_brightness; } +uint16_t common_hal_displayio_display_get_width(displayio_display_obj_t* self){ + return self->width; +} + +uint16_t common_hal_displayio_display_get_height(displayio_display_obj_t* self){ + return self->height; +} + void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* self, bool auto_brightness) { self->auto_brightness = auto_brightness; } @@ -189,9 +202,16 @@ bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, return ok; } -void displayio_display_start_region_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { +bool displayio_display_begin_transaction(displayio_display_obj_t* self) { + return self->begin_transaction(self->bus); +} + +void displayio_display_end_transaction(displayio_display_obj_t* self) { + self->end_transaction(self->bus); +} + +void displayio_display_set_region_to_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { // TODO(tannewt): Handle displays with single byte bounds. - self->begin_transaction(self->bus); uint16_t data[2]; self->send(self->bus, true, &self->set_column_command, 1); data[0] = __builtin_bswap16(x0 + self->colstart); @@ -204,10 +224,6 @@ void displayio_display_start_region_update(displayio_display_obj_t* self, uint16 self->send(self->bus, true, &self->write_ram_command, 1); } -void displayio_display_finish_region_update(displayio_display_obj_t* self) { - self->end_transaction(self->bus); -} - bool displayio_display_frame_queued(displayio_display_obj_t* self) { // Refresh at ~30 fps. return (ticks_ms - self->last_refresh) > 32; @@ -225,9 +241,8 @@ void displayio_display_finish_refresh(displayio_display_obj_t* self) { self->last_refresh = ticks_ms; } -bool displayio_display_send_pixels(displayio_display_obj_t* self, uint32_t* pixels, uint32_t length) { +void displayio_display_send_pixels(displayio_display_obj_t* self, uint32_t* pixels, uint32_t length) { self->send(self->bus, false, (uint8_t*) pixels, length * 4); - return true; } void displayio_display_update_backlight(displayio_display_obj_t* self) { diff --git a/shared-module/displayio/OnDiskBitmap.c b/shared-module/displayio/OnDiskBitmap.c index aa2ca3e10..d710bdae0 100644 --- a/shared-module/displayio/OnDiskBitmap.c +++ b/shared-module/displayio/OnDiskBitmap.c @@ -38,13 +38,13 @@ static uint32_t read_word(uint16_t* bmp_header, uint16_t index) { 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]; + uint16_t bmp_header[69]; f_rewind(&self->file->fp); UINT bytes_read; - if (f_read(&self->file->fp, bmp_header, 48, &bytes_read) != FR_OK) { + if (f_read(&self->file->fp, bmp_header, 138, &bytes_read) != FR_OK) { mp_raise_OSError(MP_EIO); } - if (bytes_read != 48 || + if (bytes_read != 138 || memcmp(bmp_header, "BM", 2) != 0) { mp_raise_ValueError(translate("Invalid BMP file")); } @@ -53,25 +53,68 @@ void common_hal_displayio_ondiskbitmap_construct(displayio_ondiskbitmap_t *self, 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; + uint32_t compression = read_word(bmp_header, 15); + uint32_t number_of_colors = read_word(bmp_header, 23); + + bool indexed = ((bits_per_pixel <= 8) && (number_of_colors != 0)); + self->bitfield_compressed = (compression == 3); + self->bits_per_pixel = bits_per_pixel; 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; + + if (bits_per_pixel == 16){ + if (((header_size >= 56)) || (self->bitfield_compressed)) { + self->r_bitmask = read_word(bmp_header, 27); + self->g_bitmask = read_word(bmp_header, 29); + self->b_bitmask = read_word(bmp_header, 31); + + } else { // no compression or short header means 5:5:5 + self->r_bitmask = 0x7c00; + self->g_bitmask = 0x3e0; + self->b_bitmask = 0x1f; + } + } else if ((indexed) && (self->bits_per_pixel != 1)) { + uint16_t palette_size = number_of_colors * sizeof(uint32_t); + uint16_t palette_offset = 0xe + header_size; + + self->palette_data = m_malloc(palette_size, false); + + f_rewind(&self->file->fp); + f_lseek(&self->file->fp, palette_offset); + + UINT palette_bytes_read; + if (f_read(&self->file->fp, self->palette_data, palette_size, &palette_bytes_read) != FR_OK) { + mp_raise_OSError(MP_EIO); + } + if (palette_bytes_read != palette_size) { + mp_raise_ValueError(translate("Unable to read color palette data")); + } + + + } else if (!(header_size == 12 || header_size == 40 || header_size == 108 || header_size == 124)) { + mp_raise_ValueError_varg(translate("Only Windows format, uncompressed BMP supported: given header size is %d"), header_size); + } + + if ((bits_per_pixel == 4 ) || (( bits_per_pixel == 8) && (number_of_colors == 0))) { + mp_raise_ValueError_varg(translate("Only monochrome, indexed 8bpp, and 16bpp or greater BMPs supported: %d bpp given"), bits_per_pixel); } + + if (self->bits_per_pixel >=8){ + self->stride = (self->width * (bits_per_pixel / 8)); + // Rows are word aligned. + if (self->stride % 4 != 0) { + self->stride += 4 - self->stride % 4; + } + + } else { + uint32_t bit_stride = self->width; + if (bit_stride % 32 != 0) { + bit_stride += 32 - bit_stride % 32; + } + self->stride = (bit_stride / 8); + } + } @@ -80,14 +123,54 @@ uint32_t common_hal_displayio_ondiskbitmap_get_pixel(displayio_ondiskbitmap_t *s 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; + uint32_t location; + uint8_t bytes_per_pixel = (self->bits_per_pixel / 8) ? (self->bits_per_pixel /8) : 1; + if (self->bits_per_pixel >= 8){ + location = self->data_offset + (self->height - y) * self->stride + x * bytes_per_pixel; + } else { + location = self->data_offset + (self->height - y) * self->stride + x / 8; + } // 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); + uint32_t pixel_data = 0; + uint32_t result = f_read(&self->file->fp, &pixel_data, bytes_per_pixel, &bytes_read); if (result == FR_OK) { - return pixel; + uint32_t tmp = 0; + uint8_t red; + uint8_t green; + uint8_t blue; + if (self->bits_per_pixel == 1){ + uint8_t bit_offset = x%8; + tmp = ( pixel_data & (0x80 >> (bit_offset))) >> (7 - bit_offset); + if (tmp == 1) { + return 0x00FFFFFF; + } else { + return 0x00000000; + } + } else if (bytes_per_pixel == 1){ + blue = ((self->palette_data[pixel_data] & 0xFF) >> 0); + red = ((self->palette_data[pixel_data] & 0xFF0000) >> 16); + green = ((self->palette_data[pixel_data] & 0xFF00) >> 8); + tmp = (red << 16 | green << 8 | blue ); + return tmp; + } else if (bytes_per_pixel == 2) { + if (self->g_bitmask == 0x07e0) { // 565 + red =((pixel_data & self->r_bitmask) >>11); + green = ((pixel_data & self->g_bitmask) >>5); + blue = ((pixel_data & self->b_bitmask) >> 0); + } else { // 555 + red =((pixel_data & self->r_bitmask) >>10); + green = ((pixel_data & self->g_bitmask) >>4); + blue = ((pixel_data & self->b_bitmask) >> 0); + } + tmp = (red << 19 | green << 10 | blue << 3); + return tmp; + } else if ((bytes_per_pixel == 4) && (self->bitfield_compressed)) { + return pixel_data & 0x00FFFFFF; + } else { + return pixel_data; + } } return 0; } diff --git a/shared-module/displayio/OnDiskBitmap.h b/shared-module/displayio/OnDiskBitmap.h index 31d6c9550..28426f11b 100644 --- a/shared-module/displayio/OnDiskBitmap.h +++ b/shared-module/displayio/OnDiskBitmap.h @@ -40,8 +40,13 @@ typedef struct { uint16_t height; uint16_t data_offset; uint16_t stride; + uint32_t r_bitmask; + uint32_t g_bitmask; + uint32_t b_bitmask; + bool bitfield_compressed; pyb_file_obj_t* file; - uint8_t bytes_per_pixel; + uint8_t bits_per_pixel; + uint32_t* palette_data; } displayio_ondiskbitmap_t; #endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_ONDISKBITMAP_H diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index 0d35abf0f..546a46b1e 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -2,7 +2,9 @@ #include <string.h> #include "shared-module/displayio/__init__.h" +#include "lib/utils/interrupt_char.h" #include "py/reload.h" +#include "py/runtime.h" #include "shared-bindings/displayio/Bitmap.h" #include "shared-bindings/displayio/Display.h" #include "shared-bindings/displayio/Group.h" @@ -20,29 +22,39 @@ static inline void swap(uint16_t* a, uint16_t* b) { *b = temp; } -bool refreshing_displays = false; +// Check for recursive calls to displayio_refresh_displays. +bool refresh_displays_in_progress = false; void displayio_refresh_displays(void) { + if (mp_hal_is_interrupted()) { + return; + } // Somehow reloads from the sdcard are being lost. So, cheat and reraise. - if (reload_requested) { + // But don't re-raise if already pending. + if (reload_requested && MP_STATE_VM(mp_pending_exception) == MP_OBJ_NULL) { mp_raise_reload_exception(); return; } - if (refreshing_displays) { + if (refresh_displays_in_progress) { + // Don't allow recursive calls to this routine. return; } - refreshing_displays = true; + + refresh_displays_in_progress = true; + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { if (displays[i].display.base.type == NULL || displays[i].display.base.type == &mp_type_NoneType) { + // Skip null display. continue; } displayio_display_obj_t* display = &displays[i].display; displayio_display_update_backlight(display); + // Time to refresh at specified frame rate? if (!displayio_display_frame_queued(display)) { - refreshing_displays = false; - return; + // Too soon. Try next display. + continue; } if (displayio_display_refresh_queued(display)) { // We compute the pixels. r and c are row and column to match the display memory @@ -54,7 +66,13 @@ void displayio_refresh_displays(void) { if (display->transpose_xy) { swap(&c1, &r1); } - displayio_display_start_region_update(display, c0, r0, c1, r1); + + if (!displayio_display_begin_transaction(display)) { + // Can't acquire display bus; skip updating this display. Try next display. + continue; + } + displayio_display_set_region_to_update(display, c0, r0, c1, r1); + displayio_display_end_transaction(display); uint16_t x0 = 0; uint16_t x1 = display->width - 1; @@ -88,6 +106,8 @@ void displayio_refresh_displays(void) { size_t index = 0; uint16_t buffer_size = 256; uint32_t buffer[buffer_size / 2]; + bool skip_this_display = false; + for (uint16_t y = starty; y0 <= y && y <= y1; y += dy) { for (uint16_t x = startx; x0 <= x && x <= x1; x += dx) { uint16_t* pixel = &(((uint16_t*)buffer)[index]); @@ -104,11 +124,14 @@ void displayio_refresh_displays(void) { index += 1; // The buffer is full, send it. if (index >= buffer_size) { - if (!displayio_display_send_pixels(display, buffer, buffer_size / 2) || reload_requested) { - displayio_display_finish_region_update(display); - refreshing_displays = false; - return; + if (!displayio_display_begin_transaction(display)) { + // Can't acquire display bus; skip the rest of the data. Try next display. + index = 0; + skip_this_display = true; + break; } + displayio_display_send_pixels(display, buffer, buffer_size / 2); + displayio_display_end_transaction(display); // TODO(tannewt): Make refresh displays faster so we don't starve other // background tasks. usb_background(); @@ -116,17 +139,26 @@ void displayio_refresh_displays(void) { } } } + + if (skip_this_display) { + // Go on to next display. + continue; + } // Send the remaining data. - if (index && !displayio_display_send_pixels(display, buffer, index * 2)) { - displayio_display_finish_region_update(display); - refreshing_displays = false; - return; + if (index) { + if (!displayio_display_begin_transaction(display)) { + // Can't get display bus. Skip the rest of the data. Try next display. + continue; + } + displayio_display_send_pixels(display, buffer, index * 2); } - displayio_display_finish_region_update(display); + displayio_display_end_transaction(display); } displayio_display_finish_refresh(display); } - refreshing_displays = false; + + // All done. + refresh_displays_in_progress = false; } void common_hal_displayio_release_displays(void) { diff --git a/shared-module/storage/__init__.c b/shared-module/storage/__init__.c index f09edd785..215e1356a 100644 --- a/shared-module/storage/__init__.c +++ b/shared-module/storage/__init__.c @@ -143,7 +143,7 @@ mp_obj_t common_hal_storage_getmount(const char *mount_path) { return storage_object_from_path(mount_path); } -void common_hal_storage_remount(const char *mount_path, bool readonly) { +void common_hal_storage_remount(const char *mount_path, bool readonly, bool disable_concurrent_write_protection) { if (strcmp(mount_path, "/") != 0) { mp_raise_OSError(MP_EINVAL); } @@ -156,7 +156,8 @@ void common_hal_storage_remount(const char *mount_path, bool readonly) { } #endif - supervisor_flash_set_usb_writable(readonly); + filesystem_set_internal_writable_by_usb(readonly); + filesystem_set_internal_concurrent_write_protection(!disable_concurrent_write_protection); } void common_hal_storage_erase_filesystem(void) { |
