Links2004 / arduinoVNC

a VNC Client for Arduino based on rfbproto
GNU General Public License v2.0
307 stars 58 forks source link

About ST7796 transplantation? #23

Closed modi12jin closed 3 years ago

modi12jin commented 3 years ago

Use this library

https://github.com/moononournation/Arduino_GFX

VNC_ST7796.cpp

#include "VNC_config.h"

#include "VNC.h"

#include <SPI.h>
#include <Arduino_GFX_Library.h>

#include "VNC_ST7796.h"

ST7796VNC::ST7796VNC(Arduino_ESP32SPI *bus) : 
Arduino_ST7796(bus, 22 /* RST */, 3 /* rotation */, false /* IPS */){
}

bool ST7796VNC::hasCopyRect(void) {
    return false;
}

uint32_t ST7796VNC::getHeight(void) {
    return 320;
}

uint32_t ST7796VNC::getWidth(void) {
    return 480;
}

void ST7796VNC::draw_area(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t * data) {
    Arduino_ST7796::draw16bitBeRGBBitmap(x, y, (uint16_t *)data, w, h);
}

void ST7796VNC::draw_rect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t color) {
    Arduino_ST7796::fillRect(x, y, w, h, ((((color)&0xff) << 8) | (((color) >> 8))));
}

void ST7796VNC::copy_rect(uint32_t src_x, uint32_t src_y, uint32_t dest_x, uint32_t dest_y, uint32_t w, uint32_t h) {
}

void ST7796VNC::area_update_start(uint32_t x, uint32_t y, uint32_t w, uint32_t h) {
    Arduino_ST7796::setAddrWindow(x, y, w, h);
}

void ST7796VNC::area_update_data(char * data, uint32_t pixel) {
    Arduino_ST7796::writePixels((uint16_t *)data, pixel);
}

void ST7796VNC::area_update_end(void) {
    Arduino_ST7796::endWrite();
}

void ST7796VNC::vnc_options_override(dfb_vnc_options * opt) {
    opt->client.bigendian = 1;
}

VNC_ST7796.h

#ifndef VNC_ST7796_H_
#define VNC_ST7796_H_

#include "VNC_config.h"
#include <Arduino_GFX_Library.h>
#include "VNC_ST7796.h"
#include "VNC.h"

class ST7796VNC : public VNCdisplay,public Arduino_ST7796{
  public:
    ST7796VNC(Arduino_ESP32SPI *bus);

    bool hasCopyRect(void);

    uint32_t getHeight(void);
    uint32_t getWidth(void);

    void draw_area(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint8_t * data);

    void draw_rect(uint32_t x, uint32_t y, uint32_t w, uint32_t h, uint16_t color);

    void copy_rect(uint32_t src_x, uint32_t src_y, uint32_t dest_x, uint32_t dest_y, uint32_t w, uint32_t h);

    void area_update_start(uint32_t x, uint32_t y, uint32_t w, uint32_t h);
    void area_update_data(char * data, uint32_t pixel);
    void area_update_end(void);

    void vnc_options_override(dfb_vnc_options * opt);

  private:
    uint32_t area_x, area_y, area_w, area_h;
};

#endif

main.cpp

#include <Arduino.h>
#include <WiFi.h>
#include <SPI.h>

#include <Arduino_GFX_Library.h>
#include <VNC_ST7796.h>
#include <VNC.h>

const char * vnc_ip = "192.168.0.111";
const uint16_t vnc_port = 5900;
const char * vnc_pass = "12345678";

const char* ssid = "FAST_BA74";
const char* password = "xxxxxxxx";

#define TFT_BL 23
Arduino_ESP32SPI *bus = new Arduino_ESP32SPI(21 /* DC */, 15 /* CS */, 14 /* SCK */, 13 /* MOSI */,-1);
ST7796VNC tft = ST7796VNC(bus);

arduinoVNC vnc = arduinoVNC(&tft);

void setup(void) {
    tft.begin();
    Serial.begin(115200);

    tft.setRotation(3);
    tft.setTextSize(2);
    tft.setCursor(0, 0);
    tft.println("Ready");

    pinMode(TFT_BL, OUTPUT);
    digitalWrite(TFT_BL, HIGH);

    Serial.setDebugOutput(true);
    Serial.println();
    Serial.println();
    Serial.println();

    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);
    while(WiFi.status() != WL_CONNECTED) {
        delay(500);
        Serial.print(".");
    }
    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println(F("[SETUP] VNC..."));

    vnc.begin(vnc_ip, vnc_port);
    //vnc.setPassword(vnc_pass); // check for vnc server settings
}

void loop() {
    if(WiFi.status() != WL_CONNECTED) {
        vnc.reconnect();
        delay(100);
    } else {
        vnc.loop();
        if(!vnc.connected()) {
            delay(5000);
        }
    }
}

1

modi12jin commented 3 years ago

Based on the above efforts, add touch

Add library

https://github.com/DustinWatts/FT6236

main.cpp

#include <Arduino.h>
#include <WiFi.h>
#include <SPI.h>

#include <Arduino_GFX_Library.h>
#include <FT6236.h>
#include <VNC_ST7796.h>
#include <VNC.h>

const char * vnc_ip = "192.168.0.107";
const uint16_t vnc_port = 5900;
const char * vnc_pass = "12345678";

const char* ssid = "FAST_BA74";
const char* password = "XXXXXXXXX";

#define TFT_BL 23
Arduino_ESP32SPI *bus = new Arduino_ESP32SPI(21 /* DC */, 15 /* CS */, 14 /* SCK */, 13 /* MOSI */,-1);
ST7796VNC tft = ST7796VNC(bus);

arduinoVNC vnc = arduinoVNC(&tft);

FT6236 ts = FT6236();

void TFTnoWifi(void) {
    tft.fillScreen(BLACK);
    tft.setCursor(0, ((tft.getHeight() / 2) - (5 * 8)));
    tft.setTextColor(RED);
    tft.setTextSize(5);
    tft.println("NO WIFI!");
    tft.setTextSize(2);
    tft.println();
}

void TFTnoVNC(void) {
    tft.fillScreen(BLACK);
    tft.setCursor(0, ((tft.getHeight() / 2) - (4 * 8)));
    tft.setTextColor(GREEN);
    tft.setTextSize(4);
    tft.println("connect VNC");
    tft.setTextSize(2);
    tft.println();
    tft.print(vnc_ip);
    tft.print(":");
    tft.println(vnc_port);
}

void setup(void) {
    //Serial.begin(115200);
    //Serial.setDebugOutput(true);

    Serial.println();
    Serial.println();

    //LCD屏幕初始化
    tft.begin(); /* TFT init */
    tft.setRotation(1);//为横屏方向
    tft.height();
    //显示屏背光
    ledcSetup(10, 5000/*freq*/, 10 /*resolution*/);
    ledcAttachPin(TFT_BL, 10);
    analogReadResolution(10);
    ledcWrite(10,300);

    //begin(40, SDA, SCL) for custom pins
    if (!ts.begin(40,18,19)){
        Serial.println("Unable to start the capacitive touchscreen.");
    }

    tft.fillScreen(BLUE);

    // We start by connecting to a WiFi network
    Serial.println();
    Serial.println();
    Serial.print("Connecting to ");
    Serial.println(ssid);

    WiFi.begin(ssid, password);
    TFTnoWifi();
    while(WiFi.status() != WL_CONNECTED) {
      delay(500);
      Serial.print(".");
    }
    TFTnoVNC();

    Serial.println("");
    Serial.println("WiFi connected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());

    Serial.println(F("[SETUP] VNC..."));

    vnc.begin(vnc_ip, vnc_port);
    vnc.setPassword(vnc_pass); // optional
}

void loop() {
    if(WiFi.status() != WL_CONNECTED) {
        vnc.reconnect();
        TFTnoWifi();
        delay(100);
    } else {
        if(vnc.connected()) {
            int16_t touchX, touchY;
            TS_Point p = ts.getPoint();
            touchX=480-p.y;
            touchY=p.x;
            static unsigned long lastUpdateP;
            static unsigned long lastUpdateR;
            static uint16_t lx, ly;
            if(ts.touched())
            {
                if((millis() - lastUpdateP) > 20) {
                    vnc.mouseEvent(touchX, touchY, 0b001);
                    lx = touchX;
                    ly = touchY;
                    lastUpdateP = millis();
                    //Serial.printf("[Touch] press: 1 X: %d Y: %d\n", touchX, touchY);
                }
                lastUpdateR = 0;
            }else{
                if((millis() - lastUpdateR) > 20) {
                    vnc.mouseEvent(lx, ly, 0b000);
                    lastUpdateR = millis();
                    //Serial.printf("[Touch] press: 0 X: %d Y: %d\n", lx, ly);
                }
                lastUpdateP = 0;
            }
        }
        vnc.loop();
        if(!vnc.connected()) {
            TFTnoVNC();
            delay(5000);
        }
    }
}

https://user-images.githubusercontent.com/40233017/128015026-d96f2faa-6e19-41f4-813f-82e20681178e.mp4