From 874f947046224691eafe4088890586865df1c5cb Mon Sep 17 00:00:00 2001 From: alastaira Date: Mon, 18 Sep 2023 20:15:27 +0100 Subject: [PATCH] Comments --- FaceBehavior.cpp | 53 ++++++++++++++++++++++++++---------------------- esp32-eyes.ino | 4 ++-- 2 files changed, 31 insertions(+), 26 deletions(-) diff --git a/FaceBehavior.cpp b/FaceBehavior.cpp index 1e42eff..d1690b5 100644 --- a/FaceBehavior.cpp +++ b/FaceBehavior.cpp @@ -34,18 +34,22 @@ void FaceBehavior::Clear() { } } +// Use roulette wheel to select a new emotion, based on assigned weights eEmotions FaceBehavior::GetRandomEmotion() { + + // Calculate the total sum of all emotional weights float sum_of_weight = 0; for (int emotion = 0; emotion < eEmotions::EMOTIONS_COUNT; emotion++) { sum_of_weight += Emotions[emotion]; } - + // If no weights have been assigned, default to "normal" emotion if (sum_of_weight == 0) { return eEmotions::Normal; } - + // Now pick a random number that lies somewhere in the range of total weights float rand = random(0, 1000 * sum_of_weight) / 1000.0; - + // Loop over emotions and select the one whose probabity distribution contains + // the value in which the random number lies float acc = 0; for (int emotion = 0; emotion < eEmotions::EMOTIONS_COUNT; emotion++) { if (Emotions[emotion] == 0) continue; @@ -54,7 +58,7 @@ eEmotions FaceBehavior::GetRandomEmotion() { return (eEmotions)emotion; } } - + // If something goes wrong in the calculation, return "normal" return eEmotions::Normal; } @@ -71,28 +75,29 @@ void FaceBehavior::Update() { } void FaceBehavior::GoToEmotion(eEmotions emotion) { + // Set the currentEmotion to the desired emotion CurrentEmotion = emotion; + // Call the appropriate expression transition function switch (CurrentEmotion) { - case eEmotions::Normal: _face.Expression.GoTo_Normal(); break; - case eEmotions::Angry: _face.Expression.GoTo_Angry(); break; - case eEmotions::Glee: _face.Expression.GoTo_Glee(); break; - case eEmotions::Happy: _face.Expression.GoTo_Happy(); break; - case eEmotions::Sad: _face.Expression.GoTo_Sad(); break; - case eEmotions::Worried: _face.Expression.GoTo_Worried(); break; - case eEmotions::Focused: _face.Expression.GoTo_Focused(); break; - case eEmotions::Annoyed: _face.Expression.GoTo_Annoyed(); break; - case eEmotions::Surprised: _face.Expression.GoTo_Surprised(); break; - case eEmotions::Skeptic: _face.Expression.GoTo_Skeptic(); break; - case eEmotions::Frustrated: _face.Expression.GoTo_Frustrated(); break; - case eEmotions::Unimpressed: _face.Expression.GoTo_Unimpressed(); break; - case eEmotions::Sleepy: _face.Expression.GoTo_Sleepy(); break; - case eEmotions::Suspicious: _face.Expression.GoTo_Suspicious(); break; - case eEmotions::Squint: _face.Expression.GoTo_Squint(); break; - case eEmotions::Furious: _face.Expression.GoTo_Furious(); break; - case eEmotions::Scared: _face.Expression.GoTo_Scared(); break; - case eEmotions::Awe: _face.Expression.GoTo_Awe(); break; - - default: break; + case eEmotions::Normal: _face.Expression.GoTo_Normal(); break; + case eEmotions::Angry: _face.Expression.GoTo_Angry(); break; + case eEmotions::Glee: _face.Expression.GoTo_Glee(); break; + case eEmotions::Happy: _face.Expression.GoTo_Happy(); break; + case eEmotions::Sad: _face.Expression.GoTo_Sad(); break; + case eEmotions::Worried: _face.Expression.GoTo_Worried(); break; + case eEmotions::Focused: _face.Expression.GoTo_Focused(); break; + case eEmotions::Annoyed: _face.Expression.GoTo_Annoyed(); break; + case eEmotions::Surprised: _face.Expression.GoTo_Surprised(); break; + case eEmotions::Skeptic: _face.Expression.GoTo_Skeptic(); break; + case eEmotions::Frustrated: _face.Expression.GoTo_Frustrated(); break; + case eEmotions::Unimpressed: _face.Expression.GoTo_Unimpressed(); break; + case eEmotions::Sleepy: _face.Expression.GoTo_Sleepy(); break; + case eEmotions::Suspicious: _face.Expression.GoTo_Suspicious(); break; + case eEmotions::Squint: _face.Expression.GoTo_Squint(); break; + case eEmotions::Furious: _face.Expression.GoTo_Furious(); break; + case eEmotions::Scared: _face.Expression.GoTo_Scared(); break; + case eEmotions::Awe: _face.Expression.GoTo_Awe(); break; + default: break; } } \ No newline at end of file diff --git a/esp32-eyes.ino b/esp32-eyes.ino index cee47ad..1232a79 100644 --- a/esp32-eyes.ino +++ b/esp32-eyes.ino @@ -30,11 +30,11 @@ void setup(void) { // Assign the current expression face->Expression.GoTo_Normal(); - // Assign a weight to each emotion that can be chosen + // Assign a weight to each emotion face->Behavior.SetEmotion(eEmotions::Normal, 1.0); //face->Behavior.SetEmotion(eEmotions::Angry, 1.0); //face->Behavior.SetEmotion(eEmotions::Sad, 1.0); - // Automatically switch between possible allowed behaviours (selecting new behaviour randomly based on the weight assigned to each emotion) + // Automatically switch between behaviours (selecting new behaviour randomly based on the weight assigned to each emotion) face->RandomBehavior = true; // Automatically blink