summaryrefslogtreecommitdiff
path: root/src/drivers/include/display.hpp
blob: 23bbbab96e344df3ee82b25249ab06b39478db15 (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
/*
 * Copyright 2023 jacqueline <me@jacqueline.id.au>
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#pragma once

#include <stdint.h>
#include <cstdint>
#include <memory>

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

#include "display_init.hpp"
#include "gpios.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(IGpios* expander,
                     const displays::InitialisationData& init_data) -> Display*;

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

  auto SetDisplayOn(bool) -> void;

  /* 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);

  // Not copyable or movable.
  Display(const Display&) = delete;
  Display& operator=(const Display&) = delete;

 private:
  IGpios* gpio_;
  spi_device_handle_t handle_;

  std::unique_ptr<tasks::Worker> worker_task_;

  bool display_on_;
  uint32_t brightness_;

  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