CHENGQI YANG / SP03

Files at this revision

API Documentation at this revision

Comitter:
yangcq88517
Date:
Mon Mar 07 14:38:55 2016 +0000
Parent:
0:2326b6172834
Child:
2:4090898287d5
Commit message:
improvment

Changed in this revision

SP03.cpp Show annotated file Show diff for this revision Revisions of this file
SP03.h Show annotated file Show diff for this revision Revisions of this file
SP03SpeechSetting.h Show diff for this revision Revisions of this file
--- a/SP03.cpp	Wed May 07 13:52:08 2014 +0000
+++ b/SP03.cpp	Mon Mar 07 14:38:55 2016 +0000
@@ -3,47 +3,58 @@
 SP03::SP03(PinName sda, PinName scl): i2c_bus(sda,scl)
 {
     i2c_bus.frequency(CLOCK_RATE);
-}
-
-void SP03::_speak(const char message[],SmartLab_SP03::Speech_Speed speed, SmartLab_SP03::Speech_Volume volume,uint8_t pitch)
-{
-    char speakout[2];
-    speakout[0] = REGISTER_FOR_COMMAND;
-    speakout[1] = SPEAK_OUT_THE_BUFFER;
-    int length = 6 + strlen(message);
-    char _message[length];
-    _message[0] = REGISTER_FOR_COMMAND;
-    _message[1] = 0x00;
-    _message[2] = volume;
-    _message[3] = speed;
-    _message[4] = pitch;
-    for (int i = 5; i < length; i++)
-        _message[i] = message[i - 5];
-    i2c_bus.write(DEFAULT_ADDRESS, _message, length);
-    i2c_bus.write(DEFAULT_ADDRESS, speakout, 2);
+    _volume = VOLUME_MAX;
+    _speed = SPEED_NORMAL;
 }
 
-void SP03::Speak(const char message[])
+void SP03::speak(const char * message)
 {
-    _speak(message, SmartLab_SP03::NORMAL, SmartLab_SP03::MAX, DEFAULT_SPEECH_PITCH);
-}
+    i2c_bus.start();
+    i2c_bus.write(DEFAULT_ADDRESS);
+    i2c_bus.write(REGISTER_FOR_COMMAND);
+    i2c_bus.write(0x00);
+    i2c_bus.write(_volume);
+    i2c_bus.write(_speed);
+    i2c_bus.write(DEFAULT_SPEECH_PITCH);
+    int i = 0;
+    while (true) {
+        i2c_bus.write(message[i++]);
+        if (message[i] == 0x00)
+            break;
+    }
+    i2c_bus.write(0x00);
+    i2c_bus.stop();
 
-void SP03::Speak(const char message[],SmartLab_SP03::Speech_Speed speed, SmartLab_SP03::Speech_Volume volume)
-{
-    _speak(message, speed, volume, DEFAULT_SPEECH_PITCH);
+    i2c_bus.start();
+    i2c_bus.write(DEFAULT_ADDRESS);
+    i2c_bus.write(REGISTER_FOR_COMMAND);
+    i2c_bus.write(SPEAK_OUT_THE_BUFFER);
+    i2c_bus.stop();
 }
 
-void SP03::Speak(const char message[],SmartLab_SP03::Speech_Speed speed, SmartLab_SP03::Speech_Volume volume,uint8_t pitch)
+void SP03::setSpeed(char speed)
 {
-    _speak(message, speed, volume, pitch);
+    _speed = speed;
+}
+
+void SP03::setVolume(char volume)
+{
+    _volume = volume;
 }
 
-bool SP03::IsSpeaking()
+bool SP03::isSpeaking()
 {
-    char m[1]= {REGISTER_FOR_COMMAND};
-    i2c_bus.write(DEFAULT_ADDRESS,m ,1);
-    i2c_bus.read(DEFAULT_ADDRESS, m, 1);
-    if (m[0] == 0x00)
+    i2c_bus.start();
+    i2c_bus.write(DEFAULT_ADDRESS);
+    i2c_bus.write(REGISTER_FOR_COMMAND);
+    i2c_bus.stop();
+
+    i2c_bus.start();
+    i2c_bus.write(DEFAULT_ADDRESS | 0x01);
+    int value = i2c_bus.read(0x00);
+    i2c_bus.stop();
+
+    if (value == 0x00)
         return false;
     else return true;
 }
\ No newline at end of file
--- a/SP03.h	Wed May 07 13:52:08 2014 +0000
+++ b/SP03.h	Mon Mar 07 14:38:55 2016 +0000
@@ -1,33 +1,66 @@
 #ifndef Smartlab_Drive_SP03
 #define Smartlab_Drive_SP03
 
-#include "SP03SpeechSetting.h"
 #include "mbed.h"
 
 class SP03
 {
 private:
-    static const uint8_t DEFAULT_ADDRESS = 0xC4;
+    static const char DEFAULT_ADDRESS = 0xC4;
     static const int CLOCK_RATE = 100000;
-    static const uint8_t REGISTER_FOR_COMMAND = 0x00;
-    static const uint8_t REGISTER_FOR_SOFTWARE_REVISION_NUMBER = 0x01;
-    static const uint8_t SPEAK_OUT_THE_BUFFER = 0x40;
-    static const uint8_t DEFAULT_SPEECH_PITCH = 0x03;
+    static const char REGISTER_FOR_COMMAND = 0x00;
+    static const char REGISTER_FOR_SOFTWARE_REVISION_NUMBER = 0x01;
+    static const char SPEAK_OUT_THE_BUFFER = 0x40;
+
+    static const char DEFAULT_SPEECH_PITCH = 0x03;
+    
+    char _volume;
+    
+    char _speed;
 
     I2C i2c_bus;
-
-    void _speak(const char message[],SmartLab_SP03::Speech_Speed speed, SmartLab_SP03::Speech_Volume volume,uint8_t pitch);
 public :
 
+    static const char SPEED_NORMAL  = 0x05;
+    static const char SPEED_FAST = 0x02;
+    static const char SPEED_SLOW = 0x06;
+
+    static const char VOLUME_MAX = 0x00;
+    static const char VOLUME_MEDIUM = 0x03;
+    static const char VOLUME_MIN = 0x06;
+
+    /** Construct
+     *
+     * @param sda I2C sda signal
+     * @param scl I2C scl signal
+     */
     SP03(PinName sda, PinName scl);
 
-    void Speak(const char message[]);
+    /** Set the speed of the speech
+     *
+     * @param message NULL terminated char array
+     */
+    void speak(const char * message);
+
+    /** Set the speed of the speech
+     *
+     * @param speed [SPEED_NORMAL = 0x05, SPEED_FAST = 0x02, SPEED_SLOW = 0x06]
+     */
+    void setSpeed(char speed);
 
-    void Speak(const char message[],SmartLab_SP03::Speech_Speed speed, SmartLab_SP03::Speech_Volume volume);
-    
-    void Speak(const char message[],SmartLab_SP03::Speech_Speed speed, SmartLab_SP03::Speech_Volume volume,uint8_t pitch);
-    
-    bool IsSpeaking();
+    /** Set the volume of the speech
+     *
+     * @param volume [VOLUME_MAX = 0x00, VOLUME_MEDIUM = 0x03, VOLUME_MIN = 0x06]
+     */
+    void setVolume(char volume);
+
+    /** Check if the SP03 is currently talking
+     *
+     * @returns
+     *  ture device is talking and no command can be send,
+     *  false command can be issued
+     */
+    bool isSpeaking();
 };
 
 #endif
\ No newline at end of file
--- a/SP03SpeechSetting.h	Wed May 07 13:52:08 2014 +0000
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#ifndef Smartlab_Drive_SP03SpeechSetting
-#define Smartlab_Drive_SP03SpeechSetting
-namespace SmartLab_SP03
-{
-enum Speech_Speed {
-    NORMAL  = 0x05,
-    FAST = 0x02,
-    SLOW = 0x06,
-};
-
-enum Speech_Volume {
-    MAX = 0x00,
-    MEDIUM = 0x03,
-    MIN = 0x06,
-};
-}
-#endif
\ No newline at end of file