-
Notifications
You must be signed in to change notification settings - Fork 1
/
PkInterface.pde
95 lines (73 loc) · 1.33 KB
/
PkInterface.pde
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
86
87
88
89
90
91
92
93
94
#include <Fat16.h>
#define RTS 2
#define CTS 3
SdCard card;
Fat16 file;
void error()
{
int i;
while(1)
{
for(i=0;i<500;i++) Serial.write((uint8_t)0x00);
for(i=0;i<500;i++) Serial.write((uint8_t)0xff);
}
}
void done()
{
delay(100);
while(1){
Serial.write((uint8_t)0x00);
}
}
void loop()
{
}
void setup()
{
int i=0;
char c,filename[13];
Serial.begin(9600);
pinMode(RTS,INPUT);
pinMode(CTS,OUTPUT);
digitalWrite(CTS,LOW);
if(!card.init()||!Fat16::init(&card)) error();
do
{
while(!Serial.available());
filename[0]=Serial.read();
}
while(filename[0]==NULL);
do
{
while(!Serial.available());
filename[++i]=Serial.read();
}
while(filename[i]!='\r'&&i<12);
filename[i]=NULL;
while(Serial.read()!=0x1a);
while(digitalRead(RTS)&&!Serial.available());
digitalWrite(CTS,HIGH);
if(!digitalRead(RTS))
{
if(!file.open(filename,O_READ)) error();
while ((c=file.read())!=-1)
{
Serial.write(c);
while(digitalRead(RTS));
}
Serial.write(0x1a);
}
else if(Serial.available())
{
if(!file.open(filename, O_CREAT|O_WRITE|O_TRUNC)) error();
while ((c=Serial.read())!=0x1a)
{
file.write(c);
digitalWrite(CTS,LOW);
while(!Serial.available());
digitalWrite(CTS,HIGH);
}
}
file.close();
done();
}