buzzer: fix for PWM frequencies
authorKiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Fri, 20 Jun 2014 11:04:25 +0000 (11:04 +0000)
committerKiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
Fri, 20 Jun 2014 11:04:25 +0000 (11:04 +0000)
Signed-off-by: Kiveisha Yevgeniy <yevgeniy.kiveisha@intel.com>
examples/buzzer-sound.cxx
src/buzzer/buzzer.cxx
src/buzzer/buzzer.h

index a3a5377..9d88b9f 100644 (file)
 #include <unistd.h>
 #include <iostream>
 #include <signal.h>
-
 #include "buzzer.h"
 
-int running = 0;
-
-void
-sig_handler(int signo)
-{
-    printf("got signal\n");
-    if (signo == SIGINT) {
-        printf("exiting application\n");
-        running = 1;
-    }
-}
 
 int
 main(int argc, char **argv) {
@@ -51,13 +39,10 @@ main(int argc, char **argv) {
     std::cout << sound->name() << std::endl;
     // play sound (DO, RE, ME, etc...)
 
-    signal(SIGINT, sig_handler);
-
-    while (!running) {
-        for (int chord_ind = 0; chord_ind < 9; chord_ind++) {
-            std::cout << sound->playSound(chord[chord_ind]) << std::endl;
-            usleep(1000);
-        }
+    for (int chord_ind = 0; chord_ind < 7; chord_ind++) {
+        // play one second for each chord
+        std::cout << sound->playSound(chord[chord_ind], 1000000) << std::endl;
+        usleep(100000);
     }
     //! [Interesting]
 
index 674ff22..cc93722 100644 (file)
@@ -34,14 +34,14 @@ Buzzer::Buzzer (int pinNumber) {
     m_name = "Buzzer";
 }
 
-int Buzzer::playSound (int note) {
+int Buzzer::playSound (int note, int delay) {
     maa_pwm_enable (m_pwm_context, 1);
     maa_pwm_period_us (m_pwm_context, note);
-    maa_pwm_write (m_pwm_context, 50.0);
-    usleep (10000);
+    maa_pwm_pulsewidth_us (m_pwm_context, note / 2);
+    usleep (delay);
     maa_pwm_enable (m_pwm_context, 0);
 
-    return 0;
+    return note;
 }
 
 Buzzer::~Buzzer() {
index d22dcbd..15a5e01 100644 (file)
 #include <string>
 #include <maa/pwm.h>
 
-#define  DO     3830000    // 261 Hz
-#define  RE     3400000    // 294 Hz
-#define  MI     3038000    // 329 Hz
-#define  FA     2864000    // 349 Hz
-#define  SOL    2550000    // 392 Hz
-#define  LA     2272000    // 440 Hz
-#define  SI     2028000    // 493 Hz
+#define  DO     3300    // 261 Hz 3830
+#define  RE     2930    // 294 Hz
+#define  MI     2600    // 329 Hz
+#define  FA     2460    // 349 Hz
+#define  SOL    2190    // 392 Hz
+#define  LA     1960    // 440 Hz
+#define  SI     1750    // 493 Hz
 
 namespace upm {
 
@@ -41,7 +41,7 @@ namespace upm {
  *
  * This file defines the Buzzer C++ interface for libbuzzer
  *
- * @snippet es08a.cxx Interesting
+ * @snippet buzzer-sound.cxx Interesting
  *
  */
 class Buzzer {
@@ -62,8 +62,9 @@ class Buzzer {
          * Play chords.
          *
          * @param note chords (DO, RE, ME, etc...)
+         * @param delay time in microsec for playing the sound
          */
-        int playSound (int note);
+        int playSound (int note, int delay);
 
         /**
          * Return name of the component