⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 xbee_gpio_client.c

📁 Dynamic C 程式語言源碼 嵌入式控制系統 Xbee蜂群網路~
💻 C
📖 第 1 页 / 共 4 页
字号:
      return 0;
   }

   if (ch == 8)
   {
      // Received a backspace character
      if (ptr != buf)
      {
      	--ptr;
         *ptr = 0; // add null character
         putchar(ch); // print newline character
      }
      return 1;
   }
   else {
   	putchar(ch); // print non-newline character
   }

   if (ch != 13)
   {
      *ptr = (char)toupper(ch);
      if ((++ptr) < (&buf[PARSE_MAX_BUF]))
      {
         return 1;
      }
   }
   if (ptr == buf) {
      printf("\nG> ");
      return 1;
   }

   // CR received or buffer full, process command string
   *ptr = 0;
   ptr = buf;
   printf("\n");

   for(state = PARSE_CMD; *ptr; ptr = end)
   {
	   // Ignore leading spaces
	   while (isspace(*ptr)) ++ptr;
      // Find end of word and break into substring
      end = strpbrk(ptr, " ");
	   if (end)
	   {
	      *end++ = 0;
	   }
      else {
         end = ptr + strlen(ptr);
      }

      switch (state) {
      	case PARSE_CMD:
	         switch (*ptr)
	         {
	            case 'C':
	               if (!strcmp(ptr, "CLOSE")) {
                     gpio_help(SHOW_NET_HELP);
                     return 0;    // Close node and set main state back to idle
                  }
                  else {
                     state = PARSE_ERROR;
                  }
	               break;

	            case 'G':
	               state = (strcmp(ptr, "GET") ? PARSE_ERROR : PARSE_SIGNAL);
	               cmd = SIGNAL_GET;
	               break;

	            case 'H':
	               if (!strcmp(ptr, "HELP")) {
                     gpio_help(SHOW_GPIO_HELP);
                     state = PARSE_EXIT;
                  }
                  else {
                     state = PARSE_ERROR;
                  }
	               break;

	            case 'N':
	               if (!strcmp(ptr, "NAMES")) {
                     gpio_help(SHOW_SIGNALS);
                     state = PARSE_EXIT;
                  }
                  else {
                     state = PARSE_ERROR;
                  }
	               break;

	            case 'S':
	               state = (strcmp(ptr, "SET") ? PARSE_ERROR : PARSE_SIGNAL);
	               cmd = SIGNAL_SET;
	               break;

               default:
	               state = PARSE_ERROR;
	               break;
	         }
            break;

        	case PARSE_SIGNAL:
            max = gpio_device[gpio_index].io_count;
	         if (cmd == SIGNAL_GET && !strcmp(ptr, "ALL"))
	         {
               // Get all inputs
               request_gpio_read(gpio_device[gpio_index].node_index,
                                   0, max - 1);
               state = PARSE_EXIT;
               break;
            }
            else {
               for (i = 0; i < max; i++)
               {
                  if (!strcmp(ptr, gpio_signal[i].name)) {
                     signal = i;
                     break;
                  }
               }
               if (i < max) {
	               if (gpio_signal[i].type & XBEE_GPIO_MASK_TYPE_INPUT) {
	                  if (cmd == SIGNAL_SET) {
	                     printf("\nCannot set value to an input signal.\n");
	                     state = PARSE_EXIT;
	                  }
	                  else {
	                     state = PARSE_DONE;
	                  }
	               }
	               else {
	                  if (cmd == SIGNAL_SET) {
                        state = PARSE_VALUE;
                     }
	                  else {
	                     state = PARSE_DONE;
	                  }
	               }
               }
               else {
                  printf("\nSignal '%s' not found on current node.\n", ptr);
                  state = PARSE_EXIT;
               }
            }
            break;

	      case PARSE_VALUE:
            type = gpio_signal[i].type;
            if (type == XBEE_GPIO_TYPE_ANALOG_OUT) {
               if (isdigit(*ptr) || *ptr == '-') {
	               fp_value = atof(ptr);
   	            state = PARSE_DONE;
	            }
	            else {
                  state = PARSE_ERROR;
	            }
            }
            else {
               state = PARSE_DONE;
               switch (*ptr)
               {
                  case 'H': // "H", "HI", "HIGH"
                  case '1': // "1"
                     if (*(ptr + 1) != 0 &&  strcmp(ptr, "HI")
                     	 && strcmp(ptr, "HIGH")) {
                        state = PARSE_ERROR;
                     }
                     else {
                     	value = 1;
                     }
							break;

                  case 'L': // "L", "LO", "LOW"
                  case '0': // "0"
                     if (*(ptr + 1) != 0 &&  strcmp(ptr, "LO")
                     	 && strcmp(ptr, "LOW")) {
                        state = PARSE_ERROR;
                     }
                     else {
                     	value = 0;
                     }
							break;

                  case 'O': // "ON", "OFF"
                     if (!strcmp(ptr, "ON")) {
                        value = (type == XBEE_GPIO_TYPE_SINK_OUT ? 0 : 1);
                     }
                     else {
                        if (!strcmp(ptr, "OFF")) {
                           value = ((type == XBEE_GPIO_TYPE_DIGITAL_OUT ||
                                    type == XBEE_GPIO_TYPE_LED_OUT) ? 0 : 2);
                        }
                        else {
                           state = PARSE_ERROR;
                        }
                     }
                     break;

                  case 'T': // "T", "TRI"
                  case '2': // "2"
                     if (*(ptr + 1) != 0 && strcmp(ptr, "TRI")) {
                        state = PARSE_ERROR;
                     }
                     value = 2;
                     break;
	               default:
	                  state = PARSE_ERROR;
	                  break;
               }
            }
            break;

         default:
            state = PARSE_ERROR;
            break;
      }

      if (state >= PARSE_DONE) {
         if (state == PARSE_DONE) {
	         if (cmd == SIGNAL_GET) {
	            request_gpio_read(gpio_device[gpio_index].node_index,
                                     signal, signal);
	         }
	         else {
	            if (cmd == SIGNAL_SET) {
                  if (gpio_signal[i].type == XBEE_GPIO_TYPE_ANALOG_OUT) {
	                  gpio_write_analog(gpio_device[gpio_index].node_index,
                                         signal, fp_value);
                  }
                  else {
	                  gpio_write_digital(gpio_device[gpio_index].node_index,
                                       signal, value);
                  }
	            }
               else {
                  printf("G> ");
               }
	         }
         }
         else {
            if (state == PARSE_ERROR) {
               printf("\nInvalid command string.\n\nG> ");
            }
            else {
               if (gpio_device[gpio_index].state == CLIENT_STATE_IDLE) {
	               printf("\nG> ");
               }
            }
         }
         break;
      }
   }
   if (state < PARSE_DONE) {
      printf("\nInvalid command string.\n\nG> ");
   }
   ptr = buf;
   return 1;
}


void main(void)
{
	int i, state, tickres;
	unsigned long now;
	int initres;	// Result of xbee_init()
	unsigned long join_start;
	int c;

	printf( "Starting XBee GPIO Client....\n\n\r");

	// *** Initialization ***

	// Initialize the radio portion of the board
	join_start = 0;
	while ( (initres = xbee_init()) == -EBUSY)
	{
		if (! join_start)
		{
	      join_start = SEC_TIMER;
	      printf( "Waiting for sleeping XBee to wake before continuing.");
		}
      if (join_start != SEC_TIMER)
      {
         join_start = SEC_TIMER;
         printf( ".");
      }
	}
	printf( "\n");

	if (initres)
	{
		printf( "xbee_init failed.  result code: %d (%ls)\n",
				initres, error_message(initres));
		exit( initres);
	}
	// Join the network.  For more information on ZB_JOINING_NETWORK,
	// perform a function lookup (ctrl-H) on ZB_LAST_STATUS().
	printf( "Waiting to join network...\n");
	join_start = MS_TIMER;
	while (ZB_JOINING_NETWORK())
	{
		// If unable to join a network, timeout after arbitrary time
		if (MS_TIMER - join_start > XBEE_JOIN_TIMEOUT)
		{
	      // There was an error, do something about it
	      printf( "\n*** Error ***\n");
	      printf( "Timed out while trying to join a network, halting.\n");
	      exit( -ETIME);
	   }
	}
	printf( "Done (%s network)\n", xbee_protocol());
	// Wait for node discovery (6-7 seconds, can do something else between calls
	printf( "Waiting for node discovery...\n");
	while (ZB_ND_RUNNING());
	printf( "Discovery done.\n\n");
	// *** End Initialization ***

   // Scan network nodes for GPIO servers (5+ seconds)
	find_gpio_servers();
   gpio_last = gpio_index;
   if (!gpio_index) {
      printf("\nNo GPIO servers found on the network.\n\n");
   }
   gpio_help(SHOW_NET_HELP);
   parse_cmd(-1);  // Initialize command parser

	for (state = ZB_IDLE, gpio_index = 0; state != ZB_EXIT; )
	{
      if (!gpio_last || gpio_device[gpio_index].state == CLIENT_STATE_IDLE)
      {
	      if (kbhit()) {
	         c = getchar();
	         if (state == ZB_IDLE) {
               putchar(c);
	            if (c >= '0' && c <= '9') {
                  c -= '0';
                  if (c < gpio_last) {
	                  state = gpio_open_node(c);
	                  if (state == ZB_IDLE) {
	                     printf("\nN> ");
	                  }
                  }
                  else {
                     printf("\n\nGPIO #%d is not valid.\n\nN> ", c);
                  }
	            }
               else switch (c) {
	               case 'h':
	               case 'H':
                     gpio_help(SHOW_NET_HELP);
                     break;
	               case 'x':
	               case 'X':
                     state = ZB_EXIT;
                     break;
	               case 'd':
	               case 'D':
	                  printf("\nStarting node discovery...\n");
	                  _zb_begin_service_discovery();
	                  while (ZB_ND_RUNNING());
	                  printf( "Discovery done.\n");
	                  // fall through to "list nodes"
	               case 'n':
	               case 'N':
	                  find_gpio_servers();
                     gpio_last = gpio_index;
	                  if (!gpio_index) {
	                     printf( "\nNo GPIO servers found.\n");
	                  }
                     // fall through to show prompt
                  default:
                     printf("\nN> ");
                     break;
	            }
	         }
	         else {
	            // Parse input character
	            if (!parse_cmd(c)) {
	               state = ZB_IDLE;
	            }
	         }
	      }
      }
      else {
         query_gpio_node();
      }

		// Let the library advance its state
      tickres = zb_tick();
#ifdef GPIO_VERBOSE
	   if (tickres == ZB_MESSAGE)
	   {
			printf( "Unknown message arrived\n");
			xb_hexdump( ZB_LAST_MSG_DATA(), ZB_LAST_MSG_DATALEN() );
		}
      else if (tickres == ZB_RADIO_STAT)
		{
         printf( "Radio status change: %s\n", ZB_LAST_STATUS() == ZB_JOINED ?
				"joined" : "unjoined");
		}
      else if (tickres == ZB_MSG_STAT)
		{
			printf( "Transmission Status received: %d\n", ZB_XMIT_STATUS());
		}
#endif
	}
} //main()


⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -