Mbed Clock application using an NTP connection to get internet time and a terminal interface to send commands

Dependencies:   4DGL-uLCD-SE EthernetInterface NTPClient mbed-rtos mbed SDFileSystem wavfile

Revision:
1:c47a2f0816bb
Child:
2:c939d0501184
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/Recorder.cpp	Mon Dec 08 10:06:35 2014 +0000
@@ -0,0 +1,91 @@
+#include "Recorder.h"
+
+#define RAM_TOTAL 1024
+
+AnalogIn adcin(p20);
+DigitalOut led_rec_ok(LED2);
+
+Ticker ticker;
+float buffer[RAM_TOTAL];
+int rp = 0;
+int wp = 0;
+int dropout = 0;
+
+#define WAVFILE_ERROR_PRINT(RESULT) \
+    do { \
+        WavFileResult R = RESULT; \
+        if (R != WavFileResultOK) { \
+            char wavfile_error_print_text[BUFSIZ]; \
+            wavfile_result_string(R, wavfile_error_print_text, sizeof(wavfile_error_print_text)); \
+            printf("%s (code=%d)\r\n", wavfile_error_print_text, R); \
+            return 1; \
+        } \
+    } while(0)
+
+void tickrec(void)
+{
+    int np = (wp + 1) & (RAM_TOTAL - 1);
+    if (np != rp) {
+        buffer[wp] = adcin;
+        wp = np;
+    } else {
+        dropout++;
+    }
+    led_rec_ok = !led_rec_ok;
+}
+
+int rec(const char *filename, const int nsec)
+{
+    WavFileResult result;
+    wavfile_info_t info;
+    wavfile_data_t data;
+
+    WAVFILE_INFO_AUDIO_FORMAT(&info)    = 1;
+    WAVFILE_INFO_NUM_CHANNELS(&info)    = 1;
+    WAVFILE_INFO_SAMPLE_RATE(&info)     = 11025;
+    WAVFILE_INFO_BYTE_RATE(&info)       = 22050;
+    WAVFILE_INFO_BLOCK_ALIGN(&info)     = 2;
+    WAVFILE_INFO_BITS_PER_SAMPLE(&info) = 16;
+
+    WAVFILE *wf = wavfile_open(filename, WavFileModeWrite, &result);
+    WAVFILE_ERROR_PRINT(result);
+    WAVFILE_ERROR_PRINT(wavfile_write_info(wf, &info));
+
+    //printf("[REC:%s]\r\n", filename);
+    //printf("\tWAVFILE_INFO_AUDIO_FORMAT(&info)    = %d\r\n", WAVFILE_INFO_AUDIO_FORMAT(&info));
+    //printf("\tWAVFILE_INFO_NUM_CHANNELS(&info)    = %d\r\n", WAVFILE_INFO_NUM_CHANNELS(&info));
+    //printf("\tWAVFILE_INFO_SAMPLE_RATE(&info)     = %d\r\n", WAVFILE_INFO_SAMPLE_RATE(&info));
+    //printf("\tWAVFILE_INFO_BYTE_RATE(&info)       = %d\r\n", WAVFILE_INFO_BYTE_RATE(&info));
+    //printf("\tWAVFILE_INFO_BLOCK_ALIGN(&info)     = %d\r\n", WAVFILE_INFO_BLOCK_ALIGN(&info));
+    //printf("\tWAVFILE_INFO_BITS_PER_SAMPLE(&info) = %d\r\n", WAVFILE_INFO_BITS_PER_SAMPLE(&info));
+
+    const int interval_us =  1000000 / WAVFILE_INFO_SAMPLE_RATE(&info);
+    const unsigned int samples_for_nsec = WAVFILE_INFO_SAMPLE_RATE(&info) * nsec;
+
+    rp = 0;
+    wp = 0;
+    dropout = 0;
+    unsigned int count = 0;
+    ticker.attach_us(tickrec, interval_us);
+    WAVFILE_DATA_NUM_CHANNELS(&data) = 1;
+    while (1) {
+        while (rp == wp) {
+            wait_us(1);
+        }
+
+        WAVFILE_DATA_CHANNEL_DATA(&data, 0) = buffer[rp];
+        rp = (rp + 1) & (RAM_TOTAL - 1);
+        WAVFILE_ERROR_PRINT(wavfile_write_data(wf, &data));
+
+        count++;
+        if (count > samples_for_nsec) {
+            break;
+        }
+    }
+    ticker.detach();
+    led_rec_ok = 0;
+    //printf("\t-- Rec done. (dropout=%d) --\r\n", dropout);
+
+    WAVFILE_ERROR_PRINT(wavfile_close(wf));
+    return 0;
+}