编译错误"variable or field'my_disp_flush'declared void"

编译错误"variable or field'my_disp_flush'declared void"

#include <WiFi.h>
#include <esp_now.h>
#include <Ticker.h>

#include <TFT_eSPI.h>
#include "ui.h"
#include <ArduinoJson.h>
static const uint16_t screenWidth  = 240;
static const uint16_t screenHeight = 240;
u8_t ti=0,tongren_n,biaoqing_n=0,eye_n=0;
int shangyanpi[18]={-220,-180,-140,-100,-60,-20,0,0,0,0,0,0,-20,-60,-100,-140,-180,-220};


typedef union {
    struct {
        uint16_t blue  : 5;
        uint16_t green : 6;
        uint16_t red   : 5;
    } ch;
    uint16_t full;
} lv_color_t;

typedef struct{
    lv_color_t *buf;
    uint32_t buf_size;
 
} lv_disp_draw_buf_t;

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[ screenWidth * screenHeight / 10 ];
static lv_color_t buf2[ screenWidth * screenHeight / 10 ];


TFT_eSPI tft = TFT_eSPI(screenWidth, screenHeight);
int a_degx,a_degy;

// void eyeclick_a()
// {
// lv_obj_set_x(ui_bgu, 0);
// lv_obj_set_y(ui_bgu, -240);
// eyeclick_Animation(ui_bgu,0);
// //eyeclickdown_Animation(ui_bgd,0);
// delay(50);
// }

void my_disp_flush( lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p )
{
    uint32_t w = ( area->x2 - area->x1 + 1 );
    uint32_t h = ( area->y2 - area->y1 + 1 );
    // tft.setSwapBytes(true);
    // tft.pushImageDMA(area->x1, area->y1, w, h,(uint16_t *)&color_p->full);
    tft.startWrite();
    tft.setAddrWindow( area->x1, area->y1, w, h );
    tft.pushColors( ( uint16_t * )&color_p->full, w * h, true );
    tft.endWrite();

    lv_disp_flush_ready( disp );
}

/*Read the touchpad*/
void my_touchpad_read( lv_indev_drv_t * indev_driver, lv_indev_data_t * data )
{
    uint16_t touchX = 0, touchY = 0;

    bool touched = false;//tft.getTouch( &touchX, &touchY, 600 );

    if( !touched )
    {
        data->state = LV_INDEV_STATE_REL;
    }
    else
    {
        data->state = LV_INDEV_STATE_PR;

        /*Set the coordinates*/
        data->point.x = touchX;
        data->point.y = touchY;

        //Serial.print( "Data x " );
        //Serial.println( touchX );

        //Serial.print( "Data y " );
        //Serial.println( touchY );
    }
}

// 设置数据结构体
typedef struct Data_Package {
  byte j1PotX;  //左右摇杆
  byte j1PotY;
  byte j2PotX;
  byte j2PotY;
  bool buttonR1;  //左右按键
  bool buttonR2;
  bool j1Button;  //左右摇杆按键
  bool j2Button;
  bool buttonLB;  //左右扳机按键
  bool buttonRB;
  bool tSwitch1;  //拨动开关   
  byte roll;      //用于M5StackCore2或未来增加的陀螺仪功能
  byte pitch;
  bool buttonR3;
};
Data_Package data; //Create a variable with the above structure

// 数据接收回调函数
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
  memcpy(&data, incomingData, sizeof(data));
  //Serial.printf("x:%d y:%d ",data.j1PotX,data.j1PotY);
  //Serial.print("  ESP32 Board MAC Address:  ");
  //Serial.println(WiFi.macAddress());
  //Serial.println();
}
/**
 * Structure for holding display buffer information.
 */
 

void setup() {
  Serial.begin(9600);
  Serial.print("  ESP32 Board MAC Address:  ");
  Serial.println(WiFi.macAddress());
  // 初始化 ESP-NOW
  WiFi.mode(WIFI_STA);
  if (esp_now_init() != 0) {
    Serial.println("Error initializing ESP-NOW");
    return;
  }

  // 设置接收数据回调函数
  esp_now_register_recv_cb(OnDataRecv);

  lv_init();

  #if LV_USE_LOG != 0
    lv_log_register_print_cb( my_print ); /* register print function for debugging */
  #endif

    tft.begin();          /* TFT init */
    tft.setRotation( 0 ); /* Landscape orientation, flipped */

    lv_disp_draw_buf_init( &draw_buf, buf, buf2, screenWidth * screenHeight / 10 );

    /*Initialize the display*/
    static lv_disp_drv_t disp_drv;
    lv_disp_drv_init( &disp_drv );
    /*Change the following line to your display resolution*/
    disp_drv.hor_res = screenWidth;
    disp_drv.ver_res = screenHeight;
    disp_drv.flush_cb = my_disp_flush;
    disp_drv.draw_buf = &draw_buf;
    lv_disp_drv_register( &disp_drv );

    /*Initialize the (dummy) input device driver*/
    static lv_indev_drv_t indev_drv;
    lv_indev_drv_init( &indev_drv );
    indev_drv.type = LV_INDEV_TYPE_POINTER;
    indev_drv.read_cb = my_touchpad_read;
    lv_indev_drv_register( &indev_drv );

    
    // analogWrite(2, 102);//2048---10ms 205---1ms 0
    // analogWrite(3, 205);//45 
    // analogWrite(19, 306);//90
    // analogWrite(18, 409);//135
     analogWrite(2, 306);//90
     analogWrite(3, 306);//90
    // analogWrite(19, 306);//90
    // analogWrite(18, 306);//90
     data.j2PotX=127;
     data.j2PotY=127;
     analogWriteResolution(12);
     analogWriteFrequency(50);

    ui_init();
    
}

void loop() {
  lv_timer_handler(); /* let the GUI do its work */
  lv_obj_set_x(ui_changgui, data.j1PotX-127);
  lv_obj_set_y(ui_changgui, data.j1PotY-127);
  lv_obj_set_x(ui_huachi, data.j1PotX-127);
  lv_obj_set_y(ui_huachi, data.j1PotY-127);
  lv_obj_set_y(ui_sm, shangyanpi[eye_n]);
  lv_obj_set_y(ui_xm, -shangyanpi[eye_n]);
  a_degx=map(data.j2PotX,0,255,102,512);
  a_degy=map(data.j2PotY,0,255,102,512);
  analogWrite(2, a_degx);//90
  analogWrite(3, a_degy);//90
  delay(10);
  if(data.buttonLB==0)
  {
    if(eye_n<=17&&eye_n>=0)
    {
      eye_n+=1;
      //Serial.printf("eye_n=%d",eye_n);
      //Serial.println();
      //delay(5);
    }
    else
    {
      eye_n=0;
    }
  }
  // if(data.buttonRB==0)
  // {
  //   //eyeclick_a();
  // }
   if(data.buttonR2==0)
  {
    lv_obj_add_flag(ui_tongren30, LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_ADV_HITTEST);
    lv_obj_add_flag(ui_tongren60, LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_ADV_HITTEST);
    lv_obj_add_flag(ui_tongren90, LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_ADV_HITTEST);
    lv_obj_add_flag(ui_tongren155, LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_ADV_HITTEST);
    lv_obj_add_flag(ui_tongren255, LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_ADV_HITTEST);
    lv_obj_add_flag(ui_tongren300, LV_OBJ_FLAG_HIDDEN | LV_OBJ_FLAG_ADV_HITTEST);
    switch(tongren_n)
    {
      case 0:
      lv_obj_clear_flag(ui_tongren30, LV_OBJ_FLAG_HIDDEN);break;
      case 1:
      lv_obj_clear_flag(ui_tongren60, LV_OBJ_FLAG_HIDDEN);break;
      case 2:
      lv_obj_clear_flag(ui_tongren90, LV_OBJ_FLAG_HIDDEN);break;
      case 3:
      lv_obj_clear_flag(ui_tongren155, LV_OBJ_FLAG_HIDDEN);break;
      case 4:
      lv_obj_clear_flag(ui_tongren255, LV_OBJ_FLAG_HIDDEN);break;
      case 5:
      lv_obj_clear_flag(ui_tongren300, LV_OBJ_FLAG_HIDDEN);break;
      case 6:
      lv_obj_clear_flag(ui_tongren255, LV_OBJ_FLAG_HIDDEN);break;
      case 7:
      lv_obj_clear_flag(ui_tongren155, LV_OBJ_FLAG_HIDDEN);break;
      case 8:
      lv_obj_clear_flag(ui_tongren90, LV_OBJ_FLAG_HIDDEN);break;
      case 9:
      lv_obj_clear_flag(ui_tongren60, LV_OBJ_FLAG_HIDDEN);break;
    }
    if(tongren_n<=8&&tongren_n>=0)
    {
      tongren_n+=1;
    }
    else
    {
      tongren_n=0;
    }
    delay(10);
    //eyeclick_a();
  }
   if(data.buttonR1==0)
    {
      lv_obj_add_flag(ui_changgui, LV_OBJ_FLAG_HIDDEN);
    lv_obj_add_flag(ui_huachi, LV_OBJ_FLAG_HIDDEN);
    switch(biaoqing_n)
    {
      case 0:
      lv_obj_clear_flag(ui_changgui, LV_OBJ_FLAG_HIDDEN);break;
      case 1:
      lv_obj_clear_flag(ui_huachi, LV_OBJ_FLAG_HIDDEN);break;
    }
    if(biaoqing_n<=0&&biaoqing_n>=0)
    {
      biaoqing_n+=1;
    }
    else
    {
      biaoqing_n=0;
    }
    delay(200);
    }
}
}

来自GPT的回答:

在你的代码中,你定义了一个名为my_disp_flush的函数,但是你在函数声明中使用了void作为返回类型,而在函数实现中没有指定返回类型。这导致编译错误:"variable or field 'my_disp_flush' declared void"。

为了解决这个错误,你需要在函数声明和定义中都指定返回类型为void,以匹配函数的实际实现。

在你的代码中,将以下函数声明和定义的行改为使用void作为返回类型:

cpp
Copy code
void my_disp_flush(lv_disp_drv_t *disp, const lv_area_t *area, lv_color_t *color_p)
{
    // 函数实现代码
}

void my_touchpad_read(lv_indev_drv_t *indev_driver, lv_indev_data_t *data)
{
    // 函数实现代码
}
请注意,函数声明和定义的返回类型必须匹配,否则会导致编译错误。确保在你的代码中所有使用的函数声明和定义中都正确指定了返回类型,并重新编译你的代码。