libpanel.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * Shared library for the Python UI: the panel core and the simulator behind
  3. * an opaque handle, so Python never has to mirror the C structures.
  4. *
  5. * Transports: i2c-dev (the caller opens and checks the bus, then passes the
  6. * file descriptor) or the simulator. All calls return 0 or -errno.
  7. */
  8. #include <errno.h>
  9. #include <linux/i2c-dev.h>
  10. #include <linux/i2c.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <sys/ioctl.h>
  14. #include <unistd.h>
  15. #include "s1d13700_core.h"
  16. #include "s1d13700_sim.h"
  17. #define PANEL_API_VERSION 1
  18. #define TX_CAP 1025 /* the driver's message limit */
  19. struct panel {
  20. struct s1d_config cfg;
  21. struct s1d_panel core;
  22. struct s1d_sim sim;
  23. bool use_sim;
  24. int fd;
  25. unsigned int addr;
  26. uint8_t shadow[S1D_VRAM_SIZE];
  27. uint8_t next[S1D_VRAM_SIZE];
  28. uint8_t tx[TX_CAP];
  29. };
  30. static int i2c_xfer(void *ctx, const uint8_t *buf, size_t len)
  31. {
  32. struct panel *p = ctx;
  33. struct i2c_msg msg = { (uint16_t)p->addr, 0, (uint16_t)len, (uint8_t *)buf };
  34. struct i2c_rdwr_ioctl_data data = { &msg, 1 };
  35. return ioctl(p->fd, I2C_RDWR, &data) < 0 ? -errno : 0;
  36. }
  37. static void i2c_delay(void *ctx, unsigned int ms)
  38. {
  39. (void)ctx;
  40. usleep(ms * 1000);
  41. }
  42. static int sim_xfer(void *ctx, const uint8_t *buf, size_t len)
  43. {
  44. s1d_sim_i2c_write(&((struct panel *)ctx)->sim, buf, len);
  45. return 0;
  46. }
  47. static void sim_delay(void *ctx, unsigned int ms)
  48. {
  49. (void)ctx;
  50. (void)ms;
  51. }
  52. static const struct s1d_ops i2c_ops = { i2c_xfer, i2c_delay };
  53. static const struct s1d_ops sim_ops = { sim_xfer, sim_delay };
  54. int panel_api_version(void)
  55. {
  56. return PANEL_API_VERSION;
  57. }
  58. /* fd < 0 selects the simulator. Returns NULL for a configuration the core rejects. */
  59. struct panel *panel_new(int fd, unsigned int addr, unsigned int width,
  60. unsigned int height, unsigned int bpp, unsigned int tcr,
  61. unsigned int tx_cap)
  62. {
  63. struct panel *p = calloc(1, sizeof(*p));
  64. if (!p)
  65. return NULL;
  66. p->cfg = (struct s1d_config){ width, height, bpp, tcr };
  67. p->fd = fd;
  68. p->addr = addr;
  69. p->use_sim = fd < 0;
  70. if (p->use_sim)
  71. s1d_sim_init(&p->sim);
  72. if (!tx_cap || tx_cap > TX_CAP)
  73. tx_cap = TX_CAP;
  74. if (s1d_panel_setup(&p->core, &p->cfg, p->use_sim ? &sim_ops : &i2c_ops, p,
  75. p->shadow, p->tx, tx_cap)) {
  76. free(p);
  77. return NULL;
  78. }
  79. return p;
  80. }
  81. void panel_free(struct panel *p)
  82. {
  83. free(p);
  84. }
  85. int panel_init(struct panel *p)
  86. {
  87. return s1d_panel_init(&p->core);
  88. }
  89. /*
  90. * Convert the changed XRGB8888 rectangle into the pending native frame and
  91. * bring the panel's rows [y0, y1) up to date with it.
  92. */
  93. int panel_update(struct panel *p, const void *pixels, unsigned int pitch,
  94. unsigned int x0, unsigned int y0, unsigned int x1, unsigned int y1)
  95. {
  96. s1d_convert_xrgb8888(&p->cfg, pixels, pitch, p->next, x0, y0, x1, y1);
  97. return s1d_panel_update(&p->core, p->next, y0, y1);
  98. }
  99. int panel_display(struct panel *p, int on)
  100. {
  101. return s1d_panel_display(&p->core, on);
  102. }
  103. int panel_backlight(struct panel *p, int on)
  104. {
  105. return s1d_panel_backlight(&p->core, on);
  106. }
  107. /* writes, data_bytes, seeks, dir_changes, i2c_msgs, i2c_bytes */
  108. void panel_stats(const struct panel *p, unsigned long out[6])
  109. {
  110. const struct s1d_stats *st = &p->core.stats;
  111. out[0] = st->writes;
  112. out[1] = st->data_bytes;
  113. out[2] = st->seeks;
  114. out[3] = st->dir_changes;
  115. out[4] = st->i2c_msgs;
  116. out[5] = st->i2c_bytes;
  117. }
  118. void panel_stats_reset(struct panel *p)
  119. {
  120. memset(&p->core.stats, 0, sizeof(p->core.stats));
  121. }
  122. double panel_bus_us(const struct panel *p, unsigned int scl_hz)
  123. {
  124. return (double)s1d_stats_bus_us(&p->core.stats, scl_hz);
  125. }
  126. /* Simulator only: levels gets width * height bytes. Returns 1 if shown. */
  127. int panel_sim_render(const struct panel *p, uint8_t *levels)
  128. {
  129. if (!p->use_sim)
  130. return -EINVAL;
  131. return s1d_sim_render(&p->sim, p->cfg.width, p->cfg.height, levels);
  132. }
  133. int panel_sim_faults(const struct panel *p)
  134. {
  135. return p->use_sim ? (int)s1d_sim_fault_count(&p->sim) : 0;
  136. }