An floppy drive audio generator using dsp on live audio
Dependencies: Terminal asyncADC mbed-dsp mbed
moppy.cpp@0:84c336a81482, 2017-05-24 (annotated)
- Committer:
- Condo2k4
- Date:
- Wed May 24 11:58:43 2017 +0000
- Revision:
- 0:84c336a81482
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Condo2k4 | 0:84c336a81482 | 1 | #include "mbed.h" |
Condo2k4 | 0:84c336a81482 | 2 | #include "moppy.h" |
Condo2k4 | 0:84c336a81482 | 3 | |
Condo2k4 | 0:84c336a81482 | 4 | #define SILENCE_ALL 250 |
Condo2k4 | 0:84c336a81482 | 5 | #define RESET_ALL 251 |
Condo2k4 | 0:84c336a81482 | 6 | #define RESET_COMPLETE 252 |
Condo2k4 | 0:84c336a81482 | 7 | #define ERROR_STATE 253 |
Condo2k4 | 0:84c336a81482 | 8 | #define RESERVED 254 |
Condo2k4 | 0:84c336a81482 | 9 | #define SYNC 255 |
Condo2k4 | 0:84c336a81482 | 10 | |
Condo2k4 | 0:84c336a81482 | 11 | void Moppy::consumeAndCheck(int event) { |
Condo2k4 | 0:84c336a81482 | 12 | while(expectedSyncs-->0) { |
Condo2k4 | 0:84c336a81482 | 13 | if(str_buffer[expectedSyncs]!=0) |
Condo2k4 | 0:84c336a81482 | 14 | error("Moppy Flush Error"); |
Condo2k4 | 0:84c336a81482 | 15 | } |
Condo2k4 | 0:84c336a81482 | 16 | } |
Condo2k4 | 0:84c336a81482 | 17 | |
Condo2k4 | 0:84c336a81482 | 18 | Moppy::Moppy(PinName tx, PinName rx, int baud) : serial(tx,rx,baud) { |
Condo2k4 | 0:84c336a81482 | 19 | |
Condo2k4 | 0:84c336a81482 | 20 | wait_ms(50); |
Condo2k4 | 0:84c336a81482 | 21 | |
Condo2k4 | 0:84c336a81482 | 22 | //Synchronise |
Condo2k4 | 0:84c336a81482 | 23 | serial.putc(SYNC); |
Condo2k4 | 0:84c336a81482 | 24 | if(serial.getc()!=SYNC) |
Condo2k4 | 0:84c336a81482 | 25 | error("Moppy Initialisation Error"); |
Condo2k4 | 0:84c336a81482 | 26 | |
Condo2k4 | 0:84c336a81482 | 27 | //Reset all |
Condo2k4 | 0:84c336a81482 | 28 | serial.putc(RESET_ALL); |
Condo2k4 | 0:84c336a81482 | 29 | if(serial.getc()!=RESET_ALL) |
Condo2k4 | 0:84c336a81482 | 30 | error("Failed to reset all drives"); |
Condo2k4 | 0:84c336a81482 | 31 | serial.putc(RESET_COMPLETE); |
Condo2k4 | 0:84c336a81482 | 32 | if(serial.getc()!=RESET_COMPLETE) |
Condo2k4 | 0:84c336a81482 | 33 | error("Failed to reset all drives"); |
Condo2k4 | 0:84c336a81482 | 34 | |
Condo2k4 | 0:84c336a81482 | 35 | //Count |
Condo2k4 | 0:84c336a81482 | 36 | serial.putc(249); |
Condo2k4 | 0:84c336a81482 | 37 | driveCount = (249-serial.getc())*2; |
Condo2k4 | 0:84c336a81482 | 38 | serial.putc(SYNC); |
Condo2k4 | 0:84c336a81482 | 39 | if(serial.getc()!=SYNC) |
Condo2k4 | 0:84c336a81482 | 40 | error("Moppy Initialisation Error"); |
Condo2k4 | 0:84c336a81482 | 41 | |
Condo2k4 | 0:84c336a81482 | 42 | str_buffer = (char*)calloc(driveCount/2, sizeof(uint8_t)*6); |
Condo2k4 | 0:84c336a81482 | 43 | |
Condo2k4 | 0:84c336a81482 | 44 | freqs = (uint16_t*)calloc(driveCount, sizeof(uint16_t)); |
Condo2k4 | 0:84c336a81482 | 45 | |
Condo2k4 | 0:84c336a81482 | 46 | pair_changed = (bool*)calloc(driveCount/2, sizeof(bool)); |
Condo2k4 | 0:84c336a81482 | 47 | |
Condo2k4 | 0:84c336a81482 | 48 | expectedSyncs = 0; |
Condo2k4 | 0:84c336a81482 | 49 | read_callback.attach(this, &Moppy::consumeAndCheck); |
Condo2k4 | 0:84c336a81482 | 50 | |
Condo2k4 | 0:84c336a81482 | 51 | } |
Condo2k4 | 0:84c336a81482 | 52 | |
Condo2k4 | 0:84c336a81482 | 53 | void Moppy::setFrequency(int drive, int frequency) { |
Condo2k4 | 0:84c336a81482 | 54 | |
Condo2k4 | 0:84c336a81482 | 55 | freqs[drive] = frequency; |
Condo2k4 | 0:84c336a81482 | 56 | pair_changed[drive>>1] = true; |
Condo2k4 | 0:84c336a81482 | 57 | |
Condo2k4 | 0:84c336a81482 | 58 | } |
Condo2k4 | 0:84c336a81482 | 59 | |
Condo2k4 | 0:84c336a81482 | 60 | void Moppy::flush() { |
Condo2k4 | 0:84c336a81482 | 61 | |
Condo2k4 | 0:84c336a81482 | 62 | //build message |
Condo2k4 | 0:84c336a81482 | 63 | int i=driveCount>>1; |
Condo2k4 | 0:84c336a81482 | 64 | int j=0; |
Condo2k4 | 0:84c336a81482 | 65 | while(i-->0) { |
Condo2k4 | 0:84c336a81482 | 66 | if(pair_changed[i]) { |
Condo2k4 | 0:84c336a81482 | 67 | int freq1 = freqs[i<<1]; |
Condo2k4 | 0:84c336a81482 | 68 | int freq2 = freqs[(i<<1)+1]; |
Condo2k4 | 0:84c336a81482 | 69 | |
Condo2k4 | 0:84c336a81482 | 70 | str_buffer[j++] = i; |
Condo2k4 | 0:84c336a81482 | 71 | str_buffer[j++] = freq1/250; |
Condo2k4 | 0:84c336a81482 | 72 | str_buffer[j++] = freq1%250; |
Condo2k4 | 0:84c336a81482 | 73 | str_buffer[j++] = freq2/250; |
Condo2k4 | 0:84c336a81482 | 74 | str_buffer[j++] = freq2%250; |
Condo2k4 | 0:84c336a81482 | 75 | str_buffer[j++] = SYNC; |
Condo2k4 | 0:84c336a81482 | 76 | |
Condo2k4 | 0:84c336a81482 | 77 | pair_changed[i] = false; //reset changed flag |
Condo2k4 | 0:84c336a81482 | 78 | } |
Condo2k4 | 0:84c336a81482 | 79 | } |
Condo2k4 | 0:84c336a81482 | 80 | |
Condo2k4 | 0:84c336a81482 | 81 | //send message asynchronously |
Condo2k4 | 0:84c336a81482 | 82 | if(j>0) { |
Condo2k4 | 0:84c336a81482 | 83 | |
Condo2k4 | 0:84c336a81482 | 84 | for(int i=0; i<j; i++) { |
Condo2k4 | 0:84c336a81482 | 85 | serial.putc(str_buffer[i]); |
Condo2k4 | 0:84c336a81482 | 86 | if(str_buffer[i]==SYNC) |
Condo2k4 | 0:84c336a81482 | 87 | serial.getc(); |
Condo2k4 | 0:84c336a81482 | 88 | } |
Condo2k4 | 0:84c336a81482 | 89 | |
Condo2k4 | 0:84c336a81482 | 90 | // serial.write((uint8_t*)str_buffer, j, NULL); |
Condo2k4 | 0:84c336a81482 | 91 | // expectedSyncs = j/6; |
Condo2k4 | 0:84c336a81482 | 92 | // serial.read((uint8_t*)str_buffer, expectedSyncs, read_callback); |
Condo2k4 | 0:84c336a81482 | 93 | } |
Condo2k4 | 0:84c336a81482 | 94 | |
Condo2k4 | 0:84c336a81482 | 95 | } |
Condo2k4 | 0:84c336a81482 | 96 | |
Condo2k4 | 0:84c336a81482 | 97 | bool Moppy::silence() { |
Condo2k4 | 0:84c336a81482 | 98 | serial.putc(250); |
Condo2k4 | 0:84c336a81482 | 99 | return serial.getc()==250; |
Condo2k4 | 0:84c336a81482 | 100 | } |