From: Daniel Giritzer Date: Sat, 3 Jun 2017 16:28:49 +0000 (+0200) Subject: Switched to getopt for parsing cmd parameters, preparations for 1.1 release X-Git-Url: https://code.nwrk.biz/?p=allwinner_tvout_manipulator.git;a=commitdiff_plain;h=d97ced9ef7befe8a2a91bd1002dd2e5f8a1f49b7;ds=sidebyside Switched to getopt for parsing cmd parameters, preparations for 1.1 release --- diff --git a/src/main.c b/src/main.c index 94010f9..cad49bc 100644 --- a/src/main.c +++ b/src/main.c @@ -10,35 +10,86 @@ /////////////////////////////////////////////////////////////////////////// #include #include +#include +#include #include "devmem2.h" #include "tvout.h" +void PrintUsageMessage(FILE* output) +{ + fprintf(output, "++++++++++++++++++++++++++++++\n"); + fprintf(output, "Usage: tvout [-m [-x val] [-y val]] \n\n"); + fprintf(output, "-m \t move \n"); + fprintf(output, "-x \t vertical offset in px \n"); + fprintf(output, "-x \t horizontal offset in px \n"); + fprintf(output, "\nThis program was created for the Armbian Project.\n"); + fprintf(output, "(c) 2017, Daniel G.\n"); + fprintf(output, "++++++++++++++++++++++++++++++\n"); +} int main(int argc, char **argv) { - unsigned long to_write = 0; - + bool mFlag = false; + bool xFlag = false; + char* xVal = 0; + bool yFlag = false; + char* yVal = 0; + char c; - //Print usage message - if(argc != 3) + if(argc < 2) { - fprintf(stdout, "++++++++++++++++++++++++++++++\n"); - fprintf(stdout, "Usage: tvout \n"); - fprintf(stdout, "The parameter values should be in px.\n\n"); - fprintf(stdout, "This program was created for the Armbian Project.\n"); - fprintf(stdout, "(c) 2017, Daniel G.\n"); - fprintf(stdout, "++++++++++++++++++++++++++++++\n"); - return EXIT_SUCCESS; + PrintUsageMessage(stdout); } - //Set X value - to_write += strtoul(argv[1], 0, 0) << X_REG_OFFSET; + // Use getopt to parse the cmd parameters + while ((c = getopt (argc, argv, "mx:y:?")) != -1) + switch (c) + { + case 'm': + mFlag = true; + break; + case 'x': + xFlag = true; + xVal = optarg; + break; + case 'y': + yFlag = true; + yVal = optarg; + break; + case '?': + //Print usage message + PrintUsageMessage(stdout); + return EXIT_FAILURE; + default: + abort (); + } - //Set Y value - to_write += strtoul(argv[2], 0, 0) << Y_REG_OFFSET; - //write to register - writemem(TV_ENCODER_RESYNC, to_write, 'w'); + //check if m flag was set + if(mFlag) + { + unsigned long to_write = 0; + + // read current position + readmem(TV_ENCODER_RESYNC, 'w', &to_write); + if(xFlag) + { + //Set new X value + to_write += strtoul(xVal, 0, 0) << X_REG_OFFSET; + } + if(yFlag) + { + //Set new Y value + to_write += strtoul(yVal, 0, 0) << Y_REG_OFFSET; + } + //write new position to register + writemem(TV_ENCODER_RESYNC, to_write, 'w'); + } + + for (unsigned int index = optind; index < argc; index++) + { + printf ("Non-option argument %s\n", argv[index]); + } return EXIT_SUCCESS; } diff --git a/src/tests/devmem2_test.c b/src/tests/devmem2_test.c index b6c0626..14f149b 100644 --- a/src/tests/devmem2_test.c +++ b/src/tests/devmem2_test.c @@ -16,10 +16,10 @@ int main() unsigned long readfrommemory = 0; //Testing writemem - writemem("0x01E00130", "0x10140025", 'w'); + writemem(0x01E00130, 0x10140025, 'w'); //Testing readmem - readmem("0x01E00130", 'w', &readfrommemory); + readmem(0x01E00130, 'w', &readfrommemory); //Print read value to stdout printf("Value: 0x%X \n", (unsigned int)readfrommemory);