📄 finalproject.c
字号:
checksum = 0xDB; //Current track checksum
stopflag = 0;
puts_int();
state = Receive; //Now we receive a response
end
//Now we send the command we wanted to send!
if(mode4sequence == UseTrack)
begin
for (k=0; k<16; k++)
begin
t_buffer[k] = t_buffer_temp[k];
//Bring back the real command
end
//Placing the current song into the command
//& with 0xFF cause we're SCARED!
t_buffer[6] = (currentsong >> 24) & 0xff;
t_buffer[7] = (currentsong >> 16) & 0xff;
t_buffer[8] = (currentsong >> 8) & 0xff;
t_buffer[9] = currentsong & 0xff;
//recreating the checksum
for (index = 2; index < 10; index ++)
begin
checksum = checksum + t_buffer[index];
end
checksum = (0x100-checksum)&0xFF;
t_buffer[10] = (char)checksum;
//Now we transmit the command we wanted!
stopflag = 0;
puts_int();
state = Receive;
end
//Switch back to mode 2
if(mode4sequence == SwitchtoMode2)
begin
for(k=0; k<16; k++)
begin
t_buffer[k] = switchback[k];
end
checksum = 0xFA; //This is the checksum
stopflag = 0;
puts_int();
state = Receive;
end
end
break;
case(Receive):
//Before we can receive, we need to wait for transmission to complete
while(!t_ready){};
if(mode4sequence == SwitchtoMode2)
begin
//Success!
failurecount = 0;
mode4sequence = Switch; //Originally Switch
state = Command;
end
if(mode4sequence == Switch)
begin
if(timeoutcounter >= 1000) {
mode4sequence = GetTrack;
state = Send;
timeoutcounter = 0;
}
end
timeoutcounter ++;
//Base case: timeoutcounter = 1000 or we failed 5x
if(timeoutcounter >= 2000 || failurecount >= 5)
begin
failurecount = 0;
timeoutcounter = 0;
if(mode4sequence != SwitchtoMode2)
begin
//Try to bring it back to initial conditions
mode4sequence = SwitchtoMode2;
state = Send;
end
else
begin
//Looks like all hope is lost.
mode4sequence = Switch;
state = Command;
end
end
//Otherwise, we're still waiting to receive stuff
if(r_ready)
begin
//r_index holds the last thing
//What did we receive? Depends on the sequence
if(mode4sequence == UseTrack)
begin
//Check for success
if((r_buffer[4] == 0x00) && ((r_buffer[5] != 0) || (r_buffer[5] != 1)))
begin
failurecount = 0;
//the string is null terminated and starts at r_buffer[6]
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("Num ");
ltoa(currentsong+1,&songnumber);
lcd_puts(&songnumber);
lcd_putsf(" - ");
//Declare artist, album, or title
if(r_buffer[5] == 0x21)
lcd_putsf("Title ");
if(r_buffer[5] == 0x23)
lcd_putsf("Artist");
if(r_buffer[5] ==0x25)
lcd_putsf("Album ");
lcdbufferlength = r_index - 8;
lcdstart = 0;
for(k=6; k< 6+lcdbufferlength; k++)
{
lcdbuffer[k-6] = r_buffer[k];
}
lcdbufferlength = lcdbufferlength+1;
lcdbuffer[lcdbufferlength-1] = ' ';
lcdbuffer[lcdbufferlength] = ' ';
lcdbufferlength = lcdbufferlength + 1;
state = Send;
mode4sequence = SwitchtoMode2;
end
else
begin
//Failure!
failurecount ++;
state = Send;
end
end
if(mode4sequence == GetTrack)
begin
if((r_buffer[3] == 0x04) && (r_buffer[4]==0x00) && (r_buffer[5] == 0x1f))
begin
failurecount = 0;
currentsong = r_buffer[6];
currentsong = (currentsong << 8) | r_buffer[7];
currentsong = (currentsong << 8) | r_buffer[8];
currentsong = (currentsong << 8) | r_buffer[9];
state = Send;
mode4sequence = UseTrack;
end
else //FAILURE!
begin
failurecount++;
state = Send;
end
end
gets_int(); //Start listening for the Serial TX again!
end
break;
case(button_release): //Send a button release;
t_buffer[0] = header1; //0xff
t_buffer[1] = header2; //0x55
t_buffer[2] = length; //0x03 at the moment (not always true)
t_buffer[3] = 0x02; //This is the mode we support right now. TWO
t_buffer[4] = 0x00; //Beginning of command
t_buffer[5] = 0x00; //button release command
checksum = 0xFB;
t_buffer[6] = 0xFB; //as a char, should be 0xfb here
state= Send;
break;
end
end
end
end
void buttons(void) {
press = ~PINA;
if((press != lastpress) && (lastpress == 0)) { // Start debouncing
debouncecounter++;
lastpress = press;
}
if((press == lastpress) && (lastpress != 0)) //Held button
debouncecounter ++;
if ((press != lastpress) && (lastpress != 0)) { //Released button
releaseflag = 1;
validcommand = 0;
debouncecounter = 0;
lastpress = 0;
press = 0;
}
if((debouncecounter >= 40) && (lastpress != 0)) { //40ms
//we have a solid press
validcommand = 1;
debouncecounter = -460; //To account for a .5 second holding time, Vol+
switch(press) {
case(0x01):
//Play
commandtype = play;
mode = 0x02;
break;
case(0x02):
commandtype = volup;
mode = 0x02;
break;
case(0x04):
commandtype = voldown;
mode = 0x02;
break;
case(0x08):
commandtype = skipfwd;
mode = 0x02;
break;
case(0x10):
commandtype = skipback;
mode = 0x02;
break;
case(0x20):
commandtype = album;
mode = 0x04;
break;
case(0x40):
commandtype = title;
mode = 0x04;
break;
case(0x80):
commandtype = artist;
mode = 0x04;
break;
default:
commandtype = 0;
break;
}
}
}
//Set it all up
void initialize(void)
begin
DDRA = 0x00; // PORT A is an input - button switches for now
DDRB = 0xff; //PORTB LED output
PORTB = 0xff; //LEDs off for our sanity
//serial setup for debugging using printf, etc.
UCSRB = 0x18 ;
UBRRL = 51 ; //set the oscillator, we need 19200 baud for iPod comm.
//set up timer 0
OCR0=249; //1 mSec
TIMSK=2; //turn on timer 0 cmp-match ISR
TCCR0=0b00001011; //prescalar to 64 and Clr-on-match
lcd_init(16); //initialize the display
lcd_clear();
lcd_gotoxy(0,0);
lcd_putsf("start");
//crank up the ISRs
#asm
sei
#endasm
gets_int(); //Start listening on the serial port
end
void puts_int(void) {
t_ready=0;
t_index=0;
stopflag =0;
if (t_buffer[0]>0)
begin
putchar(t_buffer[0]);
UCSRB.5=1;
end
}
void gets_int(void) {
r_ready = 0;
r_index = 0;
r_length = 0xF5;
UCSRB.7 = 1;
}
void lcd_scroll(void){
unsigned char displaybuffer[16];
int lcd_index;
lcd_gotoxy(0,1);
lcd_putsf(" "); //We clear it for scrolling
lcd_gotoxy(0,1);
lcd_index=0;
while(lcd_index<15)
{
displaybuffer[lcd_index] = lcdbuffer[((lcdstart + lcd_index)%lcdbufferlength)];
if(lcd_index>=lcdbufferlength) //Prevents repeating of small strings
{
displaybuffer[lcd_index] = ' ';
}
lcd_index++;
}
displaybuffer[15] = 0x00;
lcd_puts(&displaybuffer[0]);
if(lcdbufferlength>16) //Prevents scrolling of small strings
lcdstart = (lcdstart+1);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -