summaryrefslogtreecommitdiff
path: root/src/drivers/include/display.hpp
blob: 8157c3a50189d48a671ed00496ef0e180ee8823e (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
#pragma once

#include <cstdint>
#include <memory>

#include "driver/spi_master.h"
#include "lvgl/lvgl.h"
#include "result.hpp"

#include "display_init.hpp"
#include "gpio_expander.hpp"

namespace drivers {

/*
 * LVGL display driver for ST77XX family displays.
 */
class Display {
 public:
  /*
   * Creates the display driver, and resets and reinitialises the display
   * over SPI. This never fails, since unfortunately these display don't give
   * us back any kind of signal to tell us we're actually using them correctly.
   */
  static auto create(GpioExpander* expander,
                     const displays::InitialisationData& init_data)
      -> std::unique_ptr<Display>;

  Display(GpioExpander* gpio, spi_device_handle_t handle);
  ~Display();

  /* Driver callback invoked by LVGL when there is new data to display. */
  void OnLvglFlush(lv_disp_drv_t* disp_drv,
                   const lv_area_t* area,
                   lv_color_t* color_map);

 private:
  GpioExpander* gpio_;
  spi_device_handle_t handle_;

  lv_disp_draw_buf_t buffers_;
  lv_disp_drv_t driver_;
  lv_disp_t* display_ = nullptr;

  enum TransactionType {
    COMMAND = 0,
    DATA = 1,
  };

  void SendInitialisationSequence(const uint8_t* data);

  void SendCommandWithData(uint8_t command, const uint8_t* data, size_t length);
  void SendCmd(const uint8_t* data, size_t length);
  void SendData(const uint8_t* data, size_t length);

  void SendTransaction(TransactionType type,
                       const uint8_t* data,
                       size_t length);
};

}  // namespace drivers