-
Notifications
You must be signed in to change notification settings - Fork 0
/
LocoNetKS.cpp
94 lines (80 loc) · 2.57 KB
/
LocoNetKS.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
/*
||
|| @file LocoNetKS.cpp
|| @version 1.1
|| @author Michael Zimmermann
|| @contact [email protected]
||
|| @description
|| | additional functions for class 'LocoNet'
|| #
||
|| @license
|| | Copyright (c) 2021 Michael Zimmermann <http://www.kruemelsoft.privat.t-online.de>
|| | All rights reserved.
|| |
|| | This program 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.
|| |
|| | This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
|| #
||
*/
#include <LocoNet.h>
#include "LocoNetKS.h"
#if ARDUINO >= 100
#include "Arduino.h"
#else
#include "WProgram.h"
#endif
//=== LocoNetClassKS ===
LN_STATUS LocoNetClassKS::send(uint8_t OpCode)
{
lnMsg SendPacket;
SendPacket.data[0] = OpCode;
return LocoNet.send(&SendPacket);
}
LN_STATUS LocoNetClassKS::sendSwitchState(uint16_t ui16_swAdr, boolean b_swOn, boolean b_swDirection, uint8_t ui8_OPC)
{
if (!ui16_swAdr || (ui16_swAdr > 2048) || (ui8_OPC < OPC_SW_REQ) || (ui8_OPC > OPC_INPUT_REP))
return LN_UNKNOWN_ERROR;
switch (ui8_OPC)
{
case OPC_SW_REQ: // B0
return LocoNet.requestSwitch(ui16_swAdr, b_swOn, b_swDirection);
case OPC_SW_REP: // B1
return sendSwitchReport(ui16_swAdr, b_swOn, b_swDirection);
case OPC_INPUT_REP: // B2
return sendReportSensor(ui16_swAdr, b_swOn);
}
return LN_UNKNOWN_ERROR;
}
LN_STATUS LocoNetClassKS::sendSwitchReport(uint16_t Address, uint8_t Output, uint8_t Direction)
{ // B1
uint8_t AddrH = (--Address >> 7) & 0x0F;
uint8_t AddrL = Address & 0x7F;
if (Output)
AddrH |= OPC_SW_REQ_OUT;
if (Direction)
AddrH |= OPC_SW_REQ_DIR;
return LocoNet.send(OPC_SW_REP, AddrL, AddrH);
}
LN_STATUS LocoNetClassKS::sendReportSensor(uint16_t Address, uint8_t State)
{ // B2
byte AddrH = ((--Address >> 8) & 0x0F);
byte AddrL = (Address >> 1) & 0x7F;
if (Address % 2)
AddrH |= OPC_INPUT_REP_SW;
if (State)
AddrH |= OPC_INPUT_REP_HI;
return LocoNet.send(OPC_INPUT_REP, AddrL, AddrH);
}
//=== end LocoNetClassKS ===