简体   繁体   中英

Arduino C++ Dereference a #define or const int?

I'm very rusty on my C++ so forgive me if this isn't a question about dereferencing at all. But I think that's the right terminology.

I'm putting together an Arduino sketch to send out raw infrared signals to emulate IR controllers.

To achieve this I'm sending a JSON string that references the signal defined in a header file that I'm including. I'm thinking of having a separate file for each controller and I want to use a #define statement in each header to ensure that I get good separation of each signal name. Sort of a roll my own namespace if you will.

So in the header I have this signal for the power button on a remote:

#include <stdint.h>
#include <Arduino.h>

#define AKB73575431_DATA_LEN 69
String AKB73575431_mfr = "LG";
uint16_t AKB73575431_power [AKB73575431_DATA_LEN] = {
  4400,4600,550,600,500,600,550,1650,550,1700,
  550,600,500,1700,550,600,550,600,500,600,
  550,550,550,1700,550,1650,550,600,550,1700,
  500,650,500,600,500,600,550,1700,550,1650,
  600,1650,550,1700,550,600,500,600,550,600,
  500,1700,550,600,550,550,550,600,500,600,
  500,1700,600,1650,550,1700,550,1000
};

Some of you might recognise this data format as cribbed straight from the tutorials on IRLib2.

I want to send a JSON string:

{"cmd": "txIRraw", "key": "AKB73575431_power"}

My question is can I parse the string in the "key" value and use the substring:

AKB73575431

to construct a reference to the define:

#define AKB73575431_DATA_LEN 69

Failing that, is it possible to change the define to a global int and dereference it instead?

I've been Googling dereferencing variables in C++ but keep getting pages on pointers. What I want to do is construct the variable or #define name from a string and then use that.

Is this possible and if so, can someone give me an example?

Many thanks

Kali

If I'm understanding your question correctly, you are trying to see if you can use the string "AKB73575431" , to create a variable with the same name AKB73575431 , and assign the value of AKB_LEN to it.

The quick answer would be no. You can't create a variable name based on values from another string.

There are two different approach you could use to get to similar effect(I guess).


First you can utilize macros.

You could do something the other way around. Say if you want to create multiple variable with prefix AKB_ , such as AKB_len , AKB_power , AKB_mfr . You can do that with macro:

#define create(NAME, VALUE) decltype(VALUE) AKB_##NAME{VALUE}

So you can use it like:

create(LEN, 10); // create a variable called `AKB_LEN`, and has the value of 10

or:

string s = "abc";
create(S, s); // create a variable called `AKB_S`, and has the type string and value "abc"

However, I would not recommend this. It's hard to get the variable name you created, as they are hidden under macros. And if you have another remote, with a different name, you would need to create a whole new macro function, to get them to work.


The second way is through OOP. You would create a Remote class, and the Remote might have different members such as name, mfr, and power:

struct Remote
{
    std::string name;
    std::string mfr;
    std::vector<int> power;
}

And then you can create Remote like:

Remote AKB{"AKB", AKB_mfr, {std::being(AKB_power}, std::end(AKB_power)};

Then you can access data of it with things like AKB.mfr , AKB.power


Furthermore, you could also make the Remote class without the name member, and store remotes in a map container:

struct Remote
{
    std::string mfr;
    std::vector<int> power;
}

Then you can create a map like:

std::map<std::string, Remote> Remotes;

And add new remotes to the map like:

Remotes.emplace("AKB", Remote{AKB_mfr, {std::begin(AKB_power), std::end(AKB_power)}});

Now if you want to access a remote called "AKB" from the map, you can do things like:

Remotes["AKB"].mfr

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM