Structured Array


Closed Thread
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386

    Default Structured Array

    I have been working on an idea for structured arrays with named elements eg
    say i have a battery charger that can charge multiple batteries
    each battery has these properties voltage[byte] current[word] pwm_drive[word] current_limit[word] status[byte]
    thats 8 bytes per battery
    ;define structure batteries for pbp
    struct_items con 10 ;10 index size ie number of batteries
    struct_sz_of con 8 ;total 8 bytes / index

    ;elements byte offset of each element
    volts CON 0;byte
    amps CON 1;word
    drive CON 3;word
    climit con 5;word
    stat con 7;byte
    reserve our memory
    battery var byte[ struct_items*struct_sz_of ]
    some user commands
    USERCOMMAND "ARRAY_GET" ;{array,}index,element{,var}
    USERCOMMAND "ARRAY_PUT" ;index,element,DATA

    we now have some nice commands ,j=byte test=word a_index=byte
    array_get 2,DRIVE
    array_put 2,DRIVE,1234
    array_get 3,AMPS
    a_index=1
    array_get a_index,volts,j
    array_get a_index,drive,test
    even multiple arrays
    array_get battery,a_index,volts,j
    this is a work in progress ,whats lacking is a way to tell the usercommand the "size" of our elements as they are accessed .
    at present the macros are customised to suit
    Attached Files Attached Files

  2. #2
    Join Date
    Sep 2009
    Posts
    737


    Did you find this post helpful? Yes | No

    Default Re: Structured Array

    I think that what you are trying to do is already done.
    http://www.picbasic.co.uk/forum/showthread.php?t=3891
    Look at Typcasting Variables inside of Arrays.
    MyArray holds all data. And you can access them by name eg Arg0, etc.
    So you could make usercommand to do this for you.

  3. #3
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: Structured Array

    I think that what you are trying to do is already done.
    not really

    you would need to name every element
    eg bat1-drive ,bat1-current.............
    bat2-drive..................
    thats 50 named vars
    and how can you step through them in a loop ?
    my way
    for i= 0 to 10
    array_get i,volts,j
    array_get i,amps,k
    array_get i,drive,l
    serout x,y,z ["in some nice format" , j, k, l,/n]
    next
    simple clean easy to read
    Last edited by richard; - 29th March 2016 at 11:23. Reason: typo again ,i can't even cut and paste

  4. #4
    Join Date
    Nov 2005
    Location
    Bombay, India
    Posts
    947


    Did you find this post helpful? Yes | No

    Default Re: Structured Array

    How about this way?
    Code:
    ' ==============================================================================
    '  One log record
      voltage       con     0                 ' byte
      amps          CON    voltage+1      ' word (amps is 1 byte away from voltage)
      drive          CON    amps+2         ' word (drive is 2 bytes away from amps)
      climit          con    drive+2          ' word
      stat           con     climit+2         ' byte
      szLog         con     stat+1
    DataLog         var     byte[szLog]   ' reserve the bytes for structure
    This takes care of your struct size and also its member offsets - akin to doing it in assembler. This is similar to what you have suggested.

  5. #5
    Join Date
    May 2013
    Location
    australia
    Posts
    2,386


    Did you find this post helpful? Yes | No

    Default Re: Structured Array

    Well it works. I previously had 5 arrays now everything is in one nice contiguous block ready to send to the data logger. if nothing else the code [excluding the user command bits] looks simpler and more readable.
    using the
    ================================================== ============================
    ' One log record
    voltage con 0 ' byte
    amps CON voltage+1 ' word (amps is 1 byte away from voltage)
    drive CON amps+2 ' word (drive is 2 bytes away from amps)
    climit con drive+2 ' word
    stat con climit+2 ' byte
    szLog con stat+1
    DataLog var byte[szLog] ' reserve the bytes for structure
    method and ext var declarations is fine up until you need to have a mixed array with bytes and word vars and more than One log record that you can step through to read and write in a loop.

    my method works fine but I think its only cosmetically better than in this case using 5 separate arrays.
    anyway its been good practice with macros and user command

    here is the demo code if your interested on a 16f1825


    printout

    ready v2
    fetched a word 5139
    fetched a byte 24
    fetched put word 1234
    fetched put byte 56
    array byte to any byte 8
    array word to any word 3083
    any array byte to any byte 44
    any array word to any word 11308
    array variable byte to any byte 8
    array variable word to any word 3083
    any array variable byte to any byte 44
    any array variable word to any word 11308
    Battery V A PWM
    0 0 0201 0403
    1 8 0A09 0C0B
    2 16 1211 04D2
    3 56 1A19 1C1B
    4 32 2221 2423
    5 40 2A29 2C2B
    6 48 3231 3433
    7 56 3A39 3C3B
    8 64 4241 4443
    9 72 4A49 4C4B
    Attached Files Attached Files
    Last edited by richard; - 30th March 2016 at 11:11. Reason: added printout

Similar Threads

  1. Copying Array to Array
    By vamtbrider in forum mel PIC BASIC Pro
    Replies: 4
    Last Post: - 24th April 2010, 02:12
  2. Array and IF - THEN
    By Gusse in forum mel PIC BASIC Pro
    Replies: 9
    Last Post: - 25th February 2010, 13:52
  3. Custom array of ports using array?
    By blainecf in forum mel PIC BASIC Pro
    Replies: 22
    Last Post: - 18th June 2006, 02:43
  4. Array
    By ripmax in forum mel PIC BASIC Pro
    Replies: 0
    Last Post: - 29th March 2006, 23:21
  5. Word array behaving like byte array.... wierd
    By forgie in forum mel PIC BASIC Pro
    Replies: 14
    Last Post: - 2nd August 2005, 16:43

Members who have read this thread : 1

You do not have permission to view the list of names.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts