gustavonihei commented on code in PR #1211:
URL: 
https://github.com/apache/incubator-nuttx-apps/pull/1211#discussion_r907419232


##########
examples/neopixel/neopixel_main.c:
##########
@@ -0,0 +1,301 @@
+/****************************************************************************
+ * apps/examples/neopixel/neopixel_main.c
+ *
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.  The
+ * ASF licenses this file to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance with the
+ * License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
+ * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
+ * License for the specific language governing permissions and limitations
+ * under the License.
+ *
+ ****************************************************************************/
+
+/****************************************************************************
+ * Included Files
+ ****************************************************************************/
+
+#include <nuttx/config.h>
+
+#include <sys/types.h>
+#include <sys/ioctl.h>
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <debug.h>
+#include <string.h>
+#include <inttypes.h>
+
+#include <nuttx/leds/neopixel.h>
+
+/****************************************************************************
+ * Private Types
+ ****************************************************************************/
+
+struct neo_config_s
+{
+  FAR char  *path;
+  int        loops;
+  int        leds;
+  int        delay;
+};
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+struct neo_config_s config =
+{
+  .path      =   NULL,
+  .loops     =      4, /* all colors 4 times */
+  .leds      =      1,
+  .delay     =  20000  /* µs -- ~50Hz */
+};
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: help
+ ****************************************************************************/
+
+static void help(FAR struct neo_config_s  *conf)
+{
+  printf("Usage: neopixel [OPTIONS]\n");
+
+  printf("\nArguments are \"sticky\".  "
+         "For example, once the device path is\n");
+  printf("specified, that path will be re-used until it is changed.\n");
+
+  printf("  [-p path] selects the Neopixel device.  "
+         "Default: %s Current: %s\n",
+         CONFIG_EXAMPLE_NEOPIXEL_DEFAULT_DEV, conf->path ? conf->path
+                                                           : "NONE");
+
+  printf("  [-l leds] selects number of neopixels in the chain.  "
+         "Default: %d Current: %d\n",
+         1, conf->leds);
+
+  printf("  [-r repeat] selects the number change cycles.  "
+         "Default: %d Current: %d\n",
+         4, conf->loops);
+
+  printf("  [-d delay] selects delay between updates.  "
+         "Default: %d us Current: %d us\n",
+         20000, conf->delay);
+}
+
+/****************************************************************************
+ * Name: set_devpath
+ ****************************************************************************/
+
+static void set_devpath(FAR struct neo_config_s  *conf,
+                        FAR const char           *devpath)
+{
+  /* Get rid of any old device path */
+
+  if (conf->path != NULL)
+    {
+      free(conf->path);
+    }
+
+  /* Then set-up the new device path by copying the string */
+
+  conf->path = strdup(devpath);
+}
+
+/****************************************************************************
+ * Name: arg_string
+ ****************************************************************************/
+
+static int arg_string(FAR char **arg, FAR char **value)
+{
+  FAR char *ptr = *arg;
+
+  if (ptr[2] == '\0')
+    {
+      *value = arg[1];
+      return 2;
+    }
+  else
+    {
+      *value = &ptr[2];
+      return 1;
+    }
+}
+
+/****************************************************************************
+ * Name: arg_decimal
+ ****************************************************************************/
+
+static int arg_decimal(FAR char **arg, FAR long *value)
+{
+  FAR char *string;
+  int       ret;
+
+  ret = arg_string(arg, &string);
+  *value = strtol(string, NULL, 10);
+  return ret;
+}
+
+/****************************************************************************
+ * Name: parse_args
+ ****************************************************************************/
+
+static void parse_args(FAR struct neo_config_s  *conf,
+                       int argc,
+                       FAR char **argv)
+{
+  FAR char  *ptr;
+  FAR char  *str;
+  long       value;
+  int        index;
+  int        nargs;
+
+  for (index = 1; index < argc; )
+    {
+      ptr = argv[index];
+      if (ptr[0] != '-')
+        {
+          printf("Invalid options format: %s\n", ptr);
+          exit(0);
+        }
+
+      switch (ptr[1])
+        {
+          case 'l':
+            nargs = arg_decimal(&argv[index], &value);
+            if (value < 1 || value > INT_MAX)
+              {
+                printf("Led count out of range: %ld\n", value);
+                exit(1);
+              }
+
+            conf->leds = (uint32_t)value;
+            index += nargs;
+            break;
+
+          case 'r':
+            nargs = arg_decimal(&argv[index], &value);
+            if (value < 1 || value > INT_MAX)
+              {
+                printf("Repeat out of range: %ld\n", value);
+                exit(1);
+              }
+
+            conf->loops = (uint32_t)value;
+            index += nargs;
+            break;
+
+          case 'p':
+            nargs = arg_string(&argv[index], &str);
+            set_devpath(conf, str);
+            index += nargs;
+            break;
+
+          case 'd':
+            nargs = arg_decimal(&argv[index], &value);
+            if (value < 1 || value > INT_MAX)
+              {
+                printf("Delay out of range: %ld\n", value);
+                exit(1);
+              }
+
+            conf->delay = (int)value;
+            index += nargs;
+            break;
+
+          case 'h':
+            help(conf);
+            exit(0);
+
+          default:
+            printf("Unsupported option: %s\n", ptr);

Review Comment:
   ```suggestion
               fprintf(stderr, "Unsupported option: %s\n", ptr);
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to