From d87bfaf4800b3ed169831ba3ccdd4c52772cafe4 Mon Sep 17 00:00:00 2001 From: Matthew Newberg Date: Sat, 31 Aug 2019 22:07:09 -0400 Subject: [PATCH 01/16] Add random dithering to ColorConverter --- shared-bindings/displayio/Display.c | 28 +++++++ shared-bindings/displayio/Display.h | 3 + shared-module/displayio/ColorConverter.c | 97 ++++++++++++++++++------ shared-module/displayio/ColorConverter.h | 6 +- shared-module/displayio/Display.c | 8 ++ shared-module/displayio/Palette.h | 15 ++++ shared-module/displayio/TileGrid.c | 52 +++++++------ shared-module/displayio/display_core.c | 9 +++ shared-module/displayio/display_core.h | 3 + 9 files changed, 173 insertions(+), 48 deletions(-) diff --git a/shared-bindings/displayio/Display.c b/shared-bindings/displayio/Display.c index 1eb1943b82..58d5e5123e 100644 --- a/shared-bindings/displayio/Display.c +++ b/shared-bindings/displayio/Display.c @@ -346,6 +346,33 @@ const mp_obj_property_t displayio_display_auto_brightness_obj = { (mp_obj_t)&mp_const_none_obj}, }; +//| .. attribute:: dither +//| +//| True when the display is dithered +//| +STATIC mp_obj_t displayio_display_obj_get_dither(mp_obj_t self_in) { + displayio_display_obj_t *self = native_display(self_in); + return mp_obj_new_bool(common_hal_displayio_display_get_dither(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_dither_obj, displayio_display_obj_get_dither); + +STATIC mp_obj_t displayio_display_obj_set_dither(mp_obj_t self_in, mp_obj_t dither) { + displayio_display_obj_t *self = native_display(self_in); + + common_hal_displayio_display_set_dither(self, mp_obj_is_true(dither)); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_set_dither_obj, displayio_display_obj_set_dither); + +const mp_obj_property_t displayio_display_dither_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_display_get_dither_obj, + (mp_obj_t)&displayio_display_set_dither_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + + //| .. attribute:: width //| //| Gets the width of the board @@ -487,6 +514,7 @@ STATIC const mp_rom_map_elem_t displayio_display_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&displayio_display_brightness_obj) }, { MP_ROM_QSTR(MP_QSTR_auto_brightness), MP_ROM_PTR(&displayio_display_auto_brightness_obj) }, + { MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&displayio_display_dither_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_display_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_display_height_obj) }, diff --git a/shared-bindings/displayio/Display.h b/shared-bindings/displayio/Display.h index a3c77e4e82..ca2b2d4765 100644 --- a/shared-bindings/displayio/Display.h +++ b/shared-bindings/displayio/Display.h @@ -62,6 +62,9 @@ uint16_t common_hal_displayio_display_get_rotation(displayio_display_obj_t* self bool common_hal_displayio_display_get_auto_brightness(displayio_display_obj_t* self); void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* self, bool auto_brightness); +bool common_hal_displayio_display_get_dither(displayio_display_obj_t* self); +void common_hal_displayio_display_set_dither(displayio_display_obj_t* self, bool dither); + mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self); bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness); diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c index 940aaa1c6d..cf93e71b74 100644 --- a/shared-module/displayio/ColorConverter.c +++ b/shared-module/displayio/ColorConverter.c @@ -28,6 +28,17 @@ #include "py/misc.h" +uint32_t displayio_colorconverter_dither_noise_1 (uint32_t n) +{ + n = (n >> 13) ^ n; + int nn = (n * (n * n * 60493 + 19990303) + 1376312589) & 0x7fffffff; + return (uint32_t) (((float)nn / (1073741824.0f*2)) * 255); +} + +uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y) { + return displayio_colorconverter_dither_noise_1(x + y * 0xFFFF); +} + void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self) { } @@ -96,32 +107,70 @@ void displayio_colorconverter_compute_tricolor(const _displayio_colorspace_t* co } } -bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) { - if (colorspace->depth == 16) { - *output_color = displayio_colorconverter_compute_rgb565(input_color); - return true; - } else if (colorspace->tricolor) { - uint8_t luma = displayio_colorconverter_compute_luma(input_color); - *output_color = luma >> (8 - colorspace->depth); - if (displayio_colorconverter_compute_chroma(input_color) <= 16) { - if (!colorspace->grayscale) { - *output_color = 0; - } - return true; - } - uint8_t pixel_hue = displayio_colorconverter_compute_hue(input_color); - displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, luma, output_color); - return true; - } else if (colorspace->grayscale && colorspace->depth <= 8) { - uint8_t luma = displayio_colorconverter_compute_luma(input_color); - *output_color = luma >> (8 - colorspace->depth); - return true; - } - return false; +void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) { + displayio_input_pixel_t input_pixel; + input_pixel.pixel = input_color; + input_pixel.x = input_pixel.y = input_pixel.tile = input_pixel.tile_x = input_pixel.tile_y = 0; + + displayio_output_pixel_t output_pixel; + output_pixel.pixel = 0; + output_pixel.opaque = false; + + displayio_colorconverter_convert(self, colorspace, &input_pixel, &output_pixel); + + (*output_color) = output_pixel.pixel; } -void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) { - displayio_colorconverter_convert(self, colorspace, input_color, output_color); +bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) { + uint32_t pixel = input_pixel->pixel; + + if (colorspace->dither){ + uint8_t randr = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y)); + uint8_t randg = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x+33,input_pixel->tile_y)); + uint8_t randb = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y+33)); + + uint32_t r8 = (pixel >> 16); + uint32_t g8 = (pixel >> 8) & 0xff; + uint32_t b8 = pixel & 0xff; + + if (colorspace->depth == 16) { + b8 = MIN(255,b8 + (randb&0x07)); + r8 = MIN(255,r8 + (randr&0x07)); + g8 = MIN(255,g8 + (randg&0x03)); + } else { + int bitmask = 0xFF >> colorspace->depth; + b8 = MIN(255,b8 + (randb&bitmask)); + r8 = MIN(255,r8 + (randr&bitmask)); + g8 = MIN(255,g8 + (randg&bitmask)); + } + pixel = r8 << 16 | g8 << 8 | b8; + } + + if (colorspace->depth == 16) { + output_color->pixel = displayio_colorconverter_compute_rgb565(pixel); + output_color->opaque = true; + return true; + } else if (colorspace->tricolor) { + uint8_t luma = displayio_colorconverter_compute_luma(pixel); + output_color->pixel = luma >> (8 - colorspace->depth); + if (displayio_colorconverter_compute_chroma(pixel) <= 16) { + if (!colorspace->grayscale) { + output_color->pixel = 0; + } + output_color->opaque = true; + return true; + } + uint8_t pixel_hue = displayio_colorconverter_compute_hue(pixel); + displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, luma, &output_color->pixel); + return true; + } else if (colorspace->grayscale && colorspace->depth <= 8) { + uint8_t luma = displayio_colorconverter_compute_luma(pixel); + output_color->pixel = luma >> (8 - colorspace->depth); + output_color->opaque = true; + return true; + } + output_color->opaque = false; + return false; } // Currently no refresh logic is needed for a ColorConverter. diff --git a/shared-module/displayio/ColorConverter.h b/shared-module/displayio/ColorConverter.h index c79e6a2f7e..6bb52d720f 100644 --- a/shared-module/displayio/ColorConverter.h +++ b/shared-module/displayio/ColorConverter.h @@ -39,7 +39,11 @@ typedef struct { bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self); void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self); -bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color); +bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color); + +uint32_t displayio_colorconverter_dither_noise_1 (uint32_t n); +uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y); + uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888); uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888); uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888); diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c index f210a80c3c..03185dffce 100644 --- a/shared-module/displayio/Display.c +++ b/shared-module/displayio/Display.c @@ -150,6 +150,14 @@ void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* s self->auto_brightness = auto_brightness; } +void common_hal_displayio_display_set_dither(displayio_display_obj_t* self, bool dither) { + displayio_display_core_set_dither(&self->core, dither); +} + +bool common_hal_displayio_display_get_dither(displayio_display_obj_t* self) { + return displayio_display_core_get_dither(&self->core); +} + mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self) { return self->current_brightness; } diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h index 1cfdd199a5..758fd43fc1 100644 --- a/shared-module/displayio/Palette.h +++ b/shared-module/displayio/Palette.h @@ -41,6 +41,7 @@ typedef struct { bool tricolor; bool pixels_in_byte_share_row; bool reverse_pixels_in_byte; + bool dither; } _displayio_colorspace_t; typedef struct { @@ -52,6 +53,20 @@ typedef struct { bool transparent; // This may have additional bits added later for blending. } _displayio_color_t; +typedef struct { + uint32_t pixel; + uint16_t x; + uint16_t y; + uint8_t tile; + uint16_t tile_x; + uint16_t tile_y; +} displayio_input_pixel_t; + +typedef struct { + uint32_t pixel; + bool opaque; +} displayio_output_pixel_t; + typedef struct { mp_obj_base_t base; _displayio_color_t* colors; diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c index f34d2fc52d..81d142bbe1 100644 --- a/shared-module/displayio/TileGrid.c +++ b/shared-module/displayio/TileGrid.c @@ -377,12 +377,16 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c } uint8_t pixels_per_byte = 8 / colorspace->depth; - for (int16_t y = start_y; y < end_y; y++) { - int16_t row_start = start + (y - start_y + y_shift) * y_stride; // in pixels - int16_t local_y = y / self->absolute_transform->scale; - for (int16_t x = start_x; x < end_x; x++) { + + displayio_input_pixel_t input_pixel; + displayio_output_pixel_t output_pixel; + + for (input_pixel.y = start_y; input_pixel.y < end_y; ++input_pixel.y) { + int16_t row_start = start + (input_pixel.y - start_y + y_shift) * y_stride; // in pixels + int16_t local_y = input_pixel.y / self->absolute_transform->scale; + for (input_pixel.x = start_x; input_pixel.x < end_x; ++input_pixel.x) { // Compute the destination pixel in the buffer and mask based on the transformations. - int16_t offset = row_start + (x - start_x + x_shift) * x_stride; // in pixels + int16_t offset = row_start + (input_pixel.x - start_x + x_shift) * x_stride; // in pixels // This is super useful for debugging out of range accesses. Uncomment to use. // if (offset < 0 || offset >= (int32_t) displayio_area_size(area)) { @@ -393,41 +397,43 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c if ((mask[offset / 32] & (1 << (offset % 32))) != 0) { continue; } - int16_t local_x = x / self->absolute_transform->scale; + int16_t local_x = input_pixel.x / self->absolute_transform->scale; uint16_t tile_location = ((local_y / self->tile_height + self->top_left_y) % self->height_in_tiles) * self->width_in_tiles + (local_x / self->tile_width + self->top_left_x) % self->width_in_tiles; - uint8_t tile = tiles[tile_location]; - uint16_t tile_x = (tile % self->bitmap_width_in_tiles) * self->tile_width + local_x % self->tile_width; - uint16_t tile_y = (tile / self->bitmap_width_in_tiles) * self->tile_height + local_y % self->tile_height; + input_pixel.tile = tiles[tile_location]; + input_pixel.tile_x = (input_pixel.tile % self->bitmap_width_in_tiles) * self->tile_width + local_x % self->tile_width; + input_pixel.tile_y = (input_pixel.tile / self->bitmap_width_in_tiles) * self->tile_height + local_y % self->tile_height; + + //uint32_t value = 0; + output_pixel.pixel = 0; + input_pixel.pixel = 0; - uint32_t value = 0; // We always want to read bitmap pixels by row first and then transpose into the destination // buffer because most bitmaps are row associated. if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_bitmap_type)) { - value = common_hal_displayio_bitmap_get_pixel(self->bitmap, tile_x, tile_y); + input_pixel.pixel = common_hal_displayio_bitmap_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y); } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_shape_type)) { - value = common_hal_displayio_shape_get_pixel(self->bitmap, tile_x, tile_y); + input_pixel.pixel = common_hal_displayio_shape_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y); } else if (MP_OBJ_IS_TYPE(self->bitmap, &displayio_ondiskbitmap_type)) { - value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y); + input_pixel.pixel = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, input_pixel.tile_x, input_pixel.tile_y); } - - uint32_t pixel; - bool opaque = true; + + output_pixel.opaque = true; if (self->pixel_shader == mp_const_none) { - pixel = value; + output_pixel.pixel = input_pixel.pixel; } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) { - opaque = displayio_palette_get_color(self->pixel_shader, colorspace, value, &pixel); + output_pixel.opaque = displayio_palette_get_color(self->pixel_shader, colorspace, input_pixel.pixel, &output_pixel.pixel); } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) { - opaque = displayio_colorconverter_convert(self->pixel_shader, colorspace, value, &pixel); + displayio_colorconverter_convert(self->pixel_shader, colorspace, &input_pixel, &output_pixel); } - if (!opaque) { + if (!output_pixel.opaque) { // A pixel is transparent so we haven't fully covered the area ourselves. full_coverage = false; } else { mask[offset / 32] |= 1 << (offset % 32); if (colorspace->depth == 16) { - *(((uint16_t*) buffer) + offset) = pixel; + *(((uint16_t*) buffer) + offset) = output_pixel.pixel; } else if (colorspace->depth == 8) { - *(((uint8_t*) buffer) + offset) = pixel; + *(((uint8_t*) buffer) + offset) = output_pixel.pixel; } else if (colorspace->depth < 8) { // Reorder the offsets to pack multiple rows into a byte (meaning they share a column). if (!colorspace->pixels_in_byte_share_row) { @@ -446,7 +452,7 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_c // Reverse the shift by subtracting it from the leftmost shift. shift = (pixels_per_byte - 1) * colorspace->depth - shift; } - ((uint8_t*)buffer)[offset / pixels_per_byte] |= pixel << shift; + ((uint8_t*)buffer)[offset / pixels_per_byte] |= output_pixel.pixel << shift; } } } diff --git a/shared-module/displayio/display_core.c b/shared-module/displayio/display_core.c index 0f449ea34d..83df67c63e 100644 --- a/shared-module/displayio/display_core.c +++ b/shared-module/displayio/display_core.c @@ -49,6 +49,7 @@ void displayio_display_core_construct(displayio_display_core_t* self, self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row; self->colorspace.bytes_per_cell = bytes_per_cell; self->colorspace.reverse_pixels_in_byte = reverse_pixels_in_byte; + self->colorspace.dither = false; self->current_group = NULL; self->colstart = colstart; self->rowstart = rowstart; @@ -171,6 +172,14 @@ uint16_t displayio_display_core_get_height(displayio_display_core_t* self){ return self->height; } +void displayio_display_core_set_dither(displayio_display_core_t* self, bool dither){ + self->colorspace.dither = dither; +} + +bool displayio_display_core_get_dither(displayio_display_core_t* self){ + return self->colorspace.dither; +} + bool displayio_display_core_bus_free(displayio_display_core_t *self) { return self->bus_free(self->bus); } diff --git a/shared-module/displayio/display_core.h b/shared-module/displayio/display_core.h index e92fdbc9f1..3a69cd40a2 100644 --- a/shared-module/displayio/display_core.h +++ b/shared-module/displayio/display_core.h @@ -65,6 +65,9 @@ bool displayio_display_core_show(displayio_display_core_t* self, displayio_group uint16_t displayio_display_core_get_width(displayio_display_core_t* self); uint16_t displayio_display_core_get_height(displayio_display_core_t* self); +void displayio_display_core_set_dither(displayio_display_core_t* self, bool dither); +bool displayio_display_core_get_dither(displayio_display_core_t* self); + bool displayio_display_core_bus_free(displayio_display_core_t *self); bool displayio_display_core_begin_transaction(displayio_display_core_t* self); void displayio_display_core_end_transaction(displayio_display_core_t* self); From 3ab6a2343426e1b42141916a74c0c422dbd34828 Mon Sep 17 00:00:00 2001 From: Matthew Newberg Date: Wed, 4 Sep 2019 20:42:24 -0400 Subject: [PATCH 02/16] Removed unused return value in displayio_colorconverter_convert --- shared-module/displayio/ColorConverter.c | 11 +++++------ shared-module/displayio/ColorConverter.h | 2 +- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c index cf93e71b74..304d13a011 100644 --- a/shared-module/displayio/ColorConverter.c +++ b/shared-module/displayio/ColorConverter.c @@ -121,7 +121,7 @@ void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *sel (*output_color) = output_pixel.pixel; } -bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) { +void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) { uint32_t pixel = input_pixel->pixel; if (colorspace->dither){ @@ -149,7 +149,7 @@ bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d if (colorspace->depth == 16) { output_color->pixel = displayio_colorconverter_compute_rgb565(pixel); output_color->opaque = true; - return true; + return; } else if (colorspace->tricolor) { uint8_t luma = displayio_colorconverter_compute_luma(pixel); output_color->pixel = luma >> (8 - colorspace->depth); @@ -158,19 +158,18 @@ bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d output_color->pixel = 0; } output_color->opaque = true; - return true; + return; } uint8_t pixel_hue = displayio_colorconverter_compute_hue(pixel); displayio_colorconverter_compute_tricolor(colorspace, pixel_hue, luma, &output_color->pixel); - return true; + return; } else if (colorspace->grayscale && colorspace->depth <= 8) { uint8_t luma = displayio_colorconverter_compute_luma(pixel); output_color->pixel = luma >> (8 - colorspace->depth); output_color->opaque = true; - return true; + return; } output_color->opaque = false; - return false; } // Currently no refresh logic is needed for a ColorConverter. diff --git a/shared-module/displayio/ColorConverter.h b/shared-module/displayio/ColorConverter.h index 6bb52d720f..3657df1684 100644 --- a/shared-module/displayio/ColorConverter.h +++ b/shared-module/displayio/ColorConverter.h @@ -39,7 +39,7 @@ typedef struct { bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self); void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self); -bool displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color); +void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color); uint32_t displayio_colorconverter_dither_noise_1 (uint32_t n); uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y); From 4604a69498d5f7ffadb4ebf46a064ea5e18164b6 Mon Sep 17 00:00:00 2001 From: Matthew Newberg Date: Thu, 5 Sep 2019 21:55:45 -0400 Subject: [PATCH 03/16] Move dither parameter to ColorConverter constructor and parameter --- shared-bindings/displayio/ColorConverter.c | 39 ++++++++++++++++++++-- shared-bindings/displayio/ColorConverter.h | 5 ++- shared-bindings/displayio/Display.c | 25 -------------- shared-module/displayio/ColorConverter.c | 16 +++++++-- shared-module/displayio/ColorConverter.h | 1 + shared-module/displayio/Display.c | 8 ----- 6 files changed, 56 insertions(+), 38 deletions(-) diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c index d9524870dc..8c48a670a6 100644 --- a/shared-bindings/displayio/ColorConverter.c +++ b/shared-bindings/displayio/ColorConverter.c @@ -51,11 +51,18 @@ // TODO(tannewt): Add support for other color formats. //| STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - mp_arg_check_num(n_args, kw_args, 0, 0, false); + mp_arg_check_num(n_args, kw_args, 0, 1, false); displayio_colorconverter_t *self = m_new_obj(displayio_colorconverter_t); self->base.type = &displayio_colorconverter_type; - common_hal_displayio_colorconverter_construct(self); + + bool dither = false; + + if (n_args > 0) { + dither = mp_obj_is_true(pos_args[0]); + } + + common_hal_displayio_colorconverter_construct(self, dither); return MP_OBJ_FROM_PTR(self); } @@ -79,8 +86,35 @@ STATIC mp_obj_t displayio_colorconverter_obj_convert(mp_obj_t self_in, mp_obj_t } MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_convert_obj, displayio_colorconverter_obj_convert); +//| .. attribute:: dither +//| +//| True when the display is dithered +//| +STATIC mp_obj_t displayio_colorconverter_obj_get_dither(mp_obj_t self_in) { + displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in); + return mp_obj_new_bool(common_hal_displayio_colorconverter_get_dither(self)); +} +MP_DEFINE_CONST_FUN_OBJ_1(displayio_colorconverter_get_dither_obj, displayio_colorconverter_obj_get_dither); + +STATIC mp_obj_t displayio_colorconverter_obj_set_dither(mp_obj_t self_in, mp_obj_t dither) { + displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in); + + common_hal_displayio_colorconverter_set_dither(self, mp_obj_is_true(dither)); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_set_dither_obj, displayio_colorconverter_obj_set_dither); + +const mp_obj_property_t displayio_colorconverter_dither_obj = { + .base.type = &mp_type_property, + .proxy = {(mp_obj_t)&displayio_colorconverter_get_dither_obj, + (mp_obj_t)&displayio_colorconverter_set_dither_obj, + (mp_obj_t)&mp_const_none_obj}, +}; + STATIC const mp_rom_map_elem_t displayio_colorconverter_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_convert), MP_ROM_PTR(&displayio_colorconverter_convert_obj) }, + { MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&displayio_colorconverter_dither_obj) }, }; STATIC MP_DEFINE_CONST_DICT(displayio_colorconverter_locals_dict, displayio_colorconverter_locals_dict_table); @@ -90,3 +124,4 @@ const mp_obj_type_t displayio_colorconverter_type = { .make_new = displayio_colorconverter_make_new, .locals_dict = (mp_obj_dict_t*)&displayio_colorconverter_locals_dict, }; + diff --git a/shared-bindings/displayio/ColorConverter.h b/shared-bindings/displayio/ColorConverter.h index 24895500e8..d550d81be5 100644 --- a/shared-bindings/displayio/ColorConverter.h +++ b/shared-bindings/displayio/ColorConverter.h @@ -33,7 +33,10 @@ extern const mp_obj_type_t displayio_colorconverter_type; -void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self); +void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self, bool dither); void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *colorconverter, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color); +void common_hal_displayio_colorconverter_set_dither(displayio_colorconverter_t* self, bool dither); +bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t* self); + #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H diff --git a/shared-bindings/displayio/Display.c b/shared-bindings/displayio/Display.c index 58d5e5123e..5ce7b3581b 100644 --- a/shared-bindings/displayio/Display.c +++ b/shared-bindings/displayio/Display.c @@ -346,31 +346,7 @@ const mp_obj_property_t displayio_display_auto_brightness_obj = { (mp_obj_t)&mp_const_none_obj}, }; -//| .. attribute:: dither -//| -//| True when the display is dithered -//| -STATIC mp_obj_t displayio_display_obj_get_dither(mp_obj_t self_in) { - displayio_display_obj_t *self = native_display(self_in); - return mp_obj_new_bool(common_hal_displayio_display_get_dither(self)); -} -MP_DEFINE_CONST_FUN_OBJ_1(displayio_display_get_dither_obj, displayio_display_obj_get_dither); -STATIC mp_obj_t displayio_display_obj_set_dither(mp_obj_t self_in, mp_obj_t dither) { - displayio_display_obj_t *self = native_display(self_in); - - common_hal_displayio_display_set_dither(self, mp_obj_is_true(dither)); - - return mp_const_none; -} -MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_set_dither_obj, displayio_display_obj_set_dither); - -const mp_obj_property_t displayio_display_dither_obj = { - .base.type = &mp_type_property, - .proxy = {(mp_obj_t)&displayio_display_get_dither_obj, - (mp_obj_t)&displayio_display_set_dither_obj, - (mp_obj_t)&mp_const_none_obj}, -}; //| .. attribute:: width @@ -514,7 +490,6 @@ STATIC const mp_rom_map_elem_t displayio_display_locals_dict_table[] = { { MP_ROM_QSTR(MP_QSTR_brightness), MP_ROM_PTR(&displayio_display_brightness_obj) }, { MP_ROM_QSTR(MP_QSTR_auto_brightness), MP_ROM_PTR(&displayio_display_auto_brightness_obj) }, - { MP_ROM_QSTR(MP_QSTR_dither), MP_ROM_PTR(&displayio_display_dither_obj) }, { MP_ROM_QSTR(MP_QSTR_width), MP_ROM_PTR(&displayio_display_width_obj) }, { MP_ROM_QSTR(MP_QSTR_height), MP_ROM_PTR(&displayio_display_height_obj) }, diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c index 304d13a011..a5d7f0b051 100644 --- a/shared-module/displayio/ColorConverter.c +++ b/shared-module/displayio/ColorConverter.c @@ -39,7 +39,8 @@ uint32_t displayio_colorconverter_dither_noise_2(uint32_t x, uint32_t y) { return displayio_colorconverter_dither_noise_1(x + y * 0xFFFF); } -void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self) { +void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self, bool dither) { + self->dither = dither; } uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888) { @@ -121,10 +122,18 @@ void common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *sel (*output_color) = output_pixel.pixel; } +void common_hal_displayio_colorconverter_set_dither(displayio_colorconverter_t* self, bool dither) { + self->dither = dither; +} + +bool common_hal_displayio_colorconverter_get_dither(displayio_colorconverter_t* self) { + return self->dither; +} + void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, const displayio_input_pixel_t *input_pixel, displayio_output_pixel_t *output_color) { uint32_t pixel = input_pixel->pixel; - if (colorspace->dither){ + if (self->dither){ uint8_t randr = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y)); uint8_t randg = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x+33,input_pixel->tile_y)); uint8_t randb = (displayio_colorconverter_dither_noise_2(input_pixel->tile_x,input_pixel->tile_y+33)); @@ -172,6 +181,8 @@ void displayio_colorconverter_convert(displayio_colorconverter_t *self, const _d output_color->opaque = false; } + + // Currently no refresh logic is needed for a ColorConverter. bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self) { return false; @@ -179,3 +190,4 @@ bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self) { void displayio_colorconverter_finish_refresh(displayio_colorconverter_t *self) { } + diff --git a/shared-module/displayio/ColorConverter.h b/shared-module/displayio/ColorConverter.h index 3657df1684..10b1604a49 100644 --- a/shared-module/displayio/ColorConverter.h +++ b/shared-module/displayio/ColorConverter.h @@ -35,6 +35,7 @@ typedef struct { mp_obj_base_t base; + bool dither; } displayio_colorconverter_t; bool displayio_colorconverter_needs_refresh(displayio_colorconverter_t *self); diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c index 03185dffce..f210a80c3c 100644 --- a/shared-module/displayio/Display.c +++ b/shared-module/displayio/Display.c @@ -150,14 +150,6 @@ void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* s self->auto_brightness = auto_brightness; } -void common_hal_displayio_display_set_dither(displayio_display_obj_t* self, bool dither) { - displayio_display_core_set_dither(&self->core, dither); -} - -bool common_hal_displayio_display_get_dither(displayio_display_obj_t* self) { - return displayio_display_core_get_dither(&self->core); -} - mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self) { return self->current_brightness; } From b2fb5ac1c12a21d9b6cb8c60a30db65db399ca66 Mon Sep 17 00:00:00 2001 From: Matthew Newberg Date: Fri, 6 Sep 2019 16:11:15 -0400 Subject: [PATCH 04/16] Fix comment on color converter --- shared-bindings/displayio/ColorConverter.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c index 8c48a670a6..8a74e467b9 100644 --- a/shared-bindings/displayio/ColorConverter.c +++ b/shared-bindings/displayio/ColorConverter.c @@ -88,7 +88,8 @@ MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_convert_obj, displayio_colorc //| .. attribute:: dither //| -//| True when the display is dithered +//| When true the color converter dithers the output by adding random noise when +//| truncating to display bitdepth //| STATIC mp_obj_t displayio_colorconverter_obj_get_dither(mp_obj_t self_in) { displayio_colorconverter_t *self = MP_OBJ_TO_PTR(self_in); From 8e552324928ea0081cb67c818a26d2b83da55f05 Mon Sep 17 00:00:00 2001 From: Matthew Newberg Date: Fri, 6 Sep 2019 16:23:24 -0400 Subject: [PATCH 05/16] Use kwargs for dither in ColorConverter constructor --- shared-bindings/displayio/ColorConverter.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c index 8a74e467b9..75eb8ba328 100644 --- a/shared-bindings/displayio/ColorConverter.c +++ b/shared-bindings/displayio/ColorConverter.c @@ -47,22 +47,23 @@ //| //| Create a ColorConverter object to convert color formats. Only supports RGB888 to RGB565 //| currently. -//| +//| :param bool dither: Adds random noise to dither the output image + // TODO(tannewt): Add support for other color formats. //| STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - mp_arg_check_num(n_args, kw_args, 0, 1, false); + enum { ARG_dither}; + + static const mp_arg_t allowed_args[] = { + { MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, + }; + mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; + mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); displayio_colorconverter_t *self = m_new_obj(displayio_colorconverter_t); self->base.type = &displayio_colorconverter_type; - - bool dither = false; - if (n_args > 0) { - dither = mp_obj_is_true(pos_args[0]); - } - - common_hal_displayio_colorconverter_construct(self, dither); + common_hal_displayio_colorconverter_construct(self, args[ARG_dither].u_bool); return MP_OBJ_FROM_PTR(self); } From f38ee42874e4581080cd3bf808b4ab5e32e4807d Mon Sep 17 00:00:00 2001 From: jepler Date: Sun, 8 Sep 2019 13:08:15 -0500 Subject: [PATCH 06/16] nrf: Add i2s audio output Testing performed: I used a Particle Xenon with a HDA1334 I2S DAC. I played a variety of mono 16-bit samples at 11025 and 22050Hz nominal bit rates. With this setup, all the 11025Hz samples sound good. I tested play, pause, and loop functionality. During some runs with 22050Hz samples, there were glitches. However, these may have only occurred during runs where I had set breakpoints and watchpoints in gdb. I also tested with a MAX98357A I2S amplifier. On this device, everything sounded "scratchy". I was powering it from 5V and the 5V rail seemed steady, so I don't have an explanation for this. However, I haven't tried it with a SAMD board. --- ports/nrf/background.c | 8 + ports/nrf/common-hal/audiobusio/I2SOut.c | 262 ++++++++++++++++++++++- ports/nrf/common-hal/audiobusio/I2SOut.h | 27 +++ ports/nrf/supervisor/port.c | 9 + 4 files changed, 297 insertions(+), 9 deletions(-) diff --git a/ports/nrf/background.c b/ports/nrf/background.c index 94411cbce5..305f607c5c 100644 --- a/ports/nrf/background.c +++ b/ports/nrf/background.c @@ -33,6 +33,10 @@ #include "shared-module/displayio/__init__.h" #endif +#if CIRCUITPY_AUDIOBUSIO +#include "common-hal/audiobusio/I2SOut.h" +#endif + #if CIRCUITPY_AUDIOPWMIO #include "common-hal/audiopwmio/PWMAudioOut.h" #endif @@ -54,6 +58,10 @@ void run_background_tasks(void) { #if CIRCUITPY_AUDIOPWMIO audiopwmout_background(); #endif +#if CIRCUITPY_AUDIOBUSIO + i2s_background(); +#endif + #if CIRCUITPY_DISPLAYIO displayio_background(); diff --git a/ports/nrf/common-hal/audiobusio/I2SOut.c b/ports/nrf/common-hal/audiobusio/I2SOut.c index 8be1fb2f8c..6c64fb8cd4 100644 --- a/ports/nrf/common-hal/audiobusio/I2SOut.c +++ b/ports/nrf/common-hal/audiobusio/I2SOut.c @@ -24,47 +24,291 @@ * THE SOFTWARE. */ +#include +#include + #include "common-hal/microcontroller/Pin.h" #include "common-hal/audiobusio/I2SOut.h" +#include "shared-bindings/audiobusio/I2SOut.h" +#include "shared-module/audiocore/__init__.h" #include "py/obj.h" #include "py/runtime.h" +static audiobusio_i2sout_obj_t *instance; + +struct { int16_t l, r; } static_sample16 = {0x8000, 0x8000}; +struct { uint8_t l1, r1, l2, r2; } static_sample8 = {0x80, 0x80, 0x80, 0x80}; + +struct frequency_info { uint32_t RATIO; uint32_t MCKFREQ; int sample_rate; float abserr; }; +struct ratio_info { uint32_t RATIO; int16_t divisor; bool can_16bit; }; +struct ratio_info ratios[] = { + { I2S_CONFIG_RATIO_RATIO_32X, 32, true }, + { I2S_CONFIG_RATIO_RATIO_48X, 48, false }, + { I2S_CONFIG_RATIO_RATIO_64X, 64, true }, + { I2S_CONFIG_RATIO_RATIO_96X, 96, true }, + { I2S_CONFIG_RATIO_RATIO_128X, 128, true }, + { I2S_CONFIG_RATIO_RATIO_192X, 192, true }, + { I2S_CONFIG_RATIO_RATIO_256X, 256, true }, + { I2S_CONFIG_RATIO_RATIO_384X, 384, true }, + { I2S_CONFIG_RATIO_RATIO_512X, 512, true }, +}; + +struct mclk_info { uint32_t MCKFREQ; int divisor; }; +struct mclk_info mclks[] = { + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV8, 8 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV10, 10 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV11, 11 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV15, 15 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV16, 16 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV21, 21 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV23, 23 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV31, 31 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV42, 42 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV63, 63 }, + { I2S_CONFIG_MCKFREQ_MCKFREQ_32MDIV125, 125 }, +}; + +static void calculate_ratio_info(uint32_t target_sample_rate, struct frequency_info *info, + int ratio_index, int mclk_index) { + info->RATIO = ratios[ratio_index].RATIO; + info->MCKFREQ = mclks[mclk_index].MCKFREQ; + info->sample_rate = 32000000 + / ratios[ratio_index].divisor / mclks[mclk_index].divisor; + info->abserr = fabsf(1.0f * target_sample_rate - info->sample_rate) + / target_sample_rate; +} + +void choose_i2s_clocking(audiobusio_i2sout_obj_t *self, uint32_t sample_rate) { + struct frequency_info best = {0, 0, 0, 1.0}; + for (size_t ri=0; riCONFIG.SWIDTH == I2S_CONFIG_SWIDTH_SWIDTH_16Bit + && !ratios[ri].can_16bit) { + continue; + } + + for (size_t mi=0; miCONFIG.RATIO = best.RATIO; + NRF_I2S->CONFIG.MCKFREQ = best.MCKFREQ; + self->sample_rate = best.sample_rate; +} + +static void i2s_buffer_fill(audiobusio_i2sout_obj_t* self) { + void *buffer = self->buffers[self->next_buffer]; + NRF_I2S->TXD.PTR = (uintptr_t)buffer; + self->next_buffer = !self->next_buffer; + size_t bytesleft = self->buffer_length; + + if (self->paused || self->stopping) { + if (self->stopping) { + NRF_I2S->TASKS_STOP = 1; + self->playing = false; + } +stopping: ; + uint32_t *bp = (uint32_t*)buffer; + uint32_t *be = (uint32_t*)(buffer + bytesleft); + for (; bp != be; ) + *bp++ = self->hold_value; + return; + } + + while (bytesleft) { + if (self->sample_data == self->sample_end) { + uint32_t sample_buffer_length; + audioio_get_buffer_result_t get_buffer_result = + audiosample_get_buffer(self->sample, false, 0, + &self->sample_data, &sample_buffer_length); + self->sample_end = self->sample_data + sample_buffer_length; + if (get_buffer_result == GET_BUFFER_DONE) { + if (self->loop) { + audiosample_reset_buffer(self->sample, false, 0); + } else { + self->stopping = true; + goto stopping; + } + } + } + uint16_t bytecount = MIN(bytesleft, (size_t)(self->sample_end - self->sample_data)); + if (self->samples_signed) { + memcpy(buffer, self->sample_data, bytecount); + } else if (self->bytes_per_sample == 2) { + uint16_t *bp = (uint16_t*)buffer; + uint16_t *be = (uint16_t*)(buffer + bytecount); + uint16_t *sp = (uint16_t*)self->sample_data; + for (; bp != be; bp++) { + *bp++ = *sp++ + 0x8000; + } + } else { + uint8_t *bp = (uint8_t*)buffer; + uint8_t *be = (uint8_t*)(buffer + bytecount); + uint8_t *sp = (uint8_t*)self->sample_data; + for (; bp != be; bp++) { + *bp++ = *sp++ + 0x80; + } + } + buffer += bytecount; + self->sample_data += bytecount; + bytesleft -= bytecount; + } + if (self->bytes_per_sample == 1 && self->channel_count == 1) { + self->hold_value = 0x01010101 * *(uint8_t*)(buffer-1); + } else if (self->bytes_per_sample == 2 && self->channel_count == 2) { + self->hold_value = *(uint32_t*)(buffer-4); + } else { + self->hold_value = 0x00010001 * *(uint16_t*)(buffer-2); + } +} + void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t* self, const mcu_pin_obj_t* bit_clock, const mcu_pin_obj_t* word_select, const mcu_pin_obj_t* data, bool left_justified) { - mp_raise_NotImplementedError(NULL); + if (instance) + mp_raise_RuntimeError(translate("Device in use")); + instance = self; + + claim_pin(bit_clock); + claim_pin(word_select); + claim_pin(data); + + NRF_I2S->PSEL.SCK = self->bit_clock_pin_number = bit_clock->number; + NRF_I2S->PSEL.LRCK = self->word_select_pin_number = word_select->number; + NRF_I2S->PSEL.SDOUT = self->data_pin_number = data->number; + + NRF_I2S->CONFIG.MODE = I2S_CONFIG_MODE_MODE_Master; + NRF_I2S->CONFIG.RXEN = I2S_CONFIG_RXEN_RXEN_Disabled; + NRF_I2S->CONFIG.TXEN = I2S_CONFIG_TXEN_TXEN_Enabled; + NRF_I2S->CONFIG.MCKEN = I2S_CONFIG_MCKEN_MCKEN_Enabled; + NRF_I2S->CONFIG.SWIDTH = I2S_CONFIG_SWIDTH_SWIDTH_16Bit; + + NRF_I2S->CONFIG.ALIGN = I2S_CONFIG_ALIGN_ALIGN_Left; + NRF_I2S->CONFIG.FORMAT = left_justified ? I2S_CONFIG_FORMAT_FORMAT_Aligned + : I2S_CONFIG_FORMAT_FORMAT_I2S; } bool common_hal_audiobusio_i2sout_deinited(audiobusio_i2sout_obj_t* self) { - mp_raise_NotImplementedError(NULL); + return self->data_pin_number == 0xff; } void common_hal_audiobusio_i2sout_deinit(audiobusio_i2sout_obj_t* self) { - mp_raise_NotImplementedError(NULL); + if (common_hal_audiobusio_i2sout_deinited(self)) { + return; + } + reset_pin_number(self->bit_clock_pin_number); + self->bit_clock_pin_number = 0xff; + reset_pin_number(self->word_select_pin_number); + self->word_select_pin_number = 0xff; + reset_pin_number(self->data_pin_number); + self->data_pin_number = 0xff; + instance = NULL; } void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t* self, mp_obj_t sample, bool loop) { - mp_raise_NotImplementedError(NULL); + if (common_hal_audiobusio_i2sout_get_playing(self)) { + common_hal_audiobusio_i2sout_stop(self); + } + + self->sample = sample; + self->loop = loop; + uint32_t sample_rate = audiosample_sample_rate(sample); + self->bytes_per_sample = audiosample_bits_per_sample(sample) / 8; + + uint32_t max_buffer_length; + bool single_buffer, samples_signed; + audiosample_get_buffer_structure(sample, /* single channel */ false, + &single_buffer, &samples_signed, &max_buffer_length, + &self->channel_count); + self->single_buffer = single_buffer; + self->samples_signed = samples_signed; + + choose_i2s_clocking(self, sample_rate); + /* Allocate buffers based on a maximum duration */ + enum { buffer_length_ms = 8 }; + self->buffer_length = MAX( + 2*max_buffer_length, + sample_rate * buffer_length_ms * self->bytes_per_sample + * self->channel_count / 1000); + self->buffer_length = (self->buffer_length + 3) & ~3; + self->buffers[0] = m_malloc(self->buffer_length, false); + self->buffers[1] = m_malloc(self->buffer_length, false); + + + audiosample_reset_buffer(self->sample, false, 0); + + self->next_buffer = 0; + self->sample_data = self->sample_end = 0; + self->playing = true; + self->paused = false; + self->stopping = false; + i2s_buffer_fill(self); + + NRF_I2S->CONFIG.CHANNELS = self->channel_count == 1 ? I2S_CONFIG_CHANNELS_CHANNELS_Left : I2S_CONFIG_CHANNELS_CHANNELS_Stereo; + + + NRF_I2S->RXTXD.MAXCNT = self->buffer_length / 4; + NRF_I2S->ENABLE = I2S_ENABLE_ENABLE_Enabled; + + NRF_I2S->TASKS_START = 1; + + i2s_background(); } void common_hal_audiobusio_i2sout_pause(audiobusio_i2sout_obj_t* self) { - mp_raise_NotImplementedError(NULL); + self->paused = true; } void common_hal_audiobusio_i2sout_resume(audiobusio_i2sout_obj_t* self) { - mp_raise_NotImplementedError(NULL); + self->paused = false; } bool common_hal_audiobusio_i2sout_get_paused(audiobusio_i2sout_obj_t* self) { - mp_raise_NotImplementedError(NULL); + return self->paused; } void common_hal_audiobusio_i2sout_stop(audiobusio_i2sout_obj_t* self) { - mp_raise_NotImplementedError(NULL); + NRF_I2S->TASKS_STOP = 1; + self->stopping = true; } bool common_hal_audiobusio_i2sout_get_playing(audiobusio_i2sout_obj_t* self) { - mp_raise_NotImplementedError(NULL); + if (NRF_I2S->EVENTS_STOPPED) { + self->playing = false; + NRF_I2S->EVENTS_STOPPED = 0; + } + return self->playing; +} + +void i2s_background(void) { + if (NRF_I2S->EVENTS_TXPTRUPD) { + NRF_I2S->EVENTS_TXPTRUPD = 0; + if (instance) { + i2s_buffer_fill(instance); + } else { + NRF_I2S->TASKS_STOP = 1; + } + } +} + +void i2s_reset(void) { + NRF_I2S->TASKS_STOP = 1; + NRF_I2S->ENABLE = I2S_ENABLE_ENABLE_Disabled; + NRF_I2S->PSEL.MCK = 0xFFFFFFFF; + NRF_I2S->PSEL.SCK = 0xFFFFFFFF; + NRF_I2S->PSEL.LRCK = 0xFFFFFFFF; + NRF_I2S->PSEL.SDOUT = 0xFFFFFFFF; + NRF_I2S->PSEL.SDIN = 0xFFFFFFFF; + instance = NULL; } diff --git a/ports/nrf/common-hal/audiobusio/I2SOut.h b/ports/nrf/common-hal/audiobusio/I2SOut.h index 01c944e721..16047e0ab8 100644 --- a/ports/nrf/common-hal/audiobusio/I2SOut.h +++ b/ports/nrf/common-hal/audiobusio/I2SOut.h @@ -31,6 +31,33 @@ typedef struct { mp_obj_base_t base; + + mp_obj_t *sample; + uint8_t *buffers[2]; + uint8_t *sample_data, *sample_end; + + uint16_t buffer_length; + uint16_t sample_rate; + uint32_t hold_value; + + uint8_t next_buffer; + uint8_t bit_clock_pin_number; + uint8_t word_select_pin_number; + uint8_t data_pin_number; + + uint8_t channel_count; + uint8_t bytes_per_sample; + + bool left_justified : 1; + bool playing : 1; + bool stopping : 1; + bool paused : 1; + bool loop : 1; + bool samples_signed : 1; + bool single_buffer : 1; } audiobusio_i2sout_obj_t; +void i2s_reset(void); +void i2s_background(void); + #endif // MICROPY_INCLUDED_NRF_COMMON_HAL_AUDIOBUSIO_I2SOUT_H diff --git a/ports/nrf/supervisor/port.c b/ports/nrf/supervisor/port.c index 8e55835816..e061c90b09 100644 --- a/ports/nrf/supervisor/port.c +++ b/ports/nrf/supervisor/port.c @@ -50,6 +50,10 @@ #include "shared-bindings/rtc/__init__.h" +#ifdef CIRCUITPY_AUDIOBUSIO +#include "common-hal/audiobusio/I2SOut.h" +#endif + #ifdef CIRCUITPY_AUDIOPWMIO #include "common-hal/audiopwmio/PWMAudioOut.h" #endif @@ -98,10 +102,15 @@ void reset_port(void) { spi_reset(); uart_reset(); +#ifdef CIRCUITPY_AUDIOBUSIO + i2s_reset(); +#endif + #ifdef CIRCUITPY_AUDIOPWMIO audiopwmout_reset(); #endif + #if CIRCUITPY_PULSEIO pwmout_reset(); pulseout_reset(); From b613a50d8c1888c9ae2675d58f1f22e90bab5871 Mon Sep 17 00:00:00 2001 From: Jeff Epler Date: Sun, 8 Sep 2019 17:31:01 -0500 Subject: [PATCH 07/16] run 'make translate' --- locale/ID.po | 6 +++++- locale/circuitpython.pot | 6 +++++- locale/de_DE.po | 6 +++++- locale/en_US.po | 6 +++++- locale/en_x_pirate.po | 6 +++++- locale/es.po | 6 +++++- locale/fil.po | 6 +++++- locale/fr.po | 6 +++++- locale/it_IT.po | 6 +++++- locale/pl.po | 6 +++++- locale/pt_BR.po | 6 +++++- locale/zh_Latn_pinyin.po | 6 +++++- 12 files changed, 60 insertions(+), 12 deletions(-) diff --git a/locale/ID.po b/locale/ID.po index ab4f098d6f..63e3466b6e 100644 --- a/locale/ID.po +++ b/locale/ID.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -516,6 +516,10 @@ msgstr "Tidak bisa menyesuaikan data ke dalam paket advertisment" msgid "Destination capacity is smaller than destination_length." msgstr "" +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/circuitpython.pot b/locale/circuitpython.pot index a729f7e806..d709a69cc9 100644 --- a/locale/circuitpython.pot +++ b/locale/circuitpython.pot @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n" "Last-Translator: FULL NAME \n" "Language-Team: LANGUAGE \n" @@ -505,6 +505,10 @@ msgstr "" msgid "Destination capacity is smaller than destination_length." msgstr "" +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/de_DE.po b/locale/de_DE.po index 1dd23a620b..c846b646b7 100644 --- a/locale/de_DE.po +++ b/locale/de_DE.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: Pascal Deneaux\n" "Language-Team: Sebastian Plamauer, Pascal Deneaux\n" @@ -509,6 +509,10 @@ msgstr "Zu vielen Daten für das advertisement packet" msgid "Destination capacity is smaller than destination_length." msgstr "Die Zielkapazität ist kleiner als destination_length." +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/en_US.po b/locale/en_US.po index 7678fd1f55..652053ee99 100644 --- a/locale/en_US.po +++ b/locale/en_US.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: \n" @@ -505,6 +505,10 @@ msgstr "" msgid "Destination capacity is smaller than destination_length." msgstr "" +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/en_x_pirate.po b/locale/en_x_pirate.po index f1e49449aa..1d2fcb659c 100644 --- a/locale/en_x_pirate.po +++ b/locale/en_x_pirate.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2018-07-27 11:55-0700\n" "Last-Translator: \n" "Language-Team: @sommersoft, @MrCertainly\n" @@ -509,6 +509,10 @@ msgstr "" msgid "Destination capacity is smaller than destination_length." msgstr "" +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/es.po b/locale/es.po index fda00ed6c1..ca8593dcdf 100644 --- a/locale/es.po +++ b/locale/es.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2018-08-24 22:56-0500\n" "Last-Translator: \n" "Language-Team: \n" @@ -513,6 +513,10 @@ msgstr "Data es muy grande para el paquete de advertisement." msgid "Destination capacity is smaller than destination_length." msgstr "Capacidad de destino es mas pequeña que destination_length." +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/fil.po b/locale/fil.po index 65c94c854b..6f460c94cf 100644 --- a/locale/fil.po +++ b/locale/fil.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2018-12-20 22:15-0800\n" "Last-Translator: Timothy \n" "Language-Team: fil\n" @@ -517,6 +517,10 @@ msgid "Destination capacity is smaller than destination_length." msgstr "" "Ang kapasidad ng destinasyon ay mas maliit kaysa sa destination_length." +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/fr.po b/locale/fr.po index 9363f6e61f..c176fd8bcf 100644 --- a/locale/fr.po +++ b/locale/fr.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: 0.1\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2019-04-14 20:05+0100\n" "Last-Translator: Pierrick Couturier \n" "Language-Team: fr\n" @@ -521,6 +521,10 @@ msgstr "Données trop volumineuses pour un paquet de diffusion" msgid "Destination capacity is smaller than destination_length." msgstr "La capacité de destination est plus petite que 'destination_length'." +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/it_IT.po b/locale/it_IT.po index 3dc308111e..883c65d41f 100644 --- a/locale/it_IT.po +++ b/locale/it_IT.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2018-10-02 16:27+0200\n" "Last-Translator: Enrico Paganin \n" "Language-Team: \n" @@ -517,6 +517,10 @@ msgstr "Impossibile inserire dati nel pacchetto di advertisement." msgid "Destination capacity is smaller than destination_length." msgstr "La capacità di destinazione è più piccola di destination_length." +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/pl.po b/locale/pl.po index f879157182..9ed76924b5 100644 --- a/locale/pl.po +++ b/locale/pl.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: \n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2019-03-19 18:37-0700\n" "Last-Translator: Radomir Dopieralski \n" "Language-Team: pl\n" @@ -508,6 +508,10 @@ msgstr "Zbyt dużo danych pakietu rozgłoszeniowego" msgid "Destination capacity is smaller than destination_length." msgstr "Pojemność celu mniejsza od destination_length." +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/pt_BR.po b/locale/pt_BR.po index b6b7415d8f..a17a275390 100644 --- a/locale/pt_BR.po +++ b/locale/pt_BR.po @@ -8,7 +8,7 @@ msgid "" msgstr "" "Project-Id-Version: PACKAGE VERSION\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2018-10-02 21:14-0000\n" "Last-Translator: \n" "Language-Team: \n" @@ -512,6 +512,10 @@ msgstr "Não é possível ajustar dados no pacote de anúncios." msgid "Destination capacity is smaller than destination_length." msgstr "" +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "" diff --git a/locale/zh_Latn_pinyin.po b/locale/zh_Latn_pinyin.po index c20572038f..fe09fdaa77 100644 --- a/locale/zh_Latn_pinyin.po +++ b/locale/zh_Latn_pinyin.po @@ -7,7 +7,7 @@ msgid "" msgstr "" "Project-Id-Version: circuitpython-cn\n" "Report-Msgid-Bugs-To: \n" -"POT-Creation-Date: 2019-08-29 22:16-0500\n" +"POT-Creation-Date: 2019-09-08 17:30-0500\n" "PO-Revision-Date: 2019-04-13 10:10-0700\n" "Last-Translator: hexthat\n" "Language-Team: Chinese Hanyu Pinyin\n" @@ -509,6 +509,10 @@ msgstr "Guǎnggào bāo de shùjù tài dà" msgid "Destination capacity is smaller than destination_length." msgstr "Mùbiāo róngliàng xiǎoyú mùdì de_chángdù." +#: ports/nrf/common-hal/audiobusio/I2SOut.c +msgid "Device in use" +msgstr "" + #: shared-bindings/displayio/Display.c msgid "Display must have a 16 bit colorspace." msgstr "Xiǎnshì bìxū jùyǒu 16 wèi yánsè kōngjiān." From 8768896d6b0ddb35568ad996781a33fd3a0df94f Mon Sep 17 00:00:00 2001 From: jepler Date: Sun, 8 Sep 2019 21:17:36 -0500 Subject: [PATCH 08/16] audiomixer: Supply constants in a way "-Og" optimization expects These arguments are constrained to be compile-time constants, a fact that gcc complains about under "-Og" optimization, but not in normal builds. Declare them as enumerated types --- shared-module/audiomixer/Mixer.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-module/audiomixer/Mixer.c b/shared-module/audiomixer/Mixer.c index e0fb4fab89..a52489f856 100644 --- a/shared-module/audiomixer/Mixer.c +++ b/shared-module/audiomixer/Mixer.c @@ -262,8 +262,8 @@ static inline uint32_t mult16signed(uint32_t val, int32_t mul) { } #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 + enum { bits = 16 }; // saturate to 16 bits + enum { 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)); From ccf08aa3dfbaf97f2792fe55cfcbecd5ee687588 Mon Sep 17 00:00:00 2001 From: jepler Date: Sun, 8 Sep 2019 21:58:04 -0500 Subject: [PATCH 09/16] nrf: I2SOut: deal more gracefully with errors from the sample --- ports/nrf/common-hal/audiobusio/I2SOut.c | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/ports/nrf/common-hal/audiobusio/I2SOut.c b/ports/nrf/common-hal/audiobusio/I2SOut.c index 6c64fb8cd4..25ae48f592 100644 --- a/ports/nrf/common-hal/audiobusio/I2SOut.c +++ b/ports/nrf/common-hal/audiobusio/I2SOut.c @@ -140,6 +140,10 @@ stopping: ; goto stopping; } } + if (get_buffer_result == GET_BUFFER_ERROR || sample_buffer_length == 0) { + self->stopping = true; + goto stopping; + } } uint16_t bytecount = MIN(bytesleft, (size_t)(self->sample_end - self->sample_data)); if (self->samples_signed) { From 82427612d15ac85d0a34132ace7f8369dacd66ef Mon Sep 17 00:00:00 2001 From: jepler Date: Sun, 8 Sep 2019 21:58:36 -0500 Subject: [PATCH 10/16] WaveFile: Return GET_BUFFER_ERROR if wrong amount read Closes: #2128 --- shared-module/audiocore/WaveFile.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/shared-module/audiocore/WaveFile.c b/shared-module/audiocore/WaveFile.c index df5d4b61de..0248d42e22 100644 --- a/shared-module/audiocore/WaveFile.c +++ b/shared-module/audiocore/WaveFile.c @@ -216,7 +216,7 @@ audioio_get_buffer_result_t audioio_wavefile_get_buffer(audioio_wavefile_obj_t* } else { *buffer = self->buffer; } - if (f_read(&self->file->fp, *buffer, num_bytes_to_load, &length_read) != FR_OK) { + if (f_read(&self->file->fp, *buffer, num_bytes_to_load, &length_read) != FR_OK || length_read != num_bytes_to_load) { return GET_BUFFER_ERROR; } self->bytes_remaining -= length_read; From d9e06416062f7b45e0a32d1f7b251cb30f2432a4 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Sun, 8 Sep 2019 21:30:52 -0700 Subject: [PATCH 11/16] Set dither default to False and document it --- shared-bindings/displayio/ColorConverter.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c index 75eb8ba328..db2d1b6e29 100644 --- a/shared-bindings/displayio/ColorConverter.c +++ b/shared-bindings/displayio/ColorConverter.c @@ -43,7 +43,7 @@ //| //| Converts one color format to another. //| -//| .. class:: ColorConverter() +//| .. class:: ColorConverter(*, dither=False) //| //| Create a ColorConverter object to convert color formats. Only supports RGB888 to RGB565 //| currently. @@ -55,7 +55,7 @@ STATIC mp_obj_t displayio_colorconverter_make_new(const mp_obj_type_t *type, siz enum { ARG_dither}; static const mp_arg_t allowed_args[] = { - { MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = true} }, + { MP_QSTR_dither, MP_ARG_KW_ONLY | MP_ARG_BOOL, {.u_bool = false} }, }; mp_arg_val_t args[MP_ARRAY_SIZE(allowed_args)]; mp_arg_parse_all(n_args, pos_args, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args); From 8f6267115cc2595bbecdd4a52a7f296439d8a094 Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 9 Sep 2019 15:12:06 -0400 Subject: [PATCH 12/16] Renumber only chosen USB interfaces; fix HID report ids --- tools/gen_usb_descriptor.py | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) diff --git a/tools/gen_usb_descriptor.py b/tools/gen_usb_descriptor.py index 87219194bc..6bb401ef51 100644 --- a/tools/gen_usb_descriptor.py +++ b/tools/gen_usb_descriptor.py @@ -173,17 +173,21 @@ msc_interfaces = [ # args.hid_devices[1] has report_id 2 # etc. +report_ids = {} + if len(args.hid_devices) == 1: name = args.hid_devices[0] combined_hid_report_descriptor = hid.ReportDescriptor( description=name, report_descriptor=bytes(hid_report_descriptors.REPORT_DESCRIPTOR_FUNCTIONS[name](0))) + report_ids[name] = 0 else: report_id = 1 concatenated_descriptors = bytearray() for name in args.hid_devices: concatenated_descriptors.extend( bytes(hid_report_descriptors.REPORT_DESCRIPTOR_FUNCTIONS[name](report_id))) + report_ids[name] = report_id report_id += 1 combined_hid_report_descriptor = hid.ReportDescriptor( description="MULTIDEVICE", @@ -288,10 +292,24 @@ audio_control_interface = standard.InterfaceDescriptor( # Audio streaming interfaces must occur before MIDI ones. audio_interfaces = [audio_control_interface] + cs_ac_interface.audio_streaming_interfaces + cs_ac_interface.midi_streaming_interfaces -# This will renumber the endpoints to make them unique across descriptors, +interfaces_to_join = [] + +if 'CDC' in args.devices: + interfaces_to_join.append(cdc_interfaces) + +if 'MSC' in args.devices: + interfaces_to_join.append(msc_interfaces) + +if 'HID' in args.devices: + interfaces_to_join.append(hid_interfaces) + +if 'AUDIO' in args.devices: + interfaces_to_join.append(audio_interfaces) + +# util.join_interfaces() will renumber the endpoints to make them unique across descriptors, # and renumber the interfaces in order. But we still need to fix up certain # interface cross-references. -interfaces = util.join_interfaces(cdc_interfaces, msc_interfaces, hid_interfaces, audio_interfaces) +interfaces = util.join_interfaces(*interfaces_to_join) # Now adjust the CDC interface cross-references. @@ -323,13 +341,15 @@ if 'CDC' in args.devices: if 'MSC' in args.devices: descriptor_list.extend(msc_interfaces) +if 'HID' in args.devices: + descriptor_list.extend(hid_interfaces) + if 'AUDIO' in args.devices: # Only add the control interface because other audio interfaces are managed by it to ensure the # correct ordering. descriptor_list.append(audio_control_interface) -if 'HID' in args.devices: - descriptor_list.extend(hid_interfaces) +# Finally, build the composite descriptor. configuration = standard.ConfigurationDescriptor( description="Composite configuration", @@ -502,7 +522,7 @@ c_file.write("""\ """) # Write out USB HID report buffer definitions. -for report_id, name in enumerate(args.hid_devices, start=1): +for name in args.hid_devices: c_file.write("""\ static uint8_t {name}_report_buffer[{report_length}]; """.format(name=name.lower(), report_length=hid_report_descriptors.HID_DEVICE_DATA[name].report_length)) @@ -511,18 +531,18 @@ static uint8_t {name}_report_buffer[{report_length}]; c_file.write(""" usb_hid_device_obj_t usb_hid_devices[] = { """); -for report_id, name in enumerate(args.hid_devices, start=1): +for name in args.hid_devices: device_data = hid_report_descriptors.HID_DEVICE_DATA[name] c_file.write("""\ {{ .base = {{ .type = &usb_hid_device_type }}, .report_buffer = {name}_report_buffer, - .report_id = {report_id:}, + .report_id = {report_id}, .report_length = {report_length}, .usage_page = {usage_page:#04x}, .usage = {usage:#04x}, }}, -""".format(name=name.lower(), report_id=report_id, +""".format(name=name.lower(), report_id=report_ids[name], report_length=device_data.report_length, usage_page=device_data.usage_page, usage=device_data.usage)) From fe9605a6a39e92f368658fb731f38ce0830ebe68 Mon Sep 17 00:00:00 2001 From: jepler Date: Mon, 9 Sep 2019 19:25:52 -0500 Subject: [PATCH 13/16] nrf: i2s: Comment this slightly tricksy code --- ports/nrf/common-hal/audiobusio/I2SOut.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/ports/nrf/common-hal/audiobusio/I2SOut.c b/ports/nrf/common-hal/audiobusio/I2SOut.c index 25ae48f592..e18ed92693 100644 --- a/ports/nrf/common-hal/audiobusio/I2SOut.c +++ b/ports/nrf/common-hal/audiobusio/I2SOut.c @@ -167,11 +167,17 @@ stopping: ; self->sample_data += bytecount; bytesleft -= bytecount; } + + // Find the last frame of real audio data and replicate its samples until + // you have 32 bits worth, which is the fundamental unit of nRF I2S DMA if (self->bytes_per_sample == 1 && self->channel_count == 1) { - self->hold_value = 0x01010101 * *(uint8_t*)(buffer-1); + // For 8-bit mono, 4 copies of the final sample are required + self->hold_value = 0x01010101 * *(uint8_t*)(buffer-1); } else if (self->bytes_per_sample == 2 && self->channel_count == 2) { + // For 16-bit stereo, 1 copy of the final sample is required self->hold_value = *(uint32_t*)(buffer-4); } else { + // For 8-bit stereo and 16-bit mono, 2 copies of the final sample are required self->hold_value = 0x00010001 * *(uint16_t*)(buffer-2); } } From c66f5a853644c0bc2bade243b1e7a9e9a69c1b4b Mon Sep 17 00:00:00 2001 From: jepler Date: Mon, 9 Sep 2019 19:26:18 -0500 Subject: [PATCH 14/16] nrf: i2s: rewrite without 'goto' --- ports/nrf/common-hal/audiobusio/I2SOut.c | 34 +++++++++++++----------- 1 file changed, 18 insertions(+), 16 deletions(-) diff --git a/ports/nrf/common-hal/audiobusio/I2SOut.c b/ports/nrf/common-hal/audiobusio/I2SOut.c index e18ed92693..e167e964f6 100644 --- a/ports/nrf/common-hal/audiobusio/I2SOut.c +++ b/ports/nrf/common-hal/audiobusio/I2SOut.c @@ -112,20 +112,7 @@ static void i2s_buffer_fill(audiobusio_i2sout_obj_t* self) { self->next_buffer = !self->next_buffer; size_t bytesleft = self->buffer_length; - if (self->paused || self->stopping) { - if (self->stopping) { - NRF_I2S->TASKS_STOP = 1; - self->playing = false; - } -stopping: ; - uint32_t *bp = (uint32_t*)buffer; - uint32_t *be = (uint32_t*)(buffer + bytesleft); - for (; bp != be; ) - *bp++ = self->hold_value; - return; - } - - while (bytesleft) { + while (!self->paused && !self->stopping && bytesleft) { if (self->sample_data == self->sample_end) { uint32_t sample_buffer_length; audioio_get_buffer_result_t get_buffer_result = @@ -137,12 +124,12 @@ stopping: ; audiosample_reset_buffer(self->sample, false, 0); } else { self->stopping = true; - goto stopping; + break; } } if (get_buffer_result == GET_BUFFER_ERROR || sample_buffer_length == 0) { self->stopping = true; - goto stopping; + break; } } uint16_t bytecount = MIN(bytesleft, (size_t)(self->sample_end - self->sample_data)); @@ -180,6 +167,21 @@ stopping: ; // For 8-bit stereo and 16-bit mono, 2 copies of the final sample are required self->hold_value = 0x00010001 * *(uint16_t*)(buffer-2); } + + // Emulate pausing and stopping by filling the DMA buffer with copies of + // the last sample. This includes the case where this iteration of + // i2s_buffer_fill exhausted a non-looping sample. + if (self->paused || self->stopping) { + if (self->stopping) { + NRF_I2S->TASKS_STOP = 1; + self->playing = false; + } + uint32_t *bp = (uint32_t*)buffer; + uint32_t *be = (uint32_t*)(buffer + bytesleft); + for (; bp != be; ) + *bp++ = self->hold_value; + return; + } } void common_hal_audiobusio_i2sout_construct(audiobusio_i2sout_obj_t* self, From 7b9dfc9952cf4af12de14fa21831a9324b7bc025 Mon Sep 17 00:00:00 2001 From: jepler Date: Mon, 9 Sep 2019 20:12:35 -0500 Subject: [PATCH 15/16] nrf: i2s: tune audio buffering .. based on some tasks I found that caused stuttering: # Test SD and printing while True: os.listdir('.') # Test bulk I/O while True: len(open('somefile.wav', 'rb').read()) Each of these tasks *WAS* worse and I am improving them in a separate PR by adding RUN_BACKGROUND_TASKS to them. --- ports/nrf/common-hal/audiobusio/I2SOut.c | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/ports/nrf/common-hal/audiobusio/I2SOut.c b/ports/nrf/common-hal/audiobusio/I2SOut.c index e167e964f6..ac369277f2 100644 --- a/ports/nrf/common-hal/audiobusio/I2SOut.c +++ b/ports/nrf/common-hal/audiobusio/I2SOut.c @@ -247,12 +247,14 @@ void common_hal_audiobusio_i2sout_play(audiobusio_i2sout_obj_t* self, self->samples_signed = samples_signed; choose_i2s_clocking(self, sample_rate); - /* Allocate buffers based on a maximum duration */ - enum { buffer_length_ms = 8 }; - self->buffer_length = MAX( - 2*max_buffer_length, - sample_rate * buffer_length_ms * self->bytes_per_sample - * self->channel_count / 1000); + /* Allocate buffers based on a maximum duration + * This duration was chosen empirically based on what would + * cause os.listdir('') to cause stuttering. It seems like a + * rather long time. + */ + enum { buffer_length_ms = 16 }; + self->buffer_length = sample_rate * buffer_length_ms + * self->bytes_per_sample * self->channel_count / 1000; self->buffer_length = (self->buffer_length + 3) & ~3; self->buffers[0] = m_malloc(self->buffer_length, false); self->buffers[1] = m_malloc(self->buffer_length, false); From b8200d72950f4d360a29e2f62d49773954942e5f Mon Sep 17 00:00:00 2001 From: Dan Halbert Date: Mon, 9 Sep 2019 23:17:52 -0400 Subject: [PATCH 16/16] fix atmel-samd filesystem_tick'ing; clear_temp_status() should check for status indicator in use --- ports/atmel-samd/tick.c | 12 ++++++++---- supervisor/shared/external_flash/external_flash.c | 1 + supervisor/shared/rgb_led_status.c | 6 ++++++ 3 files changed, 15 insertions(+), 4 deletions(-) diff --git a/ports/atmel-samd/tick.c b/ports/atmel-samd/tick.c index dde473375e..4d7bb9dca7 100644 --- a/ports/atmel-samd/tick.c +++ b/ports/atmel-samd/tick.c @@ -29,6 +29,7 @@ #include "peripheral_clk_config.h" #include "supervisor/shared/autoreload.h" +#include "supervisor/filesystem.h" #include "shared-bindings/microcontroller/__init__.h" #include "shared-bindings/microcontroller/Processor.h" @@ -52,10 +53,13 @@ void SysTick_Handler(void) { (void) SysTick->CTRL; common_hal_mcu_enable_interrupts(); - #ifdef CIRCUITPY_AUTORELOAD_DELAY_MS +#if CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS > 0 + filesystem_tick(); +#endif +#ifdef CIRCUITPY_AUTORELOAD_DELAY_MS autoreload_tick(); - #endif - #ifdef CIRCUITPY_GAMEPAD_TICKS +#endif +#ifdef CIRCUITPY_GAMEPAD_TICKS if (!(ticks_ms & CIRCUITPY_GAMEPAD_TICKS)) { #if CIRCUITPY_GAMEPAD gamepad_tick(); @@ -64,7 +68,7 @@ void SysTick_Handler(void) { gamepadshift_tick(); #endif } - #endif +#endif } void tick_init() { diff --git a/supervisor/shared/external_flash/external_flash.c b/supervisor/shared/external_flash/external_flash.c index ad10bab516..99df553e19 100644 --- a/supervisor/shared/external_flash/external_flash.c +++ b/supervisor/shared/external_flash/external_flash.c @@ -428,6 +428,7 @@ static bool flush_ram_cache(bool keep_cache) { } // Delegates to the correct flash flush method depending on the existing cache. +// TODO Don't blink the status indicator if we don't actually do any writing (hard to tell right now). static void spi_flash_flush_keep_cache(bool keep_cache) { #ifdef MICROPY_HW_LED_MSC port_pin_set_output_level(MICROPY_HW_LED_MSC, true); diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index fd356cb48f..4d93715161 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -266,9 +266,15 @@ void temp_status_color(uint32_t rgb) { void clear_temp_status() { #ifdef MICROPY_HW_NEOPIXEL + if (neopixel_in_use) { + return; + } common_hal_neopixel_write(&status_neopixel, status_neopixel_color, 3); #endif #if defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK) + if (apa102_mosi_in_use || apa102_sck_in_use) { + return; + } #if CIRCUITPY_BITBANG_APA102 shared_module_bitbangio_spi_write(&status_apa102, status_apa102_color, APA102_BUFFER_LENGTH); #else