Matthew Tate
/
IoT_Haptic_Noise_Irritator
Code to control a device which produces annoying tones and haptic motor pulses over bluetooth.
main.cpp@0:44b37b90b0ef, 2016-11-02 (annotated)
- Committer:
- MatthewJGTate
- Date:
- Wed Nov 02 20:28:27 2016 +0000
- Revision:
- 0:44b37b90b0ef
Code to control a device which produces annoying tones and haptic motor pulses.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
MatthewJGTate | 0:44b37b90b0ef | 1 | #include "mbed.h" |
MatthewJGTate | 0:44b37b90b0ef | 2 | #include "SongPlayer.h" |
MatthewJGTate | 0:44b37b90b0ef | 3 | #include "DRV2605.h" |
MatthewJGTate | 0:44b37b90b0ef | 4 | |
MatthewJGTate | 0:44b37b90b0ef | 5 | Ticker speaker; |
MatthewJGTate | 0:44b37b90b0ef | 6 | Ticker hapticMotor; |
MatthewJGTate | 0:44b37b90b0ef | 7 | SongPlayer mySpeaker(p21); |
MatthewJGTate | 0:44b37b90b0ef | 8 | DRV2605 haptics(p9, p10); |
MatthewJGTate | 0:44b37b90b0ef | 9 | Serial Blue(p28,p27); |
MatthewJGTate | 0:44b37b90b0ef | 10 | BusOut myled(LED1,LED2,LED3,LED4); |
MatthewJGTate | 0:44b37b90b0ef | 11 | |
MatthewJGTate | 0:44b37b90b0ef | 12 | //Global Variables |
MatthewJGTate | 0:44b37b90b0ef | 13 | float note[1] = {0.0}; |
MatthewJGTate | 0:44b37b90b0ef | 14 | float duration[1] = {0.0}; |
MatthewJGTate | 0:44b37b90b0ef | 15 | int randomVibe = 0; |
MatthewJGTate | 0:44b37b90b0ef | 16 | volatile bool button_ready = 0; |
MatthewJGTate | 0:44b37b90b0ef | 17 | volatile int bnum = 0; |
MatthewJGTate | 0:44b37b90b0ef | 18 | volatile int bhit = 0; |
MatthewJGTate | 0:44b37b90b0ef | 19 | enum statetype {start = 0, got_exclm, got_B, got_num, got_hit}; |
MatthewJGTate | 0:44b37b90b0ef | 20 | statetype state = start; |
MatthewJGTate | 0:44b37b90b0ef | 21 | |
MatthewJGTate | 0:44b37b90b0ef | 22 | // Random |
MatthewJGTate | 0:44b37b90b0ef | 23 | volatile int interruptControl = 1; |
MatthewJGTate | 0:44b37b90b0ef | 24 | |
MatthewJGTate | 0:44b37b90b0ef | 25 | void parse_message() |
MatthewJGTate | 0:44b37b90b0ef | 26 | { |
MatthewJGTate | 0:44b37b90b0ef | 27 | switch (state) { |
MatthewJGTate | 0:44b37b90b0ef | 28 | case start: |
MatthewJGTate | 0:44b37b90b0ef | 29 | if (Blue.getc()=='!') state = got_exclm; |
MatthewJGTate | 0:44b37b90b0ef | 30 | else state = start; |
MatthewJGTate | 0:44b37b90b0ef | 31 | break; |
MatthewJGTate | 0:44b37b90b0ef | 32 | case got_exclm: |
MatthewJGTate | 0:44b37b90b0ef | 33 | if (Blue.getc() == 'B') state = got_B; |
MatthewJGTate | 0:44b37b90b0ef | 34 | else state = start; |
MatthewJGTate | 0:44b37b90b0ef | 35 | break; |
MatthewJGTate | 0:44b37b90b0ef | 36 | case got_B: |
MatthewJGTate | 0:44b37b90b0ef | 37 | bnum = Blue.getc(); |
MatthewJGTate | 0:44b37b90b0ef | 38 | state = got_num; |
MatthewJGTate | 0:44b37b90b0ef | 39 | break; |
MatthewJGTate | 0:44b37b90b0ef | 40 | case got_num: |
MatthewJGTate | 0:44b37b90b0ef | 41 | bhit = Blue.getc(); |
MatthewJGTate | 0:44b37b90b0ef | 42 | state = got_hit; |
MatthewJGTate | 0:44b37b90b0ef | 43 | break; |
MatthewJGTate | 0:44b37b90b0ef | 44 | case got_hit: |
MatthewJGTate | 0:44b37b90b0ef | 45 | if (Blue.getc() == char(~('!' + ' B' + bnum + bhit))) button_ready = 1; |
MatthewJGTate | 0:44b37b90b0ef | 46 | state = start; |
MatthewJGTate | 0:44b37b90b0ef | 47 | break; |
MatthewJGTate | 0:44b37b90b0ef | 48 | default: |
MatthewJGTate | 0:44b37b90b0ef | 49 | Blue.getc(); |
MatthewJGTate | 0:44b37b90b0ef | 50 | state = start; |
MatthewJGTate | 0:44b37b90b0ef | 51 | } |
MatthewJGTate | 0:44b37b90b0ef | 52 | } |
MatthewJGTate | 0:44b37b90b0ef | 53 | |
MatthewJGTate | 0:44b37b90b0ef | 54 | void speakerPB1 (void) |
MatthewJGTate | 0:44b37b90b0ef | 55 | { |
MatthewJGTate | 0:44b37b90b0ef | 56 | while (bhit == '1') { |
MatthewJGTate | 0:44b37b90b0ef | 57 | myled = 0x01; |
MatthewJGTate | 0:44b37b90b0ef | 58 | note[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*6000.0 + 2000.0; |
MatthewJGTate | 0:44b37b90b0ef | 59 | duration[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*0.1; |
MatthewJGTate | 0:44b37b90b0ef | 60 | mySpeaker.PlaySong(note,duration); |
MatthewJGTate | 0:44b37b90b0ef | 61 | } |
MatthewJGTate | 0:44b37b90b0ef | 62 | } |
MatthewJGTate | 0:44b37b90b0ef | 63 | |
MatthewJGTate | 0:44b37b90b0ef | 64 | void motorPB2 (void) |
MatthewJGTate | 0:44b37b90b0ef | 65 | { |
MatthewJGTate | 0:44b37b90b0ef | 66 | while (bhit == '1') { |
MatthewJGTate | 0:44b37b90b0ef | 67 | myled = 0x0; |
MatthewJGTate | 0:44b37b90b0ef | 68 | haptics.play_waveform(14); |
MatthewJGTate | 0:44b37b90b0ef | 69 | } |
MatthewJGTate | 0:44b37b90b0ef | 70 | } |
MatthewJGTate | 0:44b37b90b0ef | 71 | |
MatthewJGTate | 0:44b37b90b0ef | 72 | void motorISR (void) |
MatthewJGTate | 0:44b37b90b0ef | 73 | { |
MatthewJGTate | 0:44b37b90b0ef | 74 | if (interruptControl == 1) { |
MatthewJGTate | 0:44b37b90b0ef | 75 | randomVibe = rand()%123 + 1; // Produces random number from 1 to 123 |
MatthewJGTate | 0:44b37b90b0ef | 76 | haptics.play_waveform(randomVibe); |
MatthewJGTate | 0:44b37b90b0ef | 77 | } |
MatthewJGTate | 0:44b37b90b0ef | 78 | } |
MatthewJGTate | 0:44b37b90b0ef | 79 | |
MatthewJGTate | 0:44b37b90b0ef | 80 | void speakerISR (void) |
MatthewJGTate | 0:44b37b90b0ef | 81 | { |
MatthewJGTate | 0:44b37b90b0ef | 82 | if (interruptControl == 1) { |
MatthewJGTate | 0:44b37b90b0ef | 83 | note[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*6000.0 + 2000.0; |
MatthewJGTate | 0:44b37b90b0ef | 84 | duration[0] = (static_cast <float> (rand()) / static_cast <float> (RAND_MAX))*0.5; |
MatthewJGTate | 0:44b37b90b0ef | 85 | mySpeaker.PlaySong(note,duration); |
MatthewJGTate | 0:44b37b90b0ef | 86 | } |
MatthewJGTate | 0:44b37b90b0ef | 87 | } |
MatthewJGTate | 0:44b37b90b0ef | 88 | |
MatthewJGTate | 0:44b37b90b0ef | 89 | |
MatthewJGTate | 0:44b37b90b0ef | 90 | int main() |
MatthewJGTate | 0:44b37b90b0ef | 91 | { |
MatthewJGTate | 0:44b37b90b0ef | 92 | Blue.attach(&parse_message,Serial::RxIrq); |
MatthewJGTate | 0:44b37b90b0ef | 93 | hapticMotor.attach(&motorISR, 0.5); |
MatthewJGTate | 0:44b37b90b0ef | 94 | speaker.attach(&speakerISR, 1.5); |
MatthewJGTate | 0:44b37b90b0ef | 95 | |
MatthewJGTate | 0:44b37b90b0ef | 96 | while(1) { |
MatthewJGTate | 0:44b37b90b0ef | 97 | if (button_ready == 1 && bnum == '1') { |
MatthewJGTate | 0:44b37b90b0ef | 98 | myled = 0x01; // |
MatthewJGTate | 0:44b37b90b0ef | 99 | speakerPB1(); |
MatthewJGTate | 0:44b37b90b0ef | 100 | } else if (button_ready == 1 && bnum == '2') { |
MatthewJGTate | 0:44b37b90b0ef | 101 | myled = 0x02; |
MatthewJGTate | 0:44b37b90b0ef | 102 | motorPB2(); |
MatthewJGTate | 0:44b37b90b0ef | 103 | } else if (button_ready == 1 && bnum == '3') { |
MatthewJGTate | 0:44b37b90b0ef | 104 | myled = 0x04; |
MatthewJGTate | 0:44b37b90b0ef | 105 | interruptControl = 1; |
MatthewJGTate | 0:44b37b90b0ef | 106 | } else if (button_ready == 1 && bnum == '4') { |
MatthewJGTate | 0:44b37b90b0ef | 107 | myled = 0x08; |
MatthewJGTate | 0:44b37b90b0ef | 108 | interruptControl = 0; |
MatthewJGTate | 0:44b37b90b0ef | 109 | } |
MatthewJGTate | 0:44b37b90b0ef | 110 | } |
MatthewJGTate | 0:44b37b90b0ef | 111 | } |
MatthewJGTate | 0:44b37b90b0ef | 112 | |
MatthewJGTate | 0:44b37b90b0ef | 113 | |
MatthewJGTate | 0:44b37b90b0ef | 114 |