From 9de46f3edd9bd6a827ba92ef4978143b109e912c Mon Sep 17 00:00:00 2001 From: Carlos Date: Tue, 4 Jun 2019 21:20:43 -0500 Subject: [supervisor/shared/rgb_led_status.c] Initial support for RGB led as Status --- supervisor/shared/rgb_led_status.c | 96 +++++++++++++++++++++++++++++++++++--- 1 file changed, 89 insertions(+), 7 deletions(-) (limited to 'supervisor') diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index 209eef55e..2d7bb067e 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -50,10 +50,28 @@ busio_spi_obj_t status_apa102; #endif #endif -#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) +#if defined(CP_RGB_STATUS_R) || defined(CP_RGB_STATUS_G) || defined(CP_RGB_STATUS_B) +#define CP_RGB_STATUS_LED + +#include "shared-bindings/pulseio/PWMOut.h" +#include "shared-bindings/microcontroller/Pin.h" + +pulseio_pwmout_obj_t rgb_status_r; +pulseio_pwmout_obj_t rgb_status_g; +pulseio_pwmout_obj_t rgb_status_b; + +uint8_t rgb_status_brightness = 0xFF; + +uint16_t status_rgb_color[3] = { + 0 /* red */, 0 /* green */, 0 /* blue */ +}; +#endif + +#if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) static uint32_t current_status_color = 0; #endif + void rgb_led_status_init() { #ifdef MICROPY_HW_NEOPIXEL common_hal_digitalio_digitalinout_construct(&status_neopixel, MICROPY_HW_NEOPIXEL); @@ -93,7 +111,34 @@ void rgb_led_status_init() { #endif #endif - #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) + + #if defined(CP_RGB_STATUS_LED) + if (common_hal_mcu_pin_is_free(CP_RGB_STATUS_R)) { + pwmout_result_t red_result = common_hal_pulseio_pwmout_construct(&rgb_status_r, CP_RGB_STATUS_R, 0, 50000, false); + + if (PWMOUT_OK == red_result) { + common_hal_pulseio_pwmout_never_reset(&rgb_status_r); + } + } + + if (common_hal_mcu_pin_is_free(CP_RGB_STATUS_G)) { + pwmout_result_t green_result = common_hal_pulseio_pwmout_construct(&rgb_status_g, CP_RGB_STATUS_G, 0, 50000, false); + + if (PWMOUT_OK == green_result) { + common_hal_pulseio_pwmout_never_reset(&rgb_status_g); + } + } + + if (common_hal_mcu_pin_is_free(CP_RGB_STATUS_B)) { + pwmout_result_t blue_result = common_hal_pulseio_pwmout_construct(&rgb_status_b, CP_RGB_STATUS_B, 0, 50000, false); + + if (PWMOUT_OK == blue_result) { + common_hal_pulseio_pwmout_never_reset(&rgb_status_b); + } + } + #endif + + #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) // Force a write of the current status color. uint32_t rgb = current_status_color; current_status_color = 0x1000000; // Not a valid color @@ -109,10 +154,15 @@ void reset_status_led() { reset_pin_number(MICROPY_HW_APA102_MOSI->number); reset_pin_number(MICROPY_HW_APA102_SCK->number); #endif + #if defined(CP_RGB_STATUS_LED) + reset_pin_number(CP_RGB_STATUS_R->number); + reset_pin_number(CP_RGB_STATUS_G->number); + reset_pin_number(CP_RGB_STATUS_B->number); + #endif } void new_status_color(uint32_t rgb) { - #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) + #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) if (current_status_color == rgb) { return; } @@ -143,10 +193,24 @@ void new_status_color(uint32_t rgb) { common_hal_busio_spi_write(&status_apa102, status_apa102_color, 8); #endif #endif + + #if defined(CP_RGB_STATUS_LED) + uint8_t red_u8 = (rgb_adjusted >> 16) & 0xFF; + uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF; + uint8_t blue_u8 = rgb_adjusted & 0xFF; + + status_rgb_color[0] = (uint16_t) (red_u8 << 8) + red_u8; + status_rgb_color[1] = (uint16_t) (green_u8 << 8) + green_u8; + status_rgb_color[2] = (uint16_t) (blue_u8 << 8) + blue_u8; + + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, status_rgb_color[2]); + #endif } void temp_status_color(uint32_t rgb) { - #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) + #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) uint32_t rgb_adjusted = rgb; rgb_adjusted = color_brightness(rgb, rgb_status_brightness); #endif @@ -168,6 +232,19 @@ void temp_status_color(uint32_t rgb) { common_hal_busio_spi_write(&status_apa102, colors, 12); #endif #endif + #if defined(CP_RGB_STATUS_LED) + uint8_t red_u8 = (rgb_adjusted >> 16) & 0xFF; + uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF; + uint8_t blue_u8 = rgb_adjusted & 0xFF; + + status_rgb_color[0] = (uint16_t) (red_u8 << 8) + red_u8; + status_rgb_color[1] = (uint16_t) (green_u8 << 8) + green_u8; + status_rgb_color[2] = (uint16_t) (blue_u8 << 8) + blue_u8; + + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, status_rgb_color[2]); + #endif } void clear_temp_status() { @@ -181,6 +258,11 @@ void clear_temp_status() { common_hal_busio_spi_write(&status_apa102, status_apa102_color, 8); #endif #endif + #if defined(CP_RGB_STATUS_LED) + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, status_rgb_color[2]); + #endif } uint32_t color_brightness(uint32_t color, uint8_t brightness) { @@ -210,7 +292,7 @@ void prep_rgb_status_animation(const pyexec_result_t* result, bool found_main, safe_mode_t safe_mode, rgb_status_animation_t* status) { - #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) + #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) new_status_color(ALL_DONE); status->pattern_start = ticks_ms; status->safe_mode = safe_mode; @@ -256,7 +338,7 @@ void prep_rgb_status_animation(const pyexec_result_t* result, } void tick_rgb_status_animation(rgb_status_animation_t* status) { - #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) + #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) uint32_t tick_diff = ticks_ms - status->pattern_start; if (status->ok) { // All is good. Ramp ALL_DONE up and down. @@ -326,4 +408,4 @@ void tick_rgb_status_animation(rgb_status_animation_t* status) { } } #endif -} +} \ No newline at end of file -- cgit v1.2.3 From 2004be96acf1a29fd312362f84dbf86461f0500f Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Wed, 26 Jun 2019 17:36:30 -0700 Subject: Properly reset the terminal each init. Fixes #1944 --- supervisor/shared/display.c | 2 ++ 1 file changed, 2 insertions(+) (limited to 'supervisor') diff --git a/supervisor/shared/display.c b/supervisor/shared/display.c index c3afb3e00..414f85034 100644 --- a/supervisor/shared/display.c +++ b/supervisor/shared/display.c @@ -71,6 +71,8 @@ void supervisor_start_terminal(uint16_t width_px, uint16_t height_px) { if (tiles == NULL) { return; } + grid->y = 0; + grid->top_left_y = 0; if (remaining_pixels > 0) { grid->y -= (grid->tile_height - remaining_pixels); } -- cgit v1.2.3 From 5610e05b8c19c3892dfe9bfced719307a95f365e Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Sat, 6 Jul 2019 12:48:16 -0700 Subject: Fix up nrf so that it is initialized properly. Also, do not reset it's pins. --- ports/nrf/boards/particle_xenon/mpconfigboard.h | 6 +-- ports/nrf/common-hal/pulseio/PWMOut.c | 55 ++++++++++++++----------- supervisor/shared/rgb_led_status.c | 23 +++++------ 3 files changed, 44 insertions(+), 40 deletions(-) (limited to 'supervisor') diff --git a/ports/nrf/boards/particle_xenon/mpconfigboard.h b/ports/nrf/boards/particle_xenon/mpconfigboard.h index 41551d2e1..c8da66736 100644 --- a/ports/nrf/boards/particle_xenon/mpconfigboard.h +++ b/ports/nrf/boards/particle_xenon/mpconfigboard.h @@ -35,9 +35,9 @@ #define MICROPY_HW_LED_STATUS (&pin_P1_12) -#define MICROPY_HW_RGB_LED_RED (&pin_P0_13) -#define MICROPY_HW_RGB_LED_GREEN (&pin_P0_14) -#define MICROPY_HW_RGB_LED_BLUE (&pin_P0_15) +#define CP_RGB_STATUS_R (&pin_P0_13) +#define CP_RGB_STATUS_G (&pin_P0_14) +#define CP_RGB_STATUS_B (&pin_P0_15) #if QSPI_FLASH_FILESYSTEM #define MICROPY_QSPI_DATA0 NRF_GPIO_PIN_MAP(0, 20) diff --git a/ports/nrf/common-hal/pulseio/PWMOut.c b/ports/nrf/common-hal/pulseio/PWMOut.c index 5485a75d0..5233c9adc 100644 --- a/ports/nrf/common-hal/pulseio/PWMOut.c +++ b/ports/nrf/common-hal/pulseio/PWMOut.c @@ -77,33 +77,37 @@ void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { } } +void reset_single_pwmout(uint8_t i) { + NRF_PWM_Type* pwm = pwms[i]; + + pwm->ENABLE = 0; + pwm->MODE = PWM_MODE_UPDOWN_Up; + pwm->DECODER = PWM_DECODER_LOAD_Individual; + pwm->LOOP = 0; + pwm->PRESCALER = PWM_PRESCALER_PRESCALER_DIV_1; // default is 500 hz + pwm->COUNTERTOP = (PWM_MAX_FREQ/500); // default is 500 hz + + pwm->SEQ[0].PTR = (uint32_t) pwm_seq[i]; + pwm->SEQ[0].CNT = CHANNELS_PER_PWM; // default mode is Individual --> count must be 4 + pwm->SEQ[0].REFRESH = 0; + pwm->SEQ[0].ENDDELAY = 0; + + pwm->SEQ[1].PTR = 0; + pwm->SEQ[1].CNT = 0; + pwm->SEQ[1].REFRESH = 0; + pwm->SEQ[1].ENDDELAY = 0; + + for(int ch =0; ch < CHANNELS_PER_PWM; ch++) { + pwm_seq[i][ch] = (1 << 15); // polarity = 0 + } +} + void pwmout_reset(void) { for(size_t i=0; i < MP_ARRAY_SIZE(pwms); i++) { if (never_reset_pwm[i] > 0) { continue; } - NRF_PWM_Type* pwm = pwms[i]; - - pwm->ENABLE = 0; - pwm->MODE = PWM_MODE_UPDOWN_Up; - pwm->DECODER = PWM_DECODER_LOAD_Individual; - pwm->LOOP = 0; - pwm->PRESCALER = PWM_PRESCALER_PRESCALER_DIV_1; // default is 500 hz - pwm->COUNTERTOP = (PWM_MAX_FREQ/500); // default is 500 hz - - pwm->SEQ[0].PTR = (uint32_t) pwm_seq[i]; - pwm->SEQ[0].CNT = CHANNELS_PER_PWM; // default mode is Individual --> count must be 4 - pwm->SEQ[0].REFRESH = 0; - pwm->SEQ[0].ENDDELAY = 0; - - pwm->SEQ[1].PTR = 0; - pwm->SEQ[1].CNT = 0; - pwm->SEQ[1].REFRESH = 0; - pwm->SEQ[1].ENDDELAY = 0; - - for(int ch =0; ch < CHANNELS_PER_PWM; ch++) { - pwm_seq[i][ch] = (1 << 15); // polarity = 0 - } + reset_single_pwmout(i); } } @@ -148,9 +152,9 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, self->channel = CHANNELS_PER_PWM; // out-of-range value. bool pwm_already_in_use; NRF_PWM_Type* pwm; - - for (size_t i = 0 ; i < MP_ARRAY_SIZE(pwms); i++) { - pwm = pwms[i]; + size_t pwm_index = 0; + for (; pwm_index < MP_ARRAY_SIZE(pwms); pwm_index++) { + pwm = pwms[pwm_index]; pwm_already_in_use = pwm->ENABLE & SPIM_ENABLE_ENABLE_Msk; if (pwm_already_in_use) { if (variable_frequency) { @@ -199,6 +203,7 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, nrf_pwm_disable(pwm); if (!pwm_already_in_use) { + reset_single_pwmout(pwm_index); nrf_pwm_configure(pwm, base_clock, NRF_PWM_MODE_UP, countertop); } diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index 2d7bb067e..3146cc686 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -155,9 +155,7 @@ void reset_status_led() { reset_pin_number(MICROPY_HW_APA102_SCK->number); #endif #if defined(CP_RGB_STATUS_LED) - reset_pin_number(CP_RGB_STATUS_R->number); - reset_pin_number(CP_RGB_STATUS_G->number); - reset_pin_number(CP_RGB_STATUS_B->number); + // TODO: Support sharing status LED with user. #endif } @@ -199,9 +197,9 @@ void new_status_color(uint32_t rgb) { uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF; uint8_t blue_u8 = rgb_adjusted & 0xFF; - status_rgb_color[0] = (uint16_t) (red_u8 << 8) + red_u8; - status_rgb_color[1] = (uint16_t) (green_u8 << 8) + green_u8; - status_rgb_color[2] = (uint16_t) (blue_u8 << 8) + blue_u8; + status_rgb_color[0] = (1<<16) - 1 - ((uint16_t) (red_u8 << 8) + red_u8); + status_rgb_color[1] = (1<<16) - 1 - ((uint16_t) (green_u8 << 8) + green_u8); + status_rgb_color[2] = (1<<16) - 1 - ((uint16_t) (blue_u8 << 8) + blue_u8); common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]); common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]); @@ -237,13 +235,14 @@ void temp_status_color(uint32_t rgb) { uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF; uint8_t blue_u8 = rgb_adjusted & 0xFF; - status_rgb_color[0] = (uint16_t) (red_u8 << 8) + red_u8; - status_rgb_color[1] = (uint16_t) (green_u8 << 8) + green_u8; - status_rgb_color[2] = (uint16_t) (blue_u8 << 8) + blue_u8; + uint16_t temp_status_color_rgb[3]; + temp_status_color_rgb[0] = (uint16_t) (red_u8 << 8) + red_u8; + temp_status_color_rgb[1] = (uint16_t) (green_u8 << 8) + green_u8; + temp_status_color_rgb[2] = (uint16_t) (blue_u8 << 8) + blue_u8; - common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]); - common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]); - common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, status_rgb_color[2]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, temp_status_color_rgb[0]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, temp_status_color_rgb[1]); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, temp_status_color_rgb[2]); #endif } -- cgit v1.2.3 From fae1039401fe2eef70d21ae2148e8eeeec0560bd Mon Sep 17 00:00:00 2001 From: C47D Date: Sat, 6 Jul 2019 16:50:48 -0500 Subject: [rgb led status] Add CP_RGB_STATUS_PWM_INVERTED symbol to invert pwm signals. --- supervisor/shared/rgb_led_status.c | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) (limited to 'supervisor') diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index 3146cc686..2e1b8260b 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -196,11 +196,17 @@ void new_status_color(uint32_t rgb) { uint8_t red_u8 = (rgb_adjusted >> 16) & 0xFF; uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF; uint8_t blue_u8 = rgb_adjusted & 0xFF; - - status_rgb_color[0] = (1<<16) - 1 - ((uint16_t) (red_u8 << 8) + red_u8); - status_rgb_color[1] = (1<<16) - 1 - ((uint16_t) (green_u8 << 8) + green_u8); - status_rgb_color[2] = (1<<16) - 1 - ((uint16_t) (blue_u8 << 8) + blue_u8); - + + #if defined(CP_RGB_STATUS_INVERTED_PWM) + status_rgb_color[0] = (1 << 16) - 1 - ((uint16_t) (red_u8 << 8) + red_u8); + status_rgb_color[1] = (1 << 16) - 1 - ((uint16_t) (green_u8 << 8) + green_u8); + status_rgb_color[2] = (1 << 16) - 1 - ((uint16_t) (blue_u8 << 8) + blue_u8); + #else + status_rgb_color[0] = (uint16_t) (red_u8 << 8) + red_u8; + status_rgb_color[1] = (uint16_t) (green_u8 << 8) + green_u8; + status_rgb_color[2] = (uint16_t) (blue_u8 << 8) + blue_u8; + #endif + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]); common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]); common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, status_rgb_color[2]); @@ -235,11 +241,18 @@ void temp_status_color(uint32_t rgb) { uint8_t green_u8 = (rgb_adjusted >> 8) & 0xFF; uint8_t blue_u8 = rgb_adjusted & 0xFF; - uint16_t temp_status_color_rgb[3]; + uint16_t temp_status_color_rgb[3] = {0}; + + #if defined(CP_RGB_STATUS_INVERTED_PWM) + temp_status_color_rgb[0] = (1 << 16) - 1 - ((uint16_t) (red_u8 << 8) + red_u8); + temp_status_color_rgb[1] = (1 << 16) - 1 - ((uint16_t) (green_u8 << 8) + green_u8); + temp_status_color_rgb[2] = (1 << 16) - 1 - ((uint16_t) (blue_u8 << 8) + blue_u8); + #else temp_status_color_rgb[0] = (uint16_t) (red_u8 << 8) + red_u8; temp_status_color_rgb[1] = (uint16_t) (green_u8 << 8) + green_u8; temp_status_color_rgb[2] = (uint16_t) (blue_u8 << 8) + blue_u8; - + #endif + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, temp_status_color_rgb[0]); common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, temp_status_color_rgb[1]); common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, temp_status_color_rgb[2]); @@ -407,4 +420,4 @@ void tick_rgb_status_animation(rgb_status_animation_t* status) { } } #endif -} \ No newline at end of file +} -- cgit v1.2.3 From 324cbe432753297f8de6c512e2186b5075c050d0 Mon Sep 17 00:00:00 2001 From: C47D Date: Sun, 7 Jul 2019 21:15:51 -0500 Subject: [rgb status] Also invert the pwm values on clear_temp_status --- supervisor/shared/rgb_led_status.c | 21 ++++++++++++++++++--- 1 file changed, 18 insertions(+), 3 deletions(-) (limited to 'supervisor') diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index 2e1b8260b..7ca92c677 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -271,9 +271,24 @@ void clear_temp_status() { #endif #endif #if defined(CP_RGB_STATUS_LED) - common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, status_rgb_color[0]); - common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, status_rgb_color[1]); - common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, status_rgb_color[2]); + + uint16_t red = 0; + uint16_t green = 0; + uint16_t blue = 0; + + #if defined(CP_RGB_STATUS_INVERTED_PWM) + red = (1 << 16) - 1 - status_rgb_color[0]; + green = (1 << 16) - 1 - status_rgb_color[1]; + blue = (1 << 16) - 1 - status_rgb_color[2]; + #else + red = status_rgb_color[0]; + green = status_rgb_color[1]; + blue = status_rgb_color[2]; + #endif + + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_r, red); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_g, green); + common_hal_pulseio_pwmout_set_duty_cycle(&rgb_status_b, blue); #endif } -- cgit v1.2.3 From 40fbac13cdf3743d2a9f218fa067399a88ad164a Mon Sep 17 00:00:00 2001 From: C47D Date: Mon, 8 Jul 2019 22:06:43 -0500 Subject: [rgb status] Adjust to current brightness --- supervisor/shared/rgb_led_status.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'supervisor') diff --git a/supervisor/shared/rgb_led_status.c b/supervisor/shared/rgb_led_status.c index 7ca92c677..0e69cd2e0 100644 --- a/supervisor/shared/rgb_led_status.c +++ b/supervisor/shared/rgb_led_status.c @@ -293,7 +293,7 @@ void clear_temp_status() { } uint32_t color_brightness(uint32_t color, uint8_t brightness) { - #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) + #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) uint32_t result = ((color & 0xff0000) * brightness / 255) & 0xff0000; result += ((color & 0xff00) * brightness / 255) & 0xff00; result += ((color & 0xff) * brightness / 255) & 0xff; @@ -304,7 +304,7 @@ uint32_t color_brightness(uint32_t color, uint8_t brightness) { } void set_rgb_status_brightness(uint8_t level){ - #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) + #if defined(MICROPY_HW_NEOPIXEL) || (defined(MICROPY_HW_APA102_MOSI) && defined(MICROPY_HW_APA102_SCK)) || (defined(CP_RGB_STATUS_LED)) rgb_status_brightness = level; uint32_t current_color = current_status_color; // Temporarily change the current color global to force the new_status_color call to update the -- cgit v1.2.3 From 6797ec6ed396f65916fe816f8b2b49114253dd86 Mon Sep 17 00:00:00 2001 From: Scott Shawcroft Date: Fri, 5 Jul 2019 19:01:54 -0700 Subject: Add support for grayscale displays that are < 8 bit depth. This also improves Palette so it stores the original RGB888 colors. Lastly, it adds I2CDisplay as a display bus to talk over I2C. Particularly useful for the SSD1306. Fixes #1828. Fixes #1956 --- .../atmel-samd/boards/hallowing_m0_express/board.c | 3 + ports/atmel-samd/boards/pybadge/board.c | 3 + ports/atmel-samd/boards/pybadge_airlift/board.c | 3 + ports/atmel-samd/boards/pygamer/board.c | 3 + ports/atmel-samd/boards/pygamer_advance/board.c | 3 + ports/atmel-samd/boards/pyportal/board.c | 5 +- ports/atmel-samd/boards/ugame10/board.c | 3 + ports/atmel-samd/common-hal/busio/I2C.c | 9 ++ ports/atmel-samd/common-hal/busio/SPI.h | 1 + .../atmel-samd/common-hal/displayio/ParallelBus.c | 17 +- ports/nrf/common-hal/busio/I2C.c | 17 ++ ports/nrf/common-hal/busio/SPI.c | 2 +- py/circuitpy_defns.mk | 1 + shared-bindings/_stage/__init__.c | 7 +- shared-bindings/busio/I2C.h | 3 + shared-bindings/displayio/ColorConverter.c | 6 +- shared-bindings/displayio/ColorConverter.h | 4 +- shared-bindings/displayio/Display.c | 16 +- shared-bindings/displayio/Display.h | 6 +- shared-bindings/displayio/I2CDisplay.c | 138 ++++++++++++++++ shared-bindings/displayio/I2CDisplay.h | 46 ++++++ shared-bindings/displayio/Palette.c | 27 +++- shared-bindings/displayio/Palette.h | 2 + shared-bindings/displayio/__init__.c | 3 + shared-module/displayio/ColorConverter.c | 35 ++++- shared-module/displayio/ColorConverter.h | 4 + shared-module/displayio/Display.c | 173 +++++++++++++++------ shared-module/displayio/Display.h | 4 +- shared-module/displayio/FourWire.c | 21 ++- shared-module/displayio/FourWire.h | 6 +- shared-module/displayio/Group.c | 6 +- shared-module/displayio/Group.h | 3 +- shared-module/displayio/I2CDisplay.c | 105 +++++++++++++ shared-module/displayio/I2CDisplay.h | 41 +++++ shared-module/displayio/Palette.c | 50 +++--- shared-module/displayio/Palette.h | 19 ++- shared-module/displayio/TileGrid.c | 50 ++++-- shared-module/displayio/TileGrid.h | 3 +- shared-module/displayio/__init__.c | 135 ++++++++++------ shared-module/displayio/__init__.h | 2 + shared-module/terminalio/Terminal.c | 2 + supervisor/shared/display.c | 49 ++++-- tools/gen_display_resources.py | 17 +- 43 files changed, 855 insertions(+), 198 deletions(-) create mode 100644 shared-bindings/displayio/I2CDisplay.c create mode 100644 shared-bindings/displayio/I2CDisplay.h create mode 100644 shared-module/displayio/I2CDisplay.c create mode 100644 shared-module/displayio/I2CDisplay.h (limited to 'supervisor') diff --git a/ports/atmel-samd/boards/hallowing_m0_express/board.c b/ports/atmel-samd/boards/hallowing_m0_express/board.c index a0e160757..1bbbfbaf8 100644 --- a/ports/atmel-samd/boards/hallowing_m0_express/board.c +++ b/ports/atmel-samd/boards/hallowing_m0_express/board.c @@ -91,6 +91,8 @@ void board_init(void) { 1, // row start 0, // rotation 16, // Color depth + false, // Grayscale + false, // Pixels in a byte share a row. Only used for depth < 8 MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command @@ -98,6 +100,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA00, + 0x100, // Brightness command. Only available when <= 0xff 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pybadge/board.c b/ports/atmel-samd/boards/pybadge/board.c index 38902315a..7e6c11dd4 100644 --- a/ports/atmel-samd/boards/pybadge/board.c +++ b/ports/atmel-samd/boards/pybadge/board.c @@ -93,6 +93,8 @@ void board_init(void) { 0, // row start 270, // rotation 16, // Color depth + false, // grayscale + false, // pixels in byte share row. only used for depth < 8 MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command @@ -100,6 +102,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin + 0x100, // no brightness command 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pybadge_airlift/board.c b/ports/atmel-samd/boards/pybadge_airlift/board.c index a1f778df1..d7f291d7d 100644 --- a/ports/atmel-samd/boards/pybadge_airlift/board.c +++ b/ports/atmel-samd/boards/pybadge_airlift/board.c @@ -71,6 +71,8 @@ void board_init(void) { 0, // row start 90, // rotation 16, // Color depth + false, // grayscale + false, // pixels in byte share row. Only used for depth < 8 MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command @@ -78,6 +80,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin + 0x100, // brightness command, only valid <= 0xff 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pygamer/board.c b/ports/atmel-samd/boards/pygamer/board.c index 7b54b6fb5..e59b7c458 100644 --- a/ports/atmel-samd/boards/pygamer/board.c +++ b/ports/atmel-samd/boards/pygamer/board.c @@ -93,6 +93,8 @@ void board_init(void) { 0, // row start 270, // rotation 16, // Color depth + false, // Grayscale + false, // pixels in a byte share a row. Only valid for depths < 8 MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command @@ -100,6 +102,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin + 0x100, // Brightness command. Only available when < 0xff 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pygamer_advance/board.c b/ports/atmel-samd/boards/pygamer_advance/board.c index d1c5edc99..b23353998 100644 --- a/ports/atmel-samd/boards/pygamer_advance/board.c +++ b/ports/atmel-samd/boards/pygamer_advance/board.c @@ -71,6 +71,8 @@ void board_init(void) { 0, // row start 90, // rotation 16, // Color depth + false, // Grayscale + false, // pixels in a byte share a row. Only valid for depths < 8 MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command @@ -78,6 +80,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), &pin_PA01, // backlight pin + 0x100, // Brightness command. Only available when < 0xff 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/pyportal/board.c b/ports/atmel-samd/boards/pyportal/board.c index bb80c5b48..7fffb764f 100644 --- a/ports/atmel-samd/boards/pyportal/board.c +++ b/ports/atmel-samd/boards/pyportal/board.c @@ -83,13 +83,16 @@ void board_init(void) { 0, // row start 0, // rotation 16, // Color depth + false, // grayscale + false, // pixels_in_byte_share_row (unused for depths > 8) MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command 0x37, // Set vertical scroll command display_init_sequence, sizeof(display_init_sequence), - &pin_PB31, + &pin_PB31, // Backlight pin + 0x100, // Brightness command > 0xff is none s 1.0f, // brightness (ignored) true, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/boards/ugame10/board.c b/ports/atmel-samd/boards/ugame10/board.c index 828f0c87d..27a03ad91 100644 --- a/ports/atmel-samd/boards/ugame10/board.c +++ b/ports/atmel-samd/boards/ugame10/board.c @@ -91,6 +91,8 @@ void board_init(void) { 2, // row start 0, // rotation 16, // Color depth + false, // grayscale + false, // pixels in byte share row. Only used with depth < 8 MIPI_COMMAND_SET_COLUMN_ADDRESS, // Set column command MIPI_COMMAND_SET_PAGE_ADDRESS, // Set row command MIPI_COMMAND_WRITE_MEMORY_START, // Write memory command @@ -98,6 +100,7 @@ void board_init(void) { display_init_sequence, sizeof(display_init_sequence), NULL, + 0x100, // brightness command. Only valid <=0xff 1.0f, // brightness false, // auto_brightness false, // single_byte_bounds diff --git a/ports/atmel-samd/common-hal/busio/I2C.c b/ports/atmel-samd/common-hal/busio/I2C.c index a0c04d65d..cdc616bbc 100644 --- a/ports/atmel-samd/common-hal/busio/I2C.c +++ b/ports/atmel-samd/common-hal/busio/I2C.c @@ -36,6 +36,8 @@ #include "shared-bindings/microcontroller/__init__.h" #include "supervisor/shared/translate.h" +#include "common-hal/busio/SPI.h" // for never_reset_sercom + // Number of times to try to send packet if failed. #define ATTEMPTS 2 @@ -225,3 +227,10 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, } return MP_EIO; } + +void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) { + never_reset_sercom(self->i2c_desc.device.hw); + + never_reset_pin_number(self->scl_pin); + never_reset_pin_number(self->sda_pin); +} diff --git a/ports/atmel-samd/common-hal/busio/SPI.h b/ports/atmel-samd/common-hal/busio/SPI.h index 56d163a9d..a1c0e1517 100644 --- a/ports/atmel-samd/common-hal/busio/SPI.h +++ b/ports/atmel-samd/common-hal/busio/SPI.h @@ -43,6 +43,7 @@ typedef struct { } busio_spi_obj_t; void reset_sercoms(void); +void never_reset_sercom(Sercom* sercom); #endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_BUSIO_SPI_H diff --git a/ports/atmel-samd/common-hal/displayio/ParallelBus.c b/ports/atmel-samd/common-hal/displayio/ParallelBus.c index 7a5f61448..c370f7e52 100644 --- a/ports/atmel-samd/common-hal/displayio/ParallelBus.c +++ b/ports/atmel-samd/common-hal/displayio/ParallelBus.c @@ -31,6 +31,7 @@ #include "common-hal/microcontroller/Pin.h" #include "py/runtime.h" #include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/microcontroller/__init__.h" #include "tick.h" @@ -66,10 +67,6 @@ void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* sel common_hal_digitalio_digitalinout_construct(&self->chip_select, chip_select); common_hal_digitalio_digitalinout_switch_to_output(&self->chip_select, true, DRIVE_MODE_PUSH_PULL); - self->reset.base.type = &digitalio_digitalinout_type; - common_hal_digitalio_digitalinout_construct(&self->reset, reset); - common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); - self->write.base.type = &digitalio_digitalinout_type; common_hal_digitalio_digitalinout_construct(&self->write, write); common_hal_digitalio_digitalinout_switch_to_output(&self->write, true, DRIVE_MODE_PUSH_PULL); @@ -82,11 +79,21 @@ void common_hal_displayio_parallelbus_construct(displayio_parallelbus_obj_t* sel self->write_group = &PORT->Group[write->number / 32]; self->write_mask = 1 << (write->number % 32); + if (reset != NULL) { + self->reset.base.type = &digitalio_digitalinout_type; + common_hal_digitalio_digitalinout_construct(&self->reset, reset); + common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); + never_reset_pin_number(reset->number); + + common_hal_digitalio_digitalinout_set_value(&self->reset, false); + common_hal_mcu_delay_us(4); + common_hal_digitalio_digitalinout_set_value(&self->reset, true); + } + never_reset_pin_number(command->number); never_reset_pin_number(chip_select->number); never_reset_pin_number(write->number); never_reset_pin_number(read->number); - never_reset_pin_number(reset->number); for (uint8_t i = 0; i < 8; i++) { never_reset_pin_number(data_pin + i); } diff --git a/ports/nrf/common-hal/busio/I2C.c b/ports/nrf/common-hal/busio/I2C.c index 538c87f2d..71835d16a 100644 --- a/ports/nrf/common-hal/busio/I2C.c +++ b/ports/nrf/common-hal/busio/I2C.c @@ -57,13 +57,30 @@ STATIC twim_peripheral_t twim_peripherals[] = { #endif }; +STATIC bool never_reset[MP_ARRAY_SIZE(twim_peripherals)]; + void i2c_reset(void) { for (size_t i = 0 ; i < MP_ARRAY_SIZE(twim_peripherals); i++) { + if (never_reset[i]) { + continue; + } nrf_twim_disable(twim_peripherals[i].twim.p_twim); twim_peripherals[i].in_use = false; } } +void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) { + for (size_t i = 0 ; i < MP_ARRAY_SIZE(twim_peripherals); i++) { + if (self->twim_peripheral == &twim_peripherals[i]) { + never_reset[i] = true; + + never_reset_pin_number(self->scl_pin_number); + never_reset_pin_number(self->sda_pin_number); + break; + } + } +} + static uint8_t twi_error_to_mp(const nrfx_err_t err) { switch (err) { case NRFX_ERROR_DRV_TWI_ERR_ANACK: diff --git a/ports/nrf/common-hal/busio/SPI.c b/ports/nrf/common-hal/busio/SPI.c index 6ab6d9493..5f1aac193 100644 --- a/ports/nrf/common-hal/busio/SPI.c +++ b/ports/nrf/common-hal/busio/SPI.c @@ -61,7 +61,7 @@ STATIC spim_peripheral_t spim_peripherals[] = { #endif }; -STATIC bool never_reset[4]; +STATIC bool never_reset[MP_ARRAY_SIZE(spim_peripherals)]; void spi_reset(void) { for (size_t i = 0 ; i < MP_ARRAY_SIZE(spim_peripherals); i++) { diff --git a/py/circuitpy_defns.mk b/py/circuitpy_defns.mk index 782314fc7..93ca4deb9 100644 --- a/py/circuitpy_defns.mk +++ b/py/circuitpy_defns.mk @@ -317,6 +317,7 @@ $(filter $(SRC_PATTERNS), \ displayio/Display.c \ displayio/FourWire.c \ displayio/Group.c \ + displayio/I2CDisplay.c \ displayio/OnDiskBitmap.c \ displayio/Palette.c \ displayio/Shape.c \ diff --git a/shared-bindings/_stage/__init__.c b/shared-bindings/_stage/__init__.c index 03775b1de..9f3f89462 100644 --- a/shared-bindings/_stage/__init__.c +++ b/shared-bindings/_stage/__init__.c @@ -95,7 +95,12 @@ STATIC mp_obj_t stage_render(size_t n_args, const mp_obj_t *args) { MICROPY_VM_HOOK_LOOP ; #endif } - displayio_display_set_region_to_update(display, x0, y0, x1, y1); + displayio_area_t area; + area.x1 = x0; + area.y1 = y0; + area.x2 = x1; + area.y2 = y1; + displayio_display_set_region_to_update(display, &area); render_stage(x0, y0, x1, y1, layers, layers_size, buffer, buffer_size, display); displayio_display_end_transaction(display); diff --git a/shared-bindings/busio/I2C.h b/shared-bindings/busio/I2C.h index 739732e97..a2d5dbf50 100644 --- a/shared-bindings/busio/I2C.h +++ b/shared-bindings/busio/I2C.h @@ -69,4 +69,7 @@ extern uint8_t common_hal_busio_i2c_write(busio_i2c_obj_t *self, uint16_t addres extern uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t address, uint8_t * data, size_t len); +// This is used by the supervisor to claim I2C devices indefinitely. +extern void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self); + #endif // MICROPY_INCLUDED_SHARED_BINDINGS_BUSIO_I2C_H diff --git a/shared-bindings/displayio/ColorConverter.c b/shared-bindings/displayio/ColorConverter.c index 561883810..e170e0340 100644 --- a/shared-bindings/displayio/ColorConverter.c +++ b/shared-bindings/displayio/ColorConverter.c @@ -69,8 +69,10 @@ STATIC mp_obj_t displayio_colorconverter_obj_convert(mp_obj_t self_in, mp_obj_t if (!mp_obj_get_int_maybe(color_obj, &color)) { mp_raise_ValueError(translate("color should be an int")); } - uint16_t output_color; - common_hal_displayio_colorconverter_convert(self, color, &output_color); + _displayio_colorspace_t colorspace; + colorspace.depth = 16; + uint32_t output_color; + common_hal_displayio_colorconverter_convert(self, &colorspace, color, &output_color); return MP_OBJ_NEW_SMALL_INT(output_color); } MP_DEFINE_CONST_FUN_OBJ_2(displayio_colorconverter_convert_obj, displayio_colorconverter_obj_convert); diff --git a/shared-bindings/displayio/ColorConverter.h b/shared-bindings/displayio/ColorConverter.h index 71be7f543..8f61e6642 100644 --- a/shared-bindings/displayio/ColorConverter.h +++ b/shared-bindings/displayio/ColorConverter.h @@ -29,9 +29,11 @@ #include "shared-module/displayio/ColorConverter.h" +#include "shared-module/displayio/Palette.h" + extern const mp_obj_type_t displayio_colorconverter_type; void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self); -bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *colorconverter, uint32_t input_color, uint16_t* output_color); +bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *colorconverter, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color); #endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYIO_COLORCONVERTER_H diff --git a/shared-bindings/displayio/Display.c b/shared-bindings/displayio/Display.c index 1a36872a6..2586e562f 100644 --- a/shared-bindings/displayio/Display.c +++ b/shared-bindings/displayio/Display.c @@ -51,7 +51,7 @@ //| Most people should not use this class directly. Use a specific display driver instead that will //| contain the initialization sequence at minimum. //| -//| .. class:: Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0, color_depth=16, set_column_command=0x2a, set_row_command=0x2b, write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None, brightness=1.0, auto_brightness=False, single_byte_bounds=False, data_as_commands=False) +//| .. class:: Display(display_bus, init_sequence, *, width, height, colstart=0, rowstart=0, rotation=0, color_depth=16, grayscale=False, pixels_in_byte_share_row=True, set_column_command=0x2a, set_row_command=0x2b, write_ram_command=0x2c, set_vertical_scroll=0, backlight_pin=None, brightness_command=None, brightness=1.0, auto_brightness=False, single_byte_bounds=False, data_as_commands=False) //| //| Create a Display object on the given display bus (`displayio.FourWire` or `displayio.ParallelBus`). //| @@ -88,18 +88,21 @@ //| :param int rotation: The rotation of the display in degrees clockwise. Must be in 90 degree increments (0, 90, 180, 270) //| :param int color_depth: The number of bits of color per pixel transmitted. (Some displays //| support 18 bit but 16 is easier to transmit. The last bit is extrapolated.) +//| :param bool grayscale: True if the display only shows a single color. +//| :param bool pixels_in_byte_share_row: True when pixels are less than a byte and a byte includes pixels from the same row of the display. When False, pixels share a column. //| :param int set_column_command: Command used to set the start and end columns to update //| :param int set_row_command: Command used so set the start and end rows to update -//| :param int write_ram_command: Command used to write pixels values into the update region +//| :param int write_ram_command: Command used to write pixels values into the update region. Ignored if data_as_commands is set. //| :param int set_vertical_scroll: Command used to set the first row to show //| :param microcontroller.Pin backlight_pin: Pin connected to the display's backlight +//| :param int brightness_command: Command to set display brightness. Usually available in OLED controllers. //| :param bool brightness: Initial display brightness. This value is ignored if auto_brightness is True. //| :param bool auto_brightness: If True, brightness is controlled via an ambient light sensor or other mechanism. //| :param bool single_byte_bounds: Display column and row commands use single bytes //| :param bool data_as_commands: Treat all init and boundary data as SPI commands. Certain displays require this. //| STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { - enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_color_depth, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command, ARG_set_vertical_scroll, ARG_backlight_pin, ARG_brightness, ARG_auto_brightness, ARG_single_byte_bounds, ARG_data_as_commands }; + enum { ARG_display_bus, ARG_init_sequence, ARG_width, ARG_height, ARG_colstart, ARG_rowstart, ARG_rotation, ARG_color_depth, ARG_grayscale, ARG_pixels_in_byte_share_row, ARG_set_column_command, ARG_set_row_command, ARG_write_ram_command, ARG_set_vertical_scroll, ARG_backlight_pin, ARG_brightness_command, ARG_brightness, ARG_auto_brightness, ARG_single_byte_bounds, ARG_data_as_commands }; static const mp_arg_t allowed_args[] = { { MP_QSTR_display_bus, MP_ARG_REQUIRED | MP_ARG_OBJ }, { MP_QSTR_init_sequence, MP_ARG_REQUIRED | MP_ARG_OBJ }, @@ -109,11 +112,14 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a { MP_QSTR_rowstart, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, { MP_QSTR_rotation, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0} }, { MP_QSTR_color_depth, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 16} }, + { MP_QSTR_grayscale, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, + { MP_QSTR_pixels_in_byte_share_row, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = true} }, { MP_QSTR_set_column_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2a} }, { MP_QSTR_set_row_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2b} }, { MP_QSTR_write_ram_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x2c} }, { MP_QSTR_set_vertical_scroll, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x0} }, { MP_QSTR_backlight_pin, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, + { MP_QSTR_brightness_command, MP_ARG_INT | MP_ARG_KW_ONLY, {.u_int = 0x100} }, { MP_QSTR_brightness, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = MP_OBJ_NEW_SMALL_INT(1)} }, { MP_QSTR_auto_brightness, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, { MP_QSTR_single_byte_bounds, MP_ARG_BOOL | MP_ARG_KW_ONLY, {.u_bool = false} }, @@ -157,11 +163,13 @@ STATIC mp_obj_t displayio_display_make_new(const mp_obj_type_t *type, size_t n_a common_hal_displayio_display_construct( self, display_bus, args[ARG_width].u_int, args[ARG_height].u_int, args[ARG_colstart].u_int, args[ARG_rowstart].u_int, rotation, - args[ARG_color_depth].u_int, args[ARG_set_column_command].u_int, args[ARG_set_row_command].u_int, + args[ARG_color_depth].u_int, args[ARG_grayscale].u_bool, args[ARG_pixels_in_byte_share_row].u_bool, + args[ARG_set_column_command].u_int, args[ARG_set_row_command].u_int, args[ARG_write_ram_command].u_int, args[ARG_set_vertical_scroll].u_int, bufinfo.buf, bufinfo.len, MP_OBJ_TO_PTR(backlight_pin), + args[ARG_brightness_command].u_int, brightness, args[ARG_auto_brightness].u_bool, args[ARG_single_byte_bounds].u_bool, diff --git a/shared-bindings/displayio/Display.h b/shared-bindings/displayio/Display.h index 5a027561b..a60a0cf5a 100644 --- a/shared-bindings/displayio/Display.h +++ b/shared-bindings/displayio/Display.h @@ -38,9 +38,9 @@ extern const mp_obj_type_t displayio_display_type; void common_hal_displayio_display_construct(displayio_display_obj_t* self, mp_obj_t bus, uint16_t width, uint16_t height, - int16_t colstart, int16_t rowstart, uint16_t rotation, uint16_t color_depth, + int16_t colstart, int16_t rowstart, uint16_t rotation, uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, uint8_t set_column_command, uint8_t set_row_command, uint8_t write_ram_command, uint8_t set_vertical_scroll, - uint8_t* init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t* backlight_pin, + uint8_t* init_sequence, uint16_t init_sequence_len, const mcu_pin_obj_t* backlight_pin, uint16_t brightness_command, mp_float_t brightness, bool auto_brightness, bool single_byte_bounds, bool data_as_commands); @@ -54,7 +54,7 @@ bool displayio_display_begin_transaction(displayio_display_obj_t* self); void displayio_display_end_transaction(displayio_display_obj_t* self); // The second point of the region is exclusive. -void displayio_display_set_region_to_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1); +void displayio_display_set_region_to_update(displayio_display_obj_t* self, displayio_area_t* area); bool displayio_display_frame_queued(displayio_display_obj_t* self); bool displayio_display_refresh_queued(displayio_display_obj_t* self); diff --git a/shared-bindings/displayio/I2CDisplay.c b/shared-bindings/displayio/I2CDisplay.c new file mode 100644 index 000000000..2aac49b4d --- /dev/null +++ b/shared-bindings/displayio/I2CDisplay.c @@ -0,0 +1,138 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/I2CDisplay.h" + +#include +#include + +#include "lib/utils/context_manager_helpers.h" +#include "py/binary.h" +#include "py/objproperty.h" +#include "py/runtime.h" +#include "shared-bindings/microcontroller/Pin.h" +#include "shared-bindings/util.h" +#include "shared-module/displayio/__init__.h" +#include "supervisor/shared/translate.h" + +//| .. currentmodule:: displayio +//| +//| :class:`I2CDisplay` -- Manage updating a display over I2C +//| ========================================================================== +//| +//| Manage updating a display over I2C in the background while Python code runs. +//| It doesn't handle display initialization. +//| +//| .. class:: I2CDisplay(i2c_bus, *, device_address, reset=None) +//| +//| Create a I2CDisplay object associated with the given I2C bus and reset pin. +//| +//| The I2C bus and pins are then in use by the display until `displayio.release_displays()` is +//| called even after a reload. (It does this so CircuitPython can use the display after your code +//| is done.) So, the first time you initialize a display bus in code.py you should call +//| :py:func`displayio.release_displays` first, otherwise it will error after the first code.py run. +//| +//| :param busio.I2C i2c_bus: The I2C bus that make up the clock and data lines +//| :param int device_address: The I2C address of the device +//| :param microcontroller.Pin reset: Reset pin. When None only software reset can be used +//| +STATIC mp_obj_t displayio_i2cdisplay_make_new(const mp_obj_type_t *type, size_t n_args, const mp_obj_t *pos_args, mp_map_t *kw_args) { + enum { ARG_i2c_bus, ARG_device_address, ARG_reset }; + static const mp_arg_t allowed_args[] = { + { MP_QSTR_i2c_bus, MP_ARG_REQUIRED | MP_ARG_OBJ }, + { MP_QSTR_device_address, MP_ARG_INT | MP_ARG_KW_ONLY | MP_ARG_REQUIRED }, + { MP_QSTR_reset, MP_ARG_OBJ | MP_ARG_KW_ONLY, {.u_obj = mp_const_none} }, + }; + 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); + + mp_obj_t reset = args[ARG_reset].u_obj; + if (reset != mp_const_none) { + assert_pin_free(reset); + } else { + reset = NULL; + } + + displayio_i2cdisplay_obj_t* self = NULL; + mp_obj_t i2c = args[ARG_i2c_bus].u_obj; + for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { + if (displays[i].i2cdisplay_bus.base.type == NULL || + displays[i].i2cdisplay_bus.base.type == &mp_type_NoneType) { + self = &displays[i].i2cdisplay_bus; + self->base.type = &displayio_i2cdisplay_type; + break; + } + } + if (self == NULL) { + mp_raise_RuntimeError(translate("Too many display busses")); + } + + common_hal_displayio_i2cdisplay_construct(self, + MP_OBJ_TO_PTR(i2c), args[ARG_device_address].u_int, reset); + return self; +} + +//| .. method:: send(command, data) +//| +//| Sends the given command value followed by the full set of data. Display state, such as +//| vertical scroll, set via ``send`` may or may not be reset once the code is done. +//| +STATIC mp_obj_t displayio_i2cdisplay_obj_send(mp_obj_t self, mp_obj_t command_obj, mp_obj_t data_obj) { + mp_int_t command_int = MP_OBJ_SMALL_INT_VALUE(command_obj); + if (!MP_OBJ_IS_SMALL_INT(command_obj) || command_int > 255 || command_int < 0) { + mp_raise_ValueError(translate("Command must be an int between 0 and 255")); + } + uint8_t command = command_int; + mp_buffer_info_t bufinfo; + mp_get_buffer_raise(data_obj, &bufinfo, MP_BUFFER_READ); + + // Wait for display bus to be available. + while (!common_hal_displayio_i2cdisplay_begin_transaction(self)) { +#ifdef MICROPY_VM_HOOK_LOOP + MICROPY_VM_HOOK_LOOP ; +#endif + } + uint8_t full_command[bufinfo.len + 1]; + full_command[0] = command; + memcpy(full_command + 1, ((uint8_t*) bufinfo.buf), bufinfo.len); + common_hal_displayio_i2cdisplay_send(self, true, full_command, bufinfo.len + 1); + common_hal_displayio_i2cdisplay_end_transaction(self); + + return mp_const_none; +} +MP_DEFINE_CONST_FUN_OBJ_3(displayio_i2cdisplay_send_obj, displayio_i2cdisplay_obj_send); + +STATIC const mp_rom_map_elem_t displayio_i2cdisplay_locals_dict_table[] = { + { MP_ROM_QSTR(MP_QSTR_send), MP_ROM_PTR(&displayio_i2cdisplay_send_obj) }, +}; +STATIC MP_DEFINE_CONST_DICT(displayio_i2cdisplay_locals_dict, displayio_i2cdisplay_locals_dict_table); + +const mp_obj_type_t displayio_i2cdisplay_type = { + { &mp_type_type }, + .name = MP_QSTR_I2CDisplay, + .make_new = displayio_i2cdisplay_make_new, + .locals_dict = (mp_obj_dict_t*)&displayio_i2cdisplay_locals_dict, +}; diff --git a/shared-bindings/displayio/I2CDisplay.h b/shared-bindings/displayio/I2CDisplay.h new file mode 100644 index 000000000..cc4162800 --- /dev/null +++ b/shared-bindings/displayio/I2CDisplay.h @@ -0,0 +1,46 @@ +/* + * This file is part of the Micro Python project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2017, 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_I2CDISPLAY_H +#define MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_I2CDISPLAY_H + +#include "shared-module/displayio/I2CDisplay.h" +#include "common-hal/microcontroller/Pin.h" + +extern const mp_obj_type_t displayio_i2cdisplay_type; + +void common_hal_displayio_i2cdisplay_construct(displayio_i2cdisplay_obj_t* self, + busio_i2c_obj_t* i2c, uint16_t device_address, const mcu_pin_obj_t* reset); + +void common_hal_displayio_i2cdisplay_deinit(displayio_i2cdisplay_obj_t* self); + +bool common_hal_displayio_i2cdisplay_begin_transaction(mp_obj_t self); + +void common_hal_displayio_i2cdisplay_send(mp_obj_t self, bool command, uint8_t *data, uint32_t data_length); + +void common_hal_displayio_i2cdisplay_end_transaction(mp_obj_t self); + +#endif // MICROPY_INCLUDED_SHARED_BINDINGS_DISPLAYBUSIO_I2CDISPLAY_H diff --git a/shared-bindings/displayio/Palette.c b/shared-bindings/displayio/Palette.c index 11c2f677c..974cadb02 100644 --- a/shared-bindings/displayio/Palette.c +++ b/shared-bindings/displayio/Palette.c @@ -66,11 +66,26 @@ STATIC mp_obj_t displayio_palette_make_new(const mp_obj_type_t *type, size_t n_a return MP_OBJ_FROM_PTR(self); } + +//| .. method:: __len__() +//| +//| Returns the number of colors in a Palette +//| +STATIC mp_obj_t group_unary_op(mp_unary_op_t op, mp_obj_t self_in) { + displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); + switch (op) { + case MP_UNARY_OP_BOOL: return mp_obj_new_bool(true); + case MP_UNARY_OP_LEN: + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_palette_get_len(self)); + default: return MP_OBJ_NULL; // op not supported + } +} + //| .. method:: __setitem__(index, value) //| //| Sets the pixel color at the given index. The index should be an integer in the range 0 to color_count-1. //| -//| The value argument represents a color, and can be from 0x000000 to 0xFFFFFF (to represent an RGB value). +//| The value argument represents a color, and can be from 0x000000 to 0xFFFFFF (to represent an RGB value). //| Value can be an int, bytes (3 bytes (RGB) or 4 bytes (RGB + pad byte)), or bytearray. //| //| This allows you to:: @@ -89,12 +104,12 @@ STATIC mp_obj_t palette_subscr(mp_obj_t self_in, mp_obj_t index_in, mp_obj_t val if (MP_OBJ_IS_TYPE(index_in, &mp_type_slice)) { return MP_OBJ_NULL; } - // index read is not supported - if (value == MP_OBJ_SENTINEL) { - return MP_OBJ_NULL; - } displayio_palette_t *self = MP_OBJ_TO_PTR(self_in); size_t index = mp_get_index(&displayio_palette_type, self->color_count, index_in, false); + // index read + if (value == MP_OBJ_SENTINEL) { + return MP_OBJ_NEW_SMALL_INT(common_hal_displayio_palette_get_color(self, index)); + } uint32_t color; mp_int_t int_value; @@ -160,5 +175,7 @@ const mp_obj_type_t displayio_palette_type = { .name = MP_QSTR_Palette, .make_new = displayio_palette_make_new, .subscr = palette_subscr, + .unary_op = group_unary_op, + .getiter = mp_obj_new_generic_iterator, .locals_dict = (mp_obj_dict_t*)&displayio_palette_locals_dict, }; diff --git a/shared-bindings/displayio/Palette.h b/shared-bindings/displayio/Palette.h index 767fc7b63..8c9fe11e3 100644 --- a/shared-bindings/displayio/Palette.h +++ b/shared-bindings/displayio/Palette.h @@ -33,6 +33,8 @@ extern const mp_obj_type_t displayio_palette_type; void common_hal_displayio_palette_construct(displayio_palette_t* self, uint16_t color_count); void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t palette_index, uint32_t color); +uint32_t common_hal_displayio_palette_get_color(displayio_palette_t* self, uint32_t palette_index); +uint32_t common_hal_displayio_palette_get_len(displayio_palette_t* self); void common_hal_displayio_palette_make_opaque(displayio_palette_t* self, uint32_t palette_index); void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t palette_index); diff --git a/shared-bindings/displayio/__init__.c b/shared-bindings/displayio/__init__.c index 9dc17103b..fd3dc4233 100644 --- a/shared-bindings/displayio/__init__.c +++ b/shared-bindings/displayio/__init__.c @@ -35,6 +35,7 @@ #include "shared-bindings/displayio/Display.h" #include "shared-bindings/displayio/FourWire.h" #include "shared-bindings/displayio/Group.h" +#include "shared-bindings/displayio/I2CDisplay.h" #include "shared-bindings/displayio/OnDiskBitmap.h" #include "shared-bindings/displayio/Palette.h" #include "shared-bindings/displayio/ParallelBus.h" @@ -61,6 +62,7 @@ //| Display //| FourWire //| Group +//| I2CDisplay //| OnDiskBitmap //| Palette //| ParallelBus @@ -96,6 +98,7 @@ STATIC const mp_rom_map_elem_t displayio_module_globals_table[] = { { MP_ROM_QSTR(MP_QSTR_TileGrid), MP_ROM_PTR(&displayio_tilegrid_type) }, { MP_ROM_QSTR(MP_QSTR_FourWire), MP_ROM_PTR(&displayio_fourwire_type) }, + { MP_ROM_QSTR(MP_QSTR_I2CDisplay), MP_ROM_PTR(&displayio_i2cdisplay_type) }, { MP_ROM_QSTR(MP_QSTR_ParallelBus), MP_ROM_PTR(&displayio_parallelbus_type) }, { MP_ROM_QSTR(MP_QSTR_release_displays), MP_ROM_PTR(&displayio_release_displays_obj) }, diff --git a/shared-module/displayio/ColorConverter.c b/shared-module/displayio/ColorConverter.c index 3928e115a..563d40c51 100644 --- a/shared-module/displayio/ColorConverter.c +++ b/shared-module/displayio/ColorConverter.c @@ -29,15 +29,36 @@ void common_hal_displayio_colorconverter_construct(displayio_colorconverter_t* self) { } -bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, uint32_t input_color, uint16_t* output_color) { - // TODO(tannewt): Validate the color input against the input format. - uint32_t r5 = (input_color >> 19); - uint32_t g6 = (input_color >> 10) & 0x3f; - uint32_t b5 = (input_color >> 3) & 0x1f; +uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888) { + uint32_t r5 = (color_rgb888 >> 19); + uint32_t g6 = (color_rgb888 >> 10) & 0x3f; + uint32_t b5 = (color_rgb888 >> 3) & 0x1f; uint32_t packed = r5 << 11 | g6 << 5 | b5; // swap bytes - *output_color = __builtin_bswap16(packed); - return true; + return __builtin_bswap16(packed); +} + +uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888) { + uint32_t r8 = (color_rgb888 >> 16); + uint32_t g8 = (color_rgb888 >> 8) & 0xff; + uint32_t b8 = color_rgb888 & 0xff; + return (r8 * 19) / 255 + (g8 * 182) / 255 + (b8 + 54) / 255; +} + +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->grayscale && colorspace->depth <= 8) { + uint8_t luma = displayio_colorconverter_compute_luma(input_color); + *output_color = luma >> (8 - colorspace->depth); + return true; + } + return false; +} + +bool common_hal_displayio_colorconverter_convert(displayio_colorconverter_t *self, const _displayio_colorspace_t* colorspace, uint32_t input_color, uint32_t* output_color) { + return displayio_colorconverter_convert(self, colorspace, input_color, output_color); } // Currently no refresh logic is needed for a ColorConverter. diff --git a/shared-module/displayio/ColorConverter.h b/shared-module/displayio/ColorConverter.h index 7f3c1a0a0..c48b98e6d 100644 --- a/shared-module/displayio/ColorConverter.h +++ b/shared-module/displayio/ColorConverter.h @@ -31,6 +31,7 @@ #include #include "py/obj.h" +#include "shared-module/displayio/Palette.h" typedef struct { mp_obj_base_t base; @@ -38,5 +39,8 @@ 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); +uint16_t displayio_colorconverter_compute_rgb565(uint32_t color_rgb888); +uint8_t displayio_colorconverter_compute_luma(uint32_t color_rgb888); #endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_COLORCONVERTER_H diff --git a/shared-module/displayio/Display.c b/shared-module/displayio/Display.c index 0ae88300a..9aee70e32 100644 --- a/shared-module/displayio/Display.c +++ b/shared-module/displayio/Display.c @@ -28,6 +28,7 @@ #include "py/runtime.h" #include "shared-bindings/displayio/FourWire.h" +#include "shared-bindings/displayio/I2CDisplay.h" #include "shared-bindings/displayio/ParallelBus.h" #include "shared-bindings/microcontroller/Pin.h" #include "shared-bindings/time/__init__.h" @@ -35,6 +36,7 @@ #include "supervisor/shared/display.h" #include +#include #include "tick.h" @@ -42,11 +44,14 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self, mp_obj_t bus, uint16_t width, uint16_t height, int16_t colstart, int16_t rowstart, uint16_t rotation, - uint16_t color_depth, uint8_t set_column_command, uint8_t set_row_command, + uint16_t color_depth, bool grayscale, bool pixels_in_byte_share_row, + uint8_t set_column_command, uint8_t set_row_command, uint8_t write_ram_command, uint8_t set_vertical_scroll, uint8_t* init_sequence, uint16_t init_sequence_len, - const mcu_pin_obj_t* backlight_pin, mp_float_t brightness, bool auto_brightness, + const mcu_pin_obj_t* backlight_pin, uint16_t brightness_command, mp_float_t brightness, bool auto_brightness, bool single_byte_bounds, bool data_as_commands) { - self->color_depth = color_depth; + self->colorspace.depth = color_depth; + self->colorspace.grayscale = grayscale; + self->colorspace.pixels_in_byte_share_row = pixels_in_byte_share_row; self->set_column_command = set_column_command; self->set_row_command = set_row_command; self->write_ram_command = write_ram_command; @@ -54,6 +59,7 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self, self->current_group = NULL; self->colstart = colstart; self->rowstart = rowstart; + self->brightness_command = brightness_command; self->auto_brightness = auto_brightness; self->data_as_commands = data_as_commands; self->single_byte_bounds = single_byte_bounds; @@ -66,6 +72,10 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self, self->begin_transaction = common_hal_displayio_fourwire_begin_transaction; self->send = common_hal_displayio_fourwire_send; self->end_transaction = common_hal_displayio_fourwire_end_transaction; + } else if (MP_OBJ_IS_TYPE(bus, &displayio_i2cdisplay_type)) { + self->begin_transaction = common_hal_displayio_i2cdisplay_begin_transaction; + self->send = common_hal_displayio_i2cdisplay_send; + self->end_transaction = common_hal_displayio_i2cdisplay_end_transaction; } else { mp_raise_ValueError(translate("Unsupported display bus type")); } @@ -83,13 +93,13 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self, bool delay = (data_size & DELAY) != 0; data_size &= ~DELAY; uint8_t *data = cmd + 2; - self->send(self->bus, true, cmd, 1); if (self->data_as_commands) { - // Loop through each parameter to force a CS toggle - for (uint32_t j=0; j < data_size; j++) { - self->send(self->bus, true, data + j, 1); - } + uint8_t full_command[data_size + 1]; + full_command[0] = cmd[0]; + memcpy(full_command + 1, data, data_size); + self->send(self->bus, true, full_command, data_size + 1); } else { + self->send(self->bus, true, cmd, 1); self->send(self->bus, false, data, data_size); } uint16_t delay_length_ms = 10; @@ -141,11 +151,13 @@ void common_hal_displayio_display_construct(displayio_display_obj_t* self, } else { self->backlight_pwm.base.type = &pulseio_pwmout_type; common_hal_pulseio_pwmout_never_reset(&self->backlight_pwm); - if (!self->auto_brightness) { - common_hal_displayio_display_set_brightness(self, brightness); - } } } + if (!self->auto_brightness && (self->backlight_inout.base.type != &mp_type_NoneType || brightness_command <= 0xff)) { + common_hal_displayio_display_set_brightness(self, brightness); + } else { + self->current_brightness = -1.0; + } self->area.x1 = 0; self->area.y1 = 0; @@ -234,17 +246,7 @@ void common_hal_displayio_display_set_auto_brightness(displayio_display_obj_t* s } mp_float_t common_hal_displayio_display_get_brightness(displayio_display_obj_t* self) { - if (self->backlight_pwm.base.type == &pulseio_pwmout_type) { - uint16_t duty_cycle = common_hal_pulseio_pwmout_get_duty_cycle(&self->backlight_pwm); - return duty_cycle / ((mp_float_t) 0xffff); - } else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) { - if (common_hal_digitalio_digitalinout_get_value(&self->backlight_inout)) { - return 1.0; - } else { - return 0.0; - } - } - return -1.0; + return self->current_brightness; } bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, mp_float_t brightness) { @@ -256,8 +258,26 @@ bool common_hal_displayio_display_set_brightness(displayio_display_obj_t* self, } else if (self->backlight_inout.base.type == &digitalio_digitalinout_type) { common_hal_digitalio_digitalinout_set_value(&self->backlight_inout, brightness > 0.99); ok = true; + } else if (self->brightness_command < 0x100) { + ok = self->begin_transaction(self->bus); + if (ok) { + if (self->data_as_commands) { + uint8_t set_brightness[2] = {self->brightness_command, (uint8_t) (0xff * brightness)}; + self->send(self->bus, true, set_brightness, 2); + } else { + uint8_t command = self->brightness_command; + uint8_t hex_brightness = 0xff * brightness; + self->send(self->bus, true, &command, 1); + self->send(self->bus, false, &hex_brightness, 1); + } + self->end_transaction(self->bus); + } + } self->updating_backlight = false; + if (ok) { + self->current_brightness = brightness; + } return ok; } @@ -269,35 +289,67 @@ void displayio_display_end_transaction(displayio_display_obj_t* self) { self->end_transaction(self->bus); } -void displayio_display_set_region_to_update(displayio_display_obj_t* self, uint16_t x0, uint16_t y0, uint16_t x1, uint16_t y1) { - self->send(self->bus, true, &self->set_column_command, 1); - bool isCommand = self->data_as_commands; - if (self->single_byte_bounds) { - uint8_t data[2]; - data[0] = x0 + self->colstart; - data[1] = x1 - 1 + self->colstart; - self->send(self->bus, isCommand, (uint8_t*) data, 2); - } else { - uint16_t data[2]; - data[0] = __builtin_bswap16(x0 + self->colstart); - data[1] = __builtin_bswap16(x1 - 1 + self->colstart); - self->send(self->bus, isCommand, (uint8_t*) data, 4); +void displayio_display_set_region_to_update(displayio_display_obj_t* self, displayio_area_t* area) { + uint16_t x1 = area->x1; + uint16_t x2 = area->x2; + uint16_t y1 = area->y1; + uint16_t y2 = area->y2; + // Collapse down the dimension where multiple pixels are in a byte. + if (self->colorspace.depth < 8) { + uint8_t pixels_per_byte = 8 / self->colorspace.depth; + if (self->colorspace.pixels_in_byte_share_row) { + x1 /= pixels_per_byte; + x2 /= pixels_per_byte; + } else { + y1 /= pixels_per_byte; + y2 /= pixels_per_byte; + } + } + + // Set column. + uint8_t data[5]; + data[0] = self->set_column_command; + uint8_t data_length = 1; + if (!self->data_as_commands) { + self->send(self->bus, true, data, 1); + data_length = 0; } - self->send(self->bus, true, &self->set_row_command, 1); if (self->single_byte_bounds) { - uint8_t data[2]; - data[0] = y0 + self->rowstart; - data[1] = y1 - 1 + self->rowstart; - self->send(self->bus, isCommand, (uint8_t*) data, 2); + data[data_length] = x1 + self->colstart; + data[data_length + 1] = x2 - 1 + self->colstart; + data_length += 2; } else { - uint16_t data[2]; - data[0] = __builtin_bswap16(y0 + self->rowstart); - data[1] = __builtin_bswap16(y1 - 1 + self->rowstart); - self->send(self->bus, isCommand, (uint8_t*) data, 4); + x1 += self->colstart; + x2 += self->colstart - 1; + data[data_length] = x1 >> 8; + data[data_length + 1] = x1 & 0xff; + data[data_length + 2] = x2 >> 8; + data[data_length + 3] = x2 & 0xff; + data_length += 4; } + self->send(self->bus, self->data_as_commands, data, data_length); + + // Set row. + data[0] = self->set_row_command; + data_length = 1; if (!self->data_as_commands) { - self->send(self->bus, true, &self->write_ram_command, 1); + self->send(self->bus, true, data, 1); + data_length = 0; } + if (self->single_byte_bounds) { + data[data_length] = y1 + self->rowstart; + data[data_length + 1] = y2 - 1 + self->rowstart; + data_length += 2; + } else { + y1 += self->rowstart; + y2 += self->rowstart - 1; + data[data_length] = y1 >> 8; + data[data_length + 1] = y1 & 0xff; + data[data_length + 2] = y2 >> 8; + data[data_length + 3] = y2 & 0xff; + data_length += 4; + } + self->send(self->bus, self->data_as_commands, data, data_length); } void displayio_display_start_refresh(displayio_display_obj_t* self) { @@ -322,6 +374,9 @@ void displayio_display_finish_refresh(displayio_display_obj_t* self) { } void displayio_display_send_pixels(displayio_display_obj_t* self, uint8_t* pixels, uint32_t length) { + if (!self->data_as_commands) { + self->send(self->bus, true, &self->write_ram_command, 1); + } self->send(self->bus, false, pixels, length); } @@ -349,9 +404,33 @@ void release_display(displayio_display_obj_t* self) { } bool displayio_display_fill_area(displayio_display_obj_t *self, displayio_area_t* area, uint32_t* mask, uint32_t *buffer) { - return displayio_group_fill_area(self->current_group, area, mask, buffer); + return displayio_group_fill_area(self->current_group, &self->colorspace, area, mask, buffer); } bool displayio_display_clip_area(displayio_display_obj_t *self, const displayio_area_t* area, displayio_area_t* clipped) { - return displayio_area_compute_overlap(&self->area, area, clipped); + bool overlaps = displayio_area_compute_overlap(&self->area, area, clipped); + if (!overlaps) { + return false; + } + // Expand the area if we have multiple pixels per byte and we need to byte + // align the bounds. + if (self->colorspace.depth < 8) { + uint8_t pixels_per_byte = 8 / self->colorspace.depth; + if (self->colorspace.pixels_in_byte_share_row) { + if (clipped->x1 % pixels_per_byte != 0) { + clipped->x1 -= clipped->x1 % pixels_per_byte; + } + if (clipped->x2 % pixels_per_byte != 0) { + clipped->x2 += pixels_per_byte - clipped->x2 % pixels_per_byte; + } + } else { + if (clipped->y1 % pixels_per_byte != 0) { + clipped->y1 -= clipped->y1 % pixels_per_byte; + } + if (clipped->y2 % pixels_per_byte != 0) { + clipped->y2 += pixels_per_byte - clipped->y2 % pixels_per_byte; + } + } + } + return true; } diff --git a/shared-module/displayio/Display.h b/shared-module/displayio/Display.h index fa8902ced..96d1c06d5 100644 --- a/shared-module/displayio/Display.h +++ b/shared-module/displayio/Display.h @@ -52,11 +52,13 @@ typedef struct { uint64_t last_backlight_refresh; displayio_buffer_transform_t transform; displayio_area_t area; + mp_float_t current_brightness; uint16_t width; uint16_t height; - uint16_t color_depth; + _displayio_colorspace_t colorspace; int16_t colstart; int16_t rowstart; + uint16_t brightness_command; uint8_t set_column_command; uint8_t set_row_command; uint8_t write_ram_command; diff --git a/shared-module/displayio/FourWire.c b/shared-module/displayio/FourWire.c index d38da56fc..913635db8 100644 --- a/shared-module/displayio/FourWire.c +++ b/shared-module/displayio/FourWire.c @@ -59,6 +59,11 @@ void common_hal_displayio_fourwire_construct(displayio_fourwire_obj_t* self, common_hal_digitalio_digitalinout_construct(&self->reset, reset); common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); never_reset_pin_number(reset->number); + + common_hal_digitalio_digitalinout_set_value(&self->reset, false); + common_hal_mcu_delay_us(10); + common_hal_digitalio_digitalinout_set_value(&self->reset, true); + common_hal_mcu_delay_us(10); } never_reset_pin_number(command->number); @@ -88,13 +93,19 @@ bool common_hal_displayio_fourwire_begin_transaction(mp_obj_t obj) { void common_hal_displayio_fourwire_send(mp_obj_t obj, bool command, uint8_t *data, uint32_t data_length) { displayio_fourwire_obj_t* self = MP_OBJ_TO_PTR(obj); + common_hal_digitalio_digitalinout_set_value(&self->command, !command); if (command) { - common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); - common_hal_mcu_delay_us(1); - common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + // Toggle chip select after each command byte in case the display driver + // IC latches commands based on it. + for (size_t i = 0; i < data_length; i++) { + common_hal_busio_spi_write(self->bus, &data[i], 1); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, true); + common_hal_mcu_delay_us(1); + common_hal_digitalio_digitalinout_set_value(&self->chip_select, false); + } + } else { + common_hal_busio_spi_write(self->bus, data, data_length); } - common_hal_digitalio_digitalinout_set_value(&self->command, !command); - common_hal_busio_spi_write(self->bus, data, data_length); } void common_hal_displayio_fourwire_end_transaction(mp_obj_t obj) { diff --git a/shared-module/displayio/FourWire.h b/shared-module/displayio/FourWire.h index 743139e62..a4260a3ac 100644 --- a/shared-module/displayio/FourWire.h +++ b/shared-module/displayio/FourWire.h @@ -24,8 +24,8 @@ * THE SOFTWARE. */ -#ifndef MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H -#define MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FOURWIRE_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FOURWIRE_H #include "common-hal/busio/SPI.h" #include "common-hal/digitalio/DigitalInOut.h" @@ -43,4 +43,4 @@ typedef struct { uint8_t phase; } displayio_fourwire_obj_t; -#endif // MICROPY_INCLUDED_ATMEL_SAMD_COMMON_HAL_DISPLAYIO_FOURWIRE_H +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_FOURWIRE_H diff --git a/shared-module/displayio/Group.c b/shared-module/displayio/Group.c index 15060e87b..38418a029 100644 --- a/shared-module/displayio/Group.c +++ b/shared-module/displayio/Group.c @@ -270,19 +270,19 @@ void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* self->in_group = false; } -bool displayio_group_fill_area(displayio_group_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t* buffer) { +bool displayio_group_fill_area(displayio_group_t *self, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t* buffer) { // Track if any of the layers finishes filling in the given area. We can ignore any remaining // layers at that point. bool full_coverage = false; for (int32_t i = self->size - 1; i >= 0 ; i--) { mp_obj_t layer = self->children[i].native; if (MP_OBJ_IS_TYPE(layer, &displayio_tilegrid_type)) { - if (displayio_tilegrid_fill_area(layer, area, mask, buffer)) { + if (displayio_tilegrid_fill_area(layer, colorspace, area, mask, buffer)) { full_coverage = true; break; } } else if (MP_OBJ_IS_TYPE(layer, &displayio_group_type)) { - if (displayio_group_fill_area(layer, area, mask, buffer)) { + if (displayio_group_fill_area(layer, colorspace, area, mask, buffer)) { full_coverage = true; break; } diff --git a/shared-module/displayio/Group.h b/shared-module/displayio/Group.h index 7ce19a4cd..4e2308e56 100644 --- a/shared-module/displayio/Group.h +++ b/shared-module/displayio/Group.h @@ -32,6 +32,7 @@ #include "py/obj.h" #include "shared-module/displayio/area.h" +#include "shared-module/displayio/Palette.h" typedef struct { mp_obj_t native; @@ -54,7 +55,7 @@ typedef struct { void displayio_group_construct(displayio_group_t* self, displayio_group_child_t* child_array, uint32_t max_size, uint32_t scale, mp_int_t x, mp_int_t y); bool displayio_group_get_previous_area(displayio_group_t *group, displayio_area_t* area); -bool displayio_group_fill_area(displayio_group_t *group, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer); +bool displayio_group_fill_area(displayio_group_t *group, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer); void displayio_group_update_transform(displayio_group_t *group, const displayio_buffer_transform_t* parent_transform); void displayio_group_finish_refresh(displayio_group_t *self); displayio_area_t* displayio_group_get_refresh_areas(displayio_group_t *self, displayio_area_t* tail); diff --git a/shared-module/displayio/I2CDisplay.c b/shared-module/displayio/I2CDisplay.c new file mode 100644 index 000000000..9d8949528 --- /dev/null +++ b/shared-module/displayio/I2CDisplay.c @@ -0,0 +1,105 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2018 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#include "shared-bindings/displayio/I2CDisplay.h" + +#include +#include + +#include "py/gc.h" +#include "py/runtime.h" +#include "shared-bindings/busio/I2C.h" +#include "shared-bindings/digitalio/DigitalInOut.h" +#include "shared-bindings/microcontroller/__init__.h" +#include "shared-bindings/time/__init__.h" + +#include "tick.h" + +void common_hal_displayio_i2cdisplay_construct(displayio_i2cdisplay_obj_t* self, + busio_i2c_obj_t* i2c, uint16_t device_address, const mcu_pin_obj_t* reset) { + + // Probe the bus to see if a device acknowledges the given address. + if (!common_hal_busio_i2c_probe(i2c, device_address)) { + mp_raise_ValueError_varg(translate("Unable to find I2C Display at %x"), device_address); + } + + // Write to the device and return 0 on success or an appropriate error code from mperrno.h + self->bus = i2c; + common_hal_busio_i2c_never_reset(self->bus); + // Our object is statically allocated off the heap so make sure the bus object lives to the end + // of the heap as well. + gc_never_free(self->bus); + + self->address = device_address; + + if (reset != NULL) { + common_hal_digitalio_digitalinout_construct(&self->reset, reset); + common_hal_digitalio_digitalinout_switch_to_output(&self->reset, true, DRIVE_MODE_PUSH_PULL); + never_reset_pin_number(reset->number); + + common_hal_digitalio_digitalinout_set_value(&self->reset, false); + common_hal_mcu_delay_us(1); + common_hal_digitalio_digitalinout_set_value(&self->reset, true); + } +} + +void common_hal_displayio_i2cdisplay_deinit(displayio_i2cdisplay_obj_t* self) { + if (self->bus == &self->inline_bus) { + common_hal_busio_i2c_deinit(self->bus); + } + + reset_pin_number(self->reset.pin->number); +} + +bool common_hal_displayio_i2cdisplay_begin_transaction(mp_obj_t obj) { + displayio_i2cdisplay_obj_t* self = MP_OBJ_TO_PTR(obj); + if (!common_hal_busio_i2c_try_lock(self->bus)) { + return false; + } + return true; +} + +void common_hal_displayio_i2cdisplay_send(mp_obj_t obj, bool command, uint8_t *data, uint32_t data_length) { + displayio_i2cdisplay_obj_t* self = MP_OBJ_TO_PTR(obj); + if (command) { + uint8_t command_bytes[2 * data_length]; + for (uint32_t i = 0; i < data_length; i++) { + command_bytes[2 * i] = 0x80; + command_bytes[2 * i + 1] = data[i]; + } + common_hal_busio_i2c_write(self->bus, self->address, command_bytes, 2 * data_length, true); + } else { + uint8_t data_bytes[data_length + 1]; + data_bytes[0] = 0x40; + memcpy(data_bytes + 1, data, data_length); + common_hal_busio_i2c_write(self->bus, self->address, data_bytes, data_length + 1, true); + } +} + +void common_hal_displayio_i2cdisplay_end_transaction(mp_obj_t obj) { + displayio_i2cdisplay_obj_t* self = MP_OBJ_TO_PTR(obj); + common_hal_busio_i2c_unlock(self->bus); +} diff --git a/shared-module/displayio/I2CDisplay.h b/shared-module/displayio/I2CDisplay.h new file mode 100644 index 000000000..4636c3f73 --- /dev/null +++ b/shared-module/displayio/I2CDisplay.h @@ -0,0 +1,41 @@ +/* + * This file is part of the MicroPython project, http://micropython.org/ + * + * The MIT License (MIT) + * + * Copyright (c) 2019 Scott Shawcroft for Adafruit Industries + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + +#ifndef MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_I2CDISPLAY_H +#define MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_I2CDISPLAY_H + +#include "common-hal/busio/I2C.h" +#include "common-hal/digitalio/DigitalInOut.h" + +typedef struct { + mp_obj_base_t base; + busio_i2c_obj_t* bus; + busio_i2c_obj_t inline_bus; + digitalio_digitalinout_obj_t reset; + uint16_t address; +} displayio_i2cdisplay_obj_t; + +#endif // MICROPY_INCLUDED_SHARED_MODULE_DISPLAYIO_I2CDISPLAY_H diff --git a/shared-module/displayio/Palette.c b/shared-module/displayio/Palette.c index 8dc6e766b..444151b3c 100644 --- a/shared-module/displayio/Palette.c +++ b/shared-module/displayio/Palette.c @@ -26,49 +26,49 @@ #include "shared-bindings/displayio/Palette.h" +#include "shared-module/displayio/ColorConverter.h" + void common_hal_displayio_palette_construct(displayio_palette_t* self, uint16_t color_count) { self->color_count = color_count; - self->colors = (uint32_t *) m_malloc(color_count * sizeof(uint16_t), false); - uint32_t opaque_byte_count = color_count / 8; - if (color_count % 8 > 0) { - opaque_byte_count += 1; - } - self->opaque = (uint32_t *) m_malloc(opaque_byte_count, false); + self->colors = (_displayio_color_t *) m_malloc(color_count * sizeof(_displayio_color_t), false); } void common_hal_displayio_palette_make_opaque(displayio_palette_t* self, uint32_t palette_index) { - self->opaque[palette_index / 32] &= ~(0x1 << (palette_index % 32)); + self->colors[palette_index].transparent = false; } void common_hal_displayio_palette_make_transparent(displayio_palette_t* self, uint32_t palette_index) { - self->opaque[palette_index / 32] |= (0x1 << (palette_index % 32)); + self->colors[palette_index].transparent = true; +} + +uint32_t common_hal_displayio_palette_get_len(displayio_palette_t* self) { + return self->color_count; } void common_hal_displayio_palette_set_color(displayio_palette_t* self, uint32_t palette_index, uint32_t color) { - uint32_t shift = (palette_index % 2) * 16; - uint32_t masked = self->colors[palette_index / 2] & ~(0xffff << shift); - uint32_t r5 = (color >> 19); - uint32_t g6 = (color >> 10) & 0x3f; - uint32_t b5 = (color >> 3) & 0x1f; - uint32_t packed = r5 << 11 | g6 << 5 | b5; - // swap bytes - packed = __builtin_bswap16(packed); - uint32_t final_color = masked | packed << shift; - if (self->colors[palette_index / 2] == final_color) { + if (self->colors[palette_index].rgb888 == color) { return; } - self->colors[palette_index / 2] = final_color; + self->colors[palette_index].rgb888 = color; + self->colors[palette_index].luma = displayio_colorconverter_compute_luma(color); + self->colors[palette_index].rgb565 = displayio_colorconverter_compute_rgb565(color); self->needs_refresh = true; } -bool displayio_palette_get_color(displayio_palette_t *self, uint32_t palette_index, uint16_t* color) { - if (palette_index > self->color_count) { - return false; +uint32_t common_hal_displayio_palette_get_color(displayio_palette_t* self, uint32_t palette_index) { + return self->colors[palette_index].rgb888; +} + +bool displayio_palette_get_color(displayio_palette_t *self, const _displayio_colorspace_t* colorspace, uint32_t palette_index, uint32_t* color) { + if (palette_index > self->color_count || self->colors[palette_index].transparent) { + return false; // returns opaque } - if ((self->opaque[palette_index / 32] & (0x1 << (palette_index % 32))) != 0) { - return false; + + if (colorspace->grayscale) { + *color = self->colors[palette_index].luma >> (8 - colorspace->depth); + } else { + *color = self->colors[palette_index].rgb565; } - *color = (self->colors[palette_index / 2] >> (16 * (palette_index % 2))) & 0xffff; return true; } diff --git a/shared-module/displayio/Palette.h b/shared-module/displayio/Palette.h index 3813e8bdd..6f5e2774c 100644 --- a/shared-module/displayio/Palette.h +++ b/shared-module/displayio/Palette.h @@ -32,15 +32,28 @@ #include "py/obj.h" +typedef struct { + uint8_t depth; + bool grayscale; + bool pixels_in_byte_share_row; + uint8_t hue; +} _displayio_colorspace_t; + +typedef struct { + uint32_t rgb888; + uint16_t rgb565; + uint8_t luma; + bool transparent; // This may have additional bits added later for blending. +} _displayio_color_t; + typedef struct { mp_obj_base_t base; - uint32_t* opaque; - uint32_t* colors; + _displayio_color_t* colors; uint32_t color_count; bool needs_refresh; } displayio_palette_t; -bool displayio_palette_get_color(displayio_palette_t *palette, uint32_t palette_index, uint16_t* color); +bool displayio_palette_get_color(displayio_palette_t *palette, const _displayio_colorspace_t* colorspace, uint32_t palette_index, uint32_t* color); bool displayio_palette_needs_refresh(displayio_palette_t *self); void displayio_palette_finish_refresh(displayio_palette_t *self); diff --git a/shared-module/displayio/TileGrid.c b/shared-module/displayio/TileGrid.c index 0cd00e0ce..77e6daf6f 100644 --- a/shared-module/displayio/TileGrid.c +++ b/shared-module/displayio/TileGrid.c @@ -291,7 +291,7 @@ void common_hal_displayio_tilegrid_set_top_left(displayio_tilegrid_t *self, uint self->full_change = true; } -bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer) { +bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer) { // If no tiles are present we have no impact. uint8_t* tiles = self->tiles; if (self->inline_tiles) { @@ -371,12 +371,13 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_ar y_shift = temp_shift; } + 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; + 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++) { // 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; + int16_t offset = row_start + (x - start_x + x_shift) * x_stride; // in pixels // This is super useful for debugging out range accesses. Uncomment to use. // if (offset < 0 || offset >= (int32_t) displayio_area_size(area)) { @@ -404,23 +405,38 @@ bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_ar value = common_hal_displayio_ondiskbitmap_get_pixel(self->bitmap, tile_x, tile_y); } - uint16_t* pixel = ((uint16_t*) buffer) + offset; + uint32_t pixel; + bool opaque = true; if (self->pixel_shader == mp_const_none) { - *pixel = value; - return true; + pixel = value; } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_palette_type)) { - if (!displayio_palette_get_color(self->pixel_shader, value, pixel)) { - // A pixel is transparent so we haven't fully covered the area ourselves. - full_coverage = false; - } else { - mask[offset / 32] |= 1 << (offset % 32); - } + opaque = displayio_palette_get_color(self->pixel_shader, colorspace, value, &pixel); } else if (MP_OBJ_IS_TYPE(self->pixel_shader, &displayio_colorconverter_type)) { - if (!common_hal_displayio_colorconverter_convert(self->pixel_shader, value, pixel)) { - // A pixel is transparent so we haven't fully covered the area ourselves. - full_coverage = false; - } else { - mask[offset / 32] |= 1 << (offset % 32); + opaque = displayio_colorconverter_convert(self->pixel_shader, colorspace, value, &pixel); + } + if (!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; + } else if (colorspace->depth == 8) { + *(((uint8_t*) buffer) + offset) = 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) { + uint16_t width = displayio_area_width(area); + uint16_t row = offset / width; + uint16_t col = offset % width; + // Dividing by pixels_per_byte does truncated division even if we multiply it back out. + offset = col * pixels_per_byte + (row / pixels_per_byte) * pixels_per_byte * width + row % pixels_per_byte; + // Also useful for validating that the bitpacking worked correctly. + // if (offset > displayio_area_size(area)) { + // asm("bkpt"); + // } + } + ((uint8_t*)buffer)[offset / pixels_per_byte] |= pixel << ((offset % pixels_per_byte) * colorspace->depth); } } } diff --git a/shared-module/displayio/TileGrid.h b/shared-module/displayio/TileGrid.h index 4d97eccc2..fb6033e9c 100644 --- a/shared-module/displayio/TileGrid.h +++ b/shared-module/displayio/TileGrid.h @@ -32,6 +32,7 @@ #include "py/obj.h" #include "shared-module/displayio/area.h" +#include "shared-module/displayio/Palette.h" typedef struct { mp_obj_base_t base; @@ -71,7 +72,7 @@ displayio_area_t* displayio_tilegrid_get_refresh_areas(displayio_tilegrid_t *sel // Area is always in absolute screen coordinates. Update transform is used to inform TileGrids how // they relate to it. -bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer); +bool displayio_tilegrid_fill_area(displayio_tilegrid_t *self, const _displayio_colorspace_t* colorspace, const displayio_area_t* area, uint32_t* mask, uint32_t *buffer); void displayio_tilegrid_update_transform(displayio_tilegrid_t *group, const displayio_buffer_transform_t* parent_transform); // Fills in area with the maximum bounds of all related pixels in the last rendered frame. Returns diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index b6709e0eb..97060aaeb 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -19,9 +19,10 @@ #include "supervisor/usb.h" primary_display_t displays[CIRCUITPY_DISPLAY_LIMIT]; +uint32_t frame_count = 0; bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area) { - uint16_t buffer_size = 512; + uint16_t buffer_size = 128; // In uint32_ts displayio_area_t clipped; // Clip the area to the display by overlapping the areas. If there is no overlap then we're done. @@ -30,15 +31,36 @@ bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area } uint16_t subrectangles = 1; uint16_t rows_per_buffer = displayio_area_height(&clipped); - if (displayio_area_size(area) > buffer_size) { - rows_per_buffer = buffer_size / displayio_area_width(&clipped); + uint8_t pixels_per_word = (sizeof(uint32_t) * 8) / display->colorspace.depth; + uint16_t pixels_per_buffer = displayio_area_size(&clipped); + if (displayio_area_size(&clipped) > buffer_size * pixels_per_word) { + rows_per_buffer = buffer_size * pixels_per_word / displayio_area_width(&clipped); + if (rows_per_buffer == 0) { + rows_per_buffer = 1; + } + // If pixels are packed by column then ensure rows_per_buffer is on a byte boundary. + if (display->colorspace.depth < 8 && !display->colorspace.pixels_in_byte_share_row) { + uint8_t pixels_per_byte = 8 / display->colorspace.depth; + if (rows_per_buffer % pixels_per_byte != 0) { + rows_per_buffer -= rows_per_buffer % pixels_per_byte; + } + } subrectangles = displayio_area_height(&clipped) / rows_per_buffer; if (displayio_area_height(&clipped) % rows_per_buffer != 0) { subrectangles++; } - buffer_size = rows_per_buffer * displayio_area_width(&clipped); + pixels_per_buffer = rows_per_buffer * displayio_area_width(&clipped); + buffer_size = pixels_per_buffer / pixels_per_word; + if (pixels_per_buffer % pixels_per_word) { + buffer_size += 1; + } } - uint32_t buffer[buffer_size / 2]; + + // Allocated and shared as a uint32_t array so the compiler knows the + // alignment everywhere. + uint32_t buffer[buffer_size]; + volatile uint32_t mask_length = (pixels_per_buffer / 32) + 1; + uint32_t mask[mask_length]; uint16_t remaining_rows = displayio_area_height(&clipped); for (uint16_t j = 0; j < subrectangles; j++) { @@ -54,37 +76,30 @@ bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area remaining_rows -= rows_per_buffer; displayio_display_begin_transaction(display); - displayio_display_set_region_to_update(display, subrectangle.x1, subrectangle.y1, - subrectangle.x2, subrectangle.y2); + displayio_display_set_region_to_update(display, &subrectangle); displayio_display_end_transaction(display); - uint32_t mask[(buffer_size / 32) + 1]; - for (uint16_t k = 0; k < (buffer_size / 32) + 1; k++) { - mask[k] = 0x00000000; + uint16_t subrectangle_size_bytes; + if (display->colorspace.depth >= 8) { + subrectangle_size_bytes = displayio_area_size(&subrectangle) * (display->colorspace.depth / 8); + } else { + subrectangle_size_bytes = displayio_area_size(&subrectangle) / (8 / display->colorspace.depth); } - bool full_coverage = displayio_display_fill_area(display, &subrectangle, mask, buffer); - if (!full_coverage) { - uint32_t index = 0; - uint32_t current_mask = 0; - for (int16_t y = subrectangle.y1; y < subrectangle.y2; y++) { - for (int16_t x = subrectangle.x1; x < subrectangle.x2; x++) { - if (index % 32 == 0) { - current_mask = mask[index / 32]; - } - if ((current_mask & (1 << (index % 32))) == 0) { - ((uint16_t*) buffer)[index] = 0x0000; - } - index++; - } - } + for (uint16_t k = 0; k < mask_length; k++) { + mask[k] = 0x00000000; + } + for (uint16_t k = 0; k < buffer_size; k++) { + buffer[k] = 0x00000000; } + displayio_display_fill_area(display, &subrectangle, mask, buffer); + if (!displayio_display_begin_transaction(display)) { // Can't acquire display bus; skip the rest of the data. Try next display. return false; } - displayio_display_send_pixels(display, (uint8_t*) buffer, displayio_area_size(&subrectangle) * sizeof(uint16_t)); + displayio_display_send_pixels(display, (uint8_t*) buffer, subrectangle_size_bytes); displayio_display_end_transaction(display); // TODO(tannewt): Make refresh displays faster so we don't starve other @@ -96,7 +111,6 @@ bool refresh_area(displayio_display_obj_t* display, const displayio_area_t* area // Check for recursive calls to displayio_refresh_displays. bool refresh_displays_in_progress = false; -uint32_t frame_count = 0; void displayio_refresh_displays(void) { if (mp_hal_is_interrupted()) { @@ -153,6 +167,8 @@ void common_hal_displayio_release_displays(void) { continue; } else if (bus_type == &displayio_fourwire_type) { common_hal_displayio_fourwire_deinit(&displays[i].fourwire_bus); + } else if (bus_type == &displayio_i2cdisplay_type) { + common_hal_displayio_i2cdisplay_deinit(&displays[i].i2cdisplay_bus); } else if (bus_type == &displayio_parallelbus_type) { common_hal_displayio_parallelbus_deinit(&displays[i].parallel_bus); } @@ -169,30 +185,53 @@ void common_hal_displayio_release_displays(void) { void reset_displays(void) { // The SPI buses used by FourWires may be allocated on the heap so we need to move them inline. for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { - if (displays[i].fourwire_bus.base.type != &displayio_fourwire_type) { - continue; - } - displayio_fourwire_obj_t* fourwire = &displays[i].fourwire_bus; - if (((uint32_t) fourwire->bus) < ((uint32_t) &displays) || - ((uint32_t) fourwire->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) { - busio_spi_obj_t* original_spi = fourwire->bus; - #if BOARD_SPI - // We don't need to move original_spi if it is the board.SPI object because it is - // statically allocated already. (Doing so would also make it impossible to reference in - // a subsequent VM run.) - if (original_spi == common_hal_board_get_spi()) { - continue; + if (displays[i].fourwire_bus.base.type == &displayio_fourwire_type) { + displayio_fourwire_obj_t* fourwire = &displays[i].fourwire_bus; + if (((uint32_t) fourwire->bus) < ((uint32_t) &displays) || + ((uint32_t) fourwire->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) { + busio_spi_obj_t* original_spi = fourwire->bus; + #if BOARD_SPI + // We don't need to move original_spi if it is the board.SPI object because it is + // statically allocated already. (Doing so would also make it impossible to reference in + // a subsequent VM run.) + if (original_spi == common_hal_board_get_spi()) { + continue; + } + #endif + memcpy(&fourwire->inline_bus, original_spi, sizeof(busio_spi_obj_t)); + fourwire->bus = &fourwire->inline_bus; + // Check for other displays that use the same spi bus and swap them too. + for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) { + if (displays[i].fourwire_bus.base.type == &displayio_fourwire_type && + displays[i].fourwire_bus.bus == original_spi) { + displays[i].fourwire_bus.bus = &fourwire->inline_bus; + } } - #endif - memcpy(&fourwire->inline_bus, original_spi, sizeof(busio_spi_obj_t)); - fourwire->bus = &fourwire->inline_bus; - // Check for other displays that use the same spi bus and swap them too. - for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) { - if (displays[i].fourwire_bus.bus == original_spi) { - displays[i].fourwire_bus.bus = &fourwire->inline_bus; + } + } else if (displays[i].i2cdisplay_bus.base.type == &displayio_i2cdisplay_type) { + displayio_i2cdisplay_obj_t* i2c = &displays[i].i2cdisplay_bus; + if (((uint32_t) i2c->bus) < ((uint32_t) &displays) || + ((uint32_t) i2c->bus) > ((uint32_t) &displays + CIRCUITPY_DISPLAY_LIMIT)) { + busio_i2c_obj_t* original_i2c = i2c->bus; + #if BOARD_I2C + // We don't need to move original_i2c if it is the board.SPI object because it is + // statically allocated already. (Doing so would also make it impossible to reference in + // a subsequent VM run.) + if (original_i2c == common_hal_board_get_i2c()) { + continue; + } + #endif + memcpy(&i2c->inline_bus, original_i2c, sizeof(busio_i2c_obj_t)); + i2c->bus = &i2c->inline_bus; + // Check for other displays that use the same i2c bus and swap them too. + for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) { + if (displays[i].i2cdisplay_bus.base.type == &displayio_i2cdisplay_type && + displays[i].i2cdisplay_bus.bus == original_i2c) { + displays[i].i2cdisplay_bus.bus = &i2c->inline_bus; + } + } } } - } } for (uint8_t i = 0; i < CIRCUITPY_DISPLAY_LIMIT; i++) { diff --git a/shared-module/displayio/__init__.h b/shared-module/displayio/__init__.h index 18c1a4f4a..caa5ce36d 100644 --- a/shared-module/displayio/__init__.h +++ b/shared-module/displayio/__init__.h @@ -30,11 +30,13 @@ #include "shared-bindings/displayio/Display.h" #include "shared-bindings/displayio/FourWire.h" #include "shared-bindings/displayio/Group.h" +#include "shared-bindings/displayio/I2CDisplay.h" #include "shared-bindings/displayio/ParallelBus.h" typedef struct { union { displayio_fourwire_obj_t fourwire_bus; + displayio_i2cdisplay_obj_t i2cdisplay_bus; displayio_parallelbus_obj_t parallel_bus; }; displayio_display_obj_t display; diff --git a/shared-module/terminalio/Terminal.c b/shared-module/terminalio/Terminal.c index 7adf9d032..86c3e903e 100644 --- a/shared-module/terminalio/Terminal.c +++ b/shared-module/terminalio/Terminal.c @@ -35,6 +35,8 @@ void common_hal_terminalio_terminal_construct(terminalio_terminal_obj_t *self, d self->font = font; self->tilegrid = tilegrid; self->first_row = 0; + + common_hal_displayio_tilegrid_set_top_left(self->tilegrid, 0, 1); } size_t common_hal_terminalio_terminal_write(terminalio_terminal_obj_t *self, const byte *data, size_t len, int *errcode) { diff --git a/supervisor/shared/display.c b/supervisor/shared/display.c index 414f85034..6a39cbe8e 100644 --- a/supervisor/shared/display.c +++ b/supervisor/shared/display.c @@ -82,8 +82,7 @@ void supervisor_start_terminal(uint16_t width_px, uint16_t height_px) { grid->pixel_height = height_in_tiles * grid->tile_height; grid->tiles = tiles; - supervisor_terminal.cursor_x = 0; - supervisor_terminal.cursor_y = 0; + common_hal_terminalio_terminal_construct(&supervisor_terminal, grid, &supervisor_terminal_font); } void supervisor_stop_terminal(void) { @@ -147,17 +146,49 @@ displayio_bitmap_t blinka_bitmap = { .read_only = true }; -uint32_t blinka_transparency[1] = {0x80000000}; - -// These colors are RGB 565 with the bytes swapped. -uint32_t blinka_colors[8] = {0x78890000, 0x9F86B8FC, 0xffff0D5A, 0x0000f501, - 0x00000000, 0x00000000, 0x00000000, 0x00000000}; +_displayio_color_t blinka_colors[7] = { + { + .rgb888 = 0x000000, + .rgb565 = 0x0000, + .luma = 0x00, + .transparent = true + }, + { + .rgb888 = 0x8428bc, + .rgb565 = 0x7889, + .luma = 0xff // We cheat the luma here. It is actually 0x60 + }, + { + .rgb888 = 0xff89bc, + .rgb565 = 0xB8FC, + .luma = 0xb5 + }, + { + .rgb888 = 0x7beffe, + .rgb565 = 0x9F86, + .luma = 0xe0 + }, + { + .rgb888 = 0x51395f, + .rgb565 = 0x0D5A, + .luma = 0x47 + }, + { + .rgb888 = 0xffffff, + .rgb565 = 0xffff, + .luma = 0xff + }, + { + .rgb888 = 0x0736a0, + .rgb565 = 0xf501, + .luma = 0x44 + }, +}; displayio_palette_t blinka_palette = { .base = {.type = &displayio_palette_type }, - .opaque = blinka_transparency, .colors = blinka_colors, - .color_count = 16, + .color_count = 7, .needs_refresh = false }; diff --git a/tools/gen_display_resources.py b/tools/gen_display_resources.py index 2c674c8eb..a731cbd9c 100644 --- a/tools/gen_display_resources.py +++ b/tools/gen_display_resources.py @@ -98,14 +98,21 @@ c_file.write("""\ """) c_file.write("""\ -uint32_t terminal_transparency[1] = {0x00000000}; - -// These colors are RGB 565 with the bytes swapped. -uint32_t terminal_colors[1] = {0xffff0000}; +_displayio_color_t terminal_colors[2] = { + { + .rgb888 = 0x000000, + .rgb565 = 0x0000, + .luma = 0x00 + }, + { + .rgb888 = 0xffffff, + .rgb565 = 0xffff, + .luma = 0xff + }, +}; displayio_palette_t supervisor_terminal_color = { .base = {.type = &displayio_palette_type }, - .opaque = terminal_transparency, .colors = terminal_colors, .color_count = 2, .needs_refresh = false -- cgit v1.2.3 From c921f6637f55936726a880f225812302a3fdf0d1 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Jul 2019 16:46:31 +0700 Subject: update tinyusb lib to 0.5.x --- lib/tinyusb | 2 +- ports/nrf/Makefile | 1 + shared-module/usb_hid/Device.c | 10 ++-- shared-module/usb_midi/PortOut.c | 2 +- supervisor/shared/usb/tusb_config.h | 2 +- supervisor/shared/usb/usb_desc.c | 43 ++++++++++------ supervisor/shared/usb/usb_desc.h | 43 ---------------- supervisor/shared/usb/usb_msc_flash.c | 96 +++++++++++++++++++---------------- 8 files changed, 90 insertions(+), 109 deletions(-) delete mode 100644 supervisor/shared/usb/usb_desc.h (limited to 'supervisor') diff --git a/lib/tinyusb b/lib/tinyusb index 0848c462b..1ee9ef4f2 160000 --- a/lib/tinyusb +++ b/lib/tinyusb @@ -1 +1 @@ -Subproject commit 0848c462b3e431a9da42e96537d2b597a4579636 +Subproject commit 1ee9ef4f2b7c6acfab6c398a4f57ca22036958f7 diff --git a/ports/nrf/Makefile b/ports/nrf/Makefile index 83f87536b..9da2c498d 100755 --- a/ports/nrf/Makefile +++ b/ports/nrf/Makefile @@ -81,6 +81,7 @@ INC += -I./nrfx INC += -I./nrfx/hal INC += -I./nrfx/mdk INC += -I./nrfx/drivers/include +INC += -I./nrfx/drivers/src INC += -I./bluetooth INC += -I./peripherals INC += -I../../lib/mp-readline diff --git a/shared-module/usb_hid/Device.c b/shared-module/usb_hid/Device.c index 0e256cb5e..a5a57419c 100644 --- a/shared-module/usb_hid/Device.c +++ b/shared-module/usb_hid/Device.c @@ -47,15 +47,15 @@ void common_hal_usb_hid_device_send_report(usb_hid_device_obj_t *self, uint8_t* // Wait until interface is ready, timeout = 2 seconds uint64_t end_ticks = ticks_ms + 2000; - while ( (ticks_ms < end_ticks) && !tud_hid_generic_ready() ) { } + while ( (ticks_ms < end_ticks) && !tud_hid_ready() ) { } - if ( !tud_hid_generic_ready() ) { + if ( !tud_hid_ready() ) { mp_raise_msg(&mp_type_OSError, translate("USB Busy")); } memcpy(self->report_buffer, report, len); - if ( !tud_hid_generic_report(self->report_id, self->report_buffer, len) ) { + if ( !tud_hid_report(self->report_id, self->report_buffer, len) ) { mp_raise_msg(&mp_type_OSError, translate("USB Error")); } } @@ -70,7 +70,7 @@ static usb_hid_device_obj_t* get_hid_device(uint8_t report_id) { } // Callbacks invoked when receive Get_Report request through control endpoint -uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) { +uint16_t tud_hid_get_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t* buffer, uint16_t reqlen) { // only support Input Report if ( report_type != HID_REPORT_TYPE_INPUT ) return 0; @@ -80,7 +80,7 @@ uint16_t tud_hid_generic_get_report_cb(uint8_t report_id, hid_report_type_t repo } // Callbacks invoked when receive Set_Report request through control endpoint -void tud_hid_generic_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { +void tud_hid_set_report_cb(uint8_t report_id, hid_report_type_t report_type, uint8_t const* buffer, uint16_t bufsize) { usb_hid_device_obj_t* hid_device = get_hid_device(report_id); if ( report_type == HID_REPORT_TYPE_OUTPUT ) { diff --git a/shared-module/usb_midi/PortOut.c b/shared-module/usb_midi/PortOut.c index 99a4fad43..0b37dcfd4 100644 --- a/shared-module/usb_midi/PortOut.c +++ b/shared-module/usb_midi/PortOut.c @@ -33,5 +33,5 @@ size_t common_hal_usb_midi_portout_write(usb_midi_portout_obj_t *self, const uin } bool common_hal_usb_midi_portout_ready_to_tx(usb_midi_portout_obj_t *self) { - return tud_midi_connected(); + return tud_midi_mounted(); } diff --git a/supervisor/shared/usb/tusb_config.h b/supervisor/shared/usb/tusb_config.h index 301805275..ad5825b6c 100644 --- a/supervisor/shared/usb/tusb_config.h +++ b/supervisor/shared/usb/tusb_config.h @@ -107,7 +107,7 @@ // USB RAM PLACEMENT //--------------------------------------------------------------------+ #define CFG_TUSB_ATTR_USBRAM -#define CFG_TUSB_MEM_ALIGN ATTR_ALIGNED(4) +#define CFG_TUSB_MEM_ALIGN __attribute__ ((aligned(4))) #ifdef __cplusplus diff --git a/supervisor/shared/usb/usb_desc.c b/supervisor/shared/usb/usb_desc.c index 5a0de1ffc..48f2da3d3 100644 --- a/supervisor/shared/usb/usb_desc.c +++ b/supervisor/shared/usb/usb_desc.c @@ -24,23 +24,36 @@ * THE SOFTWARE. */ -#include "supervisor/shared/usb/usb_desc.h" +#include "lib/tinyusb/src/tusb.h" #include "shared-module/usb_hid/Device.h" #include "genhdr/autogen_usb_descriptor.h" -// tud_desc_set is required by tinyusb stack -tud_desc_set_t tud_desc_set = -{ - .device = &usb_desc_dev, - .config = &usb_desc_cfg, - .string_arr = (uint8_t const **) string_desc_arr, - .string_count = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]), +// Invoked when received GET DEVICE DESCRIPTOR +// Application return pointer to descriptor +uint8_t const * tud_descriptor_device_cb(void) { + return usb_desc_dev; +} - .hid_report = - { - .generic = hid_report_descriptor, - .boot_keyboard = NULL, - .boot_mouse = NULL - } -}; +// Invoked when received GET CONFIGURATION DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_descriptor_configuration_cb(uint8_t index) { + (void) index; // for multiple configurations + return usb_desc_cfg; +} + + +// Invoked when received GET HID REPORT DESCRIPTOR +// Application return pointer to descriptor +// Descriptor contents must exist long enough for transfer to complete +uint8_t const * tud_hid_descriptor_report_cb(void) { + return hid_report_descriptor; +} + +// Invoked when received GET STRING DESCRIPTOR request +// Application return pointer to descriptor, whose contents must exist long enough for transfer to complete +uint16_t const* tud_descriptor_string_cb(uint8_t index) { + uint8_t const max_index = sizeof(string_desc_arr)/sizeof(string_desc_arr[0]); + return (index < max_index) ? string_desc_arr[index] : NULL; +} diff --git a/supervisor/shared/usb/usb_desc.h b/supervisor/shared/usb/usb_desc.h deleted file mode 100644 index 250147aa6..000000000 --- a/supervisor/shared/usb/usb_desc.h +++ /dev/null @@ -1,43 +0,0 @@ -/* - * This file is part of the MicroPython project, http://micropython.org/ - * - * The MIT License (MIT) - * - * Copyright (c) 2018 hathach for Adafruit Industries - * - * Permission is hereby granted, free of charge, to any person obtaining a copy - * of this software and associated documentation files (the "Software"), to deal - * in the Software without restriction, including without limitation the rights - * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell - * copies of the Software, and to permit persons to whom the Software is - * furnished to do so, subject to the following conditions: - * - * The above copyright notice and this permission notice shall be included in - * all copies or substantial portions of the Software. - * - * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR - * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, - * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE - * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER - * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, - * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN - * THE SOFTWARE. - */ - -#ifndef USB_DESC_H_ -#define USB_DESC_H_ - -#include "lib/tinyusb/src/tusb.h" - -#ifdef __cplusplus - extern "C" { -#endif - -// Descriptors set used by tinyusb stack -extern tud_desc_set_t tud_desc_set; - -#ifdef __cplusplus - } -#endif - -#endif /* USB_DESC_H_ */ diff --git a/supervisor/shared/usb/usb_msc_flash.c b/supervisor/shared/usb/usb_msc_flash.c index f6f0fa9c3..205cb7b50 100644 --- a/supervisor/shared/usb/usb_msc_flash.c +++ b/supervisor/shared/usb/usb_msc_flash.c @@ -59,59 +59,18 @@ static fs_user_mount_t* get_vfs(int lun) { } // Callback invoked when received an SCSI command not in built-in list below -// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, MODE_SENSE6, REQUEST_SENSE +// - READ_CAPACITY10, READ_FORMAT_CAPACITY, INQUIRY, TEST_UNIT_READY, START_STOP_UNIT, MODE_SENSE6, REQUEST_SENSE // - READ10 and WRITE10 have their own callbacks int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer, uint16_t bufsize) { const void* response = NULL; int32_t resplen = 0; switch ( scsi_cmd[0] ) { - case SCSI_CMD_TEST_UNIT_READY: - // Command that host uses to check our readiness before sending other commands - resplen = 0; - if (lun > 1) { - resplen = -1; - } else { - fs_user_mount_t* current_mount = get_vfs(lun); - if (current_mount == NULL) { - resplen = -1; - } - if (ejected[lun]) { - resplen = -1; - } - } - break; - case SCSI_CMD_PREVENT_ALLOW_MEDIUM_REMOVAL: // Host is about to read/write etc ... better not to disconnect disk resplen = 0; break; - case SCSI_CMD_START_STOP_UNIT: - { - // Host try to eject/safe remove/poweroff us. We could safely disconnect with disk storage, or go into lower power - const scsi_start_stop_unit_t* start_stop = (const scsi_start_stop_unit_t*) scsi_cmd; - // Start bit = 0 : low power mode, if load_eject = 1 : unmount disk storage as well - // Start bit = 1 : Ready mode, if load_eject = 1 : mount disk storage - resplen = 0; - if (start_stop->load_eject == 1) { - if (lun > 1) { - resplen = -1; - } else { - fs_user_mount_t* current_mount = get_vfs(lun); - if (current_mount == NULL) { - resplen = -1; - } - if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) { - resplen = -1; - } else { - ejected[lun] = true; - } - } - } - } - break; - default: // Set Sense = Invalid Command Operation tud_msc_set_sense(lun, SCSI_SENSE_ILLEGAL_REQUEST, 0x20, 0x00); @@ -127,7 +86,7 @@ int32_t tud_msc_scsi_cb (uint8_t lun, const uint8_t scsi_cmd[16], void* buffer, } // copy response to stack's buffer if any - if ( response && resplen ) { + if ( response && (resplen > 0) ) { memcpy(buffer, response, resplen); } @@ -206,3 +165,54 @@ void tud_msc_write10_complete_cb (uint8_t lun) { // This write is complete, start the autoreload clock. autoreload_start(); } + +// Invoked when received SCSI_CMD_INQUIRY +// Application fill vendor id, product id and revision with string up to 8, 16, 4 characters respectively +void tud_msc_inquiry_cb(uint8_t lun, uint8_t vendor_id[8], uint8_t product_id[16], uint8_t product_rev[4]) { + (void) lun; + + memcpy(vendor_id , CFG_TUD_MSC_VENDOR , strlen(CFG_TUD_MSC_VENDOR)); + memcpy(product_id , CFG_TUD_MSC_PRODUCT , strlen(CFG_TUD_MSC_PRODUCT)); + memcpy(product_rev, CFG_TUD_MSC_PRODUCT_REV, strlen(CFG_TUD_MSC_PRODUCT_REV)); +} + +// Invoked when received Test Unit Ready command. +// return true allowing host to read/write this LUN e.g SD card inserted +bool tud_msc_test_unit_ready_cb(uint8_t lun) { + if (lun > 1) { + return false; + } + + fs_user_mount_t* current_mount = get_vfs(lun); + if (current_mount == NULL) { + return false; + } + if (ejected[lun]) { + return false; + } + + return true; +} + +// Invoked when received Start Stop Unit command +// - Start = 0 : stopped power mode, if load_eject = 1 : unload disk storage +// - Start = 1 : active mode, if load_eject = 1 : load disk storage +bool tud_msc_start_stop_cb(uint8_t lun, uint8_t power_condition, bool start, bool load_eject) { + if (load_eject) { + if (lun > 1) { + return false; + } else { + fs_user_mount_t* current_mount = get_vfs(lun); + if (current_mount == NULL) { + return false; + } + if (disk_ioctl(current_mount, CTRL_SYNC, NULL) != RES_OK) { + return false; + } else { + ejected[lun] = true; + } + } + } + + return true; +} -- cgit v1.2.3 From 5d03cda01ade5a81d3096f9af0ac5e89a31be099 Mon Sep 17 00:00:00 2001 From: hathach Date: Wed, 24 Jul 2019 16:49:13 +0700 Subject: whitespace --- supervisor/shared/usb/usb_desc.c | 1 - 1 file changed, 1 deletion(-) (limited to 'supervisor') diff --git a/supervisor/shared/usb/usb_desc.c b/supervisor/shared/usb/usb_desc.c index 48f2da3d3..9b333d6fe 100644 --- a/supervisor/shared/usb/usb_desc.c +++ b/supervisor/shared/usb/usb_desc.c @@ -43,7 +43,6 @@ uint8_t const * tud_descriptor_configuration_cb(uint8_t index) { return usb_desc_cfg; } - // Invoked when received GET HID REPORT DESCRIPTOR // Application return pointer to descriptor // Descriptor contents must exist long enough for transfer to complete -- cgit v1.2.3