Enumerations | Functions | Variables
io_node_operator.c File Reference

IO Sample for Embedded OPC UA Server More...

#include <unistd.h>
#include <string.h>
#include <iothinx/iothinxio.h>
#include <mx_node_sdk/mx_node_interface_basic.h>
#include <mx_node_sdk/mx_node_interface_data_access.h>
#include "DataSet.h"
#include "cJSON.h"

Go to the source code of this file.

Enumerations

enum  OPERATOR_STATE {
  OPERATOR_STATE_STOP, OPERATOR_STATE_STOPPING, OPERATOR_STATE_RUNNING, OPERATOR_STATE_STOP,
  OPERATOR_STATE_STOPPING, OPERATOR_STATE_RUNNING
}
 include interfaces More...
 

Functions

struct timeval now ()
 custom function More...
 
MX_NODE_RESULT add_io_nodes ()
 
int parse_json_file (const char *filename)
 
int cJSON_to_struct_array (char *text)
 
int io_control_read (int slot, int index, MX_NODE_VARIANT *node_value)
 
int io_control_write (int slot, int index, const MX_NODE_VARIANT *node_value)
 

Variables

MX_NODE_NODE_OPERATOR_HANDLE g_operator_handle
 
volatile OPERATOR_STATE g_state
 
NODE ** io_example
 
int g_totalSlot
 
int * g_slotNodeCountRecorder
 

Detailed Description

IO Sample for Embedded OPC UA Server

Date
2019-06-05
Author
TJ Tai
Version
V1.0
Introduction:
This is IO sample code for Embedded OPC UA Server.

Definition in file io_node_operator.c.

Enumeration Type Documentation

include interfaces

Enumerator
OPERATOR_STATE_STOP 
OPERATOR_STATE_STOPPING 
OPERATOR_STATE_RUNNING 
OPERATOR_STATE_STOP 
OPERATOR_STATE_STOPPING 
OPERATOR_STATE_RUNNING 

Definition at line 38 of file io_node_operator.c.

Function Documentation

struct timeval now ( )

custom function

Definition at line 303 of file demo_node_operator.c.

304 {
305  struct timeval t;
306  gettimeofday(&t, NULL);
307  return t;
308 }
MX_NODE_RESULT add_io_nodes ( )

Definition at line 206 of file io_node_operator.c.

207 {
208  MX_NODE_RESULT res;
209  int rc, i,j;
210 
211  // folder
212  MX_NODE_NODE folder;
213  {
214  strcpy(folder.node_name, "ioThinx-4533");
216  strcpy(folder.description, "device name");
217  }
218  MX_NODE_NODE_HANDLE folder_handle;
219  res = mx_node_add_node(g_operator_handle, &folder, &folder_handle);
220  if (res != MX_NODE_RESULT_GOOD)
221  return res;
222 
223  // object
224  for(i=0;i<g_totalSlot;i++)
225  {
226  MX_NODE_NODE object;
227  {
228  sprintf(object.node_name, "Slot_%d", i);
229  object.node_type = MX_NODE_NODE_TYPE_OBJECT;
230  strcpy(object.description, "number of slot");
231  }
232  MX_NODE_NODE_HANDLE object_handle;
233  res = mx_node_add_node(g_operator_handle, &object, &object_handle);
234  if (res != MX_NODE_RESULT_GOOD)
235  return res;
236  res = mx_node_set_parent_node(object_handle, folder_handle);
237  if (res != MX_NODE_RESULT_GOOD)
238  return res;
239 
240  // variable
241  for(j=0;j<g_slotNodeCountRecorder[i];j++){
242  MX_NODE_NODE variable;
243  {
244  strcpy(variable.node_name, io_example[i][j].name);
246  strcpy(variable.description, io_example[i][j].description);
247  variable.variable.access_right = io_example[i][j].access;
248  io_example[i][j].data.type = io_example[i][j].var_type;
249  memset(&io_example[i][j].data.value, 0, sizeof(io_example[i][j].data.value));
250  variable.variable.value = io_example[i][j].data;
251  }
252  res = mx_node_add_node(g_operator_handle, &variable, &io_example[i][j].node_handle);
253  if (res != MX_NODE_RESULT_GOOD)
254  return res;
255  res = mx_node_set_parent_node(io_example[i][j].node_handle, object_handle);
256  if (res != MX_NODE_RESULT_GOOD)
257  return res;
258  }
259  }
260 
261  // Config IO mode
262  uint8_t channel_count = 1;
263  uint8_t di_mode;
264  uint8_t do_mode;
265  uint8_t counter_triggers;
266  uint16_t pwm_freq = 5;
267  uint16_t pwm_duty = 50;
268  for (i = 0; i < g_totalSlot; ++i){
269  for(j = 0; j< g_slotNodeCountRecorder[i]; j++){
270  if (io_example[i][j].func_type == OPC_DO_PULSECOUNT || io_example[i][j].func_type == OPC_DO_PULSESTATUS) {
271  // set to DO_MODE PULSE
272  do_mode = DO_MODE_PWM;
273  rc = ioThinx_DO_Config_SetModes(i, io_example[i][j].ch, channel_count, &do_mode);
274  if (rc != IOTHINX_ERR_OK)
275  {
276  printf("ioThinx_DO_Config_SetModes() = %d\n", rc);
277  }
278  //set frq and duty cycle
279  rc = ioThinx_DO_Config_SetPwmConfigures(i, io_example[i][j].ch, channel_count, &pwm_freq, &pwm_duty);
280  if (rc != IOTHINX_ERR_OK)
281  {
282  printf("ioThinx_DO_Config_SetPwmConfigures() = %d\n", rc);
283  }
284  }
285  if (io_example[i][j].func_type == OPC_DI_COUNTERSTATUS || io_example[i][j].func_type == OPC_DI_COUNTERVALUE) {
286  // set to DI_MODE COUNTER
287  di_mode = DI_MODE_COUNTER;
288  counter_triggers = CNT_TRIGGER_BOTH;
289  rc = ioThinx_DI_Config_SetModes(i, io_example[i][j].ch, channel_count, &di_mode);
290  if (rc != IOTHINX_ERR_OK)
291  {
292  printf("ioThinx_DI_Config_SetModes() = %d\n", rc);
293  }
294  rc = ioThinx_DI_Config_SetCntTriggers(i, io_example[i][j].ch, channel_count, &counter_triggers);
295  if (rc != IOTHINX_ERR_OK)
296  {
297  printf("ioThinx_DI_Config_SetCntTriggers() = %d\n", rc);
298  }
299  }
300  }
301  }
302  // reload config
304  if (rc != IOTHINX_ERR_OK)
305  {
306  printf("ioThinx_IO_Config_Reload() = %d\n", rc);
307  }
308  return MX_NODE_RESULT_GOOD;
309 }
MX_NODE_VARIANT value
MX_NODE_RESULT mx_node_add_node(MX_NODE_NODE_OPERATOR_HANDLE operator_handle, MX_NODE_NODE *node, MX_NODE_NODE_HANDLE *node_handle)
Add a node to OPC UA server.
size_t MX_NODE_NODE_HANDLE
Handle of a node.
Definition: mx_node_node.h:42
int * g_slotNodeCountRecorder
int access
Definition: DataSet.h:20
MX_NODE_VARIABLE variable
enum _MX_NODE_RESULT MX_NODE_RESULT
A result enumeration represents success or not.
A node&#39;s sturcture contain name, description, node type and attribute.
IOTHINX_ERR ioThinx_DI_Config_SetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
MX_NODE_RESULT mx_node_set_parent_node(MX_NODE_NODE_HANDLE node_handle, MX_NODE_NODE_HANDLE parent_node_handle)
Set a node&#39;s parent.
#define DO_MODE_PWM
Definition: iothinxio.h:290
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
NODE ** io_example
MX_NODE_ACCESS_RIGHT access_right
int g_totalSlot
#define CNT_TRIGGER_BOTH
Definition: iothinxio.h:97
IOTHINX_ERR ioThinx_DO_Config_SetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
#define DI_MODE_COUNTER
Definition: iothinxio.h:87
IOTHINX_ERR ioThinx_IO_Config_Reload(void)
IOTHINX_ERR ioThinx_DI_Config_SetCntTriggers(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
char node_name[MAX_NODE_NAME_LEN]
IOTHINX_ERR ioThinx_DO_Config_SetPwmConfigures(uint32_t slot, uint8_t start, uint8_t count, uint16_t frequencies[], uint16_t duty_cycles[])
MX_NODE_NODE_OPERATOR_HANDLE g_operator_handle
char description[MAX_NODE_DESC_LEN]
MX_NODE_VALUE_TYPE type
char name[64]
Definition: DataSet.h:17
int var_type
Definition: DataSet.h:18
union _MX_NODE_VARIANT::@0 value
MX_NODE_VARIANT data
Definition: DataSet.h:23
char description[64]
Definition: DataSet.h:21
MX_NODE_NODE_TYPE node_type
int parse_json_file ( const char *  filename)

Definition at line 311 of file io_node_operator.c.

312 {
313  FILE *f;
314  int len;
315  char *data;
316 
317  f=fopen(filename,"rb");
318  if (f == NULL)
319  return MX_PARSE_CONFIG_ERR;
320  fseek(f,0,SEEK_END);
321  len=ftell(f);
322  fseek(f,0,SEEK_SET);
323  data=(char*)malloc(len+1);
324  fread(data,1,len,f);
325  fclose(f);
326 
327  printf("parse file %s complete, len=%d.\n",filename,len);
328 
329  cJSON_to_struct_array(data);
330 
331  free(data);
332  return 0;
333 }
#define MX_PARSE_CONFIG_ERR
Definition: DataSet.h:10
int cJSON_to_struct_array(char *text)
int cJSON_to_struct_array ( char *  text)

Definition at line 336 of file io_node_operator.c.

337 {
338  cJSON *json,*item,*object, *slotItem, *chObj;
339  int i,j;
340 
341  json=cJSON_Parse(text);
342  if (!json)
343  {
344  printf("Error before: [%s]\n",cJSON_GetErrorPtr());
345  }
346  else {
347  slotItem=cJSON_GetObjectItem(json,"slot");
348  if(slotItem!=NULL)
349  {
350  g_totalSlot =cJSON_GetArraySize(slotItem);
351  //printf("cJSON_GetSlotSize: g_totalSlot=%d\n",g_totalSlot);
352  io_example = (NODE **)malloc(sizeof(NODE *)*g_totalSlot);
353  g_slotNodeCountRecorder = (int*)malloc(sizeof(int)*g_totalSlot);
354 
355  for(i=0;i<g_totalSlot;i++)
356  {
357  //printf("i=%d\n",i);
358  chObj=cJSON_GetArrayItem(slotItem,i);
359  int chsize = cJSON_GetArraySize(chObj);
360  io_example[i] = (NODE *)calloc(chsize, sizeof(NODE));
361  g_slotNodeCountRecorder[i] = chsize;
362  //printf("chsize=%d\n",chsize);
363 
364  for(j=0;j<chsize;j++){
365  object=cJSON_GetArrayItem(chObj,j);
366 
367  item=cJSON_GetObjectItem(object,"ch");
368  if(item!=NULL)
369  {
370  //printf("cJSON_GetObjectItem: type=%d, string is %s, valueint=%d\n",item->type,item->string,item->valueint);
371  io_example[i][j].ch=item->valueint;
372  }
373  else{
374  printf("cJSON_GetObjectItem: get ch failed\n");
375  }
376 
377  item=cJSON_GetObjectItem(object,"access");
378  if(item!=NULL)
379  {
380  //printf("cJSON_GetObjectItem: type=%d, string is %s, valueint=%d\n",item->type,item->string,item->valueint);
381  io_example[i][j].access=item->valueint;
382  }
383  else{
384  printf("cJSON_GetObjectItem: get access failed\n");
385  }
386  item=cJSON_GetObjectItem(object,"variant_type");
387  if(item!=NULL)
388  {
389  //printf("cJSON_GetObjectItem: type=%d, string is %s, valueint=%d\n",item->type,item->string,item->valueint);
390  io_example[i][j].var_type=item->valueint;
391  }
392  else{
393  printf("cJSON_GetObjectItem: get variable_type failed\n");
394  }
395  item=cJSON_GetObjectItem(object,"func_type");
396  if(item!=NULL)
397  {
398  //printf("cJSON_GetObjectItem: type=%d, string is %s, valueint=%d\n",item->type,item->string,item->valueint);
399  io_example[i][j].func_type=item->valueint;
400  }
401  else{
402  printf("cJSON_GetObjectItem: get func_type failed\n");
403  }
404  item=cJSON_GetObjectItem(object,"name");
405  if(item!=NULL)
406  {
407  //printf("cJSON_GetObjectItem: type=%d, string is %s\n",item->type,item->string);
408  if (i==0 && io_example[i][j].ch == -1) // slot 0 name
409  sprintf(io_example[i][j].name, "S%d_%s", i, item->valuestring);
410  else
411  sprintf(io_example[i][j].name, "S%d_C%d_%s", i, io_example[i][j].ch, item->valuestring);
412  }
413  item=cJSON_GetObjectItem(object,"description");
414  if(item!=NULL)
415  {
416  //printf("cJSON_GetObjectItem: type=%d, string is %s\n",item->type,item->string);
417  memcpy(io_example[i][j].description,item->valuestring,strlen(item->valuestring));
418  }
419  }
420  }
421  }
422 
423  //for(i=0;i<g_totalSlot;i++)
424  //{
425  // for(j=0;j<g_slotNodeCountRecorder[i];j++)
426  // {
427  // printf("io[%d,%d] ch: %d, access: %d, var_type: %d, func_type: %d, name: %s, description: %s\n",
428  // i,
429  // j,
430  // io_example[i][j].ch,
431  // io_example[i][j].access,
432  // io_example[i][j].var_type,
433  // io_example[i][j].func_type,
434  // io_example[i][j].name,
435  // io_example[i][j].description);
436  // }
437  // printf("\r\n");
438  //}
439 
440  cJSON_Delete(json);
441  }
442  return 0;
443 }
int * g_slotNodeCountRecorder
int valueint
Definition: cJSON.h:117
int access
Definition: DataSet.h:20
int ch
Definition: DataSet.h:16
const char *const name
Definition: cJSON.h:258
NODE ** io_example
int g_totalSlot
cJSON * item
Definition: cJSON.h:218
int func_type
Definition: DataSet.h:19
int var_type
Definition: DataSet.h:18
Definition: cJSON.h:103
char * valuestring
Definition: cJSON.h:115
int io_control_read ( int  slot,
int  index,
MX_NODE_VARIANT node_value 
)

Definition at line 445 of file io_node_operator.c.

446 {
447  FUNC_TYPE fn;
448  int32_t rc = 0;
449  int32_t value_int;
450  uint32_t di_values = 0;
451  uint32_t do_values = 0;
452  uint32_t relay_values = 0;
453  uint32_t pwm_starts = 0;
454  uint32_t counter_starts = 0;
455  uint32_t value_uint = 0;
456  uint32_t ai_raws = 0;
457  float value_float = 0;
458  uint8_t value_uint8 = 0;
459 
460  switch(io_example[slot][index].func_type){
461  case OPC_DI_MODE:
462  rc = ioThinx_DI_Config_GetModes(slot, io_example[slot][index].ch, 1, &value_uint8);
463  if (rc != IOTHINX_ERR_OK)
464  {
465  printf("ioThinx_DI_Config_GetModes() = %d\n", rc);
466  return MX_IO_CONTROL_ERR;
467  }
468  io_example[slot][index].data.value.u32 = (uint32_t)value_uint8;
469  break;
470  case OPC_DI_STATUS:
471  rc = ioThinx_DI_GetValues(slot, &di_values);
472  if (rc != IOTHINX_ERR_OK)
473  {
474  printf("ioThinx_DI_GetValues() = %d\n", rc);
475  return MX_IO_CONTROL_ERR;
476  }
477  value_int = (di_values >> io_example[slot][index].ch)& 0x1;
478  //printf("\r\n=========> MX_Read() set value = %d\r\n", value_int);
479  io_example[slot][index].data.value.i32 = value_int;
480  break;
482  rc = ioThinx_DI_GetCntStarts(slot, &counter_starts);
483  if (rc != IOTHINX_ERR_OK)
484  {
485  printf("\r\n ioThinx_DI_GetCntStarts() = %d\r\n", rc);
486  return MX_IO_CONTROL_ERR;
487  }
488  value_int = (counter_starts >> io_example[slot][index].ch)& 0x1;
489  io_example[slot][index].data.value.i32 = value_int;
490  break;
491  case OPC_DI_COUNTERVALUE:
492  rc = ioThinx_DI_GetCntValues(slot, io_example[slot][index].ch, 1, &value_uint);
493  if (rc != IOTHINX_ERR_OK)
494  {
495  printf("ioThinx_DI_GetCntValues() = %d\n", rc);
496  return MX_IO_CONTROL_ERR;
497  }
498  io_example[slot][index].data.value.u32 = value_uint;
499  break;
500  case OPC_DO_MODE:
501  rc = ioThinx_DO_Config_GetModes(slot, io_example[slot][index].ch, 1, &value_uint8);
502  if (rc != IOTHINX_ERR_OK)
503  {
504  printf("ioThinx_DO_Config_GetModes() = %d\n", rc);
505  }
506  io_example[slot][index].data.value.u32 = (uint32_t)value_uint8;
507  break;
508  case OPC_DO_STATUS:
509  rc = ioThinx_DO_GetValues(slot, &do_values);
510  if (rc != IOTHINX_ERR_OK)
511  {
512  printf("\r\n*** [ERR] ioThinx_DO_GetValues() = %d\r\n", rc);
513  return MX_IO_CONTROL_ERR;
514  }
515  value_int = (do_values >> io_example[slot][index].ch)& 0x1;
516  //printf("\r\n=========> MX_Read() set value = %d\r\n", value_int);
517  io_example[slot][index].data.value.i32 = value_int;
518  break;
519  case OPC_DO_PULSESTATUS:
520  rc = ioThinx_DO_GetPwmStarts(slot, &pwm_starts);
521  if (rc != IOTHINX_ERR_OK)
522  {
523  printf("\r\n ioThinx_DO_GetPwmStarts() = %d\r\n", rc);
524  return MX_IO_CONTROL_ERR;
525  }
526  value_int = (pwm_starts >> io_example[slot][index].ch)& 0x1;
527  io_example[slot][index].data.value.i32 = value_int;
528  break;
529  case OPC_DO_PULSECOUNT:
530  rc = ioThinx_DO_GetPwmCounts(slot, io_example[slot][index].ch, 1, &value_uint);
531  if (rc != IOTHINX_ERR_OK)
532  {
533  printf("ioThinx_DO_Config_GetPwmCounts() = %d\n", rc);
534  return MX_IO_CONTROL_ERR;
535  }
536  io_example[slot][index].data.value.u32 = value_uint;
537  break;
538  case OPC_RELAY_VALUE:
539  rc = ioThinx_Relay_GetValues(slot, &relay_values);
540  if (rc != IOTHINX_ERR_OK)
541  {
542  printf("ioThinx_Relay_GetValues() = %d\n", rc);
543  return MX_IO_CONTROL_ERR;
544  }
545  value_int = (relay_values >> io_example[slot][index].ch)& 0x1;
546  //printf("\r\n=========> MX_Read() set value = %d\r\n", value_int);
547  io_example[slot][index].data.value.i32 = value_int;
548  break;
549  case OPC_AI_MODE:
550  rc = ioThinx_AI_Config_GetRanges(slot, io_example[slot][index].ch, 1, &value_uint8);
551  if (rc != IOTHINX_ERR_OK)
552  {
553  printf("ioThinx_AI_GetEngs() = %d\n", rc);
554  return MX_IO_CONTROL_ERR;
555  }
556  //printf("\r\n=========> MX_Read() set value = %u\r\n", value_uint8);
557  io_example[slot][index].data.value.u8 = value_uint8;
558  break;
559  case OPC_AI_STATUS:
560  rc = ioThinx_AI_GetStatuss(slot, io_example[slot][index].ch, 1, &value_uint8);
561  if (rc != IOTHINX_ERR_OK)
562  {
563  printf("ioThinx_AI_GetEngs() = %d\n", rc);
564  return MX_IO_CONTROL_ERR;
565  }
566  //printf("\r\n=========> MX_Read() set value = %u\r\n", value_uint8);
567  io_example[slot][index].data.value.u8 = value_uint8;
568  break;
569  case OPC_AI_RAWVALUE:
570  rc = ioThinx_AI_GetRaws(slot, io_example[slot][index].ch, 1, &ai_raws);
571  if (rc != IOTHINX_ERR_OK)
572  {
573  printf("ioThinx_AI_GetEngs() = %d\n", rc);
574  return MX_IO_CONTROL_ERR;
575  }
576  //printf("\r\n=========> MX_Read() set value = %lu\r\n", ai_raws);
577  io_example[slot][index].data.value.u32 = ai_raws;
578  break;
580  rc = ioThinx_AI_GetMinRaws(slot, io_example[slot][index].ch, 1, &ai_raws);
581  if (rc != IOTHINX_ERR_OK)
582  {
583  printf("ioThinx_AI_GetEngs() = %d\n", rc);
584  return MX_IO_CONTROL_ERR;
585  }
586  //printf("\r\n=========> MX_Read() set value = %lu\r\n", ai_raws);
587  io_example[slot][index].data.value.u32 = ai_raws;
588  break;
590  rc = ioThinx_AI_GetMaxRaws(slot, io_example[slot][index].ch, 1, &ai_raws);
591  if (rc != IOTHINX_ERR_OK)
592  {
593  printf("ioThinx_AI_GetEngs() = %d\n", rc);
594  return MX_IO_CONTROL_ERR;
595  }
596  //printf("\r\n=========> MX_Read() set value = %lu\r\n", ai_raws);
597  io_example[slot][index].data.value.u32 = ai_raws;
598  break;
599  case OPC_AI_SCALEDVALUE:
600  rc = ioThinx_AI_GetEngs(slot, io_example[slot][index].ch, 1, &value_float);
601  if (rc != IOTHINX_ERR_OK)
602  {
603  printf("ioThinx_AI_GetEngs() = %d\n", rc);
604  return MX_IO_CONTROL_ERR;
605  }
606  //printf("\r\n=========> MX_Read() set value = %f\r\n", value_float);
607  io_example[slot][index].data.value.f = value_float;
608  break;
609  default:
610  printf("IO function not found!\r\n");
611  return MX_IO_CONTROL_ERR;
612  }
613  *node_value = io_example[slot][index].data;
614 
615  return MX_IO_CONTROL_OK;
616 }
IOTHINX_ERR ioThinx_AI_GetMinRaws(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR ioThinx_DI_GetCntStarts(uint32_t slot, uint32_t *p_starts)
int ch
Definition: DataSet.h:16
IOTHINX_ERR ioThinx_DO_GetPwmCounts(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR ioThinx_Relay_GetValues(uint32_t slot, uint32_t *p_values)
int index
Definition: cJSON.h:168
IOTHINX_ERR ioThinx_DO_GetPwmStarts(uint32_t slot, uint32_t *p_starts)
IOTHINX_ERR ioThinx_DI_GetCntValues(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR ioThinx_AI_GetMaxRaws(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
NODE ** io_example
IOTHINX_ERR ioThinx_AI_GetRaws(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR ioThinx_DI_Config_GetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
#define MX_IO_CONTROL_ERR
Definition: DataSet.h:12
IOTHINX_ERR ioThinx_AI_GetEngs(uint32_t slot, uint8_t start, uint8_t count, float buf[])
IOTHINX_ERR ioThinx_AI_Config_GetRanges(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
FUNC_TYPE
Definition: DataSet.h:27
IOTHINX_ERR ioThinx_DI_GetValues(uint32_t slot, uint32_t *p_values)
#define MX_IO_CONTROL_OK
Definition: DataSet.h:11
IOTHINX_ERR ioThinx_DO_Config_GetModes(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
union _MX_NODE_VARIANT::@0 value
IOTHINX_ERR ioThinx_DO_GetValues(uint32_t slot, uint32_t *p_values)
IOTHINX_ERR ioThinx_AI_GetStatuss(uint32_t slot, uint8_t start, uint8_t count, uint8_t buf[])
MX_NODE_VARIANT data
Definition: DataSet.h:23
int io_control_write ( int  slot,
int  index,
const MX_NODE_VARIANT node_value 
)

Definition at line 618 of file io_node_operator.c.

619 {
620  FUNC_TYPE fn;
621  int32_t value_int = 0;
622  uint32_t do_values = 0;
623  uint32_t pwm_starts = 0;
624  uint32_t counter_starts = 0;
625  uint32_t value_uint = 0;
626  uint32_t relay_values = 0;
627  int rc = 0;
628 
629  switch(io_example[slot][index].func_type){
631  if (node_value->type == MX_NODE_VALUE_TYPE_INT32) {
632  value_int = node_value->value.i32;
633  }
634  else {
635  return MX_IO_CONTROL_ERR;
636  }
637  if (value_int != 0 && value_int != 1) {
638  return MX_IO_CONTROL_ERR;
639  }
640  rc = ioThinx_DI_GetCntStarts(slot, &counter_starts);
641  if (rc != IOTHINX_ERR_OK)
642  {
643  printf("\r\n ioThinx_DI_GetCntStarts() = %d\r\n", rc);
644  return MX_IO_CONTROL_ERR;
645  }
646  if (value_int == 1) {
647  counter_starts |= 0x1 << (io_example[slot][index].ch);
648  rc = ioThinx_DI_SetCntStarts(slot, counter_starts);
649  if (rc != IOTHINX_ERR_OK)
650  {
651  printf("ioThinx_DI_SetCntStarts() = %d\n", rc);
652  return MX_IO_CONTROL_ERR;
653  }
654  }
655  else if (value_int == 0) {
656  counter_starts |= 0x1 << (io_example[slot][index].ch);
657  rc = ioThinx_DI_SetCntStops(slot, counter_starts);
658  if (rc != IOTHINX_ERR_OK)
659  {
660  printf("ioThinx_DI_SetCntStops() = %d\n", rc);
661  return MX_IO_CONTROL_ERR;
662  }
663  }
664  else {
665  return MX_IO_CONTROL_ERR;
666  }
667  break;
668  case OPC_DI_COUNTERVALUE:
669  if (node_value->type == MX_NODE_VALUE_TYPE_UINT32) {
670  value_uint = node_value->value.u32;
671  }
672  else {
673  return MX_IO_CONTROL_ERR;
674  }
675  rc = ioThinx_DI_SetCntValues(slot, io_example[slot][index].ch, 1, &value_uint);
676  if (rc != IOTHINX_ERR_OK)
677  {
678  printf("ioThinx_DI_SetCntValues() = %d\n", rc);
679  return MX_IO_CONTROL_ERR;
680  }
681  break;
682  case OPC_DO_STATUS:
683  if (node_value->type == MX_NODE_VALUE_TYPE_INT32) {
684  value_int = node_value->value.i32;
685  }
686  else {
687  return MX_IO_CONTROL_ERR;
688  }
689  if (value_int != 0 && value_int != 1) {
690  return MX_IO_CONTROL_ERR;
691  }
692  //printf("\r\n=========> MX_Write() set value = %d\r\n", value_int);
693  rc = ioThinx_DO_GetValues(slot, &do_values);
694  if (rc != IOTHINX_ERR_OK)
695  {
696  printf("\r\nioThinx_DO_GetValues() = %d\r\n", rc);
697  return MX_IO_CONTROL_ERR;
698  }
699  do_values = do_values & ~(0x1 << io_example[slot][index].ch) | (value_int << io_example[slot][index].ch);
700  //printf("do value = %lu\n", (unsigned long)do_values);
701  rc = ioThinx_DO_SetValues(slot, do_values);
702  if (rc != IOTHINX_ERR_OK)
703  {
704  printf("ioThinx_DO_SetValues() = %d\n", rc);
705  return MX_IO_CONTROL_ERR;
706  }
707  //printf("\r\n******************* [%lu:%u] DO value = %x\r\n", do_slot, do_channel, (do_values >> do_channel) & 0x1);
708  break;
709  case OPC_DO_PULSESTATUS:
710  if (node_value->type == MX_NODE_VALUE_TYPE_INT32) {
711  value_int = node_value->value.i32;
712  }
713  else {
714  return MX_IO_CONTROL_ERR;
715  }
716  if (value_int != 0 && value_int != 1) {
717  return MX_IO_CONTROL_ERR;
718  }
719  rc = ioThinx_DO_GetPwmStarts(slot, &pwm_starts);
720  if (rc != IOTHINX_ERR_OK)
721  {
722  printf("\r\n ioThinx_DO_GetPwmStarts() = %d\r\n", rc);
723  return MX_IO_CONTROL_ERR;
724  }
725  if (value_int == 1) {
726  // start pwm
727  pwm_starts |= 0x1 << (io_example[slot][index].ch);
728  rc = ioThinx_DO_SetPwmStarts(slot, pwm_starts);
729  if (rc != IOTHINX_ERR_OK)
730  {
731  printf("ioThinx_DO_SetPwmStarts() = %d\n", rc);
732  return MX_IO_CONTROL_ERR;
733  }
734  }
735  else if (value_int == 0) {
736  // stop pwm
737  pwm_starts |= 0x1 << (io_example[slot][index].ch);
738  rc = ioThinx_DO_SetPwmStops(slot, pwm_starts);
739  if (rc != IOTHINX_ERR_OK)
740  {
741  printf("ioThinx_DO_SetPwmStops() = %d\n", rc);
742  return MX_IO_CONTROL_ERR;
743  }
744  }
745  else {
746  return MX_IO_CONTROL_ERR;
747  }
748  break;
749  case OPC_DO_PULSECOUNT:
750  if (node_value->type == MX_NODE_VALUE_TYPE_UINT32) {
751  value_uint = node_value->value.u32;
752  }
753  else {
754  return MX_IO_CONTROL_ERR;
755  }
756  rc = ioThinx_DO_SetPwmCounts(slot, io_example[slot][index].ch, 1, &value_uint);
757  if (rc != IOTHINX_ERR_OK)
758  {
759  printf("ioThinx_DO_Config_SetPwmCounts() = %d\n", rc);
760  }
761  break;
762  case OPC_RELAY_VALUE:
763  if (node_value->type == MX_NODE_VALUE_TYPE_INT32) {
764  value_int = node_value->value.i32;
765  }
766  else {
767  return MX_IO_CONTROL_ERR;
768  }
769  //printf("\r\n=========> MX_Write() set value = %d\r\n", value_int);
770  if (value_int != 0 && value_int != 1) {
771  return MX_IO_CONTROL_ERR;
772  }
773  rc = ioThinx_Relay_GetValues(slot, &relay_values);
774  if (rc != IOTHINX_ERR_OK)
775  {
776  printf("\r\nioThinx_DO_GetValues() = %d\r\n", rc);
777  return MX_IO_CONTROL_ERR;
778  }
779  relay_values = relay_values & ~(0x1 << io_example[slot][index].ch) | (value_int << io_example[slot][index].ch);
780  //printf("relay value = %lu\n", (unsigned long)relay_values);
781  rc = ioThinx_Relay_SetValues(slot, relay_values);
782  if (rc != IOTHINX_ERR_OK)
783  {
784  printf("ioThinx_DO_SetValues() = %d\n", rc);
785  return MX_IO_CONTROL_ERR;
786  }
787  //printf("\r\n******************* [%lu:%u] DO value = %x\r\n", slot, io_example[slot][index].ch, (relay_values >> io_example[slot][index].ch) & 0x1);
788  break;
789  default:
790  printf("function not found!\r\n");
791  }
792  io_example[slot][index].data = *node_value;
793  return MX_IO_CONTROL_OK;
794 }
IOTHINX_ERR ioThinx_DO_SetPwmCounts(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
IOTHINX_ERR ioThinx_DI_SetCntStops(uint32_t slot, uint32_t stops)
IOTHINX_ERR ioThinx_DI_GetCntStarts(uint32_t slot, uint32_t *p_starts)
IOTHINX_ERR ioThinx_DI_SetCntStarts(uint32_t slot, uint32_t starts)
int ch
Definition: DataSet.h:16
IOTHINX_ERR ioThinx_DO_SetPwmStops(uint32_t slot, uint32_t stops)
IOTHINX_ERR ioThinx_Relay_GetValues(uint32_t slot, uint32_t *p_values)
IOTHINX_ERR ioThinx_DO_SetValues(uint32_t slot, uint32_t values)
int index
Definition: cJSON.h:168
IOTHINX_ERR ioThinx_DO_GetPwmStarts(uint32_t slot, uint32_t *p_starts)
#define IOTHINX_ERR_OK
Definition: iothinxio.h:35
NODE ** io_example
IOTHINX_ERR ioThinx_Relay_SetValues(uint32_t slot, uint32_t values)
#define MX_IO_CONTROL_ERR
Definition: DataSet.h:12
FUNC_TYPE
Definition: DataSet.h:27
IOTHINX_ERR ioThinx_DI_SetCntValues(uint32_t slot, uint8_t start, uint8_t count, uint32_t buf[])
MX_NODE_VALUE_TYPE type
#define MX_IO_CONTROL_OK
Definition: DataSet.h:11
union _MX_NODE_VARIANT::@0 value
IOTHINX_ERR ioThinx_DO_GetValues(uint32_t slot, uint32_t *p_values)
IOTHINX_ERR ioThinx_DO_SetPwmStarts(uint32_t slot, uint32_t starts)
MX_NODE_VARIANT data
Definition: DataSet.h:23

Variable Documentation

MX_NODE_NODE_OPERATOR_HANDLE g_operator_handle

Definition at line 48 of file io_node_operator.c.

volatile OPERATOR_STATE g_state

Definition at line 49 of file io_node_operator.c.

NODE** io_example

Definition at line 51 of file io_node_operator.c.

int g_totalSlot

Definition at line 52 of file io_node_operator.c.

int* g_slotNodeCountRecorder

Definition at line 53 of file io_node_operator.c.