POV-Ray : Newsgroups : povray.off-topic : c++ template types for function Server Time
29 Mar 2024 05:33:08 EDT (-0400)
  c++ template types for function (Message 1 to 9 of 9)  
From: Bald Eagle
Subject: c++ template types for function
Date: 28 Apr 2020 20:40:00
Message: <web.5ea8ccaba075fdb6fb0b41570@news.povray.org>
So, I'm trying to initialize some arrays, of a size determined by how many
sensors are specified in a little control panel section of the code.

I [finally] wrote a working function ( ZeroArray )

//------------------------------------------------
void ZeroArray (float *Array)
{
 for (int Element = 0; Element < NumSensors; Element++)
 {
  Array [Element] = 0;
 }
}
//------------------------------------------------

and now I'm trying to initialize arrays either with floats OR bool values

//---------------------------------------------------------
template <typename T, typename N>
void InitializeArray (T *Array, N *Value)
{
 for (int Element = 0; Element < NumSensors; Element++)
 {
  Array [Element] = Value;
 }
}
//---------------------------------------------------------

Of course, I have no idea WTH I'm doing and trying to search online for a sane,
concise, and meaningful answer is difficult and time consuming.

void setup ()
{
     ...
     line 224:   InitializeArray (IntegralOutOfRange, false);
     ...
}


/home/oem/Arduino/Sketches/TempSensorCalibratorv0.1.0/TempSensorCalibratorv0.1.0.ino:
In function 'void setup()':
TempSensorCalibratorv0.1.0:224:44: error: no matching function for call to
'InitializeArray(bool [2], bool)'
  InitializeArray (IntegralOutOfRange, false);
                                            ^
/home/oem/Arduino/Sketches/TempSensorCalibratorv0.1.0/TempSensorCalibratorv0.1.0.ino:137:6:
note: candidate: template<class T, class N> void InitializeArray(T*, N*)
 void InitializeArray (T *Array, N *Value)
      ^~~~~~~~~~~~~~~
/home/oem/Arduino/Sketches/TempSensorCalibratorv0.1.0/TempSensorCalibratorv0.1.0.ino:137:6:
note:   template argument deduction/substitution failed:
/home/oem/Arduino/Sketches/TempSensorCalibratorv0.1.0/TempSensorCalibratorv0.1.0.ino:224:44:
note:   mismatched types 'N*' and 'bool'
  InitializeArray (IntegralOutOfRange, false);


Any ideas?


Post a reply to this message

From: jr
Subject: Re: c++ template types for function
Date: 28 Apr 2020 21:35:01
Message: <web.5ea8d961ce0a4398898043f30@news.povray.org>
hi,

"Bald Eagle" <cre### [at] netscapenet> wrote:
> ...
> and now I'm trying to initialize arrays either with floats OR bool values

no C++, but in C I'd create an array of a union of float + bool.


regards, jr.


Post a reply to this message

From: Bald Eagle
Subject: Re: c++ template types for function
Date: 30 Apr 2020 14:45:00
Message: <web.5eab1c1cce0a4398fb0b41570@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:

> no C++, but in C I'd create an array of a union of float + bool.

I think for my purposes, c and c++ are similar enough to "be the same".
{GASP!}  {Sacrilege!}  "Thou hast besmirched the pedantic purity of the names!"}


Could you possibly write a little example of that in C so I can see how it
works?


I also have a hardware address hard-coded into a .h file which prototypes the
functions that interact with the hardware.
The problem I have is that I need at least 2 different sensors, which will have
different addresses once I switch one on the solder pad.  I can do a max of 4.

Should I have 4 different .h files?
Should I have 4 differently named constructors IN the .h file?

I'm also confused about the associated .cpp file.  That's the one that actually
has the functions defined in it, so how does #include <filename.h> access what's
in filename.cpp if there's no #include <filename.cpp> in filename.h?
Is it just an internal thing to the IDE that automatically includes any
similarly named .cpp file if a .h file is included?

So much to learn.  ;)


Post a reply to this message

From: jr
Subject: Re: c++ template types for function
Date: 30 Apr 2020 15:45:00
Message: <web.5eab2a15ce0a4398898043f30@news.povray.org>
hi,

"Bald Eagle" <cre### [at] netscapenet> wrote:
> "jr" <cre### [at] gmailcom> wrote:
>
> > no C++, but in C I'd create an array of a union of float + bool.
>
> I think for my purposes, c and c++ are similar enough to "be the same".
> {GASP!}  {Sacrilege!}  "Thou hast besmirched the pedantic purity of the names!"}

too much alike, visually, imo.

> Could you possibly write a little example of that in C so I can see how it
> works?

typedef union foo foo;
union foo {
  float f;
  bool b;
};

static foo fooarray[N];

in your case, the vector would be made of type foo; the typedef isn't needed,
just a convenience to use 'foo' in place of 'union foo'.  (no idea about syntax
etc)

> I also have a hardware address hard-coded into a .h file which prototypes the
> functions that interact with the hardware.
> The problem I have is that I need at least 2 different sensors, which will have
> different addresses once I switch one on the solder pad.  I can do a max of 4.
>
> Should I have 4 different .h files?
> Should I have 4 differently named constructors IN the .h file?
>
> I'm also confused about the associated .cpp file.  That's the one that actually
> has the functions defined in it, so how does #include <filename.h> access what's
> in filename.cpp if there's no #include <filename.cpp> in filename.h?
> Is it just an internal thing to the IDE that automatically includes any
> similarly named .cpp file if a .h file is included?

yes, I recently installed one of those C++ library-in-a-header files.  not my
cup of tea.

as regards the first question, I should have thought that, for a small project,
you can have all sensor constants etc in a single header, just a matter of
avoiding name clashes.


regards, jr.


Post a reply to this message

From: Bald Eagle
Subject: Re: c++ template types for function
Date: 30 Apr 2020 16:35:05
Message: <web.5eab364dce0a4398fb0b41570@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:

> typedef union foo foo;
> union foo {
>   float f;
>   bool b;
> };
>
> static foo fooarray[N];

Interesting.   I can _rationalize_ why it would be that way, but I might have
taken me days to happen upon the proper syntax.
I'll give it a whirl and see where it gets me.
Thank you very much, as always.

> in your case, the vector would be made of type foo;

Vector....   ?


> yes, I recently installed one of those C++ library-in-a-header files.  not my
> cup of tea.

So "yes" means the cpp file gets automatically referenced...

> as regards the first question, I should have thought that, for a small project,
> you can have all sensor constants etc in a single header, just a matter of
> avoiding name clashes.

So in the .h file, I have in part:
class TMP117
{
public:
 TMP117(); // Constructor

 bool begin(uint8_t sensorAddress = 0x48, TwoWire &wirePort = Wire); // Checks
for ACK over I2C, and sets the device ID of the TMP and chooses the wire port
 uint8_t getAddress();            // Returns the address of the device
 double readTempC();             // Returns the temperature in degrees C

.....

};

in the program I'm writing, I have:

TMP117 sensor;
 and
if (sensor.begin () == true) {....}

So I'm _guessing_ that "TMP117 sensor;" assigns the "keyword" sensor to - either
the class or the public constructor.

I'm also guessing the simplest way to copy-paste code to deal with multiple
sensors would be to make a new class for each sensor? ?

Is there a way to do this that will play well with treating my functions as
elements of an array?   I just want to cycle through my sensors with a loop, so
having T_C = readTempC[N](); or however the proper syntax would look would be
most preferable.

This is worse than delving into POV-Ray SDL 7+ years ago after a 10+ year
hiatus.
"Painfully stupid newbie questions."  :D  ;)


Post a reply to this message

From: jr
Subject: Re: c++ template types for function
Date: 30 Apr 2020 17:10:04
Message: <web.5eab3dbdce0a4398898043f30@news.povray.org>
hi,

"Bald Eagle" <cre### [at] netscapenet> wrote:
> "jr" <cre### [at] gmailcom> wrote:
> ...
> Interesting.   I can _rationalize_ why it would be that way, but I might have
> taken me days to happen upon the proper syntax.
> I'll give it a whirl and see where it gets me.
> Thank you very much, as always.

no sweat.  :-)

> > in your case, the vector would be made of type foo;
>
> Vector....   ?

ignore.  misremembered.

> > yes, I recently installed one of those C++ library-in-a-header files.  not my
> > cup of tea.
>
> So "yes" means the cpp file gets automatically referenced...

no.  "yes" meant </sigh>, I guess.  afaik there's no .cpp; you include the
header and get the library functionality.

<https://github.com/g-truc/glm/releases>

> > as regards the first question, I should have thought that, for a small project,
> > you can have all sensor constants etc in a single header, just a matter of
> > avoiding name clashes.
>
> So in the .h file, I have in part:
> class TMP117
> {
> public:
>  TMP117(); // Constructor
>
>  bool begin(uint8_t sensorAddress = 0x48, TwoWire &wirePort = Wire); // Checks

this looks so wrong.  if I read your intent correctly, in C, that would need to
be something like:

bool begin(uint8_t, TwoWire *);

and then, the values, either '#define'd or, better, in (const) variables, eg in
the header:

extern uint8_t const sensorAddress;
extern TwoWire * const wirePort;

and in your source, somewhere near top:

uint8_t const sensorAddress = 0x48;
TwoWire * const wirePort = Wire;

> ...
> I'm also guessing the simplest way to copy-paste code to deal with multiple
> sensors would be to make a new class for each sensor? ?

seems to me that if the sensors are of the same type, you'd only need so many
instances, if they differ, I guess some sensor parent class + per type classes
inheriting from that.  (usual 'no speak c++' disclaimer)

can't really comment on the remaining question.  I would have thoughr though
that there are enough arduino projects on the web to .. analyse and peruse.


regards, jr.


Post a reply to this message

From: jr
Subject: Re: c++ template types for function
Date: 30 Apr 2020 18:40:00
Message: <web.5eab52dbce0a4398898043f30@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:
> ...

additionally, to create "infrastructure", you may want to look at enumerations,
for instance the sensor names might be something like (C again):

enum sensorAddr {
  sensor_a = 0x48,
  sensor_b = 0x50
};
typedef enum sensorAddr sensorAddress;

or without values, letting the compiler use 0,..,N.  again, the typedef is just
convenience.

there's a website, no details but a "corny" url with "school" in it, which
provides detailed references for all common languages, with example code for
every keyword.

also, have no experience with GLM other than having needed it to get some s/ware
to compile.


regards, jr.


Post a reply to this message

From: Bald Eagle
Subject: Re: c++ template types for function
Date: 30 Apr 2020 19:15:00
Message: <web.5eab5accce0a4398fb0b41570@news.povray.org>
"jr" <cre### [at] gmailcom> wrote:

> this looks so wrong.  if I read your intent correctly, in C, that would need to
> be something like:

Yeah, but it's code copied right out of the functioning .h file, so...


> seems to me that if the sensors are of the same type, you'd only need so many
> instances, if they differ, I guess some sensor parent class + per type classes
> inheriting from that.  (usual 'no speak c++' disclaimer)

I mean, if there's a way to keep all of the functions mostly the way they are
and somehow get them to work on a sensor argument passed to them...

> can't really comment on the remaining question.  I would have thoughr though
> that there are enough arduino projects on the web to .. analyse and peruse.

I would have thought so too, but I've spent several fruitless hours searching
for exactly that and ... no joy.  You'd think that SparkFun or Texas Instruments
would have _some_ sort of information on how to hook up multiple sensors, but
that seems to be conspicuously absent.

Let the hacking commence!   >:)


Post a reply to this message

From: jr
Subject: Re: c++ template types for function
Date: 30 Apr 2020 19:50:01
Message: <web.5eab6341ce0a4398898043f30@news.povray.org>
hi,

"Bald Eagle" <cre### [at] netscapenet> wrote:
> "jr" <cre### [at] gmailcom> wrote:
>
> > this looks so wrong.  if I read your intent correctly, in C, that would need to
> > be something like:
>
> Yeah, but it's code copied right out of the functioning .h file, so...

(some languages seem to be "write-only".  for me C++, like Perl, assaults the
eye)

> ...
> I mean, if there's a way to keep all of the functions mostly the way they are
> and somehow get them to work on a sensor argument passed to them...

if you want, use email to send the header and further detail/explanation.

> > can't really comment on the remaining question.  I would have thoughr though
> > that there are enough arduino projects on the web to .. analyse and peruse.
>
> I would have thought so too, but I've spent several fruitless hours searching
> for exactly that and ... no joy.  You'd think that SparkFun or Texas Instruments
> would have _some_ sort of information on how to hook up multiple sensors, but
> that seems to be conspicuously absent.

strange.  (lateral approach) maybe search through Raspberry PI projects
involving sensors, arduino might get used in those.


regards, jr.


Post a reply to this message

Copyright 2003-2023 Persistence of Vision Raytracer Pty. Ltd.