1
0
mirror of https://github.com/s00500/SimpleExpressions synced 2024-11-23 21:20:55 +00:00

Adding sounds and ESP32 workaround for ledc and Adafruit_NeoPixel

This commit is contained in:
Lukas Bachschwell 2017-11-23 15:36:57 +01:00
parent 3630a9342d
commit 4006d23649
2 changed files with 75 additions and 65 deletions

View File

@ -1,19 +1,16 @@
#include "Arduino.h" #include "Arduino.h"
#include "SimpleExpressions.h" #include "SimpleExpressions.h"
#define ledc_channel 5
void SimpleExpressionsClass::init(int mouthPin, int buzzerPin) { void SimpleExpressionsClass::init(int mouthPin, int buzzerPin) {
mouth = Adafruit_NeoPixel(7, mouthPin, NEO_GRB + NEO_KHZ800); mouth = Adafruit_NeoPixel(7, mouthPin, NEO_GRB + NEO_KHZ800);
mouth.begin(); mouth.begin();
mouth.show();
pinBuzzer = buzzerPin; ledcSetup(ledc_channel, 2000, 8); // channel, max frequency, resolution
pinMode(buzzerPin, OUTPUT); ledcAttachPin(buzzerPin, ledc_channel);
int freq = 2000; clearMouth();
int channel = 1;
int resolution = 8;
ledcSetup(1, freq, resolution);
ledcAttachPin(buzzerPin, 1);
} }
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
@ -27,7 +24,7 @@ void SimpleExpressionsClass::putAnimationMouth(int aniMouth, int index){
*/ */
void SimpleExpressionsClass::writeMouth(char mouthName[], int r, int g, int b){ void SimpleExpressionsClass::writeMouth(char mouthName[], int r, int g, int b) {
int number = -1; int number = -1;
for(int i = 0; i < frameCount; i++){ for(int i = 0; i < frameCount; i++){
if(strncmp(shapes[i].name, mouthName, 20) == 0) { if(strncmp(shapes[i].name, mouthName, 20) == 0) {
@ -48,9 +45,11 @@ void SimpleExpressionsClass::printMouth(int number, int r, int g, int b) {
else mouth.setPixelColor(i, 0); else mouth.setPixelColor(i, 0);
} }
mouth.show(); mouth.show();
delay(1);
clearPixels();
} }
void SimpleExpressionsClass::writeMouth(char mouthName[] ){ void SimpleExpressionsClass::writeMouth(char mouthName[] ) {
int number = -1; int number = -1;
for(int i = 0; i < colorFrameCount; i++){ for(int i = 0; i < colorFrameCount; i++){
if(strncmp(shapes[i].name, mouthName, 20) == 0) { if(strncmp(shapes[i].name, mouthName, 20) == 0) {
@ -70,6 +69,8 @@ void SimpleExpressionsClass::printMouth(int number) {
mouth.setPixelColor(i, mouth.Color(colorShapes[number].data[i][0], colorShapes[number].data[i][1], colorShapes[number].data[i][2])); mouth.setPixelColor(i, mouth.Color(colorShapes[number].data[i][0], colorShapes[number].data[i][1], colorShapes[number].data[i][2]));
} }
mouth.show(); mouth.show();
delay(1);
clearPixels();
} }
@ -79,6 +80,8 @@ void SimpleExpressionsClass::writeMouthGeneric(const bool mouthArray[7], int r,
else mouth.setPixelColor(i, 0); else mouth.setPixelColor(i, 0);
} }
mouth.show(); mouth.show();
delay(1);
clearPixels();
} }
void SimpleExpressionsClass::writeMouthGeneric(const int mouthArray[7][3]) { void SimpleExpressionsClass::writeMouthGeneric(const int mouthArray[7][3]) {
@ -86,16 +89,27 @@ void SimpleExpressionsClass::writeMouthGeneric(const int mouthArray[7][3]) {
mouth.setPixelColor(i, mouth.Color(mouthArray[i][0], mouthArray[i][1], mouthArray[i][2])); mouth.setPixelColor(i, mouth.Color(mouthArray[i][0], mouthArray[i][1], mouthArray[i][2]));
} }
mouth.show(); mouth.show();
delay(1);
clearPixels();
} }
void SimpleExpressionsClass::clearMouth(){ void SimpleExpressionsClass::clearMouth() {
for(uint16_t i=0; i<mouth.numPixels(); i++) { for(int i = 0; i < 7; i++) {
mouth.setPixelColor(i, mouth.Color(0, 0, 0)); mouth.setPixelColor(i, 0);
} }
mouth.show(); mouth.show();
delay(1);
clearPixels();
} }
void SimpleExpressionsClass::clearPixels() { // avoid strange issues on ESP32 with ledc
for(int i = 0; i < 7; i++) {
mouth.setPixelColor(i, 0);
}
delay(1);
}
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
//-- SOUNDS -----------------------------------------------------// //-- SOUNDS -----------------------------------------------------//
/////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////
@ -104,15 +118,14 @@ void SimpleExpressionsClass::clearMouth(){
void SimpleExpressionsClass::_tone (float noteFrequency, long noteDuration, int silentDuration){ void SimpleExpressionsClass::_tone (float noteFrequency, long noteDuration, int silentDuration){
if(silentDuration==0){silentDuration=1;} if(silentDuration==0){silentDuration=1;}
ledcWriteTone(0, noteFrequency); ledcWriteTone(ledc_channel, noteFrequency);
ledcWrite(1, 255); delay(noteDuration); // milliseconds
delay(noteDuration); //milliseconds to microseconds ledcWrite(ledc_channel, 0); // notone
//ledcWrite(0, 0); // notone
delay(silentDuration); delay(silentDuration);
} }
/*
void SimpleE::bendTones (float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration){
void SimpleExpressionsClass::bendTones (float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration){
//Examples: //Examples:
// bendTones (880, 2093, 1.02, 18, 1); // bendTones (880, 2093, 1.02, 18, 1);
@ -135,118 +148,116 @@ void SimpleE::bendTones (float initFrequency, float finalFrequency, float prop,
} }
void SimpleExpressionsClass::sing(int songName){ void SimpleExpressionsClass::playSound(int soundName){
switch(songName){ switch(soundName){
case S_connection: case S_CONNECTION:
_tone(note_E5,50,30); _tone(NOTE_E5,50,30);
_tone(note_E6,55,25); _tone(NOTE_E6,55,25);
_tone(note_A6,60,10); _tone(NOTE_A6,60,10);
break; break;
case S_disconnection: case S_DISCONNECTION:
_tone(note_E5,50,30); _tone(NOTE_E5,50,30);
_tone(note_A6,55,25); _tone(NOTE_A6,55,25);
_tone(note_E6,50,10); _tone(NOTE_E6,50,10);
break; break;
case S_buttonPushed: case S_BUTTON_PUSHED:
bendTones (note_E6, note_G6, 1.03, 20, 2); bendTones (NOTE_E6, NOTE_G6, 1.03, 20, 2);
delay(30); delay(30);
bendTones (note_E6, note_D7, 1.04, 10, 2); bendTones (NOTE_E6, NOTE_D7, 1.04, 10, 2);
break; break;
case S_mode1: case S_MODE1:
bendTones (note_E6, note_A6, 1.02, 30, 10); //1318.51 to 1760 bendTones (NOTE_E6, NOTE_A6, 1.02, 30, 10); //1318.51 to 1760
break; break;
case S_mode2: case S_MODE2:
bendTones (note_G6, note_D7, 1.03, 30, 10); //1567.98 to 2349.32 bendTones (NOTE_G6, NOTE_D7, 1.03, 30, 10); //1567.98 to 2349.32
break; break;
case S_mode3: case S_MODE3:
_tone(note_E6,50,100); //D6 _tone(NOTE_E6,50,100); //D6
_tone(note_G6,50,80); //E6 _tone(NOTE_G6,50,80); //E6
_tone(note_D7,300,0); //G6 _tone(NOTE_D7,300,0); //G6
break; break;
case S_surprise: case S_SURPRISE:
bendTones(800, 2150, 1.02, 10, 1); bendTones(800, 2150, 1.02, 10, 1);
bendTones(2149, 800, 1.03, 7, 1); bendTones(2149, 800, 1.03, 7, 1);
break; break;
case S_OhOoh: case S_OHOOH:
bendTones(880, 2000, 1.04, 8, 3); //A5 = 880 bendTones(880, 2000, 1.04, 8, 3); //A5 = 880
delay(200); delay(200);
for (int i=880; i<2000; i=i*1.04) { for (int i=880; i<2000; i=i*1.04) {
_tone(note_B5,5,10); _tone(NOTE_B5,5,10);
} }
break; break;
case S_OhOoh2: case S_OHOOH2:
bendTones(1880, 3000, 1.03, 8, 3); bendTones(1880, 3000, 1.03, 8, 3);
delay(200); delay(200);
for (int i=1880; i<3000; i=i*1.03) { for (int i=1880; i<3000; i=i*1.03) {
_tone(note_C6,10,10); _tone(NOTE_C6,10,10);
} }
break; break;
case S_cuddly: case S_CUDDLY:
bendTones(700, 900, 1.03, 16, 4); bendTones(700, 900, 1.03, 16, 4);
bendTones(899, 650, 1.01, 18, 7); bendTones(899, 650, 1.01, 18, 7);
break; break;
case S_sleeping: case S_SLEEPING:
bendTones(100, 500, 1.04, 10, 10); bendTones(100, 500, 1.04, 10, 10);
delay(500); delay(500);
bendTones(400, 100, 1.04, 10, 1); bendTones(400, 100, 1.04, 10, 1);
break; break;
case S_happy: case S_HAPPY:
bendTones(1500, 2500, 1.05, 20, 8); bendTones(1500, 2500, 1.05, 20, 8);
bendTones(2499, 1500, 1.05, 25, 8); bendTones(2499, 1500, 1.05, 25, 8);
break; break;
case S_superHappy: case S_SUPER_HAPPY:
bendTones(2000, 6000, 1.05, 8, 3); bendTones(2000, 6000, 1.05, 8, 3);
delay(50); delay(50);
bendTones(5999, 2000, 1.05, 13, 2); bendTones(5999, 2000, 1.05, 13, 2);
break; break;
case S_happy_short: case S_HAPPY_SHORT:
bendTones(1500, 2000, 1.05, 15, 8); bendTones(1500, 2000, 1.05, 15, 8);
delay(100); delay(100);
bendTones(1900, 2500, 1.05, 10, 8); bendTones(1900, 2500, 1.05, 10, 8);
break; break;
case S_sad: case S_SAD:
bendTones(880, 669, 1.02, 20, 200); bendTones(880, 669, 1.02, 20, 200);
break; break;
case S_confused: case S_CONFUSED:
bendTones(1000, 1700, 1.03, 8, 2); bendTones(1000, 1700, 1.03, 8, 2);
bendTones(1699, 500, 1.04, 8, 3); bendTones(1699, 500, 1.04, 8, 3);
bendTones(1000, 1700, 1.05, 9, 10); bendTones(1000, 1700, 1.05, 9, 10);
break; break;
case S_fart1: case S_FART1:
bendTones(1600, 3000, 1.02, 2, 15); bendTones(1600, 3000, 1.02, 2, 15);
break; break;
case S_fart2: case S_FART2:
bendTones(2000, 6000, 1.02, 2, 20); bendTones(2000, 6000, 1.02, 2, 20);
break; break;
case S_fart3: case S_FART3:
bendTones(1600, 4000, 1.02, 2, 20); bendTones(1600, 4000, 1.02, 2, 20);
bendTones(4000, 3000, 1.02, 2, 20); bendTones(4000, 3000, 1.02, 2, 20);
break; break;
} }
} }
*/
SimpleExpressionsClass SimpleExpressions; SimpleExpressionsClass SimpleExpressions;

View File

@ -6,6 +6,7 @@
#endif #endif
#include "Shapes.h" #include "Shapes.h"
#include "Sounds.h"
#define debug true #define debug true
@ -16,34 +17,32 @@ class SimpleExpressionsClass
void init(int mouthPin, int buzzerPin); void init(int mouthPin, int buzzerPin);
void writeMouth(char mouthName[], int r, int g, int b);
void printMouth(int number, int r, int g, int b); void printMouth(int number, int r, int g, int b);
void writeMouth(char mouthName[], int r, int g, int b);
void writeMouth(char mouthName[]);
void printMouth(int number); void printMouth(int number);
void writeMouth(char mouthName[]);
void clearMouth(); void clearMouth();
void writeMouthGeneric(const bool mouthArray[7], int r, int g, int b);
void writeMouthGeneric(const int mouthArray[7][3]); void writeMouthGeneric(const int mouthArray[7][3]);
void writeMouthGeneric(const bool mouthArray[7], int r, int g, int b);
//void putAnimationMouth(int anim, int index); //void putAnimationMouth(int anim, int index);
//-- Sounds //-- Sounds
void _tone (float noteFrequency, long noteDuration, int silentDuration); void _tone (float noteFrequency, long noteDuration, int silentDuration);
void bendTones (float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration); void bendTones (float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration);
void sing(int songName); void playSound(int soundName);
//-- Gestures //-- Gestures
//void playGesture(int gesture); //void playGesture(int gesture);
private: private:
int pinBuzzer;
Adafruit_NeoPixel mouth; Adafruit_NeoPixel mouth;
void clearPixels();
long int getMouthShape(int number);
long int getAnimShape(int anim, int index);
}; };
extern SimpleExpressionsClass SimpleExpressions; extern SimpleExpressionsClass SimpleExpressions;