-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoconetMaster.cpp
193 lines (171 loc) · 6.41 KB
/
LoconetMaster.cpp
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
/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
Copyright (C) 2016 Martin Mueller mm@nohost.de
This file is part of DCCSpider
* *
DCCSpider is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
* *
DCCSpider is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
* *
You should have received a copy of the GNU General Public License
along with DCCSpider. If not, see <http://www.gnu.org/licenses/>.
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
#include "LoconetMaster.h"
LoconetMaster::LoconetMaster(byte slotCount)
{
_messageNo = 0;
_slotCount = slotCount;
_slotData = new SlotData[_slotCount];
_dispatchSlotNumber = 0;
//Test Code
_dispatchSlotNumber = 1;
_slotData[0].locoAddress = 1101;
_slotData[0].slotStatus = LOCO_IDLE | DEC_MODE_128;
}
void LoconetMaster::begin(byte rxPin, DCCBaseStation * dccBaseStation)
{
LocoNet.init(rxPin);
_dccBaseStation = dccBaseStation;
}
byte LoconetMaster::getLocoSlotNumber(int locoAddress)
{
byte slotIndex = 0;
for (slotIndex = 0; slotIndex < _slotCount; slotIndex++)
{
if ((_slotData[slotIndex].slotStatus & LOCOSTAT_MASK) && (_slotData[slotIndex].locoAddress == locoAddress)) {
return slotIndex + 1;
}
}
return 0;
}
byte LoconetMaster::createLocoSlot(int locoAddress) {
byte slotIndex = 0;
for (slotIndex = 0; slotIndex < _slotCount; slotIndex++)
{
if (!(_slotData[slotIndex].slotStatus & LOCOSTAT_MASK) ) {
_slotData[slotIndex].locoAddress = locoAddress;
_slotData[slotIndex].slotStatus = LOCO_IDLE | DEC_MODE_128;
Serial.print("Created slot for loco address ");
Serial.print(locoAddress, DEC);
Serial.print(". Slot number: ");
Serial.println(slotIndex + 1, DEC);
return slotIndex + 1;
}
}
return 0;
}
void LoconetMaster::processReceivedMessages()
{
lnMsg * receivedMessage;
receivedMessage = LocoNet.receive() ;
if ( receivedMessage ) {
uint8_t msgLen = getLnMsgSize(receivedMessage);
String logString("RX:");
logString = String(_messageNo) + ' ' + logString;
_messageNo++;
for (uint8_t x = 0; x < msgLen; x++)
{
uint8_t val = receivedMessage->data[x];
// Print a leading 0 if less than 16 to make 2 HEX digits
if (val < 16)
{
logString += '0';
}
logString += String(val, HEX);
}
Serial.println(logString);
lnMsg replyMessage;
if (receivedMessage->data[0] == OPC_LOCO_ADR )
{
int locoAddress = receivedMessage->data[1] << 8 | receivedMessage->data[2];
byte slotNumber = getLocoSlotNumber(locoAddress);
if (!slotNumber) {
slotNumber = createLocoSlot(locoAddress);
}
if (!slotNumber) {
LocoNet.sendLongAck(0);
}
else {
sendSlotReadData(slotNumber);
}
}
else if (receivedMessage->data[0] == OPC_MOVE_SLOTS )
{
byte slotNumber = receivedMessage->data[1];
if (!slotNumber) {
slotNumber = _dispatchSlotNumber;
_dispatchSlotNumber = 0;
}
if (!slotNumber) {
LocoNet.sendLongAck(0);
}
else
{
_slotData[slotNumber - 1].slotStatus |= LOCO_IN_USE;
sendSlotReadData(slotNumber);
}
}
else if (receivedMessage->data[0] == OPC_WR_SL_DATA )
{
byte slotNumber = receivedMessage->data[2];
_slotData[slotNumber - 1].deviceID = (receivedMessage->data[11] << 8) | receivedMessage->data[12];
LocoNet.send(OPC_LONG_ACK, 0x6F, 0x7F);
}
else if (receivedMessage->data[0] == OPC_LOCO_SPD )
{
byte slotNumber = receivedMessage->data[1];
_slotData[slotNumber - 1].locoSpeed = receivedMessage->data[2];
DCCBaseStation::DCCDirection locoDirection;
if (_slotData[slotNumber - 1].directionF0F4 & DIRF_DIR) {
locoDirection = DCCBaseStation::FORWARD;
}
else {
locoDirection = DCCBaseStation::REVERSE;
}
_dccBaseStation->setLocoSpeed(_slotData[slotNumber - 1].locoAddress, _slotData[slotNumber - 1].locoSpeed, locoDirection);
}
else if (receivedMessage->data[0] == OPC_LOCO_DIRF )
{
byte slotNumber = receivedMessage->data[1];
_slotData[slotNumber - 1].directionF0F4 = receivedMessage->data[2];
byte locoF0F4 = _slotData[slotNumber - 1].directionF0F4 & 0x1F; //Strip direction info
_dccBaseStation->setLocoF0F4Functions(_slotData[slotNumber - 1].locoAddress, locoF0F4);
}
else if (receivedMessage->data[0] == OPC_LOCO_SND )
{
byte slotNumber = receivedMessage->data[1];
_slotData[slotNumber - 1].slotSound = receivedMessage->data[2];
byte locoF5F8 = _slotData[slotNumber - 1].slotSound & 0x0F; //zero the bits that should be zero anyways for extra paranoia
_dccBaseStation->setLocoF5F8Functions(_slotData[slotNumber - 1].locoAddress, locoF5F8);
}
}
}
void LoconetMaster::sendSlotReadData(byte slotNumber)
{
if (slotNumber) {
lnMsg replyMessage;
replyMessage.data[ 0 ] = OPC_SL_RD_DATA;
replyMessage.data[ 1 ] = 0x0E;
replyMessage.data[ 2 ] = slotNumber;//SLOT#
replyMessage.data[ 3 ] = _slotData[slotNumber - 1].slotStatus;
replyMessage.data[ 4 ] = _slotData[slotNumber - 1].locoAddress & 0x00FF; //ADR Address lsb
replyMessage.data[ 5 ] = _slotData[slotNumber - 1].locoSpeed; //SPD Speed
replyMessage.data[ 6 ] = _slotData[slotNumber - 1].directionF0F4; //DIRF Direction F0-F4
replyMessage.data[ 7 ] = 4;//1; //TRK Track Status power is on
replyMessage.data[ 8 ] = 0; //SS2 Status 2
replyMessage.data[ 9 ] = _slotData[slotNumber - 1].locoAddress >> 8; //0x04; //ADR2 Address msb
replyMessage.data[ 10 ] = _slotData[slotNumber - 1].slotSound; //SND
replyMessage.data[ 11 ] = _slotData[slotNumber - 1].deviceID >> 8;
replyMessage.data[ 12 ] = _slotData[slotNumber - 1].deviceID & 0x00FF;
LocoNet.send( &replyMessage );
}
else
{
LocoNet.sendLongAck(0);
}
}