-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFace.cpp
80 lines (64 loc) · 1.69 KB
/
Face.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Face.h"
#include "Common.h"
U8G2_SSD1306_128X64_NONAME_F_HW_I2C u8g2(U8G2_R0, /* reset=*/ U8X8_PIN_NONE, /* clock=*/ 22, /* data= */ 21);
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();
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();
}