Пример #1
0
void loop() {
  unsigned char ticks_needed = TICKS_TO_GATHER;
  while(1){
    doTick(); // 1
    doSleep(); // 2
    if (q_random() % 4) {
      // Be normal. A "second" is 10 ticks long.
      for(unsigned char i = 0; i < IRQS_PER_SECOND - 2; i++)
        doSleep();
    } else {
      // This is a special "second" - it's *11* ticks long.
      // Every tenth one, we're goging to insert a "stutter tick"
      if (--ticks_needed == 0) {
        doTick();
        for(unsigned char i = 0; i < PAUSE_TICKS; i++)
          doSleep();
        ticks_needed = TICKS_TO_GATHER;
      } else {
        doSleep();
      }
      for (unsigned char i = 0; i < IRQS_PER_SECOND - 2; i++) // yes, -2, not -3.
        doSleep();
    }
  }
}
Пример #2
0
void loop() {
  while(1) {
    // Do this about once a minute-ish.
    if (q_random() % 30 != 0) {
      // a normal second.
      doTick();
      for(int i = 0; i < IRQS_PER_SECOND - 1; i++)
        doSleep();
      continue;
    }

    // Time to play a song!
    unsigned int song = q_random() % SONG_COUNT;
    unsigned char *current_song = (unsigned char*)pgm_read_ptr(song_table + song);
    while(1) {
      unsigned char song_data = pgm_read_byte(current_song++);
      if (song_data == 0) break; // song over
      doTick();
      for(int i = 0; i < song_data; i++)
        doSleep();
    }
  }
}