-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTeensyROMControl.cpp
More file actions
160 lines (136 loc) · 3.6 KB
/
TeensyROMControl.cpp
File metadata and controls
160 lines (136 loc) · 3.6 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
#include "TeensyROMControl.h"
// ***** Public *****
//TeensyROMControl::TeensyROMControl()
//{
// //Constructor
//
//}
bool TeensyROMControl::begin(Stream &port)
{
_port = &port;
#ifdef DbgChannel
DbgChannel.begin(115200);
#endif
DbgPrintf("Debug channel enabled\n");
return true;
}
bool TeensyROMControl::MenuReset()
{
SendToken(ResetC64Token);
DbgPrintf("*Sent Reset\n");
if (!FlushRx()) return false;
DbgPrintf("*Success\n");
return true;
}
bool TeensyROMControl::LaunchFile(enDriveSel DriveSel, const char* PathFileName)
{
SendToken(LaunchFileToken);
DbgPrintf("*Sent Launch: %d:%s\n", DriveSel, PathFileName);
if (!GetAckToken()) //will fail if in minimal build
{
MenuReset();
SendToken(LaunchFileToken);
DbgPrintf("*Re-Sent Launch\n");
if (!GetAckToken()) return false; //retry/abort if minimal
}
_port->write(DriveSel);
_port->write(PathFileName, strlen(PathFileName)+1); //include term
if (!GetAckToken()) return false;
DbgPrintf("*Success\n");
return true;
}
bool TeensyROMControl::PauseSIDToggle()
{
SendToken(PauseSIDToken);
DbgPrintf("*Sent Pause Toggle\n");
if (!GetAckToken()) return false;
DbgPrintf("*Success\n");
return true;
}
bool TeensyROMControl::VoiceMuteToggle(uint8_t VoiceNum)
{
if (VoiceNum<1 || VoiceNum>3)
{
DbgPrintf("Bad Voice Num: $d\n", VoiceNum);
return false;
}
VoiceMuteBits ^= (1<<(VoiceNum-1));
// bit 0= Voice 1 on=0, mute=1
// bit 1= Voice 2 on=0, mute=1
// bit 2= Voice 3 on=0, mute=1
SendToken(SIDVoiceMuteToken);
_port->write(VoiceMuteBits);
DbgPrintf("*Sent SID Voice Mute: %d\n", VoiceMuteBits);
if (!GetAckToken()) return false;
DbgPrintf("*Success\n");
return true;
}
bool TeensyROMControl::C64PauseToggle()
{
C64PauseState =! C64PauseState;
if(C64PauseState) SendToken(C64PauseOnToken);
else SendToken(C64PauseOffToken);
DbgPrintf("*Sent C64 Pause: %02x\n", (uint8_t)C64PauseState);
if (!GetAckToken()) return false;
DbgPrintf("*Success\n");
return true;
}
// ***** Private *****
void TeensyROMControl::SendToken(uint16_t TokToSend)
{
_port->write((TokToSend >> 8) & 0xFF);
_port->write(TokToSend & 0xFF);
}
bool TeensyROMControl::GetAckToken()
{
uint8_t LastVal = 0xFF;
while (SerialAvailabeTimeout())
{
uint8_t Val = _port->read();
DbgWrite(Val);
if (Val == ((AckToken >> 8) & 0xFF) && LastVal == (AckToken & 0xFF)) return true;
LastVal = Val;
}
DbgPrintf("No Ack\n");
return false;
}
bool TeensyROMControl::FlushRx()
{
if (!SerialAvailabeTimeout(1000)) //wait up to 1 sec for initial response
{
DbgPrintf("Nothing to flush\n");
return false;
}
do
{
uint8_t val = _port->read();
DbgWrite(val);
} while(SerialAvailabeTimeout());
return true;
}
bool TeensyROMControl::SerialAvailabeTimeout(uint32_t MaxTime)
{
uint32_t StartMillis = millis();
while(!_port->available() && millis() - StartMillis < MaxTime); // timeout loop
return(_port->available());
}
//bool TeensyROMControl::GetSerialString(char * cpBuf, int iMaxLength)
//{
// int iCharNum = 0;
//
// iMaxLength--; //always leave room for term char
// while (iCharNum < iMaxLength)
// {
// if (!SerialAvailabeTimeout())
// {
// //Serial.println("Serial Timeout!");
// return(false);
// }
// cpBuf[iCharNum] = _port->read();
// if (cpBuf[iCharNum] == '\r') break;
// iCharNum++;
// }
// cpBuf[iCharNum] = 0; //terminate (or force if at max len)
// return(iCharNum < iMaxLength);
//}
//