Announcement

Collapse
No announcement yet.

Neatly formatted append/write to a .txt file on uSD from keyboard output

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Neatly formatted append/write to a .txt file on uSD from keyboard output

    So I am trying to append/write a slew of parameters at the time of inputting them from a keypad on the Gen4-uLCD-35DT to a .txt file on the uSD card.
    I'm trying to create a neat format so I would like each parameter to be on a newline.
    What I have currently looks like:

    func writeuSD (var n)
    str_GetW(&i, &d) ;
    hndl1 := file_Open("Config.txt", 'a');
    to (string); print(d);
    res := file_Seek(hndl1, 0x0000, 0x0005*n ) ;
    res := file_PutS(string, hndl1);
    res := file_Seek(hndl1, 0, 0) ;
    res := file_Close(hndl1) ;
    endfunc

    where 'i' is the keyboard response that I am then converting to decimal format. I set the argument 'n' being passed based upon which button was previously pressed.
    'N' can be anywhere between 0 and 150 or so as I have around 150 different parameters being set.
    I'll probably add a null terminator in there for the peripheral MCU that will be reading this file from the uSD as well.

    My problem is I can't really figure out how to correctly format the code and the .txt file such that each parameter is on a newline.
    A single parameter usually doesn't have consistent significant figures. For instance, the area factor can be entered in anywhere from 0.001 up to 999.999

    Is there a way to achieve this? I also don't really have a clear understanding of the hiword and loword for file_seek either.

    Regards,

    Justin

  • #2
    Hi Justin,

    Are you calling the function for every character typed? Should it suffice to open and write on the memory card once a particular button has been pressed (e.g. Enter)?

    My problem is I can't really figure out how to correctly format the code and the .txt file such that each parameter is on a newline.
    You could add a 'line feed' (0x0A) to the string.

    from 0.001 up to 999.999
    Since you mentioned that you have a range of up to 7 figures, you can allocate 10 bytes on each line (line feed included). If the figure you input has less than 10 characters, you can add a 'null' / 'space' / 'dummy' bytes to fill the remaining characters.

    You could do it like this:
    Code:
    else if (value == '*') // 'enter'
            hndl1 := file_Open("Config.txt", 'a');
            respchp := str_Ptr(Response);
            len:=str_Length(respchp); // count the length of the string
            if(len < 10)
                for(i:=len; i<=8; i++)
                    str_PutByte(respchp + i, ' ') ; //insert 'space'
                next
                str_PutByte(respchp + 9,'\n'); //10th byte will be line feed
            endif
    
            file_PutS(Response, hndl1);
            file_Close(hndl1);
    I also don't really have a clear understanding of the hiword and loword for file_seek either.
    You can use the File_Seek command to set the file pointer to a specific location. The hiword contains the upper 16bits of the memory pointer, and the loword
    contains the lower 16bites of the memory pointer. This is referenced from Diablo16 Internal Functions, the description of the command also has
    an example to demonstrate its use.

    I hope this helps.
    Best Regards,
    Kevin
    Last edited by John Kevin; 1 week ago.

    Comment


    • #3
      Hi Kevin,
      Thanks for the response.

      Yes, you are correct that function is being called once 'enter' is pressed.

      I should have clarified a little better. What I had meant to say is that I also want to amend previous row information.
      So originally, the config.txt file might look like

      00000
      00000
      00000
      00000
      00000
      00000

      and then once I set a new parameter to 99 where n:=3, the config.txt would look something like this:

      00000
      00000
      99
      00000
      00000
      00000

      Regards,

      Justin

      Comment


      • #4
        Correction, your code with my function is working!
        I wasn't taking into account that the newline was a character as well. The str_Length bit and fillers to make the file_seek work correctly was exactly what I needed.
        Many thanks. In case this helps someone in the future, here is the relevant snippets

        case(42) : // 'Enter' is pressed
        txt_Width(1); //reset text multiplication so strings on other pages don't change size
        txt_Height(1);
        var len ;
        var j ;

        i := str_Ptr(Response) ;
        len := str_Length(i) ;
        if(len < 10)
        for (j:=len; j<=8 ; j++)
        str_PutByte(i + j, ' ') ;
        next
        str_PutByte(i + 9, '\n') ;
        endif

        switch



        /*----- Form 20 Variables Operating Range Page ---------*/

        case(S1_AFactf == 1)
        n := 0 ;
        S1_AFactf := 0 ;
        writeuSD(n) ;
        ActivateForm(20) ;
        break ;

        case(S1_Op_Rangef == 1)
        n := 1 ;
        S1_Op_Rangef := 0 ;
        writeuSD(n) ;
        ActivateForm(20) ;
        break ;
        endswitch


        // in magicode 0 (constant/global/data)
        func writeuSD (var n)

        str_GetW(&i, &d) ;
        hndl1 := file_Open("Config.txt", 'a');
        to (string); print(d);
        res := file_Seek(hndl1, 0x0000, 0x0006*n ) ; //changed from 0x05 to 0x06 to account for newline
        res := file_PutS(string, hndl1);
        res := file_Close(hndl1);
        n := 0 ;
        i := 0 ;
        endfunc

        Regards,

        Justin

        Comment


        • #5
          Hi Justin,

          I am glad it helped you. Thank you for sharing your example code and good luck with your project.
          If you still have any concerns, please don't hesitate to ask.

          Best Regards,
          Kevin

          Comment

          Working...
          X