BEHAVIOR
BEHAVIOR can be defined by defining the class in the specified way, so that the_twelite
class object. The registered BEHAVIOR will be embedded in TWENET, allowing the user code to describe the application's behavior. It is possible to define callback functions for interrupts and events from TWENET, which is not possible in a loop description. Although it requires more definitions than a loop description, it is suitable for describing more complex applications.
See sample BEHAVIOR PAL_AMB-behavior.
Class Definition (.hpp)
BEHAVIOR definition requires a class definition as shown below.
The above example defines a BEHAVIOR class with the name MY_APP_CLASS. MY_APP_CLASS must be described in several places.
Define the class name and the base (parent) class. MWX_APPDEFS_CRTP()
is a macro.
Here, the necessary definitions are imported by #include
.
Constructor definition.
methods
loop()
This is the main loop and has the same role as loop()
in the global definition.
on_create()
on_create()
is called at object creation time (use<>()
method). The val
is a parameter for future extension.
on_begin()
on_begin()is called after
setup()ends.
val` is a parameter for future extension.
on_sleep()
Called before sleep. val
is a parameter for future extension.
warmboot()
Called in the initial stage when returning from sleep. The val
is a parameter for future expansion.
At this point, the peripherals have not yet been initialized. The sleep wake-up factor can be checked.
wakeup()
Called when waking up from sleep. The val
is a parameter for future extension.
Sleep can also be called here.
receive()
When a packet is received, it is called with the received packet information as rx
.
transmit_complete()
The transmission information is called as evTx
when packet transmission is completed. The evTx.u8CbId
is the ID at the time of transmission and evTx.bStatus
is the flag indicating success (1
) or failure (0
) of the transmission.
Define handlers (.cpp)
BEHAVIOR handlers (interrupt, event, and state definitions) are defined in a cpp file. The file cannot be split and all handler definitions must be in one file.
Even in the case of BEHAVIORs that do not define handlers, be sure to create the following cpp file.
The required definitions of the MWX library (#include "_mwx_cbs_cpphead.hpp"
) must be included at the beginning and end of the cpp file.
At the beginning of the file, include the .hpp file of the BEHAVIOR definition as shown above. Specify the class name of the behavior in __MWX_APP_CLASS_NAME
. In the above, it is MY_APP_CLASS
.
At the end of the file, include the necessary definitions (#include "_mwx_cbs_cpptail.cpp"
).
The handler definition is written as shown in the following example. Types of definitions are described later. The definition of the handler to be used is described by using the macro for definition. Do not write handlers that are not used.
The MWX_????? _INT()
is the definition of an interrupt handler, and MWX_? _EVENT()
is the definition of an event handler, and MWX_STATE()
is the state definition of a state machine.
Interrupt and Event Handlers
Interrupt handlers are executed when a microcontroller interrupt occurs, interrupting the code currently being executed. For this reason, it is desirable to write as short a process as possible, and great care must also be taken with regard to manipulation of variables and the like.
The interrupt handler has a parameter uint8_t& handled
, and setting this value to true
will prevent subsequent event calls from being made.
If the interrupt handler exits with handled
set to false
, the event handler will be called in the application loop (normal code). The event handler has no handled
parameter. Since the event handler is normal code, it can perform relatively large processing. However, the event handler also incurs overhead, so it may not be able to handle the processing that is called at each frequent interrupt. In addition, since events are processed by the system's internal FIFO queue, events may be lost if they cannot be processed within a certain period of time.
The following is an explanation of macros for defining handler functions.
DIO
DIO (digital IO) interrupt event. N
specifies the target DIO number. The arg
is a definition for future extension.
To generate an interrupt, use pinMode()
, attachDioInt()
.
TICKTIMER
TickTimer interrupt and event. The arg
is a definition for future extension.
The handled
flag of the TickTimer must not be set to true
, otherwise TWENET will not work.
TIMER
Timer interrupt event. TheN
specifies the number of the target timer. The arg
is a definition for future extension.
In order to generate an interrupt, the Timer object is started with software interrupts enabled.
Other
Definition of other interrupts and events that are not defined standardly in the MWX library and require an understanding of the AHI Peripherals Manual.
Other interrupt events can be received by the following handler functions. These will not be available in the future when dedicated handlers are defined.
Peripheral (AHI) interrupt handler u32DeviceId
corresponds to arg
and u32ItemBitmap
corresponds to arg2
.
State Machine
A state machine (state machine) is a method of describing an application that receives messages and operates by transitioning its state in response to those messages.
The PAL_AMB-behavior sample describes the flow of the application's operation, including the start of sensor operation, acquisition of sensor values, wireless packet transmission to completion of transmission, and sleep transition. Please refer to it as an actual example.
The events to be received are as follows.
Event Name | Description |
---|---|
| It is called at system startup. Immediately after power-on, it is called with |
| It is called in a new state immediately after a state transition. Describes the process that is first executed when a transition is made to a certain state. |
| Called by TickTimer every 1ms |
| It is called every second. |
PEV_SetState()
The state is set to s
.
Exiting the state handler causes a transition to the next state, followed by a state handler being called with the E_EVENTS_NEW_STATE
event.
PEV_u32Elaspsed_ms()
Returns the elapsed time ≪ ms] since the state transition. It is used for purposes such as managing timeouts.
In the above example, a system reset is performed after 100 ms.
PEV_Process()
Called from outside the state handler. Execute the state handler with the event ev
parameter u32evarg
.
The transmission completion event is communicated to the state machine. In other words, call the state handler.
Do not call the state handler directly. It will cause problems such as E_EVENT_NEW_STATE
not being executed.
PEV_KeepStateOnWakeup()
Set just before sleep. After returning from sleep, the previous state is maintained. That is, the state handler is called with E_EVENT_START_UP
with sleep started.
PEV_is_coldboot()
Determine if the event is E_EVENT_START_UP
on wake-up.
PEV_is_warmboot()
Judges whether the event is E_EVENT_START_UP
when returning from sleep.
最終更新