aboutsummaryrefslogtreecommitdiff
path: root/modules/ls0xx/ls0xx.c
blob: 01a02c66f7de56370172f9ec4e99d9e40c59acc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
// Copyright Pablo Martinez (@elpekenin) <[email protected]>
// SPDX-License-Identifier: GPL-2.0-or-later

#include "ls0xx.h"

#include "ls0xx_opcodes.h"
#include "qp_comms_spi.h"
#include "qp_surface_internal.h"
#include "spi_master.h"

typedef struct {
  painter_driver_t base; // must be first, so it can be cast to/from the
                         // painter_device_t* type
  qp_comms_spi_config_t spi_config;
  surface_painter_device_t surface;
} ls0xx_painter_device_t;

// need custom comms vtable for inverted CS logic
bool inverted_cs_init(painter_device_t device) {
  painter_driver_t *driver = (painter_driver_t *)device;
  qp_comms_spi_config_t *comms_config =
      (qp_comms_spi_config_t *)driver->comms_config;

  // Initialize the SPI peripheral
  spi_init();

  // Set up CS as output high
  gpio_set_pin_output(comms_config->chip_select_pin);
  gpio_write_pin_low(comms_config->chip_select_pin);

  return true;
}

bool inverted_cs_start(painter_device_t device) {
  painter_driver_t *driver = (painter_driver_t *)device;
  qp_comms_spi_config_t *comms_config =
      (qp_comms_spi_config_t *)driver->comms_config;

  spi_start_config_t config = (spi_start_config_t){
      .slave_pin = comms_config->chip_select_pin,
      .lsb_first = comms_config->lsb_first,
      .mode = comms_config->mode,
      .divisor = comms_config->divisor,
      .cs_active_low = false,
  };

  return spi_start_extended(&config);
}

const painter_comms_vtable_t spi_comms_inverted_cs_vtable = {
    .comms_init = inverted_cs_init,
    .comms_start = inverted_cs_start,
    .comms_send = qp_comms_spi_send_data,
    .comms_stop = qp_comms_spi_stop,
};

ls0xx_painter_device_t ls0xx_device_t_drivers[LS0XX_NUM_DEVICES] = {0};

__attribute__((weak)) bool qp_ls0xx_init(painter_device_t device,
                                         painter_rotation_t rotation) {
  ls0xx_painter_device_t *ls0xx = (ls0xx_painter_device_t *)device;
  painter_driver_t *surface = (painter_driver_t *)&ls0xx->surface;

  // Change the surface geometry based on the panel rotation
  if (rotation == QP_ROTATION_90 || rotation == QP_ROTATION_270) {
    surface->panel_width = ls0xx->base.panel_height;
    surface->panel_height = ls0xx->base.panel_width;
  } else {
    surface->panel_width = ls0xx->base.panel_width;
    surface->panel_height = ls0xx->base.panel_height;
  }

  // Init the internal surface
  if (!surface->driver_vtable->init(surface, QP_ROTATION_0)) {
    qp_dprintf("Failed to init internal surface in qp_ls0xx_init\n");
    return false;
  }

  ls0xx->base.rotation = rotation;

  writePinHigh(ls0xx->spi_config.chip_select_pin);
  const uint8_t ls0xx_init_sequence[] = {LS0XX_CLEAR, 0};
  spi_transmit(ls0xx_init_sequence, ARRAY_SIZE(ls0xx_init_sequence));
  writePinLow(ls0xx->spi_config.chip_select_pin);

  return true;
}

bool qp_ls0xx_passthru_power(painter_device_t device, bool power_on) {
  // No-op
  return true;
}

bool qp_ls0xx_passthru_clear(painter_device_t device) {
  // Just re-init the display
  painter_driver_t *driver = (painter_driver_t *)device;
  return qp_ls0xx_init(device, driver->rotation);
}

bool qp_ls0xx_flush(painter_device_t device) {
  ls0xx_painter_device_t *ls0xx = (ls0xx_painter_device_t *)device;
  surface_painter_device_t *surface = &(ls0xx->surface);
  surface_dirty_data_t dirty = surface->dirty;

  if (!dirty.is_dirty) {
    // nothing to be sent
    return true;
  }

  // find out dirty area. update is done on **complete** horizontal lines,
  // which means that if device is sideways, we need to check l/r instead
  uint16_t top = 0;
  uint16_t bottom = 0;
  switch (ls0xx->base.rotation) {
  case QP_ROTATION_0:
    top = dirty.t;
    bottom = dirty.b;
    break;

  // TODO: Check whether this are backwards
  case QP_ROTATION_90:
    top = dirty.l;
    bottom = dirty.r;
    break;

  case QP_ROTATION_180:
    top = dirty.b;
    bottom = dirty.t;
    break;

  case QP_ROTATION_270:
    top = dirty.r;
    bottom = dirty.l;
    break;
  }

  // bytes sent for each row's data
  uint8_t bytes_per_line =
      (ls0xx->base.panel_width + 7) / 8 * ls0xx->base.native_bits_per_pixel;
  // offset used to access such data
  uint16_t buffer_offset = top * bytes_per_line;

  // start sending
  writePinHigh(ls0xx->spi_config.chip_select_pin);
  uint8_t cmd = LS0XX_WRITE | LS0XX_VCOM;
  spi_transmit(&cmd, 1);

  // dummy data for alignment, value doesn't matter
  uint8_t dummy = 0;

  // iterate over the lines
  for (uint8_t i = 0; i < bottom + 1 - top; ++i) {
    // set y-pos (counts from 1, needs the `+1`)
    uint8_t n_line = bitrev(i + top + 1);
    spi_transmit(&n_line, 1);

    // send data
    spi_transmit(&surface->u8buffer[buffer_offset], bytes_per_line);
    buffer_offset += bytes_per_line;

    spi_transmit(&dummy, 1);
  }

  spi_transmit(&dummy, 1);
  writePinLow(ls0xx->spi_config.chip_select_pin);

  // clear surface's dirty area, no API to prevent extra prints
  surface->base.driver_vtable->flush(surface);

  return true;
}

bool qp_ls0xx_passthru_viewport(painter_device_t device, uint16_t left,
                                uint16_t top, uint16_t right, uint16_t bottom) {
  ls0xx_painter_device_t *ls0xx = (ls0xx_painter_device_t *)device;
  painter_driver_t *surface = (painter_driver_t *)&ls0xx->surface;

  return surface->driver_vtable->viewport(surface, left, top, right, bottom);
}

bool qp_ls0xx_passthru_pixdata(painter_device_t device, const void *pixel_data,
                               uint32_t native_pixel_count) {
  ls0xx_painter_device_t *ls0xx = (ls0xx_painter_device_t *)device;
  painter_driver_t *surface = (painter_driver_t *)&ls0xx->surface;

  return surface->driver_vtable->pixdata(surface, pixel_data,
                                         native_pixel_count);
}

bool qp_ls0xx_passthru_palette_convert(painter_device_t device,
                                       int16_t palette_size,
                                       qp_pixel_t *palette) {
  ls0xx_painter_device_t *ls0xx = (ls0xx_painter_device_t *)device;
  painter_driver_t *surface = (painter_driver_t *)&ls0xx->surface;

  return surface->driver_vtable->palette_convert(surface, palette_size,
                                                 palette);
}

bool qp_ls0xx_passthru_append_pixels(painter_device_t device,
                                     uint8_t *target_buffer,
                                     qp_pixel_t *palette, uint32_t pixel_offset,
                                     uint32_t pixel_count,
                                     uint8_t *palette_indices) {
  ls0xx_painter_device_t *ls0xx = (ls0xx_painter_device_t *)device;
  painter_driver_t *surface = (painter_driver_t *)&ls0xx->surface;

  return surface->driver_vtable->append_pixels(surface, target_buffer, palette,
                                               pixel_offset, pixel_count,
                                               palette_indices);
}

bool qp_ls0xx_passthru_append_pixdata(painter_device_t device,
                                      uint8_t *target_buffer,
                                      uint32_t pixdata_offset,
                                      uint8_t pixdata_byte) {
  ls0xx_painter_device_t *ls0xx = (ls0xx_painter_device_t *)device;
  painter_driver_t *surface = (painter_driver_t *)&ls0xx->surface;

  return surface->driver_vtable->append_pixdata(surface, target_buffer,
                                                pixdata_offset, pixdata_byte);
}

const painter_driver_vtable_t ls0xx_driver_vtable = {
    .init = qp_ls0xx_init,
    .power = qp_ls0xx_passthru_power,
    .clear = qp_ls0xx_passthru_clear,
    .flush = qp_ls0xx_flush,
    .pixdata = qp_ls0xx_passthru_pixdata,
    .viewport = qp_ls0xx_passthru_viewport,
    .palette_convert = qp_ls0xx_passthru_palette_convert,
    .append_pixels = qp_ls0xx_passthru_append_pixels,
    .append_pixdata = qp_ls0xx_passthru_append_pixdata,
};

painter_device_t qp_ls0xx_device_t_make_spi_device(uint16_t panel_width,
                                                   uint16_t panel_height,
                                                   pin_t chip_select_pin,
                                                   uint16_t spi_divisor,
                                                   int spi_mode, void *buf) {
  for (uint32_t i = 0; i < LS0XX_NUM_DEVICES; ++i) {
    ls0xx_painter_device_t *driver = &ls0xx_device_t_drivers[i];
    if (!driver->base.driver_vtable) {
      painter_device_t surface = qp_make_mono1bpp_surface_advanced(
          &driver->surface, 1, panel_width, panel_height, buf);
      if (!surface) {
        return NULL;
      }
      driver->base.driver_vtable =
          (const painter_driver_vtable_t *)&ls0xx_driver_vtable;
      driver->base.comms_vtable =
          (const painter_comms_vtable_t *)&spi_comms_inverted_cs_vtable;
      driver->base.native_bits_per_pixel = 1;
      driver->base.panel_width = panel_width;
      driver->base.panel_height = panel_height;
      driver->base.rotation = QP_ROTATION_0;
      driver->base.offset_x = 0;
      driver->base.offset_y = 0;

      // SPI and other pin configuration
      driver->base.comms_config = &driver->spi_config;
      driver->spi_config.chip_select_pin = chip_select_pin;
      driver->spi_config.lsb_first = false;
      driver->spi_config.divisor = spi_divisor;
      driver->spi_config.mode = spi_mode;

      if (!qp_internal_register_device((painter_device_t)driver)) {
        memset(driver, 0, sizeof(ls0xx_painter_device_t));
        return NULL;
      }

      return (painter_device_t)driver;
    }
  }

  return NULL;
}