voidsetup() { /*** SETUP section */ // the twelite main class the_twelite<< TWENET::appid(APP_ID) // set application ID (identify network group)<< TWENET::channel(CHANNEL) // set channel (pysical channel)<< TWENET::rx_when_idle(); // open receive circuit (if not set, it can't listen packts from others) // Register Networkauto&& nwk =the_twelite.network.use<NWK_SIMPLE>(); uid =random(1,5); // set uid by random() (1..4) nwk << NWK_SIMPLE::logical_id(uid); // set Logical ID. (0xFE means a child device with no ID) /*** BEGIN section */SerialParser.begin(PARSER::ASCII,128); // Initialize the serial parserthe_twelite.begin(); // start twelite! /*** INIT message */ Serial <<"--- WirelessUart (id="<<int(uid) <<") ---"<< mwx::crlf;}
while(Serial.available()) {if (SerialParser.parse(Serial.read())) { Serial <<".."<< SerialParser;constuint8_t* b =SerialParser.get_buf().begin();uint8_t addr =*b; ++b; // the first byte is destination address.transmit(addr, b,SerialParser.get_buf().end()); }}
if (the_twelite.receiver.available()) {auto&& rx =the_twelite.receiver.read(); // check the packet header.constuint8_t* p =rx.get_payload().begin();if (rx.get_length() >4&&!strncmp((constchar*)p, (constchar*)FOURCHARS,4)) { Serial <<format("..rx from %08x/%d",rx.get_addr_src_long(),rx.get_addr_src_lid()) << mwx::crlf; smplbuf_u8<128> buf; mwx::pack_bytes(buf ,uint8_t(rx.get_addr_src_lid()) // src addr (LID),make_pair(p+4,rx.get_payload().end()) ); // data body serparser_attach pout;pout.begin(PARSER::ASCII,buf.begin(),buf.size(),buf.size()); Serial << pout; }}