/      日本語

Behavior

An advanced framework for implementing complex applications
In Behavior, defining a class in a specified manner allows it to be registered with the the_twelite class object. Once registered, the behavior is integrated into TWENET and operates accordingly. User code can describe the application’s behavior via this mechanism. Unlike loop-based implementations, it enables defining interrupt handlers and callback functions from TWENET. Although it requires more code, this approach is suitable for constructing more complex applications.

Class Definition (.hpp)

A behavior is defined using a class structure like the one below.

class MY_APP_CLASS: MWX_APPDEFS_CRTP(MY_APP_CLASS)
{
public:
    static const uint8_t TYPE_ID = 0x01;

    // load common definition for handlers
    #define __MWX_APP_CLASS_NAME MY_APP_CLASS
    #include "_mwx_cbs_hpphead.hpp"
    #undef __MWX_APP_CLASS_NAME

public:
    // constructor
    MY_APP_CLASS() {}

    void _setup() {}
    void _begin() {}

public:
    // TWENET callback handler (mandate)
    void loop() {}
    void on_sleep(uint32_t & val) {}
    void warmboot(uint32_t & val) {}
    void wakeup(uint32_t & val) {}

    void on_create(uint32_t& val) { _setup();  }
    void on_begin(uint32_t& val) { _begin(); }
    void on_message(uint32_t& val) { }

public:
    void network_event(mwx::packet_ev_nwk& pEvNwk) {}
    void receive(mwx::packet_rx& rx) {}
    void transmit_complete(mwx::packet_ev_tx& evTx) {}
};

In the example above, a behavior class named MY_APP_CLASS is defined. Several places require the name MY_APP_CLASS.

class MY_APP_CLASS: MWX_APPDEFS_CRTP(MY_APP_CLASS)

Defines the class name and the base (parent) class. MWX_APPDEFS_CRTP() is a macro.

    #define __MWX_APP_CLASS_NAME MY_APP_CLASS
    #include "_mwx_cbs_hpphead.hpp"
    #undef __MWX_APP_CLASS_NAME

Includes the necessary definitions via #include.

MY_APP_CLASS() {}

Defines the constructor.

Methods

loop()

The main loop function, serving the same role as the globally defined loop().

on_create()

on_create() is called when the object is created (via the use<>() method). The val parameter is reserved for future extensions.

on_begin()

on_begin() is called after setup() completes. The val parameter is reserved for future extensions.

on_sleep()

Called before entering sleep. The val parameter is reserved for future extensions.

warmboot()

Called at the initial stage of wakeup from sleep. The val parameter is reserved for future extensions.

At this point, peripherals are not yet initialized. You can check the cause of the sleep wakeup.

wakeup()

Called upon waking from sleep. The val parameter is reserved for future extensions.

receive()

void receive(mwx::packet_rx& rx)

Called when a packet is received, with the received packet information passed as rx.

transmit_complete()

void transmit_complete(mwx::packet_ev_tx& evTx)

Called when packet transmission is complete, with the transmission information passed as evTx. evTx.u8CbId is the ID used during transmission, and evTx.bStatus is a flag indicating transmission success (1) or failure (0).


PAL_AMB-behavior

A sample behavior using the Ambient Sensor PAL