- How do I utilize generic pointers (void pointers) in NECTO IDE?
/* Project name:
* How do I utilize generic pointers in NECTO IDE?
* Copyright:
* (c) MIKROE, 2024.
* Description:
* - Example is meant for demonstrating void pointer
* (`void *` or generic pointer) functionality using mikroSDK 2.0.
* Library dependencies?
* - Make sure `Board` and `Driver.GPIO.Port` libraries are enabled
* in NECTO's Library Manager to ensure a successful build.
* How to test this code example?
* - Run debug Mode (F9 on your keyboard);
* - Analyze the usage of mikroSDK's implementation of
* void pointer (and subsequent type casting which is obligatory).
*/
// ------------------------------------------------------------------ INCLUDES
/**
* Any initialization code needed for MCU to function properly.
* Do not remove this line or clock might not be set correctly.
*/
#ifdef PREINIT_SUPPORTED
#include "preinit.h"
#endif
#include "board.h"
#include "generic_pointer.h"
// ----------------------------------------------------------------- USER CODE
// Function that works with a generic pointer to read sensor data
void read_sensor_data(uint8_t * __generic_ptr sensor_data, size_t data_size) {
// Cast the generic pointer to byte array
uint8_t* data = (uint8_t*)sensor_data;
// Simulate reading from a sensor and populating data
for (size_t i = 0; i < data_size; i++) {
data[i] = i * 2; // Example: populating data with dummy values
}
}
// Function to process sensor data (flexible with different sensor data types)
void process_sensor_data(void* __generic_ptr data, size_t data_size) {
// Example: process data (cast to uint8_t* for byte-by-byte processing)
uint8_t* byte_data = (uint8_t*)data;
printf_me("Processing sensor data:\n");
for (size_t i = 0; i < data_size; i++) {
printf_me("Data[%u] = %d\n", i, byte_data[i]);
}
}
int main(void)
{
/* Do not remove this line or clock might not be set correctly. */
#ifdef PREINIT_SUPPORTED
preinit();
#endif
uint8_t sensor_data[10]; // Example sensor data array
size_t data_size = sizeof(sensor_data);
// Read and process sensor data using generic pointers
read_sensor_data(sensor_data, data_size);
process_sensor_data(sensor_data, data_size);
return 0;
}
// ----------------------------------------------------------------------- END