Show Posts
Pages: 1 2 [3] 4 5 ... 30
31  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 27, 2013, 07:34:18 am
Code:
void setup() {
 test1[0]="1"; test1[1]="2"; test1[2]="3";
 test2[0]="one"; test2[1]="two"; test2[2]="three";
 Serial.begin(115200);

  testpassarray(test1);
  testpassarray(test2);

  reprint(0);
  
}


You are passing local variables to a function, which immediately go out of scope when loop ends. Thus your desire to store a pointer to them is misguided. I suggest you look up "variable scope". Also look up "malloc" and "free", or "new" and "delete".

You don't have to use the String class, you can malloc an amount of memory to hold a string of the exact size you need.

If the array is being passed purely temporarily you might want to look into passing by reference.




The array is setup outside of the function setup ie in the main code.  Does that not make it global !

I am writing a library, I do not know how big the Strings will be before hand.  They can be 0-200 characters and there can be up to 100 sets mainly empty.  Strings are what I am using.


This is as far as I've got:

Code:

// Global variables

String test1[10] = {"1","2","3"};
String test2[10] = {"one","two","three"};
String *stored[100];
int numarrays=0;

void setup() {
  Serial.begin(115200);
  testpassarray(test1);
  testpassarray(test2);
  reprint(0);
  reprint(1);
}

void loop() {
}

void testpassarray(String *elements){
  stored[numarrays]=elements;  
  numarrays+=1;
  Serial.println("");
  Serial.println((unsigned long int)elements,HEX);
  Serial.println(elements[0]);
  Serial.println(elements[1]);
  Serial.println(elements[2]);
}

void reprint(int whicharray){
  
  String elements[10]; ////  This line needs to setup "elements" to point to the String array referenced by stored[whicharray]

  Serial.println("");
  Serial.println("");
  Serial.println((unsigned long int)stored[whicharray],HEX);
  
  int objectNumber=0;
  while (elements[objectNumber]!=""){
    Serial.print(objectNumber);
    Serial.print(" = <");
    Serial.println(elements[objectNumber]);
    Serial.println(">");
    objectNumber+=1;    
  }
 
  // release any variables etc
 
}
32  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 27, 2013, 04:39:11 am
I do appreciate the help but the thread does go off in the direction of whether I need to do what I am doing.  The demo program has nothing to do with the final code which needs to use Strings and cannot be hard coded.

The application in question is the DueGUI I am writing and basically the programmer will add objects like this:

Code:
progressbar=DueGUI.addProgressBar(375,100,40,30,60,50,ss,1,clrBlue,clrWhite,clrBlack,clrRed,2,optVisible,URNProgressbar);
progressbar2=DueGUI.addProgressBar(20,100,40,350,60,30,ss,0,clrGreen,clrWhite,clrBlack,clrYellow,2,optVisible,URNProgressbar2);
DueGUI.addButton(100,425,200,50,clrBlue,clrWhite,clrWhite,clrRed,clrWhite,2,"Refresh",posCentre,posCentre,BVS_28,optVisible ,URNRefresh);
btnAdd=DueGUI.addButton(100,250,200,50,clrCyan,clrWhite,clrBlack,clrRed,clrWhite,2,"Bring back 1&2",posCentre,posCentre,BVS_28,optInvisible,URNAdd);
btnRemove=DueGUI.addButton(100 ,350,200,50,clrGreen,clrWhite ,clrBlack,clrRed,clrWhite,2,"Remove  1&2",posCentre,posCentre,BVS_28,optVisible,URNRemove);
DueGUI.addButton(350,350,199,50,clrBlue ,clrWhite ,clrWhite,clrRed,clrWhite,2,"Fan Screen",posCentre,posCentre,BVS_28,optVisible,URNFanScreen);
DueGUI.addButton(600,350,199,50,clrBlue ,clrWhite ,clrWhite,clrRed,clrWhite,2,"Clock Screen",posCentre,posCentre,BVS_28,optVisible,URNClockScreen);

The library then stores the added objects which can be buttons, images, graphical shapes, text, input boxes, analogue clocks etc etc in a master block of arrays but the arrays are general purpose so as to save memory.

What I am trying to do is allow the user to pass a function:

addCycleStringButton(word x,word y,word xs,word ys,long colour,long borcolour,long textcolour,long presscolour,long presstextcolour,byte borwidth,String top,word xo,word yo,int font,int initialstate,String* elements,int cyclexo,int options,bool visible,int URN);
 
Where the variable passed in red is an array of elements for a drop down list.

The addCycleStringButton function will then store the address of the array in an array and later the draw/redraw/update functions will recall this address and access the array to find the relevant Strings in order to draw the object on the screen.

The test program simplifies this to the extreme that it makes no sense as to WHY I am doing it but I just wanted to make it simple so that I wasn't having to post massive amounts of code.

As it stands at the moment the test program does exactly what I need up to the point of USING that stored address....

Code:
String test1[10];
String test2[10];
String *stored[100];
int numarrays=0;

void setup() {
 test1[0]="1"; test1[1]="2"; test1[2]="3";
 test2[0]="one"; test2[1]="two"; test2[2]="three";
 Serial.begin(115200);

  testpassarray(test1);
  testpassarray(test2);

  reprint(0);
 
}

void loop() {
}

void testpassarray(String *elements){
 stored[numarrays]=elements;   
 numarrays+=1;
 Serial.println(elements[0]);
 Serial.println(elements[1]);
 Serial.println(elements[2]);
}

void reprint(int whicharray){
 
 
// setup variable "elements"         //  HOW ????
 
// elements= address of which array  //  HOW ????
 
  int objectNumber=0;
  while (element[objectNumber]!=""){
    Serial.println(element[objectNumber]);
    objectNumber+=1;   
  }
 

}


So now the whole program works other than the final function....

on entry to the final function we have the following:

int whicharray - this contains the location in the store[] array of the address of the String array
stored[]  - this contains the stored pointers
the number of elements is specified by an empty element so I'm using a while loop

SO....

How do I make "element[]" point to the correct array now that I can recall it's address from the store[] array?

as I said I do appreciate the help but it has to be done using Strings and if there is a way to do what I am wanting to do it will actually make it much simpler.
33  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 26, 2013, 06:35:34 pm
Quote
How do I assign that as a type for an array
Code:
String *lotsOfCarp[100];
will create an array of pointers to Strings.

Many thanks.
34  Using Arduino / Programming Questions / Re: Functions on: May 26, 2013, 06:30:12 pm
Or....

Code:
void setup ()
{
  pinMode(7,OUTPUT);
  pinMode(8,OUTPUT);
}

void loop ()
{
  blinkit(7);
  blinkit(8);
}

void blinkit (byte a)
{
  digitalWrite(a,HIGH);
  delay(1000);
  digitalWrite(a,LOW);
  delay(1000);
}
35  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 26, 2013, 06:27:06 pm
Do you really need to use an array of 'String', or 'String' pointer, objects or can it  be an array of 'C' 'char*' (zero terminated string pointers)?


Yes I do, I've mentioned it before I have an array of maybe 100 items which contain mainly "" or 5-10 chars BUT they could contain 200 characters.  If I used arrays of strings it would take 201x100 or nearly 20K but as Strings with most of them empty it uses probably 1-1.5K
36  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 26, 2013, 06:25:00 pm
Quote
No I mean I don't know what type A pointer is. The arrays are all String arrays.
An array of Strings is pointed to be a String pointer (String *).

Thanks,  How do I assign that as a type for an array and once the values are in the array how do I then access element[1] etc from the array?

Sorry to be a pain but I know what I need to do but just don't have the experience with this language.

Cheers....
37  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 26, 2013, 04:34:15 pm
I keep revisiting your post but no matter how many times I read it I'm not understanding what it is you wish to accomplish.


The progrmastere is just a small program that uses the processes I am trying to accomplish rather than  complicating matters by posting the entire program.

Basically I have about 20 different functions that all add gui objects to a master list then the interrupt driven routine creates the objects on screen. I need to be able to pass a list of Strings for certain objects but the master storage shouldn't duplicate the objects, I just want the add routine to store the array pointer. Once the array pointer is stored the draw routine for that type of object will recall the Strings and display them as necessary.

The boss of that that I can't work out how to do are store the array pointer in a cell of an array and then later recall that pointer using it to recall the Strings.
38  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 26, 2013, 11:54:33 am
I have an array of 100 Strings for the main storage and these can be any length from empty to 200 characters but mainly empty or 1-10 characters so storing as a string array would take more space wouldn't it?
39  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 26, 2013, 11:00:20 am
No I mean I don't know what type A pointer is. The arrays are all String arrays.

In the code I posted, the actual arrays are all global unless I am misunderstanding the scope, it's defined before the setup function in the main body not inside a function. Then the code sends the address of the array to the first function so I need to store that address whilst I am in the function. Then in the other function I need to recall the address from that array and then use the Strings. I can soon find the number of elements by looking for the first empty String.

So what type is a pointer? I assumed that it was a memory location hence my first guess at unsigned long int.
Once these addresses are stored in the global array how do I then use the address to refer to the String array?

Thanks
40  Using Arduino / Programming Questions / Re: Storing an array pointer in an array! on: May 26, 2013, 07:33:22 am
Quote
The code does not work.
Of course not. You are passing a pointer to an array of Strings to testpassarray. Then, in that function, you are creating, on the stack, an array of ints. Then, you are trying to set the first element beyond the end of the array to the value that is the address of the array of Strings.

Once the function ends, the array of ints goes away. The invalid assignment is lost.

You need to properly allocate space for the array, statically as a global array, or dynamically, using malloc() or new. The array MUST be of the right type to hold the pointers. You must assign the pointer to a valid element in the array.

There are just too many things you are doing wrong in that code.

Thanks, I never expected the code to work but just that it could be followed.

The array "stored" needs to be an array of pointers to the arrays in the main program but I don't know what type the pointer is.  This array is global (as are the String arrays) so once the address of the array is stored it should stay where it is till I clear everything later.

In the other function "reprint" I need to get this pointer address back from the global array and then access the array based on that pointer.

The String arrays will not change between their pointers being placed into the global array and being used.  Certain events will completely clear all the arrays and we start again.
41  Using Arduino / Programming Questions / Storing an array pointer in an array! on: May 26, 2013, 07:03:33 am
I understand how to pass an array to a function, the following code shows how:

Code:
String test1[10];
String test2[10];

void setup() {
  // put your setup code here, to run once:
 test1[0]="1";
 test1[1]="2";
 test1[2]="3";
 test2[0]="one";
 test2[1]="two";
 test2[2]="three";
 Serial.begin(115200);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
  testpassarray(test1);
  testpassarray(test2);
  
  
}

void testpassarray(String *elements){
 
 Serial.println(elements[0]);
 Serial.println(elements[1]);
 Serial.println(elements[2]);
  
}


What I need to do is STORE the pointer in an array so:

(I know the program is pointless but I need to understand the concept!

Code:
String test1[10];
String test2[10];
unsigned long int stored[100];
int numarrays=0;

void setup() {
  // put your setup code here, to run once:
 test1[0]="1";
 test1[1]="2";
 test1[2]="3";
 test2[0]="one";
 test2[1]="two";
 test2[2]="three";
 Serial.begin(115200);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
  testpassarray(test1);
  testpassarray(test2);
  
  
}

void testpassarray(String *elements){
  
 stored[numarrays]=elements;
 numarrays+=1;
 
 Serial.println(elements[0]);
 Serial.println(elements[1]);
 Serial.println(elements[2]);
  
}

The code does not work.  I'm not at all new to programming just haven't got a big history of this language!

Also how can I tell how many elements inside the passed array?

Is the type "uint16_t" the correct one?  If so how do I store the address of the passed array so that I can use it in another function later?

Thanks all.



ADDED:

The following code shows what I am trying to do (JUST FOR THIS EXAMPLE) but clearly several lines are just rubbish

The lines which start "//////"  are the ones I probably need help with.

I know I could call the original function again BUT this is not what I am trying to do.  I need to understand HOW to do what this program is doing in order to add to an existing very large program.  So I have created this example just as a learning tool.

Code:


String test1[10];
String test2[10];
////// uint16_t stored[100];     // HOW ????
int numarrays=0;

void setup() {
  // put your setup code here, to run once:
 test1[0]="1";
 test1[1]="2";
 test1[2]="3";
 test2[0]="one";
 test2[1]="two";
 test2[2]="three";
 Serial.begin(115200);

  testpassarray(test1);
  testpassarray(test2);

  reprint(0);
  reprint(1);
  
}

void loop() {
  // put your main code here, to run repeatedly:
  
  
  
}

void testpassarray(String *elements){
  
///// stored[numarrays]=elements;   // HOW ????
 numarrays+=1;
 
 Serial.println(elements[0]);
 Serial.println(elements[1]);
 Serial.println(elements[2]);
  
}

void reprint(unsigned long int whicharray){
 
///// setup variable "elements"         //  HOW ????
  
///// elements= address of which array  //  HOW ????
 
 
 Serial.println(elements[0]);
 Serial.println(elements[1]);
 Serial.println(elements[2]);
  
}
42  Products / Arduino Due / Re: Due shield template available? on: May 25, 2013, 08:53:02 am
My library has two versions.  "Normal" is a shield template where you can connect top or bottom so fine if you are having a board made but I have also included a second version called "Bottom only" which blocks top connections so you can actually build it using home setups.
43  Products / Arduino Due / Re: Due shield template available? on: May 25, 2013, 07:00:59 am
I've posted one on here about 2 weeks ago for Eagle....

http://forum.arduino.cc/index.php?topic=153875.msg1153909#msg1153909
44  Products / Arduino Due / Re: Due GUI (Graphical user interface) - [now version 0.12] on: May 25, 2013, 06:59:16 am
Version 1.13 further news....

Cycle button now has it's first option (bit 1)

if set then you can cycle in either direction by pressing first half of button or second half of button (by setting increment - or + you can alter whether 1st half is up/down with 2nd half always opposite).

AND now

(bit 2)

if set then it does the same for top/bottom half of button
45  Products / Arduino Due / Re: Due GUI (Graphical user interface) - [now version 0.12] on: May 24, 2013, 12:02:59 pm
Version 1.13 news....

I will upload over the next few days but added:

GhostButton

Basically this is an area of the screen which acts as a button. This can be used for detecting if someone has touched part of the screen so you could draw a menu and store it as a graphic then update the whole screen with this graphic and then use this button to detect the user pressing parts of it.

ImageButton

This takes 1, 2 or 3 images.... image, pressed image and alternative image

If image only is used then it displays that image and if it is clicked it registers
If pressed image is set then when the image is pressed it changes to the pressed image momentarily to signify being pressed
If alternative image is set then the image changes between image and alternative image acting like a checkbox.
Pages: 1 2 [3] 4 5 ... 30