This library is used for student's experiment. Chiba Institute of Technology
Dependencies: QEI SoftwarePWM
Fork of adrobo by
Sound.h@1:45772cb4e01c, 2020-04-20 (annotated)
- Committer:
- yasuohayashibara
- Date:
- Mon Apr 20 04:07:37 2020 +0000
- Revision:
- 1:45772cb4e01c
- Parent:
- 0:4508c5b68135
add required libraries
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
yasuohayashibara | 0:4508c5b68135 | 1 | // Chiba Institute of Technology |
yasuohayashibara | 0:4508c5b68135 | 2 | |
yasuohayashibara | 0:4508c5b68135 | 3 | #ifndef MBED_SOUND_H |
yasuohayashibara | 0:4508c5b68135 | 4 | #define MBED_SOUND_H |
yasuohayashibara | 0:4508c5b68135 | 5 | |
yasuohayashibara | 0:4508c5b68135 | 6 | #include "mbed.h" |
yasuohayashibara | 0:4508c5b68135 | 7 | #include "SoftwarePWM.h" |
yasuohayashibara | 0:4508c5b68135 | 8 | |
yasuohayashibara | 0:4508c5b68135 | 9 | /** Class to control a motor on any pin, without using pwm pin |
yasuohayashibara | 0:4508c5b68135 | 10 | * |
yasuohayashibara | 0:4508c5b68135 | 11 | * Example: |
yasuohayashibara | 0:4508c5b68135 | 12 | * @code |
yasuohayashibara | 0:4508c5b68135 | 13 | * // Motor Control |
yasuohayashibara | 0:4508c5b68135 | 14 | * #include "mbed.h" |
yasuohayashibara | 0:4508c5b68135 | 15 | * #include "Sound.h" |
yasuohayashibara | 0:4508c5b68135 | 16 | * |
yasuohayashibara | 0:4508c5b68135 | 17 | * Motor motor(xp31, xp32, xp33); |
yasuohayashibara | 0:4508c5b68135 | 18 | * Motor motor(xp34, xp35, xp36); |
yasuohayashibara | 0:4508c5b68135 | 19 | * |
yasuohayashibara | 0:4508c5b68135 | 20 | * int main(){ |
yasuohayashibara | 0:4508c5b68135 | 21 | * while(1) { |
yasuohayashibara | 0:4508c5b68135 | 22 | * for (int pos = 1000; pos < 2000; pos += 25) { |
yasuohayashibara | 0:4508c5b68135 | 23 | * Servo1.SetPosition(pos); |
yasuohayashibara | 0:4508c5b68135 | 24 | * wait_ms(20); |
yasuohayashibara | 0:4508c5b68135 | 25 | * } |
yasuohayashibara | 0:4508c5b68135 | 26 | * for (int pos = 2000; pos > 1000; pos -= 25) { |
yasuohayashibara | 0:4508c5b68135 | 27 | * Servo1.SetPosition(pos); |
yasuohayashibara | 0:4508c5b68135 | 28 | * wait_ms(20); |
yasuohayashibara | 0:4508c5b68135 | 29 | * } |
yasuohayashibara | 0:4508c5b68135 | 30 | * } |
yasuohayashibara | 0:4508c5b68135 | 31 | * @endcode |
yasuohayashibara | 0:4508c5b68135 | 32 | */ |
yasuohayashibara | 0:4508c5b68135 | 33 | |
yasuohayashibara | 0:4508c5b68135 | 34 | typedef enum { |
yasuohayashibara | 0:4508c5b68135 | 35 | M_C2 = 130, |
yasuohayashibara | 0:4508c5b68135 | 36 | M_D2 = 146, |
yasuohayashibara | 0:4508c5b68135 | 37 | M_E2 = 164, |
yasuohayashibara | 0:4508c5b68135 | 38 | M_F2 = 174, |
yasuohayashibara | 0:4508c5b68135 | 39 | M_G2 = 195, |
yasuohayashibara | 0:4508c5b68135 | 40 | M_A3 = 220, |
yasuohayashibara | 0:4508c5b68135 | 41 | M_B3 = 246, |
yasuohayashibara | 0:4508c5b68135 | 42 | M_C4 = 261, |
yasuohayashibara | 0:4508c5b68135 | 43 | M_D4 = 293, |
yasuohayashibara | 0:4508c5b68135 | 44 | M_E4 = 329, |
yasuohayashibara | 0:4508c5b68135 | 45 | M_F4 = 349, |
yasuohayashibara | 0:4508c5b68135 | 46 | M_G4 = 391, |
yasuohayashibara | 0:4508c5b68135 | 47 | M_A4 = 440, |
yasuohayashibara | 0:4508c5b68135 | 48 | M_B4 = 493, |
yasuohayashibara | 0:4508c5b68135 | 49 | M_C5 = 523, |
yasuohayashibara | 0:4508c5b68135 | 50 | M_D5 = 587, |
yasuohayashibara | 0:4508c5b68135 | 51 | M_E5 = 659, |
yasuohayashibara | 0:4508c5b68135 | 52 | M_F5 = 698, |
yasuohayashibara | 0:4508c5b68135 | 53 | M_G5 = 783, |
yasuohayashibara | 0:4508c5b68135 | 54 | M_A5 = 880, |
yasuohayashibara | 0:4508c5b68135 | 55 | M_B5 = 987, |
yasuohayashibara | 0:4508c5b68135 | 56 | M_C6 = 1046, |
yasuohayashibara | 0:4508c5b68135 | 57 | M_D6 = 1174, |
yasuohayashibara | 0:4508c5b68135 | 58 | M_E6 = 1328, |
yasuohayashibara | 0:4508c5b68135 | 59 | M_F6 = 1396, |
yasuohayashibara | 0:4508c5b68135 | 60 | M_G6 = 1567, |
yasuohayashibara | 0:4508c5b68135 | 61 | M_A6 = 1760, |
yasuohayashibara | 0:4508c5b68135 | 62 | M_B6 = 1975, |
yasuohayashibara | 0:4508c5b68135 | 63 | } Freq; |
yasuohayashibara | 0:4508c5b68135 | 64 | |
yasuohayashibara | 0:4508c5b68135 | 65 | class Sound |
yasuohayashibara | 0:4508c5b68135 | 66 | { |
yasuohayashibara | 0:4508c5b68135 | 67 | public: |
yasuohayashibara | 0:4508c5b68135 | 68 | /** Create a new SoftwarePWM object on any mbed pin |
yasuohayashibara | 0:4508c5b68135 | 69 | * |
yasuohayashibara | 0:4508c5b68135 | 70 | * @param Pin Pin on mbed to connect PWM device to |
yasuohayashibara | 0:4508c5b68135 | 71 | */ |
yasuohayashibara | 0:4508c5b68135 | 72 | Sound(PinName Ppwm); |
yasuohayashibara | 0:4508c5b68135 | 73 | |
yasuohayashibara | 0:4508c5b68135 | 74 | void write(float value); |
yasuohayashibara | 0:4508c5b68135 | 75 | |
yasuohayashibara | 0:4508c5b68135 | 76 | float read(); |
yasuohayashibara | 0:4508c5b68135 | 77 | |
yasuohayashibara | 0:4508c5b68135 | 78 | //#ifdef MBED_OPERATORS |
yasuohayashibara | 0:4508c5b68135 | 79 | /** A operator shorthand for write() |
yasuohayashibara | 0:4508c5b68135 | 80 | */ |
yasuohayashibara | 0:4508c5b68135 | 81 | Sound& operator= (float value) { |
yasuohayashibara | 0:4508c5b68135 | 82 | write(value); |
yasuohayashibara | 0:4508c5b68135 | 83 | return *this; |
yasuohayashibara | 0:4508c5b68135 | 84 | } |
yasuohayashibara | 0:4508c5b68135 | 85 | |
yasuohayashibara | 0:4508c5b68135 | 86 | Sound& operator= (Sound& rhs) { |
yasuohayashibara | 0:4508c5b68135 | 87 | write(rhs.read()); |
yasuohayashibara | 0:4508c5b68135 | 88 | return *this; |
yasuohayashibara | 0:4508c5b68135 | 89 | } |
yasuohayashibara | 0:4508c5b68135 | 90 | |
yasuohayashibara | 0:4508c5b68135 | 91 | /** An operator shorthand for read() |
yasuohayashibara | 0:4508c5b68135 | 92 | */ |
yasuohayashibara | 0:4508c5b68135 | 93 | operator float() { |
yasuohayashibara | 0:4508c5b68135 | 94 | return read(); |
yasuohayashibara | 0:4508c5b68135 | 95 | } |
yasuohayashibara | 0:4508c5b68135 | 96 | //#endif |
yasuohayashibara | 0:4508c5b68135 | 97 | |
yasuohayashibara | 0:4508c5b68135 | 98 | private: |
yasuohayashibara | 0:4508c5b68135 | 99 | SoftwarePWM pwm; |
yasuohayashibara | 0:4508c5b68135 | 100 | float frequency_; |
yasuohayashibara | 0:4508c5b68135 | 101 | float period_; |
yasuohayashibara | 0:4508c5b68135 | 102 | }; |
yasuohayashibara | 0:4508c5b68135 | 103 | |
yasuohayashibara | 0:4508c5b68135 | 104 | #endif |