Code to control a device which produces annoying tones and haptic motor pulses over bluetooth.
Revision 0:44b37b90b0ef, committed 2016-11-02
- Comitter:
- MatthewJGTate
- Date:
- Wed Nov 02 20:28:27 2016 +0000
- Commit message:
- Code to control a device which produces annoying tones and haptic motor pulses.
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/DRV2605.lib Wed Nov 02 20:28:27 2016 +0000 @@ -0,0 +1,1 @@ +https://developer.mbed.org/users/electromotivated/code/DRV2605/#3b2b4f34aaca
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/SongPlayer.h Wed Nov 02 20:28:27 2016 +0000
@@ -0,0 +1,41 @@
+#include "mbed.h"
+// new class to play a note on Speaker based on PwmOut class
+class SongPlayer
+{
+public:
+ SongPlayer(PinName pin) : _pin(pin) {
+// _pin(pin) means pass pin to the constructor
+ }
+// class method to play a note based on PwmOut class
+ void PlaySong(float frequency[], float duration[], float volume=1.0) {
+ vol = volume;
+ notecount = 0;
+ _pin.period(1.0/frequency[notecount]);
+ _pin = volume/2.0;
+ noteduration.attach(this,&SongPlayer::nextnote, duration[notecount]);
+ // setup timer to interrupt for next note to play
+ frequencyptr = frequency;
+ durationptr = duration;
+ //returns after first note starts to play
+ }
+ void nextnote();
+private:
+ Timeout noteduration;
+ PwmOut _pin;
+ int notecount;
+ float vol;
+ float * frequencyptr;
+ float * durationptr;
+};
+//Interrupt Routine to play next note
+void SongPlayer::nextnote()
+{
+ _pin = 0.0;
+ notecount++; //setup next note in song
+ if (durationptr[notecount]!=0.0) {
+ _pin.period(1.0/frequencyptr[notecount]);
+ noteduration.attach(this,&SongPlayer::nextnote, durationptr[notecount]);
+ _pin = vol/2.0;
+ } else
+ _pin = 0.0; //turn off on last note
+}
\ No newline at end of file
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp Wed Nov 02 20:28:27 2016 +0000
@@ -0,0 +1,114 @@
+#include "mbed.h"
+#include "SongPlayer.h"
+#include "DRV2605.h"
+
+Ticker speaker;
+Ticker hapticMotor;
+SongPlayer mySpeaker(p21);
+DRV2605 haptics(p9, p10);
+Serial Blue(p28,p27);
+BusOut myled(LED1,LED2,LED3,LED4);
+
+//Global Variables
+float note[1] = {0.0};
+float duration[1] = {0.0};
+int randomVibe = 0;
+volatile bool button_ready = 0;
+volatile int bnum = 0;
+volatile int bhit = 0;
+enum statetype {start = 0, got_exclm, got_B, got_num, got_hit};
+statetype state = start;
+
+// Random
+volatile int interruptControl = 1;
+
+void parse_message()
+{
+ switch (state) {
+ case start:
+ if (Blue.getc()=='!') state = got_exclm;
+ else state = start;
+ break;
+ case got_exclm:
+ if (Blue.getc() == 'B') state = got_B;
+ else state = start;
+ break;
+ case got_B:
+ bnum = Blue.getc();
+ state = got_num;
+ break;
+ case got_num:
+ bhit = Blue.getc();
+ state = got_hit;
+ break;
+ case got_hit:
+ if (Blue.getc() == char(~('!' + ' B' + bnum + bhit))) button_ready = 1;
+ state = start;
+ break;
+ default:
+ Blue.getc();
+ state = start;
+ }
+}
+
+void speakerPB1 (void)
+{
+ while (bhit == '1') {
+ myled = 0x01;
+ note[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*6000.0 + 2000.0;
+ duration[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*0.1;
+ mySpeaker.PlaySong(note,duration);
+ }
+}
+
+void motorPB2 (void)
+{
+ while (bhit == '1') {
+ myled = 0x0;
+ haptics.play_waveform(14);
+ }
+}
+
+void motorISR (void)
+{
+ if (interruptControl == 1) {
+ randomVibe = rand()%123 + 1; // Produces random number from 1 to 123
+ haptics.play_waveform(randomVibe);
+ }
+}
+
+void speakerISR (void)
+{
+ if (interruptControl == 1) {
+ note[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*6000.0 + 2000.0;
+ duration[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*0.5;
+ mySpeaker.PlaySong(note,duration);
+ }
+}
+
+
+int main()
+{
+ Blue.attach(&parse_message,Serial::RxIrq);
+ hapticMotor.attach(&motorISR, 0.5);
+ speaker.attach(&speakerISR, 1.5);
+
+ while(1) {
+ if (button_ready == 1 && bnum == '1') {
+ myled = 0x01; //
+ speakerPB1();
+ } else if (button_ready == 1 && bnum == '2') {
+ myled = 0x02;
+ motorPB2();
+ } else if (button_ready == 1 && bnum == '3') {
+ myled = 0x04;
+ interruptControl = 1;
+ } else if (button_ready == 1 && bnum == '4') {
+ myled = 0x08;
+ interruptControl = 0;
+ }
+ }
+}
+
+
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/mbed.bld Wed Nov 02 20:28:27 2016 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/mbed_official/code/mbed/builds/9bcdf88f62b0 \ No newline at end of file