第 2 步:Cow 软件 
 
CYD 是 ESP32 和 Touchschreeen 的完美结合。它为您省去了烦人的焊接和电缆。 
诚然,我的编程有点业余。您当然可以从 CYD 的设计方面获得更多收益或为奶牛提供更多功能。但至少程序步骤很容易遵循。 
 
该库可用于模拟各种工作表面。我们不展示背景,而是专注于奶牛、饮水处和饲养处。三个点击区域以隐藏的方式集成在一起,即它们不可见,但可以在三个地方点击:用于补充的水和饲料位置以及用于抚摸的奶牛本身。 
奶牛的状态(很快)保存在 ESP32 上,以便随时关闭设备,并保持食物/情绪状态。 
 
饥饿和口渴的值随机变化。ESP32 对毫秒数进行计数。这些值可以根据您的个人喜好进行设置。 
文件 “cow-all.h” 在代码中包含了所有不同的奶牛图像,并且必须与 “demo.ino” 或 “tamagotchi_cow.ino” 存储在同一个文件夹中! 
 
cow-all.h 
演示.ino 
 
			
			
			- #include <Arduino.h>
 - #include <SPI.h>
 - #include <TFT_eSPI.h>
 - #include <XPT2046_Touchscreen.h>
 - 
 - TFT_eSPI tft = TFT_eSPI();
 - 
 - // touchscreen pins 
 - #define XPT2046_IRQ 36  // T_IRQ
 - #define XPT2046_MOSI 32 // T_DIN
 - #define XPT2046_MISO 39 // T_OUT
 - #define XPT2046_CLK 25  // T_CLK
 - #define XPT2046_CS 33   // T_CS
 - SPIClass touchscreenSPI = SPIClass(VSPI);
 - XPT2046_Touchscreen touchscreen(XPT2046_CS, XPT2046_IRQ);
 - 
 - #define SCREEN_WIDTH 320
 - #define SCREEN_HEIGHT 240
 - #define FONT_SIZE 2
 - 
 - #include "cow-all.h"
 - 
 - // extra colors
 - #define NAVY tft.color565(0,0,160)   
 - #define DODGERBLUE tft.color565(30,144,255)
 - #define TURQUOISE tft.color565(64,224,208) 
 - #define CYAN tft.color565(0,255,255)  
 - #define GOLD tft.color565(255,215,0)
 - #define ORANGE tft.color565(255,165,0)
 - #define RED tft.color565(255,10,0)
 - #define MAROON tft.color565(123,0,0)
 - #define BROWN tft.color565(150,75,0)
 - 
 - // Global Variables
 - int x, y, z;     // touchscreen coordinates + pressure
 - 
 - // Set X and Y coordinates for center of display
 - int centerX, centerY;
 - 
 - //moodmeter & cow
 -  uint32_t moodcolor;
 -  int food, water, mood, mooddegree, waterlevel, foodlevel;
 -  int cowX,cowY;
 -  uint32_t cowImage;
 -  int saturation, thirst;
 -  int flag;
 - 
 - void drawStable() {
 -   //tft.drawRect(260, 10, 50, 14, TFT_RED);         //status
 -   tft.fillRoundRect(10, 80, 30, 70, 3, GOLD );          //food   TFT_DARKGREEN
 -   tft.fillRoundRect(10, 140, 50, 10, 5, TFT_DARKGREEN); //food
 - 
 -   tft.fillRoundRect(290, 80, 16, 60, 5, DODGERBLUE);  //water
 -   tft.drawRoundRect(290, 80, 16, 60, 5, TFT_WHITE);  //water
 -   tft.drawRoundRect(280, 138, 26, 2, 2, TFT_WHITE);  //water
 - }
 - 
 - 
 - void setup()
 - {
 -   Serial.begin(115200);
 - 
 -   // init RGB LED
 -   pinMode(4, OUTPUT); pinMode(16, OUTPUT); pinMode(17, OUTPUT);
 -   // turn off LED
 -   digitalWrite(4, HIGH); digitalWrite(16, HIGH); digitalWrite(17, HIGH);
 - 
 -   // Start the SPI for the touchscreen and init the touchscreen
 -   touchscreenSPI.begin(XPT2046_CLK, XPT2046_MISO, XPT2046_MOSI, XPT2046_CS);
 -   touchscreen.begin(touchscreenSPI);
 -   touchscreen.setRotation(3);
 - 
 -   // start display
 -   tft.init();
 -   tft.setRotation(3); // may be altered
 -   tft.fillScreen(TFT_BLACK);
 -   tft.setTextColor(TFT_WHITE, TFT_BLACK);
 - 
 -   // Set X and Y coordinates for center of display
 -   centerX = SCREEN_WIDTH / 2;
 -   centerY = SCREEN_HEIGHT / 2;
 - 
 -   tft.drawCentreString("Welcome to my stable", centerX, 30, FONT_SIZE);
 - 
 -   int x = (tft.width() - 116) / 2;
 -   int y = (tft.height() - 110) / 2;
 - 
 - food=60;
 - saturation = 90;
 - water= 80;
 - 
 -   //clear cow area
 -   tft.fillRect(40, 70, 249, 170, TFT_BLACK);
 -   // draw cow
 -   //tft.drawBitmap(40, 80, cow_well, 160, 140, TFT_WHITE);
 -   //tft.drawBitmap(45, 80, cow_eat, 160, 142, TFT_WHITE);
 -   tft.drawBitmap(70, 80, cow_neutral, 160, 147, TFT_WHITE);
 -   delay(2000);
 -   tft.fillRect(50, 0, 200, 50, TFT_BLACK); //clear area
 - }
 - 
 - void loop()  {
 -   // Checks if Touchscreen is touched
 -   if (touchscreen.tirqTouched() && touchscreen.touched()) {
 -     // Get Touchscreen points
 -     TS_Point p = touchscreen.getPoint();
 -     // Calibrate Touchscreen points with map function to the correct width and height
 -     x = map(p.x, 200, 3700, 1, SCREEN_WIDTH);
 -     y = map(p.y, 240, 3800, 1, SCREEN_HEIGHT);
 -     z = p.z;
 - 
 -     printTouchToSerial(x, y, z);
 - 
 -        
 -     //food touch
 -     if ((x>5)&&(x<60)&&(y>70)&&(y<160)) food=food+20;
 -     if (food>=100) food=100;
 -     //water touch
 -     if ((x>270)&&(x<315)&&(y>60)&&(y<155)) water=water+20;
 -     if (water>=100) water=100;
 -     
 -     cowX=50; cowY = 80; //start coordinates of cow bmp
 -     //cow touch
 -     if ((x>cowX)&&(x<cowX+150)&&(y>cowY)&&(y<cowY+130)) mood=mood+20;
 -     if (mood>=100) mood=100;
 - 
 -     Serial.print(food); Serial.print("  "); Serial.print(water);  Serial.print("  "); Serial.println(mood); 
 -     delay(80);
 -   }
 -   moodmeter(mood);
 -   waterfill(water);
 -   foodfill(food);
 - 
 - /*
 -   //  food /water levels ********************
 -   if (cowImage = cow_eat) {
 -     food--;
 -     saturation = saturation + 6;
 -     
 -   }
 -   if (cowImage = cow_drink) {
 -     water--;
 -     thirst = thirst -7;
 -   }
 - 
 -   //hunger/thirst comes up  *******************
 -   if (millisnow - millispast = random(480000, 900000)) {  //8-15 minutes
 -     saturation = saturation-3;
 -     thirst = thirst + 2;
 -     if (saturation<=0) saturation=0;
 -     if (thirst<=0) thirst=0;
 -   }
 -   */
 -   food=food - 0.5; water=water -0.5; mood=mood-0.5;
 -   if (food<=0) food=0;
 -   if (water<=0) water=0;
 -   if (mood<=0) mood=0;
 - 
 -   if ((mood> 70) && (flag!=1)) {
 -     tft.fillRect(54, 70, 220, 170, TFT_BLACK); //clear cow area
 -     tft.drawBitmap(70, 80, cow_smile2, 160, 147, TFT_WHITE);// draw cow
 -     flag = 1;
 -   }
 -   if ((mood<= 70) && (mood > 30) && (flag!=2)) {
 -     tft.fillRect(54, 70, 220, 170, TFT_BLACK); //clear cow area
 -     tft.drawBitmap(70, 80, cow_neutral, 160, 147, TFT_WHITE);// draw cow
 -     flag = 2;
 -   }
 -   if ((mood<=30) && (flag !=3)) {
 -     tft.fillRect(54, 70, 220, 170, TFT_BLACK); //clear cow area
 -     tft.drawBitmap(70, 80, cow_sad, 160, 145, TFT_WHITE);// draw cow
 -     flag = 3;
 -   }
 - 
 - 
 -   delay(300);
 - }
 - 
 - 
 - void waterfill(int water) {
 -   tft.fillRoundRect(290, 80, 16, 60, 5, TFT_BLACK); //clear area
 -   waterlevel = map(water, 0, 100, 140, 80);
 -   tft.fillRect(290, waterlevel, 16, (140-waterlevel), DODGERBLUE);  //water
 -   tft.drawRoundRect(289, 79, 18, 62, 5, TFT_WHITE);  //water frame
 -   tft.drawRoundRect(275, 138, 26, 3, 1, TFT_WHITE);  //water 
 - }
 - 
 - void foodfill(int food) {
 -   tft.fillRoundRect(10, 80, 40, 70, 5, TFT_BLACK); //clear area
 -   foodlevel = map(food, 0, 100, 150 , 80);
 -   tft.drawRoundRect(9, 79, 42, 72, 3, BROWN);     //food frame
 -   tft.fillRoundRect(10, foodlevel, 40, (150-foodlevel), 3, TFT_DARKGREEN);   //food   
 -   tft.fillTriangle(9, 150, 10, 157, 62, 150, BROWN);    //feeding dish
 - }
 - 
 - void moodmeter(int mood) {
 -   tft.fillRect(0, 0, 43, 43, TFT_BLACK); //clear area
 -   mooddegree = map(mood, 0, 100, 1, 360);
 -   if (mood < 15) moodcolor = RED;
 -   if ((mood >= 15) && (mood < 30)) moodcolor = ORANGE;
 -   if ((mood >= 30) && (mood < 60)) moodcolor = GOLD;
 -   if (mood >= 60) moodcolor = TFT_DARKGREEN;
 -   tft.drawSmoothArc(22, 22, 20, 8, 0, mooddegree, moodcolor, TFT_BLACK);
 - }
 - 
 - void printTouchToSerial(int x, int y, int z) {
 -   Serial.print("X = ");
 -   Serial.print(x);
 -   Serial.print(" | Y = ");
 -   Serial.print(y);
 -   Serial.print(" | Pressure = ");
 -   Serial.print(z);
 -   Serial.println();
 - }
 
  复制代码
  
 
 
 
 
 |