Announcement

Collapse
No announcement yet.

How implement List in 4DGL

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

  • How implement List in 4DGL

    Hi everybody!!!

    I have a problem with my VISI program and if it's possible i need some advice!

    How can i implement a list in 4DGL?

    The list is composed of some entries consist of four fields. Each entries can be added or deleted!

    Thank you!

  • #2
    You will need to code some 4DGL routines to manage the lists.

    I would suggest 4 arrays, one for each field. Hopefully with the maximum number being 'fixed'.

    If the arrays are just integers then use mem_Copy to add and delete entries.

    If they are strings then use mem_alloc and mem_free to add and delete the strings and store the addresses of the elements in an array like the above
    Mark

    Comment


    • #3
      Hi Mark!!!

      sorry if i answer in late but i was very busy!!!

      Now with my project i arrived at a crucial point!!!!

      I create a setting page where i have six fields to be filled....... when i touch the fields, appears the keyboard that write into the filed!!...Now i need to understand how can i store the data that i introduce inside the fields!!!

      How can i implement this?

      Can i create six array ( one for every fields ) where i go to insert the data introduced inside the filed with the keyboard and subsequently i store the array inside the Display flash ( with mem_Copy ?)
      Or i store directly the data from the keyboard inside the memory? How can i organize the flash memory in this case?

      Or you have other idea?


      Other question: the mem_copy , mem_alloc, mem_free that you have mentioned above, are functions that works on the flash memory?

      Thank you!!!!
      Last edited by tntsei; 21 April 2015, 10:46 PM.

      Comment


      • #4
        All the flash (bank) functions start with flash_.

        So initially you store the data in RAM, then you use flash_WriteBlock() to copy sections of RAM to the desired flashbank.

        Depending what you have put in the bank and where you might use flash_putstr, flash_GetByte(), flash_GetWord(), or flash_Copy(), etc. I'd probably use flash_Copy() to copy a block back from the bank, so I could get all the saves stuff back in one hit.
        Mark

        Comment


        • #5
          Why you have mentioned above mem_Copy for integers and mem_alloc / mem_free for string? because are functions that works for RAM and not for Flash?

          Comment


          • #6
            I thought you were asking how to build and mange some lists in RAM, so I gave you those functions.

            Now you appear to be asking about saving values permanently in flash, so I must have misunderstood your question
            Mark

            Comment


            • #7
              Yes Mark.... i need to save everythings in Flash!!

              Comment


              • #8
                Hi Mark!

                I create an array which contains the 11 fields, and with flash_WriteBlock i stored in the page, how follow:

                Code:
                func store
                
                
                    var save[11];
                    var index;
                    var save_pointer;
                
                        index := 0;
                
                        // create struct
                        save[index++] := field0;
                        save[index++] := field1;
                        save[index++] := field2;
                        save[index++] := field3;
                        save[index++] := field4;
                        save[index++] := field5;
                        save[index++] := field6;
                        save[index++] := field7;
                        save[index++] := field8;
                        save[index++] := field9;
                        save[index++] := field10;
                      
                        save_pointer := str_Ptr(save);
                        flash_WriteBlock(save_pointer,FLASHBANK_1,1);
                
                endfunc
                in this way i think that i store the data in the page 1 of the flashbank1...it's correct?

                now if i want read this data by the flash i need to use the flash_Copy functions......how it works?

                In the data sheet says that the flash_Copy functions takes 4 arguments, including ptr that it's a pointer to a location inside the flashbanck selected, instead
                in the flash_writeBlock sourceptr it's the address of the source......
                Now if my data are stored in the page 1 of the flashbank_1, how can i give to the flash_Copy function the location for the page1?

                I tried how follow but it doesn't works!

                Code:
                func read()
                
                var read[11];
                var format;
                var pointer;
                
                pointer := str_Ptr(read);
                format := "%*s\n%*s\n%*s\n%*s\n%*s\n%*s\n%*s\n%*s\n%*s\n%*s\n%*s\n;
                
                flash_Copy(FLASHBANK1,1,pointer,11);
                
                str_Printf(&pointer,format);
                
                endfunc
                Could you give me same sample?
                Last edited by tntsei; 12 May 2015, 01:03 AM.

                Comment


                • #9
                  He tntsei,

                  Try this code:
                  Code:
                  #platform "uLCD-43DT"
                  
                  // main program run on bank 0
                  #MODE FLASHBANK_0
                  
                  var save[4];
                  var read[9];
                  var *ps, *pd;
                  var n;
                  
                  func main()
                      gfx_ScreenMode(LANDSCAPE) ; // change manually if orientation change
                  
                      // create struct on ram of bank 0
                      n := 0;
                      save[n++] := "abcdefghijklmnopqrstuvwxyz";    // 1   string
                      save[n++] := 10;                              // 2   decimal
                      save[n++] := 0xFF;                            // 3   hex
                      save[n++] := '\0';                            // 4   null
                  
                      // write struct to flash of bank 2 page 1
                      ps := str_Ptr(save);                                                       
                      if(flash_WriteBlock(ps, FLASHBANK_2, 1))
                          print("WRITE SUCCES\n\n");
                      else
                          print("WRITE FAILED\n\n");
                      endif
                  
                      // read struct from flash of bank 2 page 1
                      pd := str_Ptr(read);
                      if(flash_Copy(FLASHBANK_2, &ps, &pd, 4)>0)
                          print("READ SUCCES\n\n");
                          str_Printf(&pd, "%*s\n%d\n%x\n");
                      else
                          print("READ FAILED\n\n");
                      endif
                      repeat                      // maybe replace
                      forever                     // this as well
                  endfunc
                  Best Regards

                  Edha

                  Comment


                  • #10
                    Thank's Edha

                    Seems that writing for as i have done it's correct!!!

                    Two questions:

                    1) Why read it's array with 9 elements and no 4?

                    2) The flash_WriteBlock it's a functions that stored a buffer (in RAM ) in a page inside the FLASHBANK with max size 2kByte for every pages.
                    Now if the struct it's for example with 200 bytes size and i need to store another different struct with 300 bytes size, i'm forced to store the two different struct inside two different pages or it's possbile store the two struct inside the same pages in contiguos way? In the first case i lose 1800 bytes for a page ( 2000 - 200 = 1800 ) and 1700 for the second page ( 2000 - 300 = 1700 ).
                    Last edited by tntsei; 12 May 2015, 07:08 AM.

                    Comment


                    • #11
                      1. The read is in bytes, each element is 2 bytes (they are 16bit integers) so you need to read 2x the number of elements.
                      2. All variables are contiguous in the order declared, so you get a 'block' of data written, it will contain more than just 'save' variables, whether you chose to read everything back is up to you.
                      Note edha's string assignment simply puts the address of the string in the variable, the string is in program space.

                      Have a look at this example, hopefully it demonstrates the important bits.
                      Code:
                      #platform "uLCD-43DT"
                      
                      //
                      // Sample program to show saving and restoring variables from flash banks
                      //
                      
                      #inherit "4DGL_16bitColours.fnc"
                      
                      //var data[1024] ;      // could maintain everything in one array using defined offsets, this would ensure a 'clean' flash block
                      var mystr[14] ;
                      var a ;
                      var b[2] ;
                      // end of variables recovered with a read of 34 bytes
                      var c ;
                      
                      func dumpbank(var bnk, var block, var cnt)
                          var i, j ;
                          i := block * 2048 ;                        // each block is 2048 bytes, offset to it
                          for (j := 0; j < cnt; j++)
                              print([HEX2] flash_GetByte(bnk, i)," ") ;
                              if (!((i+1) % 16)) print("\n") ;
                              i++ ;
                          next
                          print("\n") ;
                      endfunc
                      
                      
                      func main()
                          var rc ;
                          print("\n") ;
                      //  flash_EraseBank(FLASHBANK_2,0) ;                // if necessary
                          to(mystr) ;
                          print("This is block 0") ;
                          a := 0x1000 ;
                          b[0] := 0x2000 ;
                          b[1] := 0x3000 ;
                          c := 0x4000 ;                                   // since 2048 bytes is saved this will include c
                          rc := flash_WriteBlock(mystr, FLASHBANK_2, 0);
                          if (!rc)
                              print("Write 1 failed!\n") ;
                              repeat forever
                          endif
                      
                          to(mystr) ;
                          print("This is block 1") ;
                          a := 0x1001 ;
                          b[0] := 0x2001 ;
                          b[1] := 0x3001 ;
                          c := 0x4001 ;                                   // since 2048 bytes is saved this will include c
                          rc := flash_WriteBlock(mystr, FLASHBANK_2, 1);
                          if (!rc)
                              print("Write 1 failed!\n") ;
                              repeat forever
                          endif
                      
                          print("String from Block 0: ") ;
                          flash_putstr(FLASHBANK_2, 0);           // demonstate printing strings directly from banks
                      
                          print("\nString from Block 1: ") ;
                          flash_putstr(FLASHBANK_2, 2048);        // and again, not the 2048 offset to the start of bank 1
                          print("\n") ;
                      
                          print("Dump of block 0\n") ;
                          dumpbank(FLASHBANK_2, 0, 48) ;          // note that 'c' 'and more' is also saved
                          print("Dump of block 1\n") ;
                          dumpbank(FLASHBANK_2, 1, 48) ;          // note that 'c' 'and more' is also saved
                      
                          c := 0xfefe ;
                      
                          if(flash_Copy(FLASHBANK_2, 0, mystr, 34)>0)
                              print("READ SUCCES\n\n");
                              print("String >", [STR] mystr, "< a=", [HEX4] a, " b[0]=", [HEX4] b[0],  " b[1]=", [HEX4] b[1], " c=", [HEX4] c, "\n") ;    // note that c is not touched
                          else
                              print("READ FAILED\n\n");
                          endif
                      
                          if(flash_Copy(FLASHBANK_2, 2048, mystr, 34)>0)
                              print("READ SUCCES\n\n");
                              print("String >", [STR] mystr, "< a=", [HEX4] a, " b[0]=", [HEX4] b[0],  " b[1]=", [HEX4] b[1], " c=", [HEX4] c, "\n") ;
                          else
                              print("READ FAILED\n\n");
                          endif
                      
                          repeat
                          forever
                      
                      endfunc
                      Mark

                      Comment


                      • #12
                        Hi Mark!

                        I have tested your code, and i tried to understand it!!

                        I have a questions:

                        This part of code:

                        Code:
                        to(mystr) ;
                            print("This is block 0") ;
                            a := 0x1000 ;
                            b[0] := 0x2000 ;
                            b[1] := 0x3000 ;
                            c := 0x4000 ;
                        Store the string and the variables a,b[0], b[1] and c inside the mystr array correct? I say it because when i display the dump bank functions, it's show the string in hex ( the ascii of every single char inside the string ), some 00 ( due to the 12 remaining element inside the array ) and after the a , b ,c variables ....and after the c variables there is some garbage...It's correct?

                        Comment


                        • #13
                          No, the array is separate, it's the declarations, assume mystr starts at ram location 0, thus you get something like this
                          Code:
                          var mystr[14] ; // at 0
                          var a ;         // at 28
                          var b[2] ;      // at 30
                          // end of variables recovered with a read of 34 bytes
                          var c ;         // at 34
                          So
                          Code:
                              flash_WriteBlock(mystr, FLASHBANK_2, 0);
                          will copy 2048 bytes starting at location 0 to flashbank 2
                          It doesn't 'stop' at the end of mystr, it just treats the memory as one chunk(block)
                          Mark

                          Comment


                          • #14
                            Now it's clear Mark!!!!!

                            Thank you

                            Comment


                            • #15
                              I have another problem

                              I understand how the functions works, but i have a problem with the flash_Copy functions....

                              If i try this code

                              Code:
                              #platform "uLCD-70DT"
                              
                              #inherit "4DGL_16bitColours.fnc"
                              
                              var struct[1024] ;
                              var read[1024];
                              
                              func dumpbank(var bnk, var block, var cnt)
                                  var i, j ;
                                  i := block * 2048 ;                        // each block is 2048 bytes, offset to it
                                  for (j := 0; j < cnt; j++)
                                      print([HEX2] flash_GetByte(bnk, i)," ") ;
                                      if (!((i+1) % 16)) print("\n") ;
                                      i++ ;
                                  next
                                  print("\n") ;
                              endfunc
                              
                              func main()
                              
                                  var rc ;
                                  var pointer;
                                  print("\n") ;
                              
                                  // string in struct
                                  struct[0] := "nome cliente\n" ;
                                  struct[1] := "nome prodotto\n" ;
                                  struct[2] := "ID Lotto\n" ;
                                  struct[3] := "Soglia Superiore\n";
                                  struct[4] := "Soglia Inferiore\n";
                                  struct[5] := "Numero Pacchi\n";
                                  struct[6] := "Soglia Pacchi\n";
                                  struct[7] := "pacchetti totali\n";
                                  struct[8] := "pacchetti scartati\n";
                                  struct[9] := "pacchi\n";
                                  struct[10] := "pacchi scartati\n";
                              
                                  // WRITE IN FLASH
                                  rc := flash_WriteBlock(struct, FLASHBANK_2, 0);
                                  if (!rc)
                                      print("Write 1 failed!\n") ;
                                      repeat forever
                                  endif
                                  print("\n") ;
                              
                                  print("Dump of block 0\n") ;
                                  dumpbank(FLASHBANK_2, 0, 48) ;
                              
                                  //READ BY FLASH
                                  pointer := str_Ptr(read);
                                  if( flash_Copy(FLASHBANK_2, 0 ,read, 22 ) > 0 )
                              
                                      str_Printf(&pointer,"%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s");
                              
                                  else
                              
                                      print("READ FAILED\n\n");
                              
                                  endif
                              
                                  repeat
                                  forever
                              
                              endfunc
                              works correctly......i store the struct in the flash and i'm able to read correctly!!

                              But if i try to read the data inside the flash after a write, it doesn't works

                              Code:
                              #platform "uLCD-70DT"
                              
                              #inherit "4DGL_16bitColours.fnc"
                              
                              var read[1024];
                              
                              func dumpbank(var bnk, var block, var cnt)
                                  var i, j ;
                                  i := block * 2048 ;                        // each block is 2048 bytes, offset to it
                                  for (j := 0; j < cnt; j++)
                                      print([HEX2] flash_GetByte(bnk, i)," ") ;
                                      if (!((i+1) % 16)) print("\n") ;
                                      i++ ;
                                  next
                                  print("\n") ;
                              endfunc
                              
                              func main()
                              
                                  var rc ;
                                  var pointer;
                                  print("\n") ;
                                  print("Dump of block 0\n") ;
                                  dumpbank(FLASHBANK_2, 0, 48) ;
                              
                                  //READ BY FLASH
                                  pointer := str_Ptr(read);
                                  if( flash_Copy(FLASHBANK_2, 0 ,read, 22 ) > 0 )
                              
                                      str_Printf(&pointer,"%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s%*s");
                              
                                  else
                              
                                      print("READ FAILED\n\n");
                              
                                  endif
                              
                                  repeat
                                  forever
                              
                              endfunc
                              In this way in the dumb function i read correctly the bytes of the previous case, but the strings are not pratically read!!

                              This happen always with the strings in the struct....if i try it with a number or char works!
                              Last edited by tntsei; 13 May 2015, 12:22 AM.

                              Comment

                              Working...
                              X