|
樓主 |
發表於 2019-3-12 14:56:05
|
顯示全部樓層
本文章最後由 jojoling 於 2019-3-12 03:54 PM 編輯
數據說明:
* 此為arduino 所抓取的一個完整傳輸信號,單位為 uSec
* 第1個 26820 指調變信號前的 low 信號 ,如果接連2個信號發射時,中間都有一個約25ms的無調變信號。
* 接下來每一行描述10個波形,表示正半週負半週共 20組。
* 最後一行少了最後一個負半週,因為這個負半週後會緊接 25ms的 low 信號,所以不會被檢測到。
* 前面 48個ticks 共 48*2 個約在275us 上下,我們會發現一開始的正半週為 336,第2個為 220 ,有可能是檢測ic 在warm up 過程,但它會其保持 約在 1.8Khz 的週期。
- 26820
- 336 220 336 216 336 216 332 228 324 224 328 232 316 236 316 240 308 248 308 244
- 304 252 304 244 308 252 296 260 296 252 296 268 284 264 296 260 288 264 284 268
- 284 272 284 272 284 264 284 272 280 276 280 272 280 276 276 272 280 276 276 280
- 276 272 284 272 280 272 280 272 284 272 284 268 284 272 280 276 276 276 276 276
- 280 272 280 276 280 276 276 280 276 276 272 284 272 276 280 272 556 556 548 556
- 552 556 548 280 276 560 548 552 272 280 276 276 276 284 548 556 548 560 552 552
- 276 276 556 556 548 556 272 284 272 284 272 276 276 280 272 284 272 280 544 564
- 268 284 544 564 544 560 268 288 268 284 548 560 268 288 540 568 264 288 268 284
- 544 560 544 568 540 568 264 284 264 292 264 284 548 564 544 560 268 292 260 288
- 268 288 264 288 268 284 540 576 260 292 264 280 268 288 268 288 264 288 264 288
- 264 292 264 284 540 572 264 284 268 284 272 288 264 288 268 288 264 284 268
複製代碼
解碼程式,此解碼程式是直接修改 rc-switch裏的,發現無法套用原來的,就直接簡單修改了它。
- bool RECEIVE_ATTR RCSwitch::receiveProtocol(unsigned int changeCount) {
- uint32_t code = 0;
- uint32_t received_code[4];
- /* For protocols that start low, the sync period looks like
- * _________
- * _____________| |XXXXXXXXXXXX|
- *
- * |--1st dur--|-2nd dur-|-Start data-|
- *
- * The 3rd saved duration starts the data.
- *
- * For protocols that start high, the sync period looks like
- *
- * ______________
- * | |____________|XXXXXXXXXXXXX|
- *
- * |-filtered out-|--1st dur--|--Start data--|
- *
- * The 2nd saved duration starts the data
- */
- unsigned int firstDataTiming = 1;//(pro.invertedSignal) ? (2) : (1);
- // must have 220 data record.
- if(changeCount != 220) return false;
- for (unsigned int i = firstDataTiming; i < changeCount - 1; i += 2) {
- if (diff(RCSwitch::timings[i], 275) < 75 &&
- diff(RCSwitch::timings[i + 1], 275) < 350) {
- // zero
- } else if (diff(RCSwitch::timings[i], 550) < 25 &&
- diff(RCSwitch::timings[i + 1], 550) < 350) {
- // one
- code |= 1;
- } else {
- // Failed
- return false;
- }
- if( (i+1)%64 == 0 && i !=1) {
- received_code[(i+1)/64-1] = code;
- code = 0;
- }
- code <<= 1;
- }
- code <<= 18;
- received_code[3] = code;
- //if (changeCount > 7) { // ignore very short transmissions: no device sends them, so this must be noise
- if(!RCSwitch::release) {
- RCSwitch::nReceivedValue[0] = received_code[0];
- RCSwitch::nReceivedValue[1] = received_code[1];
- RCSwitch::nReceivedValue[2] = received_code[2];
- RCSwitch::nReceivedValue[3] = received_code[3];
- RCSwitch::nReceivedBitlength = (changeCount) / 2;
- }
- if( received_code[0] == 0 &&
- (received_code[1] & 0xFFFFF000 ) == 0x0000F000 &&
- received_code[2] == 0x594e304 ) {
- uint16_t v1 = received_code[1] & 0xFFC;
- uint16_t v2 = received_code[3] >> 16;
- uint8_t key;
- v1 ^= v2;
- v1 &= 0xFFF;
- if(v1 == 0x674) {
- switch(v2) {
- case 0x200: key = KEY_ON; break;
- case 0x220: key = KEY_OFF; break;
- case 0x620: key = KEY_PAIR; break;
- case 0x400: key = KEY_UP; break;
- case 0x420: key = KEY_DOWN; break;
- case 0x000: key = KEY_1; break;
- case 0x040: key = KEY_2; break;
- case 0x020: key = KEY_3; break;
- case 0x700: key = KEY_4; break;
- case 0x740: key = KEY_5; break;
- case 0x720: key = KEY_6; break;
- case 0x300: key = KEY_7; break;
- case 0x340: key = KEY_8; break;
- case 0x320: key = KEY_9; break;
- default: break;
- }
- insert_key (key);
- }
- }
- return true;
- //}
- //return false;
- }
複製代碼
* 細週期 275us,容許誤差 75us ,因為我們之前有發現剛接收到的信號,正負半週的誤差會比較大。
- diff(RCSwitch::timings[i], 275) < 75
複製代碼
* 寬週期 575us,容許誤差 25us
- diff(RCSwitch::timings[i], 550) < 25
複製代碼
*下述因為我們前面有提到在負半週會有不正確的週期長度,所以我們上面在負半週檢查的2行,容許誤差改為 350us,或不檢查它也行。
- diff(RCSwitch::timings[i + 1], 275) < 350
- diff(RCSwitch::timings[i + 1], 550) < 350
複製代碼
DOUBLE_BUFF 是我另外定義的,原因為檢測信號太快,如果你需要保持檢測到的數據,需另外保存,不然會被下一個碼覆蓋。
這裏會發現,rc-switch檢測需要連續發送最少2個碼才會進入檢測,所以需要按比較久點。
如需要商用更改的話,需再加入一個timer去定時檢測觸發。
- void RECEIVE_ATTR RCSwitch::handleInterrupt() {
- static unsigned int changeCount = 0;
- static unsigned long lastTime = 0;
- static unsigned int repeatCount = 0;
- const long time = micros();
- const unsigned int duration = time - lastTime;
- if (duration > RCSwitch::nSeparationLimit) {
- // A long stretch without signal level change occurred. This could
- // be the gap between two transmission.
- if (diff(duration, RCSwitch::timings[0]) < 200) {
- // This long signal is close in length to the long signal which
- // started the previously recorded timings; this suggests that
- // it may indeed by a a gap between two transmissions (we assume
- // here that a sender will send the signal multiple times,
- // with roughly the same gap between them).
- repeatCount++;
- if (repeatCount == 2) {
- if (receiveProtocol(changeCount)) {
- #ifdef DOUBLE_BUFF
- memcpy(timings_x,RCSwitch::timings,RCSWITCH_MAX_CHANGES*sizeof(unsigned int));
- #endif
- }
- repeatCount = 0;
- }
- }
- changeCount = 0;
- }
- // detect overflow
- if (changeCount >= RCSWITCH_MAX_CHANGES) {
- changeCount = 0;
- repeatCount = 0;
- }
- RCSwitch::timings[changeCount++] = duration;
- lastTime = time;
- }
複製代碼
|
|