/** The MAIN loop. */ int main(void) { // Turn off the CPU prescale. CLKPR = 0x80; CLKPR = 0x00; // PORTA are general purpose inputs. DDRA = 0x00; PORTA = 0xff; // pull-ups all enabled PORTD = 0xff; DDRD = 0x00; // pull-ups all enabled DDRF = 0x00; // These are ADC lines. PORTF = 0x00; usb_init(); // Set up Timer 0 to match compare every 1ms. OCR0A = 250; TCCR0A = 0x02; // CTC TCCR0B = 0x03; // CK/64 (64 * 250 == 16000) sei(); char line_buf[128] = {}; uint8_t line_len = 0; uint8_t usb_ready = 0; while (1) { wdt_reset(); g_main_loop_count++; if (usb_configured() && (usb_serial_get_control() & USB_SERIAL_DTR)) { if (!usb_ready) { usb_ready = 1; strcpy_P(line_buf, PSTR("!GNR WELCOME " DEV_VERSION EOL)); usb_serial_write((uint8_t*)line_buf, strlen(line_buf)); line_len = 0; } } else { stream_stop_all(); usb_serial_flush_input(); usb_ready = 0; line_len = 0; g_arm_code = 0; } if (usb_serial_available()) { int16_t c = usb_serial_getchar(); if (c == '\r' || c == '\n') { line_buf[line_len] = 0; handle_line(line_buf); if (g_arm_code) { g_arm_timer = ARM_TIMEOUT_MS; } line_len = 0; } else { line_buf[line_len++] = c & 0xff; if ((line_len + 1) >= sizeof(line_buf)) { /* Clobber the first byte so that this line will be reported as an error. */ line_buf[0] = MAGIC_OVERRUN_CODE; line_len--; } } } if (TIFR0 & (1 << OCF0A)) { TIFR0 |= (1 << OCF0A); g_timer++; stream_timer_update(); hit_timer_update(); g_main_loop_count = (uint16_t) (((uint32_t) g_main_loop_count) * 7 / 8); } stream_poll(); usb_poll(); hit_poll(); } }
/* Call this function as a thread to handle playback. Playback will stop and this thread will return when you call sndmp3_shutdown(). */ static void sndmp3_thread() { int sj; /* Main command loop */ while(sndmp3_status != STATUS_QUIT) { switch(sndmp3_status) { case STATUS_INIT: sndmp3_status = STATUS_READY; break; case STATUS_READY: printf("sndserver: waiting on semaphore\r\n"); sem_wait(sndmp3_halt_sem); printf("sndserver: released from semaphore\r\n"); break; case STATUS_STARTING: /* Initialize streaming driver */ if (stream_init(mpglib_callback) < 0) { sndmp3_status = STATUS_READY; } else { /* stream_start(decinfo.samprate, decinfo.channels - 1); */ stream_start(44100, 1); sndmp3_status = STATUS_PLAYING; } break; case STATUS_REINIT: /* Re-initialize streaming driver */ stream_init(NULL); sndmp3_status = STATUS_READY; break; case STATUS_PLAYING: { sj = jiffies; if (stream_poll() < 0) { if (sndmp3_loop) { printf("sndserver: restarting '%s'\r\n", mp3_last_fn); if (mpglib_init(mp3_last_fn) < 0) { sndmp3_status = STATUS_STOPPING; mp3_last_fn[0] = 0; } } else { printf("sndserver: not restarting\r\n"); stream_stop(); sndmp3_status = STATUS_READY; mp3_last_fn[0] = 0; } // stream_start(); } if (sj == jiffies) thd_pass(); break; } case STATUS_STOPPING: stream_stop(); sndmp3_status = STATUS_READY; break; } } /* Done: clean up */ mpglib_shutdown(); stream_shutdown(); sndmp3_status = STATUS_ZOMBIE; }