Я использую Arduino (ESP32).
Попытка прочитать значение каждую миллисекунду и поместить его в очередь (для последующего усреднения). Эта очередь связана с указанным c выводом на ESP32.
Позже мне нужно поместить каждую из этих очередей (подключенных к каждому выводу в esp32) в отдельную очередь, сделав их подпоследовательностями.
Я запутался, где я должен использовать & и * и когда выделять память.
Я использую эту библиотеку: https://github.com/SMFSW/cQueue
На данный момент я создаю очереди следующим образом:
#include <cQueue.h>
// How many pins are attached?
#define arraySize 2
//Constant Variables
int pins[] {34, 39};
Queue_t qArray;
void setup() {
Serial.begin(115200);
delay(1000);
Serial.println("Started");
createQueues();
delay(1000);
printQInfo();
Serial.println("Printed Queue Info");
delay(1000);
}
void loop() {
delay(1000);
readIntoQueue();
delay(1000);
printQInfo();
}
/*
CREATES THE MAIN QUEUE AND ITS SUB-QUEUES AND INITIALIZES THEM.
*/
void createQueues() {
Serial.print("Initializing Main Queue: ");
q_init(&qArray, 20 * arraySize, 20, FIFO, true); // 20 is the size of a Queue with 5 float entries.
Serial.println(q_isInitialized(&qArray));
for (int i = 0; i < arraySize; i++) {
Serial.print("Entered createQueue Loop, Index: ");
Serial.println(i);
Queue_t* tempQ = (Queue_t*)malloc(sizeof(Queue_t)); //I need to allocate memory for each sub-queue here. Right?
q_init(tempQ, sizeof(float), 5, FIFO, true);
Serial.print("Subqueue initialized: ");
Serial.println(q_isInitialized(tempQ));
q_push(&qArray, tempQ);
Serial.print("Created sub-Queue, with size: ");
Serial.println(q_getCount(tempQ));
}
Serial.print("Initialized Main-Queue, with size: ");
Serial.println(q_getCount(&qArray));
}
/*
LOOPS THROUGH THE MAIN QUEUE AND ITS SUB-QUEUES, PRINTS THE VALUES (FLOATS) REGISTERED IN THE SUB-QUEUES
*/
void printQInfo() {
for (int X = 0; X < q_getCount(&qArray); X++) {
Queue_t tempQ; // This doesnt need to be allocated, since im just printing it straight away and dont mind it falling out of scope after operations are done.
float tempF; // This doesnt need to be allocated, since im just printing it straight away and dont mind it falling out of scope after operations are done.
q_peekIdx(&qArray, &tempQ, X); //Get the Queue from qArray index, and put it into temporary adressReference (I want this to be a pointer to the sub-queue created earlier, not a new address)
Serial.print("Queue: ");
Serial.print(X);
Serial.print(" Count of Queue: ");
Serial.print(q_getCount(&tempQ)); //This always returns 0?!?! Guessing its because im not really getting a pointer to the "real" subqueue or that something is wrong with how i am reading information into the queue.
Serial.print(" ");
Serial.print(" Values: ");
for (int i = 0; i < q_getCount(&tempQ); i++) { //Loop through referenced Queue and put the value to temporary float for printing. This also always returns 0, due to what i mentioned before.
q_peekIdx(&tempQ, &tempF, i);
Serial.print(", ");
Serial.print(tempF);
}
Serial.println();
}
}
/*
TAKES VALUES FROM ESP32PINS AND PUSHES THEM INTO THEIR RESPECTIVE QUEUES FOR LATER ACCESS.
*/
void readIntoQueue() {
for (int i = 0 ; i < arraySize; i++) {
Queue_t tempQ; //create an adress for a new temporary queue.
float* readValue = (float*)malloc(sizeof(float)); //Understood that i need to allocate memory for this not falling out of scope after operation is done.
q_peekIdx(&qArray, &tempQ, i); //put the sub-queue from the qArray into the Temporary Queue for reference. (Is this already a pointer??)
Serial.print("Reading into queue: ");
Serial.println(i);
*readValue = readPin(pins[i]); //Reads Resistance into readValue
Serial.print("Read Value from Pin: ");
Serial.print (pins[i]);
Serial.print (" Is : ");
Serial.print(*readValue);
Serial.println(" ");
Serial.print( "Push Sucess?: ");
Serial.println(q_push(&tempQ, readValue)); //Push the Read Value into the temporary Q reference. This feels strange.. i want to push into the original subqueue! This creates a new pointer to what?
Serial.println(q_getCount(&tempQ));
}
}
/*
READS VOLTAGE DROP OVER RESISTOR, CALCULATES AND RETURNS TOTAL RESISTANCE
*/
float readPin(int analogPin) {
int raw = 0;
float Vin = 3.3;
float Vout = 0;
float R1 = 470;
float R2 = 0;
float buffer = 0;
raw = analogRead(analogPin);
if (raw)
{
buffer = raw * Vin;
Vout = (buffer) / 4096.0;
buffer = (Vin / Vout) - 1;
R2 = R1 * buffer;
}
return R2;
}
Я хотел бы иметь несколько указателей (каламбур) на этом
мой вывод выглядит так:
> 11:31:27.817 -> Started
11:31:27.817 -> Initializing Main Queue: 1
11:31:27.817 -> Entered createQueue Loop, Index: 0
11:31:27.817 -> Subqueue initialized: 1
11:31:27.817 -> Created sub-Queue, with size: 0
11:31:27.817 -> Entered createQueue Loop, Index: 1
11:31:27.817 -> Subqueue initialized: 1
11:31:27.817 -> Created sub-Queue, with size: 0
11:31:27.817 -> Initialized Main-Queue, with size: 2
11:31:27.817 -> Created Queues
11:31:28.812 -> Filled Queues
11:31:29.809 -> Queue: 0 Count of Queue: 0 Values:
11:31:29.809 -> Queue: 1 Count of Queue: 0 Values:
11:31:29.809 -> Printed Queue Info
11:31:31.833 -> Reading into queue: 0
11:31:31.833 -> Read Value from Pin: 34 Is : 2655.19
11:31:31.833 -> Push Sucess?: 1
11:31:31.833 -> 1
11:31:31.833 -> Reading into queue: 1
11:31:31.833 -> Read Value from Pin: 39 Is : 2640.05
11:31:31.833 -> Push Sucess?: 1
11:31:31.833 -> 1
11:31:32.827 -> Queue: 0 Count of Queue: 0 Values:
11:31:32.827 -> Queue: 1 Count of Queue: 0 Values:
11:31:33.821 -> Reading into queue: 0
11:31:33.821 -> Read Value from Pin: 34 Is : 2645.08
11:31:33.821 -> Push Sucess?: 1
11:31:33.821 -> 1
11:31:33.821 -> Reading into queue: 1
11:31:33.821 -> Read Value from Pin: 39 Is : 2645.08
11:31:33.821 -> Push Sucess?: 1
11:31:33.821 -> 1
// Оскар