Skip to content

Commit 7324b70

Browse files
committed
Rework based on Dan's review
1 parent b992ca8 commit 7324b70

File tree

26 files changed

+137
-132
lines changed

26 files changed

+137
-132
lines changed

ports/atmel-samd/boards/monster_m4sk/mpconfigboard.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,5 +27,3 @@
2727
// USB is always used internally so skip the pin objects for it.
2828
#define IGNORE_PIN_PA24 1
2929
#define IGNORE_PIN_PA25 1
30-
31-
#define CIRCUITPY_FS_NAME "MONSTERM4SK"

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,9 +131,9 @@ bool common_hal_displayio_parallelbus_begin_transaction(mp_obj_t obj) {
131131
return true;
132132
}
133133

134-
void common_hal_displayio_parallelbus_send(mp_obj_t obj, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length) {
134+
void common_hal_displayio_parallelbus_send(mp_obj_t obj, display_byte_type_t byte_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length) {
135135
displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj);
136-
common_hal_digitalio_digitalinout_set_value(&self->command, !command);
136+
common_hal_digitalio_digitalinout_set_value(&self->command, byte_type == DISPLAY_DATA);
137137
uint32_t* clear_write = (uint32_t*) &self->write_group->OUTCLR.reg;
138138
uint32_t* set_write = (uint32_t*) &self->write_group->OUTSET.reg;
139139
uint32_t mask = self->write_mask;

ports/nrf/common-hal/displayio/ParallelBus.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -142,9 +142,10 @@ bool common_hal_displayio_parallelbus_begin_transaction(mp_obj_t obj) {
142142
return true;
143143
}
144144

145-
void common_hal_displayio_parallelbus_send(mp_obj_t obj, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length) {
145+
// This ignores chip_select behaviour because data is clocked in by the write line toggling.
146+
void common_hal_displayio_parallelbus_send(mp_obj_t obj, display_byte_type_t byte_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length) {
146147
displayio_parallelbus_obj_t* self = MP_OBJ_TO_PTR(obj);
147-
common_hal_digitalio_digitalinout_set_value(&self->command, !command);
148+
common_hal_digitalio_digitalinout_set_value(&self->command, byte_type == DISPLAY_DATA);
148149
uint32_t* clear_write = (uint32_t*) &self->write_group->OUTCLR;
149150
uint32_t* set_write = (uint32_t*) &self->write_group->OUTSET;
150151
uint32_t mask = self->write_mask;

py/circuitpy_mpconfig.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -657,8 +657,4 @@ void run_background_tasks(void);
657657
#define CIRCUITPY_FILESYSTEM_FLUSH_INTERVAL_MS 1000
658658
#define CIRCUITPY_BOOT_OUTPUT_FILE "/boot_out.txt"
659659

660-
#ifndef CIRCUITPY_FS_NAME
661-
#define CIRCUITPY_FS_NAME "CIRCUITPY"
662-
#endif
663-
664660
#endif // __INCLUDED_MPCONFIG_CIRCUITPY_H

shared-bindings/_stage/__init__.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ STATIC mp_obj_t stage_render(size_t n_args, const mp_obj_t *args) {
107107
area.y2 = y1;
108108
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);
109109

110-
display->core.send(display->core.bus, true, true, &display->write_ram_command, 1);
110+
display->core.send(display->core.bus, DISPLAY_COMMAND, CHIP_SELECT_TOGGLE_EVERY_BYTE, &display->write_ram_command, 1);
111111
render_stage(x0, y0, x1, y1, layers, layers_size, buffer, buffer_size,
112112
display, scale);
113113
displayio_display_core_end_transaction(&display->core);

shared-bindings/displayio/Display.c

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -218,16 +218,22 @@ STATIC mp_obj_t displayio_display_obj_show(mp_obj_t self_in, mp_obj_t group_in)
218218
}
219219
MP_DEFINE_CONST_FUN_OBJ_2(displayio_display_show_obj, displayio_display_obj_show);
220220

221-
//| .. method:: refresh(*, target_frames_per_second=None, minimum_frames_per_second=1)
221+
//| .. method:: refresh(*, target_frames_per_second=60, minimum_frames_per_second=1)
222222
//|
223-
//| When auto refresh is off, waits for the target frame rate and then refreshes the display. If
224-
//| the call is too late for the given target frame rate, then the refresh returns immediately
225-
//| without updating the screen to hopefully help getting caught up. If the current frame rate
226-
//| is below the minimum frame rate, then an exception will be raised.
223+
//| When auto refresh is off, waits for the target frame rate and then refreshes the display,
224+
//| returning True. If the call has taken too long since the last refresh call for the given
225+
//| target frame rate, then the refresh returns False immediately without updating the screen to
226+
//| hopefully help getting caught up.
227+
//|
228+
//| If the time since the last successful refresh is below the minimum frame rate, then an
229+
//| exception will be raised. Set minimum_frames_per_second to 0 to disable.
227230
//|
228231
//| When auto refresh is on, updates the display immediately. (The display will also update
229232
//| without calls to this.)
230233
//|
234+
//| :param int target_frames_per_second: How many times a second `refresh` should be called and the screen updated.
235+
//| :param int minimum_frames_per_second: The minimum number of times the screen should be updated per second.
236+
//|
231237
STATIC mp_obj_t displayio_display_obj_refresh(size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) {
232238
enum { ARG_target_frames_per_second, ARG_minimum_frames_per_second };
233239
static const mp_arg_t allowed_args[] = {
@@ -238,8 +244,12 @@ STATIC mp_obj_t displayio_display_obj_refresh(size_t n_args, const mp_obj_t *pos
238244
mp_arg_parse_all(n_args - 1, pos_args + 1, kw_args, MP_ARRAY_SIZE(allowed_args), allowed_args, args);
239245

240246
displayio_display_obj_t *self = native_display(pos_args[0]);
241-
common_hal_displayio_display_refresh(self, 1000 / args[ARG_target_frames_per_second].u_int, 1000 / args[ARG_minimum_frames_per_second].u_int);
242-
return mp_const_none;
247+
uint32_t maximum_ms_per_real_frame = 0xffffffff;
248+
mp_int_t minimum_frames_per_second = args[ARG_minimum_frames_per_second].u_int;
249+
if (minimum_frames_per_second > 0) {
250+
maximum_ms_per_real_frame = 1000 / minimum_frames_per_second;
251+
}
252+
return mp_obj_new_bool(common_hal_displayio_display_refresh(self, 1000 / args[ARG_target_frames_per_second].u_int, maximum_ms_per_real_frame));
243253
}
244254
MP_DEFINE_CONST_FUN_OBJ_KW(displayio_display_refresh_obj, 1, displayio_display_obj_refresh);
245255

shared-bindings/displayio/Display.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ bool common_hal_displayio_display_show(displayio_display_obj_t* self,
5151
displayio_group_t* root_group);
5252

5353
// Times in ms.
54-
void common_hal_displayio_display_refresh(displayio_display_obj_t* self, uint32_t target_frame_time, uint32_t maximum_frame_time);
54+
bool common_hal_displayio_display_refresh(displayio_display_obj_t* self, uint32_t target_ms_per_frame, uint32_t maximum_ms_per_real_frame);
5555

5656
bool common_hal_displayio_display_get_auto_refresh(displayio_display_obj_t* self);
5757
void common_hal_displayio_display_set_auto_refresh(displayio_display_obj_t* self, bool auto_refresh);

shared-bindings/displayio/EPaperDisplay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
//|
5656
//| Create a EPaperDisplay object on the given display bus (`displayio.FourWire` or `displayio.ParallelBus`).
5757
//|
58-
//| The ``start_sequence`` and ``stop_sequence`` bitpacked to minimize the ram impact. Every
58+
//| The ``start_sequence`` and ``stop_sequence`` are bitpacked to minimize the ram impact. Every
5959
//| command begins with a command byte followed by a byte to determine the parameter count and if
6060
//| a delay is need after. When the top bit of the second byte is 1, the next byte will be the
6161
//| delay time in milliseconds. The remaining 7 bits are the parameter count excluding any delay

shared-bindings/displayio/FourWire.c

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,12 @@ STATIC mp_obj_t displayio_fourwire_obj_send(size_t n_args, const mp_obj_t *pos_a
146146
while (!common_hal_displayio_fourwire_begin_transaction(self)) {
147147
RUN_BACKGROUND_TASKS;
148148
}
149-
common_hal_displayio_fourwire_send(self, true, args[ARG_toggle_every_byte].u_bool, &command, 1);
150-
common_hal_displayio_fourwire_send(self, false, args[ARG_toggle_every_byte].u_bool, ((uint8_t*) bufinfo.buf), bufinfo.len);
149+
display_chip_select_behavior_t chip_select = CHIP_SELECT_UNTOUCHED;
150+
if (args[ARG_toggle_every_byte].u_bool) {
151+
chip_select = CHIP_SELECT_TOGGLE_EVERY_BYTE;
152+
}
153+
common_hal_displayio_fourwire_send(self, DISPLAY_COMMAND, chip_select, &command, 1);
154+
common_hal_displayio_fourwire_send(self, DISPLAY_DATA, chip_select, ((uint8_t*) bufinfo.buf), bufinfo.len);
151155
common_hal_displayio_fourwire_end_transaction(self);
152156

153157
return mp_const_none;

shared-bindings/displayio/FourWire.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_FOURWIRE_H
2929

3030
#include "shared-module/displayio/FourWire.h"
31+
32+
#include "shared-bindings/displayio/__init__.h"
3133
#include "common-hal/microcontroller/Pin.h"
3234

3335
#include "shared-module/displayio/Group.h"
@@ -45,7 +47,7 @@ bool common_hal_displayio_fourwire_bus_free(mp_obj_t self);
4547

4648
bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t self);
4749

48-
void common_hal_displayio_fourwire_send(mp_obj_t self, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length);
50+
void common_hal_displayio_fourwire_send(mp_obj_t self, display_byte_type_t byte_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length);
4951

5052
void common_hal_displayio_fourwire_end_transaction(mp_obj_t self);
5153

shared-bindings/displayio/I2CDisplay.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ STATIC mp_obj_t displayio_i2cdisplay_obj_send(mp_obj_t self, mp_obj_t command_ob
131131
uint8_t full_command[bufinfo.len + 1];
132132
full_command[0] = command;
133133
memcpy(full_command + 1, ((uint8_t*) bufinfo.buf), bufinfo.len);
134-
common_hal_displayio_i2cdisplay_send(self, true, false, full_command, bufinfo.len + 1);
134+
common_hal_displayio_i2cdisplay_send(self, DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, full_command, bufinfo.len + 1);
135135
common_hal_displayio_i2cdisplay_end_transaction(self);
136136

137137
return mp_const_none;

shared-bindings/displayio/I2CDisplay.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_I2CDISPLAY_H
2929

3030
#include "shared-module/displayio/I2CDisplay.h"
31+
32+
#include "shared-bindings/displayio/__init__.h"
3133
#include "common-hal/microcontroller/Pin.h"
3234

3335
extern const mp_obj_type_t displayio_i2cdisplay_type;
@@ -42,7 +44,7 @@ bool common_hal_displayio_i2cdisplay_bus_free(mp_obj_t self);
4244

4345
bool common_hal_displayio_i2cdisplay_begin_transaction(mp_obj_t self);
4446

45-
void common_hal_displayio_i2cdisplay_send(mp_obj_t self, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length);
47+
void common_hal_displayio_i2cdisplay_send(mp_obj_t self, display_byte_type_t byte_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length);
4648

4749
void common_hal_displayio_i2cdisplay_end_transaction(mp_obj_t self);
4850

shared-bindings/displayio/ParallelBus.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,8 +139,8 @@ STATIC mp_obj_t displayio_parallelbus_obj_send(mp_obj_t self, mp_obj_t command_o
139139
while (!common_hal_displayio_parallelbus_begin_transaction(self)) {
140140
RUN_BACKGROUND_TASKS;
141141
}
142-
common_hal_displayio_parallelbus_send(self, true, false, &command, 1);
143-
common_hal_displayio_parallelbus_send(self, false, false, ((uint8_t*) bufinfo.buf), bufinfo.len);
142+
common_hal_displayio_parallelbus_send(self, DISPLAY_COMMAND, CHIP_SELECT_UNTOUCHED, &command, 1);
143+
common_hal_displayio_parallelbus_send(self, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, ((uint8_t*) bufinfo.buf), bufinfo.len);
144144
common_hal_displayio_parallelbus_end_transaction(self);
145145

146146
return mp_const_none;

shared-bindings/displayio/ParallelBus.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,9 @@
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_PARALLELBUS_H
2929

3030
#include "common-hal/displayio/ParallelBus.h"
31-
#include "common-hal/microcontroller/Pin.h"
3231

32+
#include "common-hal/microcontroller/Pin.h"
33+
#include "shared-bindings/displayio/__init__.h"
3334
#include "shared-module/displayio/Group.h"
3435

3536
extern const mp_obj_type_t displayio_parallelbus_type;
@@ -45,7 +46,7 @@ bool common_hal_displayio_parallelbus_bus_free(mp_obj_t self);
4546

4647
bool common_hal_displayio_parallelbus_begin_transaction(mp_obj_t self);
4748

48-
void common_hal_displayio_parallelbus_send(mp_obj_t self, bool command, bool toggle_every_byte, uint8_t *data, uint32_t data_length);
49+
void common_hal_displayio_parallelbus_send(mp_obj_t self, display_byte_type_t byte_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length);
4950

5051
void common_hal_displayio_parallelbus_end_transaction(mp_obj_t self);
5152

shared-bindings/displayio/__init__.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,25 @@
2727
#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
2828
#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H
2929

30+
#include "py/obj.h"
31+
32+
typedef enum {
33+
DISPLAY_COMMAND,
34+
DISPLAY_DATA
35+
} display_byte_type_t;
36+
37+
38+
typedef enum {
39+
CHIP_SELECT_UNTOUCHED,
40+
CHIP_SELECT_TOGGLE_EVERY_BYTE
41+
} display_chip_select_behavior_t;
42+
43+
typedef bool (*display_bus_bus_reset)(mp_obj_t bus);
44+
typedef bool (*display_bus_bus_free)(mp_obj_t bus);
45+
typedef bool (*display_bus_begin_transaction)(mp_obj_t bus);
46+
typedef void (*display_bus_send)(mp_obj_t bus, display_byte_type_t byte_type, display_chip_select_behavior_t chip_select, uint8_t *data, uint32_t data_length);
47+
typedef void (*display_bus_end_transaction)(mp_obj_t bus);
48+
3049
void common_hal_displayio_release_displays(void);
3150

3251
#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO___INIT___H

shared-module/_stage/__init__.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
5757
index += 1;
5858
// The buffer is full, send it.
5959
if (index >= buffer_size) {
60-
display->core.send(display->core.bus, false, false, ((uint8_t*)buffer),
60+
display->core.send(display->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, ((uint8_t*)buffer),
6161
buffer_size * 2);
6262
index = 0;
6363
}
@@ -67,6 +67,6 @@ void render_stage(uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1,
6767
}
6868
// Send the remaining data.
6969
if (index) {
70-
display->core.send(display->core.bus, false, false, ((uint8_t*)buffer), index * 2);
70+
display->core.send(display->core.bus, DISPLAY_DATA, CHIP_SELECT_UNTOUCHED, ((uint8_t*)buffer), index * 2);
7171
}
7272
}

shared-module/displayio/ColorConverter.c

Lines changed: 6 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626

2727
#include "shared-bindings/displayio/ColorConverter.h"
2828

29+
#include "py/misc.h"
30+
2931
void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self) {
3032
}
3133

@@ -45,49 +47,21 @@ uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888) {
4547
return (r8 * 19) / 255 + (g8 * 182) / 255 + (b8 + 54) / 255;
4648
}
4749

48-
void compute_bounds(uint8_t r8, uint8_t g8, uint8_t b8, uint8_t* min, uint8_t* max) {
49-
if (r8 > g8) {
50-
if (b8 > r8) {
51-
*max = b8;
52-
} else {
53-
*max = r8;
54-
}
55-
if (b8 < g8) {
56-
*min = b8;
57-
} else {
58-
*min = g8;
59-
}
60-
} else {
61-
if (b8 > g8) {
62-
*max = b8;
63-
} else {
64-
*max = g8;
65-
}
66-
if (b8 < r8) {
67-
*min = b8;
68-
} else {
69-
*min = r8;
70-
}
71-
}
72-
}
73-
7450
uint8_t displayio_colorconverter_compute_chroma(uint32_t color_rgb888) {
7551
uint32_t r8 = (color_rgb888 >> 16);
7652
uint32_t g8 = (color_rgb888 >> 8) & 0xff;
7753
uint32_t b8 = color_rgb888 & 0xff;
78-
uint8_t max;
79-
uint8_t min;
80-
compute_bounds(r8, g8, b8, &min, &max);
54+
uint8_t max = MAX(r8, MAX(g8, b8));
55+
uint8_t min = MIN(r8, MIN(g8, b8));
8156
return max - min;
8257
}
8358

8459
uint8_t displayio_colorconverter_compute_hue(uint32_t color_rgb888) {
8560
uint32_t r8 = (color_rgb888 >> 16);
8661
uint32_t g8 = (color_rgb888 >> 8) & 0xff;
8762
uint32_t b8 = color_rgb888 & 0xff;
88-
uint8_t max;
89-
uint8_t min;
90-
compute_bounds(r8, g8, b8, &min, &max);
63+
uint8_t max = MAX(r8, MAX(g8, b8));
64+
uint8_t min = MIN(r8, MIN(g8, b8));
9165
uint8_t c = max - min;
9266
if (c == 0) {
9367
return 0;

0 commit comments

Comments
 (0)