Repository navigation
Expand file tree
/
Copy pathSerialCommand.cpp
More file actions
306 lines (261 loc) · 7.05 KB
/
Copy pathSerialCommand.cpp
File metadata and controls
306 lines (261 loc) · 7.05 KB
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#include "Arduino.h"
#include "SerialCommand.h"
char binaryString[9];
extern char *currentD64;
void byte_to_binary(int x) {
for (int i = 0; i < 8; i++) {
if ((x & (1<<i))> 0) {
binaryString[7-i] = '1';
} else {
binaryString[7-i] = '0';
}
}
binaryString[8]=0;
}
void debug_2(char *prefix,byte c)
{
Serial.print(prefix);
byte_to_binary(c);
Serial.println(binaryString);
}
SerialCommand::SerialCommand(IEC *baseIec)
{
iec = baseIec;
iec->setDataSink(this);
}
CommandFactory::CommandFactory(byte dvcNumber,char *fileName): SerialCommand(new IEC(dvcNumber))
{
deviceNumber=dvcNumber;
strncpy(d64File,fileName,sizeof(d64File));
Serial.println("SELECCIONADO DISCO");
Serial.println(fileName);
curDisk.mount(fileName);
reset();
}
void CommandFactory::reset()
{
status=STATUS_NONE;
currentChannel=0;
isActive==true;
secondaryAddress=0;
fileNameIndex=0;
askedToTalk=false;
}
void CommandFactory::run()
{
while(1)
this->iec->read();
}
bool CommandFactory::addByte(byte c,bool isLast,bool isAtn)
{
/*debug_2("BYTE:",c);*/
/*if(!isActive && !isAtn) {
debug_2("NO PARA MI",c);
return false;
}*/
char buf[3];
sprintf(buf,"%d",c);
Serial.println(buf);
switch(status) {
case STATUS_NONE:{
// Si el device es todo 1, es un UNTALK o un UNLISTEN.
// Hacemos reset y listo.
if((c & DEVICE_MASK)==DEVICE_MASK) {
//debug_2("Resetting:",c);
reset();
return false;
}
/* if((c & DEVICE_MASK)!=deviceNumber) {
isActive=false;
return;
}*/
if((c & COMMAND_MASK)==LISTEN_COMMAND)
{
Serial.println("RS");
status=STATUS_READ_SECONDARY_ADDRESS;
return true;
}
if((c & COMMAND_MASK)==TALK_COMMAND)
{
Serial.println("TAL");
askedToTalk=true;
status=STATUS_READ_SECONDARY_ADDRESS;
}
return false;
}break;
case STATUS_READ_SECONDARY_ADDRESS:
{
Serial.println("RSS");
byte cm=c & SECONDARY_ADDRESS_MASK;
if(cm == SECONDARY_ADDRESS_COMMAND) {
secondaryAddress=cm & SECONDARY_ADDRESS_ADDRESS_MASK;
if(askedToTalk)
{
doTalk(secondaryAddress);
reset();
return false;
}
}
if(cm == EX_SECONDARY_ADDRESS_COMMAND)
{
byte subcm=(c & EX_SECONDARY_ADDRESS_SUBCOMMAND_MASK);
switch(subcm) {
case EX_SECONDARY_ADDRESS_SUBCOMMAND_CLOSE:
{
// Ejecutar close
reset();
return false;
}break;
case EX_SECONDARY_ADDRESS_SUBCOMMAND_OPEN:
{
currentChannel=c & 0x0F;
fileNames[currentChannel][0]=0;
fileNameIndex=0;
status=STATUS_READ_FILENAME;
return true;
}break;
}
}
}break;
case STATUS_READ_FILENAME:
{
fileNames[currentChannel][fileNameIndex++]=c;
fileNames[currentChannel][fileNameIndex]=0;
if(isLast) {
/*for(int i=0;i<fileNameIndex;i++) {
debug_2("FILE:",fileNames[currentChannel][i]);
}*/
status=STATUS_NONE;
return false;
}
return true;
}break;
}
}
void CommandFactory::doTalk(byte secondaryAddress)
{
char *fileName;
if(secondaryAddress < 15)
{
fileName=fileNames[secondaryAddress];
}
Serial.println("REQUESTED::");
Serial.println(fileName);
iec->doTurnAround();
if(fileName[0]==PETSCII_DOLLAR_SIGN)
{
handleDirectoryCommand(secondaryAddress);
}
else
{
SerialProgram s1(iec,0,fileName);
s1.send();
}
iec->doTurnAround();
}
void CommandFactory::handleDirectoryCommand(int channel)
{
SerialListing sl(iec,&curDisk);
sl.sendListing();
/*byte sample[]={
1,8,14,8,10,0,153,32,34,72,79,76,65,34,0,0,0,75,0,140,1,64,0
};
int len=sizeof(sample);
int i;
for(i=0;i<len-1;i++)
{
iec->writeByte(sample[i],false);
}
iec->writeByte(sample[i],true);
*/
}
SerialListing::SerialListing(IEC *_iec,D64 *_disk)
{
this->iec=iec;
this->disk=_disk;
}
bool SerialListing::sendListing(){
// Se envia la posicion inicial
int currentLine=2049;
char c=(currentLine & 0xFF) ;
iec->writeByte(c,false);
c=(currentLine & 0xFF00) >> 8;
iec->writeByte(c,false);
SerialHelper h(iec);
Serial.println("EL DISCO ES...");
//Serial.println(disk);
this->disk->sendListing(h);
}
SerialHelper::SerialHelper(IEC *iec)
{
this->iec=iec;
currentLine=2049;
}
void SerialHelper::send(short lineNo,const char *text,bool isLast)
{
int len=strlen(text); // Sumamos 2 porque tambien vamos a enviar el numero de linea
int i=0;
while(text[i]!=0)
i++;
// se calcula cual sera la siguiente linea
char ba[100];
Serial.println(text);
sprintf(ba,"C:%d, len: %d",currentLine,i);
Serial.println(ba);
sprintf(ba,"len: %d",i);
Serial.println(ba);
int nLine=currentLine + 2 + 2 + 1 + strlen(text); // 2 son los bytes necesarios para enviar nLine, 2 para el numero de linea, y se suma 1 para insertar el 0 final.
sprintf(ba,"NextLine:%d %d %d",nLine,nLine & 0xFF,nLine & 0xFF00);
Serial.println(ba);
// Se envia nLine
char c=(nLine & 0xFF) ;
sprintf(ba,"Byte bajo::%du",c);
Serial.println(ba);
iec->writeByte(c,false);
c=(nLine & 0xFF00) >> 8;
iec->writeByte(c,false);
// Ahora se envia el numero de linea.
c=lineNo & 0xFF;
iec->writeByte(c,false);
c=(lineNo & 0xFF00) >> 8;
iec->writeByte(c,false);
for(int i=0;i<len;i++)
iec->writeByte(text[i],false);
iec->writeByte(0,false);
currentLine=nLine;
if(isLast)
{
iec->writeByte(0,false);
iec->writeByte(0,true);
}
}
SerialProgram::SerialProgram(IEC *_iec,char *_disk, char *_fileName)
{
iec=_iec;
disk=_disk;
fileName=_fileName;
}
bool SerialProgram::send()
{
D64 thd("BLAST003.D64");
if(!thd.fopen(fileName))
{
Serial.println("ERROR DE FOPEN!!");
}
bool doExit=false;
char c;
while(!doExit)
{
c=thd.getc();
if(thd.isEOF())
{
doExit=true;
iec->writeByte(c,true);
}
else
{
iec->writeByte(c,false);
}
}
thd.close();
}