00001
00002
00003
00004
00005
00006
00007
00008
00009 #include "robotsound.h"
00010
00011 u08 __ATTR_PROGMEM__ beeps[] = {5,40, 10,0, 5,40, 10,0, 5,40, 10,0, 5,40, 10,0, 5,40, 10,0,
00012 5,40, 10,0, 5,40, 10,0, 5,40, 10,0, 5,40, 10,0, 5,40, 40,0, 0,0};
00013 u08 __ATTR_PROGMEM__ start_up[] = {5,42, 3,54, 10,0, 0,0};
00014 u08 __ATTR_PROGMEM__ go_sleep[] = {3,70, 3,60, 3,50, 3,40, 3,30, 3,20, 0,0};
00015 u08 __ATTR_PROGMEM__ wake_up[] = {3,20, 3,30, 3,40, 3,50, 3,60, 3,70, 0,0};
00016 u08 __ATTR_PROGMEM__ go_hungry[] = {20,36, 20,48, 20,36, 20,48, 0,0};
00017 u08 __ATTR_PROGMEM__ go_empty[] = {10,49, 255,0, 10,49, 255,0, 10,49, 0,0};
00018 u08 __ATTR_PROGMEM__ snore[] = {50,1, 40,0, 0,0};
00019 u08 __ATTR_PROGMEM__ get_connected[] = {1,20, 50,85, 50,86, 10,70, 0,0};
00020 u16 __ATTR_PROGMEM__ tones[] = { 0,
00021 33, 35, 37, 39, 41, 44, 46, 49, 52, 55, 58, 62,
00022 65, 69, 73, 78, 82, 87, 92, 98, 104, 110, 117, 123,
00023 131, 139, 147, 156, 164, 175, 185, 196, 208, 220, 233, 247,
00024 262, 277, 294, 311, 330, 349, 370, 392, 415, 440, 466, 494,
00025 523, 554, 587, 622, 659, 698, 740, 784, 831, 880, 932, 988,
00026 1047, 1109, 1175, 1245, 1319, 1397, 1480, 1568, 1661, 1760, 1865, 1976,
00027 2093, 2217, 2349, 2489, 2637, 2794, 2960, 3136, 3322, 3520, 3729, 3951};
00028
00029 u08 *act_sound;
00030 s16 s_ticks;
00031
00032 void play(u16 Hz) {
00033 u08 tccr2tmp;
00034 if (Hz < 17) {
00035 tccr2tmp = 0;
00036 } else if (Hz < 61) {
00037 tccr2tmp = _BV(CS22)|_BV(CS21)|_BV(CS20);
00038 OCR2 = 4000000/1024 / Hz;
00039 } else if (Hz < 124) {
00040 tccr2tmp = _BV(CS22)|_BV(CS21);
00041 OCR2 = 4000000/256 / Hz;
00042 } else if (Hz < 247) {
00043 tccr2tmp = _BV(CS22)|_BV(CS20);
00044 OCR2 = 4000000/128 / Hz;
00045 } else if (Hz < 493) {
00046 tccr2tmp = _BV(CS22);
00047 OCR2 = 4000000/64 / Hz;
00048 } else if (Hz < 1968) {
00049 tccr2tmp = _BV(CS21)|_BV(CS20);
00050 OCR2 = 4000000/32 / Hz;
00051 } else {
00052 tccr2tmp = _BV(CS21);
00053 OCR2 = 4000000/8 / Hz;
00054 }
00055 if (TCNT2 > OCR2) TCNT2 = 0;
00056 TCCR2 = tccr2tmp | _BV(WGM21) | _BV(COM20);
00057 }
00058
00059 void playt(u08 tone) {
00060 static u08 ltone;
00061 if (tone < 84)
00062 ltone = tone;
00063 else {
00064 switch (tone) {
00065 case 85:
00066 if (ltone++ > 84) ltone = 1;
00067 break;
00068 case 86:
00069 if (--ltone < 2) ltone = 84;
00070 break;
00071 case 87:
00072 break;
00073 }
00074 }
00075 play(pgm_read_word_near(&tones[ltone]));
00076 }
00077
00078 int playi() {
00079 static u08 ttplay;
00080 if (!soundplaying) return;
00081 if (s_ticks>0) {
00082 s_ticks--;
00083 } else {
00084 s_ticks = pgm_read_byte_near(&act_sound[0]);
00085 ttplay = pgm_read_byte_near(&act_sound[1]);
00086 act_sound +=2;
00087 if (!s_ticks) {
00088 TIMSK &= ~_BV(TOIE2);
00089 soundplaying = 0;
00090 }
00091 }
00092 playt(ttplay);
00093 }
00094
00095 int start_sound(u08 *soundtostart, u08 override) {
00096 if (soundplaying && !override) return;
00097 TIMSK |= _BV(TOIE2);
00098 act_sound = soundtostart;
00099 s_ticks = 0;
00100 soundplaying = 1;
00101 }