![](/media/cache/img/default_profile.jpg.50x50_q85.jpg)
The Big Mouth Billy Bass program
main.cpp@0:ef6fc1737022, 2011-01-17 (annotated)
- Committer:
- sravet
- Date:
- Mon Jan 17 16:53:37 2011 +0000
- Revision:
- 0:ef6fc1737022
Initial checkin
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
sravet | 0:ef6fc1737022 | 1 | |
sravet | 0:ef6fc1737022 | 2 | #include "mbed.h" |
sravet | 0:ef6fc1737022 | 3 | #include "SDFileSystem.h" |
sravet | 0:ef6fc1737022 | 4 | |
sravet | 0:ef6fc1737022 | 5 | #define SAMPLE_FREQ 40000 |
sravet | 0:ef6fc1737022 | 6 | #define BUF_SIZE (SAMPLE_FREQ/10) |
sravet | 0:ef6fc1737022 | 7 | #define SLICE_BUF_SIZE 1 |
sravet | 0:ef6fc1737022 | 8 | |
sravet | 0:ef6fc1737022 | 9 | // include this #define to enable lots of serial output |
sravet | 0:ef6fc1737022 | 10 | //#define VERBOSE |
sravet | 0:ef6fc1737022 | 11 | |
sravet | 0:ef6fc1737022 | 12 | |
sravet | 0:ef6fc1737022 | 13 | |
sravet | 0:ef6fc1737022 | 14 | typedef struct uFMT_STRUCT { |
sravet | 0:ef6fc1737022 | 15 | short comp_code; |
sravet | 0:ef6fc1737022 | 16 | short num_channels; |
sravet | 0:ef6fc1737022 | 17 | unsigned sample_rate; |
sravet | 0:ef6fc1737022 | 18 | unsigned avg_Bps; |
sravet | 0:ef6fc1737022 | 19 | short block_align; |
sravet | 0:ef6fc1737022 | 20 | short sig_bps; |
sravet | 0:ef6fc1737022 | 21 | } FMT_STRUCT; |
sravet | 0:ef6fc1737022 | 22 | |
sravet | 0:ef6fc1737022 | 23 | |
sravet | 0:ef6fc1737022 | 24 | typedef struct uMOV_STRUCT { |
sravet | 0:ef6fc1737022 | 25 | long sample; |
sravet | 0:ef6fc1737022 | 26 | unsigned motor; |
sravet | 0:ef6fc1737022 | 27 | unsigned duty_cycle; |
sravet | 0:ef6fc1737022 | 28 | unsigned played; |
sravet | 0:ef6fc1737022 | 29 | } MOV_STRUCT; |
sravet | 0:ef6fc1737022 | 30 | |
sravet | 0:ef6fc1737022 | 31 | // global MBED things |
sravet | 0:ef6fc1737022 | 32 | AnalogOut DACout(p18); |
sravet | 0:ef6fc1737022 | 33 | DigitalOut led1(LED1); |
sravet | 0:ef6fc1737022 | 34 | DigitalOut led2(LED2); |
sravet | 0:ef6fc1737022 | 35 | DigitalOut led3(LED3); |
sravet | 0:ef6fc1737022 | 36 | DigitalOut led4(LED4); |
sravet | 0:ef6fc1737022 | 37 | DigitalOut digout(p8); |
sravet | 0:ef6fc1737022 | 38 | DigitalIn pushbutton(p24); |
sravet | 0:ef6fc1737022 | 39 | PwmOut body(p21); |
sravet | 0:ef6fc1737022 | 40 | PwmOut mouth(p22); |
sravet | 0:ef6fc1737022 | 41 | PwmOut tail(p23); |
sravet | 0:ef6fc1737022 | 42 | Ticker tick; |
sravet | 0:ef6fc1737022 | 43 | SDFileSystem sd(p5, p6, p7, p13, "sd"); |
sravet | 0:ef6fc1737022 | 44 | |
sravet | 0:ef6fc1737022 | 45 | // global variables used both by the main program and the ISR |
sravet | 0:ef6fc1737022 | 46 | short DAC_fifo[256]; // FIFO for the DAC |
sravet | 0:ef6fc1737022 | 47 | short DAC_wptr; // FIFO pointer |
sravet | 0:ef6fc1737022 | 48 | volatile short DAC_rptr; // FIFO pointer |
sravet | 0:ef6fc1737022 | 49 | long slice; |
sravet | 0:ef6fc1737022 | 50 | unsigned num_movements; |
sravet | 0:ef6fc1737022 | 51 | unsigned current_movement; |
sravet | 0:ef6fc1737022 | 52 | MOV_STRUCT movements[500]; |
sravet | 0:ef6fc1737022 | 53 | |
sravet | 0:ef6fc1737022 | 54 | void dac_out(void); |
sravet | 0:ef6fc1737022 | 55 | void play_wave(char *,char *,unsigned); |
sravet | 0:ef6fc1737022 | 56 | void cleanup(char *); |
sravet | 0:ef6fc1737022 | 57 | unsigned process_movement_file (char *mfname, MOV_STRUCT *mv,unsigned samp_rate); |
sravet | 0:ef6fc1737022 | 58 | |
sravet | 0:ef6fc1737022 | 59 | |
sravet | 0:ef6fc1737022 | 60 | int main() { |
sravet | 0:ef6fc1737022 | 61 | FILE *cmdfile; |
sravet | 0:ef6fc1737022 | 62 | char cmdline[100]; |
sravet | 0:ef6fc1737022 | 63 | char *movfile,*tmp; |
sravet | 0:ef6fc1737022 | 64 | char cmdfile_name[]="/sd/fish.txt"; |
sravet | 0:ef6fc1737022 | 65 | unsigned slow_mode=0; |
sravet | 0:ef6fc1737022 | 66 | if (pushbutton) slow_mode=1; |
sravet | 0:ef6fc1737022 | 67 | led1=0; wait(.5); led1=1; wait(.5); led1=0; |
sravet | 0:ef6fc1737022 | 68 | printf("\nHello, world!\n"); |
sravet | 0:ef6fc1737022 | 69 | if (slow_mode) printf("Slooooow mode enabled...\n"); |
sravet | 0:ef6fc1737022 | 70 | printf("Waiting for button push\n"); |
sravet | 0:ef6fc1737022 | 71 | while (!pushbutton); |
sravet | 0:ef6fc1737022 | 72 | printf("Button pushed\n"); |
sravet | 0:ef6fc1737022 | 73 | while (1) { |
sravet | 0:ef6fc1737022 | 74 | cmdfile=fopen(cmdfile_name,"rb"); |
sravet | 0:ef6fc1737022 | 75 | if (!cmdfile) { |
sravet | 0:ef6fc1737022 | 76 | printf("Unable to open command file '%s'\n",cmdfile_name); |
sravet | 0:ef6fc1737022 | 77 | exit(1); |
sravet | 0:ef6fc1737022 | 78 | } |
sravet | 0:ef6fc1737022 | 79 | |
sravet | 0:ef6fc1737022 | 80 | fgets(cmdline,100,cmdfile); |
sravet | 0:ef6fc1737022 | 81 | while (!feof(cmdfile)) { |
sravet | 0:ef6fc1737022 | 82 | printf("Parsing '%s' from command file '%s'\n",cmdline,cmdfile_name); |
sravet | 0:ef6fc1737022 | 83 | tmp=strchr(cmdline,'#'); |
sravet | 0:ef6fc1737022 | 84 | if (tmp) *tmp=0; |
sravet | 0:ef6fc1737022 | 85 | movfile=strchr(cmdline,' '); |
sravet | 0:ef6fc1737022 | 86 | if (movfile) { |
sravet | 0:ef6fc1737022 | 87 | *movfile=0; |
sravet | 0:ef6fc1737022 | 88 | movfile++; |
sravet | 0:ef6fc1737022 | 89 | tmp=strchr(movfile,'\n'); |
sravet | 0:ef6fc1737022 | 90 | cleanup(movfile); |
sravet | 0:ef6fc1737022 | 91 | if (tmp) *tmp=0; |
sravet | 0:ef6fc1737022 | 92 | |
sravet | 0:ef6fc1737022 | 93 | #ifdef USE_PUSHBUTTON |
sravet | 0:ef6fc1737022 | 94 | printf("Waiting for button push\n"); |
sravet | 0:ef6fc1737022 | 95 | while (!pushbutton); |
sravet | 0:ef6fc1737022 | 96 | printf("Button pushed\n"); |
sravet | 0:ef6fc1737022 | 97 | #else |
sravet | 0:ef6fc1737022 | 98 | wait_ms(2000); |
sravet | 0:ef6fc1737022 | 99 | #endif |
sravet | 0:ef6fc1737022 | 100 | play_wave(cmdline,movfile,slow_mode); |
sravet | 0:ef6fc1737022 | 101 | printf("Back from play_wave()\n"); |
sravet | 0:ef6fc1737022 | 102 | } else { |
sravet | 0:ef6fc1737022 | 103 | printf("Unable to parse '%s' from command file '%s'\n",cmdline,cmdfile_name); |
sravet | 0:ef6fc1737022 | 104 | } |
sravet | 0:ef6fc1737022 | 105 | fgets(cmdline,100,cmdfile); |
sravet | 0:ef6fc1737022 | 106 | } |
sravet | 0:ef6fc1737022 | 107 | fclose(cmdfile); |
sravet | 0:ef6fc1737022 | 108 | printf("Goodbye, world!\n"); |
sravet | 0:ef6fc1737022 | 109 | } |
sravet | 0:ef6fc1737022 | 110 | } |
sravet | 0:ef6fc1737022 | 111 | |
sravet | 0:ef6fc1737022 | 112 | void play_wave(char *wavname,char *movname,unsigned slow) |
sravet | 0:ef6fc1737022 | 113 | { |
sravet | 0:ef6fc1737022 | 114 | unsigned chunk_id,chunk_size,channel; |
sravet | 0:ef6fc1737022 | 115 | unsigned data,samp_int,i; |
sravet | 0:ef6fc1737022 | 116 | short dac_data; |
sravet | 0:ef6fc1737022 | 117 | char *slice_buf; |
sravet | 0:ef6fc1737022 | 118 | short *data_sptr; |
sravet | 0:ef6fc1737022 | 119 | unsigned char *data_bptr; |
sravet | 0:ef6fc1737022 | 120 | int *data_wptr; |
sravet | 0:ef6fc1737022 | 121 | FMT_STRUCT wav_format; |
sravet | 0:ef6fc1737022 | 122 | FILE *wavfile; |
sravet | 0:ef6fc1737022 | 123 | long num_slices; |
sravet | 0:ef6fc1737022 | 124 | long long slice_value; |
sravet | 0:ef6fc1737022 | 125 | int verbosity=0; |
sravet | 0:ef6fc1737022 | 126 | DAC_wptr=0; |
sravet | 0:ef6fc1737022 | 127 | DAC_rptr=0; |
sravet | 0:ef6fc1737022 | 128 | for (i=0;i<256;i+=2) { |
sravet | 0:ef6fc1737022 | 129 | DAC_fifo[i]=0; |
sravet | 0:ef6fc1737022 | 130 | DAC_fifo[i+1]=3000; |
sravet | 0:ef6fc1737022 | 131 | } |
sravet | 0:ef6fc1737022 | 132 | DAC_wptr=4; |
sravet | 0:ef6fc1737022 | 133 | |
sravet | 0:ef6fc1737022 | 134 | body.period_us(100); |
sravet | 0:ef6fc1737022 | 135 | mouth.period_us(100); |
sravet | 0:ef6fc1737022 | 136 | tail.period_us(100); |
sravet | 0:ef6fc1737022 | 137 | led1=led2=led3=led4=0; |
sravet | 0:ef6fc1737022 | 138 | |
sravet | 0:ef6fc1737022 | 139 | printf("Playing wave file '%s', mov file '%s'\n",wavname,movname); |
sravet | 0:ef6fc1737022 | 140 | |
sravet | 0:ef6fc1737022 | 141 | wavfile=fopen(wavname,"rb"); |
sravet | 0:ef6fc1737022 | 142 | if (!wavfile) { |
sravet | 0:ef6fc1737022 | 143 | printf("Unable to open wav file '%s'\n",wavname); |
sravet | 0:ef6fc1737022 | 144 | return; |
sravet | 0:ef6fc1737022 | 145 | } |
sravet | 0:ef6fc1737022 | 146 | |
sravet | 0:ef6fc1737022 | 147 | |
sravet | 0:ef6fc1737022 | 148 | fread(&chunk_id,4,1,wavfile); |
sravet | 0:ef6fc1737022 | 149 | fread(&chunk_size,4,1,wavfile); |
sravet | 0:ef6fc1737022 | 150 | while (!feof(wavfile)) { |
sravet | 0:ef6fc1737022 | 151 | printf("Read chunk ID 0x%x, size 0x%x\n",chunk_id,chunk_size); |
sravet | 0:ef6fc1737022 | 152 | switch (chunk_id) { |
sravet | 0:ef6fc1737022 | 153 | case 0x46464952: |
sravet | 0:ef6fc1737022 | 154 | fread(&data,4,1,wavfile); |
sravet | 0:ef6fc1737022 | 155 | printf("RIFF chunk\n"); |
sravet | 0:ef6fc1737022 | 156 | printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); |
sravet | 0:ef6fc1737022 | 157 | printf(" RIFF type 0x%x\n",data); |
sravet | 0:ef6fc1737022 | 158 | break; |
sravet | 0:ef6fc1737022 | 159 | case 0x20746d66: |
sravet | 0:ef6fc1737022 | 160 | fread(&wav_format,sizeof(wav_format),1,wavfile); |
sravet | 0:ef6fc1737022 | 161 | printf("FORMAT chunk\n"); |
sravet | 0:ef6fc1737022 | 162 | printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); |
sravet | 0:ef6fc1737022 | 163 | printf(" compression code %d\n",wav_format.comp_code); |
sravet | 0:ef6fc1737022 | 164 | printf(" %d channels\n",wav_format.num_channels); |
sravet | 0:ef6fc1737022 | 165 | printf(" %d samples/sec\n",wav_format.sample_rate); |
sravet | 0:ef6fc1737022 | 166 | printf(" %d bytes/sec\n",wav_format.avg_Bps); |
sravet | 0:ef6fc1737022 | 167 | printf(" block align %d\n",wav_format.block_align); |
sravet | 0:ef6fc1737022 | 168 | printf(" %d bits per sample\n",wav_format.sig_bps); |
sravet | 0:ef6fc1737022 | 169 | if (chunk_size > sizeof(wav_format)) |
sravet | 0:ef6fc1737022 | 170 | fseek(wavfile,chunk_size-sizeof(wav_format),SEEK_CUR); |
sravet | 0:ef6fc1737022 | 171 | // create a slice buffer large enough to hold multiple slices |
sravet | 0:ef6fc1737022 | 172 | slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE); |
sravet | 0:ef6fc1737022 | 173 | if (!slice_buf) { |
sravet | 0:ef6fc1737022 | 174 | printf("Unable to malloc slice buffer"); |
sravet | 0:ef6fc1737022 | 175 | exit(1); |
sravet | 0:ef6fc1737022 | 176 | } |
sravet | 0:ef6fc1737022 | 177 | // now that the sample rate is known, process the movement file |
sravet | 0:ef6fc1737022 | 178 | num_movements=process_movement_file(movname,movements,wav_format.sample_rate); |
sravet | 0:ef6fc1737022 | 179 | break; |
sravet | 0:ef6fc1737022 | 180 | |
sravet | 0:ef6fc1737022 | 181 | case 0x61746164: |
sravet | 0:ef6fc1737022 | 182 | slice_buf=(char *)malloc(wav_format.block_align*SLICE_BUF_SIZE); |
sravet | 0:ef6fc1737022 | 183 | if (!slice_buf) { |
sravet | 0:ef6fc1737022 | 184 | printf("Unable to malloc slice buffer"); |
sravet | 0:ef6fc1737022 | 185 | exit(1); |
sravet | 0:ef6fc1737022 | 186 | } num_slices=chunk_size/wav_format.block_align; |
sravet | 0:ef6fc1737022 | 187 | printf("DATA chunk\n"); |
sravet | 0:ef6fc1737022 | 188 | printf(" chunk size %d (0x%x)\n",chunk_size,chunk_size); |
sravet | 0:ef6fc1737022 | 189 | printf(" %d slices\n",num_slices); |
sravet | 0:ef6fc1737022 | 190 | printf(" Ideal sample interval=%d\n",(unsigned)(1000000.0/wav_format.sample_rate)); |
sravet | 0:ef6fc1737022 | 191 | samp_int=1000000/(wav_format.sample_rate); |
sravet | 0:ef6fc1737022 | 192 | if (slow) samp_int*=1.5; |
sravet | 0:ef6fc1737022 | 193 | printf(" programmed interrupt tick interval=%d\n",samp_int); |
sravet | 0:ef6fc1737022 | 194 | |
sravet | 0:ef6fc1737022 | 195 | // starting up ticker to write samples out -- no printfs until tick.detach is called |
sravet | 0:ef6fc1737022 | 196 | current_movement=0; |
sravet | 0:ef6fc1737022 | 197 | tick.attach_us(&dac_out, samp_int); |
sravet | 0:ef6fc1737022 | 198 | led2=1; |
sravet | 0:ef6fc1737022 | 199 | for (slice=0;slice<num_slices;slice+=SLICE_BUF_SIZE) { |
sravet | 0:ef6fc1737022 | 200 | fread(slice_buf,wav_format.block_align*SLICE_BUF_SIZE,1,wavfile); |
sravet | 0:ef6fc1737022 | 201 | if (feof(wavfile)) { |
sravet | 0:ef6fc1737022 | 202 | printf("Oops -- not enough slices in the wave file\n"); |
sravet | 0:ef6fc1737022 | 203 | exit(1); |
sravet | 0:ef6fc1737022 | 204 | } |
sravet | 0:ef6fc1737022 | 205 | data_sptr=(short *)slice_buf; |
sravet | 0:ef6fc1737022 | 206 | data_bptr=(unsigned char *)slice_buf; |
sravet | 0:ef6fc1737022 | 207 | data_wptr=(int *)slice_buf; |
sravet | 0:ef6fc1737022 | 208 | slice_value=0; |
sravet | 0:ef6fc1737022 | 209 | for (i=0;i<SLICE_BUF_SIZE;i++) { |
sravet | 0:ef6fc1737022 | 210 | for (channel=0;channel<wav_format.num_channels;channel++) { |
sravet | 0:ef6fc1737022 | 211 | switch (wav_format.sig_bps) { |
sravet | 0:ef6fc1737022 | 212 | case 16: |
sravet | 0:ef6fc1737022 | 213 | if (verbosity) |
sravet | 0:ef6fc1737022 | 214 | printf("16 bit channel %d data=%d ",channel,data_sptr[channel]); |
sravet | 0:ef6fc1737022 | 215 | slice_value+=data_sptr[channel]; |
sravet | 0:ef6fc1737022 | 216 | break; |
sravet | 0:ef6fc1737022 | 217 | case 32: |
sravet | 0:ef6fc1737022 | 218 | if (verbosity) |
sravet | 0:ef6fc1737022 | 219 | printf("32 bit channel %d data=%d ",channel,data_wptr[channel]); |
sravet | 0:ef6fc1737022 | 220 | slice_value+=data_wptr[channel]; |
sravet | 0:ef6fc1737022 | 221 | break; |
sravet | 0:ef6fc1737022 | 222 | case 8: |
sravet | 0:ef6fc1737022 | 223 | if (verbosity) |
sravet | 0:ef6fc1737022 | 224 | printf("8 bit channel %d data=%d ",channel,(int)data_bptr[channel]); |
sravet | 0:ef6fc1737022 | 225 | slice_value+=data_bptr[channel]; |
sravet | 0:ef6fc1737022 | 226 | break; |
sravet | 0:ef6fc1737022 | 227 | } |
sravet | 0:ef6fc1737022 | 228 | } |
sravet | 0:ef6fc1737022 | 229 | slice_value/=wav_format.num_channels; |
sravet | 0:ef6fc1737022 | 230 | |
sravet | 0:ef6fc1737022 | 231 | // slice_value is now averaged. Next it needs to be scaled to an unsigned 16 bit value |
sravet | 0:ef6fc1737022 | 232 | // with DC offset so it can be written to the DAC. |
sravet | 0:ef6fc1737022 | 233 | switch (wav_format.sig_bps) { |
sravet | 0:ef6fc1737022 | 234 | case 8: slice_value<<=8; |
sravet | 0:ef6fc1737022 | 235 | break; |
sravet | 0:ef6fc1737022 | 236 | case 16: slice_value+=32768; |
sravet | 0:ef6fc1737022 | 237 | break; |
sravet | 0:ef6fc1737022 | 238 | case 32: slice_value>>=16; |
sravet | 0:ef6fc1737022 | 239 | slice_value+=32768; |
sravet | 0:ef6fc1737022 | 240 | break; |
sravet | 0:ef6fc1737022 | 241 | } |
sravet | 0:ef6fc1737022 | 242 | dac_data=(short unsigned )slice_value; |
sravet | 0:ef6fc1737022 | 243 | if (verbosity) |
sravet | 0:ef6fc1737022 | 244 | printf("sample %d wptr %d slice_value %d dac_data %u\n",slice,DAC_wptr,(int)slice_value,dac_data); |
sravet | 0:ef6fc1737022 | 245 | |
sravet | 0:ef6fc1737022 | 246 | // finally stick it in the DAC FIFO. If the write pointer wraps around and meets the read pointer |
sravet | 0:ef6fc1737022 | 247 | // the wait until the read pointer moves. |
sravet | 0:ef6fc1737022 | 248 | DAC_fifo[DAC_wptr]=dac_data; |
sravet | 0:ef6fc1737022 | 249 | DAC_wptr=(DAC_wptr+1) & 0xff; |
sravet | 0:ef6fc1737022 | 250 | while (DAC_wptr==DAC_rptr) { |
sravet | 0:ef6fc1737022 | 251 | } |
sravet | 0:ef6fc1737022 | 252 | } |
sravet | 0:ef6fc1737022 | 253 | } |
sravet | 0:ef6fc1737022 | 254 | led2=0; |
sravet | 0:ef6fc1737022 | 255 | // wait for ISR to drain FIFO |
sravet | 0:ef6fc1737022 | 256 | wait_us(300); |
sravet | 0:ef6fc1737022 | 257 | tick.detach(); |
sravet | 0:ef6fc1737022 | 258 | printf("Ticker detached\n"); |
sravet | 0:ef6fc1737022 | 259 | led3=1; |
sravet | 0:ef6fc1737022 | 260 | free(slice_buf); |
sravet | 0:ef6fc1737022 | 261 | break; |
sravet | 0:ef6fc1737022 | 262 | case 0x5453494c: |
sravet | 0:ef6fc1737022 | 263 | printf("INFO chunk, size %d\n",chunk_size); |
sravet | 0:ef6fc1737022 | 264 | fseek(wavfile,chunk_size,SEEK_CUR); |
sravet | 0:ef6fc1737022 | 265 | break; |
sravet | 0:ef6fc1737022 | 266 | default: |
sravet | 0:ef6fc1737022 | 267 | printf("unknown chunk type 0x%x, size %d\n",chunk_id,chunk_size); |
sravet | 0:ef6fc1737022 | 268 | data=fseek(wavfile,chunk_size,SEEK_CUR); |
sravet | 0:ef6fc1737022 | 269 | break; |
sravet | 0:ef6fc1737022 | 270 | } |
sravet | 0:ef6fc1737022 | 271 | fread(&chunk_id,4,1,wavfile); |
sravet | 0:ef6fc1737022 | 272 | fread(&chunk_size,4,1,wavfile); |
sravet | 0:ef6fc1737022 | 273 | } |
sravet | 0:ef6fc1737022 | 274 | printf("Done with wave file\n"); |
sravet | 0:ef6fc1737022 | 275 | fclose(wavfile); |
sravet | 0:ef6fc1737022 | 276 | led1=0; |
sravet | 0:ef6fc1737022 | 277 | body.pulsewidth_us(0); |
sravet | 0:ef6fc1737022 | 278 | mouth.pulsewidth_us(0); |
sravet | 0:ef6fc1737022 | 279 | tail.pulsewidth_us(0); |
sravet | 0:ef6fc1737022 | 280 | } |
sravet | 0:ef6fc1737022 | 281 | |
sravet | 0:ef6fc1737022 | 282 | |
sravet | 0:ef6fc1737022 | 283 | void dac_out() { |
sravet | 0:ef6fc1737022 | 284 | int value; |
sravet | 0:ef6fc1737022 | 285 | digout=1; |
sravet | 0:ef6fc1737022 | 286 | if (!movements[current_movement].played) { |
sravet | 0:ef6fc1737022 | 287 | if (movements[current_movement].sample<=slice) { |
sravet | 0:ef6fc1737022 | 288 | if (movements[current_movement].motor==0) body.pulsewidth_us(movements[current_movement].duty_cycle); |
sravet | 0:ef6fc1737022 | 289 | if (movements[current_movement].motor==1) mouth.pulsewidth_us(movements[current_movement].duty_cycle); |
sravet | 0:ef6fc1737022 | 290 | if (movements[current_movement].motor==2) tail.pulsewidth_us(movements[current_movement].duty_cycle); |
sravet | 0:ef6fc1737022 | 291 | movements[current_movement].played=1; |
sravet | 0:ef6fc1737022 | 292 | current_movement++; |
sravet | 0:ef6fc1737022 | 293 | } |
sravet | 0:ef6fc1737022 | 294 | } |
sravet | 0:ef6fc1737022 | 295 | DACout.write_u16(DAC_fifo[DAC_rptr]); |
sravet | 0:ef6fc1737022 | 296 | DAC_rptr=(DAC_rptr+1) & 0xff; |
sravet | 0:ef6fc1737022 | 297 | digout=0; |
sravet | 0:ef6fc1737022 | 298 | } |
sravet | 0:ef6fc1737022 | 299 | |
sravet | 0:ef6fc1737022 | 300 | |
sravet | 0:ef6fc1737022 | 301 | void cleanup(char *s) |
sravet | 0:ef6fc1737022 | 302 | { |
sravet | 0:ef6fc1737022 | 303 | char *t; |
sravet | 0:ef6fc1737022 | 304 | t=strchr(s,'\n'); |
sravet | 0:ef6fc1737022 | 305 | if (t) *t=0; |
sravet | 0:ef6fc1737022 | 306 | t=strchr(s,'\r'); |
sravet | 0:ef6fc1737022 | 307 | if (t) *t=0; |
sravet | 0:ef6fc1737022 | 308 | } |
sravet | 0:ef6fc1737022 | 309 | |
sravet | 0:ef6fc1737022 | 310 | unsigned process_movement_file (char *mfname, MOV_STRUCT *mv,unsigned samp_rate) |
sravet | 0:ef6fc1737022 | 311 | { |
sravet | 0:ef6fc1737022 | 312 | FILE *movfile; |
sravet | 0:ef6fc1737022 | 313 | char line[100],*tmp; |
sravet | 0:ef6fc1737022 | 314 | unsigned num_movements,i,j,x; |
sravet | 0:ef6fc1737022 | 315 | movfile=fopen(mfname,"rb"); |
sravet | 0:ef6fc1737022 | 316 | if (!movfile) { |
sravet | 0:ef6fc1737022 | 317 | printf("Unable to open mov file '%s'\n",mfname); |
sravet | 0:ef6fc1737022 | 318 | return 0; |
sravet | 0:ef6fc1737022 | 319 | } |
sravet | 0:ef6fc1737022 | 320 | |
sravet | 0:ef6fc1737022 | 321 | fgets(line,100,movfile); |
sravet | 0:ef6fc1737022 | 322 | num_movements=0; |
sravet | 0:ef6fc1737022 | 323 | #ifdef VERBOSE |
sravet | 0:ef6fc1737022 | 324 | printf("Motor report...\n"); |
sravet | 0:ef6fc1737022 | 325 | #endif |
sravet | 0:ef6fc1737022 | 326 | while (!feof(movfile)) { |
sravet | 0:ef6fc1737022 | 327 | if (line[0]!='#') { |
sravet | 0:ef6fc1737022 | 328 | tmp=line; |
sravet | 0:ef6fc1737022 | 329 | // first thing on line is time in ms |
sravet | 0:ef6fc1737022 | 330 | movements[num_movements].sample=(atol(tmp)*samp_rate)/1000; |
sravet | 0:ef6fc1737022 | 331 | // skip digits (non whitespace) |
sravet | 0:ef6fc1737022 | 332 | tmp=line; |
sravet | 0:ef6fc1737022 | 333 | while (*tmp!=' ' && *tmp!='\t' && *tmp!=0) |
sravet | 0:ef6fc1737022 | 334 | tmp++; |
sravet | 0:ef6fc1737022 | 335 | // skip whitespace |
sravet | 0:ef6fc1737022 | 336 | while ((*tmp==' ' | *tmp=='\t') && *tmp!=0) |
sravet | 0:ef6fc1737022 | 337 | tmp++; |
sravet | 0:ef6fc1737022 | 338 | if (strstr(tmp,"body")) |
sravet | 0:ef6fc1737022 | 339 | movements[num_movements].motor=0; |
sravet | 0:ef6fc1737022 | 340 | if (strstr(tmp,"mouth")) |
sravet | 0:ef6fc1737022 | 341 | movements[num_movements].motor=1; |
sravet | 0:ef6fc1737022 | 342 | if (strstr(tmp,"tail")) |
sravet | 0:ef6fc1737022 | 343 | movements[num_movements].motor=2; |
sravet | 0:ef6fc1737022 | 344 | // skip letters (non whitespace) |
sravet | 0:ef6fc1737022 | 345 | while (*tmp!=' ' && *tmp!='\t') |
sravet | 0:ef6fc1737022 | 346 | tmp++; |
sravet | 0:ef6fc1737022 | 347 | // skip whitespace |
sravet | 0:ef6fc1737022 | 348 | while (*tmp==' ' | *tmp=='\t') |
sravet | 0:ef6fc1737022 | 349 | tmp++; |
sravet | 0:ef6fc1737022 | 350 | if (tmp) |
sravet | 0:ef6fc1737022 | 351 | movements[num_movements].duty_cycle=atoi(tmp); |
sravet | 0:ef6fc1737022 | 352 | movements[num_movements].played=0; |
sravet | 0:ef6fc1737022 | 353 | #ifdef VERBOSE |
sravet | 0:ef6fc1737022 | 354 | printf(" moving motor %d at sample %ld with duty cycle %d\n",movements[num_movements].motor,movements[num_movements].sample,movements[num_movements].duty_cycle); |
sravet | 0:ef6fc1737022 | 355 | #endif |
sravet | 0:ef6fc1737022 | 356 | num_movements++; |
sravet | 0:ef6fc1737022 | 357 | } |
sravet | 0:ef6fc1737022 | 358 | fgets(line,100,movfile); |
sravet | 0:ef6fc1737022 | 359 | } |
sravet | 0:ef6fc1737022 | 360 | printf(" %d movements read\n",num_movements); |
sravet | 0:ef6fc1737022 | 361 | printf(" sorting movements..."); |
sravet | 0:ef6fc1737022 | 362 | for (i=0;i<num_movements;i++) { |
sravet | 0:ef6fc1737022 | 363 | for (j=i;j<num_movements;j++) { |
sravet | 0:ef6fc1737022 | 364 | if (movements[j].sample < movements[i].sample) { |
sravet | 0:ef6fc1737022 | 365 | x=movements[i].sample; movements[i].sample=movements[j].sample; movements[j].sample=x; |
sravet | 0:ef6fc1737022 | 366 | x=movements[i].motor ; movements[i].motor =movements[j].motor ; movements[j].motor =x; |
sravet | 0:ef6fc1737022 | 367 | x=movements[i].duty_cycle;movements[i].duty_cycle=movements[j].duty_cycle;movements[j].duty_cycle=x; |
sravet | 0:ef6fc1737022 | 368 | } |
sravet | 0:ef6fc1737022 | 369 | } |
sravet | 0:ef6fc1737022 | 370 | } |
sravet | 0:ef6fc1737022 | 371 | printf("done\n"); |
sravet | 0:ef6fc1737022 | 372 | return num_movements; |
sravet | 0:ef6fc1737022 | 373 | } |