![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
Cricket (insect) emulation
Fork of talking by
cricket.cpp@2:c6c74e788985, 2017-02-11 (annotated)
- Committer:
- faif
- Date:
- Sat Feb 11 15:45:54 2017 +0000
- Revision:
- 2:c6c74e788985
- Parent:
- talking.cpp@1:91104d67b0af
- Child:
- 3:1b91d910dabd
Cricket (animal) emulation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
faif | 0:17458f91a0e9 | 1 | #include "mbed.h" |
faif | 2:c6c74e788985 | 2 | #include "cricket.h" |
faif | 0:17458f91a0e9 | 3 | |
faif | 0:17458f91a0e9 | 4 | enum |
faif | 0:17458f91a0e9 | 5 | { |
faif | 0:17458f91a0e9 | 6 | OFF = 0, |
faif | 0:17458f91a0e9 | 7 | ON = 1 |
faif | 0:17458f91a0e9 | 8 | }; |
faif | 0:17458f91a0e9 | 9 | |
faif | 0:17458f91a0e9 | 10 | enum { MaxWavValue = 255 }; |
faif | 0:17458f91a0e9 | 11 | |
faif | 2:c6c74e788985 | 12 | static const float ticker_rhythm = 10E-5; // 9.766 kHz sample rate (1/9766 sec delay) |
faif | 2:c6c74e788985 | 13 | static const char clip_path[] = "/local/cricket.wav"; |
faif | 0:17458f91a0e9 | 14 | |
faif | 0:17458f91a0e9 | 15 | |
faif | 0:17458f91a0e9 | 16 | bool done = false; |
faif | 0:17458f91a0e9 | 17 | |
faif | 0:17458f91a0e9 | 18 | |
faif | 0:17458f91a0e9 | 19 | void myrhythm() |
faif | 0:17458f91a0e9 | 20 | { |
faif | 0:17458f91a0e9 | 21 | done = true; |
faif | 0:17458f91a0e9 | 22 | } |
faif | 0:17458f91a0e9 | 23 | |
faif | 0:17458f91a0e9 | 24 | int main () |
faif | 0:17458f91a0e9 | 25 | { |
faif | 2:c6c74e788985 | 26 | const int size = 1165; // 32K max in total (~30K is enough for testing) |
faif | 0:17458f91a0e9 | 27 | char clip[size]; |
faif | 0:17458f91a0e9 | 28 | |
faif | 0:17458f91a0e9 | 29 | waitForButton(led, button); |
faif | 0:17458f91a0e9 | 30 | preloadFile(clip, size, clip_path); |
faif | 0:17458f91a0e9 | 31 | |
faif | 0:17458f91a0e9 | 32 | rhythm.attach(&myrhythm, ticker_rhythm); |
faif | 0:17458f91a0e9 | 33 | |
faif | 0:17458f91a0e9 | 34 | while (true) |
faif | 0:17458f91a0e9 | 35 | { |
faif | 0:17458f91a0e9 | 36 | playSound(clip, size, headphones); |
faif | 2:c6c74e788985 | 37 | randomPause(); |
faif | 0:17458f91a0e9 | 38 | } |
faif | 0:17458f91a0e9 | 39 | } |
faif | 0:17458f91a0e9 | 40 | |
faif | 0:17458f91a0e9 | 41 | void waitForButton(DigitalOut& led, DigitalIn& button) |
faif | 0:17458f91a0e9 | 42 | { |
faif | 0:17458f91a0e9 | 43 | led = ON; |
faif | 0:17458f91a0e9 | 44 | while (!button) {}; |
faif | 0:17458f91a0e9 | 45 | led = OFF; |
faif | 0:17458f91a0e9 | 46 | } |
faif | 0:17458f91a0e9 | 47 | |
faif | 0:17458f91a0e9 | 48 | void preloadFile(char* clip, int size, const char* path) |
faif | 0:17458f91a0e9 | 49 | { |
faif | 0:17458f91a0e9 | 50 | FILE* file = fopen(path, "r"); |
faif | 0:17458f91a0e9 | 51 | for (int i = 0; i != size; ++i) |
faif | 2:c6c74e788985 | 52 | { |
faif | 0:17458f91a0e9 | 53 | clip[i] = fgetc(file); |
faif | 2:c6c74e788985 | 54 | } |
faif | 0:17458f91a0e9 | 55 | fclose(file); |
faif | 0:17458f91a0e9 | 56 | } |
faif | 0:17458f91a0e9 | 57 | |
faif | 0:17458f91a0e9 | 58 | void playSound(char* clip, int size, AnalogOut& headphones) |
faif | 0:17458f91a0e9 | 59 | { |
faif | 0:17458f91a0e9 | 60 | for (int i= 0; i != size; ++i) |
faif | 0:17458f91a0e9 | 61 | { |
faif | 0:17458f91a0e9 | 62 | while (!done) {}; |
faif | 0:17458f91a0e9 | 63 | done = false; |
faif | 0:17458f91a0e9 | 64 | headphones = float(clip[i]) / MaxWavValue; |
faif | 0:17458f91a0e9 | 65 | } |
faif | 0:17458f91a0e9 | 66 | } |
faif | 2:c6c74e788985 | 67 | |
faif | 2:c6c74e788985 | 68 | void randomPause() |
faif | 2:c6c74e788985 | 69 | { |
faif | 2:c6c74e788985 | 70 | float c = rand() % 100; |
faif | 2:c6c74e788985 | 71 | // 30% percentage to wait 2 sec |
faif | 2:c6c74e788985 | 72 | if (c > 70) |
faif | 2:c6c74e788985 | 73 | { |
faif | 2:c6c74e788985 | 74 | wait(2); |
faif | 2:c6c74e788985 | 75 | } |
faif | 2:c6c74e788985 | 76 | // 70% percentage to wait 0-1 sec |
faif | 2:c6c74e788985 | 77 | else |
faif | 2:c6c74e788985 | 78 | { |
faif | 2:c6c74e788985 | 79 | wait(c / 100); |
faif | 2:c6c74e788985 | 80 | } |
faif | 2:c6c74e788985 | 81 | } |