📄 secdedradiobytesignal.nc
字号:
{ // found preamble // clear the following buffer for matching start symbol startSymBits = 24; primary[1] = 0; primary[2] = 0; secondary[1] = 0; secondary[2] = 0; last_bit = 1; // set to use first group of samples // Start to match start symbol state = MATCH_START_SYMBOL; } break; case MATCH_START_SYMBOL: startSymBits--; if (startSymBits == 0) { // failed to detect start symbol, go back to detect preamble state = IDLE_STATE; return SUCCESS; } // put new data into two groups to match start symbol if (last_bit) { // just put into second group, now for first last_bit = 0; primary[2] >>= 1; primary[2] &= 0x7f; // clear the highest bit //if lowest bit of first is one, store it in second if (primary[1] & 0x1) primary[2] |= 0x80; primary[1] = data & 0x1; // start symbol is 9 bits if (primary[1] == 0x1 && primary[2] == 0x35 ) { // 1st group matches, read one more bit for 2nd group state = ADJUST_SAMPLE_POS; secondary[2] >>= 1; secondary[2] &= 0x7f; if (secondary[1] & 0x1) secondary[2] |= 0x80; } } else { // just put into first group, now for second last_bit = 1; secondary[2] >>= 1; secondary[2] &= 0x7f; // clear the highest bit //if lowest bit of first is one, store it in second if (secondary[1] & 0x1) secondary[2] |= 0x80; secondary[1] = data & 0x1; // start symbol is 9 bits if (secondary[1] == 0x1 && secondary[2] == 0x35) { // 2nd group matches, read one more bit for 1st group state = ADJUST_SAMPLE_POS; primary[2] >>= 1; primary[2] &= 0x7f; if (primary[1] & 0x1) primary[2] |= 0x80; } } break; case ADJUST_SAMPLE_POS: // start symbol already detected // use this additional bit for better sampling alignment if (last_bit) { if ((data & 0x1) && primary[2] == (char)0x35 ) // both groups match start symbol call Radio.setBitRate(1); // 1.5x bit rate } else { if ((data & 0x1) && secondary[2] == (char)0x35) // both groups match start symbol call Radio.setBitRate(1); // 1.5x bit rate } state = BIT_WAITING_STATE; // waiting for first bit break; case BIT_WAITING_STATE: //just read first bit. //set bit rate to do one time sampling call Radio.setBitRate(2); state = CLOCKING_STATE; count = 1; //store the first incoming bit if (data) { primary[1] = 0x80; if (!sampled){ // Sample the baseband for signal strength if it is a one call StrengthADC.getData(); sampled = TRUE; } } else primary[1] = 0; break; case CLOCKING_STATE: //clock in the rest of the incoming bits count++; primary[1] >>= 1; primary[1] &= 0x7f; if (data) { primary[1] |= 0x80; if (!sampled) { // Sample the baseband for signal strength if it is a one call StrengthADC.getData(); sampled = TRUE; } } if (count == 8) secondary[2] = primary[1]; else if (count == 16) { count++; //store the encoded data into a buffer. secondary[1] = primary[1]; state = HAS_READ_STATE; } break; case HAS_READ_STATE: secondary[0] = data; state = DECODE_READY_STATE; break; case DECODE_READY_STATE: //throw away the higest bit. state = BIT_WAITING_STATE; //scheduled the decode task to decode the encoded byte just receive post radioDecodeThread(); dbg(DBG_ENCODE, "entire byte received: %x, %x\n", secondary[1], secondary[2]); break; case IDLE_WAITING_STATE: // MAC CSMA: waiting for channle to be idle. if (data) { //if we just read activity, then reset the waiting counter. waiting = 0; call RadioControl.power(2); // Turn radio off call Radio.setBitRate(2); // Set rate to TX rate during radio off // Pick a random number and use the lowest 11 bits as the random window for backoff waiting = call Random.rand() & 0x3ff; state = BACKOFF_STATE; // Goes to Backoff/Delay } else { //if we've not heard anything for more than 7 samples then //assume channel is clean if (waiting++ > 6) { waiting = 0; //schedule task to start transfer, set TX_mode, and set bit //rate on the radio call Radio.txMode(); call Radio.setBitRate(2); //go to the transmit state. state = START_STATE; post radioEncodeThread(); } } break; case BACKOFF_STATE: // This is the delay during backoff delay++; if (delay >= waiting) { // Set the radio to listening again at 2x sampling rate call RadioControl.power(1); call Radio.rxMode(); call Radio.setBitRate(0); delay = 0; waiting = 0; state = IDLE_WAITING_STATE; // Goes back to CSMA listening } break; } return SUCCESS; } /* This function encode the data using SEC_DED encoding */ void encodeData() { char ret_high = 0; char ret_low = 0; char ret_mid = 0; char val = secondary[0]; if ((val & 0x1) != 0) { ret_high ^=0; ret_mid ^=0x0; ret_low ^=0x77; } if ((val & 0x2) != 0) { ret_high ^=0; ret_mid ^=0x1; ret_low ^=0x34; } if ((val & 0x4) != 0) { ret_high ^=0; ret_mid ^=0x2; ret_low ^=0x32; } if ((val & 0x8) != 0) { ret_high ^=0; ret_mid ^=0x8; ret_low ^=0x31; } if ((val & 0x10) != 0) { ret_high ^=0; ret_mid ^=0x10; ret_low ^=0x26; } if ((val & 0x20) != 0) { ret_high ^=0; ret_mid ^=0x60; ret_low ^=0x25; } if ((val & 0x40) != 0) { ret_high ^=0; ret_mid ^=0x80; ret_low ^=0x13; } if ((val & 0x80) != 0) { ret_high ^=0x1; ret_mid ^=0; ret_low ^=0x7; } if ((ret_low & 0xc) == 0) ret_low |= 0x8; if ((ret_low & 0x40) == 0 && (ret_mid & 0x1) == 0) ret_low |= 0x80; if ((ret_mid & 0xa) == 0) ret_mid |= 0x4; if ((ret_mid & 0x50) == 0) ret_mid |= 0x20; if ((ret_high & 0x1) == 0) ret_high |= 0x2; secondary[0] = ret_high; secondary[1] = ret_mid; secondary[2] = ret_low; } /* This function decodes SEC_DED encoded data */ uint8_t decodeData() { //strip the data char ret_high = 0; char ret_low = 0; char val, val2, output; ret_high = (char)((secondary[0] << 4) & 0x10); ret_high |= (char)((secondary[1] >> 4) & 0xc); ret_high |= (char)((secondary[1] >> 3) & 0x3); ret_low = (char)((secondary[1] << 6) & 0xc0); ret_low |= (char)((secondary[2] >> 1) & 0x38); ret_low |= (char)(secondary[2] & 0x7); //check the data val = ret_low; val2 = ret_high; output = 0; if ((val & 0x1) != 0) output ^= 0x1; if ((val & 0x2) != 0) output ^= 0x2; if ((val & 0x4) != 0) output ^= 0x4; if ((val & 0x8) != 0) output ^= 0x8; if ((val & 0x10) != 0) output ^= 0x10; if ((val & 0x20) != 0) output ^= 0x1f; if ((val & 0x40) != 0) output ^= 0x1c; if ((val & 0x80) != 0) output ^= 0x1a; if ((val2 & 0x1) != 0) output ^= 0x19; if ((val2 & 0x2) != 0) output ^= 0x16; if ((val2 & 0x4) != 0) output ^= 0x15; if ((val2 & 0x8) != 0) output ^= 0xb; if ((val2 & 0x10) != 0) output ^= 0x7; if (output == 0){} else if (output == 0x1) { val ^= 0x1;} else if (output == 0x2) { val ^= 0x2; } else if (output == 0x4) { val ^= 0x4; } else if (output == 0x8) { val ^= 0x8; } else if (output == 0x10) { val ^= 0x10;} else if (output == 0x1f) { val ^= 0x20;} else if (output == 0x1c) { val ^= 0x40;} else if (output == 0x1a) { val ^= 0x80;} else if (output == 0x19) { val2 ^= 0x1; } else if (output == 0x16) { val2 ^= 0x2; } else if (output == 0x15) { val2 ^= 0x4; } else if (output == 0xb) { val2 ^= 0x8; } else if (output == 0x7) { val2 ^= 0x10;} //pull off the data bits output = (char)((val >> 5) & 0x7); output |= ((char)val2 << 3) & 0xf8; return output; } // This handles the signal strength data event result_t StrengthADC.dataReady(uint16_t data) { strength = data; return SUCCESS; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -