Add Panel-Aware Local OLED Display Service

This commit is contained in:
2026-08-29 14:37:44 +02:00
parent ec9ca5e6d2
commit 4aa5ca80da
10 changed files with 900 additions and 427 deletions
+148 -411
View File
@@ -1,5 +1,5 @@
/* SPDX-License-Identifier: GPL-3.0-only */
/* Phase 7A bounded SSD1315-compatible OLED and button diagnostics. */
/* Phase 7A diagnostics built on the Phase 7B local display service. */
#include "local_ui_hw_test.h"
@@ -12,23 +12,11 @@
#include "board_pins.h"
#include "driver/gpio.h"
#include "driver/i2c_master.h"
#include "esp_err.h"
#include "esp_timer.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#define DISPLAY_WIDTH 128U
#define DISPLAY_HEIGHT 64U
#define DISPLAY_PAGE_COUNT (DISPLAY_HEIGHT / 8U)
#define DISPLAY_FRAMEBUFFER_SIZE (DISPLAY_WIDTH * DISPLAY_PAGE_COUNT)
#define DISPLAY_I2C_SPEED_HZ 100000U
#define DISPLAY_I2C_TIMEOUT_MS 250
#define DISPLAY_PROBE_TIMEOUT_MS 50
#define DISPLAY_SCAN_TIMEOUT_MS 10
#define DISPLAY_DEFAULT_CONTRAST 127U
#define DISPLAY_COMMAND_CAPACITY 32U
#define DISPLAY_DATA_CHUNK_SIZE 128U
#include "local_display.h"
#define BUTTON_POLL_MS 10U
#define BUTTON_DEBOUNCE_MS 30U
@@ -36,9 +24,6 @@
#define BUTTON_TEST_DEFAULT_SECONDS 10U
#define BUTTON_TEST_MAX_SECONDS 30U
/* The tested module uses 7-bit 0x3c; 0x3d is the common alternate strap. */
static const uint8_t s_expected_addresses[] = {0x3cU, 0x3dU};
typedef struct {
const char *name;
gpio_num_t gpio;
@@ -61,16 +46,8 @@ static const button_definition_t s_buttons[] = {
{.name = "next", .gpio = LOCAL_UI_BUTTON_NEXT_GPIO},
};
static i2c_master_bus_handle_t s_bus;
static i2c_master_dev_handle_t s_display;
static uint8_t s_framebuffer[DISPLAY_FRAMEBUFFER_SIZE];
static esp_err_t s_initialization_error = ESP_ERR_INVALID_STATE;
static bool s_bus_ready;
static bool s_buttons_ready;
static bool s_panel_initialized;
static uint8_t s_display_address;
static uint8_t s_contrast = DISPLAY_DEFAULT_CONTRAST;
static bool s_inverted;
static esp_err_t s_button_initialization_error = ESP_ERR_INVALID_STATE;
static TickType_t milliseconds_to_ticks(uint32_t milliseconds)
{
@@ -94,7 +71,6 @@ static bool parse_unsigned(const char *text,
parsed < minimum || parsed > maximum) {
return false;
}
*value = parsed;
return true;
}
@@ -111,7 +87,6 @@ static bool parse_display_address(const char *text, uint8_t *address)
if (errno != 0 || end == text || *end != '\0') {
return false;
}
if (parsed == 0x3cU || parsed == 0x78U || parsed == 0x79U) {
*address = 0x3cU;
return true;
@@ -140,7 +115,6 @@ static esp_err_t configure_buttons(void)
.pull_down_en = GPIO_PULLDOWN_DISABLE,
.intr_type = GPIO_INTR_DISABLE,
};
esp_err_t error = gpio_config(&config);
if (error == ESP_OK) {
s_buttons_ready = true;
@@ -148,264 +122,11 @@ static esp_err_t configure_buttons(void)
return error;
}
static esp_err_t configure_i2c_bus(void)
static void print_address(uint8_t address)
{
const i2c_master_bus_config_t config = {
.i2c_port = LOCAL_UI_I2C_PORT,
.sda_io_num = LOCAL_UI_DISPLAY_SDA_GPIO,
.scl_io_num = LOCAL_UI_DISPLAY_SCL_GPIO,
.clk_source = I2C_CLK_SRC_DEFAULT,
.glitch_ignore_cnt = 7,
.flags.enable_internal_pullup = false,
};
esp_err_t error = i2c_new_master_bus(&config, &s_bus);
if (error == ESP_OK) {
s_bus_ready = true;
}
return error;
}
static esp_err_t probe_address(uint8_t address)
{
if (!s_bus_ready) {
return s_initialization_error == ESP_OK ? ESP_ERR_INVALID_STATE
: s_initialization_error;
}
return i2c_master_probe(s_bus, address, DISPLAY_PROBE_TIMEOUT_MS);
}
static esp_err_t select_display(uint8_t address)
{
esp_err_t error = probe_address(address);
if (error != ESP_OK) {
return error;
}
if (s_display != NULL && s_display_address == address) {
return ESP_OK;
}
if (s_display != NULL) {
error = i2c_master_bus_rm_device(s_display);
if (error != ESP_OK) {
return error;
}
s_display = NULL;
s_panel_initialized = false;
}
const i2c_device_config_t config = {
.dev_addr_length = I2C_ADDR_BIT_LEN_7,
.device_address = address,
.scl_speed_hz = DISPLAY_I2C_SPEED_HZ,
};
error = i2c_master_bus_add_device(s_bus, &config, &s_display);
if (error == ESP_OK) {
s_display_address = address;
}
return error;
}
static esp_err_t probe_expected_addresses(uint8_t *selected_address)
{
if (!s_bus_ready) {
return s_initialization_error == ESP_OK ? ESP_ERR_INVALID_STATE
: s_initialization_error;
}
uint32_t found = 0U;
uint8_t first = 0U;
for (size_t index = 0U;
index < sizeof(s_expected_addresses) / sizeof(s_expected_addresses[0]);
++index) {
uint8_t address = s_expected_addresses[index];
esp_err_t error = probe_address(address);
if (error == ESP_OK) {
printf("OLED response at 7-bit 0x%02x (8-bit 0x%02x write / 0x%02x read).\n",
address, (unsigned int)(address << 1U),
(unsigned int)((address << 1U) | 1U));
if (found == 0U) {
first = address;
}
++found;
} else {
printf("No OLED response at 7-bit 0x%02x (%s).\n",
address, esp_err_to_name(error));
}
}
if (found == 0U) {
printf("No display found at the expected 7-bit 0x3c/0x3d addresses. Check 3.3 V power, ground, SDA/SCL order, and pull-ups.\n");
return ESP_ERR_NOT_FOUND;
}
if (found > 1U) {
printf("Multiple expected addresses responded; selecting 7-bit 0x%02x for diagnostics.\n",
first);
} else {
printf("Selected display address 7-bit 0x%02x.\n", first);
}
if (selected_address != NULL) {
*selected_address = first;
}
return ESP_OK;
}
static esp_err_t display_send_commands(const uint8_t *commands, size_t count)
{
if (s_display == NULL || commands == NULL || count == 0U ||
count > DISPLAY_COMMAND_CAPACITY) {
return ESP_ERR_INVALID_ARG;
}
uint8_t transfer[DISPLAY_COMMAND_CAPACITY + 1U];
transfer[0] = 0x00U;
memcpy(&transfer[1], commands, count);
return i2c_master_transmit(s_display, transfer, count + 1U,
DISPLAY_I2C_TIMEOUT_MS);
}
static esp_err_t display_send_command(uint8_t command)
{
return display_send_commands(&command, 1U);
}
static esp_err_t display_refresh(void)
{
if (!s_panel_initialized || s_display == NULL) {
return ESP_ERR_INVALID_STATE;
}
const uint8_t address_commands[] = {
0x21U, 0x00U, (uint8_t)(DISPLAY_WIDTH - 1U),
0x22U, 0x00U, (uint8_t)(DISPLAY_PAGE_COUNT - 1U),
};
esp_err_t error = display_send_commands(
address_commands, sizeof(address_commands));
if (error != ESP_OK) {
return error;
}
uint8_t transfer[DISPLAY_DATA_CHUNK_SIZE + 1U];
transfer[0] = 0x40U;
for (size_t offset = 0U; offset < DISPLAY_FRAMEBUFFER_SIZE;
offset += DISPLAY_DATA_CHUNK_SIZE) {
size_t chunk = DISPLAY_FRAMEBUFFER_SIZE - offset;
if (chunk > DISPLAY_DATA_CHUNK_SIZE) {
chunk = DISPLAY_DATA_CHUNK_SIZE;
}
memcpy(&transfer[1], &s_framebuffer[offset], chunk);
error = i2c_master_transmit(s_display, transfer, chunk + 1U,
DISPLAY_I2C_TIMEOUT_MS);
if (error != ESP_OK) {
return error;
}
}
return ESP_OK;
}
static esp_err_t display_initialize(uint8_t address)
{
/* A failed transaction must never leave a partially initialized state. */
s_panel_initialized = false;
esp_err_t error = select_display(address);
if (error != ESP_OK) {
return error;
}
const uint8_t initialization[] = {
0xaeU, /* Display off while geometry is configured. */
0xd5U, 0x80U, /* Default oscillator and divide ratio. */
0xa8U, 0x3fU, /* 1/64 multiplex. */
0xd3U, 0x00U, /* No display offset. */
0x40U, /* Display start line zero. */
0x8dU, 0x14U, /* Enable the module's internal charge pump. */
0x20U, 0x00U, /* Horizontal addressing mode. */
0xa1U, /* Segment remap for common four-pin modules. */
0xc8U, /* Descending COM scan direction. */
0xdaU, 0x12U, /* Alternative COM pin configuration. */
0x81U, DISPLAY_DEFAULT_CONTRAST,
0xd9U, 0xf1U, /* Charge-pump precharge period. */
0xdbU, 0x40U, /* VCOMH deselect level. */
0xa4U, /* Use display RAM, not all-pixels-on mode. */
0xa6U, /* Normal, non-inverted pixels. */
0x2eU, /* Disable any prior scrolling mode. */
};
error = display_send_commands(initialization, sizeof(initialization));
if (error != ESP_OK) {
return error;
}
s_panel_initialized = true;
s_contrast = DISPLAY_DEFAULT_CONTRAST;
s_inverted = false;
memset(s_framebuffer, 0, sizeof(s_framebuffer));
error = display_refresh();
if (error == ESP_OK) {
error = display_send_command(0xafU);
}
if (error != ESP_OK) {
s_panel_initialized = false;
return error;
}
vTaskDelay(milliseconds_to_ticks(20U));
return ESP_OK;
}
static void framebuffer_set_pixel(uint32_t x, uint32_t y)
{
if (x >= DISPLAY_WIDTH || y >= DISPLAY_HEIGHT) {
return;
}
size_t index = (size_t)(y / 8U) * DISPLAY_WIDTH + x;
s_framebuffer[index] |= (uint8_t)(1U << (y & 7U));
}
static void framebuffer_draw_pattern(const char *name)
{
memset(s_framebuffer, 0, sizeof(s_framebuffer));
if (strcmp(name, "fill") == 0) {
memset(s_framebuffer, 0xff, sizeof(s_framebuffer));
return;
}
if (strcmp(name, "checker") == 0) {
for (uint32_t y = 0U; y < DISPLAY_HEIGHT; ++y) {
for (uint32_t x = 0U; x < DISPLAY_WIDTH; ++x) {
if (((x + y) & 1U) == 0U) {
framebuffer_set_pixel(x, y);
}
}
}
return;
}
if (strcmp(name, "grid") == 0) {
for (uint32_t y = 0U; y < DISPLAY_HEIGHT; ++y) {
for (uint32_t x = 0U; x < DISPLAY_WIDTH; ++x) {
if ((x % 8U) == 0U || (y % 8U) == 0U) {
framebuffer_set_pixel(x, y);
}
}
}
return;
}
if (strcmp(name, "corners") == 0) {
for (uint32_t x = 0U; x < DISPLAY_WIDTH; ++x) {
framebuffer_set_pixel(x, 0U);
framebuffer_set_pixel(x, DISPLAY_HEIGHT - 1U);
}
for (uint32_t y = 0U; y < DISPLAY_HEIGHT; ++y) {
framebuffer_set_pixel(0U, y);
framebuffer_set_pixel(DISPLAY_WIDTH - 1U, y);
}
for (uint32_t offset = 0U; offset < 8U; ++offset) {
framebuffer_set_pixel(offset, offset);
framebuffer_set_pixel(DISPLAY_WIDTH - 1U - offset, offset);
framebuffer_set_pixel(offset, DISPLAY_HEIGHT - 1U - offset);
framebuffer_set_pixel(DISPLAY_WIDTH - 1U - offset,
DISPLAY_HEIGHT - 1U - offset);
}
}
printf("7-bit 0x%02x (8-bit 0x%02x write / 0x%02x read)",
address, (unsigned int)(address << 1U),
(unsigned int)((address << 1U) | 1U));
}
static void print_display_usage(void)
@@ -415,7 +136,7 @@ static void print_display_usage(void)
printf(" debug display scan --force\n");
printf(" debug display init [0x3c|0x3d|0x78|0x79|0x7a|0x7b]\n");
printf(" debug display off\n");
printf(" debug display pattern <clear|fill|checker|grid|corners>\n");
printf(" debug display pattern <clear|fill|checker|grid|corners|layout>\n");
printf(" debug display row <0..63>\n");
printf(" debug display contrast <0..255>\n");
printf(" debug display invert <on|off>\n");
@@ -429,34 +150,31 @@ static int command_display_status(int argc, char **argv)
return 1;
}
printf("Local UI diagnostics: init=%s bus=%s buttons=%s last-init=%s\n",
s_initialization_error == ESP_OK ? "ok" : "failed",
s_bus_ready ? "ready" : "unavailable",
s_buttons_ready ? "ready" : "unavailable",
esp_err_to_name(s_initialization_error));
if (s_display_address == 0U) {
printf("OLED: address=none initialized=%s speed=%u Hz contrast=%u inverted=%s\n",
s_panel_initialized ? "yes" : "no",
(unsigned int)DISPLAY_I2C_SPEED_HZ,
(unsigned int)s_contrast,
s_inverted ? "yes" : "no");
} else {
printf("OLED: 7-bit=0x%02x 8-bit=0x%02x/0x%02x initialized=%s speed=%u Hz contrast=%u inverted=%s\n",
s_display_address,
(unsigned int)(s_display_address << 1U),
(unsigned int)((s_display_address << 1U) | 1U),
s_panel_initialized ? "yes" : "no",
(unsigned int)DISPLAY_I2C_SPEED_HZ,
(unsigned int)s_contrast,
s_inverted ? "yes" : "no");
local_display_snapshot_t snapshot;
esp_err_t error = local_display_get_snapshot(&snapshot);
if (error != ESP_OK) {
return report_error("Display status", error);
}
printf("Local display: bus=%s initialized=%s last-error=%s dirty-pages=0x%02x\n",
snapshot.bus_ready ? "ready" : "unavailable",
snapshot.initialized ? "yes" : "no",
esp_err_to_name(snapshot.last_error),
snapshot.dirty_page_mask);
if (snapshot.address_7bit != 0U) {
printf("OLED: ");
print_address(snapshot.address_7bit);
printf(" contrast=%u inverted=%s\n", (unsigned int)snapshot.contrast,
snapshot.inverted ? "yes" : "no");
} else {
printf("OLED: no selected address\n");
}
printf("Panels: status=128x16 rows 0..15; content=128x48 rows 16..63; physical black divider between them\n");
printf("Pins: SDA=%d level=%d SCL=%d level=%d; buttons previous=%d select=%d next=%d\n",
LOCAL_UI_DISPLAY_SDA_GPIO, gpio_get_level(LOCAL_UI_DISPLAY_SDA_GPIO),
LOCAL_UI_DISPLAY_SCL_GPIO, gpio_get_level(LOCAL_UI_DISPLAY_SCL_GPIO),
LOCAL_UI_BUTTON_PREVIOUS_GPIO,
LOCAL_UI_BUTTON_SELECT_GPIO,
LOCAL_UI_BUTTON_PREVIOUS_GPIO, LOCAL_UI_BUTTON_SELECT_GPIO,
LOCAL_UI_BUTTON_NEXT_GPIO);
return s_bus_ready && s_buttons_ready ? 0 : 1;
return snapshot.bus_ready && s_buttons_ready ? 0 : 1;
}
static int command_display_probe(int argc, char **argv)
@@ -468,13 +186,25 @@ static int command_display_probe(int argc, char **argv)
}
uint8_t address = 0U;
esp_err_t error = probe_expected_addresses(&address);
esp_err_t error = local_display_probe_expected(&address);
if (error != ESP_OK) {
printf("No OLED response at expected 7-bit addresses 0x3c or 0x3d.\n");
return report_error("Display probe", error);
}
printf("OLED response at ");
print_address(address);
printf(".\n");
return 0;
}
static void scan_print_callback(uint8_t address, void *context)
{
(void)context;
printf(" response: ");
print_address(address);
printf("\n");
}
static int command_display_scan(int argc, char **argv)
{
if (argc != 2 || strcmp(argv[1], "--force") != 0) {
@@ -482,30 +212,13 @@ static int command_display_scan(int argc, char **argv)
print_display_usage();
return 1;
}
if (!s_bus_ready) {
return report_error("Display scan", s_initialization_error);
}
printf("Scanning usable 7-bit I2C addresses 0x08..0x77 at %u Hz.\n",
(unsigned int)DISPLAY_I2C_SPEED_HZ);
uint32_t found = 0U;
for (uint16_t address = 0x08U; address <= 0x77U; ++address) {
esp_err_t error = i2c_master_probe(s_bus, address, DISPLAY_SCAN_TIMEOUT_MS);
if (error == ESP_OK) {
printf(" response: 7-bit 0x%02x (8-bit 0x%02x write / 0x%02x read)\n",
(unsigned int)address,
(unsigned int)(address << 1U),
(unsigned int)((address << 1U) | 1U));
++found;
} else if (error != ESP_ERR_NOT_FOUND && error != ESP_ERR_TIMEOUT) {
printf(" 7-bit 0x%02x: %s\n", (unsigned int)address,
esp_err_to_name(error));
}
vTaskDelay(1U);
}
size_t found = 0U;
printf("Scanning usable 7-bit I2C addresses 0x08..0x77 at 100000 Hz.\n");
esp_err_t error = local_display_scan(scan_print_callback, NULL, &found);
printf("I2C scan complete: %u responding address%s.\n", (unsigned int)found,
found == 1U ? "" : "es");
return found == 0U ? 1 : 0;
return error == ESP_OK ? 0 : report_error("Display scan", error);
}
static int command_display_init(int argc, char **argv)
@@ -516,26 +229,27 @@ static int command_display_init(int argc, char **argv)
}
uint8_t address = 0U;
esp_err_t error;
if (argc == 2) {
if (!parse_display_address(argv[1], &address)) {
printf("Display address must be 7-bit 0x3c/0x3d or their 8-bit write/read forms.\n");
return 1;
}
error = local_display_start_at(address);
} else {
esp_err_t error = probe_expected_addresses(&address);
if (error != ESP_OK) {
return report_error("Display probe", error);
error = local_display_start();
if (error == ESP_OK) {
local_display_snapshot_t snapshot;
error = local_display_get_snapshot(&snapshot);
address = snapshot.address_7bit;
}
}
esp_err_t error = display_initialize(address);
if (error != ESP_OK) {
return report_error("Display initialization", error);
}
printf("SSD1315-compatible 128x64 display initialized at 7-bit 0x%02x (8-bit 0x%02x/0x%02x), %u Hz.\n",
address, (unsigned int)(address << 1U),
(unsigned int)((address << 1U) | 1U),
(unsigned int)DISPLAY_I2C_SPEED_HZ);
printf("SSD1315-compatible 128x64 display initialized at ");
print_address(address);
printf(".\n");
return 0;
}
@@ -546,15 +260,10 @@ static int command_display_off(int argc, char **argv)
print_display_usage();
return 1;
}
if (!s_panel_initialized) {
return report_error("Display off", ESP_ERR_INVALID_STATE);
}
esp_err_t error = display_send_command(0xaeU);
esp_err_t error = local_display_stop();
if (error != ESP_OK) {
return report_error("Display off", error);
}
s_panel_initialized = false;
printf("Display switched off; run 'debug display init' to reinitialize it.\n");
return 0;
}
@@ -563,7 +272,66 @@ static bool pattern_name_valid(const char *name)
{
return strcmp(name, "clear") == 0 || strcmp(name, "fill") == 0 ||
strcmp(name, "checker") == 0 || strcmp(name, "grid") == 0 ||
strcmp(name, "corners") == 0;
strcmp(name, "corners") == 0 || strcmp(name, "layout") == 0;
}
static void set_global_pixel(uint8_t x, uint8_t y, bool on)
{
if (y < LOCAL_DISPLAY_STATUS_HEIGHT) {
local_display_frame_set_pixel(LOCAL_DISPLAY_PANEL_STATUS, x, y, on);
} else {
local_display_frame_set_pixel(LOCAL_DISPLAY_PANEL_CONTENT, x,
(uint8_t)(y - LOCAL_DISPLAY_STATUS_HEIGHT), on);
}
}
static void draw_pattern(const char *name)
{
local_display_frame_clear_all();
if (strcmp(name, "clear") == 0) {
return;
}
if (strcmp(name, "fill") == 0) {
for (uint8_t y = 0U; y < LOCAL_DISPLAY_HEIGHT; ++y) {
for (uint8_t x = 0U; x < LOCAL_DISPLAY_WIDTH; ++x) {
set_global_pixel(x, y, true);
}
}
return;
}
if (strcmp(name, "checker") == 0 || strcmp(name, "grid") == 0) {
bool checker = strcmp(name, "checker") == 0;
for (uint8_t y = 0U; y < LOCAL_DISPLAY_HEIGHT; ++y) {
for (uint8_t x = 0U; x < LOCAL_DISPLAY_WIDTH; ++x) {
bool on = checker ? (((x + y) & 1U) == 0U)
: ((x % 8U) == 0U || (y % 8U) == 0U);
if (on) {
set_global_pixel(x, y, true);
}
}
}
return;
}
if (strcmp(name, "corners") == 0) {
for (uint8_t x = 0U; x < LOCAL_DISPLAY_WIDTH; ++x) {
set_global_pixel(x, 0U, true);
set_global_pixel(x, LOCAL_DISPLAY_HEIGHT - 1U, true);
}
for (uint8_t y = 0U; y < LOCAL_DISPLAY_HEIGHT; ++y) {
set_global_pixel(0U, y, true);
set_global_pixel(LOCAL_DISPLAY_WIDTH - 1U, y, true);
}
return;
}
local_display_frame_clear(LOCAL_DISPLAY_PANEL_STATUS);
local_display_frame_clear(LOCAL_DISPLAY_PANEL_CONTENT);
local_display_frame_draw_text(LOCAL_DISPLAY_PANEL_STATUS, 0U, 0U, "SER OK");
local_display_frame_draw_text(LOCAL_DISPLAY_PANEL_STATUS, 0U, 8U, "WR NONE");
local_display_frame_draw_text(LOCAL_DISPLAY_PANEL_CONTENT, 0U, 0U, "DISPLAY DRIVER");
local_display_frame_draw_text(LOCAL_DISPLAY_PANEL_CONTENT, 0U, 8U, "STATUS 16 PX");
local_display_frame_draw_text(LOCAL_DISPLAY_PANEL_CONTENT, 0U, 24U, "CONTENT 48 PX");
local_display_frame_draw_text(LOCAL_DISPLAY_PANEL_CONTENT, 0U, 40U, "SEPARATE PANELS");
}
static int command_display_pattern(int argc, char **argv)
@@ -572,12 +340,12 @@ static int command_display_pattern(int argc, char **argv)
print_display_usage();
return 1;
}
if (!s_panel_initialized) {
return report_error("Display pattern", ESP_ERR_INVALID_STATE);
esp_err_t error = local_display_frame_begin();
if (error != ESP_OK) {
return report_error("Display pattern", error);
}
framebuffer_draw_pattern(argv[1]);
esp_err_t error = display_refresh();
draw_pattern(argv[1]);
error = local_display_frame_end();
if (error != ESP_OK) {
return report_error("Display pattern", error);
}
@@ -588,19 +356,19 @@ static int command_display_pattern(int argc, char **argv)
static int command_display_row(int argc, char **argv)
{
unsigned long row = 0U;
if (argc != 2 || !parse_unsigned(argv[1], 0U, DISPLAY_HEIGHT - 1U, &row)) {
if (argc != 2 || !parse_unsigned(argv[1], 0U, LOCAL_DISPLAY_HEIGHT - 1U, &row)) {
print_display_usage();
return 1;
}
if (!s_panel_initialized) {
return report_error("Display row", ESP_ERR_INVALID_STATE);
esp_err_t error = local_display_frame_begin();
if (error != ESP_OK) {
return report_error("Display row", error);
}
memset(s_framebuffer, 0, sizeof(s_framebuffer));
for (uint32_t x = 0U; x < DISPLAY_WIDTH; ++x) {
framebuffer_set_pixel(x, (uint32_t)row);
local_display_frame_clear_all();
for (uint8_t x = 0U; x < LOCAL_DISPLAY_WIDTH; ++x) {
set_global_pixel(x, (uint8_t)row, true);
}
esp_err_t error = display_refresh();
error = local_display_frame_end();
if (error != ESP_OK) {
return report_error("Display row", error);
}
@@ -615,16 +383,10 @@ static int command_display_contrast(int argc, char **argv)
print_display_usage();
return 1;
}
if (!s_panel_initialized) {
return report_error("Display contrast", ESP_ERR_INVALID_STATE);
}
const uint8_t commands[] = {0x81U, (uint8_t)contrast};
esp_err_t error = display_send_commands(commands, sizeof(commands));
esp_err_t error = local_display_set_contrast((uint8_t)contrast);
if (error != ESP_OK) {
return report_error("Display contrast", error);
}
s_contrast = (uint8_t)contrast;
printf("Display contrast set to %lu.\n", contrast);
return 0;
}
@@ -635,17 +397,12 @@ static int command_display_invert(int argc, char **argv)
print_display_usage();
return 1;
}
if (!s_panel_initialized) {
return report_error("Display inversion", ESP_ERR_INVALID_STATE);
}
bool invert = strcmp(argv[1], "on") == 0;
esp_err_t error = display_send_command(invert ? 0xa7U : 0xa6U);
bool inverted = strcmp(argv[1], "on") == 0;
esp_err_t error = local_display_set_inverted(inverted);
if (error != ESP_OK) {
return report_error("Display inversion", error);
}
s_inverted = invert;
printf("Display inversion %s.\n", invert ? "enabled" : "disabled");
printf("Display inversion %s.\n", inverted ? "enabled" : "disabled");
return 0;
}
@@ -655,7 +412,6 @@ static int command_display(int argc, char **argv)
print_display_usage();
return argc < 2 || argc == 2 ? 0 : 1;
}
if (strcmp(argv[1], "status") == 0) {
return command_display_status(argc - 1, argv + 1);
}
@@ -704,17 +460,14 @@ static int command_buttons_status(int argc, char **argv)
return 1;
}
if (!s_buttons_ready) {
return report_error("Button status", s_initialization_error);
return report_error("Button status", s_button_initialization_error);
}
printf("Buttons are active-low with internal pull-ups:\n");
for (size_t index = 0U; index < sizeof(s_buttons) / sizeof(s_buttons[0]); ++index) {
int level = gpio_get_level(s_buttons[index].gpio);
printf(" %-14s GPIO%d level=%d %s\n",
s_buttons[index].name,
s_buttons[index].gpio,
level,
level == 0 ? "pressed" : "released");
printf(" %-14s GPIO%d level=%d %s\n", s_buttons[index].name,
s_buttons[index].gpio, level, level == 0 ? "pressed" : "released");
}
return 0;
}
@@ -723,33 +476,27 @@ static int command_buttons_test(int argc, char **argv)
{
unsigned long seconds = BUTTON_TEST_DEFAULT_SECONDS;
if (argc > 2 ||
(argc == 2 && !parse_unsigned(argv[1], 1U, BUTTON_TEST_MAX_SECONDS,
&seconds))) {
(argc == 2 && !parse_unsigned(argv[1], 1U, BUTTON_TEST_MAX_SECONDS, &seconds))) {
print_buttons_usage();
return 1;
}
if (!s_buttons_ready) {
return report_error("Button test", s_initialization_error);
return report_error("Button test", s_button_initialization_error);
}
button_test_state_t states[sizeof(s_buttons) / sizeof(s_buttons[0])];
int64_t now = esp_timer_get_time();
for (size_t index = 0U; index < sizeof(s_buttons) / sizeof(s_buttons[0]); ++index) {
int level = gpio_get_level(s_buttons[index].gpio);
states[index] = (button_test_state_t){
.raw_level = level,
/* Start released; a held-low input must pass through debounce. */
.raw_level = gpio_get_level(s_buttons[index].gpio),
.stable_level = 1,
.raw_changed_us = now,
.pressed_us = 0,
};
}
int64_t deadline = now + (int64_t)seconds * 1000000LL;
printf("Testing buttons for %lu second%s; short press each button and hold one for at least %u ms.\n",
seconds, seconds == 1U ? "" : "s",
(unsigned int)BUTTON_LONG_PRESS_MS);
seconds, seconds == 1U ? "" : "s", (unsigned int)BUTTON_LONG_PRESS_MS);
while ((now = esp_timer_get_time()) < deadline) {
for (size_t index = 0U; index < sizeof(s_buttons) / sizeof(s_buttons[0]); ++index) {
button_test_state_t *state = &states[index];
@@ -758,7 +505,6 @@ static int command_buttons_test(int argc, char **argv)
state->raw_level = raw;
state->raw_changed_us = now;
}
if (raw != state->stable_level &&
now - state->raw_changed_us >= (int64_t)BUTTON_DEBOUNCE_MS * 1000LL) {
state->stable_level = raw;
@@ -768,23 +514,19 @@ static int command_buttons_test(int argc, char **argv)
state->long_reported = false;
printf("%-14s pressed\n", s_buttons[index].name);
} else {
int64_t duration_ms = state->pressed_us == 0
? 0
: (now - state->pressed_us) / 1000LL;
int64_t duration_ms = state->pressed_us == 0 ? 0 :
(now - state->pressed_us) / 1000LL;
if (!state->long_reported) {
++state->short_presses;
printf("%-14s short release after %lld ms\n",
s_buttons[index].name,
(long long)duration_ms);
s_buttons[index].name, (long long)duration_ms);
} else {
printf("%-14s released after %lld ms\n",
s_buttons[index].name,
(long long)duration_ms);
s_buttons[index].name, (long long)duration_ms);
}
state->pressed_us = 0;
}
}
if (state->stable_level == 0 && !state->long_reported &&
state->pressed_us != 0 &&
now - state->pressed_us >= (int64_t)BUTTON_LONG_PRESS_MS * 1000LL) {
@@ -801,8 +543,7 @@ static int command_buttons_test(int argc, char **argv)
for (size_t index = 0U; index < sizeof(s_buttons) / sizeof(s_buttons[0]); ++index) {
bool pressed = gpio_get_level(s_buttons[index].gpio) == 0;
printf(" %-14s short=%u long=%u transitions=%u final=%s\n",
s_buttons[index].name,
(unsigned int)states[index].short_presses,
s_buttons[index].name, (unsigned int)states[index].short_presses,
(unsigned int)states[index].long_presses,
(unsigned int)states[index].stable_transitions,
pressed ? "PRESSED" : "released");
@@ -826,7 +567,6 @@ static int command_buttons(int argc, char **argv)
if (strcmp(argv[1], "test") == 0) {
return command_buttons_test(argc - 1, argv + 1);
}
printf("Unknown button diagnostic '%s'.\n", argv[1]);
print_buttons_usage();
return 1;
@@ -834,14 +574,11 @@ static int command_buttons(int argc, char **argv)
esp_err_t local_ui_hw_test_init(void)
{
if (s_bus_ready || s_buttons_ready) {
if (s_buttons_ready) {
return ESP_ERR_INVALID_STATE;
}
esp_err_t button_error = configure_buttons();
esp_err_t bus_error = configure_i2c_bus();
s_initialization_error = button_error != ESP_OK ? button_error : bus_error;
return s_initialization_error;
s_button_initialization_error = configure_buttons();
return s_button_initialization_error;
}
int local_ui_hw_test_execute(int argc, char **argv)