Shop OBEX P1 Docs P2 Docs Learn Events
Need advice on eetest2 experiment — Parallax Forums

Need advice on eetest2 experiment

RsadeikaRsadeika Posts: 3,837
edited 2012-12-15 04:11 in Propeller 1
I guess what I need are some ideas as to how to "run" a file that is created with echo() command. I will be using the QuickStart board, so that is why I will be working with eetest2(filetest.c that uses the EEPROM) for the start of this project. Basically what I would like to accomplish is to create a batch file of sorts that will run the commands that are listed in the file, so for example:
run flashLED.bat
And flashLED.bat would contain:
high(16)
sleep(4)
low(16)

Of course this will be done in C, using eetest2 as example code. I was looking at Cat() command, trying to get an idea as to how I would change that around, so it would just execute the lines as opposed to printing them on the screen, nothing seems to be jumping out at me at the moment. I guess I need a nudge in the right direction.

Thanks
Ray

Comments

  • Dave HeinDave Hein Posts: 6,347
    edited 2012-12-10 09:41
    You could create a "cmdin" FILE pointer that is initialized to stdin, and is used it to read command lines. When you run the "run" command it will open the batch file, and set "cmdin" to point to that file. When you hit the end of the batch file you would then restore "cmdin" to stdin.

    As far as executing high, sleep and low it would be easier if they worked like the other commands. That is, flashLED.bat would look like:
    high 16
    sleep 4
    low 16
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-12-12 06:16
    Below is my attempt at creating a "run" command, unfortunately it is not working as expected. I have a test.bat that contains high(22), for a simple action. The only thing that is occurring is that the contents gets printed to the screen, but the actual command is not being run. I thought by having a buff2[40], it would contain the contents of test.bat, and then it would be run when it got to the buff2; line. When I have the debug line printf("%s\n",buff2); it prints a blank line. So, any ideas as to how I would make this work correctly?

    Ray

    void Run(int argc, char **argv)
    {
        int i;
        int num;
        void *infile;
        uint8_t buffer[40];
        uint8_t buff2[40];
        int j;
    
        for (i = 0; i < argc; i++)
        {
                    infile = stdin;
                infile = fopen(argv[i], "r");
            if (infile == stdin)
            {
                while (gets(buffer))
                {
                    if (buffer[0] == 4) break;
                    fprintf(stdoutfile, "%s\n", buffer);
                    buff2[j] = buffer + j++;  //Capture the line in test.bat?
                }
            }
            else
            {
                while ((num = fread(buffer, 1, 40, infile)))
                    fwrite(buffer, 1, num, stdoutfile);
            }
           
            
            if (i)
                fclose(infile);
    
        }
        
        fflush(stdout);
            //printf("%s\n",buff2);
            //fflush(stdout);
            buff2;        //Run the command?
        
    }
    
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-12-12 07:01
    There are quite a few problems here. "buff2[j] = buffer + j++;" wil store the least significant byte of the address of the j-th element of buffer in the j+1 element of buff2. I don't think that's what you want to do. Also j isn't even initialized to any particular value. You would need a pointer initialized to buff2, and then increment that ptr by the length of the string that gets added to it. Also, you either neither to do a strcpy(buff2_ptr, buffer) or just read directly into buff2_ptr.

    The last line of the Run routine is just "buff2". All that does is get the address of buff2. Does that compile? The compiler should be issuing a warning on that at the very least. What you really need to do is loop on the strings in buff2, and then you need to parse them, and then you need to call some function to execute the various commands.

    I think I can quickly modify the filetest program to do what you want to do. I take a look at it later today.
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-12-12 07:30
    The last line of the Run routine is just "buff2". All that does is get the address of buff2. Does that compile?
    Yes, it did compile, I did not notice any warnings either.

    My thought process on this was, trying to get it down to the simplest form. I started with:
    Run(...)
    {
    high(22);
    }
    which ran correctly, it lit the LED. So, if I were to capture high(22) in the buff2, and then have buff2, in the code, it would act just like high(22)? Which I guess is not the correct way of thinking about this? I guess that if buff2 is just an address, then of course it will not run the command. I guess I have to think about this some more.

    Ray
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-12-12 08:13
    C does not have a standard way to execute a C statement contained in a string. Some languages can do this, but they are mostly interpretive languages. So you will need to build your own interpreter. This is basically what filetest is. It searches through a list of commands to find a match for the first token in a command line, and passes a list of command-line token to the function that is associated with that command string. The functions then interpret the rest of the tokens looking for options and filenames.

    So the easier way to implement this would be to make your batch file commands conform to the same syntax. So "high(22)" would be "high 22", which would then call the "High" function, which would convert argv[1] to a number and call the high function with that number.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-12-12 08:33
    I added a run command to my copy of filetest.c. Here's a sample of a test run I did. The modified file is attached below. Look for "cmdinfile" to see my changes.

    EDIT: There should be an "fclose(cmdfilein)" after hitting the end-of-file on the batch file. You would also need to close the current batch file if you allow calling another batch file. Of course, if you call another batch file none of the lines after that will be executed in the original batch file unless you put the file handles on a queue.
    Commands are run, help, cat, rm, ls, ll, echo, cd, pwd, mkdir and rmdir
    
    > cat >test.bat
    echo Hello from test.bat
    echo This is test.bat
    cat test.bat
    echo Goodbye from test.bat
    
    
    > run test.bat
    Hello from test.bat
    
    This is test.bat
    
    echo Hello from test.bat
    echo This is test.bat
    cat test.bat
    echo Goodbye from test.bat
    Goodbye from test.bat
    
    
    > 
    
    c
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-12-12 13:08
    I have attached a very diluted eetest2, for test purposes, and code review. I have three specific commands for testing a batch/script file: ledon, ledoff, and wait. The way it works is, create the .bat file:
    echo ledon 22 > test.bat
    echo wait 5000 >> test.bat
    echo ledoff 22 >> test.bat
    This sequence adds the commands to the test.bat file. The next step is to run the .bat file:
    run test.bat
    After running the test.bat file it does, in fact work, as expected. At least, with Dave Hein's help, I am seeing some satisfactory results.

    This is a very crude and simplistic scripting attempt, now what it needs is a very crude and simplistic editor or some means by which you can edit the .bat file. I think it would get very tiresome if you had to keep making a new .bat file using the echo way. The other problem is, when you run the .bat file, it runs in the foreground, you have to wait the five seconds before you can get the keyboard back. I guess this could be taken care of by using some pthreads in the program.

    If somebody has some ideas for improving the code, I would appreciate the ideas.

    Ray
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-12-14 08:29
    After giving the latest code some more thought about running the batch/script file in the background, I guess I need some ideas. A few months ago I played around with the idea of using pthreads or cognew with the filetest.c program, the problems:
    a, If you use pthreads, I could not find a solution for getting around the gets() problem, lack of C knowledge on this one.
    b, If you use cognew, then you are locked into CMM. The use of cognew, the way I see it, will not run in XMM, and therefore you are locked into CMM limits.

    The reason I am experimenting with this is, I thought it would be great to have a batch/script facility for easy manipulation of a boebot, or other such device(s). I was thinking of coming up with some suitable commands to control the motion of the robot for a batch/script file coordinated session, or movement of the device. Anybody have some thoughts on this?

    Ray
  • jazzedjazzed Posts: 11,803
    edited 2012-12-14 09:10
    Rsadeika wrote: »
    a, If you use pthreads, I could not find a solution for getting around the gets() problem, lack of C knowledge on this one.

    The problem is whether or not a function "blocks" other threads from running. Generally pthreads is an advanced subject that is made available for those who know how to use them.
    Rsadeika wrote: »
    b, If you use cognew, then you are locked into CMM. The use of cognew, the way I see it, will not run in XMM, and therefore you are locked into CMM limits.

    This is a misunderstanding. XMM/XMMC programs must copy code from the external storage to HUB space before doing cognew. I have several projects that do this.
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-12-14 11:23
    ... I have several projects that do this.
    I looked at your toggle examples and did not recognize, or maybe I missed it, example for using cognew within XMM/XMMC. Not to be provocative jazzed, since you are the only one that responded then maybe you are the only one that knows how to do this.

    I am probably going to go with the cognew concept and just stick with the CMM constraint, maybe it will force me to create some very tight code.

    Just to remind everybody, any original code that I come up with, is not MIT or Public domain, it is just Open Source, no license, if you have any use for it, use it.

    Ray
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-12-14 12:29
    But the original filetest.c code is under MIT license. So in theory, that license needs to be included in any derivations from it. Of course, your own original code can include any kind of license, or none at all. However, that code should be in a separate file so it doesn't include code from filetest.c.
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-12-14 13:22
    Thanks Dave for all your help, I will not go any further with this project. All the other programs that I have, that contain any of your code will be scrubbed. Not sure how I will approach my next project, since I have viewed a lot of your code, and I do not want to use any kind of license scheme, that puts me in a very uncomfortable predicament. I guess that goes for any code that I have viewed here, and somebody could claim that I have no original code. It has been fun.
    Again, thanks for all your help.

    Ray
  • jazzedjazzed Posts: 11,803
    edited 2012-12-14 13:32
    Rsadeika wrote: »
    I looked at your toggle examples and did not recognize, or maybe I missed it, example for using cognew within XMM/XMMC.

    Fair enough. Maybe it's not so clear because it is not in the xmm example ....

    https://code.google.com/p/propgcc/source/browse/demos/toggle/cog_c_toggle/toggle.c

    Look at the start function. Looks like the pasm_toggle example does not have it.

    When you combine code in an original way, the work you did is most certainly yours. I'm sorry that things have to get weird around here sometimes (not a statement about Dave's license or otherwise). Almost all code in propeller-gcc demos is Copyright Parallax, and they want you to use it to your advantage.
  • Dave HeinDave Hein Posts: 6,347
    edited 2012-12-14 14:14
    Ray, I think you misundstood my comment. The code that you generate is yours, and you don't need to put any license on it if you don't want to. filetest.c was written by me, but I assigned the rights to Parallax since it was done under the PropGCC project. The MIT license is about as free as you can get. It only requires including the license text in the code.

    Please do not scrub or remove your code from the forum. I have found it extemely helpful.
  • RsadeikaRsadeika Posts: 3,837
    edited 2012-12-15 04:11
    Please do not scrub or remove your code from the forum. I have found it extemely helpful.
    I do not have or have had any intention of removing any code from the forum, what has been posted will remain intact. I was only reacting to a thread that was calling to have all forum code licensed. I guess I am, as one poster suggested, a purist when interpreting the meaning of "Open Software". After thinking about it, I will add this line to future posted code: "Use this software at your own risk.", just to make sure their is no misunderstanding, in what the code is supposed to, or implied to do. Also, I will probably not be posting any code in the OBEX. On a final note, I agree with Bill's decision about the licensing of his code.

    I have decided to continue with this project, after putting in my order for a boebot from Parallax. Since I have a PropBoe, that will be put on the robot, along with an Xbee module, I feel my idea to control it remotely is a sound one. Now I just have to work out the new code, with no license attached, just my new statement. So, for now, I have to wait for my boebot, before any serious code gets developed, and find some C examples as to how to control the servos so I can get the robot to move. Since the boebot will have "whiskers", and the wheel encoder, I guess I will need some C example code for that also. After thinking about what I just mentioned, I will probably need some super tight code, if I want it to fit in the CMM model.

    Ray
Sign In or Register to comment.