SPHERE_MT3620/AirQuality5_demo/AvnetSphereAirQuality5/main.c
/// <summary>
///     Set up SIGTERM termination handler, initialize peripherals, and set up event handlers.
/// </summary>
/// <returns>0 on success, or -1 on failure</returns>

Set up SIGTERM termination handler, initialize peripherals, and set up event handlers.

0 on success, or -1 on failure
Set up SIGTERM termination handler initialize peripherals and set up event handlers . 0 on success or - 1 on failure
static int InitPeripheralsAndHandlers(void)
{
    struct sigaction action;
    memset(&action, 0, sizeof(struct sigaction));
    action.sa_handler = TerminationHandler;
    sigaction(SIGTERM, &action, NULL);
	init_gas_sensor(init_air_quality5, i2c_read, i2c_write, read_ready_pin);
	open_lcdmini(init_lcd, cs1, cs2, rst, fpwm, spi_tx, delay_us);
	lcd_set_contrast(50);
    epollFd = CreateEpollFd();
    if (epollFd < 0)
	{
        return -1;
    }
	for (int i = 0; i < twinArraySize; i++)
	{
		if (twinArray[i].twinGPIO != NO_GPIO_ASSOCIATED_WITH_TWIN) 
		{
			*twinArray[i].twinFd = -1;
			*twinArray[i].twinFd = (int)GPIO_OpenAsOutput(twinArray[i].twinGPIO, GPIO_OutputMode_PushPull, twinArray[i].active_high ? GPIO_Value_Low : GPIO_Value_High);
			if (*twinArray[i].twinFd < 0) 
			{
				Log_Debug("ERROR: Could not open LED %d: %s (%d).\n", twinArray[i].twinGPIO, strerror(errno), errno);
				return -1;
			}
		}
	}
	Log_Debug("Opening Starter Kit Button A as input.\n");
	buttonAGpioFd = GPIO_OpenAsInput(MT3620_RDB_BUTTON_A);
	if (buttonAGpioFd < 0)
	{
		Log_Debug("ERROR: Could not open button A GPIO: %s (%d).\n", strerror(errno), errno);
		return -1;
	}
	Log_Debug("Opening Starter Kit Button B as input.\n");
	buttonBGpioFd = GPIO_OpenAsInput(MT3620_RDB_BUTTON_B);
	if (buttonBGpioFd < 0)
	{
		Log_Debug("ERROR: Could not open button B GPIO: %s (%d).\n", strerror(errno), errno);
		return -1;
	}
	struct timespec buttonPressCheckPeriod = { 0, 1000000 };
	buttonPollTimerFd =
		CreateTimerFdAndAddToEpoll(epollFd, &buttonPressCheckPeriod, &buttonEventData, EPOLLIN);
	if (buttonPollTimerFd < 0) 
	{
		return -1;
	}
	struct timespec accelReadPeriod = { .tv_sec = ACCEL_READ_PERIOD_SECONDS,.tv_nsec = ACCEL_READ_PERIOD_NANO_SECONDS };
	static EventData accelEventData = { .eventHandler = &AcqTimerEventHandler };
	acquirePollTimerFd = CreateTimerFdAndAddToEpoll(epollFd, &accelReadPeriod, &accelEventData, EPOLLIN);
	if (acquirePollTimerFd < 0) 
	{
		return -1;
	}
	AzureIoT_SetDeviceTwinUpdateCallback(&deviceTwinChangedHandler);
    return 0;
}
static int InitPeripheralsAndHandlers ( void ) { struct sigaction action ; memset ( & action , 0 , sizeof ( struct sigaction ) ) ; action . sa_handler = TerminationHandler ; sigaction ( SIGTERM , & action , NULL ) ; init_gas_sensor ( init_air_quality5 , i2c_read , i2c_write , read_ready_pin ) ; open_lcdmini ( init_lcd , cs1 , cs2 , rst , fpwm , spi_tx , delay_us ) ; lcd_set_contrast ( 50 ) ; epollFd = CreateEpollFd ( ) ; if ( epollFd < 0 ) { return -1 ; } for ( int i = 0 ; i < twinArraySize ; i ++ ) { if ( twinArray [ i ] . twinGPIO != NO_GPIO_ASSOCIATED_WITH_TWIN ) { * twinArray [ i ] . twinFd = -1 ; * twinArray [ i ] . twinFd = ( int ) GPIO_OpenAsOutput ( twinArray [ i ] . twinGPIO , GPIO_OutputMode_PushPull , twinArray [ i ] . active_high ? GPIO_Value_Low : GPIO_Value_High ) ; if ( * twinArray [ i ] . twinFd < 0 ) { Log_Debug ( " \n " , twinArray [ i ] . twinGPIO , strerror ( errno ) , errno ) ; return -1 ; } } } Log_Debug ( " \n " ) ; buttonAGpioFd = GPIO_OpenAsInput ( MT3620_RDB_BUTTON_A ) ; if ( buttonAGpioFd < 0 ) { Log_Debug ( " \n " , strerror ( errno ) , errno ) ; return -1 ; } Log_Debug ( " \n " ) ; buttonBGpioFd = GPIO_OpenAsInput ( MT3620_RDB_BUTTON_B ) ; if ( buttonBGpioFd < 0 ) { Log_Debug ( " \n " , strerror ( errno ) , errno ) ; return -1 ; } struct timespec buttonPressCheckPeriod = { 0 , 1000000 } ; buttonPollTimerFd = CreateTimerFdAndAddToEpoll ( epollFd , & buttonPressCheckPeriod , & buttonEventData , EPOLLIN ) ; if ( buttonPollTimerFd < 0 ) { return -1 ; } struct timespec accelReadPeriod = { . tv_sec = ACCEL_READ_PERIOD_SECONDS , . tv_nsec = ACCEL_READ_PERIOD_NANO_SECONDS } ; static EventData accelEventData = { . eventHandler = & AcqTimerEventHandler } ; acquirePollTimerFd = CreateTimerFdAndAddToEpoll ( epollFd , & accelReadPeriod , & accelEventData , EPOLLIN ) ; if ( acquirePollTimerFd < 0 ) { return -1 ; } AzureIoT_SetDeviceTwinUpdateCallback ( & deviceTwinChangedHandler ) ; return 0 ; }
<summary>
Set up SIGTERM termination handler, initialize peripherals, and set up event handlers.
<summary > Set up SIGTERM termination handler initialize peripherals and set up event handlers .
// Init Air Quality 5  // Init LCD mini // Traverse the twin Array and for each GPIO item in the list open the file descriptor // Verify that this entry is a GPIO entry // For each item in the data structure, initialize the file descriptor and open the GPIO for output.  Initilize each GPIO to its specific inactive state. // Open button A GPIO as input // Open button B GPIO as input // Set up a timer to poll the buttons // Init the epoll interface to periodically run the AccelTimerEventHandler routine where we read the sensors // Define the period in the build_options.h file // event handler data structures. Only the event handler field needs to be populated. // Tell the system about the callback function that gets called when we receive a device twin update message from Azure