Scratch
Template code.
setup()
Set the_twelite
to set the application ID APP_ID
, the radio channel CHANNEL
and the receive presence.
It also generates nwk
and specifies the child machine address 0xFE
. This address means that this is an unnamed child machine that has not been addressed by a child machine.
The addresses that can be set are 0x00: parent unit, 0x01~0xEF: child unit, 0xFE: unspecified child unit address range.
The address to be specified as the transmission destination is 0x00 for the parent machine, 0x01~0xEF for the specified parent machine address, 0xFE for any child machine address, and 0xFF for any address including the parent machine.
It also initializes the Buttons object. This is a chatter-suppressing algorithm by successive references, which determines HI or LOW of the target port (PIN_BTN only) if the value is the same 5 times in 10ms. pack_bits
(N1, N2, ...)
pack_bits(N1, N2, ...)' does 1UL<<N1 | 1UL << N2 | ...
to make a bitmap.
This is the procedure to start the_twelite
, it didn't appear in act0..4, but you should call it if you set up the_twelite
or register various behaviors.
begin()
Called only once after setup()
on startup. Only displays a message.
loop()
Input detection of buttons (switches).
The state is determined by continuous reference to Buttons. When the button state changes, it is output serially.
Input from serial.
If Serial.available()
is true
, the input from the serial port is stored. It reads one character from the serial and processes it according to the input character.
Enter t
for wireless transmission
t
for wireless transmissionWhen 't
' is input, sending is done. In this sample, the tx_busy flag is used to prevent continuous input.
Send requests are stored in a queue up to a certain number of packets, so it is possible to stack requests in the range of the queue (3 packets).
The following is an example of what happens when you don't check if(!tx_busy)
and type 'tttt
' continuously: the queue fills up at the fourth request and the request fails. The pkt object obtained by .prepare_tx_packet()
in Transmit()
will be false
.
The timing of the transmissions is randomised, so the completion of transmissions is not in the order in which they are requested.
Type s
to sleep.
s
to sleep.The system will sleep for 5000ms=5 seconds. After recovery, wakeup()
is executed.
wakeup()
First to be called on sleep wake up. Display of message only.
Transmit()
This is the minimum procedure for making a transmission request.
When you leave this function, the request has not yet been executed. You need to wait a while. In this example we have set a delay of 100-200ms for the start of the transmission, so the transmission will not start until 100ms at the earliest.
on_tx_comp()
Called on completion of a transmission; ev contains the transmission ID and completion status.
on_rx_packet()
When a packet is received, the sender's address information is displayed.
最終更新