summaryrefslogtreecommitdiff
path: root/shared-module
diff options
context:
space:
mode:
authorDan Halbert <halbert@halwitz.org>2019-09-04 21:56:13 -0400
committerDan Halbert <halbert@halwitz.org>2019-09-04 21:56:13 -0400
commitf3af2a6fb7a9b67dc81ab471f30e212453af4627 (patch)
tree7656cc49325cb37f878fcdd77faf142b76b739dd /shared-module
parent195de97c67c4d1daa6ec8659bbdf30b8e7f4fff6 (diff)
parent28e17feb93a097bbbf8fa033be088ba009793280 (diff)
Merge remote-tracking branch 'adafruit/master' into choose-usb-devices-xac
Diffstat (limited to 'shared-module')
-rw-r--r--shared-module/_stage/__init__.c27
-rw-r--r--shared-module/audiocore/__init__.c47
-rw-r--r--shared-module/audiomixer/Mixer.c (renamed from shared-module/audiocore/Mixer.c)277
-rw-r--r--shared-module/audiomixer/Mixer.h (renamed from shared-module/audiocore/Mixer.h)26
-rw-r--r--shared-module/audiomixer/MixerVoice.c87
-rw-r--r--shared-module/audiomixer/MixerVoice.h46
-rw-r--r--shared-module/audiomixer/__init__.c0
-rw-r--r--shared-module/audiomixer/__init__.h32
-rw-r--r--shared-module/displayio/Display.c4
-rw-r--r--shared-module/displayio/Group.c41
-rw-r--r--shared-module/displayio/Group.h12
-rw-r--r--shared-module/displayio/I2CDisplay.c2
-rw-r--r--shared-module/displayio/TileGrid.c54
-rw-r--r--shared-module/displayio/TileGrid.h22
14 files changed, 520 insertions, 157 deletions
diff --git a/shared-module/_stage/__init__.c b/shared-module/_stage/__init__.c
index 2c525be87..d5da74272 100644
--- a/shared-module/_stage/__init__.c
+++ b/shared-module/_stage/__init__.c
@@ -36,6 +36,22 @@ void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
uint16_t *buffer, size_t buffer_size,
displayio_display_obj_t *display, uint8_t scale) {
+
+ displayio_area_t area;
+ area.x1 = x0;
+ area.y1 = y0;
+ area.x2 = x1;
+ area.y2 = y1;
+ displayio_display_core_set_region_to_update(
+ &display->core, display->set_column_command, display->set_row_command,
+ NO_COMMAND, NO_COMMAND, display->data_as_commands, false, &area);
+
+ while (!displayio_display_core_begin_transaction(&display->core)) {
+ RUN_BACKGROUND_TASKS;
+ }
+ display->core.send(display->core.bus, DISPLAY_COMMAND,
+ CHIP_SELECT_TOGGLE_EVERY_BYTE,
+ &display->write_ram_command, 1);
size_t index = 0;
for (uint16_t y = y0; y < y1; ++y) {
for (uint8_t yscale = 0; yscale < scale; ++yscale) {
@@ -57,8 +73,9 @@ void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
index += 1;
// The buffer is full, send it.
if (index >= buffer_size) {
- display->core.send(display->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, ((uint8_t*)buffer),
- buffer_size * 2);
+ display->core.send(display->core.bus, DISPLAY_DATA,
+ CHIP_SELECT_UNTOUCHED,
+ ((uint8_t*)buffer), buffer_size * 2);
index = 0;
}
}
@@ -67,6 +84,10 @@ void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
}
// Send the remaining data.
if (index) {
- display->core.send(display->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, ((uint8_t*)buffer), index * 2);
+ display->core.send(display->core.bus, DISPLAY_DATA,
+ CHIP_SELECT_UNTOUCHED,
+ ((uint8_t*)buffer), index * 2);
}
+
+ displayio_display_core_end_transaction(&display->core);
}
diff --git a/shared-module/audiocore/__init__.c b/shared-module/audiocore/__init__.c
index 067cae5dd..f9762676f 100644
--- a/shared-module/audiocore/__init__.c
+++ b/shared-module/audiocore/__init__.c
@@ -27,13 +27,14 @@
#include "shared-module/audioio/__init__.h"
#include "py/obj.h"
-#include "shared-bindings/audiocore/Mixer.h"
#include "shared-bindings/audiocore/RawSample.h"
#include "shared-bindings/audiocore/WaveFile.h"
-#include "shared-module/audiocore/Mixer.h"
#include "shared-module/audiocore/RawSample.h"
#include "shared-module/audiocore/WaveFile.h"
+#include "shared-bindings/audiomixer/Mixer.h"
+#include "shared-module/audiomixer/Mixer.h"
+
uint32_t audiosample_sample_rate(mp_obj_t sample_obj) {
if (MP_OBJ_IS_TYPE(sample_obj, &audioio_rawsample_type)) {
audioio_rawsample_obj_t* sample = MP_OBJ_TO_PTR(sample_obj);
@@ -41,9 +42,11 @@ uint32_t audiosample_sample_rate(mp_obj_t sample_obj) {
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
return file->sample_rate;
- } else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_mixer_type)) {
- audioio_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
+ #if CIRCUITPY_AUDIOMIXER
+ } else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
+ audiomixer_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
return mixer->sample_rate;
+ #endif
}
return 16000;
}
@@ -55,9 +58,11 @@ uint8_t audiosample_bits_per_sample(mp_obj_t sample_obj) {
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
return file->bits_per_sample;
- } else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_mixer_type)) {
- audioio_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
+ #if CIRCUITPY_AUDIOMIXER
+ } else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
+ audiomixer_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
return mixer->bits_per_sample;
+ #endif
}
return 8;
}
@@ -69,9 +74,11 @@ uint8_t audiosample_channel_count(mp_obj_t sample_obj) {
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
return file->channel_count;
- } else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_mixer_type)) {
- audioio_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
+ #if CIRCUITPY_AUDIOMIXER
+ } else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
+ audiomixer_mixer_obj_t* mixer = MP_OBJ_TO_PTR(sample_obj);
return mixer->channel_count;
+ #endif
}
return 1;
}
@@ -83,9 +90,11 @@ void audiosample_reset_buffer(mp_obj_t sample_obj, bool single_channel, uint8_t
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
audioio_wavefile_reset_buffer(file, single_channel, audio_channel);
- } else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_mixer_type)) {
- audioio_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
- audioio_mixer_reset_buffer(file, single_channel, audio_channel);
+ #if CIRCUITPY_AUDIOMIXER
+ } else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
+ audiomixer_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
+ audiomixer_mixer_reset_buffer(file, single_channel, audio_channel);
+ #endif
}
}
@@ -99,9 +108,11 @@ audioio_get_buffer_result_t audiosample_get_buffer(mp_obj_t sample_obj,
} else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_wavefile_type)) {
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
return audioio_wavefile_get_buffer(file, single_channel, channel, buffer, buffer_length);
- } else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_mixer_type)) {
- audioio_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
- return audioio_mixer_get_buffer(file, single_channel, channel, buffer, buffer_length);
+ #if CIRCUITPY_AUDIOMIXER
+ } else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
+ audiomixer_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
+ return audiomixer_mixer_get_buffer(file, single_channel, channel, buffer, buffer_length);
+ #endif
}
return GET_BUFFER_DONE;
}
@@ -117,9 +128,11 @@ void audiosample_get_buffer_structure(mp_obj_t sample_obj, bool single_channel,
audioio_wavefile_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
audioio_wavefile_get_buffer_structure(file, single_channel, single_buffer, samples_signed,
max_buffer_length, spacing);
- } else if (MP_OBJ_IS_TYPE(sample_obj, &audioio_mixer_type)) {
- audioio_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
- audioio_mixer_get_buffer_structure(file, single_channel, single_buffer, samples_signed,
+ #if CIRCUITPY_AUDIOMIXER
+ } else if (MP_OBJ_IS_TYPE(sample_obj, &audiomixer_mixer_type)) {
+ audiomixer_mixer_obj_t* file = MP_OBJ_TO_PTR(sample_obj);
+ audiomixer_mixer_get_buffer_structure(file, single_channel, single_buffer, samples_signed,
max_buffer_length, spacing);
+ #endif
}
}
diff --git a/shared-module/audiocore/Mixer.c b/shared-module/audiomixer/Mixer.c
index da66bb36e..e0fb4fab8 100644
--- a/shared-module/audiocore/Mixer.c
+++ b/shared-module/audiomixer/Mixer.c
@@ -4,6 +4,8 @@
* The MIT License (MIT)
*
* Copyright (c) 2018 Scott Shawcroft for Adafruit Industries
+ * 2018 DeanM for Adafruit Industries
+ * 2019 Michael Schroeder
*
* 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,7 +26,8 @@
* THE SOFTWARE.
*/
-#include "shared-bindings/audiocore/Mixer.h"
+#include "shared-bindings/audiomixer/Mixer.h"
+#include "shared-bindings/audiomixer/MixerVoice.h"
#include <stdint.h>
@@ -32,24 +35,24 @@
#include "shared-module/audiocore/__init__.h"
#include "shared-module/audiocore/RawSample.h"
-void common_hal_audioio_mixer_construct(audioio_mixer_obj_t* self,
- uint8_t voice_count,
- uint32_t buffer_size,
- uint8_t bits_per_sample,
- bool samples_signed,
- uint8_t channel_count,
- uint32_t sample_rate) {
+void common_hal_audiomixer_mixer_construct(audiomixer_mixer_obj_t* self,
+ uint8_t voice_count,
+ uint32_t buffer_size,
+ uint8_t bits_per_sample,
+ bool samples_signed,
+ uint8_t channel_count,
+ uint32_t sample_rate) {
self->len = buffer_size / 2 / sizeof(uint32_t) * sizeof(uint32_t);
self->first_buffer = m_malloc(self->len, false);
if (self->first_buffer == NULL) {
- common_hal_audioio_mixer_deinit(self);
+ common_hal_audiomixer_mixer_deinit(self);
mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate first buffer"));
}
self->second_buffer = m_malloc(self->len, false);
if (self->second_buffer == NULL) {
- common_hal_audioio_mixer_deinit(self);
+ common_hal_audiomixer_mixer_deinit(self);
mp_raise_msg(&mp_type_MemoryError, translate("Couldn't allocate second buffer"));
}
@@ -58,130 +61,85 @@ void common_hal_audioio_mixer_construct(audioio_mixer_obj_t* self,
self->channel_count = channel_count;
self->sample_rate = sample_rate;
self->voice_count = voice_count;
-
- for (uint8_t i = 0; i < self->voice_count; i++) {
- self->voice[i].sample = NULL;
- }
}
-void common_hal_audioio_mixer_deinit(audioio_mixer_obj_t* self) {
+void common_hal_audiomixer_mixer_deinit(audiomixer_mixer_obj_t* self) {
self->first_buffer = NULL;
self->second_buffer = NULL;
}
-bool common_hal_audioio_mixer_deinited(audioio_mixer_obj_t* self) {
+bool common_hal_audiomixer_mixer_deinited(audiomixer_mixer_obj_t* self) {
return self->first_buffer == NULL;
}
-uint32_t common_hal_audioio_mixer_get_sample_rate(audioio_mixer_obj_t* self) {
+uint32_t common_hal_audiomixer_mixer_get_sample_rate(audiomixer_mixer_obj_t* self) {
return self->sample_rate;
}
-void common_hal_audioio_mixer_play(audioio_mixer_obj_t* self, mp_obj_t sample, uint8_t v, bool loop) {
- if (v >= self->voice_count) {
- mp_raise_ValueError(translate("Voice index too high"));
- }
- if (audiosample_sample_rate(sample) != self->sample_rate) {
- mp_raise_ValueError(translate("The sample's sample rate does not match the mixer's"));
- }
- if (audiosample_channel_count(sample) != self->channel_count) {
- mp_raise_ValueError(translate("The sample's channel count does not match the mixer's"));
- }
- if (audiosample_bits_per_sample(sample) != self->bits_per_sample) {
- mp_raise_ValueError(translate("The sample's bits_per_sample does not match the mixer's"));
- }
- bool single_buffer;
- bool samples_signed;
- uint32_t max_buffer_length;
- uint8_t spacing;
- audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed,
- &max_buffer_length, &spacing);
- if (samples_signed != self->samples_signed) {
- mp_raise_ValueError(translate("The sample's signedness does not match the mixer's"));
- }
- audioio_mixer_voice_t* voice = &self->voice[v];
- voice->sample = sample;
- voice->loop = loop;
-
- audiosample_reset_buffer(sample, false, 0);
- audioio_get_buffer_result_t result = audiosample_get_buffer(sample, false, 0, (uint8_t**) &voice->remaining_buffer, &voice->buffer_length);
- // Track length in terms of words.
- voice->buffer_length /= sizeof(uint32_t);
- voice->more_data = result == GET_BUFFER_MORE_DATA;
-}
-
-void common_hal_audioio_mixer_stop_voice(audioio_mixer_obj_t* self, uint8_t voice) {
- self->voice[voice].sample = NULL;
-}
-
-bool common_hal_audioio_mixer_get_playing(audioio_mixer_obj_t* self) {
- for (int32_t v = 0; v < self->voice_count; v++) {
- if (self->voice[v].sample != NULL) {
+bool common_hal_audiomixer_mixer_get_playing(audiomixer_mixer_obj_t* self) {
+ for (uint8_t v = 0; v < self->voice_count; v++) {
+ if (common_hal_audiomixer_mixervoice_get_playing(MP_OBJ_TO_PTR(self->voice[v]))) {
return true;
}
}
return false;
}
-void audioio_mixer_reset_buffer(audioio_mixer_obj_t* self,
- bool single_channel,
- uint8_t channel) {
- for (int32_t i = 0; i < self->voice_count; i++) {
- self->voice[i].sample = NULL;
+void audiomixer_mixer_reset_buffer(audiomixer_mixer_obj_t* self,
+ bool single_channel,
+ uint8_t channel) {
+ for (uint8_t i = 0; i < self->voice_count; i++) {
+ common_hal_audiomixer_mixervoice_stop(self->voice[i]);
}
}
uint32_t add8signed(uint32_t a, uint32_t b) {
- #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
- return __QADD8(a, b);
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ return __SHADD8(a, b);
#else
uint32_t result = 0;
for (int8_t i = 0; i < 4; i++) {
int8_t ai = a >> (sizeof(int8_t) * 8 * i);
int8_t bi = b >> (sizeof(int8_t) * 8 * i);
- int32_t intermediate = (int32_t) ai + bi;
+ int32_t intermediate = (int32_t) ai + bi / 2;
if (intermediate > CHAR_MAX) {
intermediate = CHAR_MAX;
} else if (intermediate < CHAR_MIN) {
- //intermediate = CHAR_MIN;
+ intermediate = CHAR_MIN;
}
- result |= (((uint32_t) intermediate) & 0xff) << (sizeof(int8_t) * 8 * i);
+ result |= ((uint32_t) intermediate & 0xff) << (sizeof(int8_t) * 8 * i);
}
return result;
#endif
}
uint32_t add8unsigned(uint32_t a, uint32_t b) {
- #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
- // Subtract out the DC offset, add and then shift back.
- a = __USUB8(a, 0x80808080);
- b = __USUB8(b, 0x80808080);
- uint32_t sum = __QADD8(a, b);
- return __UADD8(sum, 0x80808080);
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ return __UHADD8(a, b);
#else
uint32_t result = 0;
for (int8_t i = 0; i < 4; i++) {
- int8_t ai = (a >> (sizeof(uint8_t) * 8 * i)) - 128;
- int8_t bi = (b >> (sizeof(uint8_t) * 8 * i)) - 128;
- int32_t intermediate = (int32_t) ai + bi;
+ uint8_t ai = (a >> (sizeof(uint8_t) * 8 * i));
+ uint8_t bi = (b >> (sizeof(uint8_t) * 8 * i));
+ int32_t intermediate = (int32_t) (ai + bi) / 2;
if (intermediate > UCHAR_MAX) {
intermediate = UCHAR_MAX;
}
- result |= ((uint8_t) intermediate + 128) << (sizeof(uint8_t) * 8 * i);
+ result |= ((uint32_t) intermediate & 0xff) << (sizeof(uint8_t) * 8 * i);
}
return result;
#endif
}
uint32_t add16signed(uint32_t a, uint32_t b) {
- #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
- return __QADD16(a, b);
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ return __SHADD16(a, b);
#else
uint32_t result = 0;
for (int8_t i = 0; i < 2; i++) {
int16_t ai = a >> (sizeof(int16_t) * 8 * i);
int16_t bi = b >> (sizeof(int16_t) * 8 * i);
- int32_t intermediate = (int32_t) ai + bi;
+ int32_t intermediate = (int32_t) ai + bi / 2;
if (intermediate > SHRT_MAX) {
intermediate = SHRT_MAX;
} else if (intermediate < SHRT_MIN) {
@@ -194,32 +152,146 @@ uint32_t add16signed(uint32_t a, uint32_t b) {
}
uint32_t add16unsigned(uint32_t a, uint32_t b) {
- #if (defined (__ARM_FEATURE_DSP) && (__ARM_FEATURE_DSP == 1))
- // Subtract out the DC offset, add and then shift back.
- a = __USUB16(a, 0x80008000);
- b = __USUB16(b, 0x80008000);
- uint32_t sum = __QADD16(a, b);
- return __UADD16(sum, 0x80008000);
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ return __UHADD16(a, b);
#else
uint32_t result = 0;
for (int8_t i = 0; i < 2; i++) {
int16_t ai = (a >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
int16_t bi = (b >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
- int32_t intermediate = (int32_t) ai + bi;
+ int32_t intermediate = (int32_t) ai + bi / 2;
if (intermediate > USHRT_MAX) {
intermediate = USHRT_MAX;
}
- result |= ((uint16_t) intermediate + 0x8000) << (sizeof(int16_t) * 8 * i);
+ result |= ((uint32_t) intermediate & 0xffff) << (sizeof(int16_t) * 8 * i);
}
return result;
#endif
}
-audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
- bool single_channel,
- uint8_t channel,
- uint8_t** buffer,
- uint32_t* buffer_length) {
+static inline uint32_t mult8unsigned(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ /* TODO: workout ARMv7 instructions
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ return val;
+ #else*/
+ uint32_t result = 0;
+ float mod_mul = (float) mul / (float) ((1<<15)-1);
+ for (int8_t i = 0; i < 4; i++) {
+ uint8_t ai = val >> (sizeof(uint8_t) * 8 * i);
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > SHRT_MAX) {
+ intermediate = SHRT_MAX;
+ }
+ result |= ((uint32_t) intermediate & 0xff) << (sizeof(uint8_t) * 8 * i);
+ }
+
+ return result;
+ //#endif
+}
+
+static inline uint32_t mult8signed(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ /* TODO: workout ARMv7 instructions
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ return val;
+ #else
+ */
+ uint32_t result = 0;
+ float mod_mul = (float)mul / (float)((1<<15)-1);
+ for (int8_t i = 0; i < 4; i++) {
+ int16_t ai = val >> (sizeof(int8_t) * 8 * i);
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > CHAR_MAX) {
+ intermediate = CHAR_MAX;
+ } else if (intermediate < CHAR_MIN) {
+ intermediate = CHAR_MIN;
+ }
+ result |= (((uint32_t) intermediate) & 0xff) << (sizeof(int16_t) * 8 * i);
+ }
+ return result;
+ //#endif
+}
+
+//TODO:
+static inline uint32_t mult16unsigned(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ /* TODO: the below ARMv7m instructions "work", but the amplitude is much higher/louder
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ // there is no unsigned equivalent to the 'SMULWx' ARMv7 Thumb function,
+ // so we have to do it by hand.
+ uint32_t lo = val & 0xffff;
+ uint32_t hi = val >> 16;
+ //mp_printf(&mp_plat_print, "pre-asm: (mul: %d)\n\tval: %x\tlo: %x\thi: %x\n", mul, val, lo, hi);
+ uint32_t val_lo;
+ asm volatile("mul %0, %1, %2" : "=r" (val_lo) : "r" (mul), "r" (lo));
+ asm volatile("mla %0, %1, %2, %3" : "=r" (val) : "r" (mul), "r" (hi), "r" (val_lo));
+ //mp_printf(&mp_plat_print, "post-asm:\n\tval: %x\tlo: %x\n\n", val, val_lo);
+ return val;
+ #else
+ */
+ uint32_t result = 0;
+ float mod_mul = (float)mul / (float)((1<<15)-1);
+ for (int8_t i = 0; i < 2; i++) {
+ int16_t ai = (val >> (sizeof(uint16_t) * 8 * i)) - 0x8000;
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > SHRT_MAX) {
+ intermediate = SHRT_MAX;
+ } else if (intermediate < SHRT_MIN) {
+ intermediate = SHRT_MIN;
+ }
+ result |= (((uint32_t) intermediate) + 0x8000) << (sizeof(int16_t) * 8 * i);
+ }
+ return result;
+ //#endif
+}
+
+static inline uint32_t mult16signed(uint32_t val, int32_t mul) {
+ // if mul == 0, no need in wasting cycles
+ if (mul == 0) {
+ return 0;
+ }
+ #if (defined (__ARM_ARCH_7EM__) && (__ARM_ARCH_7EM__ == 1)) //Cortex-M4 w/FPU
+ int32_t hi, lo;
+ int32_t bits = 16; // saturate to 16 bits
+ int32_t shift = 0; // shift is done automatically
+ asm volatile("smulwb %0, %1, %2" : "=r" (lo) : "r" (mul), "r" (val));
+ asm volatile("smulwt %0, %1, %2" : "=r" (hi) : "r" (mul), "r" (val));
+ asm volatile("ssat %0, %1, %2, asr %3" : "=r" (lo) : "I" (bits), "r" (lo), "I" (shift));
+ asm volatile("ssat %0, %1, %2, asr %3" : "=r" (hi) : "I" (bits), "r" (hi), "I" (shift));
+ asm volatile("pkhbt %0, %1, %2, lsl #16" : "=r" (val) : "r" (lo), "r" (hi)); // pack
+ return val;
+ #else
+ uint32_t result = 0;
+ float mod_mul = (float)mul / (float)((1<<15)-1);
+ for (int8_t i = 0; i < 2; i++) {
+ int16_t ai = val >> (sizeof(int16_t) * 8 * i);
+ int32_t intermediate = ai * mod_mul;
+ if (intermediate > SHRT_MAX) {
+ intermediate = SHRT_MAX;
+ } else if (intermediate < SHRT_MIN) {
+ intermediate = SHRT_MIN;
+ }
+ result |= (((uint32_t) intermediate) & 0xffff) << (sizeof(int16_t) * 8 * i);
+ }
+ return result;
+ #endif
+}
+
+audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t* self,
+ bool single_channel,
+ uint8_t channel,
+ uint8_t** buffer,
+ uint32_t* buffer_length) {
if (!single_channel) {
channel = 0;
}
@@ -243,7 +315,7 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
self->use_first_buffer = !self->use_first_buffer;
bool voices_active = false;
for (int32_t v = 0; v < self->voice_count; v++) {
- audioio_mixer_voice_t* voice = &self->voice[v];
+ audiomixer_mixervoice_obj_t* voice = MP_OBJ_TO_PTR(self->voice[v]);
uint32_t j = 0;
bool voice_done = voice->sample == NULL;
@@ -285,6 +357,21 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
sample_value = voice->remaining_buffer[j];
}
+ // apply the mixer level
+ if (!self->samples_signed) {
+ if (self->bits_per_sample == 8) {
+ sample_value = mult8unsigned(sample_value, voice->level);
+ } else {
+ sample_value = mult16unsigned(sample_value, voice->level);
+ }
+ } else {
+ if (self->bits_per_sample == 8) {
+ sample_value = mult8signed(sample_value, voice->level);
+ } else {
+ sample_value = mult16signed(sample_value, voice->level);
+ }
+ }
+
if (!voices_active) {
word_buffer[i] = sample_value;
} else {
@@ -327,7 +414,7 @@ audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
return GET_BUFFER_MORE_DATA;
}
-void audioio_mixer_get_buffer_structure(audioio_mixer_obj_t* self, bool single_channel,
+void audiomixer_mixer_get_buffer_structure(audiomixer_mixer_obj_t* self, bool single_channel,
bool* single_buffer, bool* samples_signed,
uint32_t* max_buffer_length, uint8_t* spacing) {
*single_buffer = false;
diff --git a/shared-module/audiocore/Mixer.h b/shared-module/audiomixer/Mixer.h
index 22eb6a37f..ab4780efc 100644
--- a/shared-module/audiocore/Mixer.h
+++ b/shared-module/audiomixer/Mixer.h
@@ -24,22 +24,15 @@
* THE SOFTWARE.
*/
-#ifndef MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO_MIXER_H
-#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO_MIXER_H
+#ifndef MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER_MIXER_H
+#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER_MIXER_H
#include "py/obj.h"
+#include "py/objtuple.h"
#include "shared-module/audiocore/__init__.h"
typedef struct {
- mp_obj_t sample;
- bool loop;
- bool more_data;
- uint32_t* remaining_buffer;
- uint32_t buffer_length;
-} audioio_mixer_voice_t;
-
-typedef struct {
mp_obj_base_t base;
uint32_t* first_buffer;
uint32_t* second_buffer;
@@ -55,21 +48,22 @@ typedef struct {
uint32_t right_read_count;
uint8_t voice_count;
- audioio_mixer_voice_t voice[];
-} audioio_mixer_obj_t;
+ mp_obj_tuple_t *voice_tuple;
+ mp_obj_t voice[];
+} audiomixer_mixer_obj_t;
// These are not available from Python because it may be called in an interrupt.
-void audioio_mixer_reset_buffer(audioio_mixer_obj_t* self,
+void audiomixer_mixer_reset_buffer(audiomixer_mixer_obj_t* self,
bool single_channel,
uint8_t channel);
-audioio_get_buffer_result_t audioio_mixer_get_buffer(audioio_mixer_obj_t* self,
+audioio_get_buffer_result_t audiomixer_mixer_get_buffer(audiomixer_mixer_obj_t* self,
bool single_channel,
uint8_t channel,
uint8_t** buffer,
uint32_t* buffer_length); // length in bytes
-void audioio_mixer_get_buffer_structure(audioio_mixer_obj_t* self, bool single_channel,
+void audiomixer_mixer_get_buffer_structure(audiomixer_mixer_obj_t* self, bool single_channel,
bool* single_buffer, bool* samples_signed,
uint32_t* max_buffer_length, uint8_t* spacing);
-#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOIO_MIXER_H
+#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER_MIXER_H
diff --git a/shared-module/audiomixer/MixerVoice.c b/shared-module/audiomixer/MixerVoice.c
new file mode 100644
index 000000000..ff05dc93e
--- /dev/null
+++ b/shared-module/audiomixer/MixerVoice.c
@@ -0,0 +1,87 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 DeanM 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/audiomixer/Mixer.h"
+#include "shared-module/audiomixer/MixerVoice.h"
+
+#include <stdint.h>
+
+#include "py/runtime.h"
+#include "shared-module/audiomixer/__init__.h"
+#include "shared-module/audiocore/RawSample.h"
+
+void common_hal_audiomixer_mixervoice_construct(audiomixer_mixervoice_obj_t *self) {
+ self->sample = NULL;
+ self->level = ((1 << 15) - 1);
+}
+
+void common_hal_audiomixer_mixervoice_set_parent(audiomixer_mixervoice_obj_t* self, audiomixer_mixer_obj_t *parent) {
+ self->parent = parent;
+}
+
+float common_hal_audiomixer_mixervoice_get_level(audiomixer_mixervoice_obj_t* self) {
+ return ((float) self->level / ((1 << 15) - 1));
+}
+
+void common_hal_audiomixer_mixervoice_set_level(audiomixer_mixervoice_obj_t* self, float level) {
+ self->level = level * ((1 << 15)-1);
+}
+
+void common_hal_audiomixer_mixervoice_play(audiomixer_mixervoice_obj_t* self, mp_obj_t sample, bool loop) {
+ if (audiosample_sample_rate(sample) != self->parent->sample_rate) {
+ mp_raise_ValueError(translate("The sample's sample rate does not match the mixer's"));
+ }
+ if (audiosample_channel_count(sample) != self->parent->channel_count) {
+ mp_raise_ValueError(translate("The sample's channel count does not match the mixer's"));
+ }
+ if (audiosample_bits_per_sample(sample) != self->parent->bits_per_sample) {
+ mp_raise_ValueError(translate("The sample's bits_per_sample does not match the mixer's"));
+ }
+ bool single_buffer;
+ bool samples_signed;
+ uint32_t max_buffer_length;
+ uint8_t spacing;
+ audiosample_get_buffer_structure(sample, false, &single_buffer, &samples_signed,
+ &max_buffer_length, &spacing);
+ if (samples_signed != self->parent->samples_signed) {
+ mp_raise_ValueError(translate("The sample's signedness does not match the mixer's"));
+ }
+ self->sample = sample;
+ self->loop = loop;
+
+ audiosample_reset_buffer(sample, false, 0);
+ audioio_get_buffer_result_t result = audiosample_get_buffer(sample, false, 0, (uint8_t**) &self->remaining_buffer, &self->buffer_length);
+ // Track length in terms of words.
+ self->buffer_length /= sizeof(uint32_t);
+ self->more_data = result == GET_BUFFER_MORE_DATA;
+}
+
+bool common_hal_audiomixer_mixervoice_get_playing(audiomixer_mixervoice_obj_t* self) {
+ return self->sample != NULL;
+}
+
+void common_hal_audiomixer_mixervoice_stop(audiomixer_mixervoice_obj_t* self) {
+ self->sample = NULL;
+}
diff --git a/shared-module/audiomixer/MixerVoice.h b/shared-module/audiomixer/MixerVoice.h
new file mode 100644
index 000000000..efac19156
--- /dev/null
+++ b/shared-module/audiomixer/MixerVoice.h
@@ -0,0 +1,46 @@
+/*
+ * This file is part of the Micro Python project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * Copyright (c) 2018 DeanM 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 SHARED_MODULE_AUDIOMIXER_MIXERVOICE_H_
+#define SHARED_MODULE_AUDIOMIXER_MIXERVOICE_H_
+
+#include "py/obj.h"
+
+#include "shared-module/audiomixer/__init__.h"
+#include "shared-module/audiomixer/Mixer.h"
+
+typedef struct {
+ mp_obj_base_t base;
+ audiomixer_mixer_obj_t *parent;
+ mp_obj_t sample;
+ bool loop;
+ bool more_data;
+ uint32_t* remaining_buffer;
+ uint32_t buffer_length;
+ int16_t level;
+} audiomixer_mixervoice_obj_t;
+
+
+#endif /* SHARED_MODULE_AUDIOMIXER_MIXERVOICE_H_ */
diff --git a/shared-module/audiomixer/__init__.c b/shared-module/audiomixer/__init__.c
new file mode 100644
index 000000000..e69de29bb
--- /dev/null
+++ b/shared-module/audiomixer/__init__.c
diff --git a/shared-module/audiomixer/__init__.h b/shared-module/audiomixer/__init__.h
new file mode 100644
index 000000000..35b731558
--- /dev/null
+++ b/shared-module/audiomixer/__init__.h
@@ -0,0 +1,32 @@
+/*
+ * This file is part of the MicroPython project, http://micropython.org/
+ *
+ * The MIT License (MIT)
+ *
+ * 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
+ * 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_AUDIOMIXER__INIT__H
+#define MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER__INIT__H
+
+#include "shared-module/audiocore/__init__.h"
+
+#endif // MICROPY_INCLUDED_SHARED_MODULE_AUDIOMIXER__INIT__H
diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c
index 212ee8c7b..06b81f2f8 100644
--- a/shared-module/displayio/Display.c
+++ b/shared-module/displayio/Display.c
@@ -50,6 +50,8 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
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) {
+ // Turn off auto-refresh as we init.
+ self->auto_refresh = false;
uint16_t ram_width = 0x100;
uint16_t ram_height = 0x100;
if (single_byte_bounds) {
@@ -64,7 +66,6 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
self->write_ram_command = write_ram_command;
self->brightness_command = brightness_command;
self->auto_brightness = auto_brightness;
- self->auto_refresh = auto_refresh;
self->first_manual_refresh = !auto_refresh;
self->data_as_commands = data_as_commands;
@@ -128,6 +129,7 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self,
// Set the group after initialization otherwise we may send pixels while we delay in
// initialization.
common_hal_displayio_display_show(self, &circuitpython_splash);
+ self->auto_refresh = auto_refresh;
}
bool common_hal_displayio_display_show(displayio_display_obj_t* self, displayio_group_t* root_group) {
diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c
index 38418a029..d69e9f585 100644
--- a/shared-module/displayio/Group.c
+++ b/shared-module/displayio/Group.c
@@ -34,6 +34,47 @@ void common_hal_displayio_group_construct(displayio_group_t* self, uint32_t max_
displayio_group_construct(self, children, max_size, scale, x, y);
}
+bool common_hal_displayio_group_get_hidden(displayio_group_t* self) {
+ return self->hidden;
+}
+
+void common_hal_displayio_group_set_hidden(displayio_group_t* self, bool hidden) {
+ if (self->hidden == hidden) {
+ return;
+ }
+ self->hidden = hidden;
+ if (self->hidden_by_parent) {
+ return;
+ }
+ for (size_t i = 0; i < self->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_set_hidden_by_parent(layer, hidden);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_set_hidden_by_parent(layer, hidden);
+ }
+ }
+}
+
+void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden) {
+ if (self->hidden_by_parent == hidden) {
+ return;
+ }
+ self->hidden_by_parent = hidden;
+ // If we're already hidden, then we're done.
+ if (self->hidden) {
+ return;
+ }
+ for (size_t i = 0; i < self->size; i++) {
+ mp_obj_t layer = self->children[i].native;
+ if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) {
+ displayio_tilegrid_set_hidden_by_parent(layer, hidden);
+ } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) {
+ displayio_group_set_hidden_by_parent(layer, hidden);
+ }
+ }
+}
+
uint32_t common_hal_displayio_group_get_scale(displayio_group_t* self) {
return self->scale;
}
diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h
index 4e2308e56..5afaac1ba 100644
--- a/shared-module/displayio/Group.h
+++ b/shared-module/displayio/Group.h
@@ -42,18 +42,22 @@ typedef struct {
typedef struct {
mp_obj_base_t base;
displayio_group_child_t* children;
+ displayio_buffer_transform_t absolute_transform;
+ displayio_area_t dirty_area; // Catch all for changed area
int16_t x;
int16_t y;
uint16_t scale;
uint16_t size;
uint16_t max_size;
- bool item_removed;
- bool in_group;
- displayio_buffer_transform_t absolute_transform;
- displayio_area_t dirty_area; // Catch all for changed area
+ bool item_removed :1;
+ bool in_group :1;
+ bool hidden :1;
+ bool hidden_by_parent :1;
+ uint8_t padding :4;
} displayio_group_t;
void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* child_array, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y);
+void displayio_group_set_hidden_by_parent(displayio_group_t *self, bool hidden);
bool displayio_group_get_previous_area(displayio_group_t *group, displayio_area_t* area);
bool displayio_group_fill_area(displayio_group_t *group, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer);
void displayio_group_update_transform(displayio_group_t *group, const displayio_buffer_transform_t* parent_transform);
diff --git a/shared-module/displayio/I2CDisplay.c b/shared-module/displayio/I2CDisplay.c
index a3b167a33..d9452dc50 100644
--- a/shared-module/displayio/I2CDisplay.c
+++ b/shared-module/displayio/I2CDisplay.c
@@ -97,7 +97,7 @@ bool common_hal_displayio_i2cdisplay_bus_free(mp_obj_t obj) {
bool common_hal_displayio_i2cdisplay_begin_transaction(mp_obj_t obj) {
displayio_i2cdisplay_obj_t* self = MP_OBJ_TO_PTR(obj);
- return !common_hal_busio_i2c_try_lock(self->bus);
+ return common_hal_busio_i2c_try_lock(self->bus);
}
void common_hal_displayio_i2cdisplay_send(mp_obj_t obj, display_byte_type_t data_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length) {
diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c
index f34d2fc52..45b05110f 100644
--- a/shared-module/displayio/TileGrid.c
+++ b/shared-module/displayio/TileGrid.c
@@ -67,14 +67,30 @@ void common_hal_displayio_tilegrid_construct(displayio_tilegrid_t *self, mp_obj_
self->bitmap = bitmap;
self->pixel_shader = pixel_shader;
self->in_group = false;
- self->first_draw = true;
+ self->hidden = false;
+ self->hidden_by_parent = false;
+ self->previous_area.x1 = 0xffff;
+ self->previous_area.x2 = self->previous_area.x1;
self->flip_x = false;
self->flip_y = false;
self->transpose_xy = false;
}
+
+bool common_hal_displayio_tilegrid_get_hidden(displayio_tilegrid_t* self) {
+ return self->hidden;
+}
+
+void common_hal_displayio_tilegrid_set_hidden(displayio_tilegrid_t* self, bool hidden) {
+ self->hidden = hidden;
+}
+
+void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden) {
+ self->hidden_by_parent = hidden;
+}
+
bool displayio_tilegrid_get_previous_area(displayio_tilegrid_t *self, displayio_area_t* area) {
- if (self->first_draw) {
+ if (self->previous_area.x1 == self->previous_area.x2) {
return false;
}
displayio_area_copy(&self->previous_area, area);
@@ -138,12 +154,10 @@ void displayio_tilegrid_update_transform(displayio_tilegrid_t *self,
self->in_group = absolute_transform != NULL;
self->absolute_transform = absolute_transform;
if (absolute_transform != NULL) {
- self->moved = !self->first_draw;
+ self->moved = true;
_update_current_x(self);
_update_current_y(self);
- } else {
- self->first_draw = true;
}
}
@@ -155,7 +169,7 @@ void common_hal_displayio_tilegrid_set_x(displayio_tilegrid_t *self, mp_int_t x)
return;
}
- self->moved = !self->first_draw;
+ self->moved = true;
self->x = x;
if (self->absolute_transform != NULL) {
@@ -170,7 +184,7 @@ void common_hal_displayio_tilegrid_set_y(displayio_tilegrid_t *self, mp_int_t y)
if (self->y == y) {
return;
}
- self->moved = !self->first_draw;
+ self->moved = true;
self->y = y;
if (self->absolute_transform != NULL) {
_update_current_y(self);
@@ -306,6 +320,11 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c
return false;
}
+ bool hidden = self->hidden || self->hidden_by_parent;
+ if (hidden) {
+ return false;
+ }
+
displayio_area_t overlap;
if (!displayio_area_compute_overlap(area, &self->current_area, &overlap)) {
return false;
@@ -455,14 +474,17 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c
}
void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) {
- if (self->moved || self->first_draw) {
+ bool first_draw = self->previous_area.x1 == self->previous_area.x2;
+ bool hidden = self->hidden || self->hidden_by_parent;
+ if (!first_draw && hidden) {
+ self->previous_area.x2 = self->previous_area.x1;
+ } else if (self->moved || first_draw) {
displayio_area_copy(&self->current_area, &self->previous_area);
}
self->moved = false;
self->full_change = false;
self->partial_change = false;
- self->first_draw = false;
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)) {
@@ -481,7 +503,17 @@ void displayio_tilegrid_finish_refresh(displayio_tilegrid_t *self) {
}
displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *self, displayio_area_t* tail) {
- if (self->moved && !self->first_draw) {
+ bool first_draw = self->previous_area.x1 == self->previous_area.x2;
+ bool hidden = self->hidden || self->hidden_by_parent;
+ // Check hidden first because it trumps all other changes.
+ if (hidden) {
+ if (!first_draw) {
+ self->previous_area.next = tail;
+ return &self->previous_area;
+ } else {
+ return tail;
+ }
+ } else if (self->moved && !first_draw) {
displayio_area_union(&self->previous_area, &self->current_area, &self->dirty_area);
if (displayio_area_size(&self->dirty_area) <= 2U * self->pixel_width * self->pixel_height) {
self->dirty_area.next = tail;
@@ -512,7 +544,7 @@ displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *sel
displayio_palette_needs_refresh(self->pixel_shader)) ||
(MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type) &&
displayio_colorconverter_needs_refresh(self->pixel_shader));
- if (self->full_change || self->first_draw) {
+ if (self->full_change || first_draw) {
self->current_area.next = tail;
return &self->current_area;
}
diff --git a/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h
index 0a414b062..e97d3dfd4 100644
--- a/shared-module/displayio/TileGrid.h
+++ b/shared-module/displayio/TileGrid.h
@@ -55,17 +55,21 @@ typedef struct {
displayio_area_t dirty_area; // Stored as a relative area until the refresh area is fetched.
displayio_area_t previous_area; // Stored as an absolute area.
displayio_area_t current_area; // Stored as an absolute area so it applies across frames.
- bool partial_change;
- bool full_change;
- bool first_draw;
- bool moved;
- bool inline_tiles;
- bool in_group;
- bool flip_x;
- bool flip_y;
- bool transpose_xy;
+ bool partial_change :1;
+ bool full_change :1;
+ bool moved :1;
+ bool inline_tiles :1;
+ bool in_group :1;
+ bool flip_x :1;
+ bool flip_y :1;
+ bool transpose_xy :1;
+ bool hidden :1;
+ bool hidden_by_parent :1;
+ uint8_t padding :6;
} displayio_tilegrid_t;
+void displayio_tilegrid_set_hidden_by_parent(displayio_tilegrid_t *self, bool hidden);
+
// Updating the screen is a three stage process.
// The first stage is used to determine i