Source

ArduinoProjects / Libraries / Emic2TextToSpeechModule / Emic2TextToSpeechModule..cpp

Full commit
//Author: Jody McAdams
//Website: http://www.jodymcadams.com

#include "Emic2TextToSpeechModule.h"
#include <Arduino.h>

Emic2TextToSpeechModule::Emic2TextToSpeechModule(int rxPin, int txPin, int ledPin)
{
	this->ledPin = ledPin;
	pEmicSerial = new SoftwareSerial(rxPin, txPin);
	
	// define pin modes
	pinMode(ledPin, OUTPUT);
	pinMode(rxPin, INPUT);
	pinMode(txPin, OUTPUT);
	
	// set the data rate for the SoftwareSerial port
	pEmicSerial->begin(9600);
	
	digitalWrite(ledPin, LOW);  // turn LED off
	
	
	// When the Emic 2 powers on, it takes about 3 seconds for it to successfully
	// intialize. It then sends a ":" character to indicate it's ready to accept
	// commands. If the Emic 2 is already initialized, a CR will also cause it
	// to send a ":"
	
	pEmicSerial->print('\n');             // Send a CR in case the system is already up
	while (pEmicSerial->read() != ':');   // When the Emic 2 has initialized and is ready, it will send a single ':' character, so wait here until we receive it
	delay(10);                          // Short delay
	pEmicSerial->flush();                 // Flush the receive buffer
}

void Emic2TextToSpeechModule::SendCommand(char command, int value)
{
	pEmicSerial->print(command);
	pEmicSerial->print(value);
	pEmicSerial->print('\n');
	
	while (pEmicSerial->read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
}

void Emic2TextToSpeechModule::SendCommand(char command)
{
	pEmicSerial->print(command);
	pEmicSerial->print('\n');
	
	while (pEmicSerial->read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
}

void Emic2TextToSpeechModule::SetVolume(int volumeDB)
{
	SendCommand('V',volumeDB);
}


void Emic2TextToSpeechModule::SetVoice(int voiceNumber)
{
	SendCommand('N',voiceNumber);
}

void Emic2TextToSpeechModule::SetSpeakingRate(int rate)
{
	SendCommand('W',rate);
}

void Emic2TextToSpeechModule::SetParser(int parser)
{
	SendCommand('P',parser);
}

void Emic2TextToSpeechModule::PrintAvailableCommands()
{
	SendCommand('H');
}

void Emic2TextToSpeechModule::Say(const char* stringToSay)
{
	pEmicSerial->print('S');
	pEmicSerial->print(stringToSay);
	pEmicSerial->print('\n');
	
	digitalWrite(ledPin, HIGH);         // Turn on LED while Emic is outputting audio
	while (pEmicSerial->read() != ':');   // Wait here until the Emic 2 responds with a ":" indicating it's ready to accept the next command
	digitalWrite(ledPin, LOW);
}