00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044 #include "beep.h"
00045 #include "key.h"
00046 #include <stdlib.h>
00047 #include "util/delay.h"
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061 void
00062 beep(void)
00063 {
00064 uint8_t i;
00065 volatile uint8_t j;
00066
00067
00068 ENTER_DDR |= (1 << PE7);
00069 ENTER_PUR |= (1 << PE7);
00070
00071
00072 BEEP_DDR |= (1 << BEEP_BIT);
00073
00074 for (i=0;i<100;i++){
00075
00076 BEEP_PIN |= (1 << BEEP_BIT);
00077
00078 for (j=0;j<0xff;j++)
00079 ;
00080 }
00081
00082 for (i=0;i<100;i++){
00083
00084 BEEP_PIN |= (1 << BEEP_BIT);
00085
00086 for (j=0;j<0xa0;j++)
00087 ;
00088 }
00089
00090
00091 BEEP_PORT &= ~(1 << BEEP_BIT);
00092 ENTER_PUR &= ~(1 << PE7);
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103 #define BASE 31250/2 //up one octave
00104 #define NONE 0
00105 #define C4 BASE/262
00106 #define CS4 BASE/277
00107 #define D4 BASE/294
00108 #define DS4 BASE/311
00109 #define E4 BASE/330
00110 #define F4 BASE/349
00111 #define FS4 BASE/370
00112 #define G4 BASE/392
00113 #define GS4 BASE/415
00114 #define A4 BASE/440
00115 #define AS4 BASE/466
00116 #define B4 BASE/494
00117 #define C5 BASE/523
00118 #define CS5 BASE/554
00119 #define D5 BASE/587
00120 #define DS5 BASE/622
00121 #define E5 BASE/659
00122 #define F5 BASE/698
00123 #define FS5 BASE/740
00124 #define G5 BASE/784
00125 #define GS5 BASE/831
00126 #define A5 BASE/880
00127 #define AS5 BASE/932
00128
00129
00130 #define TONE_LENGTH 60
00131 #define TONE_GAP 20
00132
00133
00134 static uint8_t tuneindex=0;
00135 static uint8_t pictures[] PROGMEM = {G4,4, F4,4, AS4,4, C5,2, F5,2, D5,4, C5,2, F5,2, D5,4, AS4,4, C5,4, G4,4, F4,4, 0xff};
00136 static uint8_t axel[] PROGMEM = {FS4,2, NONE,2, A4,3, FS4,2, FS4,1, B4,2, FS4,2, E4,2, FS4,2, NONE,2, CS5,3, FS4,2, FS4,1, D5,2, CS5,2, A4,2, FS4,2, CS5,2, FS5,2, FS4,1, E4,2, E4,1, CS4,2, GS4,2, FS4,6, 0xff};
00137 static uint8_t sandman1[] PROGMEM = {F4,2, G4,2, B4,4, A4,10, B4,2, B4,2, A4,2, B4,12, 0xff};
00138 static uint8_t sandman2[] PROGMEM = {C4,2, E4,2, G4,2, B4,2, A4,2, G4,2, E4,2, C4,2, D4,2, F4,2, A4,2, C5,2, B4,8, 0xff};
00139 static uint8_t furelise[] PROGMEM = {E5,1, DS5,1, E5,1, DS5,1, E5,1, B4,1, D5,1, E5,1, A4,2, NONE,1, C4,1, E4,1, A4,1, B4,2, NONE,1, E4,1, GS4,1, B4,1, C5,2, 0xff};
00140
00141 static volatile uint8_t icnt,tone;
00142
00143 #include <avr/interrupt.h>
00144 ISR(TIMER0_OVF_vect)
00145 {
00146 if (tone == NONE) icnt = 0;
00147 else if (icnt++ >= tone)
00148 {
00149 BEEP_PIN |= (1 << BEEP_BIT);
00150 icnt = 0;
00151 }
00152 }
00153
00154 void play_ringtone(void)
00155 {
00156 uint8_t i,*noteptr;
00157
00158
00159 switch (tuneindex++) {
00160 case 1 :beep();return;
00161 case 2 :noteptr=sandman1;break;
00162 case 3 :noteptr=furelise;break;
00163 case 4 :noteptr=sandman2;break;
00164 case 5 :noteptr=axel;break;
00165 default:noteptr=pictures;tuneindex=1;break;
00166 }
00167
00168
00169 ENTER_DDR |= (1 << PE7);
00170 ENTER_PUR |= (1 << PE7);
00171
00172
00173 BEEP_DDR |= (1 << BEEP_BIT);
00174
00175
00176 icnt = 0;
00177 tone = NONE;
00178
00179
00180
00181 TCCR0A |= _BV(CS00);
00182 TCNT0 = 0;
00183 TIMSK0 |= _BV(TOIE0);
00184
00185
00186 for (;;) {
00187 tone=pgm_read_byte(noteptr++);
00188 if (tone==0xff) break;
00189 for (i = pgm_read_byte(noteptr++);i > 0; i--) _delay_us(1000UL*TONE_LENGTH);
00190 tone = NONE;_delay_us(1000UL*TONE_GAP);
00191 }
00192
00193
00194 TIMSK0 &= ~_BV(TOIE0);
00195 BEEP_PORT &= ~(1 << BEEP_BIT);
00196 ENTER_PUR &= ~(1 << PE7);
00197 }
00198