#include <stdio.h>
#include <stdlib.h>
#include "ItalaApiC/ItalaC.h"
#ifdef _WIN32
#include <windows.h>
#define SLEEP_MS(ms) Sleep(ms)
#else
#include <unistd.h>
#define SLEEP_MS(ms) usleep(ms * 1000)
#endif
#define TEMPERATURE_READS 10
#define POLL_FREQ_MS 750
ItalaError ErrorManager(ItalaError e) {
size_t sizeMessage = 255;
char* message = (char*)malloc(sizeof(char) * sizeMessage);
ERR_GetLastErrorMessage(message, &sizeMessage);
printf("\nITALA ERROR (%d):\t%s, %zu char", ERR_GetLastErrorCode(), message, sizeMessage);
free(message);
return e;
}
// Define a callback function which can be registered to a node with GenApi.
// When the target node value changes, the callback is executed.
void OnDeviceTemperatureChanged(H_NODE hNode, void* pContext) {
double value = 0;
NODE_FloatGetValue(hNode, &value);
printf("\tOnDeviceTemperatureChanged function called.\n");
printf("\tDevice temperature node changed. New value is %f C\370 \n\n", value);
}
int main(int argc, char** argv) {
printf("***** NodeMapPolling example started. *****\n");
ItalaError error = ItalaErrorSuccess;
error = SYS_Initialize();
if (error) return ErrorManager(error);
size_t devicesInfoSize = 0;
error = SYS_EnumerateDevices(700);
if (error) return ErrorManager(error);
error = SYS_GetDeviceCount(&devicesInfoSize);
if (error) return ErrorManager(error);
if (devicesInfoSize == 0) {
printf("No devices found. Example canceled.\n");
return ItalaErrorError;
}
DeviceInfo deviceInfo;
error = SYS_GetDeviceByIndex(0, &deviceInfo);
if (error) return ErrorManager(error);
if (deviceInfo.AccessStatus != AvailableReadWrite) {
printf("Target device is unaccessible in RW mode. Example canceled.\n");
return ItalaErrorError;
}
H_DEVICE hDevice = NULL;
error = SYS_CreateDevice(deviceInfo, &hDevice);
if (error) return ErrorManager(error);
printf("First device found is initialized.\n\n");
H_NODEMAP hNodeMap = NULL;
error = DEV_GetNodeMap(hDevice, &hNodeMap);
if (error) return ErrorManager(error);
H_NODE hNodeDeviceTemperature = NULL;
error = NODEMAP_GetNode(hNodeMap, "DeviceTemperature", &hNodeDeviceTemperature);
if (error) return ErrorManager(error);
bool isReadable = false;
error = IsNodeReadable(hNodeDeviceTemperature, &isReadable);
if (error) return ErrorManager(error);
if (!isReadable) {
printf("DeviceTemperature feature not available. Aborting.\n");
return ItalaErrorError;
}
// Register the OnDeviceTemperatureChanged callback to the DeviceTemperature node
H_NODECALLBACK hNodeCallbackDeviceTemperature = NULL;
error = NODE_RegisterCallback(hNodeDeviceTemperature, OnDeviceTemperatureChanged, NULL,
&hNodeCallbackDeviceTemperature);
if (error) return ErrorManager(error);
printf("OnDeviceTemperatureChanged registered to DeviceTemperature node.\n\n");
for (int i = 0; i < TEMPERATURE_READS; i++) {
// Sleep for a bit..
SLEEP_MS(POLL_FREQ_MS);
printf("\tPolling temperature..\n");
// And then poll nodes of the map. At this point, all nodes with a polling time
// shorter than the elapsed time are updated and their callback executed, if any.
error = NODEMAP_Poll(hNodeMap, POLL_FREQ_MS);
if (error) return ErrorManager(error);
}
error = NODE_DeregisterCallback(hNodeDeviceTemperature, hNodeCallbackDeviceTemperature);
if (error) return ErrorManager(error);
error = DEV_Dispose(hDevice);
hDevice = NULL;
if (error) return ErrorManager(error);
printf("Device instance disposed.\n");
error = SYS_Dispose();
if (error) return ErrorManager(error);
printf("System instance disposed.\n");
return 0;
}