-
Notifications
You must be signed in to change notification settings - Fork 2
/
tuney.c
85 lines (72 loc) · 2.77 KB
/
tuney.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
/*
Tuney Clock
Copyright 2014 Nicholas W. Sayer
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
/*
* This code will keep a long-term average pulse rate of 1 Hz,
* but will do so by interjecting various "song" patterns
* into the mix
*
*/
#if defined(UNIT_TEST)
// On *nix, there is no PROGMEM. Just make it go away and turn the
// pgm_read operations into just pointer derefs.
#define PROGMEM
#define pgm_read_byte(x) *(x)
#define pgm_read_ptr(x) ((void*)(*(x)))
#define PGM_VOID_P void*
#else
#include <avr/pgmspace.h>
#ifndef pgm_read_ptr
#define pgm_read_ptr pgm_read_word
#endif
#endif
#include "base.h"
// Each "song" is a series of pause counts - the pause between the ticks
// that make up the rhythm in question. To insure that the clock keeps
// proper time, the sum of the elements in each table must equal 9 times
// the number of elements in the table. The last element should be zero
// to mark the end.
// "Shave-and-a-haircut... two bits!"
PROGMEM const unsigned char shave_table[] = { 27, 3, 1, 1, 3, 7, 3, 27, 0 };
// Backbeat from "Heart Of Rock-n-Roll"
PROGMEM const unsigned char backbeat_table[] = { 36, 5, 1, 7, 5, 1, 7, 5, 1, 7, 5, 1, 36, 0 };
// SOS in morse
PROGMEM const unsigned char sos_table[] = { 23, 2, 2, 4, 6, 6, 8, 2, 2, 35, 0};
// Star Wars Imperial March
PROGMEM const unsigned char imperialmarch_table[] = { 32, 5, 5, 5, 2, 1, 5, 2, 1, 32, 0};
PROGMEM PGM_VOID_P const song_table[] = { (PGM_VOID_P)&shave_table, (PGM_VOID_P)&backbeat_table, (PGM_VOID_P)&sos_table, (PGM_VOID_P)&imperialmarch_table };
#define SONG_COUNT 4
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();
}
}
}