Skip to content

Commit

Permalink
Respond to being ignored
Browse files Browse the repository at this point in the history
  • Loading branch information
handeyeco committed May 15, 2020
1 parent a552c16 commit ce9adb4
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 40 deletions.
4 changes: 2 additions & 2 deletions src/Expressions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -232,11 +232,11 @@ Expression Expressions::unhappyExpressions[] = {
Expression(closeLilEyesLow, closeLowBlinking)
};

Expression* Expressions::getExpression(int state) {
Expression* Expressions::getExpression(int mood) {
int i = 0;
int length;

switch(state) {
switch(mood) {
// Sleeping
case 0:
length = sizeof(sleepingExpressions) / sizeof(sleepingExpressions[0]);
Expand Down
70 changes: 54 additions & 16 deletions src/Grandbot.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,40 +42,77 @@ void Grandbot::writeExpression() {
}
}

void Grandbot::setState(int next) {
int lastState = state;
void Grandbot::updateEsteem() {
unsigned long now = millis();
unsigned long thresh = lastPlayTime + 21600000LL;

if (now > thresh) {
lastPlayTime = now;
esteem = max(0, esteem - 1);
updateMood();
}
}

void Grandbot::updateMood() {
int last = mood;

int next;
if (esteem > 7) {
next = 1;
} else if (esteem < 4) {
next = 3;
} else {
next = 2;
}
mood = next;

state = next;
setExpression();

if (lastState > -1 && lastState != state) {
light.update(state);
voice.emote(state);
if (last != next) {
voice.emote(mood);
}
}

void Grandbot::setExpression() {
expression = Expressions::getExpression(state);
expression = Expressions::getExpression(mood);
writeExpression();
light.update(mood);
}

void Grandbot::sleep() {
lc.setIntensity(0, 1);
setState(0);
lc.setIntensity(0, 0);
int lastMood = mood;
mood = 0;
setExpression();

// So we don't play a sound
// if we reset at night
if (lastMood >= 0) {
voice.emote(mood);
}
}

void Grandbot::wakeup() {
lc.setIntensity(0, 14);
setState(1);
// So he doesn't wake up angry
lastPlayTime = millis();
nextBlink = getNextBlink();
blinkLength = getBlinkLength();

lc.setIntensity(0, 14);
updateMood();
}

void Grandbot::play() {
// voice.feedback();
unsigned long now = millis();
unsigned long thresh = lastPlayTime + 3600000LL;

if (now > thresh) {
lastPlayTime = now;
esteem = min(9, esteem + 1);
}

// Keep state between 1-3
setState(((state + 1) % 3) + 1);
updateMood();
voice.emote(mood);
}

void Grandbot::update(int light) {
Expand All @@ -84,19 +121,20 @@ void Grandbot::update(int light) {
boolean asleep = light < Grandbot::sleepThreshold;
// debug(now);

if (state == 0) {
if (mood == 0) {
// Wakeup
if (awake) {
wakeup();
}
} else if (state > 0) {
} else if (mood > 0) {
// Sleep
if (asleep) {
sleep();
}
// Normal
else {
if (now > nextExpressionChange) {
updateEsteem();
setExpression();
nextExpressionChange = getNextExpressionChange();
}
Expand Down
10 changes: 8 additions & 2 deletions src/Grandbot.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ class Grandbot {
Light light;
Expression* expression;

void setState(int next);
void setExpression();
void writeExpression();

Expand All @@ -38,7 +37,14 @@ class Grandbot {
// 1 = happy
// 2 = neutral
// 3 = unhappy
int state = -1;
int mood = -1;

// between 0 (unhappiest) and 9 (happiest)
int esteem = 7;

void updateEsteem();
void updateMood();
unsigned long lastPlayTime = 0;

void debug(unsigned long now);
public:
Expand Down
21 changes: 6 additions & 15 deletions src/Light.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,28 +16,19 @@ void Light::write(int rValue, int gValue, int bValue) {
analogWrite(blue, bValue);
}

void Light::update(int state) {
int r = 0;
int g = 0;
int b = 0;
void Light::update(int mood) {
int r = random(0, 256);
int g = random(0, 256);
int b = random(0, 256);

if (state == 1) {
r = random(150, 256);
b = random(150, 256);
} else if (state == 2) {
r = random(0, 100);
g = random(150, 256);
b = random(0, 100);
}

switch(state) {
switch(mood) {
// Sleeping
case 0:
write(0, 0, 0);
return;
// Happy
case 1:
write(r, g, b);
write(r, g, 255);
return;
// Neutral
case 2:
Expand Down
2 changes: 1 addition & 1 deletion src/Light.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Light {
void write(int rValue, int gValue, int bValue);
public:
Light(int redPin, int greenPin, int bluePin);
void update(int state);
void update(int mood);
};

#endif
4 changes: 2 additions & 2 deletions src/Voice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,8 @@ void Voice::feedback() {
playRandomNote();
}

void Voice::emote(int state) {
switch(state) {
void Voice::emote(int mood) {
switch(mood) {
// Sleeping
case 0:
playMajor7th();
Expand Down
2 changes: 1 addition & 1 deletion src/Voice.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Voice {
public:
Voice(int voicePin);
void feedback();
void emote(int state);
void emote(int mood);
};

#endif
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ void setup() {

randomSeed(analogRead(randomPin));

// Serial.begin(9600);
Serial.begin(9600);
}

void loop() {
Expand Down

0 comments on commit ce9adb4

Please sign in to comment.