Arrays   Arrays are defined using an extension to VAR


Arrays  
Arrays are an efficient way to organise and access data. Providing a 4 dimensional capability allows users to store data
for multiple pages, each containing multiple objects with associated text and image entities.

A typical application would be a multi-page soft key menu system allowing user editing of key labels and images.
Multiple language support is easier to achieve rather than using pointers to a list of variables.

Arrays can be handled using a single name to simplify transfer to and from the host.
In this example a single command sends the array data to the RS2 port with STX, array length in 4 byte padded 0 Hex,
the contents of the array "PArray" then ETX.  Checksum can be added as required.
The length of the array will have been pre-defined during creation.

LOAD(RS2, "\\02", %H04%lenArray, %r%PArray, "\\03");

Arrays are defined using an extension to the VAR() command.
Each required dimension is passed as an additional parameter to the command.

VAR(name,init,type,size0); One-dimensional (or single dimension) array
VAR(name,init,type,size0,size1); Two-dimensional array
VAR(name,init,type,size0,size1,size2); Three-dimensional array
VAR(name,init,type,size0,size1,size2,size3); Four-dimensional array

Definitions
Array: A data structure consisting of a collection of elements (values), each identified by at least one index.
Index: A non-negative integer used to index a value in an array. Indices can also be called subscripts.
Element: A location in the array data structure which is used to store a value.
Dimension: The dimension of an array is the number of indices needed to select an element.

Indexing Arrays
Arrays use zero-based indexing, i.e. the first element of the array is indexed by 0.
For example, we define a 20 element array as:
VAR( A, 0, U8, 20 );
Then the elements of the array are indexed A.0 through to A.19.
Array elements are accessed by separating the indices with a dot.
Single dimension with A.0  through to 4 dimensions with name.idx0.idx1.idx2.idx3

Future Releases
Arrays currently accept numeric values and individual characters.  
The CALC() command currently only accepts single elements from an array, however it is intended to add the
ability to process a range on elements of an array in the future. If this is a critical requirement for your project then please email us.
Structures will be achieved using U8 arrays with the user defining U8,U16,U32, FLT, TXT entities and the TFT will take
care of handling each type according to its format.

Single Dimension Arrays
These are defined as
VAR( name, init, type, size0 );

For example, to create an 8 element array, named A, storing U8 data and initial values of 0,
VAR( A, 0, U8, 8 );

Accessing the elements of a single dimension array uses just a single subscript.
LOAD( ivar, A.4 );
LOAD( A.x, ivar );

As an addition to this, all elements of the array can be loaded with a single value.
LOAD( A, 45 );
LOAD( A, ivar );

The whole array can be passed to a serial port, text box, text variable or another array.
LOAD( rs2, A );
LOAD( tvar, A );
TEXT( txt, A );
LOAD( A1, A );
TEXT( txt, %h02%A );

When the array is passed, the elements are sent A.0 to A.7
The array can be loaded with the contents of a serial buffer.
LOAD( A, as1 );
A series of elements of an array can be loaded.
LOAD( A, 1, 2, 3, \\04, 5, ivar, 7, 8 );

The existing text variables are like a single dimension array of 32 characters when using style TXT.

Two-Dimensional Arrays
These are defined as
VAR( name, init, type, size0, size1 );

This can be pictured as a table with size0 rows and size1 columns.
For example, to create a 2 (row) by 32 (column) element array, named B, storing S16 data and initial values of 0,
VAR( B, 0, S16, 2, 32 );

This type of array could be used to store 32 points (x,y) for a graph, row 0 holds x values, row 1 holds y values.
Accessing the elements of a two-dimensional array uses two subscripts.
LOAD( ivar, B.1.12 );
LOAD( B.x.pt, ivar );

As an addition to this, all elements of the array can be loaded with a single value.
LOAD( B, 45 );
LOAD( B, ivar );

A single row can be loaded with a value by specifying only the first (row) subscript.
LOAD( B.1, ivar );

The whole array can be passed to a serial port, text box, text variable or another array.
LOAD( rs2, B );
LOAD( tvar, B );
TEXT( txt, B );
LOAD( B1, B );
TEXT( txt, %h02%B );
When the whole array is passed, the elements are sent a row at a time,
i.e. B.0.0, B.0.1, B.0.2, ..., B.0.31, B.1.0, B.1.1, B.1.2, ..., B.1.31

A single row can be passed by specifying only the first (row) subscript.
LOAD( rs2, B.0 );
When the single row is passed, the elements are sent B.0.0 to B.0.31

The array (or row of) can be loaded with the contents of a serial buffer.
LOAD( B, as1 );
LOAD( B.1, rs2 );

A series of elements of an array can be loaded.
LOAD( B.0, 1, 2, 3, \\04, 5, ivar, 7, 8 );

A graph can automatically be plotted when passed a two-dimensional array in the format B.2.n, where n is the
number of points and row 0 contains the x-values, row 1 the y-values.
DRAW( graph, B.0, B.1 );

Three-Dimensional Arrays
These are defined as
VAR( name, init, type, size0, size1, size2 );

This could be used to store data for three graphs each containing 50 (x,y) points.

For example, to create a 3 by 2 by 50 element array, named C, storing U8 data and initial values of 0,
VAR( C, 0, U8, 3, 2, 50 );

Accessing the elements of a three-dimensional array uses three subscripts.
LOAD( ivar, C.1.1.5 );
LOAD( C.g.x.pt, ivar );
As an addition to this, all elements of the array can be loaded with a single value.
LOAD( C, 45 );
LOAD( C, ivar );

A single dimension can be loaded with a value by specifying only the first two subscripts.
LOAD( C.1.0, ivar );
This loads C.1.0.0 to C.1.0.49 with ivar.

Two dimensions can be loaded with a value by specifying only the first subscript.
LOAD( C.1, ivar );
This loads C.1.0.0 to C.1.1.49 with ivar.

The whole array can be passed to a serial port, text box, text variable or another array.
LOAD( rs2, C );
LOAD( tvar, C );
TEXT( txt, C );
LOAD( B1, C );
TEXT( txt, %h02%C );

When the whole array is passed, the elements are sent as,
C.0.0.0, C.0.0.1, ..., C.0.0.49, C.0.1.0, ..., C.0.1.49, C.1.0.0, ..., C.1.0.49, C.1.1.0, ..., C.1.1.49, C.2.0.0, ..., C.2.0.49, C.2.1.0, ..., C.2.1.49

A single dimension can be passed by specifying only the first two subscripts.
LOAD( rs2, C.2.0 );

The elements are sent C.2.0.0 to C.2.0.49

Two dimensions can be passed by specifying only the first subscript.
LOAD( rs2, C.2 );
The elements are sent C.2.0.0 to C.2.0.49 then C.2.1.0 to C.2.1.49
The array (or dimension) can be loaded with the contents of a serial buffer.
LOAD( C, as1 );
LOAD( C.1, rs2 );
LOAD( C.1.1, i2c );

A series of elements of an array can be loaded.
LOAD( C.0.0, 1, 2, 3, \\04, 5, ivar, 7, 8 );

Graphs can automatically be plotted when passed a two-dimensional array in the format C.g.2.n,
where n is the number of points and row 0 contains the x-values, row 1 the y-values.
DRAW( graph0, C.0.0, C.0.1 );
DRAW( graph1, C.1.0, C.1.1 );
DRAW( graph2, C.2.0, C.2.1 );

Four-Dimensional Arrays - 47.24
These are defined as
VAR( name, init, type, size0, size1, size2, size3 );

For example, to create a 4 by 3 by 2 by 5 element array, named D, storing U8 data and initial values of 0,
VAR( D, 0, U8, 4, 3, 2, 5 );

This could be used to represent touch panel game with 4-rows by 3-columns of buttons where each button
has a text string (maximum length 5 characters including terminating character) for the button when it is 'up' and when is 'down'.
The player has to 'guess' where the "YES!" is.

This can be pictured as
Buttons Up Buttons Down
"1" "2" "3" "no" "no" "no"
"4" "5" "6" "no" "no" "YES!"
"7" "8" "9" "no" "no" "no"
"10" "11" "12" "no" "no" "no"

Accessing the elements of a four-dimensional array uses four subscripts.
LOAD( ivar, D.2.1.1.4 );
LOAD( D.r.c.u.s, ivar );

As an addition to this, all elements of the array can be loaded with a single value.
LOAD( D, 45 );
LOAD( D, ivar );

A single dimension can be loaded with a value by specifying the first three subscripts.
LOAD( D.1.0.0, ivar );
This loads D.1.0.0.0 to D.1.0.0.4 with ivar.

Two dimensions can be loaded with a value by specifying the first two subscripts.
LOAD( D.1.2, ivar );
This loads D.1.2.0.0 to D.1.2.1.4 with ivar.

Three dimensions can be loaded with a value by specifying only the first subscript.
LOAD( D.1, ivar );
This loads D.1.0.0.0 to D.1.2.1.4 with ivar.

The whole array can be passed to a serial port, text box, text variable or another array.
LOAD( rs2, D );
LOAD( tvar, D );
TEXT( txt, D );
LOAD( B1, D );
TEXT( txt, %h02%D );

When the whole array is passed, the elements are sent as,
D.0.0.0.0, D.0.0.0.1, ..., D.3.2.1.4
A single dimension can be passed by specifying the first three subscripts.
LOAD( rs2, D.1.2.0 );
TEXT( but1, %r%D.1.2.0 );
The elements are sent D.1.2.0.0 to D.1.2.0.4

Two dimensions can be passed by specifying the first two subscripts.
LOAD( rs2, D.2.1 );
The elements are sent D.2.1.0.0 to D.2.1.1.4

Three dimensions can be passed by specifying the first subscript.
LOAD( rs2, D.2 );
The elements are sent D.2.0.0.0 to D.2.2.1.4

The array (or dimension) can be loaded with the contents of a serial buffer.
LOAD( D, as1 );
LOAD( D.1, rs2 );
LOAD( D.1.1, i2c );
LOAD( D.1.1.1, rs4 );

A series of elements of an array can be loaded.
LOAD( D.0.0.0, 1, 2, 3, \\04, 0 );

Array with Text Usage - v49.00
This temporary solution enables text to be put in arrays using formatting %t%.
An intelligent text handling solution is being developed.

If we have the following variables:
VAR( A, \\ff, U8, 10 );
VAR( u8Var, 63, U8 );
VAR( txtVar1, "123XYZ", TXT );
VAR( txtVar2, "PQ", TXT );
VAR( txtVar3, "ABCDEFGHIJKLMN", TXT );

All examples assumed from initial declaration of array A

LOAD( A, 4 );               // A = {    4,    4,    4,    4,    4,    4,    4,    4,    4,    4 } <- fill array
LOAD( A.1, 3 );            // A = { \\ff,    3, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff } <- fill single location
LOAD( A, u8Var );        // A = {   63,   63,   63,   63,   63,   63,   63,   63,   63,   63 } <- fill array
LOAD( A.4, u8Var );     // A = { \\ff, \\ff, \\ff, \\ff,   63, \\ff, \\ff, \\ff, \\ff, \\ff } <- fill single location

LOAD( A, txtVar1 );     // A = {  123,  123,  123,  123,  123,  123,  123,  123,  123,  123 } <- fill whole array with number
LOAD( A.2, txtVar1 );  // A = { \\ff, \\ff,  123, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff } <- single destination specified
LOAD( A.2, txtVar2 );  // A = { \\ff, \\ff, \\00, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff } <- unable to convert to a number so result is zero

LOAD( A.1, %t% txtVar1 );  // A = { \\ff,  '1', \\ff, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff, \\ff } <- single destination specified, no padding applied
LOAD( A, %t% txtVar1, %t% txtVar2 );  // A = {  '1',  '2',  '3',  'X',  'Y',  'Z',  'P',  'Q', \\00, \\00 } <- concatenation with zero padding

LOAD( A, %t% txtVar1 );     // A = {'1','2','3','X','Y','Z',\\00,\\00,\\00,\\00}
                                                    <- text formatting is supplied, string is shorter than array dimension to pad with zeros

LOAD( A, %t% txtVar3 );    // A = {'A','B','C','D','E','F','G','H','I','J'} <- Longer than array, no terminator reqd as array dimension know
LOAD( A, %n% u8Var );  // A = {'6','3',\\00,\\00,\\00,\\00,\\00,\\00,\\00,\\00 } <- text formatter supplied, therefore store as text
LOAD( A, %H04% u8Var );  // A={'0','0','3','F',\\00,\\00, \\00, \\00, \\00, \\00 } <- text formatter supplied, therefore store as text

LOAD( A, "0x", %H04% u8Var ); // A = { \\00,  '0',  '0',  '3',  'F', \\00, \\00, \\00, \\00, \\00 }
                                        ** wrong - no text format provided to first string, therefore attemps conversion to number

LOAD( A, %t% "0x", %H04% u8Var );     // A = {  '0',  'x',  '0',  '0',  '3',  'F', \\00, \\00, \\00, \\00 } <- correct - text formatting applied

If A = { '1', '2', '3', 'X', 'Y', 'Z', \\00, 'P', 'Q', \\00 }
LOAD( RS2, %r% A ); \\ RS2 = "123XYZ\\00PQ\\00"    <- zeros are also sent
LOAD( RS2, %t% A ); \\ RS2 = "123XYZ" <- zeros are text terminators and are not sent, nor is text after a zero

Note:
The same destination values would be achieved
if TEXT( txt, %r% A ); 
or TEXT( txt, %t% A );
or LOAD( txtVar, %r% A );
or LOAD( txtVar, %t% A );

Array Minimum and Maximum Values - 49.02
CALC( ... "MIN" ) and CALC( ... "MAX" )
* New functions added to obtain the minimum and maximum values stored in an array
  > CALC( val, array, "MIN" );  The minimum value in the 'array' is stored in 'val'
  > CALC( val, array, "MAX" );  The maximum value in the 'array' is stored in 'val'

Array Data Shift - 49.02
CALC( ... "SHIFT" )
* New function added to shift the values in an array up or down its indices.
  > CALC( array, carry, shift, "SHIFT" );
  > CALC( array, shift, "SHIFT" );
- 'array' is shifted by 'shift' places, one shift at a time.
- If 'shift' is positive then array.1 -> array.2; array.0 -> array.1 etc
- If 'shift' is negative then array.1 -> array.0; array.2 -> array.1 etc
- The shift is 'circular', so the shifted out value is shifted in the other end.
- If 'carry' is specified then the shift passes through the carry, ie the carry is shifted in and a new
  carry is shifted out.

Example:
VAR( arr, 0.0, FLT2, 5 );
VAR car, 8.88, FLT2 );
LOAD( arr.0, 0.00 );
LOAD( arr.1, 1.11 ); LOAD( arr.2, 2.22 );
LOAD( arr.3, 3.33 ); LOAD( arr.4, 4.44 );    // setup arr = { 0.00, 1.11, 2.22, 3.33, 4.44 }

CALC( arr, -1, "SHIFT" );                           // Gives arr = { 1.11, 2.22, 3.33, 4.44, 0.00 }
CALC( arr, 2, "SHIFT" );                            // Gives arr = { 4.44, 0.00, 1.11, 2.22, 3.33 }
CALC( arr, car, -1, "SHIFT" );                    // Gives arr = { 0.00, 1.11, 2.22, 3.33, 8.88 } and car = 4.44

LOAD( car, 9.99 );
CALC( arr, car, -1, "SHIFT" );                    // Gives arr = { 1.11, 2.22, 3.33, 8.88, 9.99 } and car = 0.00


Array Sort
CALC(..."ASORT"); and CALC(..."ASORTR");

Added functions to sort arrays
CALC(arrname, "ASORT|ASORTR");
CALC(arrname.index, "ASORT|ASORTR");
Examples :-
VAR(arrname, 0, U8, 10);
LOAD(arrname, 9, 34, 2, 42, 102, 33, 52, 1, 67, 19);
CALC(arrname, "ASORT");
Results :-
arrname.0 = 1
arrname.1 = 2
arrname.2 = 9
arrname.3 = 19
arrname.4 = 33
arrname.5 = 34
arrname.6 = 42
arrname.7 = 52
arrname.8 = 67
arrname.9 = 102

VAR(arrname, 0, U8, 3, 4);
LOAD(arrname.0, 34, 2, 67, 4);
LOAD(arrname.1, 123, 45, 6, 127);
LOAD(arrname.2, 3, 109, 16, 5);
CALC(arrname, "ASORT");
Results :-
arrname.0 = 3, 109, 16, 5
arrname.1 = 34, 2, 67, 4
arrname.2 = 123, 45, 6, 127

VAR(arrname, 0, U8, 3, 4);
LOAD(arrname.0, 34, 2, 67, 4);
LOAD(arrname.1, 123, 45, 6, 127);
LOAD(arrname.2, 3, 109, 16, 5);
CALC(arrname.1, "ASORT");
Results :-
arrname.0 = 34, 2, 67, 4
arrname.1 = 6, 45, 123, 127
arrname.2 = 3, 109, 16, 5
Additional Len parameter added to sort only a specified number of elements

Syntax / Example
CALC(arrname, "ASORT");
CALC(arrname.index, "ASORT");
CALC(arrname, "ASORTR");
CALC(arrname.index, "ASORTR");

CALC(arrname,len, "ASORT");
CALC(arrname.index,len, "ASORT");
CALC(arrname,len, "ASORTR");
CALC(arrname.index,len, "ASORTR");

Pointer Arrays as LOAD() Source - v49.42
* Added functionality to allow a LOAD() source to be from a pointer array.
LOAD(RS2,A.0.0); where A is array of pointers.


Text Arrays - Functionality available by using U8 and U16 arrays - 49.46
* Text arrays can consist of either ASCII (8-bit) or Unicode (16-bit) characters.
* Creating an array of ASCII (8-bit) characters. Use the U8 data type.
VAR( varA, 0, U8, 6, 10 );

Number of text strings = 6
Maximum text string length = 10

Loading the array, use text formatting '%t%'

LOAD( varA.0, %t% "Zero" );
LOAD( varA.1, %t% "One" );
LOAD( varA.2, %t% "Two" );
LOAD( varA.3, %t% "Three" );
LOAD( varA.4, %t% "Four" );
LOAD( varA.5, %t% "This is too long" ); // truncates to "This is to"

Reading from the array, again using the text formatting '%t%'

LOAD( RS2, %t% varA.0, ", ", %t% varA.4 ); // Outputs "Zero, Four"
LOAD( RS2, %t% varA.5 ); // Outputs "This is to"
TEXT( txt1, varA.3 );; // Displays "Three"

* Creating an array of Unicode (16-bit) characters. Use the U16 data type
VAR( varB, 0, U16, 4, 8 );

Number of text strings = 4
Maximum text string length = 8

Loading the array, using text formatting '%t%'

LOAD( varB.0, %t% "\\w1234Zero" ); // varB.0.0 = \\1234
LOAD( varB.1, %t% "One" );
LOAD( varB.2, %t% "Two" );
LOAD( varB.3, %t% "Three" );

Reading from the Array, using the text formatting '%t%'

LOAD( RS2, %t% varB.0, ", ", %t% varB.3 ); // Outputs "\\w1234Zero, Three"

* Accessing individual characters using standard array accesses.
LOAD( RS2, %h% varB.0.0 ); // Outputs 1234
LOAD( varB.2.1, 42 ); // '*'
LOAD( RS2, varB.2 ); // Outputs "T*o"

* Displaying the raw data using the %r% formatting outputs the whole string including the terminating nulls.
LOAD( RS2, %r% varB.3 ); // Outputs "Three\\00\\00\\00"
 

Update Information

 Version

 Title

Date  

 Details

49.48

 System Timer Accuracy

21 Nov 13 

Show

* Fixed accuracy of 200us system tick timer - used for incrementing counters CNTRUN, CNTMILLI, ... CNTDAYS, TIMER0-TIMER19, and decrementing WAIT().
* Wrong divider value was being used. Needed to be divider value minus one.
* This resulted in a tick value of 200.174us rather than 200us.
* Assuming a perfect crystal frequency giving a 92,000,000Hz clock then there would be a +0.87ms error for every second.

47.00

 Touch Repeats in WAIT()

09 Jul 11 

Show

Touch keys were being incorrectly repeated in WAIT() loops.

42.00

 Wait() - Wait timer accuracy increased.

09 Mar 11 

Show

Wait timer accuracy increased, now running off system tick timer (max error 200ns).

36.00

 Wait - WAIT() can now be used with INTs, VARs or PTR.

23 Nov 10 

Show

WAIT() can now be used with INTs, VARs or PTR.

24.00

 Touch Screen - Controlled by Interrupt and 3 sample averaging.

25 Aug 10 

Show

Controlled by Interrupt and 3 sample averaging * The touch screen is now interrupt driven with improved accuracy, sample averaging and discarding of spurious samples.
* Added LOAD( Name, TOUCHX ); and LOAD( Name, TOUCHY ); to output last Touch Screen coordinates.
The WAIT() command is no longer needed with touch screen.
Repeat on touch screen to be in next release.