/*
PIXEL BUDDY - Khuon mat robot pixel tu dong - ESP32 + ILI9341.
Bat nguon la chay, khong can nut bam, mang hay file anh.
Arduino IDE: cai ESP32 board package va chon board cua ban.
Thu vien: Adafruit GFX Library, Adafruit ILI9341, Adafruit BusIO.
TFT: CS=15, DC=2, MOSI=13, MISO=12, SCK=14 (nhu code mau).
LED RGB active LOW: R=4, G=16, B=17.
TFT_BL=-1: giu cach cap den nen cua code mau. Neu can chan BL rieng,
sua TFT_BL/TFT_BL_ACTIVE_HIGH dung theo so do board cua ban.
Mac dinh man doc 240x320. Serial Monitor 115200 baud:
1 Binh thuong 2 Vui 3 Buon 4 Gian
5 Ngu 6 Ngac nhien 7 Noi chuyen (chi animation, KHONG am thanh)
0 hoac A: tu dong doi bieu cam, bat dau lai tu binh thuong
W: nhay mat trai; J/L: nhin trai/phai; C: nhin giua
P: tam dung/tiep tuc; ?: huong dan.
Chon 1..7 se tat tu dong doi bieu cam de giu bieu cam da chon.
CR/LF duoc bo qua; 'Newline' hay 'No line ending' deu duoc.
Do hoa: canvas logic 56x60, moi pixel ve thanh o 4x4 tren TFT.
Hai buffer 8-bit tong 6720 byte; chi ve lai cac doan pixel da doi.
Nhip animation muc tieu ~30 FPS; toc do thuc te tuy board va SPI.
Day la animation theo thoi gian, khong co mic, nhan dien hay cam ung.
*/
#include <Arduino.h>
#include <SPI.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ILI9341.h>
#include <esp_system.h>
#include <string.h>
#define TFT_CS 15
#define TFT_DC 2
#define TFT_MOSI 13
#define TFT_MISO 12
#define TFT_SCK 14
#define LED_R 4
#define LED_G 16
#define LED_B 17
constexpr int TFT_BL=-1;
constexpr bool TFT_BL_ACTIVE_HIGH=true;
constexpr int CW=56, CH=60, SCALE=4, OX=8, OY=32;
constexpr uint32_t FRAME_MS=33;
Adafruit_ILI9341 tft(&SPI,TFT_DC,TFT_CS,-1);
enum Mood : uint8_t {NORMAL,HAPPY,SAD,ANGRY,SLEEPY,SURPRISED,TALKING,MOOD_COUNT};
enum Ink : uint8_t {BACKGROUND,GRID,DIM,EYE,SHADOW,WHITE,CHEEK,MOUTH};
const char *const MOOD_NAMES[MOOD_COUNT]={"HELLO!","HAPPY","FEELING BLUE","GRUMPY","SLEEPING...","WOW!","BLA BLA BLA"};
const uint16_t EYE_COLORS[MOOD_COUNT]={0x2E9F,0x572D,0x5B7F,0xFA8D,0xA39F,0xFF09,0x2E9F};
uint16_t palette[8];
uint8_t frame[CH][CW], previous[CH][CW];
Mood mood=NORMAL;
bool autoMode=true, paused=false, statusDirty=true, paletteDirty=true;
uint32_t animTime=0,lastWall=0,lastFrame=0,nextMood=6000,nextBlink=2500,nextLook=1200;
uint32_t blinkStart=0,winkStart=0;
bool blinking=false,winking=false;
int gazeX=0,gazeY=0,targetX=0,targetY=0;
uint32_t lastGazeStep=0;
void setRGB(bool r,bool g,bool b) {
digitalWrite(LED_R,r?LOW:HIGH);digitalWrite(LED_G,g?LOW:HIGH);digitalWrite(LED_B,b?LOW:HIGH);
}
uint16_t darken(uint16_t color) {
return (((color>>11)&31)/3<<11)|(((color>>5)&63)/3<<5)|((color&31)/3);
}
void refreshPalette() {
palette[BACKGROUND]=0x0843;palette[GRID]=0x1085;palette[DIM]=0x39CE;
palette[EYE]=EYE_COLORS[mood];palette[SHADOW]=darken(palette[EYE]);
palette[WHITE]=0xFFFF;palette[CHEEK]=0xEAF3;palette[MOUTH]=palette[EYE];
memset(previous,255,sizeof(previous));paletteDirty=false;
if(paused) setRGB(false,false,false);
else if(mood==HAPPY) setRGB(false,true,false);
else if(mood==ANGRY) setRGB(true,false,false);
else if(mood==SAD) setRGB(false,false,true);
else if(mood==SLEEPY) setRGB(true,false,true);
else if(mood==SURPRISED) setRGB(true,true,false);
else setRGB(false,true,true);
}
// Drawing helpers clip at the logical canvas boundary.
void dot(int x,int y,uint8_t ink) {
if(x>=0 && x<CW && y>=0 && y<CH) frame[y][x]=ink;
}
void rect(int x,int y,int w,int h,uint8_t ink) {
for(int j=0;j<h;j++) for(int i=0;i<w;i++) dot(x+i,y+j,ink);
}
void pixelBox(int x,int y,int w,int h,uint8_t ink) {
if(w<=0 || h<=0)return;
if(w<4 || h<4){rect(x,y,w,h,ink);return;}
rect(x+2,y,w-4,h,ink);rect(x,y+2,w,h-4,ink);
rect(x+1,y+1,w-2,h-2,ink);
}
void line(int x0,int y0,int x1,int y1,uint8_t ink,int thickness=1) {
int dx=x1>x0?x1-x0:x0-x1, sx=x0<x1?1:-1;
int dy=-(y1>y0?y1-y0:y0-y1),sy=y0<y1?1:-1,err=dx+dy;
for(;;) {
rect(x0,y0,thickness,thickness,ink);
if(x0==x1 && y0==y1)break;
int e2=2*err;
if(e2>=dy){err+=dy;x0+=sx;}
if(e2<=dx){err+=dx;y0+=sy;}
}
}
// Triangle wave -100..100; integer math keeps the animation lightweight.
int wave(uint32_t time,uint32_t period) {
uint32_t phase=time%period;
int value=int(phase*400/period);
return value<=200?value-100:300-value;
}
int closure(uint32_t age) {
if(age<80)return int(age)*100/80;
if(age<120)return 100;
if(age<240)return 100-int(age-120)*100/120;
return 0;
}
void openEye(int cx,int cy,int width,int height,int shut) {
int h=height*(100-shut)/100;if(h<2)h=2;
int x=cx-width/2,y=cy-h/2;
pixelBox(x,y,width,h,SHADOW);
pixelBox(x+1,y,width-2,h-1,EYE);
if(h>=8) {
int pupilH=h/2;
pixelBox(cx-2+gazeX/2,cy-pupilH/2+gazeY/2,4,pupilH,BACKGROUND);
rect(x+2,y+2,2,2,WHITE);
}
}
void smileEye(int cx,int cy,bool sleepy,int shut) {
if(shut>45){rect(cx-6,cy,12,2,EYE);return;}
if(sleepy) {
line(cx-6,cy-1,cx-3,cy+1,EYE,2);rect(cx-2,cy+1,5,2,EYE);
line(cx+3,cy+1,cx+6,cy-1,EYE,2);
} else {
line(cx-6,cy+3,cx-2,cy-1,EYE,2);rect(cx-1,cy-2,3,2,EYE);
line(cx+2,cy-1,cx+6,cy+3,EYE,2);
}
}
void heart(int x,int y) {
rect(x+1,y,2,2,CHEEK);rect(x+4,y,2,2,CHEEK);
rect(x,y+1,7,2,CHEEK);rect(x+1,y+3,5,1,CHEEK);
rect(x+2,y+4,3,1,CHEEK);dot(x+3,y+5,CHEEK);
}
void letterZ(int x,int y,int size) {
rect(x,y,4*size,size,EYE);
line(x+3*size,y+size,x,y+4*size,EYE,size);
rect(x,y+4*size,4*size,size,EYE);
}
void drawFace() {
memset(frame,BACKGROUND,sizeof(frame));
// Very quiet pixel texture and four corner brackets.
for(int y=5;y<CH;y+=8)for(int x=4;x<CW;x+=8)dot(x,y,GRID);
for(int i=0;i<4;i++) {
int x=(i&1)?CW-4:2,y=(i&2)?CH-4:2;
rect(x,y,2,1,DIM);rect(x,y,1,2,DIM);
}
int bob=wave(animTime,3200)/70;
int cxL=16+gazeX,cxR=40+gazeX,cy=25+bob+gazeY;
int shut=blinking?closure(animTime-blinkStart):0;
int leftShut=shut;
if(winking) {int w=closure(animTime-winkStart);if(w>leftShut)leftShut=w;}
if(mood==HAPPY) {
smileEye(cxL,cy,false,leftShut);smileEye(cxR,cy,false,shut);
rect(8,cy+8,5,2,CHEEK);rect(44,cy+8,5,2,CHEEK);
int rise=wave(animTime,1600)/60;
heart(27,7+rise);
} else if(mood==SLEEPY) {
smileEye(16,cy,true,0);smileEye(40,cy,true,0);
uint32_t phase=(animTime/650)%3;
letterZ(40,12,1);
if(phase>=1)letterZ(46,7,1);
if(phase>=2)letterZ(50,2,1);
} else {
int w=mood==SURPRISED?15:13;
int h=mood==SURPRISED?20:(mood==SAD?11:17);
openEye(cxL,cy,w,h,leftShut);openEye(cxR,cy,w,h,shut);
if(mood==ANGRY) {
line(cxL-7,cy-13,cxL+6,cy-9,EYE,2);
line(cxR-6,cy-9,cxR+7,cy-13,EYE,2);
} else if(mood==SAD) {
line(cxL-6,cy-10,cxL+5,cy-13,EYE,2);
line(cxR-5,cy-13,cxR+6,cy-10,EYE,2);
int tearY=cy+7+int((animTime/110)%13);
pixelBox(cxR+5,tearY,3,5,EYE);
}
}
int my=44+bob;
if(mood==NORMAL) {
line(23,my,25,my+2,MOUTH,2);rect(26,my+2,5,2,MOUTH);
line(31,my+2,33,my,MOUTH,2);
} else if(mood==HAPPY) {
pixelBox(21,my-1,15,7,MOUTH);rect(24,my,9,2,WHITE);
rect(25,my+4,7,2,CHEEK);
} else if(mood==SAD) {
line(23,my+3,27,my,MOUTH,2);rect(27,my,3,2,MOUTH);
line(30,my,34,my+3,MOUTH,2);
} else if(mood==ANGRY) {
rect(23,my+1,11,2,MOUTH);rect(25,my,7,1,MOUTH);
} else if(mood==SLEEPY) {
int h=3+(wave(animTime,3200)+100)/70;
pixelBox(26,my,5,h,MOUTH);if(h>4)rect(28,my+2,1,h-3,BACKGROUND);
} else if(mood==SURPRISED) {
pixelBox(24,my-3,9,11,MOUTH);pixelBox(26,my-1,5,7,BACKGROUND);
} else if(mood==TALKING) {
// Deliberately irregular speech rhythm, visual animation only.
const uint8_t heights[12]={2,6,9,4,2,8,5,10,3,7,2,4};
int h=heights[(animTime/110)%12];
pixelBox(21,my-h/2,15,h,MOUTH);
if(h>=6) {rect(24,my-h/2+1,9,2,WHITE);rect(26,my+h/2-2,5,2,CHEEK);}
}
// Two soft cheek pixels give the idle face a friendly silhouette.
if(mood==NORMAL || mood==TALKING){rect(7,35+bob,3,1,SHADOW);rect(47,35+bob,3,1,SHADOW);}
}
void flushFace() {
// Group adjacent changed pixels of one color into a single SPI rectangle.
tft.startWrite();
for(int y=0;y<CH;y++) {
int x=0;
while(x<CW) {
if(frame[y][x]==previous[y][x]){x++;continue;}
int start=x;uint8_t ink=frame[y][x];
while(x<CW && frame[y][x]==ink && frame[y][x]!=previous[y][x]) {
previous[y][x]=frame[y][x];x++;
}
tft.writeFillRect(OX+start*SCALE,OY+y*SCALE,(x-start)*SCALE,SCALE,palette[ink]);
}
}
tft.endWrite();
}
void textAt(int x,int y,const char *s,uint16_t color,uint8_t size=1) {
tft.setCursor(x,y);tft.setTextSize(size);tft.setTextColor(color);tft.print(s);
}
void drawStatus() {
tft.fillRect(0,279,240,41,0x0843);
textAt(12,281,MOOD_NAMES[mood],EYE_COLORS[mood],2);
textAt(12,308,paused?"PAUSED / P: RESUME":(autoMode?"AUTO ON / 1-7: MOOD":"MANUAL / 0: AUTO"),0x9CF6);
tft.fillRect(205,9,21,9,0x0843);
if(!paused) tft.fillRect(207,11,5,5,EYE_COLORS[mood]);
tft.fillRect(217,11,5,5,autoMode?0x572D:0x39CE);
statusDirty=false;
}
void setMood(Mood chosen,bool automatic) {
mood=chosen;autoMode=automatic;
blinking=winking=false;gazeX=gazeY=targetX=targetY=0;
nextMood=animTime+(mood==SLEEPY?9000U:6000U);
nextBlink=animTime+uint32_t(random(1800,4200));
nextLook=animTime+1000;
paletteDirty=statusDirty=true;
}
void printHelp() {
Serial.println("PIXEL BUDDY: 1 normal, 2 happy, 3 sad, 4 angry, 5 sleep, 6 surprised, 7 talk.");
Serial.println("0/A auto, W wink, J/L look left/right, C center, P pause, ? help.");
Serial.println("Talking is a visual mouth animation; no audio or microphone is used.");
}
void handleInput(char key) {
if(key>='A' && key<='Z')key+=32;
if(key>='1' && key<='7')setMood(static_cast<Mood>(key-'1'),false);
else if(key=='0' || key=='a')setMood(NORMAL,true);
else if(key=='p'){paused=!paused;statusDirty=paletteDirty=true;}
else if(key=='w'){winking=true;winkStart=animTime;}
else if(key=='j'){targetX=-3;targetY=0;nextLook=animTime+3000;}
else if(key=='l'){targetX=3;targetY=0;nextLook=animTime+3000;}
else if(key=='c'){targetX=targetY=0;nextLook=animTime+3000;}
else if(key=='?')printHelp();
}
void tickFace() {
if(autoMode && int32_t(animTime-nextMood)>=0)
setMood(static_cast<Mood>((int(mood)+1)%int(MOOD_COUNT)),true);
if(blinking && uint32_t(animTime-blinkStart)>=240)blinking=false;
if(winking && uint32_t(animTime-winkStart)>=240)winking=false;
if(mood!=SLEEPY && !blinking && int32_t(animTime-nextBlink)>=0) {
blinking=true;blinkStart=animTime;
nextBlink=animTime+uint32_t(random(2400,5200));
}
if(int32_t(animTime-nextLook)>=0) {
targetX=random(-2,3);targetY=random(-1,2);
nextLook=animTime+uint32_t(random(1600,3300));
}
if(uint32_t(animTime-lastGazeStep)>=80) {
lastGazeStep=animTime;
if(gazeX<targetX)gazeX++;else if(gazeX>targetX)gazeX--;
if(gazeY<targetY)gazeY++;else if(gazeY>targetY)gazeY--;
}
}
void setup() {
Serial.begin(115200);
SPI.begin(TFT_SCK,TFT_MISO,TFT_MOSI,TFT_CS);
tft.begin();tft.setRotation(0);tft.setTextWrap(false);
if(TFT_BL>=0){pinMode(TFT_BL,OUTPUT);digitalWrite(TFT_BL,TFT_BL_ACTIVE_HIGH?HIGH:LOW);}
pinMode(LED_R,OUTPUT);pinMode(LED_G,OUTPUT);pinMode(LED_B,OUTPUT);
randomSeed(esp_random());
tft.fillScreen(0x0843);
textAt(10,7,"PIXEL BUDDY",0xFE48,2);
tft.drawRect(5,29,230,246,0x4A50);
tft.drawFastHLine(8,28,224,0x7396);
setMood(NORMAL,true);refreshPalette();drawFace();flushFace();drawStatus();
lastWall=lastFrame=millis();printHelp();
}
void loop() {
uint32_t now=millis(),dt=now-lastWall;lastWall=now;
if(!paused)animTime+=dt;
for(int n=0;n<16 && Serial.available();n++)handleInput(Serial.read());
if(!paused)tickFace();
bool repaint=paletteDirty;
if(paletteDirty)refreshPalette();
if(statusDirty)drawStatus();
if(repaint || (!paused && uint32_t(now-lastFrame)>=FRAME_MS)) {
lastFrame=now;drawFace();flushFace();
}
delay(1);
}
Loading
esp32-2432s028r
esp32-2432s028r