Code to control a device which produces annoying tones and haptic motor pulses over bluetooth.
Embed:
(wiki syntax)
Show/hide line numbers
main.cpp
00001 #include "mbed.h" 00002 #include "SongPlayer.h" 00003 #include "DRV2605.h" 00004 00005 Ticker speaker; 00006 Ticker hapticMotor; 00007 SongPlayer mySpeaker(p21); 00008 DRV2605 haptics(p9, p10); 00009 Serial Blue(p28,p27); 00010 BusOut myled(LED1,LED2,LED3,LED4); 00011 00012 //Global Variables 00013 float note[1] = {0.0}; 00014 float duration[1] = {0.0}; 00015 int randomVibe = 0; 00016 volatile bool button_ready = 0; 00017 volatile int bnum = 0; 00018 volatile int bhit = 0; 00019 enum statetype {start = 0, got_exclm, got_B, got_num, got_hit}; 00020 statetype state = start; 00021 00022 // Random 00023 volatile int interruptControl = 1; 00024 00025 void parse_message() 00026 { 00027 switch (state) { 00028 case start: 00029 if (Blue.getc()=='!') state = got_exclm; 00030 else state = start; 00031 break; 00032 case got_exclm: 00033 if (Blue.getc() == 'B') state = got_B; 00034 else state = start; 00035 break; 00036 case got_B: 00037 bnum = Blue.getc(); 00038 state = got_num; 00039 break; 00040 case got_num: 00041 bhit = Blue.getc(); 00042 state = got_hit; 00043 break; 00044 case got_hit: 00045 if (Blue.getc() == char(~('!' + ' B' + bnum + bhit))) button_ready = 1; 00046 state = start; 00047 break; 00048 default: 00049 Blue.getc(); 00050 state = start; 00051 } 00052 } 00053 00054 void speakerPB1 (void) 00055 { 00056 while (bhit == '1') { 00057 myled = 0x01; 00058 note[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*6000.0 + 2000.0; 00059 duration[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*0.1; 00060 mySpeaker.PlaySong(note,duration); 00061 } 00062 } 00063 00064 void motorPB2 (void) 00065 { 00066 while (bhit == '1') { 00067 myled = 0x0; 00068 haptics.play_waveform(14); 00069 } 00070 } 00071 00072 void motorISR (void) 00073 { 00074 if (interruptControl == 1) { 00075 randomVibe = rand()%123 + 1; // Produces random number from 1 to 123 00076 haptics.play_waveform(randomVibe); 00077 } 00078 } 00079 00080 void speakerISR (void) 00081 { 00082 if (interruptControl == 1) { 00083 note[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*6000.0 + 2000.0; 00084 duration[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*0.5; 00085 mySpeaker.PlaySong(note,duration); 00086 } 00087 } 00088 00089 00090 int main() 00091 { 00092 Blue.attach(&parse_message,Serial::RxIrq); 00093 hapticMotor.attach(&motorISR, 0.5); 00094 speaker.attach(&speakerISR, 1.5); 00095 00096 while(1) { 00097 if (button_ready == 1 && bnum == '1') { 00098 myled = 0x01; // 00099 speakerPB1(); 00100 } else if (button_ready == 1 && bnum == '2') { 00101 myled = 0x02; 00102 motorPB2(); 00103 } else if (button_ready == 1 && bnum == '3') { 00104 myled = 0x04; 00105 interruptControl = 1; 00106 } else if (button_ready == 1 && bnum == '4') { 00107 myled = 0x08; 00108 interruptControl = 0; 00109 } 00110 } 00111 } 00112 00113 00114
Generated on Mon Jul 25 2022 07:05:51 by
1.7.2