Skip to content

Commit 121903b

Browse files
committed
Tweaks based on feedback
1 parent 6697544 commit 121903b

File tree

8 files changed

+50
-59
lines changed

8 files changed

+50
-59
lines changed

ports/atmel-samd/boards/hallowing_m0_express/board.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ void board_init(void) {
8484
} else if (*cmd == 0x11) {
8585
uint64_t start = ticks_ms;
8686
while (ticks_ms - start < 500) {}
87-
} {
87+
} else {
8888
uint64_t start = ticks_ms;
8989
while (ticks_ms - start < 10) {}
9090
}

ports/atmel-samd/common-hal/displayio/FourWire.c

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -63,22 +63,17 @@ bool common_hal_displayio_fourwire_begin_transaction(displayio_fourwire_obj_t* s
6363
if (!common_hal_busio_spi_try_lock(&self->bus)) {
6464
return false;
6565
}
66+
// TODO(tannewt): Stop hardcoding SPI frequency, polarity and phase.
6667
common_hal_busio_spi_configure(&self->bus, 12000000, 0, 0, 8);
6768
common_hal_digitalio_digitalinout_set_value(&self->chip_select, false);
6869
return true;
6970
}
7071

71-
void common_hal_displayio_fourwire_wait_for_send(displayio_fourwire_obj_t* self) {
72-
}
73-
7472
void common_hal_displayio_fourwire_send(displayio_fourwire_obj_t* self, bool command, uint8_t *data, uint32_t data_length) {
75-
common_hal_displayio_fourwire_wait_for_send(self);
7673
common_hal_digitalio_digitalinout_set_value(&self->command, !command);
7774
common_hal_busio_spi_write(&self->bus, data, data_length);
78-
7975
}
8076

81-
8277
void common_hal_displayio_fourwire_end_transaction(displayio_fourwire_obj_t* self) {
8378
common_hal_digitalio_digitalinout_set_value(&self->chip_select, true);
8479
common_hal_busio_spi_unlock(&self->bus);
@@ -106,6 +101,7 @@ static uint16_t swap(uint16_t x) {
106101
}
107102

108103
void displayio_fourwire_start_region_update(displayio_fourwire_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) {
104+
// TODO(tannewt): Handle displays with single byte bounds.
109105
common_hal_displayio_fourwire_begin_transaction(self);
110106
uint16_t data[2];
111107
common_hal_displayio_fourwire_send(self, true, &self->set_column_command, 1);

ports/atmel-samd/mpconfigport.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -239,8 +239,6 @@ extern const struct _mp_obj_module_t usb_hid_module;
239239
#define DISPLAYIO_MODULE
240240
#endif
241241

242-
243-
244242
#ifndef EXTRA_BUILTIN_MODULES
245243
#define EXTRA_BUILTIN_MODULES \
246244
AUDIOIO_MODULE \

shared-bindings/displayio/Bitmap.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,30 +45,29 @@
4545
//|
4646
//| .. warning:: This will likely be changed before 4.0.0. Consider it very experimental.
4747
//|
48-
//| .. class:: Bitmap(width, height, value_size)
48+
//| .. class:: Bitmap(width, height, value_count)
4949
//|
50-
//| Create a Bitmap object with the given fixed size.
50+
//| Create a Bitmap object with the given fixed size. Each pixel stores a value that is used to
51+
//| index into a corresponding palette. This enables differently colored sprites to share the
52+
//| underlying Bitmap. value_count is used to minimize the memory used to store the Bitmap.
5153
//|
5254
//| :param int width: The number of values wide
5355
//| :param int height: The number of values high
54-
//| :param int value_size: The value size in bits. Must be power of 2.
56+
//| :param int value_count: The number of possible pixel values.
5557
//|
5658
STATIC mp_obj_t displayio_bitmap_make_new(const mp_obj_type_t *type, size_t n_args, size_t n_kw, const mp_obj_t *pos_args) {
5759
mp_arg_check_num(n_args, n_kw, 3, 3, false);
5860
uint32_t width = mp_obj_get_int(pos_args[0]);
5961
uint32_t height = mp_obj_get_int(pos_args[1]);
60-
uint32_t value_size = mp_obj_get_int(pos_args[2]);
62+
uint32_t value_count = mp_obj_get_int(pos_args[2]);
6163
uint32_t power_of_two = 1;
62-
while (value_size > power_of_two) {
64+
while (value_count > (1U << power_of_two)) {
6365
power_of_two <<= 1;
6466
}
65-
if (value_size != power_of_two) {
66-
mp_raise_ValueError(translate("value_size must be power of two"));
67-
}
6867

6968
displayio_bitmap_t *self = m_new_obj(displayio_bitmap_t);
7069
self->base.type = &displayio_bitmap_type;
71-
common_hal_displayio_bitmap_construct(self, width, height, value_size);
70+
common_hal_displayio_bitmap_construct(self, width, height, power_of_two);
7271

7372
return MP_OBJ_FROM_PTR(self);
7473
}

shared-bindings/displayio/Group.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ STATIC mp_obj_t displayio_group_make_new(const mp_obj_type_t *type, size_t n_arg
7474

7575
//| .. method:: append(layer)
7676
//|
77-
//| Switches do displaying the given group of elements.
77+
//| Append a layer to the group. It will be drawn above other layers.
7878
//|
7979
STATIC mp_obj_t displayio_group_obj_append(mp_obj_t self_in, mp_obj_t layer) {
8080
displayio_group_t *self = MP_OBJ_TO_PTR(self_in);

shared-bindings/displayio/Palette.c

Lines changed: 34 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@
4141
//| :class:`Palette` -- Stores a mapping from bitmap pixel values to display colors
4242
//| ===============================================================================
4343
//|
44-
//| Manage updating a display over SPI four wire protocol in the background while Python code runs.
45-
//| It doesn't handle display initialization.
44+
//| Map a pixel value to a full color. Colors are transformed to the display's format internally to
45+
//| save memory.
4646
//|
4747
//| .. warning:: This will be changed before 4.0.0. Consider it very experimental.
4848
//|
@@ -76,42 +76,41 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val
7676
if (value == MP_OBJ_NULL) {
7777
// delete item
7878
return MP_OBJ_NULL; // op not supported
79-
} else {
80-
displayio_palette_t *self = MP_OBJ_TO_PTR(self_in);
81-
if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
82-
return MP_OBJ_NULL; // Slicing not supported. Use a duplicate Palette to swap multiple colors atomically.
83-
} else {
84-
if (value == MP_OBJ_SENTINEL) {
85-
return MP_OBJ_NULL; // index read is not supported
86-
} else {
87-
size_t index = mp_get_index(&displayio_palette_type, self->max_value, index_in, false);
79+
}
80+
// Slicing not supported. Use a duplicate Palette to swap multiple colors atomically.
81+
if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) {
82+
return MP_OBJ_NULL;
83+
}
84+
// index read is not supported
85+
if (value == MP_OBJ_SENTINEL) {
86+
return MP_OBJ_NULL;
87+
}
88+
displayio_palette_t *self = MP_OBJ_TO_PTR(self_in);
89+
size_t index = mp_get_index(&displayio_palette_type, self->max_value, index_in, false);
8890

89-
uint32_t color;
90-
mp_int_t int_value;
91-
mp_buffer_info_t bufinfo;
92-
if (mp_get_buffer(value, &bufinfo, MP_BUFFER_READ)) {
93-
if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
94-
mp_raise_ValueError(translate("color buffer must be a bytearray or array of type 'b' or 'B'"));
95-
}
96-
uint8_t* buf = bufinfo.buf;
97-
if (bufinfo.len == 3 || bufinfo.len == 4) {
98-
color = buf[0] << 16 | buf[1] << 8 | buf[2];
99-
} else {
100-
mp_raise_ValueError(translate("color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)"));
101-
}
102-
} else if (mp_obj_get_int_maybe(value, &int_value)) {
103-
if (int_value < 0 || int_value > 0xffffff) {
104-
mp_raise_TypeError(translate("color must be between 0x000000 and 0xffffff"));
105-
}
106-
color = int_value;
107-
} else {
108-
mp_raise_TypeError(translate("color buffer must be a buffer or int"));
109-
}
110-
common_hal_displayio_palette_set_color(self, index, color);
111-
return mp_const_none;
112-
}
91+
uint32_t color;
92+
mp_int_t int_value;
93+
mp_buffer_info_t bufinfo;
94+
if (mp_get_buffer(value, &bufinfo, MP_BUFFER_READ)) {
95+
if (bufinfo.typecode != 'b' && bufinfo.typecode != 'B' && bufinfo.typecode != BYTEARRAY_TYPECODE) {
96+
mp_raise_ValueError(translate("color buffer must be a bytearray or array of type 'b' or 'B'"));
11397
}
98+
uint8_t* buf = bufinfo.buf;
99+
if (bufinfo.len == 3 || bufinfo.len == 4) {
100+
color = buf[0] << 16 | buf[1] << 8 | buf[2];
101+
} else {
102+
mp_raise_ValueError(translate("color buffer must be 3 bytes (RGB) or 4 bytes (RGBA)"));
103+
}
104+
} else if (mp_obj_get_int_maybe(value, &int_value)) {
105+
if (int_value < 0 || int_value > 0xffffff) {
106+
mp_raise_TypeError(translate("color must be between 0x000000 and 0xffffff"));
107+
}
108+
color = int_value;
109+
} else {
110+
mp_raise_TypeError(translate("color buffer must be a buffer or int"));
114111
}
112+
common_hal_displayio_palette_set_color(self, index, color);
113+
return mp_const_none;
115114
}
116115

117116
//| .. method:: make_transparent(value)

shared-bindings/displayio/__init__.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,4 @@
3131

3232
// Nothing now.
3333

34-
// typedef enum {
35-
// PIXEL_
36-
// } displayio_pixel_format;
37-
3834
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H

shared-module/displayio/Bitmap.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ void common_hal_displayio_bitmap_construct(displayio_bitmap_t *self, uint32_t wi
5656
}
5757

5858
void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y, uint8_t* data, uint16_t len) {
59-
if (len != self->stride * 4) {
59+
if (len != self->stride * sizeof(uint32_t)) {
6060
mp_raise_ValueError(translate("row must be packed and word aligned"));
6161
}
6262
uint32_t* row_value = self->data + (y * self->stride);
@@ -77,6 +77,9 @@ void common_hal_displayio_bitmap_load_row(displayio_bitmap_t *self, uint16_t y,
7777
}
7878
}
7979
uint32_t common_hal_displayio_bitmap_get_pixel(displayio_bitmap_t *self, int16_t x, int16_t y) {
80+
if (x >= self->width || x < 0 || y >= self->height || y < 0) {
81+
return 0;
82+
}
8083
int32_t row_start = y * self->stride;
8184
if (self->bits_per_value < 8) {
8285
uint32_t word = self->data[row_start + (x >> self->x_shift)];

0 commit comments

Comments
 (0)