Add some command line options
authorAntonio Ospite <ospite@studenti.unina.it>
Fri, 6 Jan 2012 23:03:55 +0000 (00:03 +0100)
committerAntonio Ospite <ospite@studenti.unina.it>
Sat, 7 Jan 2012 00:59:32 +0000 (01:59 +0100)
This will make it easier to experiment with different image size and
format.

Makefile
picoproj.c

index 20b44c4..ed57b89 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -42,6 +42,7 @@ bindir := $(PREFIX)/sbin
 all: picoproj
 
 CFLAGS += -D_BSD_SOURCE # for htole32()
+CFLAGS += -D_POSIX_C_SOURCE=2 # for getopt()
 
 picoproj: picoproj.o
 
index 2629d02..197d5da 100644 (file)
@@ -22,6 +22,7 @@
 #include <string.h>
 #include <endian.h>
 #include <errno.h>
+#include <unistd.h>
 
 typedef enum {
        AM7x01_PACKET_TYPE_INIT    = 0x01,
@@ -199,15 +200,68 @@ static int send_image(am7x01_image_format format,
        return send_data(image, size);
 }
 
+static void usage(char *name)
+{
+       printf("usage: %s [OPTIONS]\n\n", name);
+       printf("OPTIONS:\n");
+       printf("\t-f <filename>\t\tthe image file to upload\n");
+       printf("\t-F <format>\t\tthe image format to use (default is JPEG).\n");
+       printf("\t\t\t\tSUPPORTED FORMATS:\n");
+       printf("\t\t\t\t\t1 - JPEG\n");
+       printf("\t-W <image width>\tthe width of the image to upload\n");
+       printf("\t-H <image height>\tthe height of the image to upload\n");
+       printf("\t-h \t\t\tthis help message\n");
+}
 
 int main(int argc, char *argv[])
 {
        int ret;
+       int opt;
+
+       char filename[FILENAME_MAX] = {0};
+       int format = AM7x01_IMAGE_FORMAT_JPEG;
+       int width = 800;
+       int height = 480;
+       uint8_t *image = NULL;
+       int size = 59475;
+
+       while ((opt = getopt(argc, argv, "f:F:W:H:h")) != -1) {
+               switch (opt) {
+               case 'f':
+                       strncpy(filename, optarg, FILENAME_MAX);
+                       break;
+               case 'F':
+                       format = atoi(optarg);
+                       if (format != 1) {
+                               fprintf(stderr, "Unsupported format\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       break;
+               case 'W':
+                       width = atoi(optarg);
+                       if (width < 0) {
+                               fprintf(stderr, "Unsupported width\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       break;
+               case 'H':
+                       height = atoi(optarg);
+                       if (height < 0) {
+                               fprintf(stderr, "Unsupported height\n");
+                               exit(EXIT_FAILURE);
+                       }
+                       break;
+               default: /* '?' */
+                       usage(argv[0]);
+                       exit(EXIT_FAILURE);
+               }
+       }
 
-       ret = send_image(AM7x01_IMAGE_FORMAT_JPEG, 800, 480, 59475);
+       ret = send_image(format, width, height, image, size);
        if (ret < 0) {
                perror("send_image");
                exit(EXIT_FAILURE);
        }
+
        exit(EXIT_SUCCESS);
 }