The value is not that you are making a gps tracker but how you can handle (tagged $abcd in this case) data via the Serial port |
I am actually working on that right now. I am finding out that it seems to be less a pipe and more a "soup". Arduino requests data by sending a uint8_t ID. Then it reads a uint16_t. I thought that was enough, that I am supposed to get the data I requested because on the arduino, I try to read the port after I write the ID to the port. The next uint16_t has to be the response. However, while the order of transmit on the PC side is correct, they are not received in the proper order on the Arduino. The only reliable way so far is to have the PC return the uint8_t ID with the uint16_t data. Then by doing a do while loop on the arduino, when I check the first byte, I know the next two bytes are correct.
Regarding the topic of this question. And there may be better solutions that I haven't considered. Basically I will have an array (perhaps a vector) of objects of SimRequest. This will be maybe 75 elements in size. However, I only probably am interested in 25 at the moment. So I decided to have a bool class member indicate which of the 75 I am interested in. As the arduino sends its request for data, I will mark the bool class member as active and increment a global counter of number of new data subscriptions requested by the arduino.
1 2 3 4 5 6 7 8 9
|
class SimRequest {
int id;
double * index; // index to array of double.
static double dvalue[100];
bool active=false;
int counter=0; //how many requests for this data so far
};
SimRequest::dvalue[] = {};
|
So on program start, I iterate over the 75 elements, pick out the ones where active==true and build a smaller vector containing maybe 25 objects. Then iterate over the vector of 25 elements and set their index pointer value to consecutive memory addresses of dvalue[] array.
Whenever a serial port requests data about a particular SimRequest object, I increment a counter in the object.
Whenever I finnish a data retrieval cycle (PC app requests data from game). I increment a global int num_cycles.
After a certain amount of time, if there are new subscriptions requested, I push that into the smaller vector. At the same time, I evaluate all the vector's elements and if an object's internal counter is below a certain threshold ( less than num_cycles), I delete that from the vector. I also zero out the object internal counter.
If I delete an element in the middle of the vector, what I was hoping for is a way that the elements after the deleted element would automatically change the memory address "SimRequest::index" is pointing to by one memory address up.
I think the solution will have to be, since I know which element to delete from the smaller vector, I need to null its index pointer, move to the next element and adjust its index upwards until I get to the last element. Then I can delete the element in the middle whose pointer I just nulled.
I am trying to avoid having to iterate over 75 objects whenever I need to make a subscription change.
Thanks,
Chris