-
Notifications
You must be signed in to change notification settings - Fork 0
/
X10.ino
119 lines (116 loc) · 3.68 KB
/
X10.ino
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
#include "RoomExtender.h"
// Processes and executes 3 byte serial and wireless messages
bool process3BMessage(const char type[], byte byte1, byte byte2, byte byte3)
{
bool x10exBufferError = 0;
char bmHouse;
byte bmUnit,
bmCommand,
bmExtCommand;
// Convert byte2 from hex to decimal unless command is request module state
if (byte1 != 'R') byte2 = charHexToDecimal(byte2);
// Convert byte3 from hex to decimal unless command is wipe module state
if (byte1 != 'R' || byte2 != 'W') byte3 = charHexToDecimal(byte3);
// Check if standard message was received (byte1 = House, byte2 = Unit, byte3 = Command)
if (byte1 >= 'A' && byte1 <= 'P' && (byte2 <= 0xF || byte2 == '_') && (byte3 <= 0xF || byte3 == '_') && (byte2 != '_' || byte3 != '_'))
{
// Store first 3 bytes of message to make it possible to process 9 byte serial messages
bmHouse = byte1;
bmUnit = byte2 == '_' ? 0 : byte2 + 1;
bmCommand = byte3 == '_' ? CMD_STATUS_REQUEST : byte3;
bmExtCommand = 0;
// Send standard message if command type is not extended
if (bmCommand != CMD_EXTENDED_CODE && bmCommand != CMD_EXTENDED_DATA)
{
printX10Message(type, bmHouse, bmUnit, byte3, 0, 0, 8 * Serial.available());
// Check if command is handled by scenario; if not continue
#if ENABLE_X10_SCENARIO
if (!handleUnitScenario(bmHouse, bmUnit, bmCommand, false, true))
#endif
{
x10exBufferError = x10ex.sendCmd(bmHouse, bmUnit, bmCommand, bmCommand == CMD_BRIGHT || bmCommand == CMD_DIM ? 2 : 1);
}
bmHouse = 0;
}
}
// Check if extended message was received (byte1 = Hex Separator, byte2 = Hex 1, byte3 = Hex 2)
else if (byte1 == 'X' && bmHouse)
{
byte data = byte2 * 16 + byte3;
// No extended command set, assume that we are receiving command
if (!bmExtCommand)
{
bmExtCommand = data;
}
// Extended command set, we must be receiving extended data
else
{
printX10Message(type, bmHouse, bmUnit, bmCommand, data, bmExtCommand, 8 * Serial.available());
x10exBufferError = x10ex.sendExt(bmHouse, bmUnit, bmCommand, data, bmExtCommand, 1);
bmHouse = 0;
}
}
// Check if scenario execute command was received (byte1 = Scenario Character, byte2 = Hex 1, byte3 = Hex 2)
#if ENABLE_X10_SCENARIO
else if (byte1 == 'S')
{
byte scenario = byte2 * 16 + byte3;
Serial.print(type);
Serial.print(F("S"));
if (scenario <= 0xF) { Serial.print(F("0")); }
Serial.println(scenario, HEX);
handleSdScenario(scenario);
}
#endif
// Check if request module state command was received (byte1 = Request State Character, byte2 = House, byte3 = Unit)
else if (byte1 == 'R' && ((byte2 >= 'A' && byte2 <= 'P') || byte2 == '*'))
{
Serial.print(type);
Serial.print(F("R"));
Serial.print(byte2);
// All modules
if (byte2 == '*')
{
Serial.println('*');
for (short i = 0; i < 256; i++)
{
sdPrintModuleState((i >> 4) + 0x41, i & 0xF);
}
}
else
{
if (byte3 <= 0xF)
{
Serial.println(byte3, HEX);
sdPrintModuleState(byte2, byte3);
}
// All units using specified house code
else
{
Serial.println('*');
for (byte i = 0; i < 16; i++)
{
sdPrintModuleState(byte2, i);
}
}
}
}
// Check if request wipe module state command was received (byte1 = Request State Character, byte2 = Wipe Character, byte3 = House)
else if (byte1 == 'R' && byte2 == 'W' && ((byte3 >= 'A' && byte3 <= 'P') || byte3 == '*'))
{
Serial.print(type);
Serial.print(F("RW"));
Serial.println((char)byte3);
x10ex.wipeModuleState(byte3);
Serial.print(F(MODULE_STATE_MSG));
Serial.print(byte3 >= 'A' && byte3 <= 'P' ? (char)byte3 : '*');
Serial.println(F("__"));
}
// Unknown command/data
else
{
Serial.print(type);
Serial.println(F(MSG_DATA_ERROR));
}
return x10exBufferError;
}