/* * Shared library for the Python UI: the panel core and the simulator behind * an opaque handle, so Python never has to mirror the C structures. * * Transports: i2c-dev (the caller opens and checks the bus, then passes the * file descriptor) or the simulator. All calls return 0 or -errno. */ #include #include #include #include #include #include #include #include "s1d13700_core.h" #include "s1d13700_sim.h" #define PANEL_API_VERSION 1 #define TX_CAP 1025 /* the driver's message limit */ struct panel { struct s1d_config cfg; struct s1d_panel core; struct s1d_sim sim; bool use_sim; int fd; unsigned int addr; uint8_t shadow[S1D_VRAM_SIZE]; uint8_t next[S1D_VRAM_SIZE]; uint8_t tx[TX_CAP]; }; static int i2c_xfer(void *ctx, const uint8_t *buf, size_t len) { struct panel *p = ctx; struct i2c_msg msg = { (uint16_t)p->addr, 0, (uint16_t)len, (uint8_t *)buf }; struct i2c_rdwr_ioctl_data data = { &msg, 1 }; return ioctl(p->fd, I2C_RDWR, &data) < 0 ? -errno : 0; } static void i2c_delay(void *ctx, unsigned int ms) { (void)ctx; usleep(ms * 1000); } static int sim_xfer(void *ctx, const uint8_t *buf, size_t len) { s1d_sim_i2c_write(&((struct panel *)ctx)->sim, buf, len); return 0; } static void sim_delay(void *ctx, unsigned int ms) { (void)ctx; (void)ms; } static const struct s1d_ops i2c_ops = { i2c_xfer, i2c_delay }; static const struct s1d_ops sim_ops = { sim_xfer, sim_delay }; int panel_api_version(void) { return PANEL_API_VERSION; } /* fd < 0 selects the simulator. Returns NULL for a configuration the core rejects. */ struct panel *panel_new(int fd, unsigned int addr, unsigned int width, unsigned int height, unsigned int bpp, unsigned int tcr, unsigned int tx_cap) { struct panel *p = calloc(1, sizeof(*p)); if (!p) return NULL; p->cfg = (struct s1d_config){ width, height, bpp, tcr }; p->fd = fd; p->addr = addr; p->use_sim = fd < 0; if (p->use_sim) s1d_sim_init(&p->sim); if (!tx_cap || tx_cap > TX_CAP) tx_cap = TX_CAP; if (s1d_panel_setup(&p->core, &p->cfg, p->use_sim ? &sim_ops : &i2c_ops, p, p->shadow, p->tx, tx_cap)) { free(p); return NULL; } return p; } void panel_free(struct panel *p) { free(p); } int panel_init(struct panel *p) { return s1d_panel_init(&p->core); } /* * Convert the changed XRGB8888 rectangle into the pending native frame and * bring the panel's rows [y0, y1) up to date with it. */ int panel_update(struct panel *p, const void *pixels, unsigned int pitch, unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1) { s1d_convert_xrgb8888(&p->cfg, pixels, pitch, p->next, x0, y0, x1, y1); return s1d_panel_update(&p->core, p->next, y0, y1); } int panel_display(struct panel *p, int on) { return s1d_panel_display(&p->core, on); } int panel_backlight(struct panel *p, int on) { return s1d_panel_backlight(&p->core, on); } /* writes, data_bytes, seeks, dir_changes, i2c_msgs, i2c_bytes */ void panel_stats(const struct panel *p, unsigned long out[6]) { const struct s1d_stats *st = &p->core.stats; out[0] = st->writes; out[1] = st->data_bytes; out[2] = st->seeks; out[3] = st->dir_changes; out[4] = st->i2c_msgs; out[5] = st->i2c_bytes; } void panel_stats_reset(struct panel *p) { memset(&p->core.stats, 0, sizeof(p->core.stats)); } double panel_bus_us(const struct panel *p, unsigned int scl_hz) { return (double)s1d_stats_bus_us(&p->core.stats, scl_hz); } /* Simulator only: levels gets width * height bytes. Returns 1 if shown. */ int panel_sim_render(const struct panel *p, uint8_t *levels) { if (!p->use_sim) return -EINVAL; return s1d_sim_render(&p->sim, p->cfg.width, p->cfg.height, levels); } int panel_sim_faults(const struct panel *p) { return p->use_sim ? (int)s1d_sim_fault_count(&p->sim) : 0; }