Advertisement
honey_the_codewitch

tft_driver.hpp

Dec 14th, 2021
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.67 KB | None | 0 0
  1. #pragma once
  2. #include <Arduino.h>
  3. #include "tft_io.hpp"
  4. namespace arduino {
  5.     template<int8_t PinDC, int8_t PinRst, int8_t PinBL, typename Bus, uint8_t SoftResetCommand = 0x01>
  6.     struct tft_driver {
  7.         constexpr static const int8_t pin_dc = PinDC;
  8.         constexpr static const int8_t pin_rst = PinRst;
  9.         constexpr static const int8_t pin_bl = PinBL;
  10.         constexpr static const uint8_t swrst_command = SoftResetCommand;
  11.         using bus = Bus;
  12.         static bool initialize() {
  13.             if(bus::initialize()) {
  14.                 if(pin_dc>-1) {
  15.                     pinMode(pin_dc, OUTPUT);
  16.                     digitalWrite(pin_dc, HIGH);
  17.                 }
  18.                 if(pin_rst>-1) {
  19.                     pinMode(pin_rst, OUTPUT);
  20.                     // TODO: is this necessary? Should it be LOW?
  21.                     digitalWrite(pin_rst, HIGH);
  22.                 }
  23.                 bus::end_write();
  24.                 reset();
  25.                 return true;
  26.             }
  27.             return false;
  28.         }
  29.         static void deinitialize() {
  30.             bus::deinitialize();
  31.         }
  32.         static void reset() {
  33.             if(pin_rst > -1) {
  34.                 digitalWrite(pin_rst, HIGH);
  35.                 delay(5);
  36.                 digitalWrite(pin_rst, LOW);
  37.                 delay(20);
  38.                 digitalWrite(pin_rst, HIGH);
  39.             } else {
  40.                 send_command(swrst_command);
  41.             }
  42.             // wait for reset to complete
  43.             delay(150);
  44.  
  45.         }
  46.         static void send_command(uint8_t command) {
  47.             bus::begin_write();
  48.             dc_command();
  49.             bus::write_raw8(command);
  50.             dc_data();
  51.             bus::end_write();
  52.         }
  53.         static void send_data8(uint8_t data) {
  54.             bus::begin_write();
  55.             dc_data();
  56.             bus::write_raw8(data);
  57.             bus::cs_low(); // allow more hold time for low VDI rail
  58.             bus::end_write();
  59.         }
  60.         static uint8_t recv_command8(uint8_t command, uint8_t index) {
  61.             uint8_t result = 0;
  62.             send_command(command);
  63.             bus::direction(INPUT);
  64.             bus::cs_low();
  65.             while(index--) result = bus::read_raw8();
  66.             bus::direction(OUTPUT);
  67.             bus::cs_high();
  68.             return result;
  69.         }
  70.         static uint8_t recv_command16(uint8_t command, uint8_t index) {
  71.             uint8_t result = recv_command8(command, index) << 8;
  72.             result |= recv_command8(command, index);
  73.             return result;
  74.         }
  75.         static uint8_t recv_command32(uint8_t command, uint8_t index) {
  76.             uint8_t result = recv_command8(command, index) << 24;
  77.             result |= recv_command8(command, index) << 16;
  78.             result |= recv_command8(command, index) << 8;
  79.             result |= recv_command8(command, index);
  80.             return result;
  81.         }
  82.         inline static void dc_command() FORCE_INLINE {
  83. #ifdef OPTIMIZE_ESP32
  84.             if(pin_dc>31) {
  85.                 GPIO.out1_w1tc.val = (1 << ((pin_dc - 32)&31));
  86.             } else if(pin_dc>-1) {
  87.                 GPIO.out_w1tc = (1 << ((pin_dc - 32)&31));
  88.             }
  89. #else // !OPTIMIZE_ESP32
  90.             digitalWrite(pin_dc,LOW);
  91. #endif // !OPTIMIZE_ESP32
  92.         }
  93.         inline static void dc_data() FORCE_INLINE {
  94. #ifdef OPTIMIZE_ESP32
  95.             if(pin_dc>31) {
  96.                 GPIO.out1_w1ts.val = (1 << ((pin_dc - 32)&31));
  97.             } else if(pin_dc>-1) {
  98.                 GPIO.out_w1ts = (1 << ((pin_dc - 32)&31));
  99.             }
  100. #else // !OPTIMIZE_ESP32
  101.             digitalWrite(pin_dc,HIGH);
  102. #endif // !OPTIMIZE_ESP32
  103.         }
  104.     };
  105. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement