Sakis Kasampalis
/
cricket
Cricket (insect) emulation
Fork of talking by
cricket.cpp@3:1b91d910dabd, 2017-02-11 (annotated)
- Committer:
- faif
- Date:
- Sat Feb 11 15:49:06 2017 +0000
- Revision:
- 3:1b91d910dabd
- Parent:
- 2:c6c74e788985
Reorganize code
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 | int main () |
faif | 0:17458f91a0e9 | 16 | { |
faif | 2:c6c74e788985 | 17 | const int size = 1165; // 32K max in total (~30K is enough for testing) |
faif | 0:17458f91a0e9 | 18 | char clip[size]; |
faif | 0:17458f91a0e9 | 19 | |
faif | 0:17458f91a0e9 | 20 | waitForButton(led, button); |
faif | 0:17458f91a0e9 | 21 | preloadFile(clip, size, clip_path); |
faif | 0:17458f91a0e9 | 22 | |
faif | 0:17458f91a0e9 | 23 | rhythm.attach(&myrhythm, ticker_rhythm); |
faif | 0:17458f91a0e9 | 24 | |
faif | 0:17458f91a0e9 | 25 | while (true) |
faif | 0:17458f91a0e9 | 26 | { |
faif | 0:17458f91a0e9 | 27 | playSound(clip, size, headphones); |
faif | 2:c6c74e788985 | 28 | randomPause(); |
faif | 0:17458f91a0e9 | 29 | } |
faif | 0:17458f91a0e9 | 30 | } |
faif | 0:17458f91a0e9 | 31 | |
faif | 3:1b91d910dabd | 32 | bool done = false; |
faif | 3:1b91d910dabd | 33 | |
faif | 3:1b91d910dabd | 34 | void myrhythm() |
faif | 3:1b91d910dabd | 35 | { |
faif | 3:1b91d910dabd | 36 | done = true; |
faif | 3:1b91d910dabd | 37 | } |
faif | 3:1b91d910dabd | 38 | |
faif | 0:17458f91a0e9 | 39 | void waitForButton(DigitalOut& led, DigitalIn& button) |
faif | 0:17458f91a0e9 | 40 | { |
faif | 0:17458f91a0e9 | 41 | led = ON; |
faif | 0:17458f91a0e9 | 42 | while (!button) {}; |
faif | 0:17458f91a0e9 | 43 | led = OFF; |
faif | 0:17458f91a0e9 | 44 | } |
faif | 0:17458f91a0e9 | 45 | |
faif | 0:17458f91a0e9 | 46 | void preloadFile(char* clip, int size, const char* path) |
faif | 0:17458f91a0e9 | 47 | { |
faif | 0:17458f91a0e9 | 48 | FILE* file = fopen(path, "r"); |
faif | 0:17458f91a0e9 | 49 | for (int i = 0; i != size; ++i) |
faif | 2:c6c74e788985 | 50 | { |
faif | 0:17458f91a0e9 | 51 | clip[i] = fgetc(file); |
faif | 2:c6c74e788985 | 52 | } |
faif | 0:17458f91a0e9 | 53 | fclose(file); |
faif | 0:17458f91a0e9 | 54 | } |
faif | 0:17458f91a0e9 | 55 | |
faif | 0:17458f91a0e9 | 56 | void playSound(char* clip, int size, AnalogOut& headphones) |
faif | 0:17458f91a0e9 | 57 | { |
faif | 0:17458f91a0e9 | 58 | for (int i= 0; i != size; ++i) |
faif | 0:17458f91a0e9 | 59 | { |
faif | 0:17458f91a0e9 | 60 | while (!done) {}; |
faif | 0:17458f91a0e9 | 61 | done = false; |
faif | 0:17458f91a0e9 | 62 | headphones = float(clip[i]) / MaxWavValue; |
faif | 0:17458f91a0e9 | 63 | } |
faif | 0:17458f91a0e9 | 64 | } |
faif | 2:c6c74e788985 | 65 | |
faif | 2:c6c74e788985 | 66 | void randomPause() |
faif | 2:c6c74e788985 | 67 | { |
faif | 2:c6c74e788985 | 68 | float c = rand() % 100; |
faif | 3:1b91d910dabd | 69 | // 30% chance to wait 2 sec |
faif | 2:c6c74e788985 | 70 | if (c > 70) |
faif | 2:c6c74e788985 | 71 | { |
faif | 2:c6c74e788985 | 72 | wait(2); |
faif | 2:c6c74e788985 | 73 | } |
faif | 3:1b91d910dabd | 74 | // 70% chance to wait 0-1 sec |
faif | 2:c6c74e788985 | 75 | else |
faif | 2:c6c74e788985 | 76 | { |
faif | 2:c6c74e788985 | 77 | wait(c / 100); |
faif | 2:c6c74e788985 | 78 | } |
faif | 2:c6c74e788985 | 79 | } |