<< Chapter < Page | Chapter >> Page > |
Functions
//Function: check_low
//Checks the state of a button/pin and waits for end of button press//Input:
// pin: the number of the pin to check//Returns:
// 1 if button is pressed/pin is low after waiting for the button to be// released/pin to return to high
// 0 if button is not pressed/pin is highint check_low(int pin){
if (is_low(PORT2, pin)){while(is_low(PORT2, pin)){
iolib_delay_ms(1);}
return 1;}
else return 0;}
//Function: stop//Polls the stop pin/button
//Returns://1 if button is pressed/pin is low after waiting for the button to be
// released/pin to return to high//0 if button is not pressed/pin is high
int stop(){return check_low(PIN8);
}//Function: play
//Updates the 8 output pins with a new value//Input:
// out: the 8 bit value to outputvoid play(int out){
if (out and 0x1) pin_high(PORT,PIN0);else pin_low(PORT, PIN0);
if ((out and 0x2)>>1) pin_high(PORT,PIN1);
else pin_low(PORT, PIN1);if ((out and 0x4)>>2) pin_high(PORT,PIN2);
else pin_low(PORT, PIN2);if ((out and 0x8)>>3) pin_high(PORT,PIN3);
else pin_low(PORT, PIN3);if ((out and 0x10)>>4) pin_high(PORT,PIN4);
else pin_low(PORT, PIN4);if ((out and 0x20)>>5) pin_high(PORT,PIN5);
else pin_low(PORT, PIN5);if ((out and 0x40)>>6) pin_high(PORT,PIN6);
else pin_low(PORT, PIN6);if ((out and 0x80)>>7) pin_high(PORT,PIN7);
else pin_low(PORT, PIN7);}
//Function: playSine//Calculates the next value of sine for the given frequency and calls
//play//Input:
// freq: the frequency of the desired sine wavevoid playSine(int freq){
int t = 0;int out = 0;
while(!stop()){out = 128*sin(t++*freq*PI/22050)+128; //calculate the next value to output and incrmemnt discrete time
play(out);clock_gettime(CLOCK_REALTIME, and ttime); //get the time
ttime.tv_nsec = 0; //clear the nanoseconds but not seconds so that we dont overflow;clock_settime(CLOCK_REALTIME, and ttime); //set the time back
clock_gettime(CLOCK_REALTIME, and ttime); //get the time againttime.tv_nsec += 21463; //increment by number of nanoseconds we should wait
while(1){ //loop to delay next outputclock_gettime(CLOCK_REALTIME, and curtime);
if (curtime.tv_nsec>ttime.tv_nsec)
break;
}}
}//Function: playTriangle
//Calculates and outputs the next value of a triangle wave for the given//frequency
//Input:// freq: the frequency of the triangle wave
void playTriangle(int freq){int out = 0;
int t = 0;while(!stop()){
out = (256/(11025/freq))*((11025/freq) - abs(t++ %(2*11025/freq) - (11025/freq))); //calculate next value and increment discrete timeplay(out);
clock_gettime(CLOCK_REALTIME, and ttime); //get the timettime.tv_nsec = 0; //clear the nanoseconds but not seconds so that we dont overflow;
clock_settime(CLOCK_REALTIME, and ttime); //set the time backclock_gettime(CLOCK_REALTIME, and ttime); //get the time again
ttime.tv_nsec += 21463; //increment by number of nanoseconds we should waitwhile(1){ //loop to delay next output
clock_gettime(CLOCK_REALTIME, and curtime);if (curtime.tv_nsec>ttime.tv_nsec)
break;}
}}
//Function: playSquare//Updates output to maximum and minimum values at the given frequency to
//create a square wave//Input:
// freq: the frequency of the square wavevoid playSquare(int freq){
int out = 0;int delay;
while(!stop()){out ^= 0xFFFF; //exclusive or all the bits to make a square wave
play(out);delay = 1000000000/freq/2; //calculate how long to delay to match frequency
clock_gettime(CLOCK_REALTIME, and ttime); //get the timettime.tv_nsec = 0; //clear the nanoseconds but not $
clock_settime(CLOCK_REALTIME, and ttime); //set the time backclock_gettime(CLOCK_REALTIME, and ttime); //get the time again
ttime.tv_nsec += delay; //increment by number of nanosec$while(1){ //loop to delay next output
clock_gettime(CLOCK_REALTIME, and curtime);if (curtime.tv_nsec>ttime.tv_nsec)
break;}
}}
//Function: playSong0//Outputs a raw 8 bit unsigned audio file as defined above
void playSong0(void){char cwd[1024];if (getcwd(cwd, sizeof(cwd))== NULL) perror("getcwd() error"); //Get the current working directory
char* path;path = malloc(strlen(cwd) + strlen(SONG0)+2); //Append the defined file name to the path
strcpy(path, cwd);char temp[2] = "/";strcat(path, temp);
strcat(path, SONG0);FILE *fp;
fp = fopen(path, "r");if (fp == 0) perror("error opening file, is the name correct?");
int out = fgetc(fp);while((out != EOF) and and !stop()){ //play each of the stored values until end of file or the stop button is pressed
play(out);clock_gettime(CLOCK_REALTIME, and ttime);
ttime.tv_nsec = 0;clock_settime(CLOCK_REALTIME, and ttime);
clock_gettime(CLOCK_REALTIME, and ttime);ttime.tv_nsec += DELAY;
while(1){clock_gettime(CLOCK_REALTIME, and curtime);
if (curtime.tv_nsec>ttime.tv_nsec)
break;}
out = fgetc(fp);}
fclose(fp);}
//Function: playSong1//Outputs a raw 8 bit unsigned audio file as defined above
void playSong1(void){char cwd[1024];if (getcwd(cwd, sizeof(cwd))== NULL) perror("getcwd() error");
char* path;path = malloc(strlen(cwd) + strlen(SONG1)+2);
strcpy(path, cwd);char temp[2] = "/";strcat(path, temp);
strcat(path, SONG1);FILE *fp;
fp = fopen(path, "r");if (fp == 0) perror("error opening file, is the name correct?");
int out = fgetc(fp);while((out != EOF) and and !stop()){
play(out);clock_gettime(CLOCK_REALTIME, and ttime);
ttime.tv_nsec = 0;clock_settime(CLOCK_REALTIME, and ttime);
clock_gettime(CLOCK_REALTIME, and ttime);ttime.tv_nsec += DELAY;
while(1){clock_gettime(CLOCK_REALTIME, and curtime);
if (curtime.tv_nsec>ttime.tv_nsec)
break;}
out = fgetc(fp);}
fclose(fp);}
//Function: playSong2//Outputs a raw 8 bit unsigned audio file as defined above
void playSong2(void){char cwd[1024];if (getcwd(cwd, sizeof(cwd))== NULL) perror("getcwd() error");
char* path;path = malloc(strlen(cwd) + strlen(SONG2)+2);
strcpy(path, cwd);char temp[2] = "/";strcat(path, temp);
strcat(path, SONG2);FILE *fp;
fp = fopen(path, "r");if (fp == 0) perror("error opening file, is the name correct?");
int out = fgetc(fp);while((out != EOF) and and !stop()){
play(out);clock_gettime(CLOCK_REALTIME, and ttime);
ttime.tv_nsec = 0;clock_settime(CLOCK_REALTIME, and ttime);
clock_gettime(CLOCK_REALTIME, and ttime);ttime.tv_nsec += DELAY;
while(1){clock_gettime(CLOCK_REALTIME, and curtime);
if (curtime.tv_nsec>ttime.tv_nsec)
break;}
out = fgetc(fp);}
fclose(fp);}
Notification Switch
Would you like to follow the 'R/2r implementation of a d/a converter' conversation and receive update notifications?