Files
esp32-eyes/Face.cpp
alastaira 17412d86cc Added manual look and blink control
Added ability to override "random" look, blink, and behaviour change operation, so that look direction and blink can be set with, e.g. a joystick control.
2023-09-18 15:47:47 +01:00

97 lines
2.7 KiB
C++

/***************************************************
Copyright (c) 2020 Luis Llamas
(www.luisllamas.es)
This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses
****************************************************/
#include "Face.h"
#include "Common.h"
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 4, /* data= */ 5);
Face::Face(uint16_t screenWidth, uint16_t screenHeight, uint16_t eyeSize)
: LeftEye(*this), RightEye(*this), Blink(*this), Look(*this), Behavior(*this), Expression(*this) {
// Unlike almost every other Arduino library (and the I2C address scanner script etc.)
// u8g2 uses 8-bit I2C address, so we shift the 7-bit address left by one
u8g2.setI2CAddress(0x3C<<1);
u8g2.begin();
u8g2.clearBuffer(); // clear the internal memory
u8g2.setFont(u8g2_font_ncenB08_tr); // choose a suitable font
u8g2.drawStr(0,10,"Hello World!"); // write something to the internal memory
u8g2.sendBuffer(); // transfer internal memory to the display
Width = screenWidth;
Height = screenHeight;
EyeSize = eyeSize;
CenterX = Width / 2;
CenterY = Height / 2;
LeftEye.IsMirrored = true;
Behavior.Clear();
Behavior.Timer.Start();
}
void Face::LookFront() {
Look.LookAt(0.0, 0.0);
}
void Face::LookRight() {
Look.LookAt(-1.0, 0.0);
}
void Face::LookLeft() {
Look.LookAt(1.0, 0.0);
}
void Face::LookTop() {
Look.LookAt(0.0, 1.0);
}
void Face::LookBottom() {
Look.LookAt(0.0, -1.0);
}
void Face::Wait(unsigned long milliseconds) {
unsigned long start;
start = millis();
while (millis() - start < milliseconds) {
Draw();
}
}
void Face::DoBlink() {
Blink.Blink();
}
void Face::Update() {
if(RandomBehavior) Behavior.Update();
if(RandomLook) Look.Update();
if(RandomBlink) Blink.Update();
Draw();
}
void Face::Draw() {
// Clear the display
u8g2.clearBuffer();
// Draw left eye
LeftEye.CenterX = CenterX - EyeSize / 2 - EyeInterDistance;
LeftEye.CenterY = CenterY;
LeftEye.Draw();
// Draw right eye
RightEye.CenterX = CenterX + EyeSize / 2 + EyeInterDistance;
RightEye.CenterY = CenterY;
RightEye.Draw();
// Transfer the redrawn buffer to the display
u8g2.sendBuffer();
}