forked from Klusjesman/IthoEcoFanRFT
/
IthoEcoFanRFT.ino
204 lines (181 loc) · 5.5 KB
/
IthoEcoFanRFT.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
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
193
194
195
196
197
198
199
200
201
202
203
204
/*
Original Author: Klusjesman
Tested with STK500 + ATMega328P
GCC-AVR compiler
Modified by supersjimmie:
Code and libraries made compatible with Arduino and ESP8266
Tested with Arduino IDE v1.6.5 and 1.6.9
For ESP8266 tested with ESP8266 core for Arduino v 2.1.0 and 2.2.0 Stable
(See https://github.com/esp8266/Arduino/ )
*/
/*
CC11xx pins ESP pins Arduino pins Description
1 - VCC VCC VCC 3v3
2 - GND GND GND Ground
3 - MOSI 13=D7 Pin 11 Data input to CC11xx
4 - SCK 14=D5 Pin 13 Clock pin
5 - MISO/GDO1 12=D6 Pin 12 Data output from CC11xx / serial clock from CC11xx
6 - GDO2 04=D2 Pin 2? Serial data to CC11xx
7 - GDO0 ? Pin ? output as a symbol of receiving or sending data
8 - CSN 15=D8 Pin 10 Chip select / (SPI_SS)
*/
#include <SPI.h>
#include "IthoCC1101.h"
#include "IthoPacket.h"
#include <Ticker.h>
#define ITHO_IRQ_PIN D2
IthoCC1101 rf;
IthoPacket packet;
Ticker ITHOticker;
const uint8_t RFTid[] = {106, 170, 106, 101, 154, 107, 154, 86}; // my ID
bool ITHOhasPacket = false;
IthoCommand RFTcommand[3] = {IthoUnknown, IthoUnknown, IthoUnknown};
byte RFTRSSI[3] = {0, 0, 0};
byte RFTcommandpos = 0;
IthoCommand RFTlastCommand = IthoLow;
IthoCommand RFTstate = IthoUnknown;
IthoCommand savedRFTstate = IthoUnknown;
bool RFTidChk[3] = {false, false, false};
void setup(void) {
Serial.begin(115200);
delay(500);
Serial.println("setup begin");
rf.init();
Serial.println("setup done");
sendRegister();
Serial.println("join command sent");
pinMode(ITHO_IRQ_PIN, INPUT);
attachInterrupt(ITHO_IRQ_PIN, ITHOinterrupt, RISING);
}
void loop(void) {
// do whatever you want, check (and reset) the ITHOhasPacket flag whenever you like
if (ITHOhasPacket) {
showPacket();
}
}
void ITHOinterrupt() {
ITHOticker.once_ms(10, ITHOcheck);
}
void ITHOcheck() {
if (rf.checkForNewPacket()) {
IthoCommand cmd = rf.getLastCommand();
if (++RFTcommandpos > 2) RFTcommandpos = 0; // store information in next entry of ringbuffers
RFTcommand[RFTcommandpos] = cmd;
RFTRSSI[RFTcommandpos] = rf.ReadRSSI();
bool chk = rf.checkID(RFTid);
RFTidChk[RFTcommandpos] = chk;
if ((cmd != IthoUnknown) && chk) { // only act on good cmd and correct id.
ITHOhasPacket = true;
}
}
}
void showPacket() {
ITHOhasPacket = false;
uint8_t goodpos = findRFTlastCommand();
if (goodpos != -1) RFTlastCommand = RFTcommand[goodpos];
else RFTlastCommand = IthoUnknown;
//show data
Serial.print(F("RFT Current Pos: "));
Serial.print(RFTcommandpos);
Serial.print(F(", Good Pos: "));
Serial.println(goodpos);
Serial.print(F("Stored 3 commands: "));
Serial.print(RFTcommand[0]);
Serial.print(F(" "));
Serial.print(RFTcommand[1]);
Serial.print(F(" "));
Serial.print(RFTcommand[2]);
Serial.print(F(" / Stored 3 RSSI's: "));
Serial.print(RFTRSSI[0]);
Serial.print(F(" "));
Serial.print(RFTRSSI[1]);
Serial.print(F(" "));
Serial.print(RFTRSSI[2]);
Serial.print(F(" / Stored 3 ID checks: "));
Serial.print(RFTidChk[0]);
Serial.print(F(" "));
Serial.print(RFTidChk[1]);
Serial.print(F(" "));
Serial.print(RFTidChk[2]);
Serial.print(F(" / Last ID: "));
Serial.print(rf.getLastIDstr());
Serial.print(F(" / Command = "));
//show command
switch (RFTlastCommand) {
case IthoUnknown:
Serial.print("unknown\n");
break;
case IthoLow:
Serial.print("low\n");
break;
case IthoMedium:
Serial.print("medium\n");
break;
case IthoHigh:
Serial.print("high\n");
break;
case IthoFull:
Serial.print("full\n");
break;
case IthoTimer1:
Serial.print("timer1\n");
break;
case IthoTimer2:
Serial.print("timer2\n");
break;
case IthoTimer3:
Serial.print("timer3\n");
break;
case IthoJoin:
Serial.print("join\n");
break;
case IthoLeave:
Serial.print("leave\n");
break;
}
}
uint8_t findRFTlastCommand() {
if (RFTcommand[RFTcommandpos] != IthoUnknown) return RFTcommandpos;
if ((RFTcommandpos == 0) && (RFTcommand[2] != IthoUnknown)) return 2;
if ((RFTcommandpos == 0) && (RFTcommand[1] != IthoUnknown)) return 1;
if ((RFTcommandpos == 1) && (RFTcommand[0] != IthoUnknown)) return 0;
if ((RFTcommandpos == 1) && (RFTcommand[2] != IthoUnknown)) return 2;
if ((RFTcommandpos == 2) && (RFTcommand[1] != IthoUnknown)) return 1;
if ((RFTcommandpos == 2) && (RFTcommand[0] != IthoUnknown)) return 0;
return -1;
}
void sendRegister() {
Serial.println("sending join...");
rf.sendCommand(IthoJoin);
Serial.println("sending join done.");
}
void sendStandbySpeed() {
Serial.println("sending standby...");
rf.sendCommand(IthoStandby);
Serial.println("sending standby done.");
}
void sendLowSpeed() {
Serial.println("sending low...");
rf.sendCommand(IthoLow);
Serial.println("sending low done.");
}
void sendMediumSpeed() {
Serial.println("sending medium...");
rf.sendCommand(IthoMedium);
Serial.println("sending medium done.");
}
void sendHighSpeed() {
Serial.println("sending high...");
rf.sendCommand(IthoHigh);
Serial.println("sending high done.");
}
void sendFullSpeed() {
Serial.println("sending FullSpeed...");
rf.sendCommand(IthoFull);
Serial.println("sending FullSpeed done.");
}
void sendTimer() {
Serial.println("sending timer...");
rf.sendCommand(IthoTimer1);
Serial.println("sending timer done.");
}