Hacking into the iClicker2 for adding remote access abilities. http://www.amazon.com/I-Clicker-2-I-CLICKER/dp/1429280476

Dependencies:   mbed

Files at this revision

API Documentation at this revision

Comitter:
sjsm3
Date:
Tue Dec 09 13:43:26 2014 +0000
Parent:
1:7ea05d050158
Commit message:
added speaker functionality

Changed in this revision

Speaker.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Speaker.h	Tue Dec 09 13:43:26 2014 +0000
@@ -0,0 +1,51 @@
+class Speaker
+{
+public:
+    Speaker(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the Speaker Constructor
+// precompute 32 sample points on one sine wave cycle
+// used for continuous sine wave output later
+        for(int k=0; k<32; k++) {
+            Analog_out_data[k] = int (65536.0 * ((1.0 + sin((float(k)/32.0*6.28318530717959)))/2.0));
+            // scale the sine wave to 16-bits - as needed for AnalogOut write_u16 arg
+        }
+ 
+    }
+// class method to play a note based on AnalogOut class
+    void PlayNote(float frequency, float duration, float volume) {
+        // scale samples using current volume level arg
+        for(int k=0; k<32; k++) {
+            Analog_scaled_data[k] = Analog_out_data[k] * volume;
+        }
+        // reset to start of sample array
+        i=0;
+        // turn on timer interrupts to start sine wave output
+        Sample_Period.attach(this, &Speaker::Sample_timer_interrupt, 1.0/(frequency*32.0));
+        // play note for specified time
+        wait(duration);
+        // turns off timer interrupts
+        Sample_Period.detach();
+        // sets output to mid range - analog zero
+        this->_pin.write_u16(32768);
+ 
+    }
+private:
+// sets up specified pin for analog using AnalogOut class
+    AnalogOut _pin;
+    // set up a timer to be used for sample rate interrupts
+    Ticker Sample_Period;
+ 
+    //variables used by interrupt routine and PlayNote
+    volatile int i;
+    short unsigned Analog_out_data[32];
+    short unsigned Analog_scaled_data[32];
+ 
+// Interrupt routine
+// used to output next analog sample whenever a timer interrupt occurs
+    void Sample_timer_interrupt(void) {
+        // send next analog sample out to D to A
+        this->_pin.write_u16(Analog_scaled_data[i]);
+        // increment pointer and wrap around back to 0 at 32
+        i = (i+1) & 0x01F;
+    }
+};
\ No newline at end of file
--- a/main.cpp	Mon Dec 08 20:31:02 2014 +0000
+++ b/main.cpp	Tue Dec 09 13:43:26 2014 +0000
@@ -1,11 +1,12 @@
 #include "mbed.h"
 #include <string>
 #include <vector>
+#include "Speaker.h"
 
 // These are used for setting the serial buffer size and how long the mbed should wait between updates
 static const float SLEEP_TIME = 0.5;    // x86 background process will only have updates every 1 second in ideal situations
 static const float PIN_DELAY = 0.05;   // the time to wait for allowing the relays to fully cycle between transitions
-static const float POWER_UP_TIME = 5.0; // seconds for the device clicker to go from an OFF to ON state
+static const float POWER_UP_TIME = 0; // seconds for the device clicker to go from an OFF to ON state
 static const int NUM_CMDS = 8;
 
 // Used for signaling to the main loop if new serial data has arrived by the serial rx interrupt
@@ -23,6 +24,12 @@
 // Global string for storing the received serial data on the serial rx interrupt
 string gStr;
 
+Speaker mySpeaker(p18);
+//AnalogOut DACout(p18);
+//wave_player waver(&DACout);
+//LocalFileSystem local("local");
+//FILE *wave_file;
+
 
 void rx_interrupt(void)
 {
@@ -93,6 +100,8 @@
     commands[5] = "clickerC\n";
     commands[6] = "clickerD\n";
     commands[7] = "clickerE\n";
+    
+    //wave_file = fopen("/local/p.wav","r");
 
     // Initialize all relays to off state
     for (int i=0; i<6; i++)
@@ -151,7 +160,11 @@
                     break;
 
                 case 3:  // sayHere
-                    pc.printf("    Audio currently unsupported\r\n");
+                    pc.printf("    Audio playing...\r\n");
+                    mySpeaker.PlayNote(969.0,1.5,1.0);
+                    //wave_file=fopen("/local/P.WAV","r");
+                    //waver.play(wave_file);
+                    //fclose(wave_file);
                     break;
 
                 case 4: // clickerA