1

I have to transfer the C++ code into Micro Python to Wipy platform. Currently, I've some problems with it, especially with accesing the variables from other structures

C++ code:

typedef struct {
    char id[100];
    int rssi;
} tag_info;

typedef struct {
    tag_info tag[20];
} tag_list;

typedef struct {
    int timestamp;
    tag_list tags;
    int heading;
    int airt;
} data_packet;

data_packet packet;

for (i = 0; i < total_amount_of_tags - 1; i++) {
            print_function("id", packet.tags.tag[i].id, destination); 
            print_function("rssi", packet.tags.tag[i].rssi, destination);
        }

My MicroPython code:

class tag_info:
    def __init__(self):
        self.id = ""
        self.rssi = 0

class tag_list:
    def __init__(self):
        self.tag = tag_info

class data_packet():
    def __init__(self):
        self.timestamp=0
        self.tags = tag_list
        self.heading=0
        self.airt=0

packet=data_packet()

for i in range(0, total_amount_of_tags -1)
    print_function('id',packet.tags.tag[i].id,destination)
    print_function("rssi", packet.tags.tag[i].rssi, destination); 

The problem in my code that I cant access tag[i] and further variables. Should I use inheritance or namedTuple in this case. If i try to use namedTuple from collections import namedTuple the following error message occurs: no module called collections, so I would prefer to avoid that library if possible

3
  • Proper variant is namedtuple, but not namedTuple. Using this datatype is preferrable, there is no neccessary to build your own datatypes Commented Jan 9, 2019 at 13:47
  • To decide Import error - please provide python version and system info Commented Jan 9, 2019 at 13:48
  • for that project I use micropython on wipy 3.0 with the latest firmware upgrade Commented Jan 10, 2019 at 17:51

2 Answers 2

3

In the following:

self.tag = tag_info
You are assigning a class to a variable. I assume you meant to do:
self.tag = tag_info()
but if I understand correctly this is supposed to be a list of tag_info, so you actually need is:
self.tag = [tag_info() for i in range(20)]

same goes for:

self.tags = tag_list()
Sign up to request clarification or add additional context in comments.

Comments

2

This is a bit confusing as to what the expected behavior is supposed to be. But it looks like you need to create 20 instances of tag_info. Also you need to instantiate your instances using ().

class tag_info:
    def __init__(self):
        self.id = ""
        self.rssi = 0

class tag_list:
    def __init__(self):
        self.tag = [tag_info() for _ in range(20)]

class data_packet():
    def __init__(self):
        self.timestamp=0
        self.tags = tag_list()
        self.heading=0
        self.airt=0

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.