[ESP8266/ESP32]ESP32 S2 模拟触摸屏作图 精华

4327浏览
查看: 4327|回复: 5

[ESP8266/ESP32] ESP32 S2 模拟触摸屏作图

[复制链接]
实验使用ESP32 S2 模触摸屏的方式一个心形和渐开线。下面是工作演示视频:


首先介绍的是“笛卡尔的爱情坐标公式”:心形函数r=a(1-sinθ),常被人当做表达爱和浪漫的一种方法。并且关于这个函数的由来有一个传播很广的故事。
笛卡尔在52岁时邂逅了当时瑞典的公主,当时他是公主的数学老师,不久公主就对笛卡尔产生了爱慕之情。然而,国王知道后,非常愤怒,将他流放回法国。在那里,笛卡尔给公主写的信都会被拦截。
ESP32 S2 模拟触摸屏作图图1
在笛卡尔寄出第十三封信后,笛卡尔永远离开了这个世界。在最后的一封信上,笛卡尔只写了一个公式:r=a(1-sinΘ)
国王也看不懂,于是把这封信交给了公主。这就是我们知道的极坐标下的心型函数。
这封情书至今保存在欧洲笛卡尔纪念馆里。【这一段是“读者”体,真实情况如果用震惊体来描述的话就是“天才数学家竟被女王惨无人道的折磨”参考1】
在这个公式中有2个变量:a和θ。我们首先使用网页版的绘图工具【参考2】验证一下这个公式:
ESP32 S2 模拟触摸屏作图图2
上述公式的参数方程形式为:
参数方程形式:
x= a*(2*sin(t)-sin(2*t))
y= a*(2*cos(t)-cos(2*t))
0<=t<=2*pi
ESP32 S2 模拟触摸屏作图图3
根据上述公式,设计代码如下:
ESP32 S2 模拟触摸屏作图图4

完整代码如下:

  1. #include "USB.h"
  2. #include "USBHID.h"
  3. USBHID HID;
  4. #define STEP 600
  5. int STARTX=5000;
  6. int STARTY=13000;
  7. int STARTR=5000;
  8. static const uint8_t report_descriptor[] = { // 8 TouchData
  9.   0x05, 0x0D,
  10.   0x09, 0x04,
  11.   0xA1, 0x01,
  12.   0x09, 0x22,
  13.   0xA1, 0x02,
  14.   0x09, 0x42,
  15.   0x15, 0x00,
  16.   0x25, 0x01,
  17.   0x75, 0x01,
  18.   0x95, 0x01,
  19.   0x81, 0x02,
  20.   0x09, 0x30,
  21.   0x25, 0x7F,
  22.   0x75, 0x07,
  23.   0x95, 0x01,
  24.   0x81, 0x02,
  25.   0x09, 0x51,
  26.   0x26, 0xFF, 0x00,
  27.   0x75, 0x08,
  28.   0x95, 0x01,
  29.   0x81, 0x02,
  30.   0x05, 0x01,
  31.   0x09, 0x30,
  32.   0x09, 0x31,
  33.   0x26, 0xFF, 0x7F,
  34.   0x65, 0x00,
  35.   0x75, 0x10,
  36.   0x95, 0x02,
  37.   0x81, 0x02,
  38.   0xC0,
  39.   0x05, 0x0D,
  40.   0x27, 0xFF, 0xFF, 0x00, 0x00,
  41.   0x75, 0x10,
  42.   0x95, 0x01,
  43.   0x09, 0x56,
  44.   0x81, 0x02,
  45.   0x09, 0x54,
  46.   0x25, 0x0A,
  47.   0x75, 0x08,
  48.   0x95, 0x01,
  49.   0x81, 0x02,
  50.   0x05, 0x0D,
  51.   0x09, 0x55,
  52.   0x25, 0x0A,
  53.   0x75, 0x08,
  54.   0x95, 0x01,
  55.   0xB1, 0x02,
  56.   0xC0,
  57. };
  58. class CustomHIDDevice: public USBHIDDevice {
  59.   public:
  60.     CustomHIDDevice(void) {
  61.       static bool initialized = false;
  62.       if (!initialized) {
  63.         initialized = true;
  64.         HID.addDevice(this, sizeof(report_descriptor));
  65.       }
  66.     }
  67.     uint16_t _onGetFeature(uint8_t report_id, uint8_t* buffer, uint16_t len)
  68.       {
  69.         buffer[0]=0x0a;
  70.         return 1;
  71.       }
  72.     void begin(void) {
  73.       HID.begin();
  74.     }
  75.     uint16_t _onGetDescriptor(uint8_t* buffer) {
  76.       memcpy(buffer, report_descriptor, sizeof(report_descriptor));
  77.       return sizeof(report_descriptor);
  78.     }
  79.     bool send(uint8_t * value) {
  80.       return HID.SendReport(0, value, 9);
  81.     }
  82. };
  83. CustomHIDDevice Device;
  84. const int buttonPin = 0;
  85. int previousButtonState = HIGH;
  86. uint8_t TouchData[9];
  87. void setup() {
  88.   Serial.begin(115200);
  89.   Serial.setDebugOutput(true);
  90.   Device.begin();
  91.   USB.begin();
  92. }
  93. void loop() {
  94.   if (HID.ready()) {
  95.     delay(1000);
  96.     Serial.println("Finger");
  97.     int iX,iY;
  98.     iX=STARTX+(STARTR*(2*sin(0)-sin(0)))/3;
  99.     iY=STARTY-(STARTR*(2*cos(0)-cos(0)));
  100.     TouchData[0] = 0x81;
  101.     TouchData[1] = 0x02;
  102.     TouchData[2] = (iX)&0xFF;
  103.     TouchData[3] = (iX)>>8&0xFF;
  104.     TouchData[4] = (iY)&0xFF;
  105.     TouchData[5] = (iY)>>8&0xFF;
  106.     TouchData[6] = (millis()*10)&0xFF; TouchData[7] = (millis()*10>>8)&0xFF;
  107.     TouchData[8] = 0x01;
  108.     Device.send(TouchData);
  109.     delay(20);
  110.     TouchData[0] = 0x81;
  111.     TouchData[1] = 0x02;
  112.     TouchData[2] = (iX+1)&0xFF;
  113.     TouchData[3] = (iX+1)>>8&0xFF;
  114.     TouchData[4] = (iY)&0xFF;
  115.     TouchData[5] = (iY)>>8&0xFF;
  116.     TouchData[6] = (millis()*10)&0xFF; TouchData[7] = (millis()*10>>8)&0xFF;
  117.     TouchData[8] = 0x01;
  118.     Device.send(TouchData);
  119.     delay(40);
  120.     // touch report
  121.     //  0: on/off + pressure
  122.     //  1: contact id
  123.     //  2: X lsb
  124.     //  3: X msb
  125.     //  4: Y lsb
  126.     //  5: Y msb
  127.     //  6: scan time lsb
  128.     //  7: scan time msb
  129.     //  8: contact count
  130.     for (int i=0;i<STEP+1;i++) {
  131.     TouchData[0] = 0x81; TouchData[1] = 0x01;
  132.     iX=STARTX+(STARTR*(2*sin(2*PI*i/STEP)-sin(2*2*PI*i/STEP)))/3;
  133.     iY=STARTY-(STARTR*(2*cos(2*PI*i/STEP)-cos(2*2*PI*i/STEP)));
  134.     Serial.print(iX);Serial.print("  ");Serial.println(iY);
  135.     TouchData[2] = ((int)(iX))&0xFF;
  136.     TouchData[3] = ((int)(iX))>>8&0xFF;
  137.     TouchData[4] = ((int)(iY))&0xFF;
  138.     TouchData[5] = ((int)(iY))>>8&0xFF;
  139.     TouchData[6] = (millis()*10)&0xFF; TouchData[7] = (millis()*10>>8)&0xFF;
  140.     TouchData[8] = 0x01;
  141.     Device.send(TouchData);
  142.     delay(20);
  143.     }
  144.     //每隔10秒
  145.     delay(2000);
  146.     STARTX=STARTX+6000;
  147.     STARTY=STARTY;
  148.    
  149.   }
  150. }
复制代码


渐开线的参数方程:
iX=r*cos(b)+r*b*sin(b)

iY=r*sin(b)-r*b*cos(b)

  1. #include "USB.h"
  2. #include "USBHID.h"
  3. USBHID HID;
  4. #define STEP 100
  5. int STARTX = 18000;
  6. int STARTY = 18000;
  7. int STARTR = 100;
  8. static const uint8_t report_descriptor[] = { // 8 TouchData
  9.   0x05, 0x0D,
  10.   0x09, 0x04,
  11.   0xA1, 0x01,
  12.   0x09, 0x22,
  13.   0xA1, 0x02,
  14.   0x09, 0x42,
  15.   0x15, 0x00,
  16.   0x25, 0x01,
  17.   0x75, 0x01,
  18.   0x95, 0x01,
  19.   0x81, 0x02,
  20.   0x09, 0x30,
  21.   0x25, 0x7F,
  22.   0x75, 0x07,
  23.   0x95, 0x01,
  24.   0x81, 0x02,
  25.   0x09, 0x51,
  26.   0x26, 0xFF, 0x00,
  27.   0x75, 0x08,
  28.   0x95, 0x01,
  29.   0x81, 0x02,
  30.   0x05, 0x01,
  31.   0x09, 0x30,
  32.   0x09, 0x31,
  33.   0x26, 0xFF, 0x7F,
  34.   0x65, 0x00,
  35.   0x75, 0x10,
  36.   0x95, 0x02,
  37.   0x81, 0x02,
  38.   0xC0,
  39.   0x05, 0x0D,
  40.   0x27, 0xFF, 0xFF, 0x00, 0x00,
  41.   0x75, 0x10,
  42.   0x95, 0x01,
  43.   0x09, 0x56,
  44.   0x81, 0x02,
  45.   0x09, 0x54,
  46.   0x25, 0x0A,
  47.   0x75, 0x08,
  48.   0x95, 0x01,
  49.   0x81, 0x02,
  50.   0x05, 0x0D,
  51.   0x09, 0x55,
  52.   0x25, 0x0A,
  53.   0x75, 0x08,
  54.   0x95, 0x01,
  55.   0xB1, 0x02,
  56.   0xC0,
  57. };
  58. class CustomHIDDevice: public USBHIDDevice {
  59.   public:
  60.     CustomHIDDevice(void) {
  61.       static bool initialized = false;
  62.       if (!initialized) {
  63.         initialized = true;
  64.         HID.addDevice(this, sizeof(report_descriptor));
  65.       }
  66.     }
  67.     uint16_t _onGetFeature(uint8_t report_id, uint8_t* buffer, uint16_t len)
  68.     {
  69.       buffer[0] = 0x0a;
  70.       return 1;
  71.     }
  72.     void begin(void) {
  73.       HID.begin();
  74.     }
  75.     uint16_t _onGetDescriptor(uint8_t* buffer) {
  76.       memcpy(buffer, report_descriptor, sizeof(report_descriptor));
  77.       return sizeof(report_descriptor);
  78.     }
  79.     bool send(uint8_t * value) {
  80.       return HID.SendReport(0, value, 9);
  81.     }
  82. };
  83. CustomHIDDevice Device;
  84. const int buttonPin = 0;
  85. int previousButtonState = HIGH;
  86. uint8_t TouchData[9];
  87. void setup() {
  88.   Serial.begin(115200);
  89.   Serial.setDebugOutput(true);
  90.   Device.begin();
  91.   USB.begin();
  92. }
  93. void loop() {
  94.   if (HID.ready()) {
  95.     delay(1000);
  96.     Serial.println("Finger");
  97.     int iX, iY;
  98.     /*
  99.     iX = STARTX + (STARTR * (2 * sin(0) - sin(0))) / 3;
  100.     iY = STARTY - (STARTR * (2 * cos(0) - cos(0)));
  101.     TouchData[0] = 0x81;
  102.     TouchData[1] = 0x02;
  103.     TouchData[2] = (iX) & 0xFF;
  104.     TouchData[3] = (iX) >> 8 & 0xFF;
  105.     TouchData[4] = (iY) & 0xFF;
  106.     TouchData[5] = (iY) >> 8 & 0xFF;
  107.     TouchData[6] = (millis() * 10) & 0xFF; TouchData[7] = (millis() * 10 >> 8) & 0xFF;
  108.     TouchData[8] = 0x01;
  109.     Device.send(TouchData);
  110.     delay(20);
  111.     TouchData[0] = 0x81;
  112.     TouchData[1] = 0x02;
  113.     TouchData[2] = (iX + 1) & 0xFF;
  114.     TouchData[3] = (iX + 1) >> 8 & 0xFF;
  115.     TouchData[4] = (iY) & 0xFF;
  116.     TouchData[5] = (iY) >> 8 & 0xFF;
  117.     TouchData[6] = (millis() * 10) & 0xFF; TouchData[7] = (millis() * 10 >> 8) & 0xFF;
  118.     TouchData[8] = 0x01;
  119.     Device.send(TouchData);
  120.     delay(40);
  121. */
  122.     // touch report
  123.     //  0: on/off + pressure
  124.     //  1: contact id
  125.     //  2: X lsb
  126.     //  3: X msb
  127.     //  4: Y lsb
  128.     //  5: Y msb
  129.     //  6: scan time lsb
  130.     //  7: scan time msb
  131.     //  8: contact count
  132.     for (int i = 0; i < STEP*18; i++) {
  133.       TouchData[0] = 0x81; TouchData[1] = 0x01;
  134.       iX = STARTX+(STARTR * cos(2*PI*i/STEP) + STARTR * (2*PI*i/STEP) * sin(2*PI*i/STEP))*3/4;
  135.       iY = STARTY+STARTR * sin(2*PI*i/STEP) - STARTR * (2*PI*i/STEP) * cos(2*PI*i/STEP);
  136.       Serial.print(iX); Serial.print("  "); Serial.println(iY);
  137.       TouchData[2] = ((int)(iX)) & 0xFF;
  138.       TouchData[3] = ((int)(iX)) >> 8 & 0xFF;
  139.       TouchData[4] = ((int)(iY)) & 0xFF;
  140.       TouchData[5] = ((int)(iY)) >> 8 & 0xFF;
  141.       TouchData[6] = (millis() * 10) & 0xFF; TouchData[7] = (millis() * 10 >> 8) & 0xFF;
  142.       TouchData[8] = 0x01;
  143.       Device.send(TouchData);
  144.       delay(20);
  145.     }
  146.     //每隔10秒
  147.     delay(2000);
  148.     STARTX = STARTX + 3;
  149.     STARTY = STARTY;
  150.   }
  151. }
复制代码
ESP32 S2 模拟触摸屏作图图5
参考:

2.https://zuotu.91maths.com/#W3sidHlwZSI6MSwiZXEiOiIyKigxLXNpbih0aGV0YSkpIiwiY29sb3IiOiIjMDA4MGNjIiwidGhldGFtaW4iOiIwIiwidGhldGFtYXgiOiIycGkiLCJ0aGV0YXN0ZXAiOiIwLjAxIn0seyJ0eXBlIjoxMDAwLCJ3aW5kb3ciOlsiLTguMjE1MTExOTk5OTk5OTkiLCI4LjAzNDg4Nzk5OTk5OTk5MiIsIi01LjI3ODUyNzk5OTk5OTk5NSIsIjQuNzIxNDcxOTk5OTk5OTk2Il0sImdyaWQiOlsiMSIsIjEiXX1d





hnyzcj  版主

发表于 2022-4-21 16:49:56

有点意思
回复

使用道具 举报

赤星三春牛!  初级技神

发表于 2022-4-24 21:41:12

厉害厉害
回复

使用道具 举报

赤星三春牛!  初级技神

发表于 2022-4-24 21:49:17

非常棒!
回复

使用道具 举报

派大星ym  初级技匠

发表于 2022-8-1 14:20:32

酷酷酷酷酷
回复

使用道具 举报

星辰大海://。,  中级技师

发表于 2022-9-11 13:13:26

6666666666666666666666666666666
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

为本项目制作心愿单
购买心愿单
心愿单 编辑
[[wsData.name]]

硬件清单

  • [[d.name]]
btnicon
我也要做!
点击进入购买页面
上海智位机器人股份有限公司 沪ICP备09038501号-4

© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed

mail