voidsetup(){ /*** SETUP section */ /// init vars or objectsstep.setup(); // initialize state machine /// load board and settings objectsauto&& brd =the_twelite.board.use<ARIA>(); // load board supportauto&& set =the_twelite.settings.use<STG_STD>(); // load save/load settings(interactive mode) supportauto&& nwk =the_twelite.network.use<NWK_SIMPLE>(); // load network support /// configure settings // configure settings set << SETTINGS::appname("ARIA"); set << SETTINGS::appid_default(DEFAULT_APP_ID); // set default appID set << SETTINGS::ch_default(DEFAULT_CHANNEL); // set default channelset.hide_items(E_STGSTD_SETID::OPT_DWORD2, E_STGSTD_SETID::OPT_DWORD3, E_STGSTD_SETID::OPT_DWORD4, E_STGSTD_SETID::ENC_KEY_STRING, E_STGSTD_SETID::ENC_MODE); // if SET=LOW is detected, start with intaractive mode.if (digitalRead(brd.PIN_SET) == PIN_STATE::LOW) { set << SETTINGS::open_at_start();step.next(STATE::INTERACTIVE);return; } // load valuesset.reload(); // load from EEPROM. OPT_BITS =set.u32opt1(); // this value is not used in this example. LID =set.u8devid(); // set logical ID /// configure system basics the_twelite << set; // apply settings (from interactive mode) /// configure network nwk << set; // apply settings (from interactive mode) nwk << NWK_SIMPLE::logical_id(LID); // set LID again (LID can also be configured by DIP-SW.) /// configure hardware // LED setup (use periph_led_timer, which will re-start on wakeup() automatically)brd.set_led(LED_TIMER::BLINK,10); // blink (on 10ms/ off 10ms) // let the TWELITE begin!the_twelite.begin(); /*** INIT message */ Serial <<"--- ARIA:"<< FOURCHARS <<" ---"<< mwx::crlf; Serial <<format("-- app:x%08x/ch:%d/lid:%d",the_twelite.get_appid(),the_twelite.get_channel(),nwk.get_config().u8Lid )<< mwx::crlf; Serial <<format("-- pw:%d/retry:%d/opt:x%08x",the_twelite.get_tx_power(),nwk.get_config().u8RetryDefault, OPT_BITS )<< mwx::crlf;}
// configure settingsset << SETTINGS::appname("ARIA");set << SETTINGS::appid_default(DEFAULT_APP_ID); // set default appIDset << SETTINGS::ch_default(DEFAULT_CHANNEL); // set default channelset.hide_items(E_STGSTD_SETID::OPT_DWORD2, E_STGSTD_SETID::OPT_DWORD3, E_STGSTD_SETID::OPT_DWORD4, E_STGSTD_SETID::ENC_KEY_STRING, E_STGSTD_SETID::ENC_MODE);// if SET=LOW is detected, start with intaractive mode.if (digitalRead(brd.PIN_SET) == PIN_STATE::LOW) { set << SETTINGS::open_at_start();step.next(STATE::INTERACTIVE);return;}// load valuesset.reload(); // load from EEPROM.OPT_BITS =set.u32opt1(); // this value is not used in this example.LID =set.u8devid(); // set logical ID
pack_bytes(pkt.get_payload() // set payload data objects.,make_pair(FOURCHARS,4) // just to see packet identification, you can design in any.,uint8_t(sensor.b_north),uint8_t(sensor.b_south),uint16_t(sensor.i16temp),uint16_t(sensor.i16humid));
// do transmitMWX_APIRET ret =pkt.transmit();if (ret) {step.clear_flag(); // waiting for flag is set.step.set_timeout(100); // set timeoutstep.next(STATE::TX_WAIT_COMP);}
case STATE::TX_WAIT_COMP:
ここではタイムアウトの判定、送信完了イベントの判定を行います。
if (step.is_timeout()) { // maybe fatal error.the_twelite.reset_system();}if (step.is_flag_ready()) { // when tx is performed Serial <<"..transmit complete."<< mwx::crlf;Serial.flush();step.next(STATE::GO_SLEEP);}
voidsleepNow() {step.on_sleep(false); // reset state machine. // randomize sleep duration.uint32_t u32ct =1750+random(0,500); // set an interrupt for MAGnet sensor.pinMode(ARIA::PIN_SNS_OUT1, PIN_MODE::WAKE_FALLING);pinMode(ARIA::PIN_SNS_OUT2, PIN_MODE::WAKE_FALLING); // output message Serial <<"..sleeping "<<int(u32ct) <<"ms."<< mwx::crlf;Serial.flush(); // wait until all message printed. // do sleep.the_twelite.sleep(u32ct);}
voidwakeup() { Serial << mwx::crlf<<"--- ARIA:"<< FOURCHARS <<" wake up ";if (the_twelite.is_wokeup_by_wktimer()) { Serial <<"(WakeTimer) ---"; } elseif (the_twelite.is_wokeup_by_dio(ARIA::PIN_SNS_NORTH)) { Serial <<"(MAGnet INT [N]) ---"; } elseif (the_twelite.is_wokeup_by_dio(ARIA::PIN_SNS_SOUTH)) { Serial <<"(MAGnet INT [S]) ---"; } else { Serial <<"(unknown source) ---"; } Serial << mwx::crlf<<"..start sensor capture again."<< mwx::crlf;}