-
Notifications
You must be signed in to change notification settings - Fork 18
/
IthoCC1101.h
179 lines (145 loc) · 7.11 KB
/
IthoCC1101.h
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
/*
* Author: Klusjesman, modified bij supersjimmie for Arduino/ESP8266
*/
#ifndef __ITHOCC1101_H__
#define __ITHOCC1101_H__
#include <stdio.h>
#include "CC1101.h"
#include "IthoPacket.h"
//pa table settings
const uint8_t ithoPaTableSend[8] = {0x6F, 0x26, 0x2E, 0x8C, 0x87, 0xCD, 0xC7, 0xC0};
const uint8_t ithoPaTableReceive[8] = {0x6F, 0x26, 0x2E, 0x7F, 0x8A, 0x84, 0xCA, 0xC4};
//rft message 1 commands
const uint8_t ithoMessage1HighCommandBytes[] = {1,84,213,85,50,203,52};
const uint8_t ithoMessage1MediumCommandBytes[] = {1,84,213,85,74,213,52};
const uint8_t ithoMessage1LowCommandBytes[] = {1,84,213,85,83,83,84};
const uint8_t ithoMessage1Timer1CommandBytes[] = {1,83,83,84,204,202,180};
const uint8_t ithoMessage1Timer2CommandBytes[] = {1,83,83,83,53,52,180};
const uint8_t ithoMessage1Timer3CommandBytes[] = {1,83,83,82,173,82,180};
const uint8_t ithoMessage1JoinCommandBytes[] = {0,170,171,85,84,202,180};
const uint8_t ithoMessage1LeaveCommandBytes[] = {0,170,173,85,83,43,84};
//duco message1 commands
const uint8_t ducoMessage1HighCommandBytes[] = {1,84,213,85,51,45,52};
const uint8_t ducoMessage1MediumCommandBytes[] = {1,84,213,85,75,51,52};
const uint8_t ducoMessage1LowCommandBytes[] = {1,84,213,85,82,181,84};
const uint8_t ducoMessage1StandByCommandBytes[] = {1,85,53,84,205,85,52};
const uint8_t ducoMessage1JoinCommandBytes[] = {0,170,171,85,85,44,180};
const uint8_t ducoMessage1LeaveCommandBytes[] = {0,170,173,85,82,205,84};
//message 2 commands
const uint8_t ithoMessage2PowerCommandBytes[] = {6,89,150,170,165,101,90,150,85,149,101,90,102,85,150};
const uint8_t ithoMessage2HighCommandBytes[] = {6,89,150,170,165,101,90,150,85,149,101,89,102,85,150};
const uint8_t ithoMessage2MediumCommandBytes[] = {6,89,150,170,165,101,90,150,85,149,101,90,150,85,150};
const uint8_t ithoMessage2LowCommandBytes[] = {6,89,150,170,165,101,90,150,85,149,101,89,150,85,150};
const uint8_t ithoMessage2StandByCommandBytes[] = {6,89,150,170,165,101,90,150,85,149,101,90,86,85,150};
const uint8_t ithoMessage2Timer1CommandBytes[] = {6,89,150,170,169,101,90,150,85,149,101,89,86,85,153}; //10 minutes full speed
const uint8_t ithoMessage2Timer2CommandBytes[] = {6,89,150,170,169,101,90,150,85,149,101,89,86,149,150}; //20 minutes full speed
const uint8_t ithoMessage2Timer3CommandBytes[] = {6,89,150,170,169,101,90,150,85,149,101,89,86,149,154}; //30 minutes full speed
const uint8_t ithoMessage2JoinCommandBytes[] = {9,90,170,90,165,165,89,106,85,149,102,89,150,170,165};
const uint8_t ithoMessage2LeaveCommandBytes[] = {9,90,170,90,165,165,89,166,85,149,105,90,170,90,165};
//message 2, counter
const uint8_t counterBytes24a[] = {1,2};
const uint8_t counterBytes24b[] = {84,148,100,164,88,152,104,168};
const uint8_t counterBytes25[] = {149,165,153,169,150,166,154,170};
const uint8_t counterBytes26[] = {96,160};
const uint8_t counterBytes41[] = {5, 10, 6, 9};
const uint8_t counterBytes42[] = {90, 170, 106, 154};
const uint8_t counterBytes43[] = {154, 90, 166, 102, 150, 86, 170, 106};
//join/leave
const uint8_t counterBytes64[] = {154,90,166,102,150,86,169,105,153,89,165,101,149,85,170,106};
const uint8_t counterBytes65[] = {150,169,153,165,149,170,154,166};
const uint8_t counterBytes66[] = {170,106};
//state machine
enum IthoReceiveStates
{
ExpectMessageStart,
ExpectNormalCommand,
ExpectJoinCommand,
ExpectLeaveCommand
};
class IthoCC1101 : protected CC1101
{
private:
//receive
IthoReceiveStates receiveState; //state machine receive
unsigned long lastMessage1Received; //used for timeout detection
CC1101Packet inMessage1; //temp storage message1
CC1101Packet inMessage2; //temp storage message2
IthoPacket inIthoPacket; //stores last received message data
//send
IthoPacket outIthoPacket; //stores state of "remote"
//settings
uint8_t sendTries; //number of times a command is send at one button press
//functions
public:
IthoCC1101(uint8_t counter = 0, uint8_t sendTries = 3); //set initial counter value
~IthoCC1101();
//init
void init() { CC1101::init(); } //init,reset CC1101
void initReceive();
uint8_t getLastCounter() { return outIthoPacket.counter; } //counter is increased before sending a command
void setSendTries(uint8_t sendTries) { this->sendTries = sendTries; }
//- deviceid should be a setting as well? random gen function? TODO
//receive
bool checkForNewPacket(); //check RX fifo for new data
IthoPacket getLastPacket() { return inIthoPacket; } //retrieve last received/parsed packet from remote
IthoCommand getLastCommand() { return inIthoPacket.command; } //retrieve last received/parsed command from remote
uint8_t getLastInCounter() { return inIthoPacket.counter; } //retrieve last received/parsed command from remote
uint8_t ReadRSSI();
bool checkID(const uint8_t *id);
String getLastIDstr(bool ashex=true);
String getLastMessage2str(bool ashex=true);
//send
void sendCommand(IthoCommand command);
protected:
private:
IthoCC1101( const IthoCC1101 &c);
IthoCC1101& operator=( const IthoCC1101 &c);
//init CC1101 for receiving
void initReceiveMessage1();
void initReceiveMessage2(IthoMessageType expectedMessageType);
//init CC1101 for sending
void initSendMessage1();
void initSendMessage2(IthoCommand command);
void finishTransfer();
//receive message validation
bool isValidMessageStart();
bool isValidMessageCommand();
bool isValidMessageJoin();
bool isValidMessageLeave();
//parse received message
void parseReceivedPackets();
void parseMessageStart();
void parseMessageCommand();
void parseMessageJoin();
void parseMessageLeave();
//send
void createMessageStart(IthoPacket *itho, CC1101Packet *packet);
void createMessageCommand(IthoPacket *itho, CC1101Packet *packet);
void createMessageJoin(IthoPacket *itho, CC1101Packet *packet);
void createMessageLeave(IthoPacket *itho, CC1101Packet *packet);
uint8_t* getMessage1CommandBytes(IthoCommand command);
uint8_t* getMessage2CommandBytes(IthoCommand command);
//counter bytes calculation (send)
uint8_t getMessage1Byte18(IthoCommand command);
IthoCommand getMessage1PreviousCommand(uint8_t byte18);
uint8_t calculateMessage2Byte24(uint8_t counter);
uint8_t calculateMessage2Byte25(uint8_t counter);
uint8_t calculateMessage2Byte26(uint8_t counter);
uint8_t calculateMessage2Byte41(uint8_t counter, IthoCommand command);
uint8_t calculateMessage2Byte42(uint8_t counter, IthoCommand command);
uint8_t calculateMessage2Byte43(uint8_t counter, IthoCommand command);
uint8_t calculateMessage2Byte49(uint8_t counter);
uint8_t calculateMessage2Byte50(uint8_t counter);
uint8_t calculateMessage2Byte51(uint8_t counter);
uint8_t calculateMessage2Byte64(uint8_t counter);
uint8_t calculateMessage2Byte65(uint8_t counter);
uint8_t calculateMessage2Byte66(uint8_t counter);
//counter calculation (receive)
uint8_t calculateMessageCounter(uint8_t byte24, uint8_t byte25, uint8_t byte26);
//general
uint8_t getCounterIndex(const uint8_t *arr, uint8_t length, uint8_t value);
//test
void testCreateMessage();
}; //IthoCC1101
extern volatile uint32_t data1[];
#endif //__ITHOCC1101_H__