Author Topic: how to declare an array of IO pins?  (Read 3449 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
how to declare an array of IO pins?
« on: May 13, 2014, 03:07:53 PM »
I've been racking my brain on this all day, hoping a better programmer than I can figure this out. I want to declare an array of pins (best attempt):

Code: [Select]
int *multi[3]={H3, H4, H5};
But it's giving me this:

 warning: initialization from incompatible pointer type

Code: [Select]
int multi[3]={H5, H3, H4};
 warning: initialization makes integer from pointer without a cast

H3, H4, etc. were declared in using WebbotLib

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: how to declare an array of IO pins?
« Reply #1 on: May 14, 2014, 12:47:38 PM »
So the type of H3, H4, and H5 are pointer, but not pointer to int.
I don't know what they're pointer to, but you could try declaring multi as void * instead of int*, or if using modern C++, auto*.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: how to declare an array of IO pins?
« Reply #2 on: May 14, 2014, 01:37:21 PM »
This:
Code: [Select]
void *multi[3]={H3, H4, H5};
Gives this:

 warning: initialization discards qualifiers from pointer target type

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: how to declare an array of IO pins?
« Reply #3 on: May 14, 2014, 03:15:32 PM »
I'd suggest "auto * multi[] = ..."
If that doesn't work, the you have to figure out whether the pointers need to be volatile, or const, or both. Try something like this:
"volatile const void *multi[] = ..."

Note -- this may not really do what you want once you use "multi" because I presume you will then want to pass those H3/H4/H5 values elsewhere, and those functions might not like void*. auto* would be best if it works.

Or, do it the right way: Figure out where in the headers H3, H4 and H5 are declared, and use the correct type to begin with ;-)


Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: how to declare an array of IO pins?
« Reply #4 on: May 14, 2014, 03:24:23 PM »
using auto* gives this:

warning: type defaults to 'int' in declaration of 'multi'
error: file-scope declaration of 'multi' specifies 'auto'
warning: initialization from incompatible pointer type
warning: initialization from incompatible pointer type
warning: initialization from incompatible pointer type

This code:
Code: [Select]
volatile const void *and
Code: [Select]
const void *
compiles without error . . . thanks! :D

Now to write code and hook up hardware to see if that actually does what I want . . .

Quote
Or, do it the right way: Figure out where in the headers H3, H4 and H5 are declared, and use the correct type to begin with ;-)
Tried, but couldn't find it. It appears WebbotLib has that precompiled in an ant file, but I could be wrong . . .

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: how to declare an array of IO pins?
« Reply #5 on: May 14, 2014, 03:31:52 PM »
doh, it breaks again when I change it to a matrix . . .

note: all of those multi's are defines for digital output pins, like H3, H4, H5, etc.

Code: [Select]
const void *multi[4][6]={
{multi_0_S0, multi_0_S1, multi_0_S2, multi_0_S3, multi_0_EN, multi_0_SIG},
{multi_1_S0, multi_1_S1, multi_1_S2, multi_1_S3, multi_1_EN, multi_1_SIG},
{multi_2_S0, multi_2_S1, multi_2_S2, multi_2_S3, multi_2_EN, multi_2_SIG},
{multi_3_S0, multi_3_S1, multi_3_S2, multi_3_S3, multi_3_EN, multi_3_SIG}};

gives me this warning four times:

  warning: initialization makes pointer from integer without a cast

Offline jwatte

  • Supreme Robot
  • *****
  • Posts: 1,345
  • Helpful? 82
Re: how to declare an array of IO pins?
« Reply #6 on: May 14, 2014, 04:35:08 PM »
SO multi_xx_yy is an int, whereas H3 is a pointer type.
In that latest declaration, change void * to int.

Note: I don't have any experience with this code base, I go solely on the compiler error messages.
It should be possible to figure out where the H3 declaration is coming from. Try adding the "-MD" compile option, which will spit out a list of all the #includes and what files they find.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: how to declare an array of IO pins?
« Reply #7 on: May 14, 2014, 05:43:18 PM »
Let's get back to some basics....

WebbotLib stops you addressing pins like H3, H4 etc directly. Why? Well the pins may exist on the micro-controller but may also be used for specific things by your board (an Axon say) - ie they are dedicated. Take the Axon II:- this has a switch mounted on the board on pin D5 so if I let you configure it as an output at 5V then if you pressed the button you'd get a short circuit and blow the pin (at best) or whole chip (at worst).

So if you want to do stuff with a bunch of pins:- then define them as Digital In/Out pins in Project Designer - this will complain if the pin is used for other things (ie like the button mentioned above on D5). This guarantees that the pins are yours to do what you want with.

Assuming you are using WebbotLib Studio (if not then why not :-)) and you've called your pin 'bossPin' in Project Designer then it is defined as:-
webbotlib::Pin bossPin;

Now assume you've created 5 of them called bossPin0 to bossPin4 then you could create an array:-
webbotlib::Pin* pins[] = { &bossPin0, &bossPin1, &bossPin2, &bossPin3, &bossPin4 };




Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: how to declare an array of IO pins?
« Reply #8 on: May 15, 2014, 08:01:06 AM »
Perhaps I oversimplified my above posts . . . clarification:

multi_x_yy are not ints. They were defined as Digital In/Out pins in Project Designer.

I am not using WebbotLib Studio (I'll email you why). As such, that code suggestion isn't working . . .

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: how to declare an array of IO pins?
« Reply #9 on: May 17, 2014, 06:46:06 AM »
Ok - only missing info is whether you are generating your code a C or C++.

Assuming you have used Project Designer to create Digital Input/Output pins called multi_0_S0, multi_0_S1, multi_0_S2, multi_0_S3, multi_0_EN, multi_0_SIG etc

Assuming C++then:
Code: [Select]
Pin *multi[4][6]={
{&multi_0_S0, &multi_0_S1, &multi_0_S2, &multi_0_S3, &multi_0_EN, &multi_0_SIG},
{&multi_1_S0, &multi_1_S1, &multi_1_S2, &multi_1_S3, &multi_1_EN, &multi_1_SIG},
{&multi_2_S0, &multi_2_S1, &multi_2_S2, &multi_2_S3, &multi_2_EN, &multi_2_SIG},
{&multi_3_S0, &multi_3_S1, &multi_3_S2, &multi_3_S3, &multi_3_EN, &multi_3_SIG}};
You can then reference any given pin as:-
Code: [Select]
Pin* aPin = multi[0][0];
aPin->set(true); // or whatever you want to do with it.

Alternatively if you are generating in C:-
Code: [Select]
const IOPin* multi[4][6]={
{multi_0_S0, multi_0_S1, multi_0_S2, multi_0_S3, multi_0_EN, multi_0_SIG},
{multi_1_S0, multi_1_S1, multi_1_S2, multi_1_S3, multi_1_EN, multi_1_SIG},
{multi_2_S0, multi_2_S1, multi_2_S2, multi_2_S3, multi_2_EN, multi_2_SIG},
{multi_3_S0, multi_3_S1, multi_3_S2, multi_3_S3, multi_3_EN, multi_3_SIG}};

You can then reference any given pin as:-
Code: [Select]
const IOPin* aPin = multi[0][0];
pin_set(aPin,true); // or whatever you want to do with it.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: how to declare an array of IO pins?
« Reply #10 on: May 19, 2014, 03:10:08 PM »
The C++ code gives me this error:

error: expected '=', ',', ';', 'asm' or '__attribute__' before '*' token

and the C code gives me this:

warning: initialization makes pointer from integer without a cast
warning: initialization makes pointer from integer without a cast
warning: initialization makes pointer from integer without a cast
warning: initialization makes pointer from integer without a cast

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: how to declare an array of IO pins?
« Reply #11 on: May 19, 2014, 04:11:35 PM »
Seems odd - I spent time to create projects from scratch - ie its not just 'think so' its actual cut'n'paste of code that works for me.

Perhaps send me your .prj (as that will have your codegen options in it) - plus any source files.

Wonder if you've foul of trying to create C++ and C into the same folder. ie if your project is called test then have you got a generated test.cpp and a test.c in the project folder? This mayhem as they are both compiler to test.o (not Webbot thing - its a compiler thing).

Doing a 'make clean' wont delete them - as I purposely dont delete source files otherwise I may delete your entire typed in code.
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: how to declare an array of IO pins?
« Reply #12 on: May 20, 2014, 07:22:01 AM »
I created a new empty project, default everything. Still, the same four warnings.

The only change I made was adding my function after the #include:
Code: [Select]
#include "hardware.h"

const IOPin* multi[4][6]={
{multi_0_S0, multi_0_S1, multi_0_S2, multi_0_S3, multi_0_EN, multi_0_SIG},
{multi_1_S0, multi_1_S1, multi_1_S2, multi_1_S3, multi_1_EN, multi_1_SIG},
{multi_2_S0, multi_2_S1, multi_2_S2, multi_2_S3, multi_2_EN, multi_2_SIG},
{multi_3_S0, multi_3_S1, multi_3_S2, multi_3_S3, multi_3_EN, multi_3_SIG}};

Project file attached. Not using the C++ option.

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: how to declare an array of IO pins?
« Reply #13 on: May 21, 2014, 04:36:55 PM »
Ok - so you're problem is that you are mixing different stuff:
ie  all 'multi_x_Sy' things are digitial I/O pins but 'multi_x_SIG' are not - they are ADC channel numbers not IOPins.


Since ADC channels are just an integer number ( 0 to 15 say) then you are seeing the error because the compiler cannot convert an integer ADC channel into an IOPin. Its perfectly possible for an AVR chip to have an ADC pin that is NOT a digital I/O pin.

So behaviour is as expected.

Next post will give ways on how you may want to write working/better code :-)
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: how to declare an array of IO pins?
« Reply #14 on: May 21, 2014, 05:02:14 PM »
See previous post re 'why the error' - ie its not array of io pins its an array of 'stuff'.

Your code, no idea what is for, seems to have 4 things - each needs 5 io pins and an ADC channel.


Best solution is to create a struct(ure) - which is like a C++ class - called say myThingy which has 5 io pins and an adc.
This can be define in C as:
Code: [Select]
// Declare definition of one myThingy
typedef struct s_myThingy {
  const IOPin* pin1;
  const IOPin* pin2;
  const IOPin* pin3;
  const IOPin* pin4;
  const IOPin* pin5;
  ADC_CHANNEL  channel;
} myThingy;

You can now create an array of myThingy
Code: [Select]
// Create an array of myThingy's
myThingy multi[ /* you could specify 4 here but why bother as the compiler can work it out */] = {
{multi_0_S0, multi_0_S1, multi_0_S2, multi_0_S3, multi_0_EN, multi_0_SIG},
{multi_1_S0, multi_1_S1, multi_1_S2, multi_1_S3, multi_1_EN, multi_1_SIG},
{multi_2_S0, multi_2_S1, multi_2_S2, multi_2_S3, multi_2_EN, multi_2_SIG},
{multi_3_S0, multi_3_S1, multi_3_S2, multi_3_S3, multi_3_EN, multi_3_SIG}
};


Your code should now compile.
Next post is to how to use a myThingy  (this is  like C 101....)
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline Webbot

  • Expert Roboticist
  • Supreme Robot
  • *****
  • Posts: 2,165
  • Helpful? 111
    • Webbot stuff
Re: how to declare an array of IO pins?
« Reply #15 on: May 21, 2014, 05:12:21 PM »
How to use a thingy ?

Lets say its the second one:

Code: [Select]
myThingy* thing2 = &multi[1];


// And we want to do something with the second io pin
pin_set(thing2->pin2,true);

// or read its adc channel
uint16_t value = a2dConvert10bit(thing2->channel);

Much easier - and even easier in C++
Webbot Home: http://webbot.org.uk/
WebbotLib online docs: http://webbot.org.uk/WebbotLibDocs
If your in the neighbourhood: http://www.hovinghamspa.co.uk

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,703
  • Helpful? 173
    • Society of Robots
Re: how to declare an array of IO pins?
« Reply #16 on: May 30, 2014, 12:58:05 PM »
thanks - fixed!

I created two arrays, one for digital IO and one for ADC IO.

 


Get Your Ad Here

data_list