This is the multi-page printable view of this section. Click here to print...

Return to the regular view of this page

As of 2025-07-24

PAL_MAG

Sample using magnetic sensor pal
    Using Open-Close Sensor Pal OPEN-CLOSE SENSE PAL, sensor values are acquired.

    Function of the Act

    • Using the Open-Close Sensor Pal OPEN-CLOSE SENSE PAL, it wakes up by interrupt when the magnetic sensor is detected and transmits wirelessly.
    • Uses sleep function for operation with coin battery.

    How to Use the Act

    Required TWELITE

    RoleExample
    Parent Device

    MONOSTICK BLUE or RED

    Operate the act Parent_MONOSTICK.

    Child DeviceBLUE PAL or RED PAL + Open-Close Sensor Pal OPEN-CLOSE SENSE PAL

    Explanation of the Act

    Include

    ##include <TWELITE>
    ##include <NWK_SIMPLE>
    ##include <PAL_MAG>

    Include the board behavior <PAL_MAG> of the Open-Close Sensor Pal.

    setup()

    void setup() {
    	/*** SETUP section */
    	// use PAL_AMB board support.
    	auto&& brd = the_twelite.board.use<PAL_MAG>();
    	// now it can read DIP sw status.
    	u8ID = (brd.get_DIPSW_BM() & 0x07) + 1;
    	if (u8ID == 0) u8ID = 0xFE; // 0 is to 0xFE
    
    	// 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)
    
    	// the twelite main object.
    	the_twelite
    		<< TWENET::appid(APP_ID)     // set application ID (identify network group)
    		<< TWENET::channel(CHANNEL); // set channel (pysical channel)
    
    	// Register Network
    	auto&& nwk = the_twelite.network.use<NWK_SIMPLE>();
    	nwk << NWK_SIMPLE::logical_id(u8ID); // set Logical ID. (0xFE means a child device with no ID)
    
    	/*** BEGIN section */
    	the_twelite.begin(); // start twelite!
    
    	/*** INIT message */
    	Serial << "--- PAL_MAG:" << FOURCHARS << " ---" << mwx::crlf;
    }

    First, the board behavior <PAL_MAG> is registered. During board behavior initialization, sensors and DIO are initialized. It is common to first check the status of DIP SW on the board and then perform network settings and other processing.

    auto&& brd = the_twelite.board.use<PAL_MAG>();
    
    u8ID = (brd.get_DIPSW_BM() & 0x07) + 1;
    if (u8ID == 0) u8ID = 0xFE; // 0 is to 0xFE
    

    Here, three bits of the 4-bit DIP SW on the board are read and set as the child ID. If it is 0, it is set as a child device without ID (0xFE).

    LED settings are made. Here, the LED is set to blink ON/OFF every 10ms (in applications where sleep is performed and wake-up time is short, this setting is almost the same as being lit during wake-up).

    	brd.set_led(LED_TIMER::BLINK, 10); // blink (on 10ms/ off 10ms)
    

    begin()

    The begin() function is called just before the first loop() after the setup() function ends (then TWENET initialization is performed).

    void begin() {
    	sleepNow(); // the first time is just sleeping.
    }

    sleepNow() is called after setup() ends to perform the initial sleep.

    sleepNow()

    void sleepNow() {
    	uint32_t u32ct = 60000;
    
    	pinMode(PAL_MAG::PIN_SNS_OUT1, PIN_MODE::WAKE_FALLING);
    	pinMode(PAL_MAG::PIN_SNS_OUT2, PIN_MODE::WAKE_FALLING);
    
    	the_twelite.sleep(u32ct);
    }

    Before entering sleep, interrupt settings for magnetic sensor DIO pins are made using pinMode(). The second parameter specifies PIN_MODE::WAKE_FALLING, which wakes up when the pin state changes from HIGH to LOW.

    On line 7, the_twelite.sleep() is called to execute sleep. The parameter 60000 is necessary to reset the watchdog of the TWELITE PAL board. Without resetting, a hardware reset occurs after 60 seconds.

    wakeup()

    When waking up from sleep, wakeup() is called. After that, loop() is called each time. Before wakeup(), peripherals such as UART and board devices are woken up (such as resetting the watchdog timer). For example, LED control restarts.

    void wakeup() {
    	if (the_twelite.is_wokeup_by_wktimer()) {
    		sleepNow();
    	}
    }

    Here, if waking up by the wake timer (the_twelite.is_wokeup_by_wktimer()), sleep is executed again. This wake-up is only for resetting the watchdog timer mentioned above.

    In the case of waking up by magnetic sensor detection, it proceeds to loop() processing as is.

    loop()

    Here, the detected magnetic sensor DIO is checked, a packet is transmitted, and after transmission completes, it goes back to sleep.

    void loop() {
    	if (!b_transmit) {
    		if (auto&& pkt =
          the_twelite.network.use<NWK_SIMPLE>().prepare_tx_packet())
    
    			uint8_t b_north =
    			  the_twelite.is_wokeup_by_dio(PAL_MAG::PIN_SNS_NORTH);
    			uint8_t b_south =
    			  the_twelite.is_wokeup_by_dio(PAL_MAG::PIN_SNS_SOUTH);
    
    			Serial << "..sensor north=" << int(b_north)
    			       << " south=" << int(b_south) << mwx::crlf;
    
    			// set tx packet behavior
    			pkt << tx_addr(0x00)
    				<< tx_retry(0x1)
    				<< tx_packet_delay(0, 0, 2);
    
    			// prepare packet payload
    			pack_bytes(pkt.get_payload()
    				, make_pair(FOURCHARS, 4)
    				, b_north
    				, b_south
    			);
    
    			// do transmit
    			MWX_APIRET ret = pkt.transmit();
    
    			if (ret) {
    				u8txid = ret.get_value() & 0xFF;
    				b_transmit = true;
    			}
    			else {
    				// fail to request
    				sleepNow();
    			}
    		} else {
    		  sleepNow();
    		}
    	} else {
    		if (the_twelite.tx_status.is_complete(u8txid)) {
    			b_transmit = 0;
    			sleepNow();
    		}
    	}
    }

    The behavior inside loop() is controlled by the variable b_transmit. After a successful transmission request, this value is set to 1 and waits for packet transmission completion.

    	if (!b_transmit) {

    The detection DIO pins of the magnetic sensor are checked. There are two types of detection pins: north pole detection and south pole detection. If you simply want to know that a magnet approached, detection of either pin is the condition.

    uint8_t b_north =
      the_twelite.is_wokeup_by_dio(PAL_MAG::PIN_SNS_NORTH);
    uint8_t b_south =
      the_twelite.is_wokeup_by_dio(PAL_MAG::PIN_SNS_SOUTH);

    To check the wake-up source pin, use the_twelite.is_wokeup_by_dio(). The parameter is the pin number. The return value is stored in uint8_t to be included in the packet payload.

    After setting communication conditions and preparing the payload, transmission is performed.

    // do transmit
    MWX_APIRET ret = pkt.transmit();

    Then, if b_transmit is true in loop(), completion check is performed, and if complete, sleep is executed again by sleepNow().

    if (the_twelite.tx_status.is_complete(u8txid)) {
    	b_transmit = 0;
    	sleepNow();
    }

    Transmission completion is confirmed by the_twelite.tx_status.is_complete(u8txid). u8txid is the ID value returned at the time of transmission.