1. Home
  2. Docs
  3. Sensor Node
  4. Introduction
  5. SNODE Logic Engine

SNODE Logic Engine

There are only three global functions that are needed to use the SNODE library that are defined in the snode.h file. Let’s consider each of these functions.

SNODE_ERROR SNODE_Init(uint16_t canFrameSourceId);
SNODE_ERROR SNODE_Registration(const PSNODE_SENSOR_REG pSensor);
SNODE_ERROR SNODE_ProcessPacket(void);

The SNODE_Init function initializes the SNODE Engine. The Node ID (Identifier) is used as the input parameter of the function (uint16_t canFrameSourceId). Each node must have a unique identifier (Node ID). The maximum number of nodes in a network is defined by

#define SNODE_CACHE_ENTRY_MAX_NUM_OF_NODES   (0x08)

The max number of nodes is depending on free memory of the MCU and the CAN transceiver.

The SNODE_ProcessPacket function should be called in a loop in order to procedure the SNODE Engine (receive and send frames, multi-frames, timings, etc.).

The SNODE_Registration function is used to register sensors of the node. The pointer to the SNODE_SENSOR_REG structure is used as the input parameter of the function (const PSNODE_SENSOR_REG pSensor). The struct is as follows.

typedef SNODE_ERROR (* SNODE_SENSOR_CALLBACK)(void *);

typedef struct _SNODE_SENSOR_REG //! Registration.
{
  uint8_t                     addr;
  const uint16_t              type; //! SNODE_SENSOR_TYPE.
  const uint8_t               mode; //! SNODE_SENSOR_MODE_TYPE.
  const uint16_t              subscriber; //! SNODE_SENSOR_TYPE.
  const SNODE_SENSOR_CALLBACK callback;
}SNODE_SENSOR_REG, *PSNODE_SENSOR_REG;

The SNODE_SENSOR_REG structure has five members which describe the sensor of the node. The maximum number of sensors for each node (max value is 0x0F) is defined by

#define SNODE_MAX_NUM_OF_SENSORS             (0x04)

As it was written above, each node can have a few sensors.  Each sensor must have its own address to communicate between other sensors of nodes in a network. When the new sensor is registered the addr value is automatically incremented (it starts from addr = 0x01) therefore the addr variable of the struct should be ignored or set to zero.

void setup()
{
  SNODE_Init((uint16_t)CAN_FRAME_SOURCE_ID);
 
  //! The Button Sensor Addr is 0x01.
  SNODE_Registration((PSNODE_SENSOR_REG)&SNODE_ButtonSensor); 
 
  //! The Led Sensor Addr is 0x02.
  SNODE_Registration((PSNODE_SENSOR_REG)&SNODE_LedSensor);
}

The mode of the sensor is defined by the SNODE_SENSOR_MODE_TYPE enumeration:

typedef enum _SNODE_SENSOR_MODE_TYPE
{
  SNODE_INPUT_SENSOR_MODE_TYPE   = 0x00,
  SNODE_OUTPUT_SENSOR_MODE_TYPE  = 0x01
}SNODE_SENSOR_MODE_TYPE, *PSNODE_SENSOR_MODE_TYPE;

There are two types of the sensor mode (input and output) and may be depicted as shown below.

Figure 7 – Sensor modes and sensor types

The type and subscriber values of the sensor structure are defined by the SNODE_SENSOR_TYPE enumeration. Each sensor type number is written as a bit of uint16_t. The subscriber member of the structure is only used for output sensors and describes the types of sensors from which the data should be received and handled by the last ones (subscribe). For input sensors the subscriber value should be set to SNODE_NONE_SENSOR_TYPE.

typedef enum _SNODE_SENSOR_TYPE
{
  SNODE_NONE_SENSOR_TYPE              = 0x00,

//! Output interfaces - SNODE_OUTPUT_SENSOR_MODE_TYPE.
  SNODE_DC_MOTOR_SENSOR_TYPE          = 0x01,  //! DC motor.
  SNODE_STEPPER_MOTOR_SENSOR_TYPE     = 0x02,  //! Stepper motor.
  SNODE_SERVO_SENSOR_TYPE             = 0x04,  //! Servo motor.
  SNODE_LED_SENSOR_TYPE               = 0x08,  //! Led.
  SNODE_RGB_LED_SENSOR_TYPE           = 0x10,  //! RGB led.
  SNODE_MATRIX_LED_SENSOR_TYPE        = 0x20,  //! Matrix led.
  SNODE_SPEAKER_SENSOR_TYPE           = 0x40,  //! Speaker sensor.
  SNODE_BUZZER_SENSOR_TYPE            = 0x80,  //! Buzzer.
  SNODE_DISPLAY_SENSOR_TYPE           = 0x100, //! Display.

//! Input interfaces - SNODE_INPUT_SENSOR_MODE_TYPE.
  SNODE_COLOR_SENSOR_TYPE             = 0x01,   //! Color.
  SNODE_ULTRASONIC_SENSOR_TYPE        = 0x02,   //! Ultrasonic.
  SNODE_GYROSCOPE_SENSOR_TYPE         = 0x04,   //! Gyroscope.
  SNODE_LIGHT_INTENSITY_SENSOR_TYPE   = 0x08,   //! Light intensity.
  SNODE_SOUND_INTENSITY_SENSOR_TYPE   = 0x10,   //! Sound intensity.
  SNODE_TEMPERATURE_SENSOR_TYPE       = 0x20,   //! Temperature.
  SNODE_HUMIDITY_SENSOR_TYPE          = 0x40,   //! Humidity.
  SNODE_SOIL_SENSOR_TYPE              = 0x80,   //! Soil.
  SNODE_PIR_SENSOR_TYPE               = 0x100,  //! Pir.
  SNODE_DSP_SENSOR_TYPE               = 0x200,  //! Dsp.
  SNODE_RPM_SENSOR_TYPE               = 0x400,  //! Revolutions per minute.
  SNODE_INTERNAL_VOLTAGE_SENSOR_TYPE  = 0x800,  //! Internal voltage.
  SNODE_EXTERNAL_VOLTAGE_SENSOR_TYPE  = 0x1000, //! External voltage.
//! Control interfaces.
  SNODE_BUTTONS_SENSOR_TYPE           = 0x2000, //! Buttons.
  SNODE_KNOB_SENSOR_TYPE              = 0x1000, //! Knob.
  SNODE_JOYSTICK_SENSOR_TYPE          = 0x4000, //! Joystick.
  SNODE_ENCODER_SENSOR_TYPE           = 0x8000, //! Encoder.
}SNODE_SENSOR_TYPE, *PSNODE_SENSOR_TYPE;

The callback member of the structure is used to handle the data of the sensor (receive data for output sensors and send data for input sensors). The maximum payload size of data is 4 bytes.

Example of usage for input sensors:

SNODE_ERROR SNODE_GetSensorData(void * data)
{
  uint32_t *pSensor = (uint32_t *) data;

  *pSensor = ReadSensorData();

  return SNODE_SUCCESS;
}

Example of usage for output sensors:

SNODE_ERROR SNODE_SetSensorData(void * data)
{
  PSNODE_SENSOR_CALLBACK_DATA  pSensorCallbackData = (PSNODE_SENSOR_CALLBACK_DATA) data;

  WriteSensorData(pSensorCallbackData->data);

  return SNODE_SUCCESS;
}

For output sensors the pointer to the data has an additional information and should be pointed to the SNODE_SENSOR_CALLBACK_DATA structure. The struct is as follows.

typedef struct _SNODE_SENSOR_CALLBACK_DATA
{
  uint16_t id;
  uint8_t  addr;
  uint16_t type;
  bool     status; //! Connected or disconnected.
  uint32_t data;
}SNODE_SENSOR_CALLBACK_DATA, *PSNODE_SENSOR_CALLBACK_DATA;

The structure above is shown from what id, addr, type of the node the data is received. There is a status flag which is shown if the sensor is connected or disconnected. If the sensor is disconnected, the SNODE Engine calls the callback for the last time with status flag that is set to false and the data that is set to zero.

Info: Output sensors subscribes to Input sensors.