voidsetup() { /*** SETUP section */Buttons.setup(5); // init button manager with 5 history table.Analogue.setup(true,50); // setup analogue read (check every 50ms) // 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&& nwksmpl =the_twelite.network.use<NWK_SIMPLE>(); nwksmpl << NWK_SIMPLE::logical_id(0xFE) // set Logical ID. (0xFE means a child device with no ID)<< NWK_SIMPLE::repeat_max(3); // can repeat a packet up to three times. (being kind of a router) /*** BEGIN section */Buttons.begin(pack_bits(PIN_BTN),5,10); // check every 10ms, a change is reported by 5 consequent values.Analogue.begin(pack_bits(PIN_ANALOGUE::A1, PIN_ANALOGUE::VCC)); // _start continuous adc capture.the_twelite.begin(); // start twelite! /*** INIT message */ Serial <<"--- PingPong sample (press 't' to transmit) ---"<< mwx::crlf;}
大まかな流れは、各部の初期設定、各部の開始となっています。
the_twelite
このオブジェクトはTWENETを操作するための中核クラスオブジェクトです。
// 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)
Timer0は32Hzで動作しています。タイマー割り込みが発生直後の loop() で available になります。つまり、秒32回の処理をします。ここでは、ちょうど1秒になったところで送信処理をしています。
if (Timer0.available()) {staticuint8_t u16ct; u16ct++;if (u8DI_BM !=0xFF&&au16AI[0] !=0xFFFF) { // finished the first captureif ((u16ct %32) ==0) { // every 32ticks of Timer0transmit(); } }}
while (the_twelite.receiver.available()) {auto&& rx =the_twelite.receiver.read();uint8_tmsg[MSG_LEN];uint16_t adcval, volt;uint32_t timestamp; // expand packet payload (shall match with sent packet data structure, see pack_bytes())expand_bytes(rx.get_payload().begin(),rx.get_payload().end(), msg // 4bytes of msg // also can be -> std::make_pair(&msg[0], MSG_LEN), adcval // 2bytes, A1 value [0..1023], volt // 2bytes, Module VCC[mV], timestamp // 4bytes of timestamp ); // if PING packet, respond pong!if (!strncmp((constchar*)msg,"PING", MSG_LEN)) { // transmit a PONG packet with specifying the address.vTransmit(MSG_PONG,rx.get_psRxDataApp()->u32SrcAddr); } // display the packet Serial <<format("<RX ad=%x/lq=%d/ln=%d/sq=%d:" // note: up to 4 args!,rx.get_psRxDataApp()->u32SrcAddr,rx.get_lqi(),rx.get_length(),rx.get_psRxDataApp()->u8Seq )<<format(" %s AD=%d V=%d TS=%dms>" // note: up to 4 args!, msg, adcval, volt, timestamp )<< mwx::crlf<< mwx::flush; }
uint8_tmsg[MSG_LEN];uint16_t adcval, volt;uint32_t timestamp;// expand packet payload (shall match with sent packet data structure, see pack_bytes())expand_bytes(rx.get_payload().begin(),rx.get_payload().end(), msg // 4bytes of msg // also can be -> std::make_pair(&msg[0], MSG_LEN), adcval // 2bytes, A1 value [0..1023], volt // 2bytes, Module VCC[mV], timestamp // 4bytes of timestamp );
if (!strncmp((constchar*)msg,"PING", MSG_LEN)) {vTransmit(MSG_PONG,rx.get_psRxDataApp()->u32SrcAddr);}
続いて到着したパケット情報を表示します。
Serial <<format("<RX ad=%x/lq=%d/ln=%d/sq=%d:" // note: up to 4 args!,rx.get_psRxDataApp()->u32SrcAddr,rx.get_lqi(),rx.get_length(),rx.get_psRxDataApp()->u8Seq )<<format(" %s AD=%d V=%d TS=%dms>" // note: up to 4 args!, msg, adcval, volt, timestamp )<< mwx::crlf<< mwx::flush;
voidvTransmit(constchar* msg,uint32_t addr) { Serial <<"vTransmit()"<< mwx::crlf;if (auto&& pkt =the_twelite.network.use<NWK_SIMPLE>().prepare_tx_packet()) { // set tx packet behavior pkt <<tx_addr(addr) // 0..0xFF (LID 0:parent, FE:child w/ no id, FF:LID broad cast), 0x8XXXXXXX (long address)<<tx_retry(0x3) // set retry (0x3 send four times in total) << tx_packet_delay(100,200,20); // send packet w/ delay (send first packet with randomized delay from 100 to 200ms, repeat every 20ms)
// prepare packet payloadpack_bytes(pkt.get_payload() // set payload data objects.,make_pair(msg, MSG_LEN) // string should be paired with length explicitly. , uint16_t(analogRead(PIN_ANALOGUE::A1)) // possible numerical values types are uint8_t, uint16_t, uint32_t. (do not put other types)
, uint16_t(analogRead_mv(PIN_ANALOGUE::VCC)) // A1 and VCC values (note: alalog read is valid after the first (Analogue.available() == true).)
,uint32_t(millis()) // put timestamp here. ); // do transmit pkt.transmit(); }}
ネットワークオブジェクトとパケットオブジェクトの取得
if (auto&& pkt =the_twelite.network.use<NWK_SIMPLE>().prepare_tx_packet()) {
pkt <<tx_addr(addr) // 0..0xFF (LID 0:parent, FE:child w/ no id, FF:LID broad cast), 0x8XXXXXXX (long address)<<tx_retry(0x3) // set retry (0x3 send four times in total) << tx_packet_delay(100,200,20); // send packet w/ delay (send first packet with randomized delay from 100 to 200ms, repeat every 20ms)
// prepare packet payloadpack_bytes(pkt.get_payload() // set payload data objects.,make_pair(msg, MSG_LEN) // string should be paired with length explicitly. , uint16_t(analogRead(PIN_ANALOGUE::A1)) // possible numerical values types are uint8_t, uint16_t, uint32_t. (do not put other types)
, uint16_t(analogRead_mv(PIN_ANALOGUE::VCC)) // A1 and VCC values (note: alalog read is valid after the first (Analogue.available() == true).)
,uint32_t(millis()) // put timestamp here.);