1
0
mirror of https://github.com/s00500/SimpleExpressions synced 2025-07-07 06:20:18 +00:00

Moving to src for format

This commit is contained in:
2017-12-03 20:11:50 +01:00
parent 387ea3e2d8
commit 9310f8213c
4 changed files with 0 additions and 0 deletions

121
src/Shapes.h Executable file
View File

@ -0,0 +1,121 @@
#ifndef Shapes_h
#define Shapes_h
struct Frame
{
char name[20];
bool data[7];
}
typedef Frame;
struct MulticolorFrame
{
char name[20];
int data[7][3];
}
typedef MulticolorFrame;
//***********************************************************************************
//*********************************MOUTHS DEFINES************************************
//***********************************************************************************
#define frameCount 19
#define colorFrameCount 1
// TODO: calculate me
const PROGMEM Frame shapes[] = {
{
"zeros",
{0, 0, 0, 0, 0, 0, 0}
},
{
"happySmall",
{0, 0, 1, 1, 0, 0, 0}
},
{
"happyFull",
{0, 1, 1, 1, 1, 0, 0}
},
{
"sadSmall",
{0, 0, 0, 0, 0, 1, 1}
},
{
"sadFull",
{0, 1, 0, 0, 1, 1, 1}
},
{
"neutral",
{1, 1, 0, 0, 1, 0, 0}
},
{
"circle",
{0, 1, 1, 1, 1, 1, 1}
},
{
"center",
{1, 0, 0, 0, 0, 0, 0}
},
{
"hook",
{1, 0, 0, 1, 1, 0, 1}
},
{
"upsidedownhook",
{1, 0, 1, 0, 1, 1, 0}
},
{
"kooh",
{1, 1, 1, 0, 0, 1, 0}
},
{
"upsidedownkooh",
{1, 1, 0, 1, 0, 0, 1}
},
{
"cross",
{1, 0, 1, 1, 0, 1, 1}
},
{
"rect",
{0, 0, 1, 1, 0, 1, 1}
},
{
"leftarrow",
{1, 1, 0, 1, 1, 1, 0}
},
{
"rightarrow",
{1, 1, 1, 0, 1, 0, 1}
},
{
"lefthalf",
{0, 0, 0, 1, 1, 1, 0}
},
{
"righthalf",
{0, 1, 1, 0, 0, 0, 1}
},
};
const PROGMEM MulticolorFrame colorShapes[] = {
{
"colorCircle",
{
{50,0,0},
{0,50,0},
{0,0,50},
{50,0,0},
{0,50,0},
{0,0,50},
{50,0,0}
}
}
};
#endif

275
src/SimpleExpressions.cpp Executable file
View File

@ -0,0 +1,275 @@
#include "Arduino.h"
#include "SimpleExpressions.h"
#define ledc_channel 5
void SimpleExpressionsClass::init(int aMouthPin, int aBuzzerPin) {
mouth = Adafruit_NeoPixel(7, aMouthPin, NEO_GRB + NEO_KHZ800);
mouth.begin();
buzzerPin = aBuzzerPin;
#if defined(ESP32)
ledcSetup(ledc_channel, 2000, 8); // channel, max frequency, resolution
ledcAttachPin(aBuzzerPin, ledc_channel);
#endif
clearMouth();
}
///////////////////////////////////////////////////////////////////
//-- MOUTHS ----------------------------------------//
///////////////////////////////////////////////////////////////////
void SimpleExpressionsClass::writeMouth(char mouthName[], int r, int g, int b) {
int number = -1;
for(int i = 0; i < frameCount; i++){
if(strncmp(shapes[i].name, mouthName, 20) == 0) {
number = i;
break;
}
}
if(number != -1){
printMouth(number, r, g, b);
} else {
if(debug) Serial.println("Error: mouth name does not exist");
}
}
void SimpleExpressionsClass::printMouth(int number, int r, int g, int b) {
for(uint16_t i = 0; i<7; i++) {
if(shapes[number].data[i]) mouth.setPixelColor(i, mouth.Color(r, g, b));
else mouth.setPixelColor(i, 0);
}
mouth.show();
delay(1);
clearPixels();
}
void SimpleExpressionsClass::writeMouth(char mouthName[] ) {
int number = -1;
for(int i = 0; i < colorFrameCount; i++){
if(strncmp(shapes[i].name, mouthName, 20) == 0) {
number = i;
break;
}
}
if(number != -1){
printMouth(number);
} else {
if(debug) Serial.println("Error: mouth name does not exist");
}
}
void SimpleExpressionsClass::printMouth(int number) {
for(uint16_t i = 0; i<7; i++) {
mouth.setPixelColor(i, mouth.Color(colorShapes[number].data[i][0], colorShapes[number].data[i][1], colorShapes[number].data[i][2]));
}
mouth.show();
delay(1);
clearPixels();
}
void SimpleExpressionsClass::writeMouthGeneric(const bool mouthArray[7], int r, int g, int b) {
for(uint16_t i=0; i<7; i++) {
if(mouthArray[i]) mouth.setPixelColor(i, mouth.Color(r, g, b));
else mouth.setPixelColor(i, 0);
}
mouth.show();
delay(1);
clearPixels();
}
void SimpleExpressionsClass::writeMouthGeneric(const int mouthArray[7][3]) {
for(uint16_t i=0; i<7; i++) {
mouth.setPixelColor(i, mouth.Color(mouthArray[i][0], mouthArray[i][1], mouthArray[i][2]));
}
mouth.show();
delay(1);
clearPixels();
}
void SimpleExpressionsClass::clearMouth() {
for(int i = 0; i < 7; i++) {
mouth.setPixelColor(i, 0);
}
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 -----------------------------------------------------//
///////////////////////////////////////////////////////////////////
void SimpleExpressionsClass::_tone (float noteFrequency, long noteDuration, int silentDuration){
if(silentDuration==0){silentDuration=1;}
#if defined(ESP32)
ledcWriteTone(ledc_channel, noteFrequency);
delay(noteDuration); // milliseconds
ledcWrite(ledc_channel, 0); // notone
#else
tone(buzzerPin, noteFrequency, noteDuration);
delay(noteDuration); // milliseconds
#endif
delay(silentDuration);
}
void SimpleExpressionsClass::bendTones (float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration){
//Examples:
// bendTones (880, 2093, 1.02, 18, 1);
// bendTones (note_A5, note_C7, 1.02, 18, 0);
if(silentDuration==0){silentDuration=1;}
if(initFrequency < finalFrequency)
{
for (int i=initFrequency; i<finalFrequency; i=i*prop) {
_tone(i, noteDuration, silentDuration);
}
} else{
for (int i=initFrequency; i>finalFrequency; i=i/prop) {
_tone(i, noteDuration, silentDuration);
}
}
}
void SimpleExpressionsClass::playSound(int soundName){
switch(soundName){
case S_CONNECTION:
_tone(NOTE_E5,50,30);
_tone(NOTE_E6,55,25);
_tone(NOTE_A6,60,10);
break;
case S_DISCONNECTION:
_tone(NOTE_E5,50,30);
_tone(NOTE_A6,55,25);
_tone(NOTE_E6,50,10);
break;
case S_BUTTON_PUSHED:
bendTones (NOTE_E6, NOTE_G6, 1.03, 20, 2);
delay(30);
bendTones (NOTE_E6, NOTE_D7, 1.04, 10, 2);
break;
case S_MODE1:
bendTones (NOTE_E6, NOTE_A6, 1.02, 30, 10); //1318.51 to 1760
break;
case S_MODE2:
bendTones (NOTE_G6, NOTE_D7, 1.03, 30, 10); //1567.98 to 2349.32
break;
case S_MODE3:
_tone(NOTE_E6,50,100); //D6
_tone(NOTE_G6,50,80); //E6
_tone(NOTE_D7,300,0); //G6
break;
case S_SURPRISE:
bendTones(800, 2150, 1.02, 10, 1);
bendTones(2149, 800, 1.03, 7, 1);
break;
case S_OHOOH:
bendTones(880, 2000, 1.04, 8, 3); //A5 = 880
delay(200);
for (int i=880; i<2000; i=i*1.04) {
_tone(NOTE_B5,5,10);
}
break;
case S_OHOOH2:
bendTones(1880, 3000, 1.03, 8, 3);
delay(200);
for (int i=1880; i<3000; i=i*1.03) {
_tone(NOTE_C6,10,10);
}
break;
case S_CUDDLY:
bendTones(700, 900, 1.03, 16, 4);
bendTones(899, 650, 1.01, 18, 7);
break;
case S_SLEEPING:
bendTones(100, 500, 1.04, 10, 10);
delay(500);
bendTones(400, 100, 1.04, 10, 1);
break;
case S_HAPPY:
bendTones(1500, 2500, 1.05, 20, 8);
bendTones(2499, 1500, 1.05, 25, 8);
break;
case S_SUPER_HAPPY:
bendTones(2000, 6000, 1.05, 8, 3);
delay(50);
bendTones(5999, 2000, 1.05, 13, 2);
break;
case S_HAPPY_SHORT:
bendTones(1500, 2000, 1.05, 15, 8);
delay(100);
bendTones(1900, 2500, 1.05, 10, 8);
break;
case S_SAD:
bendTones(880, 669, 1.02, 20, 200);
break;
case S_CONFUSED:
bendTones(1000, 1700, 1.03, 8, 2);
bendTones(1699, 500, 1.04, 8, 3);
bendTones(1000, 1700, 1.05, 9, 10);
break;
case S_FART1:
bendTones(1600, 3000, 1.02, 2, 15);
break;
case S_FART2:
bendTones(2000, 6000, 1.02, 2, 20);
break;
case S_FART3:
bendTones(1600, 4000, 1.02, 2, 20);
bendTones(4000, 3000, 1.02, 2, 20);
break;
case PIRATES:
// This is funny but very experimental and probably take long haha =P
for (int i = 0; i < 203; i++) { //203 is the total number of music notes in the song
int wait = duration[i] * songspeed;
_tone( notes[i], wait, 0); //tone(pin,frequency,duration)
}
break;
}
}
SimpleExpressionsClass SimpleExpressions;

46
src/SimpleExpressions.h Executable file
View File

@ -0,0 +1,46 @@
#ifndef SimpleExpressions_h
#define SimpleExpressions_h
#include <Adafruit_NeoPixel.h>
#ifdef __AVR__
#include <avr/power.h>
#endif
#include "Shapes.h"
#include "Sounds.h"
#define debug true
class SimpleExpressionsClass
{
public:
// General
void init(int mouthPin, int buzzerPin);
// Mouths
void printMouth(int number, int r, int g, int b);
void writeMouth(char mouthName[], int r, int g, int b);
void printMouth(int number);
void writeMouth(char mouthName[]);
void clearMouth();
void writeMouthGeneric(const int mouthArray[7][3]);
void writeMouthGeneric(const bool mouthArray[7], int r, int g, int b);
// Sounds
void _tone (float noteFrequency, long noteDuration, int silentDuration);
void bendTones (float initFrequency, float finalFrequency, float prop, long noteDuration, int silentDuration);
void playSound(int soundName);
private:
Adafruit_NeoPixel mouth;
void clearPixels();
int buzzerPin;
};
extern SimpleExpressionsClass SimpleExpressions;
#endif

245
src/Sounds.h Normal file
View File

@ -0,0 +1,245 @@
#ifndef Sounds_h
#define Sounds_h
//***********************************************************************************
//*********************************SOUNDS DEFINES************************************
//***********************************************************************************
// Reference: This list was adapted from the table located here:
// http://www.phy.mtu.edu/~suits/notefreqs.html
#define NOTE_C0 16.35 //C0
#define NOTE_Db0 17.32 //C#0/Db0
#define NOTE_D0 18.35 //D0
#define NOTE_Eb0 19.45 //D#0/Eb0
#define NOTE_E0 20.6 //E0
#define NOTE_F0 21.83 //F0
#define NOTE_Gb0 23.12 //F#0/Gb0
#define NOTE_G0 24.5 //G0
#define NOTE_Ab0 25.96 //G#0/Ab0
#define NOTE_A0 27.5 //A0
#define NOTE_Bb0 29.14 //A#0/Bb0
#define NOTE_B0 30.87 //B0
#define NOTE_C1 32.7 //C1
#define NOTE_Db1 34.65 //C#1/Db1
#define NOTE_D1 36.71 //D1
#define NOTE_Eb1 38.89 //D#1/Eb1
#define NOTE_E1 41.2 //E1
#define NOTE_F1 43.65 //F1
#define NOTE_Gb1 46.25 //F#1/Gb1
#define NOTE_G1 49 //G1
#define NOTE_Ab1 51.91 //G#1/Ab1
#define NOTE_A1 55 //A1
#define NOTE_Bb1 58.27 //A#1/Bb1
#define NOTE_B1 61.74 //B1
#define NOTE_C2 65.41 //C2 (Middle C)
#define NOTE_Db2 69.3 //C#2/Db2
#define NOTE_D2 73.42 //D2
#define NOTE_Eb2 77.78 //D#2/Eb2
#define NOTE_E2 82.41 //E2
#define NOTE_F2 87.31 //F2
#define NOTE_Gb2 92.5 //F#2/Gb2
#define NOTE_G2 98 //G2
#define NOTE_Ab2 103.83 //G#2/Ab2
#define NOTE_A2 110 //A2
#define NOTE_Bb2 116.54 //A#2/Bb2
#define NOTE_B2 123.47 //B2
#define NOTE_C3 130.81 //C3
#define NOTE_Db3 138.59 //C#3/Db3
#define NOTE_D3 146.83 //D3
#define NOTE_Eb3 155.56 //D#3/Eb3
#define NOTE_E3 164.81 //E3
#define NOTE_F3 174.61 //F3
#define NOTE_Gb3 185 //F#3/Gb3
#define NOTE_G3 196 //G3
#define NOTE_Ab3 207.65 //G#3/Ab3
#define NOTE_A3 220 //A3
#define NOTE_Bb3 233.08 //A#3/Bb3
#define NOTE_B3 246.94 //B3
#define NOTE_C4 261.63 //C4
#define NOTE_Db4 277.18 //C#4/Db4
#define NOTE_D4 293.66 //D4
#define NOTE_Eb4 311.13 //D#4/Eb4
#define NOTE_E4 329.63 //E4
#define NOTE_F4 349.23 //F4
#define NOTE_Gb4 369.99 //F#4/Gb4
#define NOTE_G4 392 //G4
#define NOTE_Ab4 415.3 //G#4/Ab4
#define NOTE_A4 440 //A4
#define NOTE_Bb4 466.16 //A#4/Bb4
#define NOTE_B4 493.88 //B4
#define NOTE_C5 523.25 //C5
#define NOTE_Db5 554.37 //C#5/Db5
#define NOTE_D5 587.33 //D5
#define NOTE_Eb5 622.25 //D#5/Eb5
#define NOTE_E5 659.26 //E5
#define NOTE_F5 698.46 //F5
#define NOTE_Gb5 739.99 //F#5/Gb5
#define NOTE_G5 783.99 //G5
#define NOTE_Ab5 830.61 //G#5/Ab5
#define NOTE_A5 880 //A5
#define NOTE_Bb5 932.33 //A#5/Bb5
#define NOTE_B5 987.77 //B5
#define NOTE_C6 1046.5 //C6
#define NOTE_Db6 1108.73 //C#6/Db6
#define NOTE_D6 1174.66 //D6
#define NOTE_Eb6 1244.51 //D#6/Eb6
#define NOTE_E6 1318.51 //E6
#define NOTE_F6 1396.91 //F6
#define NOTE_Gb6 1479.98 //F#6/Gb6
#define NOTE_G6 1567.98 //G6
#define NOTE_Ab6 1661.22 //G#6/Ab6
#define NOTE_A6 1760 //A6
#define NOTE_Bb6 1864.66 //A#6/Bb6
#define NOTE_B6 1975.53 //B6
#define NOTE_C7 2093 //C7
#define NOTE_Db7 2217.46 //C#7/Db7
#define NOTE_D7 2349.32 //D7
#define NOTE_Eb7 2489.02 //D#7/Eb7
#define NOTE_E7 2637.02 //E7
#define NOTE_F7 2793.83 //F7
#define NOTE_Gb7 2959.96 //F#7/Gb7
#define NOTE_G7 3135.96 //G7
#define NOTE_Ab7 3322.44 //G#7/Ab7
#define NOTE_A7 3520 //A7
#define NOTE_Bb7 3729.31 //A#7/Bb7
#define NOTE_B7 3951.07 //B7
#define NOTE_C8 4186.01 //C8
#define NOTE_Db8 4434.92 //C#8/Db8
#define NOTE_D8 4698.64 //D8
#define NOTE_Eb8 4978.03 //D#8/Eb8
#define S_CONNECTION 0
#define S_DISCONNECTION 1
#define S_BUTTON_PUSHED 2
#define S_MODE1 3
#define S_MODE2 4
#define S_MODE3 5
#define S_SURPRISE 6
#define S_OHOOH 7
#define S_OHOOH2 8
#define S_CUDDLY 9
#define S_SLEEPING 10
#define S_HAPPY 11
#define S_SUPER_HAPPY 12
#define S_HAPPY_SHORT 13
#define S_SAD 14
#define S_CONFUSED 15
#define S_FART1 16
#define S_FART2 17
#define S_FART3 18
#define PIRATES 19
const int songspeed = 1;
const PROGMEM int notes[] = { //Note of the song, 0 is a rest/pulse
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
NOTE_A4, NOTE_A4,
//Repeat of first part
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_C5, NOTE_D5, NOTE_B4, NOTE_B4, 0,
NOTE_A4, NOTE_G4, NOTE_A4, 0,
NOTE_E4, NOTE_G4, NOTE_A4, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_D5, NOTE_D5, 0,
NOTE_D5, NOTE_E5, NOTE_F5, NOTE_F5, 0,
NOTE_E5, NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_B4, NOTE_C5, NOTE_C5, 0,
NOTE_D5, NOTE_E5, NOTE_A4, 0,
NOTE_A4, NOTE_C5, NOTE_B4, NOTE_B4, 0,
NOTE_C5, NOTE_A4, NOTE_B4, 0,
//End of Repeat
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4,
NOTE_E5, 0, 0, NOTE_F5, 0, 0,
NOTE_E5, NOTE_E5, 0, NOTE_G5, 0, NOTE_E5, NOTE_D5, 0, 0,
NOTE_D5, 0, 0, NOTE_C5, 0, 0,
NOTE_B4, NOTE_C5, 0, NOTE_B4, 0, NOTE_A4
};
//*****************************************
const PROGMEM int duration[] = { //duration of each note (in ms) Quarter Note is set to 250 ms
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
250, 125,
//Rpeat of First Part
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 375, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 250, 125, 125,
125, 125, 125, 250, 125,
125, 125, 250, 125, 125,
250, 125, 250, 125,
125, 125, 250, 125, 125,
125, 125, 375, 375,
//End of Repeat
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 125, 125, 125, 375,
250, 125, 375, 250, 125, 375,
125, 125, 125, 125, 125, 500
};
#endif