Skip to content
dberenguer edited this page Aug 21, 2014 · 6 revisions

Registers are the basis of the SWAP protocol. Every endpoint or set of endpoints is represented by its own register so writing or reading each register is the way to control and read each value.

The REGISTER class is then the way to control things but it is also used to transmit statuses about the register. The following is a list of the main methods provided by the class:

Apart from reading this page, taking a look at any of the SWAP-enabled sample applications is probably the best way to understand how to work with registers. In our sample sketches, regtable.ino and regtable.h contain all the register-related code.

Class constructor

REGISTER(uint8_t *value, const uint8_t length, const void (*updater)(uint8_t rId), const void (*setter)(uint8_t rId, uint8_t *v), const SWDTYPE typ=SWDTYPE_OTHER, const int eepromAddr=-1, const uint16_t bank=DEFAULT_NVOLAT_SECTION)

Description

Class constructor.

Parameters value : pointer to the variable containing the endpoint value.

length : size (bytes) of the variable containing the endpoint value

updater : pointer to a custom value called whenever getData is called from user application or whenever a SWAP query is addressed to the register from another node. Set to NULL if the register is not an input (binary input, sensor data, ADC, etc).

setter : pointer to a custom value called whenever setData is called from user application or whenever a SWAP command is addressed to the register from another node. Set to NULL if the register does not contain output information (binary outputs, PWM's, DAC, etc.).

type : type of register. Options: SWDTYPE_INTEGER or SWDTYPE_OTHER, which is the default type if no type is specified.

eepromAddr : address in EEPROM or in non-volatile memory where the register value needs to be stored. Don't specify or set to -1 if the register does not need to be saved in non-volatile memory.

bank : Non-volatile bank, in case that the register needs to be saved.

Example

// Define custom registers
unsigned int sensorData;  // Sensor data (input)
REGISTER regSensor(&sensorData, sizeof(sensorData), &updtSensorData, NULL);
byte dtPwmOutput[4]; // 4 x 8-bit PWM outputs
REGISTER regPwmOutput(dtPwmOutput, sizeof(dtPwmOutput), NULL, &setPwmOutput);

// Declare custom registers in regTable
DECLARE_REGISTERS_START()  // Here all standard registers are declared
  &regSensor,              // Declare custom registers following the same sequence as
  &regPwmOutput            // in the above definition
DECLARE_REGISTERS_END()    // End of declaration

// Register indexes. They will let us access our registers my means of getRegister
DEFINE_REGINDEX_START()  // Here indexes for the standard registers are created (0..10)
  REGI_SENSOR,           // First custom index (11)
  REGI_PWMOUTPUT         // Second custom register (12)
DEFINE_REGINDEX_END()    // End of index definition

getData

void getData(void)

Description

Update register contents by means of the associated updater function. Transmit SWAP status packet after the update.

Parameters

None

Example

regSensor.getData();   // Call to updtSensorData. Update sensor data and transmit status
getRegister(REGI_SENSOR)->getData();   // Same thing but using getRegister

setData

void setData(uint8_t *data)

Description Change register contents by means of the associated setter function. Transmit SWAP status packet after the update.

Parameters

data : byte array with the new data.

Example

byte pwmVal[] = {0x10, 0x25, 0, 0xFF}; // Four PWM values to be passed to setData
regSensor.setData(pwmVal);   // Call to setPwmOutput. Set data and transmit status
getRegister(REGI_PWMOUTPUT)->setData(pwmVal);   // Same thing but using getRegister

setValueFromBeBuffer

void setValueFromBeBuffer(uint8_t* beBuffer)

Description

Set the integer value of the register from the payload of a SWAP command. This can be seen as a Big-Endian byte array to integer converter. The reason of this method is that panStamp's MCUs are little-endian (MSB has the lowest address) but SWAP and other protocols are big-endian (MSB has the highest address).

Parameters

beBuffer : big-endian byte array, following the same sequence as in the SWAP command received.

Example

uint16_t dtTempPoint;  // Temperature setpoint
REGISTER regTempPoint(&dtTempPoint, sizeof(dtTempPoint), NULL, &setTempPoint);

// Setter function of our output register
const void setTempPoint(uint8_t id, uint8_t *point)
{
  // Update register's integer value from BE array
  regTempPoint.setValueFromBeBuffer(point);

  /* This is an alternative to the above instruction
  dtTempPoint = point[1];
  dtTempPoint = (dtTempPoint << 8) | point[0];
  */

  // Now dtTempPoint is updated. We can do something with it.
}  

Anti Swap

API for Anti Swap

Clone this wiki locally