SPHERE_MT3620/AirQuality5_demo/AvnetSphereAirQuality5/main.c
/// <summary>
///     Handle button timer event: if the button is pressed, report the event to the IoT Hub.
/// </summary>

Handle button timer event: if the button is pressed, report the event to the IoT Hub.
Handle button timer event : if the button is pressed report the event to the IoT Hub .
static void ButtonTimerEventHandler(EventData *eventData)
{
	bool sendTelemetryButtonA = false;
	bool sendTelemetryButtonB = false;
	if (ConsumeTimerFdEvent(buttonPollTimerFd) != 0) 
	{
		terminationRequired = true;
		return;
	}
	GPIO_Value_Type newButtonAState;
	int result = GPIO_GetValue(buttonAGpioFd, &newButtonAState);
	if (result != 0) 
	{
		Log_Debug("ERROR: Could not read button GPIO: %s (%d).\n", strerror(errno), errno);
		terminationRequired = true;
		return;
	}
	if (newButtonAState != buttonAState) 
	{
		if (newButtonAState == GPIO_Value_Low) 
		{
			Log_Debug("Button A pressed!\n");
			sendTelemetryButtonA = true;
		}
		else {
			Log_Debug("Button A released!\n");
		}
		buttonAState = newButtonAState;
	}
	GPIO_Value_Type newButtonBState;
	result = GPIO_GetValue(buttonBGpioFd, &newButtonBState);
	if (result != 0)
	{
		Log_Debug("ERROR: Could not read button GPIO: %s (%d).\n", strerror(errno), errno);
		terminationRequired = true;
		return;
	}
	if (newButtonBState != buttonBState) 
	{
		if (newButtonBState == GPIO_Value_Low) 
		{
			Log_Debug("Button B pressed!\n");
			sendTelemetryButtonB = true;
		}
		else 
		{
			Log_Debug("Button B released!\n");
		}
		buttonBState = newButtonBState;
	}
	if (sendTelemetryButtonA || sendTelemetryButtonB) 
	{
		char *pjsonBuffer = (char *)malloc(JSON_BUFFER_SIZE);
		if (pjsonBuffer == NULL) 
		{
			Log_Debug("ERROR: not enough memory to send telemetry");
		}
		if (sendTelemetryButtonA)
		{
			snprintf(pjsonBuffer, JSON_BUFFER_SIZE, cstrButtonTelemetryJson, "buttonA", newButtonAState);
			Log_Debug("\n[Info] Sending telemetry %s\n", pjsonBuffer);
			AzureIoT_SendMessage(pjsonBuffer);
		}
		if (sendTelemetryButtonB)
		{
			snprintf(pjsonBuffer, JSON_BUFFER_SIZE, cstrButtonTelemetryJson, "buttonB", newButtonBState);
			Log_Debug("\n[Info] Sending telemetry %s\n", pjsonBuffer);
			AzureIoT_SendMessage(pjsonBuffer);
		}
		free(pjsonBuffer);
	}
}
static void ButtonTimerEventHandler ( EventData * eventData ) { bool sendTelemetryButtonA = false ; bool sendTelemetryButtonB = false ; if ( ConsumeTimerFdEvent ( buttonPollTimerFd ) != 0 ) { terminationRequired = true ; return ; } GPIO_Value_Type newButtonAState ; int result = GPIO_GetValue ( buttonAGpioFd , & newButtonAState ) ; if ( result != 0 ) { Log_Debug ( " \n " , strerror ( errno ) , errno ) ; terminationRequired = true ; return ; } if ( newButtonAState != buttonAState ) { if ( newButtonAState == GPIO_Value_Low ) { Log_Debug ( " \n " ) ; sendTelemetryButtonA = true ; } else { Log_Debug ( " \n " ) ; } buttonAState = newButtonAState ; } GPIO_Value_Type newButtonBState ; result = GPIO_GetValue ( buttonBGpioFd , & newButtonBState ) ; if ( result != 0 ) { Log_Debug ( " \n " , strerror ( errno ) , errno ) ; terminationRequired = true ; return ; } if ( newButtonBState != buttonBState ) { if ( newButtonBState == GPIO_Value_Low ) { Log_Debug ( " \n " ) ; sendTelemetryButtonB = true ; } else { Log_Debug ( " \n " ) ; } buttonBState = newButtonBState ; } if ( sendTelemetryButtonA || sendTelemetryButtonB ) { char * pjsonBuffer = ( char * ) malloc ( JSON_BUFFER_SIZE ) ; if ( pjsonBuffer == NULL ) { Log_Debug ( " " ) ; } if ( sendTelemetryButtonA ) { snprintf ( pjsonBuffer , JSON_BUFFER_SIZE , cstrButtonTelemetryJson , " " , newButtonAState ) ; Log_Debug ( " \n \n " , pjsonBuffer ) ; AzureIoT_SendMessage ( pjsonBuffer ) ; } if ( sendTelemetryButtonB ) { snprintf ( pjsonBuffer , JSON_BUFFER_SIZE , cstrButtonTelemetryJson , " " , newButtonBState ) ; Log_Debug ( " \n \n " , pjsonBuffer ) ; AzureIoT_SendMessage ( pjsonBuffer ) ; } free ( pjsonBuffer ) ; } }
<summary>
Handle button timer event: if the button is pressed, report the event to the IoT Hub.
<summary > Handle button timer event : if the button is pressed report the event to the IoT Hub .
// Check for button A press // If the A button has just been pressed, send a telemetry message // The button has GPIO_Value_Low when pressed and GPIO_Value_High when released // Update the static variable to use next time we enter this routine // Check for button B press // If the B button has just been pressed/released, send a telemetry message // The button has GPIO_Value_Low when pressed and GPIO_Value_High when released // Send Telemetry here // Update the static variable to use next time we enter this routine // If either button was pressed, then enter the code to send the telemetry message // construct the telemetry message  for Button A // construct the telemetry message for Button B