-
Notifications
You must be signed in to change notification settings - Fork 29
SWAP API
The SWAP (Simple Abstract Wireless Protocol) API is implemented in a separate library located in libraries/swap/. This API provides a set of classes and methods to interface with the SWAP communication stack without having to deal with low-level communication tasks and protocol details.
Using SWAP in your application leads you to understanding what SWAP registers are and how to declare them. Registers are the core of the SWAP protocol. Every wireless packet makes reference to a specific register in the node so this API makes extensive use of registers.
This API relies on the swap object, automatically instantiated from the SWAP class. The following is a list of methods provided by this class:
You will notice that the SWAP class does not provide any method to transmit or receive SWAP packets. This is in fact REGISTER class' "honor".
void init(void)
Description
Initialize SWAP registers and communication settings
Example
#include "swap.h"
void setup()
{
swap.init()
...
}
void enterSystemState(SYSTATE state)
Description
Enter and communicate system state.
Parameters
state : system state.
Options:
- SYSTATE_RESTART : Restart node
- SYSTATE_RXON : Node with RF reception enabled (not sleeping)
- SYSTATE_RXOFF : Node with RF reception disabled (probably sleeping)
- SYSTATE_SYNC : Node momentarily in synchronization mode, with RF reception enabled
- SYSTATE_LOWBAT : Low-battery state. This mode needs to be handled from user application
- SYSTATE_UPGRADE : Node in firmware upgrade mode
Example
// Enter SYNC state and transmit SWAP status packet
swap.enterSystemState(SYSTATE_SYNC);
// During 3 seconds, listen the network for possible commands whilst the LED blinks
for(i=0 ; i<6 ; i++)
{
digitalWrite(LED, HIGH);
delay(100);
digitalWrite(LED, LOW);
delay(400);
}
// Switch to Rx OFF state and transmit SWAP status packet
swap.enterSystemState(SYSTATE_RXOFF);
void goToSleep(void)
Description
Put the module in sleep mode during swap.txInterval seconds. swap.txInterval is represented by register number 10 (register ID = 10) so this value can be changed wirelessly.
Parameters
None.
Example
void loop()
{
getRegister(REGI_SENSOR)->getData(); // Update sensor data and transmit SWAP status
swap.goToSleep(); // Sleep for swap.txInterval seconds
}
void nvolatToFactoryDefaults(void)
Description
Write default config values in non-volatile memory (infomem flash or EEPROM).
Parameters
None
Example
void setup()
{
swap.nvolatToFactoryDefaults();
swap.init();
}
void attachInterrupt(uint8_t type, void (*funct)(SWPACKET*))
Description
Declare custom ISR, to be called whenever a SWAP status is received
Parameters
type : funct : pointer to a custom callback function.
Example
/**
* swapStatusReceived
*
* Function automatically called by the panStamp API whenever a SWAP status
* packet is received
*
* 'status' SWAP status packet received
*/
void swapStatusReceived(SWPACKET *status)
{
// status->regId Register ID
// status->srcAddr Source address
// status->value.length Length of data field
// status->value.data Array of data bytes
}
/**
* Arduino's setup function
*/
void setup()
{
// Init panStamp
//panstamp.init(CFREQ_868); // Not necessary unless you want a different frequency
// Init SWAP stack
swap.init();
// Declare callback function for dispatching incoming SWAP status packets
swap.attachInterrupt(STATUS, swapStatusReceived);
}
void enableRepeater(uint8_t maxHop)
Description
Enable repeater mode. This mode needs the node to be continuously listening the wireless network.
Parameters
maxHop : maximum number of times that a packet can be repeated.
Example
// Enable repeater mode. Maximum hop count = 3
swap.enableRepeater(3);
REGISTER * getRegister(uint8_t regId)
Description
getRegister is an independent function (not belonging to any class) returning the register with the ID passed as argument.
Parameters
regId : register ID
Example
void loop()
{
// Update and transmit sensor data
getRegister(REGI_SENSOR)->getData();
// Sleep
swap.goToSleep();
}