2021-08-03 23:12:25 +02:00
|
|
|
/*
|
|
|
|
* © 2021, Neil McKechnie. All rights reserved.
|
|
|
|
*
|
|
|
|
* This file is part of DCC++EX API
|
|
|
|
*
|
|
|
|
* This 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.
|
|
|
|
*
|
|
|
|
* It 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 CommandStation. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "DCC.h"
|
|
|
|
#include "IODevice.h"
|
|
|
|
#include "DIAG.h"
|
2021-08-27 22:47:13 +02:00
|
|
|
#include "defines.h"
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2021-09-22 15:12:23 +02:00
|
|
|
#define PACKEDADDRESS(addr, subaddr) (((addr) << 2) + (subaddr))
|
|
|
|
#define ADDRESS(packedaddr) ((packedaddr) >> 2)
|
|
|
|
#define SUBADDRESS(packedaddr) ((packedaddr) % 4)
|
2021-08-03 23:12:25 +02:00
|
|
|
|
2022-06-14 16:15:42 +02:00
|
|
|
void DCCAccessoryDecoder::create(VPIN firstVpin, int nPins, int DCCAddress, int DCCSubaddress) {
|
|
|
|
if (checkNoOverlap(firstVpin,nPins)) new DCCAccessoryDecoder(firstVpin, nPins, DCCAddress, DCCSubaddress);
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2021-08-27 22:47:13 +02:00
|
|
|
// Constructors
|
2021-08-03 23:12:25 +02:00
|
|
|
DCCAccessoryDecoder::DCCAccessoryDecoder(VPIN vpin, int nPins, int DCCAddress, int DCCSubaddress) {
|
|
|
|
_firstVpin = vpin;
|
|
|
|
_nPins = nPins;
|
2021-09-22 15:12:23 +02:00
|
|
|
_packedAddress = PACKEDADDRESS(DCCAddress, DCCSubaddress);
|
2021-08-27 22:47:13 +02:00
|
|
|
addDevice(this);
|
2021-08-18 00:41:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCAccessoryDecoder::_begin() {
|
2021-08-27 16:44:26 +02:00
|
|
|
#if defined(DIAG_IO)
|
|
|
|
_display();
|
|
|
|
#endif
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
2021-08-27 22:47:13 +02:00
|
|
|
// Device-specific write function. State 1=closed, 0=thrown. Adjust for RCN-213 compliance
|
2021-08-03 23:12:25 +02:00
|
|
|
void DCCAccessoryDecoder::_write(VPIN id, int state) {
|
|
|
|
int packedAddress = _packedAddress + id - _firstVpin;
|
2021-11-25 23:10:03 +01:00
|
|
|
#if defined(HAL_ACCESSORY_COMMAND_REVERSE)
|
2021-08-27 22:47:13 +02:00
|
|
|
state = !state;
|
2021-11-25 23:10:03 +01:00
|
|
|
#ifdef DIAG_IO
|
|
|
|
DIAG(F("DCC Write Linear Address:%d State:%d (inverted)"), packedAddress, state);
|
|
|
|
#endif
|
|
|
|
#else
|
|
|
|
#ifdef DIAG_IO
|
|
|
|
DIAG(F("DCC Write Linear Address:%d State:%d"), packedAddress, state);
|
|
|
|
#endif
|
2021-08-27 22:47:13 +02:00
|
|
|
#endif
|
|
|
|
DCC::setAccessory(ADDRESS(packedAddress), SUBADDRESS(packedAddress), state);
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void DCCAccessoryDecoder::_display() {
|
|
|
|
int endAddress = _packedAddress + _nPins - 1;
|
2023-03-27 14:03:19 +02:00
|
|
|
DIAG(F("DCCAccessoryDecoder Configured on Vpins:%u-%u Addresses %d/%d-%d/%d)"), _firstVpin, _firstVpin+_nPins-1,
|
2021-08-27 22:47:13 +02:00
|
|
|
ADDRESS(_packedAddress), SUBADDRESS(_packedAddress), ADDRESS(endAddress), SUBADDRESS(endAddress));
|
2021-08-03 23:12:25 +02:00
|
|
|
}
|
|
|
|
|